MDL-10240:
[moodle-linuxchix.git] / mod / scorm / lib.php
blobe53e8a6b6832904de0a49e7d3434981c99c2d5f8
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 return $id;
61 } else {
62 error(get_string('badpackage','scorm'));
66 /**
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
72 * @return int
74 function scorm_update_instance($scorm) {
75 global $CFG;
77 require_once('locallib.php');
79 $scorm->parse = 0;
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;
85 $scorm->parse = 1;
86 if (!scorm_external_link($scorm->reference)) {
87 $scorm->md5hash = md5_file($CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference);
88 } else {
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);
119 } else {
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
133 * @return boolean
135 function scorm_delete_instance($id) {
137 global $CFG;
139 if (! $scorm = get_record('scorm', 'id', $id)) {
140 return false;
143 $result = true;
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)) {
154 $result = false;
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)) {
159 $result = false;
162 delete_records('scorm_scoes', 'scorm', $scorm->id);
163 } else {
164 $result = false;
166 if (! delete_records('scorm', 'id', $scorm->id)) {
167 $result = false;
170 /*if (! delete_records('scorm_sequencing_controlmode', 'scormid', $scorm->id)) {
171 $result = false;
173 if (! delete_records('scorm_sequencing_rolluprules', 'scormid', $scorm->id)) {
174 $result = false;
176 if (! delete_records('scorm_sequencing_rolluprule', 'scormid', $scorm->id)) {
177 $result = false;
179 if (! delete_records('scorm_sequencing_rollupruleconditions', 'scormid', $scorm->id)) {
180 $result = false;
182 if (! delete_records('scorm_sequencing_rolluprulecondition', 'scormid', $scorm->id)) {
183 $result = false;
185 if (! delete_records('scorm_sequencing_rulecondition', 'scormid', $scorm->id)) {
186 $result = false;
188 if (! delete_records('scorm_sequencing_ruleconditions', 'scormid', $scorm->id)) {
189 $result = false;
190 }*/
191 return $result;
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
201 * @param int $mod
202 * @param int $scorm The scorm id
203 * @return mixed
205 function scorm_user_outline($course, $user, $mod, $scorm) {
207 $return = NULL;
209 require_once('locallib.php');
211 $return = scorm_grade_user($scorm, $user->id, true);
213 return $return;
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
222 * @param int $mod
223 * @param int $scorm The scorm id
224 * @return boolean
226 function scorm_user_complete($course, $user, $mod, $scorm) {
227 global $CFG;
229 $liststyle = 'structlist';
230 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
231 $now = time();
232 $firstmodify = $now;
233 $lastmodify = 0;
234 $sometoreport = false;
235 $report = '';
237 if ($orgs = get_records_select('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,identifier,title')) {
238 if (count($orgs) <= 1) {
239 unset($orgs);
240 $orgs[]->identifier = '';
242 $report .= '<div class="mod-scorm">'."\n";
243 foreach ($orgs as $org) {
244 $organizationsql = '';
245 $currentorg = '';
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")){
253 $level=0;
254 $sublist=1;
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";
260 $level--;
261 } else {
262 $i = $level;
263 $closelist = '';
264 while (($i > 0) && ($parents[$level] != $sco->parent)) {
265 $closelist .= "\t\t</ul></li>\n";
266 $i--;
268 if (($i == 0) && ($sco->parent != $currentorg)) {
269 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
270 $level++;
271 } else {
272 $report .= $closelist;
273 $level = $i;
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)))) {
281 $sublist++;
282 } else {
283 $report .= '<img src="'.$scormpixdir.'/spacer.gif" alt="" />';
286 if ($sco->launch) {
287 require_once('locallib.php');
288 $score = '';
289 $totaltime = '';
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;
304 } else {
305 if ($sco->scormtype == 'sco') {
306 $report .= '<img src="'.$scormpixdir.'/'.'notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
307 } else {
308 $report .= '<img src="'.$scormpixdir.'/'.'asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
311 $report .= "&nbsp;$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";
322 } else {
323 $report .= "&nbsp;$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";
334 if ($sometoreport) {
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";
344 echo $report;
345 } else {
346 print_string('noactivity','scorm');
349 return true;
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
360 * @return boolean
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 ...
371 * @return boolean
373 function scorm_cron () {
375 global $CFG;
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);
385 $timenow = time();
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);
403 return true;
407 * Given a scorm id return all the grades of that activity
409 * @param int $scormid Scorm instance id
410 * @return mixed
412 function scorm_grades($scormid) {
414 global $CFG;
416 if (!$scorm = get_record('scorm', 'id', $scormid)) {
417 return NULL;
420 if (($scorm->grademethod % 10) == 0) { // GRADESCOES
421 if (!$return->maxgrade = count_records_select('scorm_scoes',"scorm='$scormid' AND launch<>''")) {
422 return NULL;
424 } else {
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);
435 return $return;
438 function scorm_get_view_actions() {
439 return array('pre-view','view','view all','report');
442 function scorm_get_post_actions() {
443 return array();
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;
454 } else {
455 $optionlist[] = $name.'=0';
458 $scorm->options = implode(',', $optionlist);
459 } else {
460 $scorm->options = '';
462 } else {
463 $scorm->popup = 0;
464 $scorm->options = '';
466 return $scorm;