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.
   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   * @package    backup-convert
  18   * @subpackage cc-library
  19   * @copyright  2011 Darko Miletic <dmiletic@moodlerooms.com>
  20   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21   */
  22  
  23  require_once ('cc_interfaces.php');
  24  
  25  abstract class cc_converter {
  26      /**
  27       *
  28       * Enter description here ...
  29       * @var cc_item
  30       */
  31      protected $item     = null;
  32      /**
  33       *
  34       * Enter description here ...
  35       * @var cc_manifest
  36       */
  37      protected $manifest = null;
  38      /**
  39       *
  40       * Enter description here ...
  41       * @var string
  42       */
  43      protected $rootpath = null;
  44      /**
  45       *
  46       * Enter description here ...
  47       * @var string
  48       */
  49      protected $path     = null;
  50      /**
  51       *
  52       * Enter description here ...
  53       * @var string
  54       */
  55      protected $defaultfile = null;
  56      /**
  57       *
  58       * Enter description here ...
  59       * @var string
  60       */
  61      protected $defaultname = null;
  62      /**
  63       *
  64       * Enter description here ...
  65       * @var string
  66       */
  67      protected $cc_type = null;
  68      /**
  69       *
  70       * Document
  71       * @var XMLGenericDocument
  72       */
  73      protected $doc = null;
  74  
  75      /**
  76       *
  77       * ctor
  78       * @param  cc_i_item $item
  79       * @param  cc_i_manifest $manifest
  80       * @param  string $rootpath
  81       * @param  string $path
  82       * @throws InvalidArgumentException
  83       */
  84      public function __construct(cc_i_item &$item, cc_i_manifest &$manifest, $rootpath, $path) {
  85          $rpath = realpath($rootpath);
  86          if (empty($rpath)) {
  87              throw new InvalidArgumentException('Invalid path!');
  88          }
  89          $rpath2 = realpath($path);
  90          if (empty($rpath)) {
  91              throw new InvalidArgumentException('Invalid path!');
  92          }
  93          $doc = new XMLGenericDocument();
  94          if (!$doc->load($path . DIRECTORY_SEPARATOR . $this->defaultfile)) {
  95              throw new RuntimeException('File does not exist!');
  96          }
  97  
  98          $this->doc      = $doc;
  99          $this->item     = $item;
 100          $this->manifest = $manifest;
 101          $this->rootpath = $rpath;
 102          $this->path     = $rpath2;
 103      }
 104  
 105      /**
 106       *
 107       * performs conversion
 108       * @param string $outdir - root directory of common cartridge
 109       * @return boolean
 110       */
 111      abstract public function convert($outdir);
 112  
 113      /**
 114       *
 115       * Is the element visible in the course?
 116       * @throws RuntimeException
 117       * @return bool
 118       */
 119      protected function is_visible() {
 120          $tdoc = new XMLGenericDocument();
 121          if (!$tdoc->load($this->path . DIRECTORY_SEPARATOR . 'module.xml')) {
 122              throw new RuntimeException('File does not exist!');
 123          }
 124          $visible = (int)$tdoc->nodeValue('/module/visible');
 125          return ($visible > 0);
 126      }
 127  
 128      /**
 129       *
 130       * Stores any files that need to be stored
 131       */
 132      protected function store(general_cc_file $doc, $outdir, $title, $deps = null) {
 133          $rdir = new cc_resource_location($outdir);
 134          $rtp = $rdir->fullpath(true).$this->defaultname;
 135          if ( $doc->saveTo($rtp) ) {
 136              $resource = new cc_resource($rdir->rootdir(), $this->defaultname, $rdir->dirname(true));
 137              $resource->dependency = empty($deps) ? array() : $deps;
 138              $resource->instructoronly = !$this->is_visible();
 139              $res = $this->manifest->add_resource($resource, null, $this->cc_type);
 140              $resitem = new cc_item();
 141              $resitem->attach_resource($res[0]);
 142              $resitem->title = $title;
 143              $this->item->add_child_item($resitem);
 144          } else {
 145              throw new RuntimeException("Unable to save file {$rtp}!");
 146          }
 147      }
 148  }