Language bug fix [#1542]
[moodle.git] / mod / scorm / lib.php
CommitLineData
98ca59f6 1<?PHP // $Id$
2
3/// Library of functions and constants for module scorm
4/// (replace scorm with the name of your module and delete this line)
5
6
c5163bfd 7$SCORM_FRAME_SIZE = 140;
98ca59f6 8
9function scorm_add_instance($scorm) {
10/// Given an object containing all the necessary data,
11/// (defined by the form in mod.html) this function
12/// will create a new instance and return the id number
13/// of the new instance.
14
15 $scorm->timemodified = time();
16
17 # May have to add extra stuff in here #
18
19 return insert_record("scorm", $scorm);
20}
21
22
23function scorm_update_instance($scorm) {
24/// Given an object containing all the necessary data,
25/// (defined by the form in mod.html) this function
26/// will update an existing instance with new data.
27
28 $scorm->timemodified = time();
29 $scorm->id = $scorm->instance;
30
31 # May have to add extra stuff in here #
32
33 return update_record("scorm", $scorm);
34}
35
36
37function scorm_delete_instance($id) {
38/// Given an ID of an instance of this module,
39/// this function will permanently delete the instance
40/// and any data that depends on it.
41
42 require('../config.php');
43
44 if (! $scorm = get_record("scorm", "id", "$id")) {
45 return false;
46 }
47
48 $result = true;
49
50 # Delete any dependent files #
51 scorm_delete_files($CFG->dataroot."/".$scorm->course."/moddata/scorm".$scorm->datadir);
52
53 # Delete any dependent records here #
54 if (! delete_records("scorm_sco_users", "scormid", "$scorm->id")) {
55 $result = false;
56 }
57 if (! delete_records("scorm_scoes", "scorm", "$scorm->id")) {
58 $result = false;
59 }
60 if (! delete_records("scorm", "id", "$scorm->id")) {
61 $result = false;
62 }
63
64
65 return $result;
66}
67
68function scorm_user_outline($course, $user, $mod, $scorm) {
69/// Return a small object with summary information about what a
70/// user has done with a given particular instance of this module
71/// Used for user activity reports.
72/// $return->time = the time they did it
73/// $return->info = a short text description
74
75 return $return;
76}
77
78function scorm_user_complete($course, $user, $mod, $scorm) {
79/// Print a detailed representation of what a user has done with
80/// a given particular instance of this module, for user activity reports.
81
82 return true;
83}
84
85function scorm_print_recent_activity(&$logs, $isteacher=false) {
86/// Given a list of logs, assumed to be those since the last login
87/// this function prints a short list of changes related to this module
88/// If isteacher is true then perhaps additional information is printed.
89/// This function is called from course/lib.php: print_recent_activity()
90
91 global $CFG, $COURSE_TEACHER_COLOR;
92
e1e108d4 93 $content = NULL;
94
98ca59f6 95 return $content; // True if anything was printed, otherwise false
96}
97
98function scorm_cron () {
99/// Function to be run periodically according to the moodle cron
100/// This function searches for things that need to be done, such
101/// as sending out mail, toggling flags etc ...
102
103 global $CFG;
104
105 return true;
106}
107
108function scorm_grades($scormid) {
109/// Must return an array of grades for a given instance of this module,
110/// indexed by user. It also returns a maximum allowed grade.
111
112 global $CFG;
113
114 $return->grades = NULL;
115 if ($sco_users=get_records_select("scorm_sco_users", "scormid='$scormid' GROUP BY userid")) {
116 foreach ($sco_users as $sco_user) {
117 $user_data=get_records_select("scorm_sco_users","scormid='$scormid' AND userid='$sco_user->userid'");
118 $scores->completed=0;
119 $scores->browsed=0;
120 $scores->incomplete=0;
121 $scores->failed=0;
122 $scores->notattempted=0;
123 $result="";
124 $data = current($user_data);
125 foreach ($user_data as $data) {
126 if ($data->cmi_core_lesson_status=="passed")
127 $scores->completed++;
128 else
129 $scores->{scorm_remove_spaces($data->cmi_core_lesson_status)}++;
130
131 }
132 if ($scores->completed)
133 $result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/completed.gif\" alt=\"".get_string("completed","scorm")."\" title=\"".get_string("completed","scorm")."\"> $scores->completed ";
134 if ($scores->incomplete)
135 $result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/incomplete.gif\" alt=\"".get_string("incomplete","scorm")."\" title=\"".get_string("incomplete","scorm")."\"> $scores->incomplete ";
136 if ($scores->failed)
137 $result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/failed.gif\" alt=\"".get_string("failed","scorm")."\" title=\"".get_string("failed","scorm")."\"> $scores->failed ";
138 if ($scores->browsed)
139 $result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/browsed.gif\" alt=\"".get_string("browsed","scorm")."\" title=\"".get_string("browsed","scorm")."\"> $scores->browsed ";
140 if ($scores->notattempted)
141 $result.="<img src=\"$CFG->wwwroot/mod/scorm/pix/notattempted.gif\" alt=\"".get_string("notattempted","scorm")."\" title=\"".get_string("notattempted","scorm")."\"> $scores->notattempted ";
142
143 $return->grades[$sco_user->userid]=$result;
144 }
145
146 }
147
148 $return->maxgrade = count_records_select("scorm_scoes","scorm='$scormid' AND launch<>''");
149
150 return $return;
151}
152
153
154//////////////////////////////////////////////////////////////////////////////////////
155/// Any other scorm functions go here. Each of them must have a name that
156/// starts with scorm_
157
158
159function scorm_randstring($len = "8")
160{
161 $rstring = NULL;
162 for($i=0; $i<$len; $i++) {
163 $char = chr(rand(48,122));
164 while (!ereg("[a-zA-Z0-9]", $char)){
165 if($char == $lchar) continue;
166 $char = chr(rand(48,90));
167 }
168 $rstring .= $char;
169 $lchar = $char;
170 }
171 return $rstring;
172}
173
98ca59f6 174
175function scorm_datadir($strPath, $existingdir="", $prefix = "SCORM")
176{
e23ef951 177 global $CFG;
178
98ca59f6 179 if (($existingdir!="") && (is_dir($strPath.$existingdir)))
180 return $strPath.$existingdir;
181
182 if (is_dir($strPath)) {
183 do {
184 $datadir="/".$prefix.scorm_randstring();
185 } while (file_exists($strPath.$datadir));
c5163bfd 186 mkdir($strPath.$datadir, $CFG->directorypermissions);
e23ef951 187 @chmod($strPath.$datadir, $CFG->directorypermissions); // Just in case mkdir didn't do it
98ca59f6 188 return $strPath.$datadir;
189 } else {
190 return false;
191 }
192}
193
194function scorm_validate($manifest)
195{
196 if (is_file ($manifest)) {
197 if (file_exists($manifest))
198 {
199 return "regular";
200 }
201 } else {
202 return "nomanifest";
203 }
204}
205
206function scorm_delete_files($directory)
207{
208 if (is_dir($directory))
209 {
210 $handle=opendir($directory);
211 while (($file = readdir($handle)) != '')
212 {
213 if ($file != "." && $file != "..")
214 {
215 if (!is_dir($directory."/".$file))
216 unlink($directory."/".$file);
217 else
218 scorm_delete_files($directory."/".$file);
219 }
220 }
221 rmdir($directory);
222 }
223}
224
225function scorm_startElement($parser, $name, $attrs) {
226 global $manifest,$i,$resources,$parent,$level;
227 if ($name == "ITEM") {
228 $i++;
229 $manifest[$i]["identifier"] = $attrs["IDENTIFIER"];
230 $manifest[$i]["identifierref"] = $attrs["IDENTIFIERREF"];
231 $manifest[$i]["isvisible"] = $attrs["ISVISIBLE"];
232 $manifest[$i]["parent"] = $parent[$level];
233 $level++;
234 $parent[$level] = $attrs["IDENTIFIER"];
235 }
236 if ($name == "RESOURCE") {
237 $resources[$attrs["IDENTIFIER"]]["href"]=$attrs["HREF"];
238 $resources[$attrs["IDENTIFIER"]]["type"]=$attrs["ADLCP:SCORMTYPE"];
239 }
240}
241
242function scorm_endElement($parser, $name) {
243 global $manifest,$i,$level,$datacontent,$navigation;
244 if ($name == "ITEM") {
245 $level--;
246 }
247 if ($name == "TITLE" && $level>0)
248 $manifest[$i]["title"] = $datacontent;
249 if ($name == "ADLCP:HIDERTSUI")
250 $manifest[$i][$datacontent] = 1;
251}
252
253function scorm_characterData($parser, $data) {
254 global $datacontent;
255 $datacontent = $data;
256}
257
258function scorm_parse($basedir,$file,$scorm_id) {
259 global $manifest,$i,$resources,$parent,$level;
260 $datacontent = "";
261 $manifest[][] = "";
262 $resources[] = "";
263 $i = 0;
264 $level = 0;
265 $parent[$level] = "/";
266
267 $xml_parser = xml_parser_create();
268 // use case-folding so we are sure to find the tag in $map_array
269 xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
270 xml_set_element_handler($xml_parser, "scorm_startElement", "scorm_endElement");
271 xml_set_character_data_handler($xml_parser, "scorm_characterData");
272 if (!($fp = fopen($basedir.$file, "r"))) {
273 die("could not open XML input");
274 }
275
276 while ($data = fread($fp, 4096)) {
277 if (!xml_parse($xml_parser, $data, feof($fp))) {
278 die(sprintf("XML error: %s at line %d",
279 xml_error_string(xml_get_error_code($xml_parser)),
280 xml_get_current_line_number($xml_parser)));
281 }
282 }
283 xml_parser_free($xml_parser);
284 $launch = 0;
285
286 $sco->scorm = $scorm_id;
287 delete_records("scorm_scoes","scorm",$scorm_id);
288 delete_records("scorm_sco_users","scormid",$scorm_id);
289 for ($j=1; $j<=$i; $j++) {
290 $sco->identifier = $manifest[$j]["identifier"];
291 $sco->parent = $manifest[$j]["parent"];
292 $sco->title = $manifest[$j]["title"];
293 $sco->launch = $resources[$manifest[$j]["identifierref"]]["href"];
294 $sco->type = $resources[$manifest[$j]["identifierref"]]["type"];
295 $sco->previous = $manifest[$j]["previous"];
296 $sco->next = $manifest[$j]["continue"];
297 if (scorm_remove_spaces($manifest[$j]["isvisible"]) != "false")
298 $id = insert_record("scorm_scoes",$sco);
299 if ($launch==0 && $sco->launch)
300 $launch = $id;
301 }
302 return $launch;
303}
304
305function scorm_get_scoes_records($sco_user) {
306/// Gets all info required to display the table of scorm results
307/// for report.php
308 global $CFG;
309
310 return get_records_sql("SELECT su.*, u.firstname, u.lastname, u.picture
311 FROM {$CFG->prefix}scorm_sco_users su,
312 {$CFG->prefix}user u
313 WHERE su.scormid = '$sco_user->scormid'
314 AND su.userid = u.id
315 AND su.userid = $sco_user->userid
316 ORDER BY scoid");
317}
318
319function scorm_remove_spaces($sourcestr) {
320// Remove blank space from a string
321 $newstr="";
322 for( $i=0; $i<strlen($sourcestr); $i++) {
323 if ($sourcestr[$i]!=' ')
324 $newstr .=$sourcestr[$i];
325 }
326 return $newstr;
327}
328
329function scorm_string_round($stringa) {
330// Crop a string to $len character and set an anchor title to the full string
331 $len=11;
332 if ( strlen($stringa)>$len ) {
333 return "<A name=\"\" title=\"$stringa\">".substr($stringa,0,$len-4)."...".substr($stringa,strlen($stringa)-1,1)."</A>";
334 } else
335 return $stringa;
336}
337
338function scorm_external_link($link) {
339// check if a link is external
340 $result = false;
341 $link = strtolower($link);
342 if (substr($link,0,7) == "http://")
343 $result = true;
344 else if (substr($link,0,8) == "https://")
345 $result = true;
346 else if (substr($link,0,4) == "www.")
347 $result = true;
348 else if (substr($link,0,7) == "rstp://")
349 $result = true;
350 else if (substr($link,0,6) == "rtp://")
351 $result = true;
352 else if (substr($link,0,6) == "ftp://")
353 $result = true;
354 else if (substr($link,0,9) == "gopher://")
355 $result = true;
356 return $result;
357}
358?>