2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * @subpackage backup-imscc
19 * @copyright 2009 Mauro Rondinelli (mauro.rondinelli [AT] uvcms.com)
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
25 require_once($CFG->dirroot
. '/backup/cc/entities.class.php');
26 require_once($CFG->dirroot
. '/backup/cc/entity.label.class.php');
27 require_once($CFG->dirroot
. '/backup/cc/entity.resource.class.php');
28 require_once($CFG->dirroot
. '/backup/cc/entity.forum.class.php');
29 require_once($CFG->dirroot
. '/backup/cc/entity.quiz.class.php');
33 const CC_TYPE_FORUM
= 'imsdt_xmlv1p0';
34 const CC_TYPE_QUIZ
= 'imsqti_xmlv1p2/imscc_xmlv1p0/assessment';
35 const CC_TYPE_QUESTION_BANK
= 'imsqti_xmlv1p2/imscc_xmlv1p0/question-bank';
36 const CC_TYPE_WEBLINK
= 'imswl_xmlv1p0';
37 const CC_TYPE_WEBCONTENT
= 'webcontent';
38 const CC_TYPE_ASSOCIATED_CONTENT
= 'associatedcontent/imscc_xmlv1p0/learning-application-resource';
39 const CC_TYPE_EMPTY
= '';
41 public static $restypes = array('associatedcontent/imscc_xmlv1p0/learning-application-resource', 'webcontent');
42 public static $forumns = array('dt' => 'http://www.imsglobal.org/xsd/imsdt_v1p0');
43 public static $quizns = array('xmlns' => 'http://www.imsglobal.org/xsd/ims_qtiasiv1p2');
44 public static $resourcens = array('wl' => 'http://www.imsglobal.org/xsd/imswl_v1p0');
49 public static function getquizns() {
50 return static::$quizns;
57 public static function getforumns() {
58 return static::$forumns;
65 public static function getresourcens() {
66 return static::$resourcens;
69 public static function get_manifest($folder) {
70 if (!is_dir($folder)) {
74 // Before iterate over directories, try to find one manifest at top level
75 if (file_exists($folder . '/imsmanifest.xml')) {
76 return $folder . '/imsmanifest.xml';
81 $dirIter = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator
::KEY_AS_PATHNAME
);
82 $recIter = new RecursiveIteratorIterator($dirIter, RecursiveIteratorIterator
::CHILD_FIRST
);
83 foreach ($recIter as $info) {
84 if ($info->isFile() && ($info->getFilename() == 'imsmanifest.xml')) {
85 $result = $info->getPathname();
89 } catch (Exception
$e) {}
94 public static $instances = array();
95 public static $manifest;
96 public static $path_to_manifest_folder;
98 public static $namespaces = array('imscc' => 'http://www.imsglobal.org/xsd/imscc/imscp_v1p1',
99 'lomimscc' => 'http://ltsc.ieee.org/xsd/imscc/LOM',
100 'lom' => 'http://ltsc.ieee.org/xsd/LOM',
101 'voc' => 'http://ltsc.ieee.org/xsd/LOM/vocab',
102 'xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
103 'cc' => 'http://www.imsglobal.org/xsd/imsccauth_v1p0');
105 function __construct ($path_to_manifest) {
107 static::$manifest = new DOMDocument();
108 static::$manifest->validateOnParse
= false;
110 static::$path_to_manifest_folder = dirname($path_to_manifest);
112 static::log_action('Proccess start');
113 static::log_action('Load the manifest file: ' . $path_to_manifest);
115 if (!static::$manifest->load($path_to_manifest, LIBXML_NONET
)) {
116 static::log_action('Cannot load the manifest file: ' . $path_to_manifest, true);
120 public function is_auth () {
122 $xpath = static::newx_path(static::$manifest, static::$namespaces);
124 $count_auth = $xpath->evaluate('count(/imscc:manifest/cc:authorizations)');
126 if ($count_auth > 0) {
135 protected function get_metadata ($section, $key) {
137 $xpath = static::newx_path(static::$manifest, static::$namespaces);
139 $metadata = $xpath->query('/imscc:manifest/imscc:metadata/lomimscc:lom/lomimscc:' . $section . '/lomimscc:' . $key . '/lomimscc:string');
140 $value = !empty($metadata->item(0)->nodeValue
) ?
$metadata->item(0)->nodeValue
: '';
145 public function generate_moodle_xml () {
149 $cdir = static::$path_to_manifest_folder . DIRECTORY_SEPARATOR
. 'course_files';
151 if (!file_exists($cdir)) {
155 $sheet_base = static::loadsheet(SHEET_BASE
);
157 // MOODLE_BACKUP / INFO / DETAILS / MOD
158 $node_info_details_mod = $this->create_code_info_details_mod();
160 // MOODLE_BACKUP / BLOCKS / BLOCK
161 $node_course_blocks_block = $this->create_node_course_blocks_block();
163 // MOODLE_BACKUP / COURSES / SECTIONS / SECTION
164 $node_course_sections_section = $this->create_node_course_sections_section();
166 // MOODLE_BACKUP / COURSES / QUESTION_CATEGORIES
167 $node_course_question_categories = $this->create_node_question_categories();
169 // MOODLE_BACKUP / COURSES / MODULES / MOD
170 $node_course_modules_mod = $this->create_node_course_modules_mod();
172 // MOODLE_BACKUP / COURSE / HEADER
173 $node_course_header = $this->create_node_course_header();
176 $filename = optional_param('file', 'not_available.zip', PARAM_RAW
);
177 $filename = basename($filename);
179 $www_root = $CFG->wwwroot
;
181 $find_tags = array('[#zip_filename#]',
183 '[#node_course_header#]',
184 '[#node_info_details_mod#]',
185 '[#node_course_blocks_block#]',
186 '[#node_course_sections_section#]',
187 '[#node_course_question_categories#]',
188 '[#node_course_modules#]');
190 $replace_values = array($filename,
193 $node_info_details_mod,
194 $node_course_blocks_block,
195 $node_course_sections_section,
196 $node_course_question_categories,
197 $node_course_modules_mod);
199 $result_xml = str_replace($find_tags, $replace_values, $sheet_base);
201 // COPY RESOURSE FILES
202 $entities = new entities();
204 $entities->move_all_files();
206 if (array_key_exists("index", self
::$instances)) {
208 if (!file_put_contents(static::$path_to_manifest_folder . DIRECTORY_SEPARATOR
. 'moodle.xml', $result_xml)) {
209 static::log_action('Cannot save the moodle manifest file: ' . static::$path_to_tmp_folder . DIRECTORY_SEPARATOR
. 'moodle.xml', true);
216 notify('The course is empty');
217 static::log_action('The course is empty', false);
224 protected function get_sections_numbers ($instances) {
228 if (array_key_exists("index", $instances)) {
229 foreach ($instances["index"] as $instance) {
230 if ($instance["deep"] == ROOT_DEEP
) {
239 protected function create_node_course_header () {
241 $node_course_header = '';
242 $sheet_course_header = static::loadsheet(SHEET_COURSE_HEADER
);
244 $course_title = trim($this->get_metadata('general', 'title'));
245 $course_title = empty($course_title) ?
'Untitled Course' : $course_title;
246 $course_description = $this->get_metadata('general', 'description');
247 $section_count = $this->get_sections_numbers(static::$instances) - 1;
249 if ($section_count == -1) {
253 if (empty($course_title)) {
254 $this->log_action('The course title not found', true);
257 $course_short_name = $this->create_course_code($course_title);
259 $find_tags = array('[#course_name#]',
260 '[#course_short_name#]',
261 '[#course_description#]',
263 '[#section_count#]');
265 $replace_values = array(entities
::safexml($course_title),
266 entities
::safexml($course_short_name),
267 entities
::safexml($course_description),
271 $node_course_header = str_replace($find_tags, $replace_values, $sheet_course_header);
273 return $node_course_header;
276 protected function create_node_question_categories () {
278 $quiz = new cc_quiz();
280 static::log_action('Creating node: QUESTION_CATEGORIES');
282 $node_course_question_categories = $quiz->generate_node_question_categories();
284 return $node_course_question_categories;
287 protected function create_node_course_modules_mod () {
289 $labels = new cc_label();
290 $resources = new cc_resource();
291 $forums = new cc_forum();
292 $quiz = new cc_quiz();
294 static::log_action('Creating node: COURSE/MODULES/MOD');
297 $node_course_modules_mod_label = $labels->generate_node();
299 // RESOURCES (WEB CONTENT AND WEB LINK)
300 $node_course_modules_mod_resource = $resources->generate_node();
303 $node_course_modules_mod_forum = $forums->generate_node();
306 $node_course_modules_mod_quiz = $quiz->generate_node_course_modules_mod();
308 $node_course_modules = $node_course_modules_mod_label . $node_course_modules_mod_resource . $node_course_modules_mod_forum . $node_course_modules_mod_quiz;
310 return $node_course_modules;
314 protected function create_node_course_sections_section () {
316 static::log_action('Creating node: COURSE/SECTIONS/SECTION');
318 $node_course_sections_section = '';
319 $sheet_course_sections_section = static::loadsheet(SHEET_COURSE_SECTIONS_SECTION
);
321 $topics = $this->get_nodes_by_criteria('deep', ROOT_DEEP
);
325 if (!empty($topics)) {
327 foreach ($topics as $topic) {
330 $node_node_course_sections_section_mods_mod = $this->create_node_course_sections_section_mods_mod($topic['index']);
332 if ($topic['moodle_type'] == MOODLE_TYPE_LABEL
) {
334 $find_tags = array('[#section_id#]',
335 '[#section_number#]',
336 '[#section_summary#]',
337 '[#node_course_sections_section_mods_mod#]');
339 $replace_values = array($i,
342 $node_node_course_sections_section_mods_mod);
346 $find_tags = array('[#section_id#]',
347 '[#section_number#]',
348 '[#section_summary#]',
349 '[#node_course_sections_section_mods_mod#]');
351 $replace_values = array($i,
354 $node_node_course_sections_section_mods_mod);
358 $node_course_sections_section .= str_replace($find_tags, $replace_values, $sheet_course_sections_section);
363 return $node_course_sections_section;
366 protected function create_node_course_blocks_block () {
370 static::log_action('Creating node: COURSE/BLOCKS/BLOCK');
372 $sheet_course_blocks_block = static::loadsheet(SHEET_COURSE_BLOCKS_BLOCK
);
373 $node_course_blocks_block = '';
375 $format_config = $CFG->dirroot
. '/course/format/weeks/config.php';
377 if (@is_file
($format_config) && is_readable($format_config)) {
378 require ($format_config);
381 if (!empty($format['defaultblocks'])) {
382 $blocknames = $format['defaultblocks'];
384 if (!empty($CFG->defaultblocks
)) {
385 $blocknames = $CFG->defaultblocks
;
387 $blocknames = 'participants,activity_modules,search_forums,course_list:news_items,calendar_upcoming,recent_activity';
391 $blocknames = explode(':', $blocknames);
392 $blocks_left = explode(',', $blocknames[0]);
393 $blocks_right = explode(',', $blocknames[1]);
395 $find_tags = array('[#block_id#]',
397 '[#block_position#]',
403 foreach ($blocks_left as $block) {
407 $replace_values = array($i,
412 $node_course_blocks_block .= str_replace($find_tags, $replace_values, $sheet_course_blocks_block);
417 foreach ($blocks_right as $block) {
422 $replace_values = array($i,
427 $node_course_blocks_block .= str_replace($find_tags, $replace_values, $sheet_course_blocks_block);
430 return $node_course_blocks_block;
436 * Is activity visible or not
437 * @param string $identifier
440 protected function get_module_visible($identifier) {
441 //Should item be hidden or not
443 if (!empty($identifier)) {
444 $xpath = static::newx_path(static::$manifest, static::$namespaces);
445 $query = '/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $identifier . '"]';
446 $query .= '//lom:intendedEndUserRole/voc:vocabulary/lom:value';
447 $intendeduserrole = $xpath->query($query);
448 if (!empty($intendeduserrole) && ($intendeduserrole->length
> 0)) {
449 $role = trim($intendeduserrole->item(0)->nodeValue
);
450 if (strcasecmp('Instructor', $role) == 0) {
458 protected function create_node_course_sections_section_mods_mod ($root_parent) {
460 $sheet_course_sections_section_mods_mod = static::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD
);
461 $childs = $this->get_nodes_by_criteria('root_parent', $root_parent);
465 $node_course_sections_section_mods_mod = '';
467 foreach ($childs as $child) {
469 if ($child['moodle_type'] == MOODLE_TYPE_LABEL
) {
470 if ($child['index'] == $child['root_parent']) {
481 $indent = $child['deep'] - ROOT_DEEP
;
484 $indent = $indent - 1;
487 $find_tags = array('[#mod_id#]',
488 '[#mod_instance_id#]',
494 $replace_values = array($child['index'],
496 $child['moodle_type'],
499 $this->get_module_visible($child['resource_indentifier']));
501 $node_course_sections_section_mods_mod .= str_replace($find_tags, $replace_values, $sheet_course_sections_section_mods_mod);
505 $response = $node_course_sections_section_mods_mod;
515 public function get_nodes_by_criteria ($key, $value) {
519 if (array_key_exists('index', static::$instances)) {
520 foreach (static::$instances['index'] as $item) {
521 if ($item[$key] == $value) {
531 protected function create_code_info_details_mod () {
533 static::log_action('Creating node: INFO/DETAILS/MOD');
535 $xpath = static::newx_path(static::$manifest, static::$namespaces);
537 $items = $xpath->query('/imscc:manifest/imscc:organizations/imscc:organization/imscc:item | /imscc:manifest/imscc:resources/imscc:resource[@type="' . static::CC_TYPE_QUESTION_BANK
. '"]');
539 $this->create_instances($items);
541 $count_quiz = $this->count_instances(MOODLE_TYPE_QUIZ
);
542 $count_forum = $this->count_instances(MOODLE_TYPE_FORUM
);
543 $count_resource = $this->count_instances(MOODLE_TYPE_RESOURCE
);
544 $count_label = $this->count_instances(MOODLE_TYPE_LABEL
);
546 $sheet_info_details_mod_instances_instance = static::loadsheet(SHEET_INFO_DETAILS_MOD_INSTANCE
);
548 if ($count_resource > 0) {
549 $resource_instance = $this->create_mod_info_details_mod_instances_instance($sheet_info_details_mod_instances_instance, $count_resource, static::$instances['instances'][MOODLE_TYPE_RESOURCE
]);
551 if ($count_quiz > 0) {
552 $quiz_instance = $this->create_mod_info_details_mod_instances_instance($sheet_info_details_mod_instances_instance, $count_quiz, static::$instances['instances'][MOODLE_TYPE_QUIZ
]);
554 if ($count_forum > 0) {
555 $forum_instance = $this->create_mod_info_details_mod_instances_instance($sheet_info_details_mod_instances_instance, $count_forum, static::$instances['instances'][MOODLE_TYPE_FORUM
]);
557 if ($count_label > 0) {
558 $label_instance = $this->create_mod_info_details_mod_instances_instance($sheet_info_details_mod_instances_instance, $count_label, static::$instances['instances'][MOODLE_TYPE_LABEL
]);
561 $resource_mod = $count_resource ?
$this->create_mod_info_details_mod(MOODLE_TYPE_RESOURCE
, $resource_instance) : '';
562 $quiz_mod = $count_quiz ?
$this->create_mod_info_details_mod(MOODLE_TYPE_QUIZ
, $quiz_instance) : '';
563 $forum_mod = $count_forum ?
$this->create_mod_info_details_mod(MOODLE_TYPE_FORUM
, $forum_instance) : '';
564 $label_mod = $count_label ?
$this->create_mod_info_details_mod(MOODLE_TYPE_LABEL
, $label_instance) : '';
567 return $label_mod . $resource_mod . $quiz_mod . $forum_mod;
571 protected function create_mod_info_details_mod ($mod_type, $node_info_details_mod_instances_instance) {
573 $sheet_info_details_mod = static::loadsheet(SHEET_INFO_DETAILS_MOD
);
575 $find_tags = array('[#mod_type#]' ,'[#node_info_details_mod_instances_instance#]');
576 $replace_values = array($mod_type , $node_info_details_mod_instances_instance);
578 return str_replace($find_tags, $replace_values, $sheet_info_details_mod);
581 protected function create_mod_info_details_mod_instances_instance ($sheet, $instances_quantity, $instances) {
585 $find_tags = array('[#mod_instance_id#]',
587 '[#mod_user_info#]');
589 for ($i = 1; $i <= $instances_quantity; $i++
) {
591 $user_info = ($instances[$i - 1]['common_cartriedge_type'] == static::CC_TYPE_FORUM
) ?
'true' : 'false';
592 if ($instances[$i - 1]['common_cartriedge_type'] == static::CC_TYPE_EMPTY
) {
593 if ($instances[$i - 1]['deep'] <= ROOT_DEEP
) {
598 $replace_values = array($instances[$i - 1]['instance'],
599 entities
::safexml($instances[$i - 1]['title']),
602 $instance .= str_replace($find_tags, $replace_values, $sheet);
609 protected function create_instances ($items, $level = 0, &$array_index = 0, $index_root = 0) {
616 $xpath = self
::newx_path(static::$manifest, static::$namespaces);
618 foreach ($items as $item) {
622 if ($item->nodeName
== "item") {
624 $identifierref = $xpath->query('@identifierref', $item);
625 $identifierref = !empty($identifierref->item(0)->nodeValue
) ?
$identifierref->item(0)->nodeValue
: '';
627 $title = $xpath->query('imscc:title', $item);
628 $title = !empty($title->item(0)->nodeValue
) ?
$title->item(0)->nodeValue
: '';
630 $cc_type = $this->get_item_cc_type($identifierref);
631 $moodle_type = $this->convert_to_moodle_type($cc_type);
633 elseif ($item->nodeName
== "resource") {
635 $identifierref = $xpath->query('@identifier', $item);
636 $identifierref = !empty($identifierref->item(0)->nodeValue
) ?
$identifierref->item(0)->nodeValue
: '';
638 $cc_type = $this->get_item_cc_type($identifierref);
639 $moodle_type = $this->convert_to_moodle_type($cc_type);
641 $title = 'Quiz Bank ' . ($this->count_instances($moodle_type) +
1);
645 if ($level == ROOT_DEEP
) {
646 $index_root = $array_index;
649 static::$instances['index'][$array_index]['common_cartriedge_type'] = $cc_type;
650 static::$instances['index'][$array_index]['moodle_type'] = $moodle_type;
651 static::$instances['index'][$array_index]['title'] = $title ?
$title : '';
652 static::$instances['index'][$array_index]['root_parent'] = $index_root;
653 static::$instances['index'][$array_index]['index'] = $array_index;
654 static::$instances['index'][$array_index]['deep'] = $level;
655 static::$instances['index'][$array_index]['instance'] = $this->count_instances($moodle_type);
656 static::$instances['index'][$array_index]['resource_indentifier'] = $identifierref;
658 static::$instances['instances'][$moodle_type][] = array('title' => $title,
659 'instance' => static::$instances['index'][$array_index]['instance'],
660 'common_cartriedge_type' => $cc_type,
661 'resource_indentifier' => $identifierref,
664 $more_items = $xpath->query('imscc:item', $item);
666 if ($more_items->length
> 0) {
667 $this->create_instances($more_items, $level, $array_index, $index_root);
676 public function count_instances ($type) {
680 if (array_key_exists('index', static::$instances)) {
681 if (static::$instances['index'] && $type) {
683 foreach (static::$instances['index'] as $instance) {
684 if (!empty($instance['moodle_type'])) {
685 $types[] = $instance['moodle_type'];
689 $quantity_instances = array_count_values($types);
690 $quantity = array_key_exists($type, $quantity_instances) ?
$quantity_instances[$type] : 0;
697 public function convert_to_moodle_type ($cc_type) {
698 $type = TYPE_UNKNOWN
;
700 if ($cc_type == static::CC_TYPE_FORUM
) {
701 $type = MOODLE_TYPE_FORUM
;
704 if ($cc_type == static::CC_TYPE_QUIZ
) {
705 $type = MOODLE_TYPE_QUIZ
;
708 if ($cc_type == static::CC_TYPE_WEBLINK
) {
709 $type = MOODLE_TYPE_RESOURCE
;
712 if ($cc_type == static::CC_TYPE_WEBCONTENT
) {
713 $type = MOODLE_TYPE_RESOURCE
;
716 if ($cc_type == static::CC_TYPE_ASSOCIATED_CONTENT
) {
717 $type = MOODLE_TYPE_RESOURCE
;
720 if ($cc_type == static::CC_TYPE_QUESTION_BANK
) {
721 $type = MOODLE_TYPE_QUESTION_BANK
;
724 if ($cc_type == static::CC_TYPE_EMPTY
) {
725 $type = MOODLE_TYPE_LABEL
;
731 public function get_item_cc_type ($identifier) {
733 $xpath = static::newx_path(static::$manifest, static::$namespaces);
735 $nodes = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $identifier . '"]/@type');
737 if ($nodes && !empty($nodes->item(0)->nodeValue
)) {
738 return $nodes->item(0)->nodeValue
;
744 public static function newx_path (DOMDocument
$manifest, $namespaces = '') {
746 $xpath = new DOMXPath($manifest);
748 if (!empty($namespaces)) {
749 foreach ($namespaces as $prefix => $ns) {
750 if (!$xpath->registerNamespace($prefix, $ns)) {
751 static::log_action('Cannot register the namespace: ' . $prefix . ':' . $ns, true);
759 public static function loadsheet ($file) {
761 $content = (is_readable($file) && ($content = file_get_contents($file))) ?
$content : false;
763 static::log_action('Loading sheet: ' . $file);
766 static::log_action('Cannot load the xml sheet: ' . $file, true);
769 static::log_action('Load OK!');
774 public static function log_file() {
775 return static::$path_to_manifest_folder . DIRECTORY_SEPARATOR
. 'cc2moodle.log';
778 public static function log_action ($text, $critical_error = false) {
780 $full_message = strtoupper(date("j/n/Y g:i:s a")) . " - " . $text . "\r";
782 file_put_contents(static::log_file(), $full_message, FILE_APPEND
);
784 if ($critical_error) {
785 static::critical_error($text);
789 protected function critical_error ($text) {
791 $path_to_log = static::log_file();
796 <hr />A critical error has been found!
802 The process has been stopped. Please see the <a href="' . $path_to_log . '">log file</a> for more information.</p>
804 <p>Log: ' . $path_to_log . '</p>
814 protected function create_course_code ($title) {
815 //Making sure that text of the short name does not go over the DB limit.
816 //and leaving the space to add additional characters by the platform
817 $code = substr(strtoupper(str_replace(' ', '', trim($title))),0,94);