Ejemplo n.º 1
0
function cc_convert($dir)
{
    global $OUTPUT;
    $manifest_file = $dir . DIRECTORY_SEPARATOR . 'imsmanifest.xml';
    $moodle_file = $dir . DIRECTORY_SEPARATOR . 'moodle.xml';
    $schema_file = 'cc' . DIRECTORY_SEPARATOR . '' . DIRECTORY_SEPARATOR . 'schemas' . DIRECTORY_SEPARATOR . 'cclibxml2validator.xsd';
    if (is_readable($manifest_file) && !is_readable($moodle_file)) {
        $is_cc = detect_cc_format($manifest_file);
        if ($is_cc) {
            $detected_requirements = detect_requirements();
            if (!$detected_requirements["php5"]) {
                echo $OUTPUT->notification(get_string('cc_import_req_php5', 'imscc'));
                return false;
            }
            if (!$detected_requirements["dom"]) {
                echo $OUTPUT->notification(get_string('cc_import_req_dom', 'imscc'));
                return false;
            }
            if (!$detected_requirements["libxml"]) {
                echo $OUTPUT->notification(get_string('cc_import_req_libxml', 'imscc'));
                return false;
            }
            if (!$detected_requirements["libxmlminversion"]) {
                echo $OUTPUT->notification(get_string('cc_import_req_libxmlminversion', 'imscc'));
                return false;
            }
            if (!$detected_requirements["xsl"]) {
                echo $OUTPUT->notification(get_string('cc_import_req_xsl', 'imscc'));
                return false;
            }
            echo get_string('cc2moodle_checking_schema', 'imscc') . '<br />';
            $cc_manifest = new DOMDocument();
            if ($cc_manifest->load($manifest_file)) {
                if ($cc_manifest->schemaValidate($schema_file)) {
                    echo get_string('cc2moodle_valid_schema', 'imscc') . '<br />';
                    $cc2moodle = new cc2moodle($manifest_file);
                    if (!$cc2moodle->is_auth()) {
                        return $cc2moodle->generate_moodle_xml();
                    } else {
                        echo $OUTPUT->notification(get_string('cc2moodle_req_auth', 'imscc'));
                        return false;
                    }
                } else {
                    echo $OUTPUT->notification(get_string('cc2moodle_invalid_schema', 'imscc'));
                    return false;
                }
            } else {
                echo $OUTPUT->notification(get_string('cc2moodle_manifest_dont_load', 'imscc'));
                return false;
            }
        }
    }
    return true;
}
 private function create_node_course_modules_mod_resource($sheet_mod_resource, $instance)
 {
     $link = '';
     $xpath = cc2moodle::newx_path(CC2Moodle::$manifest, CC2Moodle::$namespaces);
     if ($instance['common_cartriedge_type'] == CC_TYPE_WEBCONTENT || $instance['common_cartriedge_type'] == CC_TYPE_ASSOCIATED_CONTENT) {
         $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/@href');
         $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : '';
         if (empty($resource)) {
             unset($resource);
             $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href');
             $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : '';
         }
         if (!empty($resource)) {
             $link = $resource;
         }
     }
     if ($instance['common_cartriedge_type'] == CC_TYPE_WEBLINK) {
         $external_resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href')->item(0)->nodeValue;
         if ($external_resource) {
             $resource = $this->load_xml_resource(cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $external_resource);
             if (!empty($resource)) {
                 $xpath = cc2moodle::newx_path($resource, $this->namespaces);
                 $resource = $xpath->query('/wl:webLink/url/@href');
                 $link = $resource->item(0)->nodeValue;
             }
         }
     }
     $find_tags = array('[#mod_instance#]', '[#mod_name#]', '[#mod_type#]', '[#mod_reference#]', '[#mod_summary#]', '[#mod_alltext#]', '[#date_now#]');
     $replace_values = array($instance['instance'], $instance['title'], 'file', $link, '', '', time());
     return str_replace($find_tags, $replace_values, $sheet_mod_resource);
 }
Ejemplo n.º 3
0
 public function get_topic_data($instance)
 {
     $topic_data = '';
     $namespaces = array('dt' => 'http://www.imsglobal.org/xsd/imsdt_v1p0');
     $topic_file = $this->get_external_xml($instance['resource_indentifier']);
     if (!empty($topic_file)) {
         $topic = $this->load_xml_resource(cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $topic_file);
         if (!empty($topic)) {
             $xpath = cc2moodle::newx_path($topic, $namespaces);
             $topic_title = $xpath->query('/dt:topic/title');
             $topic_title = !empty($topic_title->item(0)->nodeValue) ? $topic_title->item(0)->nodeValue : '';
             $topic_text = $xpath->query('/dt:topic/text');
             $topic_text = !empty($topic_text->item(0)->nodeValue) ? $this->update_sources($topic_text->item(0)->nodeValue, dirname($topic_file)) : '';
             $topic_text = !empty($topic_text) ? str_replace("%24", "\$", $this->include_titles($topic_text)) : '';
             if (!empty($topic_title)) {
                 $topic_data['title'] = $topic_title;
                 $topic_data['description'] = $topic_text;
             }
         }
         $topic_attachments = $xpath->query('/dt:topic/attachments/attachment/@href');
         if ($topic_attachments->length > 0) {
             $attachment_html = '';
             foreach ($topic_attachments as $file) {
                 $attachment_html .= $this->generate_attachment_html($file->nodeValue);
             }
             $topic_data['description'] = !empty($attachment_html) ? $topic_text . '<p>Attachments:</p>' . $attachment_html : $topic_text;
         }
     }
     return $topic_data;
 }
Ejemplo n.º 4
0
 public function get_basiclti_data($instance)
 {
     $topic_data = '';
     $basiclti_file = $this->get_external_xml($instance['resource_indentifier']);
     if (!empty($basiclti_file)) {
         $basiclti_file_path = cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $basiclti_file;
         $basiclti_file_dir = dirname($basiclti_file_path);
         $basiclti = $this->load_xml_resource($basiclti_file_path);
         if (!empty($basiclti)) {
             $xpath = cc2moodle::newx_path($basiclti, cc112moodle::$basicltins);
             $topic_title = $this->getValue($xpath->query('/xmlns:cartridge_basiclti_link/blti:title'), 'Untitled');
             $blti_description = $this->getValue($xpath->query('/xmlns:cartridge_basiclti_link/blti:description'));
             $launch_url = $this->getValue($xpath->query('/xmlns:cartridge_basiclti_link/blti:launch_url'));
             $tool_raw = $this->getValue($xpath->query('/xmlns:cartridge_basiclti_link/blti:vendor/lticp:code'), null);
             $tool_url = $this->getValue($xpath->query('/xmlns:cartridge_basiclti_link/blti:vendor/lticp:url'), null);
             $tool_desc = $this->getValue($xpath->query('/xmlns:cartridge_basiclti_link/blti:vendor/lticp:description'), null);
             $topic_data['title'] = $topic_title;
             $topic_data['description'] = $blti_description;
             $topic_data['launchurl'] = $launch_url;
             $topic_data['orgid'] = $tool_raw;
             $topic_data['orgurl'] = $tool_url;
             $topic_data['orgdesc'] = $tool_desc;
         }
     }
     return $topic_data;
 }
Ejemplo n.º 5
0
 function __construct($path_to_manifest)
 {
     self::$manifest = new DOMDocument();
     self::$path_to_manifest_folder = dirname($path_to_manifest);
     self::log_action('Proccess start');
     self::log_action('Load the manifest file: ' . $path_to_manifest);
     if (!self::$manifest->load($path_to_manifest)) {
         self::log_action('Cannot load the manifest file: ' . $path_to_manifest, true);
     }
 }
Ejemplo n.º 6
0
 public function generate_node()
 {
     cc2moodle::log_action('Creating Labels mods');
     $response = '';
     $sheet_mod_label = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_LABEL);
     if (!empty(cc2moodle::$instances['instances'][MOODLE_TYPE_LABEL])) {
         foreach (cc2moodle::$instances['instances'][MOODLE_TYPE_LABEL] as $instance) {
             $response .= $this->create_node_course_modules_mod_label($sheet_mod_label, $instance);
         }
     }
     return $response;
 }
Ejemplo n.º 7
0
 protected function execute()
 {
     global $CFG;
     $manifest = cc2moodle::get_manifest($this->get_tempdir_path());
     if (empty($manifest)) {
         throw new imscc1_convert_exception('No Manifest detected!');
     }
     $this->log('validating manifest', backup::LOG_DEBUG, null, 1);
     $validator = new manifest10_validator($CFG->dirroot . '/backup/cc/schemas');
     if (!$validator->validate($manifest)) {
         $this->log('validation error(s): ' . PHP_EOL . error_messages::instance(), backup::LOG_DEBUG, null, 2);
         throw new imscc1_convert_exception(error_messages::instance()->to_string(true));
     }
     $manifestdir = dirname($manifest);
     $cc2moodle = new cc2moodle($manifest);
     if ($cc2moodle->is_auth()) {
         throw new imscc1_convert_exception('Protected cartridge content - Skipping import!');
     }
     $status = $cc2moodle->generate_moodle_xml();
     //Final cleanup
     $xml_error = new libxml_errors_mgr(true);
     $mdoc = new DOMDocument();
     $mdoc->preserveWhiteSpace = false;
     $mdoc->formatOutput = true;
     $mdoc->validateOnParse = false;
     $mdoc->strictErrorChecking = false;
     if ($mdoc->load($manifestdir . '/moodle.xml', LIBXML_NONET)) {
         $mdoc->save($this->get_workdir_path() . '/moodle.xml', LIBXML_NOEMPTYTAG);
     } else {
         $xml_error->collect();
         $this->log('validation error(s): ' . PHP_EOL . error_messages::instance(), backup::LOG_DEBUG, null, 2);
         throw new imscc1_convert_exception(error_messages::instance()->to_string(true));
     }
     //Move the files to the workdir
     rename($manifestdir . '/course_files', $this->get_workdir_path() . '/course_files');
 }
Ejemplo n.º 8
0
 protected function get_all_files()
 {
     global $CFG;
     $all_files = array();
     $xpath = cc2moodle::newx_path(cc112moodle::$manifest, cc112moodle::$namespaces);
     foreach (cc112moodle::$restypes as $type) {
         $files = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@type="' . $type . '"]/imscc:file/@href');
         if (empty($files) || $files->length == 0) {
             continue;
         }
         foreach ($files as $file) {
             //omit html files
             //this is a bit too simplistic
             $ext = strtolower(pathinfo($file->nodeValue, PATHINFO_EXTENSION));
             if (in_array($ext, array('html', 'htm', 'xhtml'))) {
                 continue;
             }
             $all_files[] = $file;
         }
         unset($files);
     }
     //are there any labels?
     $xquery = "//imscc:item/imscc:item/imscc:item[imscc:title][not(@identifierref)]";
     $labels = $xpath->query($xquery);
     if (!empty($labels) && $labels->length > 0) {
         $tname = 'course_files';
         $dpath = cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $tname;
         $fpath = $dpath . DIRECTORY_SEPARATOR . 'folder.gif';
         $rfpath = $tname . '/folder.gif';
         if (!file_exists($dpath)) {
             mkdir($dpath);
         }
         //copy the folder.gif file
         $folder_gif = "{$CFG->dirroot}/pix/f/folder.gif";
         copy($folder_gif, $fpath);
         $all_files[] = $rfpath;
     }
     $all_files = empty($all_files) ? '' : $all_files;
     return $all_files;
 }
Ejemplo n.º 9
0
 public function get_topic_data($instance)
 {
     $topic_data = '';
     $topic_file = $this->get_external_xml($instance['resource_indentifier']);
     if (!empty($topic_file)) {
         $topic_file_path = cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $topic_file;
         $topic_file_dir = dirname($topic_file_path);
         $topic = $this->load_xml_resource($topic_file_path);
         if (!empty($topic)) {
             $xpath = cc2moodle::newx_path($topic, cc112moodle::$forumns);
             $topic_title = $xpath->query('/dt:topic/dt:title');
             if ($topic_title->length > 0 && !empty($topic_title->item(0)->nodeValue)) {
                 $topic_title = $topic_title->item(0)->nodeValue;
             } else {
                 $topic_title = 'Untitled Topic';
             }
             $topic_text = $xpath->query('/dt:topic/dt:text');
             $topic_text = !empty($topic_text->item(0)->nodeValue) ? $this->update_sources($topic_text->item(0)->nodeValue, dirname($topic_file)) : '';
             $topic_text = !empty($topic_text) ? str_replace("%24", "\$", $this->include_titles($topic_text)) : '';
             if (!empty($topic_title)) {
                 $topic_data['title'] = $topic_title;
                 $topic_data['description'] = $topic_text;
             }
         }
         $topic_attachments = $xpath->query('/dt:topic/dt:attachments/dt:attachment/@href');
         if ($topic_attachments->length > 0) {
             $attachment_html = '';
             foreach ($topic_attachments as $file) {
                 $attachment_html .= $this->generate_attachment_html($this->full_path($file->nodeValue, '/'));
             }
             $topic_data['description'] = !empty($attachment_html) ? $topic_text . '<p>Attachments:</p>' . $attachment_html : $topic_text;
         }
     }
     return $topic_data;
 }
Ejemplo n.º 10
0
 private function get_all_files()
 {
     $all_files = array();
     $xpath = cc2moodle::newx_path(cc2moodle::$manifest, cc2moodle::$namespaces);
     $types = array('associatedcontent/imscc_xmlv1p0/learning-application-resource', 'webcontent');
     foreach ($types as $type) {
         $files = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@type="' . $type . '"]/imscc:file/@href');
         if (!empty($files)) {
             foreach ($files as $file) {
                 $all_files[] = $file;
             }
         }
         unset($files);
     }
     $all_files = empty($all_files) ? '' : $all_files;
     return $all_files;
 }
Ejemplo n.º 11
0
 protected function create_code_info_details_mod()
 {
     $result = parent::create_code_info_details_mod();
     $count_blti = $this->count_instances(MOODLE_TYPE_LTI);
     $sheet_info_details_mod_instances_instance = static::loadsheet(SHEET_INFO_DETAILS_MOD_INSTANCE);
     $blti_mod = '';
     if ($count_blti > 0) {
         $blti_instance = $this->create_mod_info_details_mod_instances_instance($sheet_info_details_mod_instances_instance, $count_blti, static::$instances['instances'][MOODLE_TYPE_LTI]);
         $blti_mod = $blti_instance ? $this->create_mod_info_details_mod(MOODLE_TYPE_LTI, $blti_instance) : '';
     }
     return $result . $blti_mod;
 }
Ejemplo n.º 12
0
 private function get_question_type($identifier, $assessment)
 {
     $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
     $metadata = $xpath->query('//xmlns:item[@ident="' . $identifier . '"]/xmlns:itemmetadata/xmlns:qtimetadata/xmlns:qtimetadatafield');
     foreach ($metadata as $field) {
         $field_label = $xpath->query('xmlns:fieldlabel', $field);
         $field_label = !empty($field_label->item(0)->nodeValue) ? $field_label->item(0)->nodeValue : '';
         if ($field_label == 'cc_profile') {
             $field_entry = $xpath->query('xmlns:fieldentry', $field);
             $type = !empty($field_entry->item(0)->nodeValue) ? $field_entry->item(0)->nodeValue : '';
         }
     }
     $return_type = array();
     $return_type['moodle'] = '';
     $return_type['cc'] = $type;
     if ($type == CC_QUIZ_MULTIPLE_CHOICE) {
         $return_type['moodle'] = MOODLE_QUIZ_MULTIPLE_CHOICE;
     }
     if ($type == CC_QUIZ_MULTIPLE_RESPONSE) {
         $return_type['moodle'] = MOODLE_QUIZ_MULTIPLE_CHOICE;
     }
     if ($type == CC_QUIZ_TRUE_FALSE) {
         $return_type['moodle'] = MOODLE_QUIZ_TRUE_FALSE;
     }
     if ($type == CC_QUIZ_ESSAY) {
         $return_type['moodle'] = MOODLE_QUIZ_ESSAY;
     }
     if ($type == CC_QUIZ_FIB) {
         $return_type['moodle'] = MOODLE_QUIZ_SHORTANSWER;
     }
     if ($type == CC_QUIZ_PATTERN_MACHT) {
         $return_type['moodle'] = MOODLE_QUIZ_SHORTANSWER;
     }
     return $return_type;
 }
Ejemplo n.º 13
0
 private function create_node_course_modules_mod_resource($sheet_mod_resource, $instance)
 {
     global $CFG;
     require_once $CFG->libdir . '/validateurlsyntax.php';
     $link = '';
     $mod_alltext = '';
     $mod_summary = '';
     $xpath = cc2moodle::newx_path(cc2moodle::$manifest, cc2moodle::$namespaces);
     if ($instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBCONTENT || $instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_ASSOCIATED_CONTENT) {
         $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/@href');
         $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : '';
         if (empty($resource)) {
             unset($resource);
             $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href');
             $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : '';
         }
         if (!empty($resource)) {
             $link = $resource;
         }
     }
     if ($instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBLINK) {
         $external_resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href')->item(0)->nodeValue;
         if ($external_resource) {
             $resource = $this->load_xml_resource(cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $external_resource);
             if (!empty($resource)) {
                 $xpath = cc2moodle::newx_path($resource, cc2moodle::getresourcens());
                 $resource = $xpath->query('//url/@href');
                 if ($resource->length > 0) {
                     $rawlink = $resource->item(0)->nodeValue;
                     if (!validateUrlSyntax($rawlink, 's+')) {
                         $changed = rawurldecode($rawlink);
                         if (validateUrlSyntax($changed, 's+')) {
                             $link = $changed;
                         } else {
                             $link = 'http://invalidurldetected/';
                         }
                     } else {
                         $link = $rawlink;
                     }
                 }
             }
         }
     }
     $find_tags = array('[#mod_instance#]', '[#mod_name#]', '[#mod_type#]', '[#mod_reference#]', '[#mod_summary#]', '[#mod_alltext#]', '[#mod_options#]', '[#date_now#]');
     $mod_type = 'file';
     $mod_options = 'objectframe';
     $mod_reference = $link;
     //detected if we are dealing with html file
     if (!empty($link) && $instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBCONTENT) {
         $ext = strtolower(pathinfo($link, PATHINFO_EXTENSION));
         if (in_array($ext, array('html', 'htm', 'xhtml'))) {
             $mod_type = 'html';
             //extract the content of the file
             $rootpath = realpath(cc112moodle::$path_to_manifest_folder);
             $htmlpath = realpath($rootpath . DIRECTORY_SEPARATOR . $link);
             $dirpath = dirname($htmlpath);
             if (file_exists($htmlpath)) {
                 $fcontent = file_get_contents($htmlpath);
                 $mod_alltext = clean_param($this->prepare_content($fcontent), PARAM_CLEANHTML);
                 $mod_reference = '';
                 $mod_options = '';
                 //TODO: try to handle embedded resources
                 /**
                  * images, linked static resources, applets, videos
                  */
                 $doc = new DOMDocument();
                 $cdir = getcwd();
                 chdir($dirpath);
                 try {
                     if (!empty($mod_alltext) && $doc->loadHTML($mod_alltext)) {
                         $xpath = new DOMXPath($doc);
                         $attributes = array('href', 'src', 'background', 'archive', 'code');
                         $qtemplate = "//*[@##][not(contains(@##,'://'))]/@##";
                         $query = '';
                         foreach ($attributes as $attrname) {
                             if (!empty($query)) {
                                 $query .= " | ";
                             }
                             $query .= str_replace('##', $attrname, $qtemplate);
                         }
                         $list = $xpath->query($query);
                         $searches = array();
                         $replaces = array();
                         foreach ($list as $resrc) {
                             $rpath = $resrc->nodeValue;
                             $rtp = realpath($rpath);
                             if ($rtp !== false && is_file($rtp)) {
                                 //file is there - we are in business
                                 $strip = str_replace("\\", "/", str_ireplace($rootpath, '', $rtp));
                                 $encoded_file = '$@FILEPHP@$' . str_replace('/', '$@SLASH@$', $strip);
                                 $searches[] = $resrc->nodeValue;
                                 $replaces[] = $encoded_file;
                             }
                         }
                         $mod_alltext = str_replace($searches, $replaces, $mod_alltext);
                     }
                 } catch (Exception $e) {
                     //silence the complaints
                 }
                 chdir($cdir);
                 $mod_alltext = self::safexml($mod_alltext);
             }
         }
     }
     $replace_values = array($instance['instance'], self::safexml($instance['title']), $mod_type, $mod_reference, '', $mod_alltext, $mod_options, time());
     return str_replace($find_tags, $replace_values, $sheet_mod_resource);
 }
 private function get_defaultgrade($assessment, $question_identifier)
 {
     $result = 1;
     $xpath = cc2moodle::newx_path($assessment, cc2moodle::getquizns());
     $query = '//xmlns:item[@ident="' . $question_identifier . '"]';
     $query .= '//xmlns:qtimetadatafield[xmlns:fieldlabel="cc_weighting"]/xmlns:fieldentry';
     $defgrade = $xpath->query($query);
     if (!empty($defgrade) && $defgrade->length > 0) {
         $resp = (int) $defgrade->item(0)->nodeValue;
         if ($resp >= 0 && $resp <= 99) {
             $result = $resp;
         }
     }
     return $result;
 }