forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlerlib.php
2171 lines (1865 loc) · 80.8 KB
/
handlerlib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines Moodle 1.9 backup conversion handlers
*
* Handlers are classes responsible for the actual conversion work. Their logic
* is similar to the functionality provided by steps in plan based restore process.
*
* @package backup-convert
* @subpackage moodle1
* @copyright 2011 David Mudrak <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/backup/util/xml/xml_writer.class.php');
require_once($CFG->dirroot . '/backup/util/xml/output/xml_output.class.php');
require_once($CFG->dirroot . '/backup/util/xml/output/file_xml_output.class.php');
/**
* Handlers factory class
*/
abstract class moodle1_handlers_factory {
/**
* @param moodle1_converter the converter requesting the converters
* @return list of all available conversion handlers
*/
public static function get_handlers(moodle1_converter $converter) {
$handlers = array(
new moodle1_root_handler($converter),
new moodle1_info_handler($converter),
new moodle1_course_header_handler($converter),
new moodle1_course_outline_handler($converter),
new moodle1_roles_definition_handler($converter),
new moodle1_question_bank_handler($converter),
new moodle1_scales_handler($converter),
new moodle1_outcomes_handler($converter),
new moodle1_gradebook_handler($converter),
);
$handlers = array_merge($handlers, self::get_plugin_handlers('mod', $converter));
$handlers = array_merge($handlers, self::get_plugin_handlers('block', $converter));
// make sure that all handlers have expected class
foreach ($handlers as $handler) {
if (!$handler instanceof moodle1_handler) {
throw new moodle1_convert_exception('wrong_handler_class', get_class($handler));
}
}
return $handlers;
}
/// public API ends here ///////////////////////////////////////////////////
/**
* Runs through all plugins of a specific type and instantiates their handlers
*
* @todo ask mod's subplugins
* @param string $type the plugin type
* @param moodle1_converter $converter the converter requesting the handler
* @throws moodle1_convert_exception
* @return array of {@link moodle1_handler} instances
*/
protected static function get_plugin_handlers($type, moodle1_converter $converter) {
global $CFG;
$handlers = array();
$plugins = core_component::get_plugin_list($type);
foreach ($plugins as $name => $dir) {
$handlerfile = $dir . '/backup/moodle1/lib.php';
$handlerclass = "moodle1_{$type}_{$name}_handler";
if (file_exists($handlerfile)) {
require_once($handlerfile);
} elseif ($type == 'block') {
$handlerclass = "moodle1_block_generic_handler";
} else {
continue;
}
if (!class_exists($handlerclass)) {
throw new moodle1_convert_exception('missing_handler_class', $handlerclass);
}
$handlers[] = new $handlerclass($converter, $type, $name);
}
return $handlers;
}
}
/**
* Base backup conversion handler
*/
abstract class moodle1_handler implements loggable {
/** @var moodle1_converter */
protected $converter;
/**
* @param moodle1_converter $converter the converter that requires us
*/
public function __construct(moodle1_converter $converter) {
$this->converter = $converter;
}
/**
* @return moodle1_converter the converter that required this handler
*/
public function get_converter() {
return $this->converter;
}
/**
* Log a message using the converter's logging mechanism
*
* @param string $message message text
* @param int $level message level {@example backup::LOG_WARNING}
* @param null|mixed $a additional information
* @param null|int $depth the message depth
* @param bool $display whether the message should be sent to the output, too
*/
public function log($message, $level, $a = null, $depth = null, $display = false) {
$this->converter->log($message, $level, $a, $depth, $display);
}
}
/**
* Base backup conversion handler that generates an XML file
*/
abstract class moodle1_xml_handler extends moodle1_handler {
/** @var null|string the name of file we are writing to */
protected $xmlfilename;
/** @var null|xml_writer */
protected $xmlwriter;
/**
* Opens the XML writer - after calling, one is free to use $xmlwriter
*
* @param string $filename XML file name to write into
* @return void
*/
protected function open_xml_writer($filename) {
if (!is_null($this->xmlfilename) and $filename !== $this->xmlfilename) {
throw new moodle1_convert_exception('xml_writer_already_opened_for_other_file', $this->xmlfilename);
}
if (!$this->xmlwriter instanceof xml_writer) {
$this->xmlfilename = $filename;
$fullpath = $this->converter->get_workdir_path() . '/' . $this->xmlfilename;
$directory = pathinfo($fullpath, PATHINFO_DIRNAME);
if (!check_dir_exists($directory)) {
throw new moodle1_convert_exception('unable_create_target_directory', $directory);
}
$this->xmlwriter = new xml_writer(new file_xml_output($fullpath), new moodle1_xml_transformer());
$this->xmlwriter->start();
}
}
/**
* Close the XML writer
*
* At the moment, the caller must close all tags before calling
*
* @return void
*/
protected function close_xml_writer() {
if ($this->xmlwriter instanceof xml_writer) {
$this->xmlwriter->stop();
}
unset($this->xmlwriter);
$this->xmlwriter = null;
$this->xmlfilename = null;
}
/**
* Checks if the XML writer has been opened by {@link self::open_xml_writer()}
*
* @return bool
*/
protected function has_xml_writer() {
if ($this->xmlwriter instanceof xml_writer) {
return true;
} else {
return false;
}
}
/**
* Writes the given XML tree data into the currently opened file
*
* @param string $element the name of the root element of the tree
* @param array $data the associative array of data to write
* @param array $attribs list of additional fields written as attributes instead of nested elements
* @param string $parent used internally during the recursion, do not set yourself
*/
protected function write_xml($element, array $data, array $attribs = array(), $parent = '/') {
if (!$this->has_xml_writer()) {
throw new moodle1_convert_exception('write_xml_without_writer');
}
$mypath = $parent . $element;
$myattribs = array();
// detect properties that should be rendered as element's attributes instead of children
foreach ($data as $name => $value) {
if (!is_array($value)) {
if (in_array($mypath . '/' . $name, $attribs)) {
$myattribs[$name] = $value;
unset($data[$name]);
}
}
}
// reorder the $data so that all sub-branches are at the end (needed by our parser)
$leaves = array();
$branches = array();
foreach ($data as $name => $value) {
if (is_array($value)) {
$branches[$name] = $value;
} else {
$leaves[$name] = $value;
}
}
$data = array_merge($leaves, $branches);
$this->xmlwriter->begin_tag($element, $myattribs);
foreach ($data as $name => $value) {
if (is_array($value)) {
// recursively call self
$this->write_xml($name, $value, $attribs, $mypath.'/');
} else {
$this->xmlwriter->full_tag($name, $value);
}
}
$this->xmlwriter->end_tag($element);
}
/**
* Makes sure that a new XML file exists, or creates it itself
*
* This is here so we can check that all XML files that the restore process relies on have
* been created by an executed handler. If the file is not found, this method can create it
* using the given $rootelement as an empty root container in the file.
*
* @param string $filename relative file name like 'course/course.xml'
* @param string|bool $rootelement root element to use, false to not create the file
* @param array $content content of the root element
* @return bool true is the file existed, false if it did not
*/
protected function make_sure_xml_exists($filename, $rootelement = false, $content = array()) {
$existed = file_exists($this->converter->get_workdir_path().'/'.$filename);
if ($existed) {
return true;
}
if ($rootelement !== false) {
$this->open_xml_writer($filename);
$this->write_xml($rootelement, $content);
$this->close_xml_writer();
}
return false;
}
}
/**
* Process the root element of the backup file
*/
class moodle1_root_handler extends moodle1_xml_handler {
public function get_paths() {
return array(new convert_path('root_element', '/MOODLE_BACKUP'));
}
/**
* Converts course_files and site_files
*/
public function on_root_element_start() {
// convert course files
$fileshandler = new moodle1_files_handler($this->converter);
$fileshandler->process();
}
/**
* This is executed at the end of the moodle.xml parsing
*/
public function on_root_element_end() {
global $CFG;
// restore the stashes prepared by other handlers for us
$backupinfo = $this->converter->get_stash('backup_info');
$originalcourseinfo = $this->converter->get_stash('original_course_info');
////////////////////////////////////////////////////////////////////////
// write moodle_backup.xml
////////////////////////////////////////////////////////////////////////
$this->open_xml_writer('moodle_backup.xml');
$this->xmlwriter->begin_tag('moodle_backup');
$this->xmlwriter->begin_tag('information');
// moodle_backup/information
$this->xmlwriter->full_tag('name', $backupinfo['name']);
$this->xmlwriter->full_tag('moodle_version', $backupinfo['moodle_version']);
$this->xmlwriter->full_tag('moodle_release', $backupinfo['moodle_release']);
$this->xmlwriter->full_tag('backup_version', $CFG->backup_version); // {@see restore_prechecks_helper::execute_prechecks}
$this->xmlwriter->full_tag('backup_release', $CFG->backup_release);
$this->xmlwriter->full_tag('backup_date', $backupinfo['date']);
// see the commit c0543b - all backups created in 1.9 and later declare the
// information or it is considered as false
if (isset($backupinfo['mnet_remoteusers'])) {
$this->xmlwriter->full_tag('mnet_remoteusers', $backupinfo['mnet_remoteusers']);
} else {
$this->xmlwriter->full_tag('mnet_remoteusers', false);
}
$this->xmlwriter->full_tag('original_wwwroot', $backupinfo['original_wwwroot']);
// {@see backup_general_helper::backup_is_samesite()}
if (isset($backupinfo['original_site_identifier_hash'])) {
$this->xmlwriter->full_tag('original_site_identifier_hash', $backupinfo['original_site_identifier_hash']);
} else {
$this->xmlwriter->full_tag('original_site_identifier_hash', null);
}
$this->xmlwriter->full_tag('original_course_id', $originalcourseinfo['original_course_id']);
$this->xmlwriter->full_tag('original_course_fullname', $originalcourseinfo['original_course_fullname']);
$this->xmlwriter->full_tag('original_course_shortname', $originalcourseinfo['original_course_shortname']);
$this->xmlwriter->full_tag('original_course_startdate', $originalcourseinfo['original_course_startdate']);
$this->xmlwriter->full_tag('original_system_contextid', $this->converter->get_contextid(CONTEXT_SYSTEM));
// note that even though we have original_course_contextid available, we regenerate the
// original course contextid using our helper method to be sure that the data are consistent
// within the MBZ file
$this->xmlwriter->full_tag('original_course_contextid', $this->converter->get_contextid(CONTEXT_COURSE));
// moodle_backup/information/details
$this->xmlwriter->begin_tag('details');
$this->write_xml('detail', array(
'backup_id' => $this->converter->get_id(),
'type' => backup::TYPE_1COURSE,
'format' => backup::FORMAT_MOODLE,
'interactive' => backup::INTERACTIVE_YES,
'mode' => backup::MODE_CONVERTED,
'execution' => backup::EXECUTION_INMEDIATE,
'executiontime' => 0,
), array('/detail/backup_id'));
$this->xmlwriter->end_tag('details');
// moodle_backup/information/contents
$this->xmlwriter->begin_tag('contents');
// moodle_backup/information/contents/activities
$this->xmlwriter->begin_tag('activities');
$activitysettings = array();
foreach ($this->converter->get_stash('coursecontents') as $activity) {
$modinfo = $this->converter->get_stash('modinfo_'.$activity['modulename']);
$modinstance = $modinfo['instances'][$activity['instanceid']];
$this->write_xml('activity', array(
'moduleid' => $activity['cmid'],
'sectionid' => $activity['sectionid'],
'modulename' => $activity['modulename'],
'title' => $modinstance['name'],
'directory' => 'activities/'.$activity['modulename'].'_'.$activity['cmid']
));
$activitysettings[] = array(
'level' => 'activity',
'activity' => $activity['modulename'].'_'.$activity['cmid'],
'name' => $activity['modulename'].'_'.$activity['cmid'].'_included',
'value' => (($modinfo['included'] === 'true' and $modinstance['included'] === 'true') ? 1 : 0));
$activitysettings[] = array(
'level' => 'activity',
'activity' => $activity['modulename'].'_'.$activity['cmid'],
'name' => $activity['modulename'].'_'.$activity['cmid'].'_userinfo',
//'value' => (($modinfo['userinfo'] === 'true' and $modinstance['userinfo'] === 'true') ? 1 : 0));
'value' => 0); // todo hardcoded non-userinfo for now
}
$this->xmlwriter->end_tag('activities');
// moodle_backup/information/contents/sections
$this->xmlwriter->begin_tag('sections');
$sectionsettings = array();
foreach ($this->converter->get_stash_itemids('sectioninfo') as $sectionid) {
$sectioninfo = $this->converter->get_stash('sectioninfo', $sectionid);
$sectionsettings[] = array(
'level' => 'section',
'section' => 'section_'.$sectionid,
'name' => 'section_'.$sectionid.'_included',
'value' => 1);
$sectionsettings[] = array(
'level' => 'section',
'section' => 'section_'.$sectionid,
'name' => 'section_'.$sectionid.'_userinfo',
'value' => 0); // @todo how to detect this from moodle.xml?
$this->write_xml('section', array(
'sectionid' => $sectionid,
'title' => $sectioninfo['number'], // because the title is not available
'directory' => 'sections/section_'.$sectionid));
}
$this->xmlwriter->end_tag('sections');
// moodle_backup/information/contents/course
$this->write_xml('course', array(
'courseid' => $originalcourseinfo['original_course_id'],
'title' => $originalcourseinfo['original_course_shortname'],
'directory' => 'course'));
unset($originalcourseinfo);
$this->xmlwriter->end_tag('contents');
// moodle_backup/information/settings
$this->xmlwriter->begin_tag('settings');
// fake backup root seetings
$rootsettings = array(
'filename' => $backupinfo['name'],
'users' => 0, // @todo how to detect this from moodle.xml?
'anonymize' => 0,
'role_assignments' => 0,
'activities' => 1,
'blocks' => 1,
'filters' => 0,
'comments' => 0,
'userscompletion' => 0,
'logs' => 0,
'grade_histories' => 0,
);
unset($backupinfo);
foreach ($rootsettings as $name => $value) {
$this->write_xml('setting', array(
'level' => 'root',
'name' => $name,
'value' => $value));
}
unset($rootsettings);
// activity settings populated above
foreach ($activitysettings as $activitysetting) {
$this->write_xml('setting', $activitysetting);
}
unset($activitysettings);
// section settings populated above
foreach ($sectionsettings as $sectionsetting) {
$this->write_xml('setting', $sectionsetting);
}
unset($sectionsettings);
$this->xmlwriter->end_tag('settings');
$this->xmlwriter->end_tag('information');
$this->xmlwriter->end_tag('moodle_backup');
$this->close_xml_writer();
////////////////////////////////////////////////////////////////////////
// write files.xml
////////////////////////////////////////////////////////////////////////
$this->open_xml_writer('files.xml');
$this->xmlwriter->begin_tag('files');
foreach ($this->converter->get_stash_itemids('files') as $fileid) {
$this->write_xml('file', $this->converter->get_stash('files', $fileid), array('/file/id'));
}
$this->xmlwriter->end_tag('files');
$this->close_xml_writer('files.xml');
////////////////////////////////////////////////////////////////////////
// write scales.xml
////////////////////////////////////////////////////////////////////////
$this->open_xml_writer('scales.xml');
$this->xmlwriter->begin_tag('scales_definition');
foreach ($this->converter->get_stash_itemids('scales') as $scaleid) {
$this->write_xml('scale', $this->converter->get_stash('scales', $scaleid), array('/scale/id'));
}
$this->xmlwriter->end_tag('scales_definition');
$this->close_xml_writer('scales.xml');
////////////////////////////////////////////////////////////////////////
// write course/inforef.xml
////////////////////////////////////////////////////////////////////////
$this->open_xml_writer('course/inforef.xml');
$this->xmlwriter->begin_tag('inforef');
$this->xmlwriter->begin_tag('fileref');
// legacy course files
$fileids = $this->converter->get_stash('course_files_ids');
if (is_array($fileids)) {
foreach ($fileids as $fileid) {
$this->write_xml('file', array('id' => $fileid));
}
}
// todo site files
// course summary files
$fileids = $this->converter->get_stash('course_summary_files_ids');
if (is_array($fileids)) {
foreach ($fileids as $fileid) {
$this->write_xml('file', array('id' => $fileid));
}
}
$this->xmlwriter->end_tag('fileref');
$this->xmlwriter->begin_tag('question_categoryref');
foreach ($this->converter->get_stash_itemids('question_categories') as $questioncategoryid) {
$this->write_xml('question_category', array('id' => $questioncategoryid));
}
$this->xmlwriter->end_tag('question_categoryref');
$this->xmlwriter->end_tag('inforef');
$this->close_xml_writer();
// make sure that the files required by the restore process have been generated.
// missing file may happen if the watched tag is not present in moodle.xml (for example
// QUESTION_CATEGORIES is optional in moodle.xml but questions.xml must exist in
// moodle2 format) or the handler has not been implemented yet.
// apparently this must be called after the handler had a chance to create the file.
$this->make_sure_xml_exists('questions.xml', 'question_categories');
$this->make_sure_xml_exists('groups.xml', 'groups');
$this->make_sure_xml_exists('outcomes.xml', 'outcomes_definition');
$this->make_sure_xml_exists('users.xml', 'users');
$this->make_sure_xml_exists('course/roles.xml', 'roles',
array('role_assignments' => array(), 'role_overrides' => array()));
$this->make_sure_xml_exists('course/enrolments.xml', 'enrolments',
array('enrols' => array()));
}
}
/**
* The class responsible for course and site files migration
*
* @todo migrate site_files
*/
class moodle1_files_handler extends moodle1_xml_handler {
/**
* Migrates course_files and site_files in the converter workdir
*/
public function process() {
$this->migrate_course_files();
// todo $this->migrate_site_files();
}
/**
* Migrates course_files in the converter workdir
*/
protected function migrate_course_files() {
$ids = array();
$fileman = $this->converter->get_file_manager($this->converter->get_contextid(CONTEXT_COURSE), 'course', 'legacy');
$this->converter->set_stash('course_files_ids', array());
if (file_exists($this->converter->get_tempdir_path().'/course_files')) {
$ids = $fileman->migrate_directory('course_files');
$this->converter->set_stash('course_files_ids', $ids);
}
$this->log('course files migrated', backup::LOG_INFO, count($ids));
}
}
/**
* Handles the conversion of /MOODLE_BACKUP/INFO paths
*
* We do not produce any XML file here, just storing the data in the temp
* table so thay can be used by a later handler.
*/
class moodle1_info_handler extends moodle1_handler {
/** @var array list of mod names included in info_details */
protected $modnames = array();
/** @var array the in-memory cache of the currently parsed info_details_mod element */
protected $currentmod;
public function get_paths() {
return array(
new convert_path('info', '/MOODLE_BACKUP/INFO'),
new convert_path('info_details', '/MOODLE_BACKUP/INFO/DETAILS'),
new convert_path('info_details_mod', '/MOODLE_BACKUP/INFO/DETAILS/MOD'),
new convert_path('info_details_mod_instance', '/MOODLE_BACKUP/INFO/DETAILS/MOD/INSTANCES/INSTANCE'),
);
}
/**
* Stashes the backup info for later processing by {@link moodle1_root_handler}
*/
public function process_info($data) {
$this->converter->set_stash('backup_info', $data);
}
/**
* Initializes the in-memory cache for the current mod
*/
public function process_info_details_mod($data) {
$this->currentmod = $data;
$this->currentmod['instances'] = array();
}
/**
* Appends the current instance data to the temporary in-memory cache
*/
public function process_info_details_mod_instance($data) {
$this->currentmod['instances'][$data['id']] = $data;
}
/**
* Stashes the backup info for later processing by {@link moodle1_root_handler}
*/
public function on_info_details_mod_end($data) {
global $CFG;
// keep only such modules that seem to have the support for moodle1 implemented
$modname = $this->currentmod['name'];
if (file_exists($CFG->dirroot.'/mod/'.$modname.'/backup/moodle1/lib.php')) {
$this->converter->set_stash('modinfo_'.$modname, $this->currentmod);
$this->modnames[] = $modname;
} else {
$this->log('unsupported activity module', backup::LOG_WARNING, $modname);
}
$this->currentmod = array();
}
/**
* Stashes the list of activity module types for later processing by {@link moodle1_root_handler}
*/
public function on_info_details_end() {
$this->converter->set_stash('modnameslist', $this->modnames);
}
}
/**
* Handles the conversion of /MOODLE_BACKUP/COURSE/HEADER paths
*/
class moodle1_course_header_handler extends moodle1_xml_handler {
/** @var array we need to merge course information because it is dispatched twice */
protected $course = array();
/** @var array we need to merge course information because it is dispatched twice */
protected $courseraw = array();
/** @var array */
protected $category;
public function get_paths() {
return array(
new convert_path(
'course_header', '/MOODLE_BACKUP/COURSE/HEADER',
array(
'newfields' => array(
'summaryformat' => 1,
'legacyfiles' => 2,
'requested' => 0, // @todo not really new, but maybe never backed up?
'restrictmodules' => 0,
'enablecompletion' => 0,
'completionstartonenrol' => 0,
'completionnotify' => 0,
'tags' => array(),
'allowed_modules' => array(),
),
'dropfields' => array(
'roles_overrides',
'roles_assignments',
'cost',
'currancy',
'defaultrole',
'enrol',
'enrolenddate',
'enrollable',
'enrolperiod',
'enrolstartdate',
'expirynotify',
'expirythreshold',
'guest',
'notifystudents',
'password',
'student',
'students',
'teacher',
'teachers',
'metacourse',
)
)
),
new convert_path(
'course_header_category', '/MOODLE_BACKUP/COURSE/HEADER/CATEGORY',
array(
'newfields' => array(
'description' => null,
)
)
),
);
}
/**
* Because there is the CATEGORY branch in the middle of the COURSE/HEADER
* branch, this is dispatched twice. We use $this->coursecooked to merge
* the result. Once the parser is fixed, it can be refactored.
*/
public function process_course_header($data, $raw) {
$this->course = array_merge($this->course, $data);
$this->courseraw = array_merge($this->courseraw, $raw);
}
public function process_course_header_category($data) {
$this->category = $data;
}
public function on_course_header_end() {
$contextid = $this->converter->get_contextid(CONTEXT_COURSE);
// stash the information needed by other handlers
$info = array(
'original_course_id' => $this->course['id'],
'original_course_fullname' => $this->course['fullname'],
'original_course_shortname' => $this->course['shortname'],
'original_course_startdate' => $this->course['startdate'],
'original_course_contextid' => $contextid
);
$this->converter->set_stash('original_course_info', $info);
$this->course['contextid'] = $contextid;
$this->course['category'] = $this->category;
// migrate files embedded into the course summary and stash their ids
$fileman = $this->converter->get_file_manager($contextid, 'course', 'summary');
$this->course['summary'] = moodle1_converter::migrate_referenced_files($this->course['summary'], $fileman);
$this->converter->set_stash('course_summary_files_ids', $fileman->get_fileids());
// write course.xml
$this->open_xml_writer('course/course.xml');
$this->write_xml('course', $this->course, array('/course/id', '/course/contextid'));
$this->close_xml_writer();
}
}
/**
* Handles the conversion of course sections and course modules
*/
class moodle1_course_outline_handler extends moodle1_xml_handler {
/** @var array ordered list of the course contents */
protected $coursecontents = array();
/** @var array current section data */
protected $currentsection;
/**
* This handler is interested in course sections and course modules within them
*/
public function get_paths() {
return array(
new convert_path('course_sections', '/MOODLE_BACKUP/COURSE/SECTIONS'),
new convert_path(
'course_section', '/MOODLE_BACKUP/COURSE/SECTIONS/SECTION',
array(
'newfields' => array(
'name' => null,
'summaryformat' => 1,
'sequence' => null,
),
)
),
new convert_path(
'course_module', '/MOODLE_BACKUP/COURSE/SECTIONS/SECTION/MODS/MOD',
array(
'newfields' => array(
'completion' => 0,
'completiongradeitemnumber' => null,
'completionview' => 0,
'completionexpected' => 0,
'availability' => null,
'visibleold' => 1,
'showdescription' => 0,
),
'dropfields' => array(
'instance',
'roles_overrides',
'roles_assignments',
),
'renamefields' => array(
'type' => 'modulename',
),
)
),
new convert_path('course_modules', '/MOODLE_BACKUP/COURSE/MODULES'),
// todo new convert_path('course_module_roles_overrides', '/MOODLE_BACKUP/COURSE/SECTIONS/SECTION/MODS/MOD/ROLES_OVERRIDES'),
// todo new convert_path('course_module_roles_assignments', '/MOODLE_BACKUP/COURSE/SECTIONS/SECTION/MODS/MOD/ROLES_ASSIGNMENTS'),
);
}
public function process_course_section($data) {
$this->currentsection = $data;
}
/**
* Populates the section sequence field (order of course modules) and stashes the
* course module info so that is can be dumped to activities/xxxx_x/module.xml later
*/
public function process_course_module($data, $raw) {
global $CFG;
// check that this type of module should be included in the mbz
$modinfo = $this->converter->get_stash_itemids('modinfo_'.$data['modulename']);
if (empty($modinfo)) {
return;
}
// add the course module into the course contents list
$this->coursecontents[$data['id']] = array(
'cmid' => $data['id'],
'instanceid' => $raw['INSTANCE'],
'sectionid' => $this->currentsection['id'],
'modulename' => $data['modulename'],
'title' => null
);
// add the course module id into the section's sequence
if (is_null($this->currentsection['sequence'])) {
$this->currentsection['sequence'] = $data['id'];
} else {
$this->currentsection['sequence'] .= ',' . $data['id'];
}
// add the sectionid and sectionnumber
$data['sectionid'] = $this->currentsection['id'];
$data['sectionnumber'] = $this->currentsection['number'];
// generate the module version - this is a bit tricky as this information
// is not present in 1.9 backups. we will use the currently installed version
// whenever we can but that might not be accurate for some modules.
// also there might be problem with modules that are not present at the target
// host...
$versionfile = $CFG->dirroot.'/mod/'.$data['modulename'].'/version.php';
if (file_exists($versionfile)) {
$plugin = new stdClass();
$plugin->version = null;
$module = $plugin;
include($versionfile);
$data['version'] = $plugin->version;
} else {
$data['version'] = null;
}
// stash the course module info in stashes like 'cminfo_forum' with
// itemid set to the instance id. this is needed so that module handlers
// can later obtain information about the course module and dump it into
// the module.xml file
$this->converter->set_stash('cminfo_'.$data['modulename'], $data, $raw['INSTANCE']);
}
/**
* Writes sections/section_xxx/section.xml file and stashes it, too
*/
public function on_course_section_end() {
// migrate files embedded into the section summary field
$contextid = $this->converter->get_contextid(CONTEXT_COURSE);
$fileman = $this->converter->get_file_manager($contextid, 'course', 'section', $this->currentsection['id']);
$this->currentsection['summary'] = moodle1_converter::migrate_referenced_files($this->currentsection['summary'], $fileman);
// write section's inforef.xml with the file references
$this->open_xml_writer('sections/section_' . $this->currentsection['id'] . '/inforef.xml');
$this->xmlwriter->begin_tag('inforef');
$this->xmlwriter->begin_tag('fileref');
$fileids = $fileman->get_fileids();
if (is_array($fileids)) {
foreach ($fileids as $fileid) {
$this->write_xml('file', array('id' => $fileid));
}
}
$this->xmlwriter->end_tag('fileref');
$this->xmlwriter->end_tag('inforef');
$this->close_xml_writer();
// stash the section info and write section.xml
$this->converter->set_stash('sectioninfo', $this->currentsection, $this->currentsection['id']);
$this->open_xml_writer('sections/section_' . $this->currentsection['id'] . '/section.xml');
$this->write_xml('section', $this->currentsection);
$this->close_xml_writer();
unset($this->currentsection);
}
/**
* Stashes the course contents
*/
public function on_course_sections_end() {
$this->converter->set_stash('coursecontents', $this->coursecontents);
}
/**
* Writes the information collected by mod handlers
*/
public function on_course_modules_end() {
foreach ($this->converter->get_stash('modnameslist') as $modname) {
$modinfo = $this->converter->get_stash('modinfo_'.$modname);
foreach ($modinfo['instances'] as $modinstanceid => $modinstance) {
$cminfo = $this->converter->get_stash('cminfo_'.$modname, $modinstanceid);
$directory = 'activities/'.$modname.'_'.$cminfo['id'];
// write module.xml
$this->open_xml_writer($directory.'/module.xml');
$this->write_xml('module', $cminfo, array('/module/id', '/module/version'));
$this->close_xml_writer();
// write grades.xml
$this->open_xml_writer($directory.'/grades.xml');
$this->xmlwriter->begin_tag('activity_gradebook');
$gradeitems = $this->converter->get_stash_or_default('gradebook_modgradeitem_'.$modname, $modinstanceid, array());
if (!empty($gradeitems)) {
$this->xmlwriter->begin_tag('grade_items');
foreach ($gradeitems as $gradeitem) {
$this->write_xml('grade_item', $gradeitem, array('/grade_item/id'));
}
$this->xmlwriter->end_tag('grade_items');
}
$this->write_xml('grade_letters', array()); // no grade_letters in module context in Moodle 1.9
$this->xmlwriter->end_tag('activity_gradebook');
$this->close_xml_writer();
// todo: write proper roles.xml, for now we just make sure the file is present
$this->make_sure_xml_exists($directory.'/roles.xml', 'roles');
}
}
}
}
/**
* Handles the conversion of the defined roles
*/
class moodle1_roles_definition_handler extends moodle1_xml_handler {
/**
* Where the roles are defined in the source moodle.xml
*/
public function get_paths() {
return array(
new convert_path('roles', '/MOODLE_BACKUP/ROLES'),
new convert_path(
'roles_role', '/MOODLE_BACKUP/ROLES/ROLE',
array(
'newfields' => array(
'description' => '',
'sortorder' => 0,
'archetype' => ''
)
)
)
);
}
/**
* If there are any roles defined in moodle.xml, convert them to roles.xml
*/
public function process_roles_role($data) {
if (!$this->has_xml_writer()) {
$this->open_xml_writer('roles.xml');
$this->xmlwriter->begin_tag('roles_definition');
}
if (!isset($data['nameincourse'])) {
$data['nameincourse'] = null;
}
$this->write_xml('role', $data, array('role/id'));
}
/**
* Finishes writing roles.xml