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 error(get_string('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
)) {
89 $scorm->md5hash
= md5_file($CFG->dataroot
.'/'.$scorm->course
.'/'.$scorm->reference
);
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));
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
141 function scorm_delete_instance($id) {
145 if (! $scorm = get_record('scorm', 'id', $id)) {
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
)) {
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
)) {
168 delete_records('scorm_scoes', 'scorm', $scorm->id
);
172 if (! delete_records('scorm', 'id', $scorm->id
)) {
176 /*if (! delete_records('scorm_sequencing_controlmode', 'scormid', $scorm->id)) {
179 if (! delete_records('scorm_sequencing_rolluprules', 'scormid', $scorm->id)) {
182 if (! delete_records('scorm_sequencing_rolluprule', 'scormid', $scorm->id)) {
185 if (! delete_records('scorm_sequencing_rollupruleconditions', 'scormid', $scorm->id)) {
188 if (! delete_records('scorm_sequencing_rolluprulecondition', 'scormid', $scorm->id)) {
191 if (! delete_records('scorm_sequencing_rulecondition', 'scormid', $scorm->id)) {
194 if (! delete_records('scorm_sequencing_ruleconditions', 'scormid', $scorm->id)) {
198 scorm_grade_item_delete(stripslashes_recursive($scorm));
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
211 * @param int $scorm The scorm id
214 function scorm_user_outline($course, $user, $mod, $scorm) {
216 require_once('locallib.php');
218 $return = scorm_grade_user($scorm, $user->id
, true);
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
230 * @param int $scorm The scorm id
233 function scorm_user_complete($course, $user, $mod, $scorm) {
236 $liststyle = 'structlist';
237 $scormpixdir = $CFG->modpixpath
.'/scorm/pix';
241 $sometoreport = false;
244 if ($orgs = get_records_select('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,identifier,title')) {
245 if (count($orgs) <= 1) {
247 $orgs[]->identifier
= '';
249 $report .= '<div class="mod-scorm">'."\n";
250 foreach ($orgs as $org) {
251 $organizationsql = '';
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")){
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";
271 while (($i > 0) && ($parents[$level] != $sco->parent
)) {
272 $closelist .= "\t\t</ul></li>\n";
275 if (($i == 0) && ($sco->parent
!= $currentorg)) {
276 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
279 $report .= $closelist;
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
)))) {
290 $report .= '<img src="'.$scormpixdir.'/spacer.gif" alt="" />';
294 require_once('locallib.php');
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
;
312 if ($sco->scormtype
== 'sco') {
313 $report .= '<img src="'.$scormpixdir.'/'.'notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
315 $report .= '<img src="'.$scormpixdir.'/'.'asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
318 $report .= " $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";
330 $report .= " $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";
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";
353 print_string('noactivity','scorm');
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
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 ...
380 function scorm_cron () {
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);
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);
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) {
422 require_once('locallib.php');
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
);
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);
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) {
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);
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);
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) {
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;
509 $params['gradetype'] = GRADE_TYPE_NONE
;
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) {
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() {
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;
550 $optionlist[] = $name.'=0';
553 $scorm->options
= implode(',', $optionlist);
555 $scorm->options
= '';
559 $scorm->options
= '';