Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/common_cartridge/export/src/CcManifest.php (2 issues)

1
<?php
2
/* Source: https://github.com/moodle/moodle/blob/MOODLE_310_STABLE/backup/cc/cc_lib/cc_manifest.php under GNU/GPL license */
3
4
class CcManifest extends XMLGenericDocument implements CcIManifest
5
{
6
    private $ccversion = null;
7
    private $ccobj = null;
8
    private $rootmanifest = null;
9
    private $activemanifest = null;
10
    private $parentmanifest = null;
11
    private $parentparentmanifest = null;
12
    private $ares = [];
0 ignored issues
show
The private property $ares is not used, and could be removed.
Loading history...
13
    private $mainidentifier = null;
0 ignored issues
show
The private property $mainidentifier is not used, and could be removed.
Loading history...
14
15
    public function __construct($ccver = 13, $activemanifest = null,
16
                        $parentmanifest = null, $parentparentmanifest = null)
17
    {
18
        $this->ccversion = $ccver;
19
        $this->ccobj = new CcVersion13();
20
        parent::__construct('UTF-8', true);
21
    }
22
23
    /**
24
     * Register Namespace for use XPATH.
25
     */
26
    public function registerNamespacesForXpath()
27
    {
28
        $scnam = $this->activemanifest->getCcNamespaces();
29
        foreach ($scnam as $key => $value) {
30
            $this->registerNS($key, $value);
31
        }
32
    }
33
34
    /**
35
     * Add Metadata For Manifest.
36
     */
37
    public function addMetadataManifest(CcIMetadataManifest $met)
38
    {
39
        $metanode = $this->node("//imscc:manifest[@identifier='".
40
                                $this->activemanifest->manifestID().
41
                                "']/imscc:metadata");
42
        $nmeta = $this->activemanifest->createMetadataNode($met, $this->doc, $metanode);
43
        $metanode->appendChild($nmeta);
44
    }
45
46
    /**
47
     * Add Metadata For Resource.
48
     *
49
     * @param string $identifier
50
     */
51
    public function addMetadataResource(CcIMetadataResource $met, $identifier)
52
    {
53
        $metanode = $this->node("//imscc:resource".
54
            "[@identifier='".
55
            $identifier.
56
            "']");
57
        $metanode2 = $this->node("//imscc:resource".
58
            "[@identifier='".
59
            $identifier.
60
            "']/imscc:file");
61
        $nspaces = $this->activemanifest->getCcNamespaces();
62
        $dnode = $this->appendNewElementNs($metanode2, $nspaces['imscc'], 'metadata');
63
        $this->activemanifest->createMetadataResourceNode($met, $this->doc, $dnode);
64
    }
65
66
    /**
67
     * Add Metadata For File.
68
     *
69
     * @param string $identifier
70
     * @param string $filename
71
     */
72
    public function addMetadataFile(CcIMetadataFile $met, $identifier, $filename)
73
    {
74
        if (empty($met) || empty($identifier) || empty($filename)) {
75
            throw new Exception('Try to add a metadata file with nulls values given!');
76
        }
77
78
        $metanode = $this->node("//imscc:resource".
79
            "[@identifier='".
80
            $identifier.
81
            "']/imscc:file".
82
            "[@href='".
83
            $filename.
84
            "']");
85
86
        $nspaces = $this->activemanifest->getCcNamespaces();
87
        $dnode = $this->doc->createElementNS($nspaces['imscc'], "metadata");
88
89
        $metanode->appendChild($dnode);
90
91
        $this->activemanifest->createMetadataFileNode($met, $this->doc, $dnode);
92
    }
93
94
    public function onCreate()
95
    {
96
        $this->activemanifest = new CcVersion13();
97
        $this->rootmanifest = $this->activemanifest;
98
        $result = $this->activemanifest->createManifest($this->doc);
99
        $this->registerNamespacesForXpath();
100
101
        return $result;
102
    }
103
104
    public function getRelativeBasePath()
105
    {
106
        return $this->activemanifest->base();
107
    }
108
109
    public function parentManifest()
110
    {
111
        return new CcManifest($this, $this->parentmanifest, $this->parentparentmanifest);
112
    }
113
114
    public function rootManifest()
115
    {
116
        return new CcManifest($this, $this->rootmanifest);
117
    }
118
119
    public function manifestID()
120
    {
121
        return $this->activemanifest->manifestID();
122
    }
123
124
    public function getManifestNamespaces()
125
    {
126
        return $this->rootmanifest->getCcNamespaces();
127
    }
128
129
    /**
130
     * Add a new organization.
131
     */
132
    public function addNewOrganization(CcIOrganization &$org)
133
    {
134
        $norg = $this->activemanifest->createOrganizationNode($org, $this->doc);
135
        $orgnode = $this->node("//imscc:manifest[@identifier='".
136
            $this->activemanifest->manifestID().
137
            "']/imscc:organizations");
138
        $orgnode->appendChild($norg);
139
    }
140
141
    public function getResources($searchspecific = '')
142
    {
143
        $reslist = $this->getResourceList($searchspecific);
144
        $resourcelist = [];
145
        foreach ($reslist as $resourceitem) {
146
            $resourcelist[] = new CcResources($this, $resourceitem);
147
        }
148
149
        return $resourcelist;
150
    }
151
152
    public function getCcNamespacePath($nsname)
153
    {
154
        if (is_string($nsname) && (!empty($nsname))) {
155
            $scnam = $this->activemanifest->getCcNamespaces();
156
157
            return $scnam[$nsname];
158
        }
159
160
        return null;
161
    }
162
163
    public function getResourceList($searchspecific = '')
164
    {
165
        return $this->nodeList("//imscc:manifest[@identifier='".
166
                            $this->activemanifest->manifestID().
167
                            "']/imscc:resources/imscc:resource".$searchspecific);
168
    }
169
170
    public function onLoad()
171
    {
172
        $this->registerNamespacesForXpath();
173
        $this->fillManifest();
174
175
        return true;
176
    }
177
178
    public function onSave()
179
    {
180
        return true;
181
    }
182
183
    /**
184
     * Add a resource to the manifest.
185
     *
186
     * @param string $identifier
187
     * @param string $type
188
     *
189
     * @return array
190
     */
191
    public function addResource(CcIResource $res, $identifier = null, $type = 'webcontent')
192
    {
193
        if (!$this->ccobj->valid($type)) {
194
            throw new Exception("Type invalid...");
195
        }
196
197
        if ($res == null) {
198
            throw new Exception('Invalid Resource or dont give it');
199
        }
200
        $rst = $res;
201
202
        // TODO: This has to be reviewed since it does not handle multiple files properly.
203
        // Dependencies.
204
        if (is_object($identifier)) {
205
            $this->activemanifest->createResourceNode($rst, $this->doc, $identifier);
206
        } else {
207
            $nresnode = null;
208
209
            $rst->type = $type;
210
            if (!CcHelpers::isHtml($rst->filename)) {
211
                $rst->href = null;
212
            }
213
214
            $this->activemanifest->createResourceNode($rst, $this->doc, $nresnode);
215
            foreach ($rst->files as $file) {
216
                $ident = $this->getIdentifierByFilename($file);
217
                if ($ident == null) {
218
                    $newres = new CcResources($rst->manifestroot, $file);
219
                    if (!CcHelpers::isHtml($file)) {
220
                        $newres->href = null;
221
                    }
222
                    $newres->type = 'webcontent';
223
                    $this->activemanifest->createResourceNode($newres, $this->doc, $nresnode);
224
                }
225
            }
226
        }
227
228
        $tmparray = [$rst->identifier, $rst->files[0]];
229
230
        return $tmparray;
231
    }
232
233
    public function updateInstructoronly($identifier, $value = false)
234
    {
235
        if (isset($this->activemanifest->resources[$identifier])) {
236
            $resource = $this->activemanifest->resources[$identifier];
237
            $resource->instructoronly = $value;
238
        }
239
    }
240
241
    /**
242
     * Append the resources nodes in the Manifest.
243
     *
244
     * @return DOMNode
245
     */
246
    public function putNodes()
247
    {
248
        $resnodestr = "//imscc:manifest[@identifier='".$this->activemanifest->manifestID().
249
            "']/imscc:resources";
250
        $resnode = $this->node($resnodestr);
251
252
        foreach ($this->activemanifest->resources as $k => $v) {
253
            ($k);
254
            $depen = $this->checkIfExistInOther($v->files[0], $v->identifier);
255
            if (!empty($depen)) {
256
                $this->replaceFileXDependency($depen, $v->files[0]);
257
                $v->type = 'webcontent';
258
            }
259
        }
260
261
        foreach ($this->activemanifest->resources as $node) {
262
            $rnode = $this->activemanifest->createResourceNode($node, $this->doc, null);
263
            $resnode->appendChild($rnode);
264
            if ($node->instructoronly) {
265
                $metafileceduc = new CcMetadataResourceEducational();
266
                $metafileceduc->setValue(intended_user_role::INSTRUCTOR);
267
                $metafile = new CcMetadataResource();
268
                $metafile->addMetadataResourceEducational($metafileceduc);
269
                $this->activemanifest->createMetadataEducational($metafile, $this->doc, $rnode);
270
            }
271
        }
272
273
        return $resnode;
274
    }
275
276
    /**
277
     * TODO - implement this method - critical
278
     */
279
    private function fillManifest()
280
    {
281
    }
282
283
    private function checkIfExistInOther($name, $identifier)
284
    {
285
        $status = [];
286
        foreach ($this->activemanifest->resources as $value) {
287
            if (($value->identifier != $identifier) && isset($value->files[$name])) {
288
                $status[] = $value->identifier;
289
            }
290
        }
291
292
        return $status;
293
    }
294
295
    private function replaceFileXDependency($depen, $name)
296
    {
297
        foreach ($depen as $key => $value) {
298
            ($key);
299
            $ident = $this->getIdentifierByFilename($name);
300
            $this->activemanifest->resources[$value]->files =
301
                $this->arrayRemoveByValue($this->activemanifest->resources[$value]->files, $name);
302
            if (!in_array($ident, $this->activemanifest->resources[$value]->dependency)) {
303
                array_push($this->activemanifest->resources[$value]->dependency, $ident);
304
            }
305
        }
306
307
        return true;
308
    }
309
310
    private function getIdentifierByFilename($name)
311
    {
312
        $result = null;
313
        if (isset($this->activemanifest->resourcesInd[$name])) {
314
            $result = $this->activemanifest->resourcesInd[$name];
315
        }
316
317
        return $result;
318
    }
319
320
    private function arrayRemoveByValue($arr, $value)
321
    {
322
        return array_values(array_diff($arr, [$value]));
323
    }
324
325
    private function arrayRemoveByKey($arr, $key)
326
    {
327
        return array_values(array_diff_key($arr, [$key]));
328
    }
329
}
330