|
Moodle
2.2.1
http://www.collinsharper.com
|
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 00028 defined('MOODLE_INTERNAL') || die(); 00029 00033 class moodle1_mod_assignment_handler extends moodle1_mod_handler { 00034 00036 protected $fileman = null; 00037 00039 protected $moduleid = null; 00040 00042 private $currentsubpluginname = null; 00043 00045 private $subpluginhandlers = null; 00046 00060 public function get_paths() { 00061 return array( 00062 new convert_path( 00063 'assignment', '/MOODLE_BACKUP/COURSE/MODULES/MOD/ASSIGNMENT', 00064 array( 00065 'renamefields' => array( 00066 'description' => 'intro', 00067 'format' => 'introformat', 00068 ) 00069 ) 00070 ) 00071 //@todo process user data 00072 //new convert_path('assignment_submission', '/MOODLE_BACKUP/COURSE/MODULES/MOD/ASSIGNMENT/SUBMISSIONS/SUBMISSION') 00073 ); 00074 } 00075 00080 public function process_assignment($data) { 00081 // get the course module id and context id 00082 $instanceid = $data['id']; 00083 $cminfo = $this->get_cminfo($instanceid); 00084 $this->moduleid = $cminfo['id']; 00085 $contextid = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid); 00086 00087 //store assignment type for possible subplugin conversions. 00088 $this->currentsubpluginname = $data['assignmenttype']; 00089 00090 // get a fresh new file manager for this instance 00091 $this->fileman = $this->converter->get_file_manager($contextid, 'mod_assignment'); 00092 00093 // convert course files embedded into the intro 00094 $this->fileman->filearea = 'intro'; 00095 $this->fileman->itemid = 0; 00096 $data['intro'] = moodle1_converter::migrate_referenced_files($data['intro'], $this->fileman); 00097 00098 // start writing assignment.xml 00099 $this->open_xml_writer("activities/assignment_{$this->moduleid}/assignment.xml"); 00100 $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid, 00101 'modulename' => 'assignment', 'contextid' => $contextid)); 00102 $this->xmlwriter->begin_tag('assignment', array('id' => $instanceid)); 00103 00104 foreach ($data as $field => $value) { 00105 if ($field <> 'id') { 00106 $this->xmlwriter->full_tag($field, $value); 00107 } 00108 } 00109 00110 //after writing the assignment type element, let the subplugin add on whatever it wants. 00111 $this->handle_assignment_subplugin($data); 00112 00113 $this->xmlwriter->begin_tag('submissions'); 00114 00115 return $data; 00116 } 00117 00122 public function process_assignment_submission($data) { 00123 //@todo process user data 00124 //$this->write_xml('submission', $data, array('/submission/id')); 00125 } 00126 00131 public function handle_assignment_subplugin($data) { 00132 $handler = $this->get_subplugin_handler($this->currentsubpluginname); 00133 $this->log('Instantiated assignment subplugin handler for '.$this->currentsubpluginname.'.', backup::LOG_DEBUG); 00134 $handler->use_xml_writer($this->xmlwriter); 00135 00136 $this->log('Processing assignment subplugin handler callback for '.$this->currentsubpluginname.'.', backup::LOG_DEBUG); 00137 $handler->append_subplugin_data($data); 00138 } 00139 00143 public function on_assignment_end() { 00144 // finish writing assignment.xml 00145 $this->xmlwriter->end_tag('submissions'); 00146 $this->xmlwriter->end_tag('assignment'); 00147 $this->xmlwriter->end_tag('activity'); 00148 $this->close_xml_writer(); 00149 00150 // write inforef.xml 00151 $this->open_xml_writer("activities/assignment_{$this->moduleid}/inforef.xml"); 00152 $this->xmlwriter->begin_tag('inforef'); 00153 $this->xmlwriter->begin_tag('fileref'); 00154 foreach ($this->fileman->get_fileids() as $fileid) { 00155 $this->write_xml('file', array('id' => $fileid)); 00156 } 00157 $this->xmlwriter->end_tag('fileref'); 00158 $this->xmlwriter->end_tag('inforef'); 00159 $this->close_xml_writer(); 00160 } 00161 00163 00171 protected function get_subplugin_handler($subplugin) { 00172 global $CFG; // we include other files here 00173 00174 if (is_null($this->subpluginhandlers)) { 00175 $this->subpluginhandlers = array(); 00176 $subplugins = get_plugin_list('assignment'); 00177 foreach ($subplugins as $name => $dir) { 00178 $handlerfile = $dir.'/backup/moodle1/lib.php'; 00179 $handlerclass = "moodle1_mod_assignment_{$name}_subplugin_handler"; 00180 if (!file_exists($handlerfile)) { 00181 continue; 00182 } 00183 require_once($handlerfile); 00184 00185 if (!class_exists($handlerclass)) { 00186 throw new moodle1_convert_exception('missing_handler_class', $handlerclass); 00187 } 00188 $this->log('preparing assignment subplugin handler', backup::LOG_DEBUG, $handlerclass); 00189 $this->subpluginhandlers[$name] = new $handlerclass($this, $name); 00190 if (!$this->subpluginhandlers[$name] instanceof moodle1_assignment_subplugin_handler) { 00191 throw new moodle1_convert_exception('wrong_handler_class', get_class($this->subpluginhandlers[$name])); 00192 } 00193 } 00194 } 00195 00196 if (!isset($this->subpluginhandlers[$subplugin])) { 00197 throw new moodle1_convert_exception('unsupported_subplugin', 'assignment_'.$subplugin); 00198 } 00199 00200 return $this->subpluginhandlers[$subplugin]; 00201 } 00202 } 00203 00204 00209 abstract class moodle1_assignment_subplugin_handler extends moodle1_submod_handler { 00210 00215 public function __construct(moodle1_mod_handler $assignmenthandler, $subpluginname) { 00216 parent::__construct($assignmenthandler, 'assignment', $subpluginname); 00217 } 00218 00224 public function use_xml_writer(xml_writer $xmlwriter) { 00225 $this->xmlwriter = $xmlwriter; 00226 } 00227 00233 public function append_subplugin_data($data) { 00234 // an example that does nothing - you'll do nothing if you don't overide it 00235 return false; 00236 00237 //you will probably want to do stuff with $this->xmlwriter here (within your overridden method) to write plugin specific data. 00238 } 00239 }