Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/assignment/locallib.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // this file contains all the functions that aren't needed by core moodle
00019 // but start becoming required once we're actually inside the assignment module.
00020 
00021 require_once($CFG->dirroot . '/mod/assignment/lib.php');
00022 require_once($CFG->libdir . '/portfolio/caller.php');
00023 
00029 class assignment_portfolio_caller extends portfolio_module_caller_base {
00030 
00034     private $assignment;
00035 
00039     private $assignmentfile;
00040 
00042     protected $submissionid;
00043 
00047     protected $fileid;
00048 
00049     public static function expected_callbackargs() {
00050         return array(
00051             'id'           => true,
00052             'submissionid' => false,
00053             'fileid'       => false,
00054         );
00055     }
00056 
00064     public function load_data() {
00065         global $DB, $CFG;
00066 
00067         if (! $this->cm = get_coursemodule_from_id('assignment', $this->id)) {
00068             throw new portfolio_caller_exception('invalidcoursemodule');
00069         }
00070 
00071         if (! $assignment = $DB->get_record("assignment", array("id"=>$this->cm->instance))) {
00072             throw new portfolio_caller_exception('invalidid', 'assignment');
00073         }
00074 
00075         $this->assignmentfile = '/mod/assignment/type/' . $assignment->assignmenttype . '/assignment.class.php';
00076         require_once($CFG->dirroot . $this->assignmentfile);
00077         $assignmentclass = "assignment_$assignment->assignmenttype";
00078 
00079         $this->assignment = new $assignmentclass($this->cm->id, $assignment, $this->cm);
00080 
00081         if (!$this->assignment->portfolio_exportable()) {
00082             throw new portfolio_caller_exception('notexportable', 'portfolio', $this->get_return_url());
00083         }
00084 
00085         if (is_callable(array($this->assignment, 'portfolio_load_data'))) {
00086             return $this->assignment->portfolio_load_data($this);
00087         }
00088 
00089         if (empty($this->fileid)) {
00090             if (empty($this->submissionid)) {
00091                 throw new portfolio_caller_exception('invalidfileandsubmissionid', 'mod_assignment');
00092             }
00093 
00094             if (! $submission = $DB->get_record('assignment_submissions', array('assignment'=>$assignment->id, 'id'=>$this->submissionid))) {
00095                 throw new portfolio_caller_exception('invalidsubmissionid', 'mod_assignment');
00096             }
00097 
00098             $submissionid = $submission->id;
00099 
00100         } else {
00101             // once we know the file id, we do not need the submission
00102             $submissionid = null;
00103         }
00104 
00105         $this->set_file_and_format_data($this->fileid, $this->assignment->context->id, 'mod_assignment', 'submission', $submissionid, 'timemodified', false);
00106     }
00107 
00108     public function prepare_package() {
00109         global $CFG, $DB;
00110         if (is_callable(array($this->assignment, 'portfolio_prepare_package'))) {
00111             return $this->assignment->portfolio_prepare_package($this->exporter, $this->user);
00112         }
00113         if ($this->exporter->get('formatclass') == PORTFOLIO_FORMAT_LEAP2A) {
00114             $leapwriter = $this->exporter->get('format')->leap2a_writer();
00115             $files = array();
00116             if ($this->singlefile) {
00117                 $files[] = $this->singlefile;
00118             } elseif ($this->multifiles) {
00119                 $files = $this->multifiles;
00120             } else {
00121                 throw new portfolio_caller_exception('invalidpreparepackagefile', 'portfolio', $this->get_return_url());
00122             }
00123             $baseid = 'assignment' . $this->assignment->assignment->assignmenttype . $this->assignment->assignment->id . 'submission';
00124             $entryids = array();
00125             foreach ($files as $file) {
00126                 $entry = new portfolio_format_leap2a_file($file->get_filename(), $file);
00127                 $entry->author = $this->user;
00128                 $leapwriter->add_entry($entry);
00129                 $this->exporter->copy_existing_file($file);
00130                 $entryids[] = $entry->id;
00131             }
00132             if (count($files) > 1) {
00133                 // if we have multiple files, they should be grouped together into a folder
00134                 $entry = new portfolio_format_leap2a_entry($baseid . 'group', $this->assignment->assignment->name, 'selection');
00135                 $leapwriter->add_entry($entry);
00136                 $leapwriter->make_selection($entry, $entryids, 'Folder');
00137             }
00138             return $this->exporter->write_new_file($leapwriter->to_xml(), $this->exporter->get('format')->manifest_name(), true);
00139         }
00140         return $this->prepare_package_file();
00141     }
00142 
00143     public function get_sha1() {
00144         global $CFG;
00145         if (is_callable(array($this->assignment, 'portfolio_get_sha1'))) {
00146             return $this->assignment->portfolio_get_sha1($this);
00147         }
00148         return $this->get_sha1_file();
00149     }
00150 
00151     public function expected_time() {
00152         if (is_callable(array($this->assignment, 'portfolio_get_expected_time'))) {
00153             return $this->assignment->portfolio_get_expected_time();
00154         }
00155         return $this->expected_time_file();
00156     }
00157 
00158     public function check_permissions() {
00159         $context = get_context_instance(CONTEXT_MODULE, $this->assignment->cm->id);
00160         return has_capability('mod/assignment:exportownsubmission', $context);
00161     }
00162 
00163     public function __wakeup() {
00164         global $CFG;
00165         if (empty($CFG)) {
00166             return true; // too early yet
00167         }
00168         require_once($CFG->dirroot . $this->assignmentfile);
00169         $this->assignment = unserialize(serialize($this->assignment));
00170     }
00171 
00172     public static function display_name() {
00173         return get_string('modulename', 'assignment');
00174     }
00175 
00176     public static function base_supported_formats() {
00177         return array(PORTFOLIO_FORMAT_FILE, PORTFOLIO_FORMAT_LEAP2A);
00178     }
00179 }
 All Data Structures Namespaces Files Functions Variables Enumerations