Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/lesson/backup/moodle1/lib.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 
00027 defined('MOODLE_INTERNAL') || die();
00028 
00032 class moodle1_mod_lesson_handler extends moodle1_mod_handler {
00033     // @var array of answers, when there are more that 4 answers, we need to fix <jumpto>.
00034     protected $answers;
00035 
00036     // @var stdClass a page object of the current page
00037     protected $page;
00038     // @var array of page objects to store entire pages, to help generate nextpageid and prevpageid in data
00039     protected $pages;
00040     // @var int a page id (previous)
00041     protected $prevpageid = 0;
00042 
00044     protected $fileman = null;
00045 
00047     protected $moduleid = null;
00048 
00062     public function get_paths() {
00063         return array(
00064             new convert_path(
00065                 'lesson', '/MOODLE_BACKUP/COURSE/MODULES/MOD/LESSON',
00066                 array(
00067                     'renamefields' => array(
00068                         'usegrademax' => 'usemaxgrade',
00069                     ),
00070                 )
00071             ),
00072             new convert_path(
00073                 'lesson_page', '/MOODLE_BACKUP/COURSE/MODULES/MOD/LESSON/PAGES/PAGE',
00074                 array(
00075                     'newfields' => array(
00076                         'contentsformat' => FORMAT_MOODLE,
00077                         'nextpageid' => 0, //set to default to the next sequencial page in process_lesson_page()
00078                         'prevpageid' => 0
00079                     ),
00080                 )
00081             ),
00082             new convert_path(
00083                 'lesson_pages', '/MOODLE_BACKUP/COURSE/MODULES/MOD/LESSON/PAGES'
00084             ),
00085             new convert_path(
00086                 'lesson_answer', '/MOODLE_BACKUP/COURSE/MODULES/MOD/LESSON/PAGES/PAGE/ANSWERS/ANSWER',
00087                 array(
00088                     'newfields' => array(
00089                         'answerformat' => 0,
00090                         'responseformat' => 0,
00091                     ),
00092                     'renamefields' => array(
00093                         'answertext' => 'answer_text',
00094                     ),
00095                 )
00096             )
00097         );
00098     }
00099 
00104     public function process_lesson($data) {
00105 
00106         // get the course module id and context id
00107         $instanceid     = $data['id'];
00108         $cminfo         = $this->get_cminfo($instanceid);
00109         $this->moduleid = $cminfo['id'];
00110         $contextid      = $this->converter->get_contextid(CONTEXT_MODULE, $this->moduleid);
00111 
00112         // get a fresh new file manager for this instance
00113         $this->fileman = $this->converter->get_file_manager($contextid, 'mod_lesson');
00114 
00115         // migrate referenced local media files
00116         if (!empty($data['mediafile']) and strpos($data['mediafile'], '://') === false) {
00117             $this->fileman->filearea = 'mediafile';
00118             $this->fileman->itemid   = 0;
00119             try {
00120                 $this->fileman->migrate_file('course_files/'.$data['mediafile']);
00121             } catch (moodle1_convert_exception $e) {
00122                 // the file probably does not exist
00123                 $this->log('error migrating lesson mediafile', backup::LOG_WARNING, 'course_files/'.$data['mediafile']);
00124             }
00125         }
00126 
00127         // start writing lesson.xml
00128         $this->open_xml_writer("activities/lesson_{$this->moduleid}/lesson.xml");
00129         $this->xmlwriter->begin_tag('activity', array('id' => $instanceid, 'moduleid' => $this->moduleid,
00130             'modulename' => 'lesson', 'contextid' => $contextid));
00131         $this->xmlwriter->begin_tag('lesson', array('id' => $instanceid));
00132 
00133         foreach ($data as $field => $value) {
00134             if ($field <> 'id') {
00135                 $this->xmlwriter->full_tag($field, $value);
00136             }
00137         }
00138 
00139         return $data;
00140     }
00141 
00142     public function on_lesson_pages_start() {
00143         $this->xmlwriter->begin_tag('pages');
00144     }
00145 
00150     public function process_lesson_page($data) {
00151         global $CFG;
00152 
00153         // replay the upgrade step 2009120801
00154         if ($CFG->texteditors !== 'textarea') {
00155             $data['contents'] = text_to_html($data['contents'], false, false, true);
00156             $data['contentsformat'] = FORMAT_HTML;
00157         }
00158 
00159         // store page in pages
00160         $this->page = new stdClass();
00161         $this->page->id = $data['pageid'];
00162         unset($data['pageid']);
00163         $this->page->data = $data;
00164     }
00165 
00170     public function process_lesson_answer($data) {
00171 
00172         // replay the upgrade step 2010072003
00173         $flags = intval($data['flags']);
00174         if ($flags & 1) {
00175             $data['answer_text']  = text_to_html($data['answer_text'], false, false, true);
00176             $data['answerformat'] = FORMAT_HTML;
00177         }
00178         if ($flags & 2) {
00179             $data['response']       = text_to_html($data['response'], false, false, true);
00180             $data['responseformat'] = FORMAT_HTML;
00181         }
00182 
00183         // buffer for conversion of <jumpto> in line with
00184         // upgrade step 2010121400 from mod/lesson/db/upgrade.php
00185         $this->answers[] = $data;
00186     }
00187 
00188     public function on_lesson_page_end() {
00189         $this->page->answers = $this->answers;
00190         $this->pages[] = $this->page;
00191 
00192         $firstbatch = count($this->pages) > 2;
00193         $nextbatch = count($this->pages) > 1 && $this->prevpageid != 0;
00194 
00195         if ( $firstbatch || $nextbatch ) { //we can write out 1 page atleast
00196             if ($this->prevpageid == 0) {
00197                 // start writing with n-2 page (relative to this on_lesson_page_end() call)
00198                 $pg1 = $this->pages[1];
00199                 $pg0 = $this->pages[0];
00200                 $this->write_single_page_xml($pg0, 0, $pg1->id);
00201                 $this->prevpageid = $pg0->id;
00202                 array_shift($this->pages); //bye bye page0
00203             }
00204 
00205             $pg1 = $this->pages[0];
00206             // write pg1 referencing prevpageid and pg2
00207             $pg2 = $this->pages[1];
00208             $this->write_single_page_xml($pg1, $this->prevpageid, $pg2->id);
00209             $this->prevpageid = $pg1->id;
00210             array_shift($this->pages); //throw written n-1th page
00211         }
00212         $this->answers = array(); //clear answers for the page ending. do not unset, object property will be missing.
00213         $this->page = null;
00214     }
00215 
00216     public function on_lesson_pages_end() {
00217         if ($this->pages) {
00218             if (isset($this->pages[1])) { // write the case of only 2 pages.
00219                 $this->write_single_page_xml($this->pages[0], $this->prevpageid, $this->pages[1]->id);
00220                 $this->prevpageid = $this->pages[0]->id;
00221                 array_shift($this->pages);
00222             }
00223             //write the remaining (first/last) single page
00224             $this->write_single_page_xml($this->pages[0], $this->prevpageid, 0);
00225         }
00226         $this->xmlwriter->end_tag('pages');
00227         //reset
00228         unset($this->pages);
00229         $this->prevpageid = 0;
00230 
00231     }
00232 
00236     public function on_lesson_end() {
00237         // finish writing lesson.xml
00238         $this->xmlwriter->end_tag('lesson');
00239         $this->xmlwriter->end_tag('activity');
00240         $this->close_xml_writer();
00241 
00242         // write inforef.xml
00243         $this->open_xml_writer("activities/lesson_{$this->moduleid}/inforef.xml");
00244         $this->xmlwriter->begin_tag('inforef');
00245         $this->xmlwriter->begin_tag('fileref');
00246         foreach ($this->fileman->get_fileids() as $fileid) {
00247             $this->write_xml('file', array('id' => $fileid));
00248         }
00249         $this->xmlwriter->end_tag('fileref');
00250         $this->xmlwriter->end_tag('inforef');
00251         $this->close_xml_writer();
00252     }
00253 
00260     protected function write_single_page_xml($page, $prevpageid=0, $nextpageid=0) {
00261         //mince nextpageid and prevpageid
00262         $page->data['nextpageid'] = $nextpageid;
00263         $page->data['prevpageid'] = $prevpageid;
00264 
00265         // write out each page data
00266         $this->xmlwriter->begin_tag('page', array('id' => $page->id));
00267 
00268         foreach ($page->data as $field => $value) {
00269             $this->xmlwriter->full_tag($field, $value);
00270         }
00271 
00272         //effectively on_lesson_answers_end(), where we write out answers for current page.
00273         $answers = $page->answers;
00274 
00275         $this->xmlwriter->begin_tag('answers');
00276 
00277         $numanswers = count($answers);
00278         if ($numanswers) { //if there are any answers (possible there are none!)
00279             if ($numanswers > 3 && $page->data['qtype'] == 5) { //fix only jumpto only for matching question types.
00280                 if ($answers[0]['jumpto'] !== '0' || $answers[1]['jumpto'] !== '0') {
00281                     if ($answers[2]['jumpto'] !== '0') {
00282                         $answers[0]['jumpto'] = $answers[2]['jumpto'];
00283                         $answers[2]['jumpto'] = '0';
00284                     }
00285                     if ($answers[3]['jumpto'] !== '0') {
00286                         $answers[1]['jumpto'] = $answers[3]['jumpto'];
00287                         $answers[3]['jumpto'] = '0';
00288                     }
00289                 }
00290             }
00291             foreach ($answers as $data) {
00292                 $this->write_xml('answer', $data, array('/answer/id'));
00293             }
00294         }
00295 
00296         $this->xmlwriter->end_tag('answers');
00297 
00298         // answers is now closed for current page. Ending the page.
00299         $this->xmlwriter->end_tag('page');
00300     }
00301 }
 All Data Structures Namespaces Files Functions Variables Enumerations