MDL-11082 Improved groups upgrade performance 1.8x -> 1.9; thanks Eloy for telling...
[moodle-pu.git] / mod / scorm / lib.php
blob035e9853d33e4899a3380e3a5a9379236c04255d
1 <?php // $Id$
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.
9 * @param mixed $scorm Form data
10 * @return int
12 //require_once('locallib.php');
13 function scorm_add_instance($scorm) {
14 global $CFG;
16 require_once('locallib.php');
18 if (($packagedata = scorm_check_package($scorm)) != null) {
19 $scorm->pkgtype = $packagedata->pkgtype;
20 $scorm->datadir = $packagedata->datadir;
21 $scorm->launch = $packagedata->launch;
22 $scorm->parse = 1;
24 $scorm->timemodified = time();
25 if (!scorm_external_link($scorm->reference)) {
26 $scorm->md5hash = md5_file($CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference);
27 } else {
28 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
29 $scorm->md5hash = md5_file($scorm->dir.$scorm->datadir.'/'.basename($scorm->reference));
32 $scorm = scorm_option2text($scorm);
33 $scorm->width = str_replace('%','',$scorm->width);
34 $scorm->height = str_replace('%','',$scorm->height);
36 //sanitize submitted values a bit
37 $scorm->width = clean_param($scorm->width, PARAM_INT);
38 $scorm->height = clean_param($scorm->height, PARAM_INT);
40 if (!isset($scorm->whatgrade)) {
41 $scorm->whatgrade = 0;
43 $scorm->grademethod = ($scorm->whatgrade * 10) + $scorm->grademethod;
45 $id = insert_record('scorm', $scorm);
47 if (scorm_external_link($scorm->reference) || ((basename($scorm->reference) != 'imsmanifest.xml') && ($scorm->reference[0] != '#'))) {
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);
53 // Parse scorm manifest
54 if ($scorm->parse == 1) {
55 $scorm->id = $id;
56 $scorm->launch = scorm_parse($scorm);
57 set_field('scorm','launch',$scorm->launch,'id',$scorm->id);
60 scorm_grade_item_update(stripslashes_recursive($scorm));
62 return $id;
63 } else {
64 error(get_string('badpackage','scorm'));
68 /**
69 * Given an object containing all the necessary data,
70 * (defined by the form in mod.html) this function
71 * will update an existing instance with new data.
73 * @param mixed $scorm Form data
74 * @return int
76 function scorm_update_instance($scorm) {
77 global $CFG;
79 require_once('locallib.php');
81 $scorm->parse = 0;
82 if (($packagedata = scorm_check_package($scorm)) != null) {
83 $scorm->pkgtype = $packagedata->pkgtype;
84 if ($packagedata->launch == 0) {
85 $scorm->launch = $packagedata->launch;
86 $scorm->datadir = $packagedata->datadir;
87 $scorm->parse = 1;
88 if (!scorm_external_link($scorm->reference)) {
89 $scorm->md5hash = md5_file($CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference);
90 } else {
91 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
92 $scorm->md5hash = md5_file($scorm->dir.$scorm->datadir.'/'.basename($scorm->reference));
97 $scorm->timemodified = time();
98 $scorm->id = $scorm->instance;
100 $scorm = scorm_option2text($scorm);
101 $scorm->width = str_replace('%','',$scorm->width);
102 $scorm->height = str_replace('%','',$scorm->height);
104 if (!isset($scorm->whatgrade)) {
105 $scorm->whatgrade = 0;
107 $scorm->grademethod = ($scorm->whatgrade * 10) + $scorm->grademethod;
109 // Check if scorm manifest needs to be reparsed
110 if ($scorm->parse == 1) {
111 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
112 if (is_dir($scorm->dir.'/'.$scorm->id)) {
113 scorm_delete_files($scorm->dir.'/'.$scorm->id);
115 if (isset($scorm->datadir) && ($scorm->datadir != $scorm->id) &&
116 (scorm_external_link($scorm->reference) || ((basename($scorm->reference) != 'imsmanifest.xml') && ($scorm->reference[0] != '#')))) {
117 rename($scorm->dir.$scorm->datadir,$scorm->dir.'/'.$scorm->id);
120 $scorm->launch = scorm_parse($scorm);
121 } else {
122 $oldscorm = get_record('scorm','id',$scorm->id);
123 $scorm->reference = $oldscorm->reference; // This fix a problem with Firefox when the teacher choose Cancel on overwrite question
126 if ($result = update_record('scorm', $scorm)) {
127 scorm_grade_item_update(stripslashes_recursive($scorm));
130 return $result;
134 * Given an ID of an instance of this module,
135 * this function will permanently delete the instance
136 * and any data that depends on it.
138 * @param int $id Scorm instance id
139 * @return boolean
141 function scorm_delete_instance($id) {
143 global $CFG;
145 if (! $scorm = get_record('scorm', 'id', $id)) {
146 return false;
149 $result = true;
151 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
152 if (is_dir($scorm->dir.'/'.$scorm->id)) {
153 // Delete any dependent files
154 require_once('locallib.php');
155 scorm_delete_files($scorm->dir.'/'.$scorm->id);
158 // Delete any dependent records
159 if (! delete_records('scorm_scoes_track', 'scormid', $scorm->id)) {
160 $result = false;
162 if ($scoes = get_records('scorm_scoes','scorm',$scorm->id)) {
163 foreach ($scoes as $sco) {
164 if (! delete_records('scorm_scoes_data', 'scoid', $sco->id)) {
165 $result = false;
168 delete_records('scorm_scoes', 'scorm', $scorm->id);
169 } else {
170 $result = false;
172 if (! delete_records('scorm', 'id', $scorm->id)) {
173 $result = false;
176 /*if (! delete_records('scorm_sequencing_controlmode', 'scormid', $scorm->id)) {
177 $result = false;
179 if (! delete_records('scorm_sequencing_rolluprules', 'scormid', $scorm->id)) {
180 $result = false;
182 if (! delete_records('scorm_sequencing_rolluprule', 'scormid', $scorm->id)) {
183 $result = false;
185 if (! delete_records('scorm_sequencing_rollupruleconditions', 'scormid', $scorm->id)) {
186 $result = false;
188 if (! delete_records('scorm_sequencing_rolluprulecondition', 'scormid', $scorm->id)) {
189 $result = false;
191 if (! delete_records('scorm_sequencing_rulecondition', 'scormid', $scorm->id)) {
192 $result = false;
194 if (! delete_records('scorm_sequencing_ruleconditions', 'scormid', $scorm->id)) {
195 $result = false;
196 }*/
198 scorm_grade_item_delete(stripslashes_recursive($scorm));
200 return $result;
204 * Return a small object with summary information about what a
205 * user has done with a given particular instance of this module
206 * Used for user activity reports.
208 * @param int $course Course id
209 * @param int $user User id
210 * @param int $mod
211 * @param int $scorm The scorm id
212 * @return mixed
214 function scorm_user_outline($course, $user, $mod, $scorm) {
215 global $CFG;
216 require_once('locallib.php');
218 $return = scorm_grade_user($scorm, $user->id, true);
220 return $return;
224 * Print a detailed representation of what a user has done with
225 * a given particular instance of this module, for user activity reports.
227 * @param int $course Course id
228 * @param int $user User id
229 * @param int $mod
230 * @param int $scorm The scorm id
231 * @return boolean
233 function scorm_user_complete($course, $user, $mod, $scorm) {
234 global $CFG;
236 $liststyle = 'structlist';
237 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
238 $now = time();
239 $firstmodify = $now;
240 $lastmodify = 0;
241 $sometoreport = false;
242 $report = '';
244 if ($orgs = get_records_select('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,identifier,title')) {
245 if (count($orgs) <= 1) {
246 unset($orgs);
247 $orgs[]->identifier = '';
249 $report .= '<div class="mod-scorm">'."\n";
250 foreach ($orgs as $org) {
251 $organizationsql = '';
252 $currentorg = '';
253 if (!empty($org->identifier)) {
254 $report .= '<div class="orgtitle">'.$org->title.'</div>';
255 $currentorg = $org->identifier;
256 $organizationsql = "AND organization='$currentorg'";
258 $report .= "<ul id='0' class='$liststyle'>";
259 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
260 $level=0;
261 $sublist=1;
262 $parents[$level]='/';
263 foreach ($scoes as $sco) {
264 if ($parents[$level]!=$sco->parent) {
265 if ($level>0 && $parents[$level-1]==$sco->parent) {
266 $report .= "\t\t</ul></li>\n";
267 $level--;
268 } else {
269 $i = $level;
270 $closelist = '';
271 while (($i > 0) && ($parents[$level] != $sco->parent)) {
272 $closelist .= "\t\t</ul></li>\n";
273 $i--;
275 if (($i == 0) && ($sco->parent != $currentorg)) {
276 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
277 $level++;
278 } else {
279 $report .= $closelist;
280 $level = $i;
282 $parents[$level]=$sco->parent;
285 $report .= "\t\t<li>";
286 $nextsco = next($scoes);
287 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
288 $sublist++;
289 } else {
290 $report .= '<img src="'.$scormpixdir.'/spacer.gif" alt="" />';
293 if ($sco->launch) {
294 require_once('locallib.php');
295 $score = '';
296 $totaltime = '';
297 if ($usertrack=scorm_get_tracks($sco->id,$user->id)) {
298 if ($usertrack->status == '') {
299 $usertrack->status = 'notattempted';
301 $strstatus = get_string($usertrack->status,'scorm');
302 $report .= "<img src='".$scormpixdir.'/'.$usertrack->status.".gif' alt='$strstatus' title='$strstatus' />";
303 if ($usertrack->timemodified != 0) {
304 if ($usertrack->timemodified > $lastmodify) {
305 $lastmodify = $usertrack->timemodified;
307 if ($usertrack->timemodified < $firstmodify) {
308 $firstmodify = $usertrack->timemodified;
311 } else {
312 if ($sco->scormtype == 'sco') {
313 $report .= '<img src="'.$scormpixdir.'/'.'notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
314 } else {
315 $report .= '<img src="'.$scormpixdir.'/'.'asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
318 $report .= "&nbsp;$sco->title $score$totaltime</li>\n";
319 if ($usertrack !== false) {
320 $sometoreport = true;
321 $report .= "\t\t\t<li><ul class='$liststyle'>\n";
322 foreach($usertrack as $element => $value) {
323 if (substr($element,0,3) == 'cmi') {
324 $report .= '<li>'.$element.' => '.$value.'</li>';
327 $report .= "\t\t\t</ul></li>\n";
329 } else {
330 $report .= "&nbsp;$sco->title</li>\n";
333 for ($i=0;$i<$level;$i++) {
334 $report .= "\t\t</ul></li>\n";
337 $report .= "\t</ul><br />\n";
339 $report .= "</div>\n";
341 if ($sometoreport) {
342 if ($firstmodify < $now) {
343 $timeago = format_time($now - $firstmodify);
344 echo get_string('firstaccess','scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
346 if ($lastmodify > 0) {
347 $timeago = format_time($now - $lastmodify);
348 echo get_string('lastaccess','scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
350 echo get_string('report','scorm').":<br />\n";
351 echo $report;
352 } else {
353 print_string('noactivity','scorm');
356 return true;
360 * Given a list of logs, assumed to be those since the last login
361 * this function prints a short list of changes related to this module
362 * If isteacher is true then perhaps additional information is printed.
363 * This function is called from course/lib.php: print_recent_activity()
365 * @param reference $logs Logs reference
366 * @param boolean $isteacher
367 * @return boolean
369 function scorm_print_recent_activity(&$logs, $isteacher=false) {
370 return false; // True if anything was printed, otherwise false
374 * Function to be run periodically according to the moodle cron
375 * This function searches for things that need to be done, such
376 * as sending out mail, toggling flags etc ...
378 * @return boolean
380 function scorm_cron () {
382 global $CFG;
384 require_once('locallib.php');
386 $sitetimezone = $CFG->timezone;
387 /// Now see if there are any digest mails waiting to be sent, and if we should send them
388 if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time
389 set_config('scorm_updatetimelast', 0);
392 $timenow = time();
393 $updatetime = usergetmidnight($timenow, $sitetimezone) + ($CFG->scorm_updatetime * 3600);
395 if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
397 set_config('scorm_updatetimelast', $timenow);
399 mtrace('Updating scorm packages which require daily update');//We are updating
401 $scormsupdate = get_records('scorm','updatefreq',UPDATE_EVERYDAY);
402 if (!empty($scormsupdate)) {
403 foreach($scormsupdate as $scormupdate) {
404 $scormupdate->instance = $scormupdate->id;
405 $id = scorm_update_instance($scormupdate);
410 return true;
414 * Return grade for given user or all users.
416 * @param int $scormid id of scorm
417 * @param int $userid optional user id, 0 means all users
418 * @return array array of grades, false if none
420 function scorm_get_user_grades($scorm, $userid=0) {
421 global $CFG;
422 require_once('locallib.php');
424 $grades = array();
425 if (empty($userid)) {
426 if ($scousers = get_records_select('scorm_scoes_track', "scormid='$scorm->id' GROUP BY userid", "", "userid,null")) {
427 foreach ($scousers as $scouser) {
428 $grades[$scouser->userid] = new object();
429 $grades[$scouser->userid]->id = $scouser->userid;
430 $grades[$scouser->userid]->userid = $scouser->userid;
431 $grades[$scouser->userid]->rawgrade = scorm_grade_user($scorm, $scouser->userid);
433 } else {
434 return false;
437 } else {
438 if (!get_records_select('scorm_scoes_track', "scormid='$scorm->id' AND userid='$userid' GROUP BY userid", "", "userid,null")) {
439 return false; //no attempt yet
441 $grades[$userid] = new object();
442 $grades[$userid]->id = $userid;
443 $grades[$userid]->userid = $userid;
444 $grades[$userid]->rawgrade = scorm_grade_user($scorm, $userid);
447 return $grades;
451 * Update grades in central gradebook
453 * @param object $scorm null means all scormbases
454 * @param int $userid specific user only, 0 mean all
456 function scorm_update_grades($scorm=null, $userid=0, $nullifnone=true) {
457 global $CFG;
458 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
459 require_once($CFG->libdir.'/gradelib.php');
462 if ($scorm != null) {
463 if ($grades = scorm_get_user_grades($scorm, $userid)) {
464 grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grades);
466 } else if ($userid and $nullifnone) {
467 $grade = new object();
468 $grade->userid = $userid;
469 $grade->rawgrade = NULL;
470 grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grade);
473 } else {
474 $sql = "SELECT s.*, cm.idnumber as cmidnumber
475 FROM {$CFG->prefix}scorm s, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
476 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id";
477 if ($rs = get_recordset_sql($sql)) {
478 if ($rs->RecordCount() > 0) {
479 while ($scorm = rs_fetch_next_record($rs)) {
480 scorm_grade_item_update($scorm);
481 scorm_update_grades($scorm, 0, false);
484 rs_close($rs);
490 * Update/create grade item for given scorm
492 * @param object $scorm object with extra cmidnumber
493 * @return object grade_item
495 function scorm_grade_item_update($scorm) {
496 global $CFG;
497 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
498 require_once($CFG->libdir.'/gradelib.php');
501 $params = array('itemname'=>$scorm->name, 'idnumber'=>$scorm->cmidnumber);
503 if (($scorm->grademethod % 10) == 0) { // GRADESCOES
504 if ($maxgrade = count_records_select('scorm_scoes',"scorm='$scorm->id' AND launch<>''")) {
505 $params['gradetype'] = GRADE_TYPE_VALUE;
506 $params['grademax'] = $maxgrade;
507 $params['grademin'] = 0;
508 } else {
509 $params['gradetype'] = GRADE_TYPE_NONE;
511 } else {
512 $params['gradetype'] = GRADE_TYPE_VALUE;
513 $params['grademax'] = $scorm->maxgrade;
514 $params['grademin'] = 0;
517 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, NULL, $params);
521 * Delete grade item for given scorm
523 * @param object $scorm object
524 * @return object grade_item
526 function scorm_grade_item_delete($scorm) {
527 global $CFG;
528 require_once($CFG->libdir.'/gradelib.php');
530 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, NULL, array('deleted'=>1));
533 function scorm_get_view_actions() {
534 return array('pre-view','view','view all','report');
537 function scorm_get_post_actions() {
538 return array();
541 function scorm_option2text($scorm) {
542 global $SCORM_POPUP_OPTIONS;
543 if (isset($scorm->popup)) {
544 if ($scorm->popup == 1) {
545 $optionlist = array();
546 foreach ($SCORM_POPUP_OPTIONS as $name => $option) {
547 if (isset($scorm->$name)) {
548 $optionlist[] = $name.'='.$scorm->$name;
549 } else {
550 $optionlist[] = $name.'=0';
553 $scorm->options = implode(',', $optionlist);
554 } else {
555 $scorm->options = '';
557 } else {
558 $scorm->popup = 0;
559 $scorm->options = '';
561 return $scorm;