|
Moodle
2.2.1
http://www.collinsharper.com
|
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/>. 00024 defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.'); 00025 00026 class cc_resource extends entities { 00027 00028 public function generate_node () { 00029 00030 cc2moodle::log_action('Creating Resource mods'); 00031 00032 $response = ''; 00033 $sheet_mod_resource = cc2moodle::loadsheet(SHEET_COURSE_SECTIONS_SECTION_MODS_MOD_RESOURCE); 00034 00035 if (!empty(cc2moodle::$instances['instances'][MOODLE_TYPE_RESOURCE])) { 00036 foreach (cc2moodle::$instances['instances'][MOODLE_TYPE_RESOURCE] as $instance) { 00037 $response .= $this->create_node_course_modules_mod_resource($sheet_mod_resource, $instance); 00038 } 00039 } 00040 00041 return $response; 00042 00043 } 00044 00045 private function create_node_course_modules_mod_resource ($sheet_mod_resource, $instance) { 00046 global $CFG; 00047 00048 require_once($CFG->libdir.'/validateurlsyntax.php'); 00049 00050 $link = ''; 00051 $mod_alltext = ''; 00052 $mod_summary = ''; 00053 $xpath = cc2moodle::newx_path(cc2moodle::$manifest, cc2moodle::$namespaces); 00054 00055 if ($instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBCONTENT || $instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_ASSOCIATED_CONTENT) { 00056 $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/@href'); 00057 $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : ''; 00058 00059 if (empty($resource)) { 00060 00061 unset($resource); 00062 00063 $resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href'); 00064 $resource = !empty($resource->item(0)->nodeValue) ? $resource->item(0)->nodeValue : ''; 00065 00066 } 00067 00068 if (!empty($resource)) { 00069 $link = $resource; 00070 } 00071 } 00072 00073 if ($instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBLINK) { 00074 00075 $external_resource = $xpath->query('/imscc:manifest/imscc:resources/imscc:resource[@identifier="' . $instance['resource_indentifier'] . '"]/imscc:file/@href')->item(0)->nodeValue; 00076 00077 if ($external_resource) { 00078 00079 $resource = $this->load_xml_resource(cc2moodle::$path_to_manifest_folder . DIRECTORY_SEPARATOR . $external_resource); 00080 00081 if (!empty($resource)) { 00082 $xpath = cc2moodle::newx_path($resource, cc2moodle::getresourcens()); 00083 $resource = $xpath->query('//url/@href'); 00084 if ($resource->length > 0) { 00085 $rawlink = $resource->item(0)->nodeValue; 00086 if (!validateUrlSyntax($rawlink, 's+')) { 00087 $changed = rawurldecode($rawlink); 00088 if (validateUrlSyntax($changed, 's+')) { 00089 $link = $changed; 00090 } else { 00091 $link = 'http://invalidurldetected/'; 00092 } 00093 } else { 00094 $link = $rawlink; 00095 } 00096 } 00097 } 00098 } 00099 } 00100 00101 $find_tags = array('[#mod_instance#]', 00102 '[#mod_name#]', 00103 '[#mod_type#]', 00104 '[#mod_reference#]', 00105 '[#mod_summary#]', 00106 '[#mod_alltext#]', 00107 '[#mod_options#]', 00108 '[#date_now#]'); 00109 00110 $mod_type = 'file'; 00111 $mod_options = 'objectframe'; 00112 $mod_reference = $link; 00113 //detected if we are dealing with html file 00114 if (!empty($link) && ($instance['common_cartriedge_type'] == cc2moodle::CC_TYPE_WEBCONTENT)) { 00115 $ext = strtolower(pathinfo($link, PATHINFO_EXTENSION)); 00116 if (in_array($ext, array('html', 'htm', 'xhtml'))) { 00117 $mod_type = 'html'; 00118 //extract the content of the file 00119 $rootpath = realpath(cc112moodle::$path_to_manifest_folder); 00120 $htmlpath = realpath($rootpath . DIRECTORY_SEPARATOR . $link); 00121 $dirpath = dirname($htmlpath); 00122 if (file_exists($htmlpath)) { 00123 $fcontent = file_get_contents($htmlpath); 00124 $mod_alltext = clean_param($this->prepare_content($fcontent), PARAM_CLEANHTML); 00125 $mod_reference = ''; 00126 $mod_options = ''; 00127 //TODO: try to handle embedded resources 00131 $doc = new DOMDocument(); 00132 $cdir = getcwd(); 00133 chdir($dirpath); 00134 try { 00135 if (!empty($mod_alltext) && $doc->loadHTML($mod_alltext)) { 00136 $xpath = new DOMXPath($doc); 00137 $attributes = array('href', 'src', 'background', 'archive', 'code'); 00138 $qtemplate = "//*[@##][not(contains(@##,'://'))]/@##"; 00139 $query = ''; 00140 foreach ($attributes as $attrname) { 00141 if (!empty($query)) { 00142 $query .= " | "; 00143 } 00144 $query .= str_replace('##', $attrname, $qtemplate); 00145 } 00146 $list = $xpath->query($query); 00147 $searches = array(); 00148 $replaces = array(); 00149 foreach ($list as $resrc) { 00150 $rpath = $resrc->nodeValue; 00151 $rtp = realpath($rpath); 00152 if (($rtp !== false) && is_file($rtp)) { 00153 //file is there - we are in business 00154 $strip = str_replace("\\", "/", str_ireplace($rootpath, '', $rtp)); 00155 $encoded_file = '$@FILEPHP@$'.str_replace('/', '$@SLASH@$', $strip); 00156 $searches[] = $resrc->nodeValue; 00157 $replaces[] = $encoded_file; 00158 } 00159 } 00160 $mod_alltext = str_replace($searches, $replaces, $mod_alltext); 00161 } 00162 } catch (Exception $e) { 00163 //silence the complaints 00164 } 00165 chdir($cdir); 00166 $mod_alltext = self::safexml($mod_alltext); 00167 } 00168 } 00169 } 00170 00171 $replace_values = array($instance['instance'], 00172 self::safexml($instance['title']), 00173 $mod_type, 00174 $mod_reference, 00175 '', 00176 $mod_alltext, 00177 $mod_options, 00178 time()); 00179 00180 00181 return str_replace($find_tags, $replace_values, $sheet_mod_resource); 00182 } 00183 }