Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/backup/cc/cc2moodle.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00023 defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
00024 
00025 require_once($CFG->dirroot . '/backup/cc/entities.class.php');
00026 require_once($CFG->dirroot . '/backup/cc/entity.label.class.php');
00027 require_once($CFG->dirroot . '/backup/cc/entity.resource.class.php');
00028 require_once($CFG->dirroot . '/backup/cc/entity.forum.class.php');
00029 require_once($CFG->dirroot . '/backup/cc/entity.quiz.class.php');
00030 
00031 class cc2moodle {
00032 
00033     const CC_TYPE_FORUM              = 'imsdt_xmlv1p0';
00034     const CC_TYPE_QUIZ               = 'imsqti_xmlv1p2/imscc_xmlv1p0/assessment';
00035     const CC_TYPE_QUESTION_BANK      = 'imsqti_xmlv1p2/imscc_xmlv1p0/question-bank';
00036     const CC_TYPE_WEBLINK            = 'imswl_xmlv1p0';
00037     const CC_TYPE_WEBCONTENT         = 'webcontent';
00038     const CC_TYPE_ASSOCIATED_CONTENT = 'associatedcontent/imscc_xmlv1p0/learning-application-resource';
00039     const CC_TYPE_EMPTY              = '';
00040 
00041     public static $restypes = array('associatedcontent/imscc_xmlv1p0/learning-application-resource', 'webcontent');
00042     public static $forumns  = array('dt' => 'http://www.imsglobal.org/xsd/imsdt_v1p0');
00043     public static $quizns   = array('xmlns' => 'http://www.imsglobal.org/xsd/ims_qtiasiv1p2');
00044     public static $resourcens = array('wl' => 'http://www.imsglobal.org/xsd/imswl_v1p0');
00049     public static function getquizns() {
00050         return static::$quizns;
00051     }
00052 
00057     public static function getforumns() {
00058         return static::$forumns;
00059     }
00060 
00065     public static function getresourcens() {
00066         return static::$resourcens;
00067     }
00068 
00069     public static function get_manifest($folder) {
00070         if (!is_dir($folder)) {
00071             return false;
00072         }
00073 
00074         $result = false;
00075         try {
00076             $dirIter = new RecursiveDirectoryIterator($folder, RecursiveDirectoryIterator::KEY_AS_PATHNAME);
00077             $recIter = new RecursiveIteratorIterator($dirIter, RecursiveIteratorIterator::CHILD_FIRST);
00078             foreach ($recIter as $info) {
00079                 if ($info->isFile() && ($info->getFilename() == 'imsmanifest.xml')) {
00080                     $result = $info->getPathname();
00081                     break;
00082                 }
00083             }
00084         } catch (Exception $e) {}
00085 
00086         return $result;
00087     }
00088 
00089     public static $instances = array();
00090     public static $manifest;
00091     public static $path_to_manifest_folder;
00092 
00093     public static $namespaces = array('imscc'    => 'http://www.imsglobal.org/xsd/imscc/imscp_v1p1',
00094                                       'lomimscc' => 'http://ltsc.ieee.org/xsd/imscc/LOM',
00095                                       'lom'      => 'http://ltsc.ieee.org/xsd/LOM',
00096                                       'voc'      => 'http://ltsc.ieee.org/xsd/LOM/vocab',
00097                                       'xsi'      => 'http://www.w3.org/2001/XMLSchema-instance',
00098                                       'cc'       => 'http://www.imsglobal.org/xsd/imsccauth_v1p0');
00099 
00100     function __construct ($path_to_manifest) {
00101 
00102         static::$manifest = new DOMDocument();
00103         static::$manifest->validateOnParse = false;
00104 
00105         static::$path_to_manifest_folder = dirname($path_to_manifest);
00106 
00107         static::log_action('Proccess start');
00108         static::log_action('Load the manifest file: ' . $path_to_manifest);
00109 
00110         if (!static::$manifest->load($path_to_manifest, LIBXML_NONET)) {
00111             static::log_action('Cannot load the manifest file: ' . $path_to_manifest, true);
00112         }
00113     }
00114 
00115     public function is_auth () {
00116 
00117         $xpath = static::newx_path(static::$manifest, static::$namespaces);
00118 
00119         $count_auth = $xpath->evaluate('count(/imscc:manifest/cc:authorizations)');
00120 
00121         if ($count_auth > 0) {
00122             $response = true;
00123         } else {
00124             $response = false;
00125         }
00126 
00127         return $response;
00128     }
00129 
00130     protected function get_metadata ($section, $key) {
00131 
00132         $xpath = static::newx_path(static::$manifest, static::$namespaces);
00133 
00134         $metadata = $xpath->query('/imscc:manifest/imscc:metadata/lomimscc:lom/lomimscc:' . $section . '/lomimscc:' . $key . '/lomimscc:string');
00135         $value = !empty($metadata->item(0)->nodeValue) ? $metadata->item(0)->nodeValue : '';
00136 
00137         return $value;
00138     }
00139 
00140     public function generate_moodle_xml () {
00141 
00142         global $CFG;
00143 
00144         $cdir = static::$path_to_manifest_folder . DIRECTORY_SEPARATOR . 'course_files';
00145 
00146         if (!file_exists($cdir)) {
00147             mkdir($cdir);
00148         }
00149 
00150         $sheet_base = static::loadsheet(SHEET_BASE);
00151 
00152         // MOODLE_BACKUP / INFO / DETAILS / MOD
00153         $node_info_details_mod = $this->create_code_info_details_mod();
00154 
00155         // MOODLE_BACKUP / BLOCKS / BLOCK
00156         $node_course_blocks_block = $this->create_node_course_blocks_block();
00157 
00158         // MOODLE_BACKUP / COURSES / SECTIONS / SECTION
00159         $node_course_sections_section = $this->create_node_course_sections_section();
00160 
00161         // MOODLE_BACKUP / COURSES / QUESTION_CATEGORIES
00162         $node_course_question_categories = $this->create_node_question_categories();
00163 
00164         // MOODLE_BACKUP / COURSES / MODULES / MOD
00165         $node_course_modules_mod = $this->create_node_course_modules_mod();
00166 
00167         // MOODLE_BACKUP / COURSE / HEADER
00168         $node_course_header = $this->create_node_course_header();
00169 
00170         // GENERAL INFO
00171         $filename = optional_param('file', 'not_available.zip', PARAM_RAW);
00172         $filename = basename($filename);
00173 
00174         $www_root = $CFG->wwwroot;
00175 
00176         $find_tags = array('[#zip_filename#]',
00177                            '[#www_root#]',
00178                            '[#node_course_header#]',
00179                            '[#node_info_details_mod#]',
00180                            '[#node_course_blocks_block#]',
00181                            '[#node_course_sections_section#]',
00182                            '[#node_course_question_categories#]',
00183                            '[#node_course_modules#]');
00184 
00185         $replace_values = array($filename,
00186                                 $www_root,
00187                                 $node_course_header,
00188                                 $node_info_details_mod,
00189                                 $node_course_blocks_block,
00190                                 $node_course_sections_section,
00191                                 $node_course_question_categories,
00192                                 $node_course_modules_mod);
00193 
00194         $result_xml = str_replace($find_tags, $replace_values, $sheet_base);
00195 
00196         // COPY RESOURSE FILES
00197         $entities = new entities();
00198 
00199         $entities->move_all_files();
00200 
00201         if (array_key_exists("index", self::$instances)) {
00202 
00203             if (!file_put_contents(static::$path_to_manifest_folder . DIRECTORY_SEPARATOR . 'moodle.xml', $result_xml)) {
00204                 static::log_action('Cannot save the moodle manifest file: ' . static::$path_to_tmp_folder . DIRECTORY_SEPARATOR . 'moodle.xml', true);
00205             } else {
00206                 $status = true;
00207             }
00208 
00209         } else {
00210             $status = false;
00211             notify('The course is empty');
00212             static::log_action('The course is empty', false);
00213         }
00214 
00215         return $status;
00216 
00217     }
00218 
00219     protected function get_sections_numbers ($instances) {
00220 
00221         $count = 0;
00222 
00223         if (array_key_exists("index", $instances)) {
00224             foreach ($instances["index"] as $instance) {
00225                 if ($instance["deep"] == ROOT_DEEP) {
00226                     $count++;
00227                 }
00228             }
00229         }
00230 
00231         return $count;
00232     }
00233 
00234     protected function create_node_course_header () {
00235 
00236         $node_course_header = '';
00237         $sheet_course_header = static::loadsheet(SHEET_COURSE_HEADER);
00238 
00239         $course_title = trim($this->get_metadata('general', 'title'));
00240         $course_title = empty($course_title) ? 'Untitled Course' : $course_title;
00241         $course_description = $this->get_metadata('general', 'description');
00242         $section_count = $this->get_sections_numbers(static::$instances) - 1;
00243 
00244         if ($section_count == -1) {
00245             $section_count = 0;
00246         }
00247 
00248         if (empty($course_title)) {
00249             $this->log_action('The course title not found', true);
00250         }
00251 
00252         $course_short_name = $this->create_course_code($course_title);
00253 
00254         $find_tags = array('[#course_name#]',
00255                            '[#course_short_name#]',
00256                            '[#course_description#]',
00257                            '[#date_now#]',
00258                            '[#section_count#]');
00259 
00260         $replace_values = array(entities::safexml($course_title),
00261                                 entities::safexml($course_short_name),
00262                                 entities::safexml($course_description),
00263                                 time(),
00264                                 $section_count);
00265 
00266         $node_course_header = str_replace($find_tags, $replace_values, $sheet_course_header);
00267 
00268         return $node_course_header;
00269     }
00270 
00271     protected function create_node_question_categories () {
00272 
00273         $quiz = new cc_quiz();
00274 
00275         static::log_action('Creating node: QUESTION_CATEGORIES');
00276 
00277         $node_course_question_categories = $quiz->generate_node_question_categories();
00278 
00279         return $node_course_question_categories;
00280     }
00281 
00282     protected function create_node_course_modules_mod () {
00283 
00284         $labels = new cc_label();
00285         $resources = new cc_resource();
00286         $forums = new cc_forum();
00287         $quiz = new cc_quiz();
00288 
00289         static::log_action('Creating node: COURSE/MODULES/MOD');
00290 
00291         // LABELS
00292         $node_course_modules_mod_label = $labels->generate_node();
00293 
00294         // RESOURCES (WEB CONTENT AND WEB LINK)
00295         $node_course_modules_mod_resource = $resources->generate_node();
00296 
00297         // FORUMS
00298         $node_course_modules_mod_forum = $forums->generate_node();
00299 
00300         // QUIZ
00301         $node_course_modules_mod_quiz = $quiz->generate_node_course_modules_mod();
00302         //TODO: label
00303         $node_course_modules = $node_course_modules_mod_label . $node_course_modules_mod_resource . $node_course_modules_mod_forum . $node_course_modules_mod_quiz;
00304 
00305         return $node_course_modules;
00306     }
00307 
00308 
00309     protected function create_node_course_sections_section () {
00310 
00311         static::log_action('Creating node: COURSE/SECTIONS/SECTION');
00312 
00313         $node_course_sections_section = '';
00314         $sheet_course_sections_section = static::loadsheet(SHEET_COURSE_SECTIONS_SECTION);
00315 
00316         $topics = $this->get_nodes_by_criteria('deep', ROOT_DEEP);
00317 
00318         $i = 0;
00319 
00320         if (!empty($topics)) {
00321 
00322             foreach ($topics as $topic) {
00323 
00324                 $i++;
00325                 $node_node_course_sections_section_mods_mod = $this->create_node_course_sections_section_mods_mod($topic['index']);
00326 
00327                 if ($topic['moodle_type'] == MOODLE_TYPE_LABEL) {
00328 
00329                     $find_tags = array('[#section_id#]',
00330                                        '[#section_number#]',
00331                                        '[#section_summary#]',
00332                                        '[#node_course_sections_section_mods_mod#]');
00333 
00334                     $replace_values = array($i,
00335                                             $i - 1,
00336                                             $topic['title'],
00337                                             $node_node_course_sections_section_mods_mod);
00338 
00339                 } else {
00340 
00341                     $find_tags = array('[#section_id#]',
00342                                        '[#section_number#]',
00343                                        '[#section_summary#]',
00344                                        '[#node_course_sections_section_mods_mod#]');
00345 
00346                     $replace_values = array($i,
00347                                             $i - 1,
00348                                             '',
00349                                             $node_node_course_sections_section_mods_mod);
00350 
00351                 }
00352 
00353                 $node_course_sections_section .= str_replace($find_tags, $replace_values, $sheet_course_sections_section);
00354             }
00355         }
00356 
00357 
00358         return $node_course_sections_section;
00359     }
00360 
00361     protected function create_node_course_blocks_block () {
00362 
00363         global $CFG;
00364 
00365         static::log_action('Creating node: COURSE/BLOCKS/BLOCK');
00366 
00367         $sheet_course_blocks_block = static::loadsheet(SHEET_COURSE_BLOCKS_BLOCK);
00368         $node_course_blocks_block = '';
00369 
00370         $format_config = $CFG->dirroot . '/course/format/weeks/config.php';
00371 
00372         if (@is_file($format_config) && is_readable($format_config)) {
00373             require ($format_config);
00374         }
00375 
00376         if (!empty($format['defaultblocks'])) {
00377             $blocknames = $format['defaultblocks'];
00378         } else {
00379             if (!empty($CFG->defaultblocks)) {
00380                 $blocknames = $CFG->defaultblocks;
00381             } else {
00382                 $blocknames = 'participants,activity_modules,search_forums,course_list:news_items,calendar_upcoming,recent_activity';
00383             }
00384         }
00385 
00386         $blocknames = explode(':', $blocknames);
00387         $blocks_left = explode(',', $blocknames[0]);
00388         $blocks_right = explode(',', $blocknames[1]);
00389 
00390         $find_tags = array('[#block_id#]',
00391                            '[#block_name#]',
00392                            '[#block_position#]',
00393                            '[#block_weight#]');
00394 
00395         $i = 0;
00396         $weight = 0;
00397 
00398         foreach ($blocks_left as $block) {
00399             $i++;
00400             $weight++;
00401 
00402             $replace_values = array($i,
00403                                     $block,
00404                                     'l',
00405                                     $weight);
00406 
00407             $node_course_blocks_block .= str_replace($find_tags, $replace_values, $sheet_course_blocks_block);
00408         }
00409 
00410         $weight = 0;
00411 
00412         foreach ($blocks_right as $block) {
00413 
00414             $i++;
00415             $weight ++;
00416 
00417             $replace_values = array($i,
00418                                     $block,
00419                                     'r',
00420                                     $weight);
00421 
00422             $node_course_blocks_block .= str_replace($find_tags, $replace_values, $sheet_course_blocks_block);
00423         }
00424 
00425         return $node_course_blocks_block;
00426 
00427     }
00428 
00435     protected function get_module_visible($identifier) {
00436         //Should item be hidden or not
00437         $mod_visible = 1;
00438         if (!empty($identifier)) {
00439             $xpath = static::newx_path(static::$manifest, static::$namespaces);
00440             $query  = '/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $identifier . '"]';
00441             $query .= '//lom:intendedEndUserRole/voc:vocabulary/lom:value';
00442             $intendeduserrole = $xpath->query($query);
00443             if (!empty($intendeduserrole) && ($intendeduserrole->length > 0)) {
00444                 $role = trim($intendeduserrole->item(0)->nodeValue);
00445                 if (strcasecmp('Instructor', $role) == 0) {
00446                     $mod_visible = 0;
00447                 }
00448             }
00449         }
00450         return $mod_visible;
00451     }
00452 
00453     protected function create_node_course_sections_section_mods_mod ($root_parent) {
00454 
00455         $sheet_course_sections_section_mods_mod = static::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD);
00456         $childs = $this->get_nodes_by_criteria('root_parent', $root_parent);
00457 
00458         if ($childs) {
00459 
00460             $node_course_sections_section_mods_mod = '';
00461 
00462             foreach ($childs as $child) {
00463 
00464                 if ($child['moodle_type'] == MOODLE_TYPE_LABEL) {
00465                     if ($child['index'] == $child['root_parent']) {
00466                         $is_summary = true;
00467                     } else {
00468                         $is_summary = false;
00469                     }
00470                 } else {
00471                     $is_summary = false;
00472                 }
00473 
00474                 if (!$is_summary) {
00475 
00476                     $indent = $child['deep'] - ROOT_DEEP;
00477 
00478                     if ($indent > 0) {
00479                         $indent = $indent - 1;
00480                     }
00481 
00482                     $find_tags = array('[#mod_id#]',
00483                                        '[#mod_instance_id#]',
00484                                        '[#mod_type#]',
00485                                        '[#date_now#]',
00486                                        '[#mod_indent#]',
00487                                        '[#mod_visible#]');
00488 
00489                     $replace_values = array($child['index'],
00490                                             $child['instance'],
00491                                             $child['moodle_type'],
00492                                             time(),
00493                                             $indent,
00494                                             $this->get_module_visible($child['resource_indentifier']));
00495 
00496                     $node_course_sections_section_mods_mod .= str_replace($find_tags, $replace_values, $sheet_course_sections_section_mods_mod);
00497                 }
00498             }
00499 
00500             $response = $node_course_sections_section_mods_mod;
00501 
00502         } else {
00503             $response = '';
00504         }
00505 
00506         return $response;
00507 
00508     }
00509 
00510     public function get_nodes_by_criteria ($key, $value) {
00511 
00512         $response = array();
00513 
00514         if (array_key_exists('index', static::$instances)) {
00515             foreach (static::$instances['index'] as $item) {
00516                 if ($item[$key] == $value) {
00517                     $response[] = $item;
00518                 }
00519             }
00520         }
00521 
00522         return $response;
00523     }
00524 
00525     //Modified here
00526     protected function create_code_info_details_mod () {
00527 
00528         static::log_action('Creating node: INFO/DETAILS/MOD');
00529 
00530         $xpath = static::newx_path(static::$manifest, static::$namespaces);
00531 
00532         $items = $xpath->query('/imscc:manifest/imscc:organizations/imscc:organization/imscc:item | /imscc:manifest/imscc:resources/imscc:resource[@type="' . static::CC_TYPE_QUESTION_BANK . '"]');
00533 
00534         $this->create_instances($items);
00535 
00536         $count_quiz = $this->count_instances(MOODLE_TYPE_QUIZ);
00537         $count_forum = $this->count_instances(MOODLE_TYPE_FORUM);
00538         $count_resource = $this->count_instances(MOODLE_TYPE_RESOURCE);
00539         $count_label = $this->count_instances(MOODLE_TYPE_LABEL);
00540 
00541         $sheet_info_details_mod_instances_instance = static::loadsheet(SHEET_INFO_DETAILS_MOD_INSTANCE);
00542 
00543         if ($count_resource > 0) {
00544             $resource_instance = $this->create_mod_info_details_mod_instances_instance($sheet_info_details_mod_instances_instance, $count_resource, static::$instances['instances'][MOODLE_TYPE_RESOURCE]);
00545         }
00546         if ($count_quiz > 0) {
00547             $quiz_instance = $this->create_mod_info_details_mod_instances_instance($sheet_info_details_mod_instances_instance, $count_quiz, static::$instances['instances'][MOODLE_TYPE_QUIZ]);
00548         }
00549         if ($count_forum > 0) {
00550             $forum_instance = $this->create_mod_info_details_mod_instances_instance($sheet_info_details_mod_instances_instance, $count_forum, static::$instances['instances'][MOODLE_TYPE_FORUM]);
00551         }
00552         if ($count_label > 0) {
00553             $label_instance = $this->create_mod_info_details_mod_instances_instance($sheet_info_details_mod_instances_instance, $count_label, static::$instances['instances'][MOODLE_TYPE_LABEL]);
00554         }
00555 
00556         $resource_mod = $count_resource ? $this->create_mod_info_details_mod(MOODLE_TYPE_RESOURCE, $resource_instance) : '';
00557         $quiz_mod = $count_quiz ? $this->create_mod_info_details_mod(MOODLE_TYPE_QUIZ, $quiz_instance) : '';
00558         $forum_mod = $count_forum ? $this->create_mod_info_details_mod(MOODLE_TYPE_FORUM, $forum_instance) : '';
00559         $label_mod = $count_label ? $this->create_mod_info_details_mod(MOODLE_TYPE_LABEL, $label_instance) : '';
00560 
00561         //TODO: label
00562         return $label_mod . $resource_mod . $quiz_mod . $forum_mod;
00563 
00564     }
00565 
00566     protected function create_mod_info_details_mod ($mod_type, $node_info_details_mod_instances_instance) {
00567 
00568         $sheet_info_details_mod = static::loadsheet(SHEET_INFO_DETAILS_MOD);
00569 
00570         $find_tags = array('[#mod_type#]' ,'[#node_info_details_mod_instances_instance#]');
00571         $replace_values = array($mod_type , $node_info_details_mod_instances_instance);
00572 
00573         return str_replace($find_tags, $replace_values, $sheet_info_details_mod);
00574     }
00575 
00576     protected function create_mod_info_details_mod_instances_instance ($sheet, $instances_quantity, $instances) {
00577 
00578         $instance = '';
00579 
00580         $find_tags = array('[#mod_instance_id#]',
00581                            '[#mod_name#]',
00582                            '[#mod_user_info#]');
00583 
00584         for ($i = 1; $i <= $instances_quantity; $i++) {
00585 
00586             $user_info = ($instances[$i - 1]['common_cartriedge_type'] == static::CC_TYPE_FORUM) ? 'true' : 'false';
00587             if ($instances[$i - 1]['common_cartriedge_type'] == static::CC_TYPE_EMPTY) {
00588                 if ($instances[$i - 1]['deep'] <= ROOT_DEEP ) {
00589                     continue;
00590                 }
00591             }
00592 
00593             $replace_values = array($instances[$i - 1]['instance'],
00594                                     entities::safexml($instances[$i - 1]['title']),
00595                                     $user_info);
00596 
00597             $instance .= str_replace($find_tags, $replace_values, $sheet);
00598         }
00599 
00600         return $instance;
00601 
00602     }
00603 
00604     protected function create_instances ($items, $level = 0, &$array_index = 0, $index_root = 0) {
00605 
00606         $level++;
00607         $i = 1;
00608 
00609         if ($items) {
00610 
00611             $xpath = self::newx_path(static::$manifest, static::$namespaces);
00612 
00613             foreach ($items as $item) {
00614 
00615                 $array_index++;
00616 
00617                 if ($item->nodeName == "item")  {
00618 
00619                     $identifierref = $xpath->query('@identifierref', $item);
00620                     $identifierref = !empty($identifierref->item(0)->nodeValue) ? $identifierref->item(0)->nodeValue : '';
00621 
00622                     $title = $xpath->query('imscc:title', $item);
00623                     $title = !empty($title->item(0)->nodeValue) ? $title->item(0)->nodeValue : '';
00624 
00625                     $cc_type = $this->get_item_cc_type($identifierref);
00626                     $moodle_type = $this->convert_to_moodle_type($cc_type);
00627                 }
00628                 elseif ($item->nodeName == "resource")  {
00629 
00630                     $identifierref = $xpath->query('@identifier', $item);
00631                     $identifierref = !empty($identifierref->item(0)->nodeValue) ? $identifierref->item(0)->nodeValue : '';
00632 
00633                     $cc_type = $this->get_item_cc_type($identifierref);
00634                     $moodle_type = $this->convert_to_moodle_type($cc_type);
00635 
00636                     $title = 'Quiz Bank ' . ($this->count_instances($moodle_type) + 1);
00637 
00638                 }
00639 
00640                 if ($level == ROOT_DEEP) {
00641                     $index_root = $array_index;
00642                 }
00643 
00644                 static::$instances['index'][$array_index]['common_cartriedge_type'] = $cc_type;
00645                 static::$instances['index'][$array_index]['moodle_type'] = $moodle_type;
00646                 static::$instances['index'][$array_index]['title'] = $title ? $title : '';
00647                 static::$instances['index'][$array_index]['root_parent'] = $index_root;
00648                 static::$instances['index'][$array_index]['index'] = $array_index;
00649                 static::$instances['index'][$array_index]['deep'] = $level;
00650                 static::$instances['index'][$array_index]['instance'] = $this->count_instances($moodle_type);
00651                 static::$instances['index'][$array_index]['resource_indentifier'] = $identifierref;
00652 
00653                 static::$instances['instances'][$moodle_type][] = array('title' => $title,
00654                                                                         'instance' => static::$instances['index'][$array_index]['instance'],
00655                                                                         'common_cartriedge_type' => $cc_type,
00656                                                                         'resource_indentifier' => $identifierref,
00657                                                                         'deep' => $level);
00658 
00659                 $more_items = $xpath->query('imscc:item', $item);
00660 
00661                 if ($more_items->length > 0) {
00662                     $this->create_instances($more_items, $level, $array_index, $index_root);
00663                 }
00664 
00665                 $i++;
00666 
00667             }
00668         }
00669     }
00670 
00671     public function count_instances ($type) {
00672 
00673         $quantity = 0;
00674 
00675         if (array_key_exists('index', static::$instances)) {
00676             if (static::$instances['index'] && $type) {
00677 
00678                 foreach (static::$instances['index'] as $instance) {
00679                     if (!empty($instance['moodle_type'])) {
00680                         $types[] = $instance['moodle_type'];
00681                     }
00682                 }
00683 
00684                 $quantity_instances = array_count_values($types);
00685                 $quantity = array_key_exists($type, $quantity_instances) ? $quantity_instances[$type] : 0;
00686             }
00687         }
00688 
00689         return $quantity;
00690     }
00691 
00692     public function convert_to_moodle_type ($cc_type) {
00693         $type = TYPE_UNKNOWN;
00694 
00695         if ($cc_type == static::CC_TYPE_FORUM) {
00696             $type = MOODLE_TYPE_FORUM;
00697         }
00698 
00699         if ($cc_type == static::CC_TYPE_QUIZ) {
00700             $type = MOODLE_TYPE_QUIZ;
00701         }
00702 
00703         if ($cc_type == static::CC_TYPE_WEBLINK) {
00704             $type = MOODLE_TYPE_RESOURCE;
00705         }
00706 
00707         if ($cc_type == static::CC_TYPE_WEBCONTENT) {
00708             $type = MOODLE_TYPE_RESOURCE;
00709         }
00710 
00711         if ($cc_type == static::CC_TYPE_ASSOCIATED_CONTENT) {
00712             $type = MOODLE_TYPE_RESOURCE;
00713         }
00714 
00715         if ($cc_type == static::CC_TYPE_QUESTION_BANK) {
00716             $type = MOODLE_TYPE_QUESTION_BANK;
00717         }
00718         //TODO: label
00719         if ($cc_type == static::CC_TYPE_EMPTY) {
00720             $type = MOODLE_TYPE_LABEL;
00721         }
00722 
00723         return $type;
00724     }
00725 
00726     public function get_item_cc_type ($identifier) {
00727 
00728         $xpath = static::newx_path(static::$manifest, static::$namespaces);
00729 
00730         $nodes = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $identifier . '"]/@type');
00731 
00732         if ($nodes && !empty($nodes->item(0)->nodeValue)) {
00733             return $nodes->item(0)->nodeValue;
00734         } else {
00735             return '';
00736         }
00737     }
00738 
00739     public static function newx_path (DOMDocument $manifest, $namespaces = '') {
00740 
00741         $xpath = new DOMXPath($manifest);
00742 
00743         if (!empty($namespaces)) {
00744             foreach ($namespaces as $prefix => $ns) {
00745                 if (!$xpath->registerNamespace($prefix, $ns)) {
00746                     static::log_action('Cannot register the namespace: ' . $prefix . ':' . $ns, true);
00747                 }
00748             }
00749         }
00750 
00751         return $xpath;
00752     }
00753 
00754     public static function loadsheet ($file) {
00755 
00756         $content = (is_readable($file) && ($content = file_get_contents($file))) ? $content : false;
00757 
00758         static::log_action('Loading sheet: ' . $file);
00759 
00760         if (!$content) {
00761             static::log_action('Cannot load the xml sheet: ' . $file, true);
00762         }
00763 
00764         static::log_action('Load OK!');
00765 
00766         return $content;
00767     }
00768 
00769     public static function log_file() {
00770         return static::$path_to_manifest_folder . DIRECTORY_SEPARATOR . 'cc2moodle.log';
00771     }
00772 
00773     public static function log_action ($text, $critical_error = false) {
00774 
00775         $full_message = strtoupper(date("j/n/Y g:i:s a")) . " - " . $text . "\r";
00776 
00777         file_put_contents(static::log_file(), $full_message, FILE_APPEND);
00778 
00779         if ($critical_error) {
00780             static::critical_error($text);
00781         }
00782     }
00783 
00784     protected function critical_error ($text) {
00785 
00786         $path_to_log = static::log_file();
00787 
00788         echo '
00789 
00790         <p>
00791         <hr />A critical error has been found!
00792 
00793         <p>' . $text . '</p>
00794 
00795 
00796         <p>
00797         The process has been stopped. Please see the <a href="' . $path_to_log . '">log file</a> for more information.</p>
00798 
00799         <p>Log: ' . $path_to_log . '</p>
00800 
00801         <hr />
00802 
00803         </p>
00804         ';
00805 
00806         die();
00807     }
00808 
00809     protected function create_course_code ($title) {
00810         //Making sure that text of the short name does not go over the DB limit.
00811         //and leaving the space to add additional characters by the platform
00812         $code = substr(strtoupper(str_replace(' ', '', trim($title))),0,94);
00813         return $code;
00814     }
00815 }
 All Data Structures Namespaces Files Functions Variables Enumerations