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