Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18  * @package    backup-convert
  19  * @subpackage cc-library
  20  * @copyright  2011 Darko Miletic <dmiletic@moodlerooms.com>
  21  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22  */
  23  
  24  require_once  'cc_utils.php';
  25  require_once  'cc_version_base.php';
  26  require_once  'cc_resources.php';
  27  require_once  'cc_manifest.php';
  28  
  29  
  30  /**
  31   * Organization Class
  32   *
  33   */
  34  
  35  class cc_organization implements cc_i_organization {
  36  
  37  
  38      public   $title         = null;
  39      public   $identifier    = null;
  40      public   $structure     = null;
  41      public   $itemlist      = null;
  42      private  $metadata      = null;
  43      private  $sequencing    = null;
  44  
  45      /** @var bool true if empty, otherwise false. */
  46      protected $isempty;
  47  
  48      public function __construct($node=null, $doc=null) {
  49          if (is_object($node) && is_object($doc)) {
  50              $this->process_organization($node,$doc);
  51          } else {
  52              $this->init_new();
  53          }
  54      }
  55  
  56      /**
  57       * Add one Item into the Organization
  58       *
  59       * @param cc_i_item $item
  60       */
  61      public function add_item(cc_i_item &$item) {
  62          if (is_null($this->itemlist)) {
  63              $this->itemlist = array();
  64          }
  65          $this->itemlist[$item->identifier] = $item;
  66      }
  67  
  68      /**
  69       * Add new Item into the Organization
  70       *
  71       * @param string $title
  72       * @return cc_i_item
  73       */
  74      public function add_new_item($title='') {
  75          $nitem = new cc_item();
  76          $nitem->title = $title;
  77          $this->add_item($nitem);
  78          return $nitem;
  79      }
  80  
  81  
  82      public function has_items() {
  83          return is_array($this->itemlist) && (count($this->itemlist) > 0);
  84      }
  85  
  86      public function attr_value(&$nod, $name, $ns=null) {
  87        return is_null($ns) ?
  88               ($nod->hasAttribute($name) ? $nod->getAttribute($name) : null) :
  89               ($nod->hasAttributeNS($ns, $name) ? $nod->getAttributeNS($ns, $name) : null);
  90      }
  91  
  92  
  93      public function process_organization(&$node,&$doc) {
  94          $this->identifier   = $this->attr_value($node,"identifier");
  95          $this->structure    = $this->attr_value($node,"structure");
  96          $this->title        = '';
  97          $nlist              = $node->getElementsByTagName('title');
  98          if (is_object($nlist) && ($nlist->length > 0) ) {
  99              $this->title = $nlist->item(0)->nodeValue;
 100          }
 101          $nlist = $doc->nodeList("//imscc:organization[@identifier='".$this->identifier."']/imscc:item");
 102          $this->itemlist=array();
 103          foreach ($nlist as $item) {
 104              $this->itemlist[$item->getAttribute("identifier")] = new cc_item($item,$doc);
 105          }
 106          $this->isempty=false;
 107      }
 108  
 109      public function init_new() {
 110          $this->title        = null;
 111          $this->identifier   = cc_helpers::uuidgen('O_');
 112          $this->structure    = 'rooted-hierarchy';
 113          $this->itemlist     = null;
 114          $this->metadata     = null;
 115          $this->sequencing   = null;
 116  
 117      }
 118  
 119      public function uuidgen() {
 120          $uuid = sprintf('%04x%04x', mt_rand(0, 65535), mt_rand(0, 65535));
 121          return strtoupper(trim($uuid));
 122      }
 123  
 124  
 125  }
 126  
 127  
 128  /**
 129   * Item Class
 130   *
 131   */
 132  class cc_item implements cc_i_item  {
 133  
 134  
 135      public  $identifier     = null;
 136      public  $identifierref  = null;
 137      public  $isvisible      = null;
 138      public  $title          = null;
 139      public  $parameters     = null;
 140      public  $childitems     = null;
 141      private $parentItem     = null;
 142      private $isempty        = true;
 143  
 144      /** @var mixed node structure. */
 145      public $structure;
 146  
 147      public function __construct($node=null,$doc=null) {
 148          if (is_object($node)) {
 149              $clname = get_class($node);
 150              if ($clname =='cc_resource') {
 151                  $this->init_new_item();
 152                  $this->identifierref = $node->identifier;
 153                  $this->title = is_string($doc) && (!empty($doc)) ? $doc : 'item';
 154              } else if ($clname =='cc_manifest') {
 155                  $this->init_new_item();
 156                  $this->identifierref = $node->manifestID();
 157                  $this->title = is_string($doc) && (!empty($doc)) ? $doc : 'item';
 158              } else if ( is_object($doc)){
 159                  $this->process_item($node,$doc);
 160              } else {
 161                  $this->init_new_item();
 162              }
 163          } else {
 164              $this->init_new_item();
 165          }
 166      }
 167  
 168  
 169  
 170      public function attr_value(&$nod, $name, $ns=null) {
 171        return is_null($ns) ?
 172               ($nod->hasAttribute($name) ? $nod->getAttribute($name) : null) :
 173               ($nod->hasAttributeNS($ns, $name) ? $nod->getAttributeNS($ns, $name) : null);
 174      }
 175  
 176  
 177      public function process_item(&$node,&$doc) {
 178          $this->identifier       = $this->attr_value($node,"identifier");
 179          $this->structure        = $this->attr_value($node,"structure");
 180          $this->identifierref    = $this->attr_value($node,"identifierref");
 181          $atr = $this->attr_value($node,"isvisible");
 182          $this->isvisible = is_null($atr) ? true : $atr;
 183          $nlist = $node->getElementsByTagName('title');
 184          if (is_object($nlist) && ($nlist->length > 0) ) {
 185              $this->title = $nlist->item(0)->nodeValue;
 186          }
 187          $nlist = $doc->nodeList("//imscc:item[@identifier='".$this->identifier."']/imscc:item");
 188          if ($nlist->length > 0) {
 189              $this->childitems=array();
 190              foreach ($nlist as $item) {
 191                  $key=$this->attr_value($item,"identifier");
 192                  $this->childitems[$key] = new cc_item($item,$doc);
 193              }
 194          }
 195          $this->isempty = false;
 196      }
 197  
 198      /**
 199       * Add one Child Item
 200       *
 201       * @param cc_i_item $item
 202       */
 203      public function add_child_item(cc_i_item &$item) {
 204          if (is_null($this->childitems)) {
 205              $this->childitems = array();
 206          }
 207          $this->childitems[$item->identifier] = $item;
 208      }
 209  
 210  
 211      /**
 212       * Add new child Item
 213       *
 214       * @param string $title
 215       * @return cc_i_item
 216       */
 217      public function add_new_child_item($title='') {
 218          $sc         = new cc_item();
 219          $sc->title  = $title;
 220          $this->add_child_item($sc);
 221          return $sc;
 222      }
 223  
 224  
 225  
 226      public function attach_resource($resource) {
 227  
 228          if ($this->has_child_items()) {
 229              throw new Exception("Can not attach resource to item that contains other items!");
 230          }
 231          $resident = null;
 232          if (is_string($resource)) {
 233              $resident = $resource;
 234          } else if (is_object($resource)) {
 235              $clname = get_class($resource);
 236              if ($clname == 'cc_resource') {
 237                  $resident = $resource->identifier;
 238              } else
 239              if ($clname == 'cc_manifest') {
 240                  $resident = $resource->manifestID();
 241              } else {
 242                  throw new Exception("Unable to attach resource. Invalid object.");
 243              }
 244          }
 245          if (is_null($resident) || (empty($resident))) {
 246              throw new Exception("Resource must have valid identifier!");
 247          }
 248          $this->identifierref = $resident;
 249      }
 250  
 251      public function has_child_items() {
 252          return is_array($this->childitems) && (count($this->childitems) > 0);
 253      }
 254  
 255      public function child_item($identifier) {
 256          return $this->has_child_items() ? $this->childitems[$identifier] : null;
 257      }
 258  
 259  
 260      public function init_clean() {
 261              $this->identifier   = null;
 262              $this->isvisible    = null;
 263              $this->title        = null;
 264              $this->parameters   = null;
 265              $this->childitems   = null;
 266              $this->parentItem   = null;
 267              $this->isempty      = true;
 268      }
 269  
 270      public function init_new_item() {
 271              $this->identifier   = cc_helpers::uuidgen('I_');
 272              $this->isvisible    = true; //default is true
 273              $this->title        = null;
 274              $this->parameters   = null;
 275              $this->childitems   = null;
 276              $this->parentItem   = null;
 277              $this->isempty      = false;
 278      }
 279  
 280  }