MDL-8857
[moodle-linuxchix.git] / mod / scorm / lib.php
blob9a170a7ef7548789f84bf4c8dc68dcbbe2d5fc10
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 if (($packagedata = scorm_check_package($scorm)) != null) {
80 $scorm->pkgtype = $packagedata->pkgtype;
81 if ($packagedata->launch == 0) {
82 $scorm->launch = $packagedata->launch;
83 $scorm->datadir = $packagedata->datadir;
84 $scorm->parse = 1;
85 if (!scorm_external_link($scorm->reference)) {
86 $scorm->md5hash = md5_file($CFG->dataroot.'/'.$scorm->course.'/'.$scorm->reference);
87 } else {
88 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
89 $scorm->md5hash = md5_file($scorm->dir.$scorm->datadir.'/'.basename($scorm->reference));
91 mtrace($scorm->md5hash);
92 } else {
93 $scorm->parse = 0;
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);
121 } else {
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 return update_record('scorm', $scorm);
130 * Given an ID of an instance of this module,
131 * this function will permanently delete the instance
132 * and any data that depends on it.
134 * @param int $id Scorm instance id
135 * @return boolean
137 function scorm_delete_instance($id) {
139 global $CFG;
141 if (! $scorm = get_record('scorm', 'id', $id)) {
142 return false;
145 $result = true;
147 $scorm->dir = $CFG->dataroot.'/'.$scorm->course.'/moddata/scorm';
148 if (is_dir($scorm->dir.'/'.$scorm->id)) {
149 // Delete any dependent files
150 require_once('locallib.php');
151 scorm_delete_files($scorm->dir.'/'.$scorm->id);
154 // Delete any dependent records
155 if (! delete_records('scorm_scoes_track', 'scormid', $scorm->id)) {
156 $result = false;
158 if ($scoes = get_records('scorm_scoes','scorm',$scorm->id)) {
159 foreach ($scoes as $sco) {
160 if (! delete_records('scorm_scoes_data', 'scoid', $sco->id)) {
161 $result = false;
164 delete_records('scorm_scoes', 'scorm', $scorm->id);
165 } else {
166 $result = false;
168 if (! delete_records('scorm', 'id', $scorm->id)) {
169 $result = false;
172 /*if (! delete_records('scorm_sequencing_controlmode', 'scormid', $scorm->id)) {
173 $result = false;
175 if (! delete_records('scorm_sequencing_rolluprules', 'scormid', $scorm->id)) {
176 $result = false;
178 if (! delete_records('scorm_sequencing_rolluprule', 'scormid', $scorm->id)) {
179 $result = false;
181 if (! delete_records('scorm_sequencing_rollupruleconditions', 'scormid', $scorm->id)) {
182 $result = false;
184 if (! delete_records('scorm_sequencing_rolluprulecondition', 'scormid', $scorm->id)) {
185 $result = false;
187 if (! delete_records('scorm_sequencing_rulecondition', 'scormid', $scorm->id)) {
188 $result = false;
190 if (! delete_records('scorm_sequencing_ruleconditions', 'scormid', $scorm->id)) {
191 $result = false;
192 }*/
193 return $result;
197 * Return a small object with summary information about what a
198 * user has done with a given particular instance of this module
199 * Used for user activity reports.
201 * @param int $course Course id
202 * @param int $user User id
203 * @param int $mod
204 * @param int $scorm The scorm id
205 * @return mixed
207 function scorm_user_outline($course, $user, $mod, $scorm) {
209 $return = NULL;
211 require_once('locallib.php');
213 $return = scorm_grade_user($scorm, $user->id, true);
215 return $return;
219 * Print a detailed representation of what a user has done with
220 * a given particular instance of this module, for user activity reports.
222 * @param int $course Course id
223 * @param int $user User id
224 * @param int $mod
225 * @param int $scorm The scorm id
226 * @return boolean
228 function scorm_user_complete($course, $user, $mod, $scorm) {
229 global $CFG;
231 $liststyle = 'structlist';
232 $scormpixdir = $CFG->modpixpath.'/scorm/pix';
233 $now = time();
234 $firstmodify = $now;
235 $lastmodify = 0;
236 $sometoreport = false;
237 $report = '';
239 if ($orgs = get_records_select('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,identifier,title')) {
240 if (count($orgs) <= 1) {
241 unset($orgs);
242 $orgs[]->identifier = '';
244 $report .= '<div class="mod-scorm">'."\n";
245 foreach ($orgs as $org) {
246 $organizationsql = '';
247 $currentorg = '';
248 if (!empty($org->identifier)) {
249 $report .= '<div class="orgtitle">'.$org->title.'</div>';
250 $currentorg = $org->identifier;
251 $organizationsql = "AND organization='$currentorg'";
253 $report .= "<ul id='0' class='$liststyle'>";
254 if ($scoes = get_records_select('scorm_scoes',"scorm='$scorm->id' $organizationsql order by id ASC")){
255 $level=0;
256 $sublist=1;
257 $parents[$level]='/';
258 foreach ($scoes as $sco) {
259 if ($parents[$level]!=$sco->parent) {
260 if ($level>0 && $parents[$level-1]==$sco->parent) {
261 $report .= "\t\t</ul></li>\n";
262 $level--;
263 } else {
264 $i = $level;
265 $closelist = '';
266 while (($i > 0) && ($parents[$level] != $sco->parent)) {
267 $closelist .= "\t\t</ul></li>\n";
268 $i--;
270 if (($i == 0) && ($sco->parent != $currentorg)) {
271 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
272 $level++;
273 } else {
274 $report .= $closelist;
275 $level = $i;
277 $parents[$level]=$sco->parent;
280 $report .= "\t\t<li>";
281 $nextsco = next($scoes);
282 if (($nextsco !== false) && ($sco->parent != $nextsco->parent) && (($level==0) || (($level>0) && ($nextsco->parent == $sco->identifier)))) {
283 $sublist++;
284 } else {
285 $report .= '<img src="'.$scormpixdir.'/spacer.gif" alt="" />';
288 if ($sco->launch) {
289 require_once('locallib.php');
290 $score = '';
291 $totaltime = '';
292 if ($usertrack=scorm_get_tracks($sco->id,$user->id)) {
293 if ($usertrack->status == '') {
294 $usertrack->status = 'notattempted';
296 $strstatus = get_string($usertrack->status,'scorm');
297 $report .= "<img src='".$scormpixdir.'/'.$usertrack->status.".gif' alt='$strstatus' title='$strstatus' />";
298 if ($usertrack->timemodified != 0) {
299 if ($usertrack->timemodified > $lastmodify) {
300 $lastmodify = $usertrack->timemodified;
302 if ($usertrack->timemodified < $firstmodify) {
303 $firstmodify = $usertrack->timemodified;
306 } else {
307 if ($sco->scormtype == 'sco') {
308 $report .= '<img src="'.$scormpixdir.'/'.'notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
309 } else {
310 $report .= '<img src="'.$scormpixdir.'/'.'asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
313 $report .= "&nbsp;$sco->title $score$totaltime</li>\n";
314 if ($usertrack !== false) {
315 $sometoreport = true;
316 $report .= "\t\t\t<li><ul class='$liststyle'>\n";
317 foreach($usertrack as $element => $value) {
318 if (substr($element,0,3) == 'cmi') {
319 $report .= '<li>'.$element.' => '.$value.'</li>';
322 $report .= "\t\t\t</ul></li>\n";
324 } else {
325 $report .= "&nbsp;$sco->title</li>\n";
328 for ($i=0;$i<$level;$i++) {
329 $report .= "\t\t</ul></li>\n";
332 $report .= "\t</ul><br />\n";
334 $report .= "</div>\n";
336 if ($sometoreport) {
337 if ($firstmodify < $now) {
338 $timeago = format_time($now - $firstmodify);
339 echo get_string('firstaccess','scorm').': '.userdate($firstmodify).' ('.$timeago.")<br />\n";
341 if ($lastmodify > 0) {
342 $timeago = format_time($now - $lastmodify);
343 echo get_string('lastaccess','scorm').': '.userdate($lastmodify).' ('.$timeago.")<br />\n";
345 echo get_string('report','scorm').":<br />\n";
346 echo $report;
347 } else {
348 print_string('noactivity','scorm');
351 return true;
355 * Given a list of logs, assumed to be those since the last login
356 * this function prints a short list of changes related to this module
357 * If isteacher is true then perhaps additional information is printed.
358 * This function is called from course/lib.php: print_recent_activity()
360 * @param reference $logs Logs reference
361 * @param boolean $isteacher
362 * @return boolean
364 function scorm_print_recent_activity(&$logs, $isteacher=false) {
365 return false; // True if anything was printed, otherwise false
369 * Function to be run periodically according to the moodle cron
370 * This function searches for things that need to be done, such
371 * as sending out mail, toggling flags etc ...
373 * @return boolean
375 function scorm_cron () {
377 global $CFG;
379 require_once('locallib.php');
381 $sitetimezone = $CFG->timezone;
382 /// Now see if there are any digest mails waiting to be sent, and if we should send them
383 if (!isset($CFG->scorm_updatetimelast)) { // To catch the first time
384 set_config('scorm_updatetimelast', 0);
387 $timenow = time();
388 $updatetime = usergetmidnight($timenow, $sitetimezone) + ($CFG->scorm_updatetime * 3600);
390 if ($CFG->scorm_updatetimelast < $updatetime and $timenow > $updatetime) {
392 set_config('scorm_updatetimelast', $timenow);
394 mtrace('Updating scorm packages which require daily update');//"estamos actualizando"
396 $scormsupdate = get_records('scorm','updatefreq',UPDATE_EVERYDAY);
397 if (!empty($scormsupdate)) {
398 foreach($scormsupdate as $scormupdate) {
399 $scormupdate->instance = $scormupdate->id;
400 $id = scorm_update_instance($scormupdate);
405 return true;
409 * Given a scorm id return all the grades of that activity
411 * @param int $scormid Scorm instance id
412 * @return mixed
414 function scorm_grades($scormid) {
416 global $CFG;
418 if (!$scorm = get_record('scorm', 'id', $scormid)) {
419 return NULL;
422 if (($scorm->grademethod % 10) == 0) { // GRADESCOES
423 if (!$return->maxgrade = count_records_select('scorm_scoes',"scorm='$scormid' AND launch<>''")) {
424 return NULL;
426 } else {
427 $return->maxgrade = $scorm->maxgrade;
430 $return->grades = NULL;
431 if ($scousers=get_records_select('scorm_scoes_track', "scormid='$scormid' GROUP BY userid", "", "userid,null")) {
432 require_once('locallib.php');
433 foreach ($scousers as $scouser) {
434 $return->grades[$scouser->userid] = scorm_grade_user($scorm, $scouser->userid);
437 return $return;
440 function scorm_get_view_actions() {
441 return array('pre-view','view','view all','report');
444 function scorm_get_post_actions() {
445 return array();
448 function scorm_option2text($scorm) {
449 global $SCORM_POPUP_OPTIONS;
450 if (isset($scorm->popup)) {
451 if ($scorm->popup == 1) {
452 $optionlist = array();
453 foreach ($SCORM_POPUP_OPTIONS as $name => $option) {
454 if (isset($scorm->$name)) {
455 $optionlist[] = $name.'='.$scorm->$name;
456 } else {
457 $optionlist[] = $name.'=0';
460 $scorm->options = implode(',', $optionlist);
461 } else {
462 $scorm->options = '';
464 } else {
465 $scorm->popup = 0;
466 $scorm->options = '';
468 return $scorm;