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
9 * @param mixed $scorm Form data
12 //require_once('locallib.php');
13 function scorm_add_instance($scorm) {
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
;
24 $scorm->timemodified
= time();
25 if (!scorm_external_link($scorm->reference
)) {
26 $scorm->md5hash
= md5_file($CFG->dataroot
.'/'.$scorm->course
.'/'.$scorm->reference
);
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) {
56 $scorm->launch
= scorm_parse($scorm);
57 set_field('scorm','launch',$scorm->launch
,'id',$scorm->id
);
60 scorm_grade_item_update(stripslashes_recursive($scorm));
64 print_error('badpackage','scorm');
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
76 function scorm_update_instance($scorm) {
79 require_once('locallib.php');
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
;
88 if (!scorm_external_link($scorm->reference
) && $scorm->reference
[0] != '#') { //dont set md5hash if this is from a repo.
89 $scorm->md5hash
= md5_file($CFG->dataroot
.'/'.$scorm->course
.'/'.$scorm->reference
);
90 } elseif($scorm->reference
[0] != '#') { //dont set md5hash if this is from a repo.
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);
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));
128 //scorm_grade_item_update($scorm); // John Macklins fix - dont think this is needed
135 * Given an ID of an instance of this module,
136 * this function will permanently delete the instance
137 * and any data that depends on it.
139 * @param int $id Scorm instance id
142 function scorm_delete_instance($id) {
146 if (! $scorm = get_record('scorm', 'id', $id)) {
152 $scorm->dir
= $CFG->dataroot
.'/'.$scorm->course
.'/moddata/scorm';
153 if (is_dir($scorm->dir
.'/'.$scorm->id
)) {
154 // Delete any dependent files
155 require_once('locallib.php');
156 scorm_delete_files($scorm->dir
.'/'.$scorm->id
);
159 // Delete any dependent records
160 if (! delete_records('scorm_scoes_track', 'scormid', $scorm->id
)) {
163 if ($scoes = get_records('scorm_scoes','scorm',$scorm->id
)) {
164 foreach ($scoes as $sco) {
165 if (! delete_records('scorm_scoes_data', 'scoid', $sco->id
)) {
169 delete_records('scorm_scoes', 'scorm', $scorm->id
);
173 if (! delete_records('scorm', 'id', $scorm->id
)) {
177 /*if (! delete_records('scorm_sequencing_controlmode', 'scormid', $scorm->id)) {
180 if (! delete_records('scorm_sequencing_rolluprules', 'scormid', $scorm->id)) {
183 if (! delete_records('scorm_sequencing_rolluprule', 'scormid', $scorm->id)) {
186 if (! delete_records('scorm_sequencing_rollupruleconditions', 'scormid', $scorm->id)) {
189 if (! delete_records('scorm_sequencing_rolluprulecondition', 'scormid', $scorm->id)) {
192 if (! delete_records('scorm_sequencing_rulecondition', 'scormid', $scorm->id)) {
195 if (! delete_records('scorm_sequencing_ruleconditions', 'scormid', $scorm->id)) {
199 scorm_grade_item_delete(stripslashes_recursive($scorm));
205 * Return a small object with summary information about what a
206 * user has done with a given particular instance of this module
207 * Used for user activity reports.
209 * @param int $course Course id
210 * @param int $user User id
212 * @param int $scorm The scorm id
215 function scorm_user_outline($course, $user, $mod, $scorm) {
217 require_once('locallib.php');
219 $return = scorm_grade_user($scorm, $user->id
, true);
225 * Print a detailed representation of what a user has done with
226 * a given particular instance of this module, for user activity reports.
228 * @param int $course Course id
229 * @param int $user User id
231 * @param int $scorm The scorm id
234 function scorm_user_complete($course, $user, $mod, $scorm) {
237 $liststyle = 'structlist';
238 $scormpixdir = $CFG->modpixpath
.'/scorm/pix';
242 $sometoreport = false;
245 if ($orgs = get_records_select('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,identifier,title')) {
246 if (count($orgs) <= 1) {
248 $orgs[]->identifier
= '';
250 $report .= '<div class="mod-scorm">'."\n";
251 foreach ($orgs as $org) {
252 $organizationsql = '';
254 if (!empty($org->identifier
)) {
255 $report .= '<div class="orgtitle">'.$org->title
.'</div>';
256 $currentorg = $org->identifier
;
257 $organizationsql = "AND organization='$currentorg'";
259 $report .= "<ul id='0' class='$liststyle'>";
260 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
261 // drop keys so that we can access array sequentially
262 $scoes = array_values($scoes);
265 $parents[$level]='/';
266 foreach ($scoes as $pos=>$sco) {
267 if ($parents[$level]!=$sco->parent
) {
268 if ($level>0 && $parents[$level-1]==$sco->parent
) {
269 $report .= "\t\t</ul></li>\n";
274 while (($i > 0) && ($parents[$level] != $sco->parent
)) {
275 $closelist .= "\t\t</ul></li>\n";
278 if (($i == 0) && ($sco->parent
!= $currentorg)) {
279 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
282 $report .= $closelist;
285 $parents[$level]=$sco->parent
;
288 $report .= "\t\t<li>";
289 if (isset($scoes[$pos+
1])) {
290 $nextsco = $scoes[$pos+
1];
294 if (($nextsco !== false) && ($sco->parent
!= $nextsco->parent
) && (($level==0) ||
(($level>0) && ($nextsco->parent
== $sco->identifier
)))) {
297 $report .= '<img src="'.$scormpixdir.'/spacer.gif" alt="" />';
301 require_once('locallib.php');
304 if ($usertrack=scorm_get_tracks($sco->id
,$user->id
)) {
305 if ($usertrack->status
== '') {
306 $usertrack->status
= 'notattempted';
308 $strstatus = get_string($usertrack->status
,'scorm');
309 $report .= "<img src='".$scormpixdir.'/'.$usertrack->status
.".gif' alt='$strstatus' title='$strstatus' />";
310 if ($usertrack->timemodified
!= 0) {
311 if ($usertrack->timemodified
> $lastmodify) {
312 $lastmodify = $usertrack->timemodified
;
314 if ($usertrack->timemodified
< $firstmodify) {
315 $firstmodify = $usertrack->timemodified
;
319 if ($sco->scormtype
== 'sco') {
320 $report .= '<img src="'.$scormpixdir.'/'.'notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
322 $report .= '<img src="'.$scormpixdir.'/'.'asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
325 $report .= " $sco->title $score$totaltime</li>\n";
326 if ($usertrack !== false) {
327 $sometoreport = true;
328 $report .= "\t\t\t<li><ul class='$liststyle'>\n";
329 foreach($usertrack as $element => $value) {
330 if (substr($element,0,3) == 'cmi') {
331 $report .= '<li>'.$element.' => '.$value.'</li>';
334 $report .= "\t\t\t</ul></li>\n";
337 $report .= " $sco->title</li>\n";
340 for ($i=0;$i<$level;$i++
) {
341 $report .= "\t\t</ul></li>\n";
344 $report .= "\t</ul><br />\n";
346 $report .= "</div>\n";
349 if ($firstmodify < $now) {
350 $timeago = format_time($now - $firstmodify);
351 echo get_string('firstaccess','scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
353 if ($lastmodify > 0) {
354 $timeago = format_time($now - $lastmodify);
355 echo get_string('lastaccess','scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
357 echo get_string('report','scorm').":<br />\n";
360 print_string('noactivity','scorm');
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 ...
373 function scorm_cron () {
377 require_once('locallib.php');
379 $sitetimezone = $CFG->timezone
;
380 /// Now see if there are any digest mails waiting to be sent, and if we should send them
381 if (!isset($CFG->scorm_updatetimelast
)) { // To catch the first time
382 set_config('scorm_updatetimelast', 0);
386 $updatetime = usergetmidnight($timenow, $sitetimezone) +
($CFG->scorm_updatetime
* 3600);
388 if ($CFG->scorm_updatetimelast
< $updatetime and $timenow > $updatetime) {
390 set_config('scorm_updatetimelast', $timenow);
392 mtrace('Updating scorm packages which require daily update');//We are updating
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);
407 * Return grade for given user or all users.
409 * @param int $scormid id of scorm
410 * @param int $userid optional user id, 0 means all users
411 * @return array array of grades, false if none
413 function scorm_get_user_grades($scorm, $userid=0) {
415 require_once('locallib.php');
418 if (empty($userid)) {
419 if ($scousers = get_records_select('scorm_scoes_track', "scormid='$scorm->id' GROUP BY userid", "", "userid,null")) {
420 foreach ($scousers as $scouser) {
421 $grades[$scouser->userid
] = new object();
422 $grades[$scouser->userid
]->id
= $scouser->userid
;
423 $grades[$scouser->userid
]->userid
= $scouser->userid
;
424 $grades[$scouser->userid
]->rawgrade
= scorm_grade_user($scorm, $scouser->userid
);
431 if (!get_records_select('scorm_scoes_track', "scormid='$scorm->id' AND userid='$userid' GROUP BY userid", "", "userid,null")) {
432 return false; //no attempt yet
434 $grades[$userid] = new object();
435 $grades[$userid]->id
= $userid;
436 $grades[$userid]->userid
= $userid;
437 $grades[$userid]->rawgrade
= scorm_grade_user($scorm, $userid);
444 * Update grades in central gradebook
446 * @param object $scorm null means all scormbases
447 * @param int $userid specific user only, 0 mean all
449 function scorm_update_grades($scorm=null, $userid=0, $nullifnone=true) {
451 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
452 require_once($CFG->libdir
.'/gradelib.php');
455 if ($scorm != null) {
456 if ($grades = scorm_get_user_grades($scorm, $userid)) {
457 scorm_grade_item_update($scorm, $grades);
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);
466 scorm_grade_item_update($scorm);
470 $sql = "SELECT s.*, cm.idnumber as cmidnumber
471 FROM {$CFG->prefix}scorm s, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
472 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id";
473 if ($rs = get_recordset_sql($sql)) {
474 while ($scorm = rs_fetch_next_record($rs)) {
475 scorm_update_grades($scorm, 0, false);
483 * Update/create grade item for given scorm
485 * @param object $scorm object with extra cmidnumber
486 * @param mixed optional array/object of grade(s); 'reset' means reset grades in gradebook
487 * @return object grade_item
489 function scorm_grade_item_update($scorm, $grades=NULL) {
491 if (!function_exists('grade_update')) { //workaround for buggy PHP versions
492 require_once($CFG->libdir
.'/gradelib.php');
495 $params = array('itemname'=>$scorm->name
);
496 if (isset($scorm->cmidnumber
)) {
497 $params['idnumber'] = $scorm->cmidnumber
;
500 if (($scorm->grademethod %
10) == 0) { // GRADESCOES
501 if ($maxgrade = count_records_select('scorm_scoes',"scorm='$scorm->id' AND launch<>'".sql_empty()."'")) {
502 $params['gradetype'] = GRADE_TYPE_VALUE
;
503 $params['grademax'] = $maxgrade;
504 $params['grademin'] = 0;
506 $params['gradetype'] = GRADE_TYPE_NONE
;
509 $params['gradetype'] = GRADE_TYPE_VALUE
;
510 $params['grademax'] = $scorm->maxgrade
;
511 $params['grademin'] = 0;
514 if ($grades === 'reset') {
515 $params['reset'] = true;
519 return grade_update('mod/scorm', $scorm->course
, 'mod', 'scorm', $scorm->id
, 0, $grades, $params);
523 * Delete grade item for given scorm
525 * @param object $scorm object
526 * @return object grade_item
528 function scorm_grade_item_delete($scorm) {
530 require_once($CFG->libdir
.'/gradelib.php');
532 return grade_update('mod/scorm', $scorm->course
, 'mod', 'scorm', $scorm->id
, 0, NULL, array('deleted'=>1));
535 function scorm_get_view_actions() {
536 return array('pre-view','view','view all','report');
539 function scorm_get_post_actions() {
543 function scorm_option2text($scorm) {
544 global $SCORM_POPUP_OPTIONS;
545 if (isset($scorm->popup
)) {
546 if ($scorm->popup
== 1) {
547 $optionlist = array();
548 foreach ($SCORM_POPUP_OPTIONS as $name => $option) {
549 if (isset($scorm->$name)) {
550 $optionlist[] = $name.'='.$scorm->$name;
552 $optionlist[] = $name.'=0';
555 $scorm->options
= implode(',', $optionlist);
557 $scorm->options
= '';
561 $scorm->options
= '';
567 * Implementation of the function for printing the form elements that control
568 * whether the course reset functionality affects the scorm.
569 * @param $mform form passed by reference
571 function scorm_reset_course_form_definition(&$mform) {
572 $mform->addElement('header', 'scormheader', get_string('modulenameplural', 'scorm'));
573 $mform->addElement('advcheckbox', 'reset_scorm', get_string('deleteallattempts','scorm'));
577 * Course reset form defaults.
579 function scorm_reset_course_form_defaults($course) {
580 return array('reset_scorm'=>1);
584 * Removes all grades from gradebook
585 * @param int $courseid
586 * @param string optional type
588 function scorm_reset_gradebook($courseid, $type='') {
591 $sql = "SELECT s.*, cm.idnumber as cmidnumber, s.course as courseid
592 FROM {$CFG->prefix}scorm s, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
593 WHERE m.name='scorm' AND m.id=cm.module AND cm.instance=s.id AND s.course=$courseid";
595 if ($scorms = get_records_sql($sql)) {
596 foreach ($scorms as $scorm) {
597 scorm_grade_item_update($scorm, 'reset');
603 * Actual implementation of the rest coures functionality, delete all the
604 * scorm attempts for course $data->courseid.
605 * @param $data the data submitted from the reset course.
606 * @return array status array
608 function scorm_reset_userdata($data) {
611 $componentstr = get_string('modulenameplural', 'scorm');
614 if (!empty($data->reset_scorm
)) {
615 $scormssql = "SELECT s.id
616 FROM {$CFG->prefix}scorm s
617 WHERE s.course={$data->courseid}";
619 delete_records_select('scorm_scoes_track', "scormid IN ($scormssql)");
621 // remove all grades from gradebook
622 if (empty($data->reset_gradebook_grades
)) {
623 scorm_reset_gradebook($data->courseid
);
626 $status[] = array('component'=>$componentstr, 'item'=>get_string('deleteallattempts', 'scorm'), 'error'=>false);
629 // no dates to shift here
635 * Returns all other caps used in module
637 function scorm_get_extra_capabilities() {
638 return array('moodle/site:accessallgroups');