|
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/>. 00026 require_once 'cc_utils.php'; 00027 require_once 'xmlbase.php'; 00028 require_once 'cc_resources.php'; 00029 require_once 'cc_version_base.php'; 00030 require_once 'gral_lib/pathutils.php'; 00031 00032 00037 class cc_manifest extends XMLGenericDocument implements cc_i_manifest { 00038 private $ccversion = null; 00039 private $ccobj = null; 00040 private $rootmanifest = null; 00041 private $activemanifest = null; 00042 private $parentmanifest = null; 00043 private $parentparentmanifest = null; 00044 private $ares = array(); 00045 private $mainidentifier = null; 00046 00047 public function __construct($ccver = cc_version::v1,$activemanifest=null, 00048 $parentmanifest=null,$parentparentmanifest=null) { 00049 00050 if (is_int($ccver)){ 00051 $this->ccversion=$ccver; 00052 $classname = "cc_version{$ccver}"; 00053 $this->ccobj = new $classname; 00054 parent::__construct('UTF-8',true); 00055 } else 00056 if (is_object($ccver) && (get_class($ccver)=='cc_manifest')){ 00057 $this->doc = $ccver->doc; 00058 $this->rootmanifest = $ccver->rootmanifest; 00059 $this->activemanifest = $activemanifest; 00060 $this->parentmanifest = $parentmanifest; 00061 $this->parentparentmanifest = $parentparentmanifest; 00062 $this->ccversion = $ccver->ccversion; 00063 $this->ccobj = $ccver; 00064 $this->register_namespaces_for_xpath(); 00065 } 00066 } 00067 00068 public function __destruct() { 00069 parent::__destruct(); 00070 } 00071 00072 00077 public function register_namespaces_for_xpath(){ 00078 $scnam = $this->activemanifest->get_cc_namespaces(); 00079 foreach ($scnam as $key => $value){ 00080 $this->registerNS($key,$value); 00081 } 00082 } 00083 00088 private function fill_manifest() { 00089 00090 } 00091 00097 public function add_metadata_manifest (cc_i_metadata_manifest $met){ 00098 $metanode = $this->node("//imscc:manifest[@identifier='". 00099 $this->activemanifest->manifestID(). 00100 "']/imscc:metadata"); 00101 $nmeta = $this->activemanifest->create_metadata_node($met,$this->doc,$metanode); 00102 $metanode->appendChild($nmeta); 00103 } 00104 00105 00112 public function add_metadata_resource (cc_i_metadata_resource $met,$identifier){ 00113 $metanode = $this->node("//imscc:resource". 00114 "[@identifier='". 00115 $identifier. 00116 "']"); 00117 $metanode2 = $this->node("//imscc:resource". 00118 "[@identifier='". 00119 $identifier. 00120 "']/imscc:file"); 00121 $dnode = $this->doc->createElementNS($this->ccnamespaces['imscc'], "metadata"); 00122 00123 $metanode->insertBefore($dnode,$metanode2); 00124 00125 $this->activemanifest->create_metadata_resource_node($met,$this->doc,$dnode); 00126 } 00127 00128 00136 public function add_metadata_file (cc_i_metadata_file $met,$identifier,$filename){ 00137 00138 if (empty($met) || empty($identifier) || empty($filename)){ 00139 throw new Exception('Try to add a metadata file with nulls values given!'); 00140 } 00141 00142 $metanode = $this->node("//imscc:resource". 00143 "[@identifier='". 00144 $identifier. 00145 "']/imscc:file". 00146 "[@href='". 00147 $filename. 00148 "']"); 00149 00150 $dnode = $this->doc->createElementNS($this->ccnamespaces['imscc'], "metadata"); 00151 00152 $metanode->appendChild($dnode); 00153 00154 $this->activemanifest->create_metadata_file_node($met,$this->doc,$dnode); 00155 } 00156 00157 00158 public function on_create (){ 00159 $this->activemanifest = cc_builder_creator::factory($this->ccversion); 00160 $this->rootmanifest = $this->activemanifest; 00161 $result = $this->activemanifest->create_manifest($this->doc); 00162 $this->register_namespaces_for_xpath(); 00163 return $result; 00164 00165 } 00166 00167 00168 public function get_relative_base_path() {return $this->activemanifest->base();} 00169 public function parent_manifest () {return new cc_manifest($this,$this->parentmanifest,$this->parentparentmanifest);} 00170 public function root_manifest () {return new cc_manifest($this,$this->rootmanifest);} 00171 public function manifestID () {return $this->activemanifest->manifestID();} 00172 public function get_manifest_namespaces() {return $this->rootmanifest->get_cc_namespaces(); } 00173 00174 00175 00181 public function add_new_organization(cc_i_organization &$org) { 00182 $norg = $this->activemanifest->create_organization_node($org,$this->doc); 00183 $orgnode = $this->node("//imscc:manifest[@identifier='". 00184 $this->activemanifest->manifestID(). 00185 "']/imscc:organizations"); 00186 $orgnode->appendChild($norg); 00187 } 00188 00189 00190 00191 public function get_resources($searchspecific='') { 00192 $reslist = $this->get_resource_list($searchspecific); 00193 $resourcelist = array(); 00194 foreach ($reslist as $resourceitem) { 00195 $resourcelist[]=new cc_resource($this, $resourceitem); 00196 } 00197 return $resourcelist; 00198 } 00199 00200 00201 00202 public function get_cc_namespace_path($nsname) { 00203 if (is_string($nsname) && (!empty($nsname))){ 00204 $scnam = $this->activemanifest->get_cc_namespaces(); 00205 return $scnam[$nsname]; 00206 } 00207 return null; 00208 } 00209 00210 00211 public function get_resource_list($searchspecific=''){ 00212 return $this->nodeList("//imscc:manifest[@identifier='". 00213 $this->activemanifest->manifestID(). 00214 "']/imscc:resources/imscc:resource".$searchspecific); 00215 } 00216 00217 00218 public function on_load (){ 00219 $this->register_namespaces_for_xpath(); 00220 $this->fill_manifest(); 00221 return true; 00222 } 00223 00224 public function on_save (){ 00225 return true; 00226 } 00227 00228 00237 public function add_resource (cc_i_resource $res, $identifier=null, $type='webcontent'){ 00238 00239 if (!$this->ccobj->valid($type)){ 00240 throw new Exception("Type invalid..."); 00241 } 00242 00243 if (is_null($res)){ 00244 throw new Exception('Invalid Resource or dont give it'); 00245 } 00246 $rst = null; 00247 00248 if (is_string($res)){ 00249 $rst = new cc_resource($this->filePath(), $res); 00250 if (is_string($identifier)){ 00251 $rst->identifier = $identifier; 00252 } 00253 } else { 00254 $rst = $res; 00255 } 00256 00257 //TODO: This has to be reviewed since it does not handle properly mutiple file 00258 // dependencies 00259 if (is_object($identifier)) { 00260 $this->activemanifest->create_resource_node($rst,$this->doc,$identifier); 00261 } else { 00262 $nresnode = null; 00263 00264 $rst->type = $type; 00265 if (!cc_helpers::is_html($rst->filename)) { 00266 $rst->href = null; 00267 } 00268 $this->activemanifest->create_resource_node($rst,$this->doc,$nresnode); 00269 00270 00271 for ($i = 1 ; $i < count ($rst->files); $i++){ 00272 $ident = $this->get_identifier_by_filename($rst->files[$i]); 00273 if(empty($ident)){ 00274 $newres = new cc_resource($rst->manifestroot,$rst->files[$i],false); 00275 if (!empty($newres)) { 00276 if (!cc_helpers::is_html($rst->files[$i])) { 00277 $newres->href = null; 00278 } 00279 $newres->type = 'webcontent'; 00280 $this->activemanifest->create_resource_node($newres,$this->doc,$nresnode); 00281 } 00282 } 00283 00284 } 00285 foreach ($this->activemanifest->resources as $k => $v){ 00286 ($k); 00287 $depen = $this->check_if_exist_in_other($v->files[0]); 00288 if (!empty($depen)){ 00289 $this->replace_file_x_dependency($depen,$v->files[0]); 00290 // coloca aca como type = webcontent porque son archivos dependientes 00291 // quizas aqui habria q ver de que type es el que vino y segun eso, ponerlo 00292 // en associatedcontent o en webcontent 00293 $v->type = 'webcontent'; 00294 } 00295 } 00296 } 00297 00298 $tmparray = array($rst->identifier,$rst->files[0]); 00299 return $tmparray; 00300 } 00301 00302 00303 00304 private function check_if_exist_in_other($name){ 00305 $status = array(); 00306 foreach ($this->activemanifest->resources as $key => $value){ 00307 ($key); 00308 for ($i=1; $i< count($value->files); $i++){ 00309 if ($name == $value->files[$i]){ 00310 array_push($status,$value->identifier); 00311 } 00312 } 00313 } 00314 return $status; 00315 } 00316 00317 00318 private function replace_file_x_dependency($depen,$name){ 00319 foreach ($depen as $key => $value){ 00320 ($key); 00321 $ident = $this->get_identifier_by_filename($name); 00322 $this->activemanifest->resources[$value]->files = 00323 $this->array_remove_by_value($this->activemanifest->resources[$value]->files,$name); 00324 if (!in_array($ident,$this->activemanifest->resources[$value]->dependency)){ 00325 array_push($this->activemanifest->resources[$value]->dependency,$ident); 00326 } 00327 00328 } 00329 return true; 00330 } 00331 00332 00333 private function get_identifier_by_filename($name){ 00334 $result = null; 00335 foreach ($this->activemanifest->resources as $key => $value) { 00336 if ($name == $value->files[0]){ 00337 $result = $key; 00338 break; 00339 } 00340 } 00341 return $result; 00342 } 00343 00344 00345 00346 private function array_remove_by_value($arr,$value) { 00347 return array_values(array_diff($arr,array($value))); 00348 00349 } 00350 00351 private function array_remove_by_key($arr,$key) { 00352 return array_values(array_diff_key($arr,array($key))); 00353 00354 } 00355 00356 00362 public function put_nodes (){ 00363 00364 $resnodestr = "//imscc:manifest[@identifier='".$this->activemanifest->manifestID(). 00365 "']/imscc:resources"; 00366 $resnode = $this->node($resnodestr); 00367 00368 foreach ($this->activemanifest->resources as $key => $node) { 00369 ($key); 00370 $resnode->appendChild($this->activemanifest->create_resource_node($node,$this->doc,null)); 00371 } 00372 return $resnode; 00373 00374 } 00375 } 00376 00377