-
Notifications
You must be signed in to change notification settings - Fork 1
/
convertCourse.php
286 lines (224 loc) · 8.92 KB
/
convertCourse.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
//This software converts course exports from the OpenEclass v3 LMS to Moodle v2.6 LMS
//TODO Use simplexml references to cleanup editing sections
//TODO Section Counter does not need to be global?
//TODO Fix folders
//TODO Add categories to link conversion
//TODO Use a function for manifest editing?
include('helper.php');
class ConvertCourse {
private $course_id;
private $outputName; //Name for export zip
private $link; //Database connection
private $host; //Server url with eclass installation
private $course; //Array storing course information
private $manifest; //IMS Manifest to be exported
private $files; //Array storing files and folders
private $identifierCounter; //Counter for identifier to be used next, starts at 1
private $resourceCounter; //Counter for all resources
private $currentSectionCounter; //Counter for current section being edited in manifest
public function __construct($dbhost,$user,$pass,$dbname,$host){
//Connect to database
$this->link = mysqli_connect($dbhost,$user,$pass,$dbname) or die ('Error connecting to mysql');
mysqli_set_charset($this->link, "utf8");
$this->course = array();
$this->host = $host;
//Load manifest template
$this->manifest = simplexml_load_file('./files/imsmanifest.xml');
//Create temporary directory
mkdir('tmp');
//Initialize identifier and currentSection counter
$this->identifierCounter = 1;
$this->currentSectionCounter = 0;
$this->resourceCounter = 0;
}
private function retrieveName(){
//Course code and title
$query = "SELECT cours_id,code,intitule FROM cours WHERE cours_id=".$this->course_id.";";
$result = mysqli_query($this->link,$query);
$data = $result->fetch_assoc();
//Check if course exists
if(is_null($data))
{
echo 'Course with specified id does not exist';
echo "\n";
mysqli_close($this->link);
exit;
}
$this->course['id'] = $this->course_id;
$this->course['code'] = $data['code'];
$this->course['title'] = $data['intitule'];
}
private function retrieveDescription(){
//Course description
$query = "SELECT unit_resources.title,unit_resources.comments,unit_resources.`order` FROM unit_resources
INNER JOIN course_units ON course_units.id=unit_resources.`unit_id`
INNER JOIN cours ON cours.`cours_id`=course_units.`course_id`
WHERE cours.`cours_id`=".$this->course_id." ORDER BY unit_resources.`order`;";
$result = mysqli_query($this->link,$query);
while($data = $result->fetch_assoc())
{
$info = array(
'title' => $data['title'],
'content' => $data['comments']
);
$this->course['course_information'][] = $info;
}
}
private function retrieveAnnouncements(){
//Course announcements
$query = "SELECT annonces.`title`,annonces.`contenu`,annonces.`preview`,annonces.`temps`,annonces.`ordre` FROM annonces WHERE annonces.`cours_id`=".$this->course_id." ORDER BY annonces.`ordre`;";
$result = mysqli_query($this->link,$query);
while($data = $result->fetch_assoc())
{
$announcement = array(
'title' => $data['title'],
'content' => $data['contenu'],
'preview' => $data['preview'],
'date' => $data['temps']
);
$this->course['course_announcements'][] = $announcement;
}
}
private function retrieveLinks(){
//Course link categories
$query = "SELECT link_category.`name`,description,link_category.order,link_category.`id` FROM link_category WHERE link_category.`course_id`=".$this->course_id." ORDER BY link_category.order;";
$result = mysqli_query($this->link,$query);
while($data = $result->fetch_assoc())
{
$category = array(
'id' => $data['id'],
'name' => $data['name'],
'description' => $data['description']
);
$this->course['course_link_categories'][] = $category;
}
$query = "SELECT link.`id`,link.`title`, link.`url`, link.`description`, link.`category`, link.`order` FROM link WHERE link.`course_id`=".$this->course_id." ORDER BY link.`category`, link.`order`;";
//Course links
$result = mysqli_query($this->link, $query);
while($data = $result->fetch_assoc())
{
$link_url = array(
'id' => $data['id'],
'title' => $data['title'],
'url' => $data['url'],
'description' => $data['description'],
'category' => $data['category']
);
$this->course['course_links'][] = $link_url;
}
}
private function convertFiles(){
//Recursively download all files in course and place in logical subfolders
//Create folder in tmp for files
mkdir('./tmp/course_files');
$sectionCounter = 0;
//Add section to manifest
Helper::addIMSSection($this->manifest, $this->currentSectionCounter, $this->identifierCounter, 'Documents');
$section = $this->manifest->organizations->organization->item->item[$this->currentSectionCounter];
//Find all folders and documents
$query = "SELECT document.`path`,document.`filename`,document.`format`,document.`title` FROM document WHERE document.`course_id`=".$this->course_id." ORDER BY path;";
$result = mysqli_query($this->link,$query);
while($data = $result->fetch_assoc())
{
//Find item level
$level = substr_count($data['path'], '/') - 1;
//If it is a file
if($data['format']!='.dir')
{
$s_filename = Helper::sanitizeFilename($data['filename']);
file_put_contents('./tmp/course_files/'.$s_filename, fopen($this->host.'/courses/'.$this->course['code'].'/document'.$data['path'], 'r'));
Helper::addIMSItem($section, $this->manifest, $sectionCounter, $this->identifierCounter, $this->resourceCounter, 'file', $data, $level);
}
//If it is a directory
else if($data['format']=='.dir')
{
Helper::addIMSLabel($section, $sectionCounter, $this->identifierCounter, $data['filename'], $level);
}
}
//Proceed to next section
$this->currentSectionCounter++;
}
private function convertName(){
//Fill in title
$this->manifest->metadata->children('lomimscc',true)->lom->general->title->string = $this->course['title'];
}
private function convertDescription(){
//Fill in description
$description_html = '';
foreach($this->course['course_information'] as $section)
{
$description_html = $description_html.'<h3>'.$section['title'].'</h3><p>'.$section['content'].'</p>';
}
$this->manifest->metadata->children('lomimscc',true)->lom->general->description->string = $description_html;
}
private function convertLinks(){
//If there are links
if(!isset($this->course['course_links']))
return;
//Create folder in tmp for links
mkdir('./tmp/links');
$sectionCounter = 0;
//Add section to manifest
Helper::addIMSSection($this->manifest, $this->currentSectionCounter, $this->identifierCounter, 'Links');
$section = $this->manifest->organizations->organization->item->item[$this->currentSectionCounter];
//First add all general links
foreach($this->course['course_links'] as $link_url)
{
if(intval($link_url['category'])==0)
{
//Create the xml
Helper::createLinkXML($link_url);
//Edit the manifest
Helper::addIMSItem($section, $this->manifest, $sectionCounter, $this->identifierCounter, $this->resourceCounter, 'link', $link_url, 0);
}
}
foreach($this->course['course_link_categories'] as $cat)
{
//Add the label to the manifest
Helper::addIMSLabel($section, $sectionCounter, $this->identifierCounter, $cat['name'], 0);
//Add links below the label
foreach($this->course['course_links'] as $link_url)
{
if(intval($link_url['category'])==$cat['id'])
{
//Create the xml
Helper::createLinkXML($link_url);
//Edit the manifest
Helper::addIMSItem($section, $this->manifest, $sectionCounter, $this->identifierCounter, $this->resourceCounter, 'link', $link_url, 1);
}
}
}
//Proceed to next section
$this->currentSectionCounter++;
}
private function createZip(){
//Write xml output
file_put_contents('./tmp/imsmanifest.xml' , $this->manifest->saveXML());
$result = Helper::Zip('./tmp','./output/'.$this->outputName);
if(!$result)
echo 'Error: Zip creation failed';
//Close database link
mysqli_close($this->link);
}
private function retrieveAll(){
$this->retrieveName();
$this->retrieveDescription();
$this->retrieveAnnouncements();
$this->retrieveLinks();
}
private function convertAll(){
$this->convertName();
$this->convertDescription();
$this->convertFiles();
$this->convertLinks();
}
public function convert($course_id, $outputName){
$this->course_id = $course_id;
$this->outputName = $outputName;
$this->retrieveAll();
$this->convertAll();
$this->createZip();
}
}
?>