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
);
62 error(get_string('badpackage','scorm'));
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.
71 * @param mixed $scorm Form data
74 function scorm_update_instance($scorm) {
77 require_once('locallib.php');
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
;
86 if (!scorm_external_link($scorm->reference
)) {
87 $scorm->md5hash
= md5_file($CFG->dataroot
.'/'.$scorm->course
.'/'.$scorm->reference
);
89 $scorm->dir
= $CFG->dataroot
.'/'.$scorm->course
.'/moddata/scorm';
90 $scorm->md5hash
= md5_file($scorm->dir
.$scorm->datadir
.'/'.basename($scorm->reference
));
95 $scorm->timemodified
= time();
96 $scorm->id
= $scorm->instance
;
98 $scorm = scorm_option2text($scorm);
99 $scorm->width
= str_replace('%','',$scorm->width
);
100 $scorm->height
= str_replace('%','',$scorm->height
);
102 if (!isset($scorm->whatgrade
)) {
103 $scorm->whatgrade
= 0;
105 $scorm->grademethod
= ($scorm->whatgrade
* 10) +
$scorm->grademethod
;
107 // Check if scorm manifest needs to be reparsed
108 if ($scorm->parse
== 1) {
109 $scorm->dir
= $CFG->dataroot
.'/'.$scorm->course
.'/moddata/scorm';
110 if (is_dir($scorm->dir
.'/'.$scorm->id
)) {
111 scorm_delete_files($scorm->dir
.'/'.$scorm->id
);
113 if (isset($scorm->datadir
) && ($scorm->datadir
!= $scorm->id
) &&
114 (scorm_external_link($scorm->reference
) ||
((basename($scorm->reference
) != 'imsmanifest.xml') && ($scorm->reference
[0] != '#')))) {
115 rename($scorm->dir
.$scorm->datadir
,$scorm->dir
.'/'.$scorm->id
);
118 $scorm->launch
= scorm_parse($scorm);
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
124 return update_record('scorm', $scorm);
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.
132 * @param int $id Scorm instance id
135 function scorm_delete_instance($id) {
139 if (! $scorm = get_record('scorm', 'id', $id)) {
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
);
152 // Delete any dependent records
153 if (! delete_records('scorm_scoes_track', 'scormid', $scorm->id
)) {
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
)) {
162 delete_records('scorm_scoes', 'scorm', $scorm->id
);
166 if (! delete_records('scorm', 'id', $scorm->id
)) {
170 /*if (! delete_records('scorm_sequencing_controlmode', 'scormid', $scorm->id)) {
173 if (! delete_records('scorm_sequencing_rolluprules', 'scormid', $scorm->id)) {
176 if (! delete_records('scorm_sequencing_rolluprule', 'scormid', $scorm->id)) {
179 if (! delete_records('scorm_sequencing_rollupruleconditions', 'scormid', $scorm->id)) {
182 if (! delete_records('scorm_sequencing_rolluprulecondition', 'scormid', $scorm->id)) {
185 if (! delete_records('scorm_sequencing_rulecondition', 'scormid', $scorm->id)) {
188 if (! delete_records('scorm_sequencing_ruleconditions', 'scormid', $scorm->id)) {
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.
199 * @param int $course Course id
200 * @param int $user User id
202 * @param int $scorm The scorm id
205 function scorm_user_outline($course, $user, $mod, $scorm) {
209 require_once('locallib.php');
211 $return = scorm_grade_user($scorm, $user->id
, true);
217 * Print a detailed representation of what a user has done with
218 * a given particular instance of this module, for user activity reports.
220 * @param int $course Course id
221 * @param int $user User id
223 * @param int $scorm The scorm id
226 function scorm_user_complete($course, $user, $mod, $scorm) {
229 $liststyle = 'structlist';
230 $scormpixdir = $CFG->modpixpath
.'/scorm/pix';
234 $sometoreport = false;
237 if ($orgs = get_records_select('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,identifier,title')) {
238 if (count($orgs) <= 1) {
240 $orgs[]->identifier
= '';
242 $report .= '<div class="mod-scorm">'."\n";
243 foreach ($orgs as $org) {
244 $organizationsql = '';
246 if (!empty($org->identifier
)) {
247 $report .= '<div class="orgtitle">'.$org->title
.'</div>';
248 $currentorg = $org->identifier
;
249 $organizationsql = "AND organization='$currentorg'";
251 $report .= "<ul id='0' class='$liststyle'>";
252 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
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";
264 while (($i > 0) && ($parents[$level] != $sco->parent
)) {
265 $closelist .= "\t\t</ul></li>\n";
268 if (($i == 0) && ($sco->parent
!= $currentorg)) {
269 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
272 $report .= $closelist;
275 $parents[$level]=$sco->parent
;
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
)))) {
283 $report .= '<img src="'.$scormpixdir.'/spacer.gif" alt="" />';
287 require_once('locallib.php');
290 if ($usertrack=scorm_get_tracks($sco->id
,$user->id
)) {
291 if ($usertrack->status
== '') {
292 $usertrack->status
= 'notattempted';
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
;
300 if ($usertrack->timemodified
< $firstmodify) {
301 $firstmodify = $usertrack->timemodified
;
305 if ($sco->scormtype
== 'sco') {
306 $report .= '<img src="'.$scormpixdir.'/'.'notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
308 $report .= '<img src="'.$scormpixdir.'/'.'asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
311 $report .= " $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>';
320 $report .= "\t\t\t</ul></li>\n";
323 $report .= " $sco->title</li>\n";
326 for ($i=0;$i<$level;$i++
) {
327 $report .= "\t\t</ul></li>\n";
330 $report .= "\t</ul><br />\n";
332 $report .= "</div>\n";
335 if ($firstmodify < $now) {
336 $timeago = format_time($now - $firstmodify);
337 echo get_string('firstaccess','scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
339 if ($lastmodify > 0) {
340 $timeago = format_time($now - $lastmodify);
341 echo get_string('lastaccess','scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
343 echo get_string('report','scorm').":<br />\n";
346 print_string('noactivity','scorm');
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()
358 * @param reference $logs Logs reference
359 * @param boolean $isteacher
362 function scorm_print_recent_activity(&$logs, $isteacher=false) {
363 return false; // True if anything was printed, otherwise false
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 * Given a scorm id return all the grades of that activity
409 * @param int $scormid Scorm instance id
412 function scorm_grades($scormid) {
416 if (!$scorm = get_record('scorm', 'id', $scormid)) {
420 if (($scorm->grademethod %
10) == 0) { // GRADESCOES
421 if (!$return->maxgrade
= count_records_select('scorm_scoes',"scorm='$scormid' AND launch<>''")) {
425 $return->maxgrade
= $scorm->maxgrade
;
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) {
432 $return->grades
[$scouser->userid
] = scorm_grade_user($scorm, $scouser->userid
);
438 function scorm_get_view_actions() {
439 return array('pre-view','view','view all','report');
442 function scorm_get_post_actions() {
446 function scorm_option2text($scorm) {
447 global $SCORM_POPUP_OPTIONS;
448 if (isset($scorm->popup
)) {
449 if ($scorm->popup
== 1) {
450 $optionlist = array();
451 foreach ($SCORM_POPUP_OPTIONS as $name => $option) {
452 if (isset($scorm->$name)) {
453 $optionlist[] = $name.'='.$scorm->$name;
455 $optionlist[] = $name.'=0';
458 $scorm->options
= implode(',', $optionlist);
460 $scorm->options
= '';
464 $scorm->options
= '';