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');
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
;
85 if (!scorm_external_link($scorm->reference
)) {
86 $scorm->md5hash
= md5_file($CFG->dataroot
.'/'.$scorm->course
.'/'.$scorm->reference
);
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
);
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 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
137 function scorm_delete_instance($id) {
141 if (! $scorm = get_record('scorm', 'id', $id)) {
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
)) {
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
)) {
164 delete_records('scorm_scoes', 'scorm', $scorm->id
);
168 if (! delete_records('scorm', 'id', $scorm->id
)) {
172 /*if (! delete_records('scorm_sequencing_controlmode', 'scormid', $scorm->id)) {
175 if (! delete_records('scorm_sequencing_rolluprules', 'scormid', $scorm->id)) {
178 if (! delete_records('scorm_sequencing_rolluprule', 'scormid', $scorm->id)) {
181 if (! delete_records('scorm_sequencing_rollupruleconditions', 'scormid', $scorm->id)) {
184 if (! delete_records('scorm_sequencing_rolluprulecondition', 'scormid', $scorm->id)) {
187 if (! delete_records('scorm_sequencing_rulecondition', 'scormid', $scorm->id)) {
190 if (! delete_records('scorm_sequencing_ruleconditions', 'scormid', $scorm->id)) {
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
204 * @param int $scorm The scorm id
207 function scorm_user_outline($course, $user, $mod, $scorm) {
211 require_once('locallib.php');
213 $return = scorm_grade_user($scorm, $user->id
, true);
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
225 * @param int $scorm The scorm id
228 function scorm_user_complete($course, $user, $mod, $scorm) {
231 $liststyle = 'structlist';
232 $scormpixdir = $CFG->modpixpath
.'/scorm/pix';
236 $sometoreport = false;
239 if ($orgs = get_records_select('scorm_scoes',"scorm='$scorm->id' AND organization='' AND launch=''",'id','id,identifier,title')) {
240 if (count($orgs) <= 1) {
242 $orgs[]->identifier
= '';
244 $report .= '<div class="mod-scorm">'."\n";
245 foreach ($orgs as $org) {
246 $organizationsql = '';
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")){
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";
266 while (($i > 0) && ($parents[$level] != $sco->parent
)) {
267 $closelist .= "\t\t</ul></li>\n";
270 if (($i == 0) && ($sco->parent
!= $currentorg)) {
271 $report .= "\t\t<li><ul id='$sublist' class='$liststyle'>\n";
274 $report .= $closelist;
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
)))) {
285 $report .= '<img src="'.$scormpixdir.'/spacer.gif" alt="" />';
289 require_once('locallib.php');
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
;
307 if ($sco->scormtype
== 'sco') {
308 $report .= '<img src="'.$scormpixdir.'/'.'notattempted.gif" alt="'.get_string('notattempted','scorm').'" title="'.get_string('notattempted','scorm').'" />';
310 $report .= '<img src="'.$scormpixdir.'/'.'asset.gif" alt="'.get_string('asset','scorm').'" title="'.get_string('asset','scorm').'" />';
313 $report .= " $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";
325 $report .= " $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";
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";
348 print_string('noactivity','scorm');
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
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 ...
375 function scorm_cron () {
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);
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);
409 * Given a scorm id return all the grades of that activity
411 * @param int $scormid Scorm instance id
414 function scorm_grades($scormid) {
418 if (!$scorm = get_record('scorm', 'id', $scormid)) {
422 if (($scorm->grademethod %
10) == 0) { // GRADESCOES
423 if (!$return->maxgrade
= count_records_select('scorm_scoes',"scorm='$scormid' AND launch<>''")) {
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
);
440 function scorm_get_view_actions() {
441 return array('pre-view','view','view all','report');
444 function scorm_get_post_actions() {
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;
457 $optionlist[] = $name.'=0';
460 $scorm->options
= implode(',', $optionlist);
462 $scorm->options
= '';
466 $scorm->options
= '';