Now handling of BYTEA (blobs) under PostgreSQL are working properly
[moodle.git] / mod / scorm / lib.php
CommitLineData
e4aa175a 1<?php // $Id$
2
e4aa175a 3/**
4* Given an object containing all the necessary data,
5* (defined by the form in mod.html) this function
6* will create a new instance and return the id number
7* of the new instance.
8*
9* @param mixed $scorm Form data
10* @return int
11*/
23ab8e8d 12//require_once('locallib.php');
e4aa175a 13function scorm_add_instance($scorm) {
a679d64d 14 global $CFG;
15
16 require_once('locallib.php');
17
18 if (($packagedata = scorm_check_package($scorm)) != null) {
19 $scorm->pkgtype = $packagedata->pkgtype;
20 $scorm->datadir = $packagedata->datadir;
21 $scorm->launch = $packagedata->launch;
76ea4fb4 22 $scorm->parse = 1;
76ea4fb4 23
a679d64d 24 $scorm->timemodified = time();
25 if (!scorm_external_link($scorm->reference)) {
26 $scorm->md5hash = md5_file($CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference);
76ea4fb4 27 } else {
a679d64d 28 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
29 $scorm->md5hash = md5_file($scorm->dir.$scorm->datadir.'/'.basename($scorm->reference));
76ea4fb4 30 }
a679d64d 31
e4aa175a 32 $scorm = scorm_option2text($scorm);
33 $scorm->width = str_replace('%','',$scorm->width);
34 $scorm->height = str_replace('%','',$scorm->height);
35
36 //sanitize submitted values a bit
37 $scorm->width = clean_param($scorm->width, PARAM_INT);
38 $scorm->height = clean_param($scorm->height, PARAM_INT);
b3659259 39
40 if (!isset($scorm->whatgrade)) {
41 $scorm->whatgrade = 0;
42 }
a30b6819 43 $scorm->grademethod = ($scorm->whatgrade * 10) + $scorm->grademethod;
e4aa175a 44
45 $id = insert_record('scorm', $scorm);
46
8949f8df 47 if (scorm_external_link($scorm->reference) || ((basename($scorm->reference) != 'imsmanifest.xml') && ($scorm->reference[0] != '#'))) {
e4aa175a 48 // Rename temp scorm dir to scorm id
49 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
50 rename($scorm->dir.$scorm->datadir,$scorm->dir.'/'.$id);
51 }
52
53 // Parse scorm manifest
5c1ac70c 54 if ($scorm->parse == 1) {
e4aa175a 55 $scorm->id = $id;
56 $scorm->launch = scorm_parse($scorm);
57 set_field('scorm','launch',$scorm->launch,'id',$scorm->id);
58 }
59
60 return $id;
a679d64d 61 } else {
62 error(get_string('badpackage','scorm'));
e4aa175a 63 }
64}
65
66/**
67* Given an object containing all the necessary data,
68* (defined by the form in mod.html) this function
69* will update an existing instance with new data.
70*
71* @param mixed $scorm Form data
72* @return int
73*/
74function scorm_update_instance($scorm) {
e4aa175a 75 global $CFG;
76
a679d64d 77 require_once('locallib.php');
76ea4fb4 78
23ab8e8d 79 $scorm->parse = 0;
a679d64d 80 if (($packagedata = scorm_check_package($scorm)) != null) {
81 $scorm->pkgtype = $packagedata->pkgtype;
82 if ($packagedata->launch == 0) {
83 $scorm->launch = $packagedata->launch;
84 $scorm->datadir = $packagedata->datadir;
76ea4fb4 85 $scorm->parse = 1;
a679d64d 86 if (!scorm_external_link($scorm->reference)) {
87 $scorm->md5hash = md5_file($CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference);
88 } else {
89 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
90 $scorm->md5hash = md5_file($scorm->dir.$scorm->datadir.'/'.basename($scorm->reference));
91 }
76ea4fb4 92 }
93 }
94
e4aa175a 95 $scorm->timemodified = time();
96 $scorm->id = $scorm->instance;
bfe8c2f0 97
e4aa175a 98 $scorm = scorm_option2text($scorm);
99 $scorm->width = str_replace('%','',$scorm->width);
100 $scorm->height = str_replace('%','',$scorm->height);
101
b3659259 102 if (!isset($scorm->whatgrade)) {
103 $scorm->whatgrade = 0;
104 }
a30b6819 105 $scorm->grademethod = ($scorm->whatgrade * 10) + $scorm->grademethod;
106
e4aa175a 107 // Check if scorm manifest needs to be reparsed
5c1ac70c 108 if ($scorm->parse == 1) {
e4aa175a 109 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
5c1ac70c 110 if (is_dir($scorm->dir.'/'.$scorm->id)) {
e4aa175a 111 scorm_delete_files($scorm->dir.'/'.$scorm->id);
5c1ac70c 112 }
113 if (isset($scorm->datadir) && ($scorm->datadir != $scorm->id) &&
8949f8df 114 (scorm_external_link($scorm->reference) || ((basename($scorm->reference) != 'imsmanifest.xml') && ($scorm->reference[0] != '#')))) {
e4aa175a 115 rename($scorm->dir.$scorm->datadir,$scorm->dir.'/'.$scorm->id);
116 }
a30b6819 117
e4aa175a 118 $scorm->launch = scorm_parse($scorm);
a9d22c84 119 } else {
120 $oldscorm = get_record('scorm','id',$scorm->id);
121 $scorm->reference = $oldscorm->reference; // This fix a problem with Firefox when the teacher choose Cancel on overwrite question
e4aa175a 122 }
a9d22c84 123
e4aa175a 124 return update_record('scorm', $scorm);
125}
126
127/**
128* Given an ID of an instance of this module,
129* this function will permanently delete the instance
130* and any data that depends on it.
131*
132* @param int $id Scorm instance id
133* @return boolean
134*/
135function scorm_delete_instance($id) {
136
137 global $CFG;
138
139 if (! $scorm = get_record('scorm', 'id', $id)) {
140 return false;
141 }
142
143 $result = true;
144
2b3447c3 145 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
146 if (is_dir($scorm->dir.'/'.$scorm->id)) {
147 // Delete any dependent files
148 require_once('locallib.php');
149 scorm_delete_files($scorm->dir.'/'.$scorm->id);
150 }
e4aa175a 151
152 // Delete any dependent records
153 if (! delete_records('scorm_scoes_track', 'scormid', $scorm->id)) {
154 $result = false;
155 }
b3659259 156 if ($scoes = get_records('scorm_scoes','scorm',$scorm->id)) {
157 foreach ($scoes as $sco) {
158 if (! delete_records('scorm_scoes_data', 'scoid', $sco->id)) {
159 $result = false;
160 }
161 }
162 delete_records('scorm_scoes', 'scorm', $scorm->id);
163 } else {
e4aa175a 164 $result = false;
165 }
166 if (! delete_records('scorm', 'id', $scorm->id)) {
167 $result = false;
168 }
a30b6819 169
2b3447c3 170 /*if (! delete_records('scorm_sequencing_controlmode', 'scormid', $scorm->id)) {
e4aa175a 171 $result = false;
172 }
173 if (! delete_records('scorm_sequencing_rolluprules', 'scormid', $scorm->id)) {
174 $result = false;
175 }
176 if (! delete_records('scorm_sequencing_rolluprule', 'scormid', $scorm->id)) {
177 $result = false;
178 }
179 if (! delete_records('scorm_sequencing_rollupruleconditions', 'scormid', $scorm->id)) {
180 $result = false;
181 }
182 if (! delete_records('scorm_sequencing_rolluprulecondition', 'scormid', $scorm->id)) {
183 $result = false;
184 }
185 if (! delete_records('scorm_sequencing_rulecondition', 'scormid', $scorm->id)) {
186 $result = false;
187 }
188 if (! delete_records('scorm_sequencing_ruleconditions', 'scormid', $scorm->id)) {
189 $result = false;
2b3447c3 190 }*/
e4aa175a 191 return $result;
192}
193
194/**
195* Return a small object with summary information about what a
196* user has done with a given particular instance of this module
197* Used for user activity reports.
198*
199* @param int $course Course id
200* @param int $user User id
201* @param int $mod
202* @param int $scorm The scorm id
203* @return mixed
204*/
205function scorm_user_outline($course, $user, $mod, $scorm) {
206
207 $return = NULL;
a30b6819 208
209 require_once('locallib.php');
210
211 $return = scorm_grade_user($scorm, $user->id, true);
212
e4aa175a 213 return $return;
214}
215
216/**
217* Print a detailed representation of what a user has done with
218* a given particular instance of this module, for user activity reports.
219*
220* @param int $course Course id
221* @param int $user User id
222* @param int $mod
223* @param int $scorm The scorm id
224* @return boolean
225*/
226function scorm_user_complete($course, $user, $mod, $scorm) {
227 global $CFG;
228
229 $liststyle = 'structlist';
230 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
231 $now = time();
232 $firstmodify = $now;
233 $lastmodify = 0;
234 $sometoreport = false;
235 $report = '';
236
237 if ($orgs = get_records_select('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,identifier,title')) {
238 if (count($orgs) <= 1) {
239 unset($orgs);
240 $orgs[]->identifier = '';
241 }
242 $report .= '<div class="mod-scorm">'."\n";
243 foreach ($orgs as $org) {
244 $organizationsql = '';
245 $currentorg = '';
246 if (!empty($org->identifier)) {
247 $report .= '<div class="orgtitle">'.$org->title.'</div>';
248 $currentorg = $org->identifier;
249 $organizationsql = "AND organization='$currentorg'";
250 }
251 $report .= "<ul id='0' class='$liststyle'>";
252 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
253 $level=0;
254 $sublist=1;
255 $parents[$level]='/';
256 foreach ($scoes as $sco) {
257 if ($parents[$level]!=$sco->parent) {
258 if ($level>0 && $parents[$level-1]==$sco->parent) {
259 $report .= "\t\t</ul></li>\n";
260 $level--;
261 } else {
262 $i = $level;
263 $closelist = '';
264 while (($i > 0) && ($parents[$level] != $sco->parent)) {
265 $closelist .= "\t\t</ul></li>\n";
266 $i--;
267 }
268 if (($i == 0) && ($sco->parent != $currentorg)) {
269 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
270 $level++;
271 } else {
272 $report .= $closelist;
273 $level = $i;
274 }
275 $parents[$level]=$sco->parent;
276 }
277 }
278 $report .= "\t\t<li>";
279 $nextsco = next($scoes);
280 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
281 $sublist++;
282 } else {
7150b8ae 283 $report .= '<img src="'.$scormpixdir.'/spacer.gif" alt="" />';
e4aa175a 284 }
285
286 if ($sco->launch) {
287 require_once('locallib.php');
288 $score = '';
289 $totaltime = '';
290 if ($usertrack=scorm_get_tracks($sco->id,$user->id)) {
291 if ($usertrack->status == '') {
292 $usertrack->status = 'notattempted';
293 }
294 $strstatus = get_string($usertrack->status,'scorm');
295 $report .= "<img src='".$scormpixdir.'/'.$usertrack->status.".gif' alt='$strstatus' title='$strstatus' />";
296 if ($usertrack->timemodified != 0) {
297 if ($usertrack->timemodified > $lastmodify) {
298 $lastmodify = $usertrack->timemodified;
299 }
300 if ($usertrack->timemodified < $firstmodify) {
301 $firstmodify = $usertrack->timemodified;
302 }
303 }
304 } else {
305 if ($sco->scormtype == 'sco') {
306 $report .= '<img src="'.$scormpixdir.'/'.'notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
307 } else {
308 $report .= '<img src="'.$scormpixdir.'/'.'asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
309 }
310 }
311 $report .= "&nbsp;$sco->title $score$totaltime</li>\n";
312 if ($usertrack !== false) {
313 $sometoreport = true;
314 $report .= "\t\t\t<li><ul class='$liststyle'>\n";
315 foreach($usertrack as $element => $value) {
316 if (substr($element,0,3) == 'cmi') {
317 $report .= '<li>'.$element.' => '.$value.'</li>';
318 }
319 }
320 $report .= "\t\t\t</ul></li>\n";
321 }
322 } else {
323 $report .= "&nbsp;$sco->title</li>\n";
324 }
325 }
326 for ($i=0;$i<$level;$i++) {
327 $report .= "\t\t</ul></li>\n";
328 }
329 }
330 $report .= "\t</ul><br />\n";
331 }
332 $report .= "</div>\n";
333 }
334 if ($sometoreport) {
335 if ($firstmodify < $now) {
336 $timeago = format_time($now - $firstmodify);
337 echo get_string('firstaccess','scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
338 }
339 if ($lastmodify > 0) {
340 $timeago = format_time($now - $lastmodify);
341 echo get_string('lastaccess','scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
342 }
343 echo get_string('report','scorm').":<br />\n";
344 echo $report;
345 } else {
dabfd0ed 346 print_string('noactivity','scorm');
e4aa175a 347 }
348
349 return true;
350}
351
352/**
353* Given a list of logs, assumed to be those since the last login
354* this function prints a short list of changes related to this module
355* If isteacher is true then perhaps additional information is printed.
356* This function is called from course/lib.php: print_recent_activity()
357*
358* @param reference $logs Logs reference
359* @param boolean $isteacher
360* @return boolean
361*/
362function scorm_print_recent_activity(&$logs, $isteacher=false) {
363 return false; // True if anything was printed, otherwise false
364}
365
366/**
367* Function to be run periodically according to the moodle cron
368* This function searches for things that need to be done, such
369* as sending out mail, toggling flags etc ...
370*
371* @return boolean
372*/
373function scorm_cron () {
374
375 global $CFG;
a679d64d 376
377 require_once('locallib.php');
378
379 $sitetimezone = $CFG->timezone;
380 /// Now see if there are any digest mails waiting to be sent, and if we should send them
bfe8c2f0 381 if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time
382 set_config('scorm_updatetimelast', 0);
383 }
384
a679d64d 385 $timenow = time();
bfe8c2f0 386 $updatetime = usergetmidnight($timenow, $sitetimezone) + ($CFG->scorm_updatetime * 3600);
a679d64d 387
388 if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
389
bfe8c2f0 390 set_config('scorm_updatetimelast', $timenow);
e4aa175a 391
376c9c70 392 mtrace('Updating scorm packages which require daily update');//We are updating
bfe8c2f0 393
a679d64d 394 $scormsupdate = get_records('scorm','updatefreq',UPDATE_EVERYDAY);
395 if (!empty($scormsupdate)) {
396 foreach($scormsupdate as $scormupdate) {
397 $scormupdate->instance = $scormupdate->id;
398 $id = scorm_update_instance($scormupdate);
399 }
400 }
401 }
402
e4aa175a 403 return true;
404}
405
406/**
407* Given a scorm id return all the grades of that activity
408*
409* @param int $scormid Scorm instance id
410* @return mixed
411*/
412function scorm_grades($scormid) {
413
414 global $CFG;
415
416 if (!$scorm = get_record('scorm', 'id', $scormid)) {
417 return NULL;
418 }
e4aa175a 419
a30b6819 420 if (($scorm->grademethod % 10) == 0) { // GRADESCOES
e4aa175a 421 if (!$return->maxgrade = count_records_select('scorm_scoes',"scorm='$scormid' AND launch<>''")) {
422 return NULL;
423 }
424 } else {
425 $return->maxgrade = $scorm->maxgrade;
426 }
427
428 $return->grades = NULL;
429 if ($scousers=get_records_select('scorm_scoes_track', "scormid='$scormid' GROUP BY userid", "", "userid,null")) {
430 require_once('locallib.php');
431 foreach ($scousers as $scouser) {
a30b6819 432 $return->grades[$scouser->userid] = scorm_grade_user($scorm, $scouser->userid);
e4aa175a 433 }
434 }
435 return $return;
436}
437
438function scorm_get_view_actions() {
439 return array('pre-view','view','view all','report');
440}
441
442function scorm_get_post_actions() {
443 return array();
444}
445
e4aa175a 446function scorm_option2text($scorm) {
447 global $SCORM_POPUP_OPTIONS;
e4aa175a 448 if (isset($scorm->popup)) {
76ea4fb4 449 if ($scorm->popup == 1) {
e4aa175a 450 $optionlist = array();
451 foreach ($SCORM_POPUP_OPTIONS as $name => $option) {
452 if (isset($scorm->$name)) {
453 $optionlist[] = $name.'='.$scorm->$name;
454 } else {
455 $optionlist[] = $name.'=0';
456 }
457 }
458 $scorm->options = implode(',', $optionlist);
459 } else {
460 $scorm->options = '';
461 }
462 } else {
463 $scorm->popup = 0;
464 $scorm->options = '';
465 }
466 return $scorm;
467}
468
5c1ac70c 469?>