|
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_resources.php'; 00027 require_once 'cc_manifest.php'; 00028 00029 00035 class cc_organization implements cc_i_organization { 00036 00037 00038 public $title = null; 00039 public $identifier = null; 00040 public $structure = null; 00041 public $itemlist = null; 00042 private $metadata = null; 00043 private $sequencing = null; 00044 00045 00046 00047 public function __construct($node=null, $doc=null) { 00048 if (is_object($node) && is_object($doc)) { 00049 $this->process_organization($node,$doc); 00050 } else { 00051 $this->init_new(); 00052 } 00053 } 00054 00060 public function add_item(cc_i_item &$item) { 00061 if (is_null($this->itemlist)) { 00062 $this->itemlist = array(); 00063 } 00064 $this->itemlist[$item->identifier] = $item; 00065 } 00066 00073 public function add_new_item($title='') { 00074 $nitem = new cc_item(); 00075 $nitem->title = $title; 00076 $this->add_item($nitem); 00077 return $nitem; 00078 } 00079 00080 00081 public function has_items() { 00082 return is_array($this->itemlist) && (count($this->itemlist) > 0); 00083 } 00084 00085 public function attr_value(&$nod, $name, $ns=null) { 00086 return is_null($ns) ? 00087 ($nod->hasAttribute($name) ? $nod->getAttribute($name) : null) : 00088 ($nod->hasAttributeNS($ns, $name) ? $nod->getAttributeNS($ns, $name) : null); 00089 } 00090 00091 00092 public function process_organization(&$node,&$doc) { 00093 $this->identifier = $this->attr_value($node,"identifier"); 00094 $this->structure = $this->attr_value($node,"structure"); 00095 $this->title = ''; 00096 $nlist = $node->getElementsByTagName('title'); 00097 if (is_object($nlist) && ($nlist->length > 0) ) { 00098 $this->title = $nlist->item(0)->nodeValue; 00099 } 00100 $nlist = $doc->nodeList("//imscc:organization[@identifier='".$this->identifier."']/imscc:item"); 00101 $this->itemlist=array(); 00102 foreach ($nlist as $item) { 00103 $this->itemlist[$item->getAttribute("identifier")] = new cc_item($item,$doc); 00104 } 00105 $this->isempty=false; 00106 } 00107 00108 public function init_new() { 00109 $this->title = null; 00110 $this->identifier = cc_helpers::uuidgen('O_'); 00111 $this->structure = 'rooted-hierarchy'; 00112 $this->itemlist = null; 00113 $this->metadata = null; 00114 $this->sequencing = null; 00115 00116 } 00117 00118 public function uuidgen() { 00119 $uuid = sprintf('%04x%04x', mt_rand(0, 65535), mt_rand(0, 65535)); 00120 return strtoupper(trim($uuid)); 00121 } 00122 00123 00124 } 00125 00126 00131 class cc_item implements cc_i_item { 00132 00133 00134 public $identifier = null; 00135 public $identifierref = null; 00136 public $isvisible = null; 00137 public $title = null; 00138 public $parameters = null; 00139 public $childitems = null; 00140 private $parentItem = null; 00141 private $isempty = true; 00142 00143 00144 00145 public function __construct($node=null,$doc=null) { 00146 if (is_object($node)) { 00147 $clname = get_class($node); 00148 if ($clname =='cc_resource') { 00149 $this->init_new_item(); 00150 $this->identifierref = $node->identifier; 00151 $this->title = is_string($doc) && (!empty($doc)) ? $doc : 'item'; 00152 } else if ($clname =='cc_manifest') { 00153 $this->init_new_item(); 00154 $this->identifierref = $node->manifestID(); 00155 $this->title = is_string($doc) && (!empty($doc)) ? $doc : 'item'; 00156 } else if ( is_object($doc)){ 00157 $this->process_item($node,$doc); 00158 } else { 00159 $this->init_new_item(); 00160 } 00161 } else { 00162 $this->init_new_item(); 00163 } 00164 } 00165 00166 00167 00168 public function attr_value(&$nod, $name, $ns=null) { 00169 return is_null($ns) ? 00170 ($nod->hasAttribute($name) ? $nod->getAttribute($name) : null) : 00171 ($nod->hasAttributeNS($ns, $name) ? $nod->getAttributeNS($ns, $name) : null); 00172 } 00173 00174 00175 public function process_item(&$node,&$doc) { 00176 $this->identifier = $this->attr_value($node,"identifier"); 00177 $this->structure = $this->attr_value($node,"structure"); 00178 $this->identifierref = $this->attr_value($node,"identifierref"); 00179 $atr = $this->attr_value($node,"isvisible"); 00180 $this->isvisible = is_null($atr) ? true : $atr; 00181 $nlist = $node->getElementsByTagName('title'); 00182 if (is_object($nlist) && ($nlist->length > 0) ) { 00183 $this->title = $nlist->item(0)->nodeValue; 00184 } 00185 $nlist = $doc->nodeList("//imscc:item[@identifier='".$this->identifier."']/imscc:item"); 00186 if ($nlist->length > 0) { 00187 $this->childitems=array(); 00188 foreach ($nlist as $item) { 00189 $key=$this->attr_value($item,"identifier"); 00190 $this->childitems[$key] = new cc_item($item,$doc); 00191 } 00192 } 00193 $this->isempty = false; 00194 } 00195 00201 public function add_child_item(cc_i_item &$item) { 00202 if (is_null($this->childitems)) { 00203 $this->childitems = array(); 00204 } 00205 $this->childitems[$item->identifier] = $item; 00206 } 00207 00208 00215 public function add_new_child_item($title='') { 00216 $sc = new cc_item(); 00217 $sc->title = $title; 00218 $this->add_child_item($sc); 00219 return $sc; 00220 } 00221 00222 00223 00224 public function attach_resource($resource) { 00225 00226 if ($this->has_child_items()) { 00227 throw new Exception("Can not attach resource to item that contains other items!"); 00228 } 00229 $resident = null; 00230 if (is_string($resource)) { 00231 $resident = $resource; 00232 } else if (is_object($resource)) { 00233 $clname = get_class($resource); 00234 if ($clname == 'cc_resource') { 00235 $resident = $resource->identifier; 00236 } else 00237 if ($clname == 'cc_manifest') { 00238 $resident = $resource->manifestID(); 00239 } else { 00240 throw new Exception("Unable to attach resource. Invalid object."); 00241 } 00242 } 00243 if (is_null($resident) || (empty($resident))) { 00244 throw new Exception("Resource must have valid identifier!"); 00245 } 00246 $this->identifierref = $resident; 00247 } 00248 00249 public function has_child_items() { 00250 return is_array($this->childitems) && (count($this->childitems) > 0); 00251 } 00252 00253 public function child_item($identifier) { 00254 return $this->has_child_items() ? $this->childitems[$identifier] : null; 00255 } 00256 00257 00258 public function init_clean() { 00259 $this->identifier = null; 00260 $this->isvisible = null; 00261 $this->title = null; 00262 $this->parameters = null; 00263 $this->childitems = null; 00264 $this->parentItem = null; 00265 $this->isempty = true; 00266 } 00267 00268 public function init_new_item() { 00269 $this->identifier = cc_helpers::uuidgen('I_'); 00270 $this->isvisible = true; //default is true 00271 $this->title = null; 00272 $this->parameters = null; 00273 $this->childitems = null; 00274 $this->parentItem = null; 00275 $this->isempty = false; 00276 } 00277 00278 }