1
|
|
|
<?php |
2
|
|
|
/* Source: https://github.com/moodle/moodle/blob/MOODLE_310_STABLE/backup/cc/cc_lib/cc_organization.php under GNU/GPL license */ |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Organization Class. |
6
|
|
|
*/ |
7
|
|
|
class CcOrganization implements CcIOrganization |
8
|
|
|
{ |
9
|
|
|
public $title = null; |
10
|
|
|
public $identifier = null; |
11
|
|
|
public $structure = null; |
12
|
|
|
public $itemlist = null; |
13
|
|
|
private $metadata = null; |
14
|
|
|
private $sequencing = null; |
15
|
|
|
|
16
|
|
|
public function __construct($node = null, $doc = null) |
17
|
|
|
{ |
18
|
|
|
if (is_object($node) && is_object($doc)) { |
19
|
|
|
$this->processOrganization($node, $doc); |
20
|
|
|
} else { |
21
|
|
|
$this->initNew(); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Add one Item into the Organization. |
27
|
|
|
*/ |
28
|
|
|
public function addItem(CcIItem &$item) |
29
|
|
|
{ |
30
|
|
|
if (is_null($this->itemlist)) { |
31
|
|
|
$this->itemlist = []; |
32
|
|
|
} |
33
|
|
|
$this->itemlist[$item->identifier] = $item; |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Add new Item into the Organization. |
38
|
|
|
* |
39
|
|
|
* @param string $title |
40
|
|
|
* |
41
|
|
|
* @return CcIItem |
42
|
|
|
*/ |
43
|
|
|
public function addNewItem($title = '') |
44
|
|
|
{ |
45
|
|
|
$nitem = new CcItem(); |
46
|
|
|
$nitem->title = $title; |
47
|
|
|
$this->addItem($nitem); |
48
|
|
|
|
49
|
|
|
return $nitem; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function hasItems() |
53
|
|
|
{ |
54
|
|
|
return is_array($this->itemlist) && (count($this->itemlist) > 0); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function attrValue(&$nod, $name, $ns = null) |
58
|
|
|
{ |
59
|
|
|
return is_null($ns) ? |
60
|
|
|
($nod->hasAttribute($name) ? $nod->getAttribute($name) : null) : |
61
|
|
|
($nod->hasAttributeNS($ns, $name) ? $nod->getAttributeNS($ns, $name) : null); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function processOrganization(&$node, &$doc) |
65
|
|
|
{ |
66
|
|
|
$this->identifier = $this->attrValue($node, "identifier"); |
67
|
|
|
$this->structure = $this->attrValue($node, "structure"); |
68
|
|
|
$this->title = ''; |
69
|
|
|
$nlist = $node->getElementsByTagName('title'); |
70
|
|
|
if (is_object($nlist) && ($nlist->length > 0)) { |
71
|
|
|
$this->title = $nlist->item(0)->nodeValue; |
72
|
|
|
} |
73
|
|
|
$nlist = $doc->nodeList("//imscc:organization[@identifier='".$this->identifier."']/imscc:item"); |
74
|
|
|
$this->itemlist = []; |
75
|
|
|
foreach ($nlist as $item) { |
76
|
|
|
$this->itemlist[$item->getAttribute("identifier")] = new CcItem($item, $doc); |
77
|
|
|
} |
78
|
|
|
$this->isempty = false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function initNew() |
82
|
|
|
{ |
83
|
|
|
$this->title = null; |
84
|
|
|
$this->identifier = CcHelpers::uuidgen('O_'); |
85
|
|
|
$this->structure = 'rooted-hierarchy'; |
86
|
|
|
$this->itemlist = null; |
87
|
|
|
$this->metadata = null; |
88
|
|
|
$this->sequencing = null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function uuidgen() |
92
|
|
|
{ |
93
|
|
|
$uuid = sprintf('%04x%04x', mt_rand(0, 65535), mt_rand(0, 65535)); |
94
|
|
|
|
95
|
|
|
return strtoupper(trim($uuid)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|