-
Notifications
You must be signed in to change notification settings - Fork 1
/
helper.php
198 lines (175 loc) · 7.97 KB
/
helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
class Helper {
/* creates a compressed zip file */
static function Zip($source, $destination)
{
if (!extension_loaded('zip') || !file_exists($source)) {
return false;
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
return false;
}
$source = str_replace('\\', '/', realpath($source));
if (is_dir($source) === true)
{
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);
foreach ($files as $file)
{
$file = str_replace('\\', '/', $file);
// Ignore "." and ".." folders
if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
continue;
$file = realpath($file);
if (is_dir($file) === true)
{
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
}
else if (is_file($file) === true)
{
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
}
}
}
else if (is_file($source) === true)
{
$zip->addFromString(basename($source), file_get_contents($source));
}
return $zip->close();
}
static function sanitizeFilename($filename) {
//Conver to lowercase
$clean = strtolower($filename);
//Replace blanks with underscores
$clean = str_replace(" ", "_", $clean);
return $clean;
}
//Manifest helpers
static function createLinkXML($link_url)
{
$weblink = simplexml_load_file('./files/weblink.xml');
$weblink->title = $link_url['title'];
$weblink->url->addAttribute('href',$link_url['url']);
$weblink->url->addAttribute('target','_blank');
file_put_contents('./tmp/links/weblink_'.$link_url['id'].'.xml', $weblink->saveXML());
}
static function addIMSSection(&$manifest, &$currentSectionCounter, &$identifierCounter, $section_title)
{
$manifest->organizations->organization->item->addChild('item'); //Section Item
$section = $manifest->organizations->organization->item->item[$currentSectionCounter];
$section->addAttribute('identifier','I_'.$identifierCounter);
$section->addChild('title',$section_title);
$identifierCounter++;
}
static function addIMSItem(&$section, &$manifest, &$sectionCounter, &$identifierCounter, &$resourceCounter, $type, $data, $level)
{
//$level represents indentation
if($level==0)
{ //L0 Item
$section->addChild('item');
$section->item[$sectionCounter]->addAttribute('identifier','I_'.$identifierCounter);
$identifierCounter++;
$section->item[$sectionCounter]->addAttribute('identifierref','I_'.$identifierCounter);
$section->item[$sectionCounter]->addChild('title',$data['title']);
}
else if($level==1)
{
//L0 Item
$section->addChild('item');
$section->item[$sectionCounter]->addAttribute('identifier','I_'.$identifierCounter);
$identifierCounter++;
$section->item[$sectionCounter]->addChild('title');
//L1 Item
$section->item[$sectionCounter]->addChild('item');
$section->item[$sectionCounter]->item->addAttribute('identifier','I_'.$identifierCounter);
$identifierCounter++;
$section->item[$sectionCounter]->item->addAttribute('identifierref','I_'.$identifierCounter);
$section->item[$sectionCounter]->item->addChild('title',$data['title']);
}
else if($level==2)
{
//L0 Item
$section->addChild('item');
$section->item[$sectionCounter]->addAttribute('identifier','I_'.$identifierCounter);
$identifierCounter++;
$section->item[$sectionCounter]->addChild('title');
//L1 Item
$section->item[$sectionCounter]->addChild('item');
$section->item[$sectionCounter]->item->addAttribute('identifier','I_'.$identifierCounter);
$identifierCounter++;
$section->item[$sectionCounter]->item->addChild('title');
//L2 Item
$section->item[$sectionCounter]->item->addChild('item');
$section->item[$sectionCounter]->item->item->addAttribute('identifier','I_'.$identifierCounter);
$identifierCounter++;
$section->item[$sectionCounter]->item->item->addAttribute('identifierref','I_'.$identifierCounter);
$section->item[$sectionCounter]->item->item->addChild('title',$data['title']);
}
if($type=='link')
{
$manifest->resources->addChild('resource');
$resource = $manifest->resources->resource[$resourceCounter];
$resource->addAttribute('identifier','I_'.$identifierCounter);
$resource->addAttribute('type','imswl_xmlv1p1');
$resource->addChild('file');
$resource->file->addAttribute('href','links/weblink_'.$data['id'].'.xml');
$identifierCounter++;
$resourceCounter++;
$sectionCounter++;
}
else if($type=='file')
{
$manifest->resources->addChild('resource');
$resource = $manifest->resources->resource[$resourceCounter];
$resource->addAttribute('identifier','I_'.$identifierCounter);
$resource->addAttribute('type','webcontent');
$resource->addChild('file');
$resource->file->addAttribute('href','course_files/'.Helper::sanitizeFilename($data['filename']));
$identifierCounter++;
$resourceCounter++;
$sectionCounter++;
}
}
static function addIMSLabel(&$section, &$sectionCounter, &$identifierCounter, $name, $level)
{
if($level==0)
{ //L0 Item
$section->addChild('item');
$section->item[$sectionCounter]->addAttribute('identifier','I_'.$identifierCounter);
$identifierCounter++;
$section->item[$sectionCounter]->addChild('title',$name);
}
else if($level==1)
{
//L0 Item
$section->addChild('item');
$section->item[$sectionCounter]->addAttribute('identifier','I_'.$identifierCounter);
$identifierCounter++;
$section->item[$sectionCounter]->addChild('title');
//L1 Item
$section->item[$sectionCounter]->addChild('item');
$section->item[$sectionCounter]->item->addAttribute('identifier','I_'.$identifierCounter);
$identifierCounter++;
$section->item[$sectionCounter]->item->addChild('title',$name);
}
else if($level==2)
{
//L0 Item
$section->addChild('item');
$section->item[$sectionCounter]->addAttribute('identifier','I_'.$identifierCounter);
$identifierCounter++;
$section->item[$sectionCounter]->addChild('title');
//L1 Item
$section->item[$sectionCounter]->addChild('item');
$section->item[$sectionCounter]->item->addAttribute('identifier','I_'.$identifierCounter);
$identifierCounter++;
$section->item[$sectionCounter]->item->addChild('title');
//L2 Item
$section->item[$sectionCounter]->item->addChild('item');
$section->item[$sectionCounter]->item->item->addAttribute('identifier','I_'.$identifierCounter);
$identifierCounter++;
$section->item[$sectionCounter]->item->item->addChild('title',$name);
}
$sectionCounter++;
}
}