moodle_database conversion error.
[moodle.git] / mod / scorm / lib.php
CommitLineData
e4aa175a 1<?php // $Id$
2
e4aa175a 3/**
4* Given an object containing all the necessary data,
7cac0c4b 5* (defined by the form in mod_form.php) this function
e4aa175a 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) {
c18269c7 14 global $CFG, $DB;
a679d64d 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
c18269c7 45 $id = $DB->insert_record('scorm', $scorm);
e4aa175a 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';
8ba4f1e0 50 if (file_exists($scorm->dir.'/'.$id)) {
51 //delete directory as it shouldn't exist! - most likely there from an old moodle install with old files in dataroot
52 scorm_delete_files($scorm->dir.'/'.$id);
53 }
e4aa175a 54 rename($scorm->dir.$scorm->datadir,$scorm->dir.'/'.$id);
55 }
56
57 // Parse scorm manifest
5c1ac70c 58 if ($scorm->parse == 1) {
e4aa175a 59 $scorm->id = $id;
60 $scorm->launch = scorm_parse($scorm);
c18269c7 61 $DB->set_field('scorm', 'launch', $scorm->launch, array('id'=>$scorm->id));
e4aa175a 62 }
63
c18269c7 64 scorm_grade_item_update($scorm);
531fa830 65
e4aa175a 66 return $id;
a679d64d 67 } else {
7cac0c4b 68 print_error('badpackage','scorm');
e4aa175a 69 }
70}
71
72/**
73* Given an object containing all the necessary data,
7cac0c4b 74* (defined by the form in mod_form.php) this function
e4aa175a 75* will update an existing instance with new data.
76*
77* @param mixed $scorm Form data
78* @return int
79*/
80function scorm_update_instance($scorm) {
c18269c7 81 global $CFG, $DB;
e4aa175a 82
a679d64d 83 require_once('locallib.php');
76ea4fb4 84
23ab8e8d 85 $scorm->parse = 0;
a679d64d 86 if (($packagedata = scorm_check_package($scorm)) != null) {
87 $scorm->pkgtype = $packagedata->pkgtype;
88 if ($packagedata->launch == 0) {
89 $scorm->launch = $packagedata->launch;
90 $scorm->datadir = $packagedata->datadir;
76ea4fb4 91 $scorm->parse = 1;
d9397d51 92 if (!scorm_external_link($scorm->reference) && $scorm->reference[0] != '#') { //dont set md5hash if this is from a repo.
a679d64d 93 $scorm->md5hash = md5_file($CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference);
d9397d51 94 } elseif($scorm->reference[0] != '#') { //dont set md5hash if this is from a repo.
a679d64d 95 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
96 $scorm->md5hash = md5_file($scorm->dir.$scorm->datadir.'/'.basename($scorm->reference));
97 }
76ea4fb4 98 }
99 }
100
e4aa175a 101 $scorm->timemodified = time();
102 $scorm->id = $scorm->instance;
bfe8c2f0 103
e4aa175a 104 $scorm = scorm_option2text($scorm);
105 $scorm->width = str_replace('%','',$scorm->width);
106 $scorm->height = str_replace('%','',$scorm->height);
107
b3659259 108 if (!isset($scorm->whatgrade)) {
109 $scorm->whatgrade = 0;
110 }
a30b6819 111 $scorm->grademethod = ($scorm->whatgrade * 10) + $scorm->grademethod;
112
e4aa175a 113 // Check if scorm manifest needs to be reparsed
5c1ac70c 114 if ($scorm->parse == 1) {
e4aa175a 115 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
5c1ac70c 116 if (is_dir($scorm->dir.'/'.$scorm->id)) {
e4aa175a 117 scorm_delete_files($scorm->dir.'/'.$scorm->id);
5c1ac70c 118 }
119 if (isset($scorm->datadir) && ($scorm->datadir != $scorm->id) &&
8949f8df 120 (scorm_external_link($scorm->reference) || ((basename($scorm->reference) != 'imsmanifest.xml') && ($scorm->reference[0] != '#')))) {
e4aa175a 121 rename($scorm->dir.$scorm->datadir,$scorm->dir.'/'.$scorm->id);
122 }
a30b6819 123
e4aa175a 124 $scorm->launch = scorm_parse($scorm);
a9d22c84 125 } else {
c18269c7 126 $oldscorm = $DB->get_record('scorm', array('id'=>$scorm->id));
a9d22c84 127 $scorm->reference = $oldscorm->reference; // This fix a problem with Firefox when the teacher choose Cancel on overwrite question
e4aa175a 128 }
a9d22c84 129
c18269c7 130 if ($result = $DB->update_record('scorm', $scorm)) {
7ef16bf9 131 scorm_grade_item_update(stripslashes_recursive($scorm));
132 //scorm_grade_item_update($scorm); // John Macklins fix - dont think this is needed
531fa830 133 }
134
135 return $result;
e4aa175a 136}
137
138/**
139* Given an ID of an instance of this module,
140* this function will permanently delete the instance
141* and any data that depends on it.
142*
143* @param int $id Scorm instance id
144* @return boolean
145*/
146function scorm_delete_instance($id) {
c18269c7 147 global $CFG, $DB;
e4aa175a 148
c18269c7 149 if (! $scorm = $DB->get_record('scorm', array('id'=>$id))) {
e4aa175a 150 return false;
151 }
152
153 $result = true;
154
2b3447c3 155 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
156 if (is_dir($scorm->dir.'/'.$scorm->id)) {
157 // Delete any dependent files
158 require_once('locallib.php');
159 scorm_delete_files($scorm->dir.'/'.$scorm->id);
160 }
e4aa175a 161
162 // Delete any dependent records
c18269c7 163 if (! $DB->delete_records('scorm_scoes_track', array('scormid'=>$scorm->id))) {
e4aa175a 164 $result = false;
165 }
c18269c7 166 if ($scoes = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id))) {
b3659259 167 foreach ($scoes as $sco) {
c18269c7 168 if (! $DB->delete_records('scorm_scoes_data', array('scoid'=>$sco->id))) {
b3659259 169 $result = false;
170 }
171 }
c18269c7 172 $DB->delete_records('scorm_scoes', array('scorm'=>$scorm->id));
b3659259 173 } else {
e4aa175a 174 $result = false;
175 }
c18269c7 176 if (! $DB->delete_records('scorm', array('id'=>$scorm->id))) {
e4aa175a 177 $result = false;
178 }
a30b6819 179
c18269c7 180 /*if (! $DB->delete_records('scorm_sequencing_controlmode', array('scormid'=>$scorm->id))) {
e4aa175a 181 $result = false;
182 }
c18269c7 183 if (! $DB->delete_records('scorm_sequencing_rolluprules', array('scormid'=>$scorm->id))) {
e4aa175a 184 $result = false;
185 }
c18269c7 186 if (! $DB->delete_records('scorm_sequencing_rolluprule', array('scormid'=>$scorm->id))) {
e4aa175a 187 $result = false;
188 }
c18269c7 189 if (! $DB->delete_records('scorm_sequencing_rollupruleconditions', array('scormid'=>$scorm->id))) {
e4aa175a 190 $result = false;
191 }
c18269c7 192 if (! $DB->delete_records('scorm_sequencing_rolluprulecondition', array('scormid'=>$scorm->id))) {
e4aa175a 193 $result = false;
194 }
c18269c7 195 if (! $DB->delete_records('scorm_sequencing_rulecondition', array('scormid'=>$scorm->id))) {
e4aa175a 196 $result = false;
197 }
c18269c7 198 if (! $DB->delete_records('scorm_sequencing_ruleconditions', array('scormid'=>$scorm->id))) {
e4aa175a 199 $result = false;
531fa830 200 }*/
201
c18269c7 202 scorm_grade_item_delete($scorm);
531fa830 203
e4aa175a 204 return $result;
205}
206
207/**
208* Return a small object with summary information about what a
209* user has done with a given particular instance of this module
210* Used for user activity reports.
211*
212* @param int $course Course id
213* @param int $user User id
214* @param int $mod
215* @param int $scorm The scorm id
216* @return mixed
217*/
218function scorm_user_outline($course, $user, $mod, $scorm) {
531fa830 219 global $CFG;
a30b6819 220 require_once('locallib.php');
221
222 $return = scorm_grade_user($scorm, $user->id, true);
223
e4aa175a 224 return $return;
225}
226
227/**
228* Print a detailed representation of what a user has done with
229* a given particular instance of this module, for user activity reports.
230*
231* @param int $course Course id
232* @param int $user User id
233* @param int $mod
234* @param int $scorm The scorm id
235* @return boolean
236*/
237function scorm_user_complete($course, $user, $mod, $scorm) {
bf347041 238 global $CFG, $DB;
e4aa175a 239
240 $liststyle = 'structlist';
241 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
242 $now = time();
243 $firstmodify = $now;
244 $lastmodify = 0;
245 $sometoreport = false;
246 $report = '';
247
bf347041 248 if ($orgs = $DB->get_records('scorm_scoes', array('scorm'=>$scorm->id, 'organization'=>'', 'launch'=>''),'id','id,identifier,title')) {
e4aa175a 249 if (count($orgs) <= 1) {
250 unset($orgs);
251 $orgs[]->identifier = '';
252 }
253 $report .= '<div class="mod-scorm">'."\n";
254 foreach ($orgs as $org) {
bf347041 255 $conditions = array();
e4aa175a 256 $currentorg = '';
257 if (!empty($org->identifier)) {
258 $report .= '<div class="orgtitle">'.$org->title.'</div>';
259 $currentorg = $org->identifier;
bf347041 260 $conditions['organization'] = $currentorg;
e4aa175a 261 }
262 $report .= "<ul id='0' class='$liststyle'>";
bf347041 263 $conditions['scorm'] = $scorm->id;
264 if ($scoes = $DB->get_records('scorm_scoes', $conditions, "id ASC")){
9fb2de4e 265 // drop keys so that we can access array sequentially
266 $scoes = array_values($scoes);
e4aa175a 267 $level=0;
268 $sublist=1;
269 $parents[$level]='/';
9fb2de4e 270 foreach ($scoes as $pos=>$sco) {
e4aa175a 271 if ($parents[$level]!=$sco->parent) {
272 if ($level>0 && $parents[$level-1]==$sco->parent) {
273 $report .= "\t\t</ul></li>\n";
274 $level--;
275 } else {
276 $i = $level;
277 $closelist = '';
278 while (($i > 0) && ($parents[$level] != $sco->parent)) {
279 $closelist .= "\t\t</ul></li>\n";
280 $i--;
281 }
282 if (($i == 0) && ($sco->parent != $currentorg)) {
283 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
284 $level++;
285 } else {
286 $report .= $closelist;
287 $level = $i;
288 }
289 $parents[$level]=$sco->parent;
290 }
291 }
292 $report .= "\t\t<li>";
9fb2de4e 293 if (isset($scoes[$pos+1])) {
294 $nextsco = $scoes[$pos+1];
295 } else {
296 $nextsco = false;
297 }
e4aa175a 298 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
299 $sublist++;
300 } else {
7150b8ae 301 $report .= '<img src="'.$scormpixdir.'/spacer.gif" alt="" />';
e4aa175a 302 }
303
304 if ($sco->launch) {
305 require_once('locallib.php');
306 $score = '';
307 $totaltime = '';
308 if ($usertrack=scorm_get_tracks($sco->id,$user->id)) {
309 if ($usertrack->status == '') {
310 $usertrack->status = 'notattempted';
311 }
312 $strstatus = get_string($usertrack->status,'scorm');
313 $report .= "<img src='".$scormpixdir.'/'.$usertrack->status.".gif' alt='$strstatus' title='$strstatus' />";
314 if ($usertrack->timemodified != 0) {
315 if ($usertrack->timemodified > $lastmodify) {
316 $lastmodify = $usertrack->timemodified;
317 }
318 if ($usertrack->timemodified < $firstmodify) {
319 $firstmodify = $usertrack->timemodified;
320 }
321 }
322 } else {
323 if ($sco->scormtype == 'sco') {
324 $report .= '<img src="'.$scormpixdir.'/'.'notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
325 } else {
326 $report .= '<img src="'.$scormpixdir.'/'.'asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
327 }
328 }
329 $report .= "&nbsp;$sco->title $score$totaltime</li>\n";
330 if ($usertrack !== false) {
331 $sometoreport = true;
332 $report .= "\t\t\t<li><ul class='$liststyle'>\n";
333 foreach($usertrack as $element => $value) {
334 if (substr($element,0,3) == 'cmi') {
335 $report .= '<li>'.$element.' => '.$value.'</li>';
336 }
337 }
338 $report .= "\t\t\t</ul></li>\n";
339 }
340 } else {
341 $report .= "&nbsp;$sco->title</li>\n";
342 }
343 }
344 for ($i=0;$i<$level;$i++) {
345 $report .= "\t\t</ul></li>\n";
346 }
347 }
348 $report .= "\t</ul><br />\n";
349 }
350 $report .= "</div>\n";
351 }
352 if ($sometoreport) {
353 if ($firstmodify < $now) {
354 $timeago = format_time($now - $firstmodify);
355 echo get_string('firstaccess','scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
356 }
357 if ($lastmodify > 0) {
358 $timeago = format_time($now - $lastmodify);
359 echo get_string('lastaccess','scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
360 }
361 echo get_string('report','scorm').":<br />\n";
362 echo $report;
363 } else {
dabfd0ed 364 print_string('noactivity','scorm');
e4aa175a 365 }
366
367 return true;
368}
369
e4aa175a 370/**
371* Function to be run periodically according to the moodle cron
372* This function searches for things that need to be done, such
373* as sending out mail, toggling flags etc ...
374*
375* @return boolean
376*/
377function scorm_cron () {
bf347041 378 global $CFG, $DB;
a679d64d 379
380 require_once('locallib.php');
381
382 $sitetimezone = $CFG->timezone;
383 /// Now see if there are any digest mails waiting to be sent, and if we should send them
bfe8c2f0 384 if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time
385 set_config('scorm_updatetimelast', 0);
386 }
387
a679d64d 388 $timenow = time();
bfe8c2f0 389 $updatetime = usergetmidnight($timenow, $sitetimezone) + ($CFG->scorm_updatetime * 3600);
a679d64d 390
391 if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
392
bfe8c2f0 393 set_config('scorm_updatetimelast', $timenow);
e4aa175a 394
376c9c70 395 mtrace('Updating scorm packages which require daily update');//We are updating
bfe8c2f0 396
bf347041 397 $scormsupdate = $DB->get_records('scorm', array('updatefreq'=>UPDATE_EVERYDAY));
a679d64d 398 if (!empty($scormsupdate)) {
399 foreach($scormsupdate as $scormupdate) {
400 $scormupdate->instance = $scormupdate->id;
401 $id = scorm_update_instance($scormupdate);
402 }
403 }
404 }
405
e4aa175a 406 return true;
407}
408
409/**
531fa830 410 * Return grade for given user or all users.
411 *
412 * @param int $scormid id of scorm
413 * @param int $userid optional user id, 0 means all users
414 * @return array array of grades, false if none
415 */
416function scorm_get_user_grades($scorm, $userid=0) {
bf347041 417 global $CFG, $DB;
531fa830 418 require_once('locallib.php');
419
420 $grades = array();
421 if (empty($userid)) {
bf347041 422 if ($scousers = $DB->get_records_select('scorm_scoes_track', "scormid=? GROUP BY userid", array($scorm->id), "", "userid,null")) {
531fa830 423 foreach ($scousers as $scouser) {
424 $grades[$scouser->userid] = new object();
425 $grades[$scouser->userid]->id = $scouser->userid;
426 $grades[$scouser->userid]->userid = $scouser->userid;
427 $grades[$scouser->userid]->rawgrade = scorm_grade_user($scorm, $scouser->userid);
428 }
429 } else {
430 return false;
431 }
e4aa175a 432
531fa830 433 } else {
bf347041 434 if (!$DB->get_records_select('scorm_scoes_track', "scormid=? AND userid=? GROUP BY userid", array($scorm->id, $userid), "", "userid,null")) {
531fa830 435 return false; //no attempt yet
436 }
437 $grades[$userid] = new object();
438 $grades[$userid]->id = $userid;
439 $grades[$userid]->userid = $userid;
440 $grades[$userid]->rawgrade = scorm_grade_user($scorm, $userid);
e4aa175a 441 }
e4aa175a 442
531fa830 443 return $grades;
444}
445
446/**
447 * Update grades in central gradebook
448 *
775f811a 449 * @param object $scorm
531fa830 450 * @param int $userid specific user only, 0 mean all
451 */
775f811a 452function scorm_update_grades($scorm, $userid=0, $nullifnone=true) {
bf347041 453 global $CFG, $DB;
775f811a 454 require_once($CFG->libdir.'/gradelib.php');
531fa830 455
775f811a 456 if ($grades = scorm_get_user_grades($scorm, $userid)) {
457 scorm_grade_item_update($scorm, $grades);
531fa830 458
775f811a 459 } else if ($userid and $nullifnone) {
460 $grade = new object();
461 $grade->userid = $userid;
462 $grade->rawgrade = NULL;
463 scorm_grade_item_update($scorm, $grade);
531fa830 464
e4aa175a 465 } else {
775f811a 466 scorm_grade_item_update($scorm);
467 }
468}
469
470/**
471 * Update all grades in gradebook.
472 */
473function scorm_upgrade_grades() {
474 global $DB;
475
476 $sql = "SELECT COUNT('x')
477 FROM {scorm} s, {course_modules} cm, {modules} m
478 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id";
479 $count = $DB->count_records_sql($sql);
480
481 $sql = "SELECT s.*, cm.idnumber AS cmidnumber, s.course AS courseid
482 FROM {scorm} s, {course_modules} cm, {modules} m
483 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id";
484 if ($rs = $DB->get_recordset_sql($sql)) {
485 $prevdebug = $DB->get_debug();
486 $DB->set_debug(false);
487 $pbar = new progress_bar('scormupgradegrades', 500, true);
488 $i=0;
489 foreach ($rs as $scorm) {
490 $i++;
491 upgrade_set_timeout(60*5); // set up timeout, may also abort execution
492 scorm_update_grades($scorm, 0, false);
493 $pbar->update($i, $count, "Updating Scorm grades ($i/$count).");
531fa830 494 }
775f811a 495 $DB->set_debug($prevdebug);
496 $rs->close();
e4aa175a 497 }
531fa830 498}
e4aa175a 499
531fa830 500/**
501 * Update/create grade item for given scorm
502 *
503 * @param object $scorm object with extra cmidnumber
0b5a80a1 504 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
531fa830 505 * @return object grade_item
506 */
0b5a80a1 507function scorm_grade_item_update($scorm, $grades=NULL) {
bf347041 508 global $CFG, $DB;
531fa830 509 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
510 require_once($CFG->libdir.'/gradelib.php');
511 }
512
7ef16bf9 513 $params = array('itemname'=>$scorm->name);
514 if (isset($scorm->cmidnumber)) {
515 $params['idnumber'] = $scorm->cmidnumber;
516 }
517
531fa830 518 if (($scorm->grademethod % 10) == 0) { // GRADESCOES
5b4b959b 519 if ($maxgrade = $DB->count_records_select('scorm_scoes', 'scorm = ? AND launch <> ?', array($scorm->id, $DB->sql_empty()))) {
531fa830 520 $params['gradetype'] = GRADE_TYPE_VALUE;
521 $params['grademax'] = $maxgrade;
522 $params['grademin'] = 0;
523 } else {
524 $params['gradetype'] = GRADE_TYPE_NONE;
e4aa175a 525 }
531fa830 526 } else {
527 $params['gradetype'] = GRADE_TYPE_VALUE;
528 $params['grademax'] = $scorm->maxgrade;
529 $params['grademin'] = 0;
e4aa175a 530 }
531fa830 531
0b5a80a1 532 if ($grades === 'reset') {
533 $params['reset'] = true;
534 $grades = NULL;
535 }
536
537 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, $grades, $params);
531fa830 538}
539
540/**
541 * Delete grade item for given scorm
542 *
543 * @param object $scorm object
544 * @return object grade_item
545 */
546function scorm_grade_item_delete($scorm) {
547 global $CFG;
548 require_once($CFG->libdir.'/gradelib.php');
549
550 return grade_update('mod/scorm', $scorm->course, 'mod', 'scorm', $scorm->id, 0, NULL, array('deleted'=>1));
e4aa175a 551}
552
553function scorm_get_view_actions() {
554 return array('pre-view','view','view all','report');
555}
556
557function scorm_get_post_actions() {
558 return array();
559}
560
e4aa175a 561function scorm_option2text($scorm) {
562 global $SCORM_POPUP_OPTIONS;
e4aa175a 563 if (isset($scorm->popup)) {
76ea4fb4 564 if ($scorm->popup == 1) {
e4aa175a 565 $optionlist = array();
566 foreach ($SCORM_POPUP_OPTIONS as $name => $option) {
567 if (isset($scorm->$name)) {
568 $optionlist[] = $name.'='.$scorm->$name;
569 } else {
570 $optionlist[] = $name.'=0';
571 }
572 }
573 $scorm->options = implode(',', $optionlist);
574 } else {
575 $scorm->options = '';
576 }
577 } else {
578 $scorm->popup = 0;
579 $scorm->options = '';
580 }
581 return $scorm;
582}
583
0b5a80a1 584/**
585 * Implementation of the function for printing the form elements that control
586 * whether the course reset functionality affects the scorm.
587 * @param $mform form passed by reference
588 */
589function scorm_reset_course_form_definition(&$mform) {
590 $mform->addElement('header', 'scormheader', get_string('modulenameplural', 'scorm'));
591 $mform->addElement('advcheckbox', 'reset_scorm', get_string('deleteallattempts','scorm'));
592}
593
594/**
595 * Course reset form defaults.
596 */
597function scorm_reset_course_form_defaults($course) {
598 return array('reset_scorm'=>1);
599}
600
601/**
602 * Removes all grades from gradebook
603 * @param int $courseid
604 * @param string optional type
605 */
606function scorm_reset_gradebook($courseid, $type='') {
bf347041 607 global $CFG, $DB;
0b5a80a1 608
609 $sql = "SELECT s.*, cm.idnumber as cmidnumber, s.course as courseid
bf347041 610 FROM {scorm} s, {course_modules} cm, {modules} m
611 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id AND s.course=?";
0b5a80a1 612
bf347041 613 if ($scorms = $DB->get_records_sql($sql, array($courseid))) {
0b5a80a1 614 foreach ($scorms as $scorm) {
615 scorm_grade_item_update($scorm, 'reset');
616 }
617 }
618}
619
620/**
621 * Actual implementation of the rest coures functionality, delete all the
622 * scorm attempts for course $data->courseid.
623 * @param $data the data submitted from the reset course.
624 * @return array status array
625 */
626function scorm_reset_userdata($data) {
bf347041 627 global $CFG, $DB;
0b5a80a1 628
629 $componentstr = get_string('modulenameplural', 'scorm');
630 $status = array();
631
632 if (!empty($data->reset_scorm)) {
633 $scormssql = "SELECT s.id
bf347041 634 FROM {scorm} s
635 WHERE s.course=?";
0b5a80a1 636
bf347041 637 $DB->delete_records_select('scorm_scoes_track', "scormid IN ($scormssql)", array($data->courseid));
0b5a80a1 638
639 // remove all grades from gradebook
640 if (empty($data->reset_gradebook_grades)) {
641 scorm_reset_gradebook($data->courseid);
642 }
643
644 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallattempts', 'scorm'), 'error'=>false);
645 }
646
647 // no dates to shift here
648
649 return $status;
650}
651
f432bebf 652/**
653 * Returns all other caps used in module
654 */
655function scorm_get_extra_capabilities() {
656 return array('moodle/site:accessallgroups');
657}
658
5c1ac70c 659?>