Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [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  
  46  
  47      public function __construct($node=null, $doc=null) {
  48          if (is_object($node) && is_object($doc)) {
  49              $this->process_organization($node,$doc);
  50          } else {
  51              $this->init_new();
  52          }
  53      }
  54  
  55      /**
  56       * Add one Item into the Organization
  57       *
  58       * @param cc_i_item $item
  59       */
  60      public function add_item(cc_i_item &$item) {
  61          if (is_null($this->itemlist)) {
  62              $this->itemlist = array();
  63          }
  64          $this->itemlist[$item->identifier] = $item;
  65      }
  66  
  67      /**
  68       * Add new Item into the Organization
  69       *
  70       * @param string $title
  71       * @return cc_i_item
  72       */
  73      public function add_new_item($title='') {
  74          $nitem = new cc_item();
  75          $nitem->title = $title;
  76          $this->add_item($nitem);
  77          return $nitem;
  78      }
  79  
  80  
  81      public function has_items() {
  82          return is_array($this->itemlist) && (count($this->itemlist) > 0);
  83      }
  84  
  85      public function attr_value(&$nod, $name, $ns=null) {
  86        return is_null($ns) ?
  87               ($nod->hasAttribute($name) ? $nod->getAttribute($name) : null) :
  88               ($nod->hasAttributeNS($ns, $name) ? $nod->getAttributeNS($ns, $name) : null);
  89      }
  90  
  91  
  92      public function process_organization(&$node,&$doc) {
  93          $this->identifier   = $this->attr_value($node,"identifier");
  94          $this->structure    = $this->attr_value($node,"structure");
  95          $this->title        = '';
  96          $nlist              = $node->getElementsByTagName('title');
  97          if (is_object($nlist) && ($nlist->length > 0) ) {
  98              $this->title = $nlist->item(0)->nodeValue;
  99          }
 100          $nlist = $doc->nodeList("//imscc:organization[@identifier='".$this->identifier."']/imscc:item");
 101          $this->itemlist=array();
 102          foreach ($nlist as $item) {
 103              $this->itemlist[$item->getAttribute("identifier")] = new cc_item($item,$doc);
 104          }
 105          $this->isempty=false;
 106      }
 107  
 108      public function init_new() {
 109          $this->title        = null;
 110          $this->identifier   = cc_helpers::uuidgen('O_');
 111          $this->structure    = 'rooted-hierarchy';
 112          $this->itemlist     = null;
 113          $this->metadata     = null;
 114          $this->sequencing   = null;
 115  
 116      }
 117  
 118      public function uuidgen() {
 119          $uuid = sprintf('%04x%04x', mt_rand(0, 65535), mt_rand(0, 65535));
 120          return strtoupper(trim($uuid));
 121      }
 122  
 123  
 124  }
 125  
 126  
 127  /**
 128   * Item Class
 129   *
 130   */
 131  class cc_item implements cc_i_item  {
 132  
 133  
 134      public  $identifier     = null;
 135      public  $identifierref  = null;
 136      public  $isvisible      = null;
 137      public  $title          = null;
 138      public  $parameters     = null;
 139      public  $childitems     = null;
 140      private $parentItem     = null;
 141      private $isempty        = true;
 142  
 143  
 144  
 145      public function __construct($node=null,$doc=null) {
 146          if (is_object($node)) {
 147              $clname = get_class($node);
 148              if ($clname =='cc_resource') {
 149                  $this->init_new_item();
 150                  $this->identifierref = $node->identifier;
 151                  $this->title = is_string($doc) && (!empty($doc)) ? $doc : 'item';
 152              } else if ($clname =='cc_manifest') {
 153                  $this->init_new_item();
 154                  $this->identifierref = $node->manifestID();
 155                  $this->title = is_string($doc) && (!empty($doc)) ? $doc : 'item';
 156              } else if ( is_object($doc)){
 157                  $this->process_item($node,$doc);
 158              } else {
 159                  $this->init_new_item();
 160              }
 161          } else {
 162              $this->init_new_item();
 163          }
 164      }
 165  
 166  
 167  
 168      public function attr_value(&$nod, $name, $ns=null) {
 169        return is_null($ns) ?
 170               ($nod->hasAttribute($name) ? $nod->getAttribute($name) : null) :
 171               ($nod->hasAttributeNS($ns, $name) ? $nod->getAttributeNS($ns, $name) : null);
 172      }
 173  
 174  
 175      public function process_item(&$node,&$doc) {
 176          $this->identifier       = $this->attr_value($node,"identifier");
 177          $this->structure        = $this->attr_value($node,"structure");
 178          $this->identifierref    = $this->attr_value($node,"identifierref");
 179          $atr = $this->attr_value($node,"isvisible");
 180          $this->isvisible = is_null($atr) ? true : $atr;
 181          $nlist = $node->getElementsByTagName('title');
 182          if (is_object($nlist) && ($nlist->length > 0) ) {
 183              $this->title = $nlist->item(0)->nodeValue;
 184          }
 185          $nlist = $doc->nodeList("//imscc:item[@identifier='".$this->identifier."']/imscc:item");
 186          if ($nlist->length > 0) {
 187              $this->childitems=array();
 188              foreach ($nlist as $item) {
 189                  $key=$this->attr_value($item,"identifier");
 190                  $this->childitems[$key] = new cc_item($item,$doc);
 191              }
 192          }
 193          $this->isempty = false;
 194      }
 195  
 196      /**
 197       * Add one Child Item
 198       *
 199       * @param cc_i_item $item
 200       */
 201      public function add_child_item(cc_i_item &$item) {
 202          if (is_null($this->childitems)) {
 203              $this->childitems = array();
 204          }
 205          $this->childitems[$item->identifier] = $item;
 206      }
 207  
 208  
 209      /**
 210       * Add new child Item
 211       *
 212       * @param string $title
 213       * @return cc_i_item
 214       */
 215      public function add_new_child_item($title='') {
 216          $sc         = new cc_item();
 217          $sc->title  = $title;
 218          $this->add_child_item($sc);
 219          return $sc;
 220      }
 221  
 222  
 223  
 224      public function attach_resource($resource) {
 225  
 226          if ($this->has_child_items()) {
 227              throw new Exception("Can not attach resource to item that contains other items!");
 228          }
 229          $resident = null;
 230          if (is_string($resource)) {
 231              $resident = $resource;
 232          } else if (is_object($resource)) {
 233              $clname = get_class($resource);
 234              if ($clname == 'cc_resource') {
 235                  $resident = $resource->identifier;
 236              } else
 237              if ($clname == 'cc_manifest') {
 238                  $resident = $resource->manifestID();
 239              } else {
 240                  throw new Exception("Unable to attach resource. Invalid object.");
 241              }
 242          }
 243          if (is_null($resident) || (empty($resident))) {
 244              throw new Exception("Resource must have valid identifier!");
 245          }
 246          $this->identifierref = $resident;
 247      }
 248  
 249      public function has_child_items() {
 250          return is_array($this->childitems) && (count($this->childitems) > 0);
 251      }
 252  
 253      public function child_item($identifier) {
 254          return $this->has_child_items() ? $this->childitems[$identifier] : null;
 255      }
 256  
 257  
 258      public function init_clean() {
 259              $this->identifier   = null;
 260              $this->isvisible    = null;
 261              $this->title        = null;
 262              $this->parameters   = null;
 263              $this->childitems   = null;
 264              $this->parentItem   = null;
 265              $this->isempty      = true;
 266      }
 267  
 268      public function init_new_item() {
 269              $this->identifier   = cc_helpers::uuidgen('I_');
 270              $this->isvisible    = true; //default is true
 271              $this->title        = null;
 272              $this->parameters   = null;
 273              $this->childitems   = null;
 274              $this->parentItem   = null;
 275              $this->isempty      = false;
 276      }
 277  
 278  }