|
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/>. 00016 00024 require_once 'cc_utils.php'; 00025 require_once 'cc_version_base.php'; 00026 require_once 'cc_organization.php'; 00027 00028 00033 class cc_version1 extends cc_version_base { 00034 const webcontent = 'webcontent'; 00035 const questionbank = 'imsqti_xmlv1p2/imscc_xmlv1p0/question-bank'; 00036 const assessment = 'imsqti_xmlv1p2/imscc_xmlv1p0/assessment'; 00037 const associatedcontent = 'associatedcontent/imscc_xmlv1p0/learning-application-resource'; 00038 const discussiontopic = 'imsdt_xmlv1p0'; 00039 const weblink = 'imswl_xmlv1p0'; 00040 00041 public static $checker = array(self::webcontent, 00042 self::assessment, 00043 self::associatedcontent, 00044 self::discussiontopic, 00045 self::questionbank, 00046 self::weblink); 00047 00054 public function valid($type) { 00055 return in_array($type, self::$checker); 00056 } 00057 00058 public function __construct() { 00059 $this->ccnamespaces = array('imscc' => 'http://www.imsglobal.org/xsd/imscc/imscp_v1p1', 00060 'lomimscc' => 'http://ltsc.ieee.org/xsd/imscc/LOM', 00061 'lom' => 'http://ltsc.ieee.org/xsd/LOM', 00062 'voc' => 'http://ltsc.ieee.org/xsd/LOM/vocab', 00063 'xsi' => 'http://www.w3.org/2001/XMLSchema-instance' 00064 ); 00065 00066 $this->ccnsnames = array('imscc' => 'http://www.imsglobal.org/profile/cc/ccv1p0/derived_schema/imscp_v1p2_localised.xsd' , 00067 'lom' => 'http://www.imsglobal.org/profile/cc/ccv1p0/derived_schema/domainProfile_2/lomLoose_localised.xsd' , 00068 'lomimscc' => 'http://www.imsglobal.org/profile/cc/ccv1p0/derived_schema/domainProfile_1/lomLoose_localised.xsd', 00069 'voc' => 'http://www.imsglobal.org/profile/cc/ccv1p0/derived_schema/domainProfile_2/vocab/loose.xsd' 00070 ); 00071 00072 $this->ccversion = '1.0.0'; 00073 $this->camversion = '1.0.0'; 00074 $this->_generator = 'Moodle 2 Common Cartridge generator'; 00075 } 00076 00077 protected function on_create(DOMDocument &$doc, $rootmanifestnode=null,$nmanifestID=null) { 00078 $doc->formatOutput = true; 00079 $doc->preserveWhiteSpace = true; 00080 00081 00082 $this->manifestID = is_null($nmanifestID) ? cc_helpers::uuidgen('M_') : $nmanifestID; 00083 $mUUID = $doc->createAttribute('identifier'); 00084 $mUUID->nodeValue = $this->manifestID; 00085 00086 00087 if (is_null($rootmanifestnode)) { 00088 if (!empty($this->_generator)) { 00089 $comment = $doc->createComment($this->_generator); 00090 $doc->appendChild($comment); 00091 } 00092 00093 $rootel = $doc->createElementNS($this->ccnamespaces['imscc'],'manifest'); 00094 $rootel->appendChild($mUUID); 00095 $doc->appendChild($rootel); 00096 00097 //add all namespaces 00098 foreach ($this->ccnamespaces as $key => $value) { 00099 if ($key != 'lom' ){ 00100 $dummy_attr = $key.":dummy"; 00101 $doc->createAttributeNS($value,$dummy_attr); 00102 } 00103 } 00104 00105 // add location of schemas 00106 $schemaLocation=''; 00107 foreach ($this->ccnsnames as $key => $value) { 00108 $vt = empty($schemaLocation) ? '' : ' '; 00109 $schemaLocation .= $vt.$this->ccnamespaces[$key].' '.$value; 00110 } 00111 $aSchemaLoc = $doc->createAttributeNS($this->ccnamespaces['xsi'],'xsi:schemaLocation'); 00112 $aSchemaLoc->nodeValue=$schemaLocation; 00113 $rootel->appendChild($aSchemaLoc); 00114 00115 } else { 00116 $rootel = $doc->createElementNS($this->ccnamespaces['imscc'],'imscc:manifest'); 00117 $rootel->appendChild($mUUID); 00118 } 00119 00120 $metadata = $doc->createElementNS($this->ccnamespaces['imscc'],'metadata'); 00121 $schema = $doc->createElementNS($this->ccnamespaces['imscc'],'schema','IMS Common Cartridge'); 00122 $schemaversion = $doc->createElementNS($this->ccnamespaces['imscc'],'schemaversion',$this->ccversion); 00123 00124 $metadata->appendChild($schema); 00125 $metadata->appendChild($schemaversion); 00126 $rootel->appendChild($metadata); 00127 00128 if (!is_null($rootmanifestnode)) { 00129 $rootmanifestnode->appendChild($rootel); 00130 } 00131 00132 $organizations = $doc->createElementNS($this->ccnamespaces['imscc'],'organizations'); 00133 $rootel->appendChild($organizations); 00134 $resources = $doc->createElementNS($this->ccnamespaces['imscc'],'resources'); 00135 $rootel->appendChild($resources); 00136 00137 return true; 00138 } 00139 00140 00141 protected function update_attribute(DOMDocument &$doc, $attrname, $attrvalue, DOMElement &$node) { 00142 $busenew = (is_object($node) && $node->hasAttribute($attrname)); 00143 $nResult = null; 00144 if (!$busenew && is_null($attrvalue)) { 00145 $node->removeAttribute($attrname); 00146 } else { 00147 $nResult = $busenew ? $node->getAttributeNode($attrname) : $doc->createAttribute($attrname); 00148 $nResult->nodeValue = $attrvalue; 00149 if (!$busenew) { 00150 $node->appendChild($nResult); 00151 } 00152 } 00153 return $nResult; 00154 } 00155 00156 00157 protected function update_attribute_ns(DOMDocument &$doc, $attrname, $attrnamespace,$attrvalue, DOMElement &$node) { 00158 $busenew = (is_object($node) && $node->hasAttributeNS($attrnamespace, $attrname)); 00159 $nResult = null; 00160 if (!$busenew && is_null($attrvalue)) { 00161 $node->removeAttributeNS($attrnamespace, $attrname); 00162 } else { 00163 $nResult = $busenew ? $node->getAttributeNodeNS($attrnamespace, $attrname) : $doc->createAttributeNS($attrnamespace, $attrname); 00164 $nResult->nodeValue = $attrvalue; 00165 if (!$busenew) { 00166 $node->appendChild($nResult); 00167 } 00168 } 00169 return $nResult; 00170 } 00171 00172 00173 protected function get_child_node(DOMDocument &$doc, $itemname, DOMElement &$node) { 00174 $nlist = $node->getElementsByTagName($itemname); 00175 $item = is_object($nlist) && ($nlist->length > 0) ? $nlist->item(0) : null; 00176 return $item; 00177 } 00178 00179 00180 protected function update_child_item(DOMDocument &$doc, $itemname, $itemvalue, DOMElement &$node, $attrtostore=null) { 00181 $tnode = $this->get_child_node($doc,'title',$node); 00182 $usenew = is_null($tnode); 00183 $tnode = $usenew ? $doc->createElementNS($this->ccnamespaces['imscc'], $itemname) : $tnode; 00184 if (!is_null($attrtostore)) { 00185 foreach ($attrtostore as $key => $value) { 00186 $this->update_attribute($doc,$key,$value,$tnode); 00187 } 00188 } 00189 $tnode->nodeValue = $itemvalue; 00190 if ($usenew) { 00191 $node->appendChild($tnode); 00192 } 00193 } 00194 00195 00196 protected function update_items($items, DOMDocument &$doc, DOMElement &$xmlnode) { 00197 foreach ($items as $key => $item) { 00198 $itemnode = $doc->createElementNS($this->ccnamespaces['imscc'], 'item'); 00199 $this->update_attribute($doc, 'identifier' , $key , $itemnode); 00200 $this->update_attribute($doc, 'identifierref', $item->identifierref, $itemnode); 00201 $this->update_attribute($doc, 'parameters' , $item->parameters , $itemnode); 00202 if (!empty($item->title)) { 00203 $titlenode = $doc->createElementNS($this->ccnamespaces['imscc'], 00204 'title', 00205 $item->title); 00206 $itemnode->appendChild($titlenode); 00207 } 00208 if ($item->has_child_items()) { 00209 $this->update_items($item->childitems, $doc, $itemnode); 00210 } 00211 $xmlnode->appendChild($itemnode); 00212 } 00213 } 00214 00215 00224 protected function create_resource(cc_i_resource &$res, DOMDocument &$doc, $xmlnode=null) { 00225 $usenew = is_object($xmlnode); 00226 $dnode = $usenew ? $xmlnode : $doc->createElementNS($this->ccnamespaces['imscc'], "resource"); 00227 $this->update_attribute($doc,'identifier',$res->identifier,$dnode); 00228 $this->update_attribute($doc,'type' ,$res->type ,$dnode); 00229 !is_null($res->href) ? $this->update_attribute($doc,'href',$res->href,$dnode): null; 00230 $this->update_attribute($doc,'base' ,$res->base ,$dnode); 00231 00232 foreach ($res->files as $file) { 00233 $nd = $doc->createElementNS($this->ccnamespaces['imscc'],'file'); 00234 $ndatt = $doc->createAttribute('href'); 00235 $ndatt->nodeValue = $file; 00236 $nd->appendChild($ndatt); 00237 $dnode->appendChild($nd); 00238 } 00239 $this->resources[$res->identifier] = $res; 00240 00241 foreach ($res->dependency as $dependency){ 00242 $nd = $doc->createElementNS($this->ccnamespaces['imscc'],'dependency'); 00243 $ndatt = $doc->createAttribute('identifierref'); 00244 $ndatt->nodeValue = $dependency; 00245 $nd->appendChild($ndatt); 00246 $dnode->appendChild($nd); 00247 } 00248 00249 return $dnode; 00250 } 00251 00252 00253 00261 protected function create_item_folder (cc_i_organization &$org, DOMDocument &$doc, DOMElement &$xmlnode=null){ 00262 00263 $itemfoldernode = $doc->createElementNS($this->ccnamespaces['imscc'],'item'); 00264 $this->update_attribute($doc,'identifier', "root", $itemfoldernode); 00265 00266 if ($org->has_items()) { 00267 $this->update_items($org->itemlist, $doc, $itemfoldernode); 00268 } 00269 if (is_null($this->organizations)) { 00270 $this->organizations = array(); 00271 } 00272 $this->organizations[$org->identifier] = $org; 00273 00274 $xmlnode->appendChild($itemfoldernode); 00275 } 00276 00277 00278 00279 00288 protected function create_organization (cc_i_organization &$org, DOMDocument &$doc, $xmlnode=null){ 00289 00290 $usenew = is_object($xmlnode); 00291 $dnode = $usenew ? $xmlnode : $doc->createElementNS($this->ccnamespaces['imscc'], "organization"); 00292 $this->update_attribute($doc,'identifier' ,$org->identifier,$dnode); 00293 $this->update_attribute($doc,'structure' ,$org->structure ,$dnode); 00294 00295 $this->create_item_folder($org,$doc,$dnode); 00296 00297 return $dnode; 00298 } 00299 00300 00301 00302 00311 protected function create_metadata_manifest (cc_i_metadata_manifest $met,DOMDocument &$doc,$xmlnode=null) { 00312 00313 $dnode = $doc->createElementNS($this->ccnamespaces['lomimscc'], "lom"); 00314 if (!empty($xmlnode)) { 00315 $xmlnode->appendChild($dnode); 00316 } 00317 $dnodegeneral = empty($met->arraygeneral ) ? null : $this->create_metadata_general ($met, $doc, $xmlnode); 00318 $dnodetechnical = empty($met->arraytech ) ? null : $this->create_metadata_technical($met, $doc, $xmlnode); 00319 $dnoderights = empty($met->arrayrights ) ? null : $this->create_metadata_rights ($met, $doc, $xmlnode); 00320 $dnodelifecycle = empty($met->arraylifecycle) ? null : $this->create_metadata_lifecycle($met, $doc, $xmlnode); 00321 00322 !is_null($dnodegeneral)?$dnode->appendChild($dnodegeneral):null; 00323 !is_null($dnodetechnical)?$dnode->appendChild($dnodetechnical):null; 00324 !is_null($dnoderights)?$dnode->appendChild($dnoderights):null; 00325 !is_null($dnodelifecycle)?$dnode->appendChild($dnodelifecycle):null; 00326 00327 return $dnode; 00328 00329 } 00330 00331 00332 00341 protected function create_metadata_resource (cc_i_metadata_resource $met,DOMDocument &$doc,$xmlnode=null) { 00342 00343 $dnode = $doc->createElementNS($this->ccnamespaces['lom'], "lom"); 00344 00345 !empty($xmlnode)? $xmlnode->appendChild($dnode):null; 00346 !empty($met->arrayeducational) ? $this->create_metadata_educational($met,$doc,$dnode):null; 00347 00348 return $dnode; 00349 } 00350 00351 00352 00353 00362 protected function create_metadata_file (cc_i_metadata_file $met,DOMDocument &$doc,$xmlnode=null) { 00363 00364 $dnode = $doc->createElementNS($this->ccnamespaces['lom'], "lom"); 00365 00366 !empty($xmlnode)? $xmlnode->appendChild($dnode):null; 00367 !empty($met->arrayeducational) ? $this->create_metadata_educational($met,$doc,$dnode):null; 00368 00369 return $dnode; 00370 } 00371 00372 00373 00374 00383 protected function create_metadata_general($met,DOMDocument &$doc,$xmlnode){ 00384 ($xmlnode); 00385 $nd = $doc->createElementNS($this->ccnamespaces['lomimscc'],'general'); 00386 00387 foreach ($met->arraygeneral as $name => $value) { 00388 !is_array($value)?$value =array($value):null; 00389 foreach ($value as $k => $v){ 00390 ($k); 00391 if ($name != 'language' && $name != 'catalog' && $name != 'entry'){ 00392 $nd2 = $doc->createElementNS($this->ccnamespaces['lomimscc'],$name); 00393 $nd3 = $doc->createElementNS($this->ccnamespaces['lomimscc'],'string',$v[1]); 00394 $ndatt = $doc->createAttribute('language'); 00395 $ndatt->nodeValue = $v[0]; 00396 $nd3->appendChild($ndatt); 00397 $nd2->appendChild($nd3); 00398 $nd->appendChild($nd2); 00399 }else{ 00400 if ($name == 'language'){ 00401 $nd2 = $doc->createElementNS($this->ccnamespaces['lomimscc'],$name,$v[0]); 00402 $nd->appendChild($nd2); 00403 } 00404 } 00405 } 00406 } 00407 if (!empty($met->arraygeneral['catalog']) || !empty($met->arraygeneral['entry'])){ 00408 $nd2 = $doc->createElementNS($this->ccnamespaces['lomimscc'],'identifier'); 00409 $nd->appendChild($nd2); 00410 if (!empty($met->arraygeneral['catalog'])){ 00411 $nd3 = $doc->createElementNS($this->ccnamespaces['lomimscc'],'catalog',$met->arraygeneral['catalog'][0][0]); 00412 $nd2->appendChild($nd3); 00413 } 00414 if (!empty($met->arraygeneral['entry'])){ 00415 $nd4 = $doc->createElementNS($this->ccnamespaces['lomimscc'],'entry',$met->arraygeneral['entry'][0][0]); 00416 $nd2->appendChild($nd4); 00417 } 00418 } 00419 return $nd; 00420 } 00421 00422 00423 00424 00433 protected function create_metadata_technical($met,DOMDocument &$doc,$xmlnode){ 00434 ($xmlnode); 00435 $nd = $doc->createElementNS($this->ccnamespaces['lomimscc'],'technical'); 00436 $xmlnode->appendChild($nd); 00437 00438 foreach ($met->arraytech as $name => $value) { 00439 ($name); 00440 !is_array($value)?$value =array($value):null; 00441 foreach ($value as $k => $v){ 00442 ($k); 00443 $nd2 = $doc->createElementNS($this->ccnamespaces['lomimscc'],$name,$v[0]); 00444 $nd->appendChild($nd2); 00445 } 00446 } 00447 return $nd; 00448 } 00449 00450 00451 00452 00461 protected function create_metadata_rights($met,DOMDocument &$doc,$xmlnode){ 00462 ($xmlnode); 00463 00464 $nd = $doc->createElementNS($this->ccnamespaces['lomimscc'],'rights'); 00465 00466 foreach ($met->arrayrights as $name => $value) { 00467 !is_array($value)?$value =array($value):null; 00468 foreach ($value as $k => $v){ 00469 ($k); 00470 if ($name == 'description'){ 00471 $nd2 = $doc->createElementNS($this->ccnamespaces['lomimscc'],$name); 00472 $nd3 = $doc->createElementNS($this->ccnamespaces['lomimscc'],'string',$v[1]); 00473 $ndatt = $doc->createAttribute('language'); 00474 $ndatt->nodeValue = $v[0]; 00475 $nd3->appendChild($ndatt); 00476 $nd2->appendChild($nd3); 00477 $nd->appendChild($nd2); 00478 }elseif ($name == 'copyrightAndOtherRestrictions' || $name == 'cost'){ 00479 $nd2 = $doc->createElementNS($this->ccnamespaces['lomimscc'],$name); 00480 $nd3 = $doc->createElementNS($this->ccnamespaces['lomimscc'],'value',$v[0]); 00481 $nd2->appendChild($nd3); 00482 $nd->appendChild($nd2); 00483 } 00484 } 00485 } 00486 return $nd; 00487 } 00488 00489 00490 00491 00500 protected function create_metadata_lifecycle($met,DOMDocument &$doc,$xmlnode){ 00501 ($xmlnode); 00502 00503 $nd = $doc->createElementNS($this->ccnamespaces['lomimscc'],'lifeCycle'); 00504 $nd2= $doc->createElementNS($this->ccnamespaces['lomimscc'],'contribute'); 00505 00506 00507 $nd->appendChild ($nd2); 00508 $xmlnode->appendChild ($nd); 00509 00510 foreach ($met->arraylifecycle as $name => $value) { 00511 !is_array($value)?$value =array($value):null; 00512 foreach ($value as $k => $v){ 00513 ($k); 00514 if ($name == 'role'){ 00515 $nd3 = $doc->createElementNS($this->ccnamespaces['lomimscc'],$name); 00516 $nd2->appendChild($nd3); 00517 $nd4 = $doc->createElementNS($this->ccnamespaces['lomimscc'],'value',$v[0]); 00518 $nd3->appendChild($nd4); 00519 }else{ 00520 if ($name == 'date'){ 00521 $nd3 = $doc->createElementNS($this->ccnamespaces['lomimscc'],$name); 00522 $nd2->appendChild($nd3); 00523 $nd4 = $doc->createElementNS($this->ccnamespaces['lomimscc'],'dateTime',$v[0]); 00524 $nd3->appendChild($nd4); 00525 }else{ 00526 $nd3 = $doc->createElementNS($this->ccnamespaces['lomimscc'],$name,$v[0]); 00527 $nd2->appendChild($nd3); 00528 } 00529 } 00530 } 00531 } 00532 return $nd; 00533 } 00534 00535 00536 00537 00546 protected function create_metadata_educational ($met,DOMDocument &$doc, $xmlnode){ 00547 $nd = $doc->createElementNS($this->ccnamespaces['lom'],'educational'); 00548 $nd2 = $doc->createElementNS($this->ccnamespaces['lom'],'intendedEndUserRole'); 00549 $nd3 = $doc->createElementNS($this->ccnamespaces['voc'],'vocabulary'); 00550 00551 $xmlnode->appendChild($nd); 00552 $nd->appendChild($nd2); 00553 $nd2->appendChild($nd3); 00554 00555 foreach ($met->arrayeducational as $name => $value) { 00556 !is_array($value)?$value =array($value):null; 00557 foreach ($value as $k => $v){ 00558 ($k); 00559 $nd4 = $doc->createElementNS($this->ccnamespaces['voc'],$name,$v[0]); 00560 $nd3->appendChild($nd4); 00561 } 00562 } 00563 return $nd; 00564 } 00565 00566 }