Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.
   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_interfaces.php');
  25  require_once ('xmlbase.php');
  26  require_once ('gral_lib/pathutils.php');
  27  require_once ('gral_lib/ccdependencyparser.php');
  28  require_once ('cc_version_base.php');
  29  require_once ('cc_version1.php');
  30  require_once ('cc_manifest.php');
  31  
  32  /**
  33   * Common Cartridge Version
  34   *
  35   */
  36  class cc_version{
  37    const v1  = 1;
  38    const v11 = 11;
  39  }
  40  
  41  
  42  class cc1_resource_type {
  43      const   webcontent          = 'webcontent';
  44      const   questionbank        = 'imsqti_xmlv1p2/imscc_xmlv1p0/question-bank';
  45      const   assessment          = 'imsqti_xmlv1p2/imscc_xmlv1p0/assessment';
  46      const   associatedcontent   = 'associatedcontent/imscc_xmlv1p0/learning-application-resource';
  47      const   discussiontopic     = 'imsdt_xmlv1p0';
  48      const   weblink             = 'imswl_xmlv1p0';
  49  
  50      public static $checker = array(self::webcontent,
  51                                      self::assessment,
  52                                      self::associatedcontent,
  53                                      self::discussiontopic,
  54                                      self::questionbank,
  55                                      self::weblink);
  56  
  57  }
  58  
  59  class cc11_resource_type {
  60      const webcontent         = 'webcontent';
  61      const questionbank       = 'imsqti_xmlv1p2/imscc_xmlv1p1/question-bank';
  62      const assessment         = 'imsqti_xmlv1p2/imscc_xmlv1p1/assessment';
  63      const associatedcontent  = 'associatedcontent/imscc_xmlv1p1/learning-application-resource';
  64      const discussiontopic    = 'imsdt_xmlv1p1';
  65      const weblink            = 'imswl_xmlv1p1';
  66      const basiclti           = 'imsbasiclti_xmlv1p0';
  67  
  68      public static $checker = array(self::webcontent,
  69                                      self::assessment,
  70                                      self::associatedcontent,
  71                                      self::discussiontopic,
  72                                      self::questionbank,
  73                                      self::weblink,
  74                                      self::basiclti);
  75  
  76  }
  77  
  78  
  79  /**
  80   * Resource Class
  81   *
  82   */
  83  class cc_resource implements cc_i_resource {
  84  
  85      public $identifier     = null;
  86      public $type           = null;
  87      public $dependency     = array();
  88      public $identifierref  = null;
  89      public $href           = null;
  90      public $base           = null;
  91      public $persiststate   = null;
  92      public $metadata       = array();
  93      public $filename       = null;
  94      public $files          = array();
  95      public $isempty        = null;
  96      public $manifestroot   = null;
  97      public $folder         = null;
  98      public $instructoronly = false;
  99  
 100      private $throwonerror   = true;
 101  
 102      public function __construct($manifest, $file, $folder='', $throwonerror = true) {
 103          $this->throwonerror = $throwonerror;
 104          if (is_string($manifest)) {
 105              $this->folder = $folder;
 106              $this->process_resource($manifest, $file, $folder);
 107              $this->manifestroot = $manifest;
 108          } else if (is_object($manifest)) {
 109              $this->import_resource($file, $manifest);
 110          }
 111      }
 112  
 113      /**
 114       * Add resource
 115       *
 116       * @param string $fname
 117       * @param string $location
 118       */
 119      public function add_resource ($fname, $location ='') {
 120          $this->process_resource($fname, $location, null);
 121      }
 122  
 123      /**
 124       * Import a resource
 125       *
 126       * @param DOMElement $node
 127       * @param cc_i_manifest $doc
 128       */
 129      public function import_resource(DOMElement &$node, cc_i_manifest &$doc) {
 130  
 131          $searchstr = "//imscc:manifest[@identifier='".$doc->manifestID().
 132                       "']/imscc:resources/imscc:resource";
 133          $this->identifier   = $this->get_attr_value($node, "identifier");
 134          $this->type         = $this->get_attr_value($node, "type");
 135          $this->href         = $this->get_attr_value($node, "href");
 136          $this->base         = $this->get_attr_value($node, "base");
 137          $this->persiststate = null;
 138          $nodo               = $doc->nodeList($searchstr."[@identifier='".
 139                                $this->identifier."']/metadata/@href");
 140          $this->metadata     = $nodo->nodeValue;
 141          $this->filename     = $this->href;
 142          $nlist              = $doc->nodeList($searchstr."[@identifier='".
 143                                $this->identifier."']/imscc:file/@href");
 144          $this->files        = array();
 145          foreach ($nlist as $file) {
 146              $this->files[]  = $file->nodeValue;
 147          }
 148          $nlist              = $doc->nodeList($searchstr."[@identifier='".
 149                                $this->identifier."']/imscc:dependency/@identifierref");
 150          $this->dependency   = array();
 151          foreach ($nlist as $dependency) {
 152              $this->dependency[]  = $dependency->nodeValue;
 153          }
 154          $this->isempty      = false;
 155      }
 156  
 157      /**
 158       * Get a attribute value
 159       *
 160       * @param DOMElement $nod
 161       * @param string $name
 162       * @param string $ns
 163       * @return string
 164       */
 165      public function get_attr_value(&$nod, $name, $ns=null) {
 166          if (is_null($ns)) {
 167              return ($nod->hasAttribute($name) ? $nod->getAttribute($name) : null);
 168          }
 169          return ($nod->hasAttributeNS($ns, $name) ? $nod->getAttributeNS($ns, $name) : null);
 170      }
 171  
 172      /**
 173       * Process a resource
 174       *
 175       * @param string $manifestroot
 176       * @param string $fname
 177       * @param string $folder
 178       */
 179      public function process_resource($manifestroot, &$fname, $folder) {
 180          $file = empty($folder) ? $manifestroot.'/'.$fname : $manifestroot.'/'.$folder.'/'.$fname;
 181          if (!file_exists($file) && $this->throwonerror) {
 182              throw new Exception('The file doesnt exist!');
 183          }
 184  
 185          GetDepFiles($manifestroot, $fname, $this->folder, $this->files);
 186          array_unshift($this->files, $folder.$fname);
 187          $this->init_empty_new();
 188          $this->href             = $folder.$fname;
 189          $this->identifierref    = $folder.$fname;
 190          $this->filename         = $fname;
 191          $this->isempty          = false;
 192          $this->folder           = $folder;
 193      }
 194  
 195      public function adjust_path($mroot, $fname) {
 196          $result = null;
 197          if (file_exists($fname->filename)) {
 198              $result = pathDiff($fname->filename, $mroot);
 199  
 200          } else if (file_exists($mroot.$fname->filename) || file_exists($mroot.DIRECTORY_SEPARATOR.$fname->filename)) {
 201              $result = $fname->filename;
 202              toUrlPath($result);
 203              $result = trim($result, "/");
 204          }
 205          return $result;
 206      }
 207  
 208      public function init_clean() {
 209          $this->identifier    = null;
 210          $this->type          = null;
 211          $this->href          = null;
 212          $this->base          = null;
 213          $this->metadata      = array();
 214          $this->dependency    = array();
 215          $this->identifierref = null;
 216          $this->persiststate  = null;
 217          $this->filename      = '';
 218          $this->files         = array();
 219          $this->isempty       = true;
 220      }
 221  
 222      public function init_empty_new() {
 223          $this->identifier    = cc_helpers::uuidgen('I_', '_R');
 224          $this->type          = null;
 225          $this->href          = null;
 226          $this->persiststate  = null;
 227          $this->filename      = null;
 228          $this->isempty       = false;
 229          $this->identifierref = null;
 230      }
 231  
 232      public function get_manifestroot() {
 233          return $this->manifestroot;
 234      }
 235  }