Passed
Push — 1.11.x ( 47915c...7101db )
by Yannick
09:38
created

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

Labels
Severity
1
<?php
2
/* Source: https://github.com/moodle/moodle/blob/MOODLE_310_STABLE/backup/cc/cc_lib/cc_resources.php under GNU/GPL license */
3
4
class CcResources implements CcIResource
5
{
6
    public $identifier = null;
7
    public $type = null;
8
    public $dependency = [];
9
    public $identifierref = null;
10
    public $href = null;
11
    public $base = null;
12
    public $persiststate = null;
13
    public $metadata = [];
14
    public $filename = null;
15
    public $files = [];
16
    public $isempty = null;
17
    public $manifestroot = null;
18
    public $folder = null;
19
    public $instructoronly = false;
20
21
    private $throwonerror = true;
22
23
    public function __construct($manifest, $file, $folder = '', $throwonerror = true)
24
    {
25
        $this->throwonerror = $throwonerror;
26
        if (is_string($manifest)) {
27
            $this->folder = $folder;
28
            $this->processResource($manifest, $file, $folder);
29
            $this->manifestroot = $manifest;
30
        } elseif (is_object($manifest)) {
31
            $this->importResource($file, $manifest);
32
        }
33
    }
34
35
    /**
36
     * Add resource.
37
     *
38
     * @param string $fname
39
     * @param string $location
40
     */
41
    public function addResource($fname, $location = '')
42
    {
43
        $this->processResource($fname, $location, null);
44
    }
45
46
    /**
47
     * Import a resource.
48
     */
49
    public function importResource(DOMElement &$node, CcIManifest &$doc)
50
    {
51
        $searchstr = "//imscc:manifest[@identifier='".$doc->manifestID().
0 ignored issues
show
The method manifestID() does not exist on CcIManifest. Since it exists in all sub-types, consider adding an abstract or default implementation to CcIManifest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

51
        $searchstr = "//imscc:manifest[@identifier='".$doc->/** @scrutinizer ignore-call */ manifestID().
Loading history...
52
                     "']/imscc:resources/imscc:resource";
53
        $this->identifier = $this->getAttrValue($node, "identifier");
54
        $this->type = $this->getAttrValue($node, "type");
55
        $this->href = $this->getAttrValue($node, "href");
56
        $this->base = $this->getAttrValue($node, "base");
57
        $this->persiststate = null;
58
        $nodo = $doc->nodeList($searchstr."[@identifier='".
0 ignored issues
show
The method nodeList() does not exist on CcIManifest. Since it exists in all sub-types, consider adding an abstract or default implementation to CcIManifest. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
        /** @scrutinizer ignore-call */ 
59
        $nodo = $doc->nodeList($searchstr."[@identifier='".
Loading history...
59
                              $this->identifier."']/metadata/@href");
60
        $this->metadata = $nodo->nodeValue;
61
        $this->filename = $this->href;
62
        $nlist = $doc->nodeList($searchstr."[@identifier='".
63
                              $this->identifier."']/imscc:file/@href");
64
        $this->files = [];
65
        foreach ($nlist as $file) {
66
            $this->files[] = $file->nodeValue;
67
        }
68
        $nlist = $doc->nodeList($searchstr."[@identifier='".
69
                              $this->identifier."']/imscc:dependency/@identifierref");
70
        $this->dependency = [];
71
        foreach ($nlist as $dependency) {
72
            $this->dependency[] = $dependency->nodeValue;
73
        }
74
        $this->isempty = false;
75
    }
76
77
    /**
78
     * Get a attribute value.
79
     *
80
     * @param DOMElement $nod
81
     * @param string     $name
82
     * @param string     $ns
83
     *
84
     * @return string
85
     */
86
    public function getAttrValue(&$nod, $name, $ns = null)
87
    {
88
        if (is_null($ns)) {
89
            return $nod->hasAttribute($name) ? $nod->getAttribute($name) : null;
90
        }
91
92
        return $nod->hasAttributeNS($ns, $name) ? $nod->getAttributeNS($ns, $name) : null;
93
    }
94
95
    /**
96
     * Process a resource.
97
     *
98
     * @param string $manifestroot
99
     * @param string $fname
100
     * @param string $folder
101
     */
102
    public function processResource($manifestroot, &$fname, $folder)
103
    {
104
        $file = empty($folder) ? $manifestroot.'/'.$fname : $manifestroot.'/'.$folder.'/'.$fname;
105
106
        if (!file_exists($file) && $this->throwonerror) {
107
            throw new Exception('The file doesnt exist!');
108
        }
109
110
        getDepFiles($manifestroot, $fname, $this->folder, $this->files);
111
        array_unshift($this->files, $folder.$fname);
112
        $this->initEmptyNew();
113
        $this->href = $folder.$fname;
114
        $this->identifierref = $folder.$fname;
115
        $this->filename = $fname;
116
        $this->isempty = false;
117
        $this->folder = $folder;
118
    }
119
120
    public function adjustPath($mroot, $fname)
121
    {
122
        $result = null;
123
        if (file_exists($fname->filename)) {
124
            $result = pathDiff($fname->filename, $mroot);
125
        } elseif (file_exists($mroot.$fname->filename) || file_exists($mroot.DIRECTORY_SEPARATOR.$fname->filename)) {
126
            $result = $fname->filename;
127
            toUrlPath($result);
128
            $result = trim($result, "/");
129
        }
130
131
        return $result;
132
    }
133
134
    public function initClean()
135
    {
136
        $this->identifier = null;
137
        $this->type = null;
138
        $this->href = null;
139
        $this->base = null;
140
        $this->metadata = [];
141
        $this->dependency = [];
142
        $this->identifierref = null;
143
        $this->persiststate = null;
144
        $this->filename = '';
145
        $this->files = [];
146
        $this->isempty = true;
147
    }
148
149
    public function initEmptyNew()
150
    {
151
        $this->identifier = CcHelpers::uuidgen('I_', '_R');
152
        $this->type = null;
153
        $this->href = null;
154
        $this->persiststate = null;
155
        $this->filename = null;
156
        $this->isempty = false;
157
        $this->identifierref = null;
158
    }
159
160
    public function getManifestroot()
161
    {
162
        return $this->manifestroot;
163
    }
164
}
165