Merge branch 'MDL-40255_M25' of git://github.com/lazydaisy/moodle into MOODLE_25_STABLE
[moodle.git] / backup / cc / cc2moodle.php
blob05e8239f12d6f640d4f2deae23611ef163a17d68
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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/>.
16 /**
17 * @package moodlecore
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');
31 class cc2moodle {
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');
45 /**
47 * @return array
49 public static function getquizns() {
50 return static::$quizns;
53 /**
55 * @return array
57 public static function getforumns() {
58 return static::$forumns;
61 /**
63 * @return array
65 public static function getresourcens() {
66 return static::$resourcens;
69 public static function get_manifest($folder) {
70 if (!is_dir($folder)) {
71 return false;
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';
79 $result = false;
80 try {
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();
86 break;
89 } catch (Exception $e) {}
91 return $result;
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) {
127 $response = true;
128 } else {
129 $response = false;
132 return $response;
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 : '';
142 return $value;
145 public function generate_moodle_xml () {
147 global $CFG;
149 $cdir = static::$path_to_manifest_folder . DIRECTORY_SEPARATOR . 'course_files';
151 if (!file_exists($cdir)) {
152 mkdir($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();
175 // GENERAL INFO
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#]',
182 '[#www_root#]',
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,
191 $www_root,
192 $node_course_header,
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);
210 } else {
211 $status = true;
214 } else {
215 $status = false;
216 notify('The course is empty');
217 static::log_action('The course is empty', false);
220 return $status;
224 protected function get_sections_numbers ($instances) {
226 $count = 0;
228 if (array_key_exists("index", $instances)) {
229 foreach ($instances["index"] as $instance) {
230 if ($instance["deep"] == ROOT_DEEP) {
231 $count++;
236 return $count;
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) {
250 $section_count = 0;
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#]',
262 '[#date_now#]',
263 '[#section_count#]');
265 $replace_values = array(entities::safexml($course_title),
266 entities::safexml($course_short_name),
267 entities::safexml($course_description),
268 time(),
269 $section_count);
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');
296 // LABELS
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();
302 // FORUMS
303 $node_course_modules_mod_forum = $forums->generate_node();
305 // QUIZ
306 $node_course_modules_mod_quiz = $quiz->generate_node_course_modules_mod();
307 //TODO: label
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);
323 $i = 0;
325 if (!empty($topics)) {
327 foreach ($topics as $topic) {
329 $i++;
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,
340 $i - 1,
341 $topic['title'],
342 $node_node_course_sections_section_mods_mod);
344 } else {
346 $find_tags = array('[#section_id#]',
347 '[#section_number#]',
348 '[#section_summary#]',
349 '[#node_course_sections_section_mods_mod#]');
351 $replace_values = array($i,
352 $i - 1,
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 () {
368 global $CFG;
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'];
383 } else {
384 if (!empty($CFG->defaultblocks)) {
385 $blocknames = $CFG->defaultblocks;
386 } else {
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#]',
396 '[#block_name#]',
397 '[#block_position#]',
398 '[#block_weight#]');
400 $i = 0;
401 $weight = 0;
403 foreach ($blocks_left as $block) {
404 $i++;
405 $weight++;
407 $replace_values = array($i,
408 $block,
409 'l',
410 $weight);
412 $node_course_blocks_block .= str_replace($find_tags, $replace_values, $sheet_course_blocks_block);
415 $weight = 0;
417 foreach ($blocks_right as $block) {
419 $i++;
420 $weight ++;
422 $replace_values = array($i,
423 $block,
424 'r',
425 $weight);
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
438 * @return number
440 protected function get_module_visible($identifier) {
441 //Should item be hidden or not
442 $mod_visible = 1;
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) {
451 $mod_visible = 0;
455 return $mod_visible;
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);
463 if ($childs) {
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']) {
471 $is_summary = true;
472 } else {
473 $is_summary = false;
475 } else {
476 $is_summary = false;
479 if (!$is_summary) {
481 $indent = $child['deep'] - ROOT_DEEP;
483 if ($indent > 0) {
484 $indent = $indent - 1;
487 $find_tags = array('[#mod_id#]',
488 '[#mod_instance_id#]',
489 '[#mod_type#]',
490 '[#date_now#]',
491 '[#mod_indent#]',
492 '[#mod_visible#]');
494 $replace_values = array($child['index'],
495 $child['instance'],
496 $child['moodle_type'],
497 time(),
498 $indent,
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;
507 } else {
508 $response = '';
511 return $response;
515 public function get_nodes_by_criteria ($key, $value) {
517 $response = array();
519 if (array_key_exists('index', static::$instances)) {
520 foreach (static::$instances['index'] as $item) {
521 if ($item[$key] == $value) {
522 $response[] = $item;
527 return $response;
530 //Modified here
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) : '';
566 //TODO: label
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) {
583 $instance = '';
585 $find_tags = array('[#mod_instance_id#]',
586 '[#mod_name#]',
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 ) {
594 continue;
598 $replace_values = array($instances[$i - 1]['instance'],
599 entities::safexml($instances[$i - 1]['title']),
600 $user_info);
602 $instance .= str_replace($find_tags, $replace_values, $sheet);
605 return $instance;
609 protected function create_instances ($items, $level = 0, &$array_index = 0, $index_root = 0) {
611 $level++;
612 $i = 1;
614 if ($items) {
616 $xpath = self::newx_path(static::$manifest, static::$namespaces);
618 foreach ($items as $item) {
620 $array_index++;
622 if ($item->nodeName == "item") {
623 $identifierref = '';
624 if ($item->hasAttribute('identifierref')) {
625 $identifierref = $item->getAttribute('identifierref');
628 $title = '';
629 $titles = $xpath->query('imscc:title', $item);
630 if ($titles->length > 0) {
631 $title = $titles->item(0)->nodeValue;
634 $cc_type = $this->get_item_cc_type($identifierref);
635 $moodle_type = $this->convert_to_moodle_type($cc_type);
636 //Fix the label issue - MDL-33523
637 if (empty($identifierref) && empty($title)) {
638 $moodle_type = TYPE_UNKNOWN;
641 elseif ($item->nodeName == "resource") {
643 $identifierref = $xpath->query('@identifier', $item);
644 $identifierref = !empty($identifierref->item(0)->nodeValue) ? $identifierref->item(0)->nodeValue : '';
646 $cc_type = $this->get_item_cc_type($identifierref);
647 $moodle_type = $this->convert_to_moodle_type($cc_type);
649 $title = 'Quiz Bank ' . ($this->count_instances($moodle_type) + 1);
653 if ($level == ROOT_DEEP) {
654 $index_root = $array_index;
657 static::$instances['index'][$array_index]['common_cartriedge_type'] = $cc_type;
658 static::$instances['index'][$array_index]['moodle_type'] = $moodle_type;
659 static::$instances['index'][$array_index]['title'] = $title ? $title : '';
660 static::$instances['index'][$array_index]['root_parent'] = $index_root;
661 static::$instances['index'][$array_index]['index'] = $array_index;
662 static::$instances['index'][$array_index]['deep'] = $level;
663 static::$instances['index'][$array_index]['instance'] = $this->count_instances($moodle_type);
664 static::$instances['index'][$array_index]['resource_indentifier'] = $identifierref;
666 static::$instances['instances'][$moodle_type][] = array('title' => $title,
667 'instance' => static::$instances['index'][$array_index]['instance'],
668 'common_cartriedge_type' => $cc_type,
669 'resource_indentifier' => $identifierref,
670 'deep' => $level);
672 $more_items = $xpath->query('imscc:item', $item);
674 if ($more_items->length > 0) {
675 $this->create_instances($more_items, $level, $array_index, $index_root);
678 $i++;
684 public function count_instances ($type) {
686 $quantity = 0;
688 if (array_key_exists('index', static::$instances)) {
689 if (static::$instances['index'] && $type) {
691 foreach (static::$instances['index'] as $instance) {
692 if (!empty($instance['moodle_type'])) {
693 $types[] = $instance['moodle_type'];
697 $quantity_instances = array_count_values($types);
698 $quantity = array_key_exists($type, $quantity_instances) ? $quantity_instances[$type] : 0;
702 return $quantity;
705 public function convert_to_moodle_type ($cc_type) {
706 $type = TYPE_UNKNOWN;
708 if ($cc_type == static::CC_TYPE_FORUM) {
709 $type = MOODLE_TYPE_FORUM;
712 if ($cc_type == static::CC_TYPE_QUIZ) {
713 $type = MOODLE_TYPE_QUIZ;
716 if ($cc_type == static::CC_TYPE_WEBLINK) {
717 $type = MOODLE_TYPE_RESOURCE;
720 if ($cc_type == static::CC_TYPE_WEBCONTENT) {
721 $type = MOODLE_TYPE_RESOURCE;
724 if ($cc_type == static::CC_TYPE_ASSOCIATED_CONTENT) {
725 $type = MOODLE_TYPE_RESOURCE;
728 if ($cc_type == static::CC_TYPE_QUESTION_BANK) {
729 $type = MOODLE_TYPE_QUESTION_BANK;
731 //TODO: label
732 if ($cc_type == static::CC_TYPE_EMPTY) {
733 $type = MOODLE_TYPE_LABEL;
736 return $type;
739 public function get_item_cc_type ($identifier) {
741 $xpath = static::newx_path(static::$manifest, static::$namespaces);
743 $nodes = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $identifier . '"]/@type');
745 if ($nodes && !empty($nodes->item(0)->nodeValue)) {
746 return $nodes->item(0)->nodeValue;
747 } else {
748 return '';
752 public static function newx_path (DOMDocument $manifest, $namespaces = '') {
754 $xpath = new DOMXPath($manifest);
756 if (!empty($namespaces)) {
757 foreach ($namespaces as $prefix => $ns) {
758 if (!$xpath->registerNamespace($prefix, $ns)) {
759 static::log_action('Cannot register the namespace: ' . $prefix . ':' . $ns, true);
764 return $xpath;
767 public static function loadsheet ($file) {
769 $content = (is_readable($file) && ($content = file_get_contents($file))) ? $content : false;
771 static::log_action('Loading sheet: ' . $file);
773 if (!$content) {
774 static::log_action('Cannot load the xml sheet: ' . $file, true);
777 static::log_action('Load OK!');
779 return $content;
782 public static function log_file() {
783 return static::$path_to_manifest_folder . DIRECTORY_SEPARATOR . 'cc2moodle.log';
786 public static function log_action ($text, $critical_error = false) {
788 $full_message = strtoupper(date("j/n/Y g:i:s a")) . " - " . $text . "\r";
790 file_put_contents(static::log_file(), $full_message, FILE_APPEND);
792 if ($critical_error) {
793 static::critical_error($text);
797 protected function critical_error ($text) {
799 $path_to_log = static::log_file();
801 echo '
804 <hr />A critical error has been found!
806 <p>' . $text . '</p>
810 The process has been stopped. Please see the <a href="' . $path_to_log . '">log file</a> for more information.</p>
812 <p>Log: ' . $path_to_log . '</p>
814 <hr />
816 </p>
819 die();
822 protected function create_course_code ($title) {
823 //Making sure that text of the short name does not go over the DB limit.
824 //and leaving the space to add additional characters by the platform
825 $code = substr(strtoupper(str_replace(' ', '', trim($title))),0,94);
826 return $code;