2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
23 require_once('cc_interfaces.php');
25 abstract class cc_converter
{
28 * Enter description here ...
31 protected $item = null;
34 * Enter description here ...
37 protected $manifest = null;
40 * Enter description here ...
43 protected $rootpath = null;
46 * Enter description here ...
49 protected $path = null;
52 * Enter description here ...
55 protected $defaultfile = null;
58 * Enter description here ...
61 protected $defaultname = null;
64 * Enter description here ...
67 protected $cc_type = null;
71 * @var XMLGenericDocument
73 protected $doc = null;
78 * @param cc_i_item $item
79 * @param cc_i_manifest $manifest
80 * @param string $rootpath
82 * @throws InvalidArgumentException
84 public function __construct(cc_i_item
&$item, cc_i_manifest
&$manifest, $rootpath, $path) {
85 $rpath = realpath($rootpath);
87 throw new InvalidArgumentException('Invalid path!');
89 $rpath2 = realpath($path);
91 throw new InvalidArgumentException('Invalid path!');
93 $doc = new XMLGenericDocument();
94 if (!$doc->load($path . DIRECTORY_SEPARATOR
. $this->defaultfile
)) {
95 throw new RuntimeException('File does not exist!');
100 $this->manifest
= $manifest;
101 $this->rootpath
= $rpath;
102 $this->path
= $rpath2;
107 * performs conversion
108 * @param string $outdir - root directory of common cartridge
111 abstract public function convert($outdir);
115 * Is the element visible in the course?
116 * @throws RuntimeException
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!');
124 $visible = (int)$tdoc->nodeValue('/module/visible');
125 return ($visible > 0);
130 * Stores any files that need to be stored
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);
145 throw new RuntimeException("Unable to save file {$rtp}!");