Incorrect variable name used for parameter.
[moodle-linuxchix.git] / mod / lesson / lib.php
blob614719df5bec4e4776517957f3e4b22085b361f4
1 <?PHP // $Id$
2 // modified by mnielsen @ CDC
3 /// Update: The lib.php now contains only the functions that are
4 /// used outside of the lesson module. All functions (I hope) that are only local
5 /// are now in locallib.php.
7 /// Library of functions and constants for module lesson
9 define("LESSON_MAX_EVENT_LENGTH", "432000"); // 5 days maximum
11 /// (replace lesson with the name of your module and delete this line)
13 /*******************************************************************/
14 function lesson_add_instance($lesson) {
15 /// Given an object containing all the necessary data,
16 /// (defined by the form in mod.html) this function
17 /// will create a new instance and return the id number
18 /// of the new instance.
19 global $SESSION;
21 $lesson->timemodified = time();
23 $lesson->available = make_timestamp($lesson->availableyear,
24 $lesson->availablemonth, $lesson->availableday, $lesson->availablehour,
25 $lesson->availableminute);
27 $lesson->deadline = make_timestamp($lesson->deadlineyear,
28 $lesson->deadlinemonth, $lesson->deadlineday, $lesson->deadlinehour,
29 $lesson->deadlineminute);
31 /// CDC-FLAG ///
32 if (!empty($lesson->password)) {
33 $lesson->password = md5($lesson->password);
34 } else {
35 unset($lesson->password);
37 /// CDC-FLAG ///
38 if (!$lesson->id = insert_record("lesson", $lesson)) {
39 return false; // bad
42 if ($lesson->lessondefault) {
43 $lessondefault = $lesson;
44 unset($lessondefault->lessondefault);
45 unset($lessondefault->name);
46 unset($lessondefault->timemodified);
47 unset($lessondefault->available);
48 unset($lessondefault->deadline);
49 if ($lessondefault->id = get_field("lesson_default", "id", "course", $lessondefault->course)) {
50 update_record("lesson_default", $lessondefault);
51 } else {
52 insert_record("lesson_default", $lessondefault);
54 } else {
55 unset($lesson->lessondefault);
58 // got this code from quiz, thanks quiz!!!
59 delete_records('event', 'modulename', 'lesson', 'instance', $lesson->id); // Just in case
61 $event = new stdClass;
62 $event->name = $lesson->name;
63 $event->description = $lesson->name;
64 $event->courseid = $lesson->course;
65 $event->groupid = 0;
66 $event->userid = 0;
67 $event->modulename = 'lesson';
68 $event->instance = $lesson->id;
69 $event->eventtype = 'open';
70 $event->timestart = $lesson->available;
71 $event->visible = instance_is_visible('lesson', $lesson);
72 $event->timeduration = ($lesson->deadline - $lesson->available);
74 if ($event->timeduration > LESSON_MAX_EVENT_LENGTH) { /// Long durations create two events
75 $event2 = $event;
77 $event->name .= ' ('.get_string('lessonopens', 'lesson').')';
78 $event->timeduration = 0;
80 $event2->timestart = $lesson->deadline;
81 $event2->eventtype = 'close';
82 $event2->timeduration = 0;
83 $event2->name .= ' ('.get_string('lessoncloses', 'lesson').')';
85 add_event($event2);
88 add_event($event);
90 return $lesson->id;
94 /*******************************************************************/
95 function lesson_update_instance($lesson) {
96 /// Given an object containing all the necessary data,
97 /// (defined by the form in mod.html) this function
98 /// will update an existing instance with new data.
100 $lesson->timemodified = time();
101 $lesson->available = make_timestamp($lesson->availableyear,
102 $lesson->availablemonth, $lesson->availableday, $lesson->availablehour,
103 $lesson->availableminute);
104 $lesson->deadline = make_timestamp($lesson->deadlineyear,
105 $lesson->deadlinemonth, $lesson->deadlineday, $lesson->deadlinehour,
106 $lesson->deadlineminute);
107 $lesson->id = $lesson->instance;
109 /// CDC-FLAG ///
110 if (!empty($lesson->password)) {
111 $lesson->password = md5($lesson->password);
112 } else {
113 unset($lesson->password);
115 /// CDC-FLAG ///
117 if ($lesson->lessondefault) {
118 $lessondefault = $lesson;
119 unset($lessondefault->lessondefault);
120 unset($lessondefault->name);
121 unset($lessondefault->timemodified);
122 unset($lessondefault->available);
123 unset($lessondefault->deadline);
124 if ($lessondefault->id = get_field("lesson_default", "id", "course", $lessondefault->course)) {
125 update_record("lesson_default", $lessondefault);
126 } else {
127 insert_record("lesson_default", $lessondefault);
129 } else {
130 unset($lesson->lessondefault);
133 // update the calendar events (credit goes to quiz module)
134 if ($events = get_records_select('event', "modulename = 'lesson' and instance = '$lesson->id'")) {
135 foreach($events as $event) {
136 delete_event($event->id);
140 $event = new stdClass;
141 $event->name = $lesson->name;
142 $event->description = $lesson->name;
143 $event->courseid = $lesson->course;
144 $event->groupid = 0;
145 $event->userid = 0;
146 $event->modulename = 'lesson';
147 $event->instance = $lesson->id;
148 $event->eventtype = 'open';
149 $event->timestart = $lesson->available;
150 $event->visible = instance_is_visible('lesson', $lesson);
151 $event->timeduration = ($lesson->deadline - $lesson->available);
153 if ($event->timeduration > LESSON_MAX_EVENT_LENGTH) { /// Long durations create two events
154 $event2 = $event;
156 $event->name .= ' ('.get_string('lessonopens', 'lesson').')';
157 $event->timeduration = 0;
159 $event2->timestart = $lesson->deadline;
160 $event2->eventtype = 'close';
161 $event2->timeduration = 0;
162 $event2->name .= ' ('.get_string('lessoncloses', 'lesson').')';
164 add_event($event2);
167 add_event($event);
169 if (!empty($lesson->deleteattempts)) {
170 $subject = "Delete User Attempts";
171 $message = "";
173 if ($userid = get_field("user", "id", "username", $lesson->deleteattempts)) {
174 if (delete_records("lesson_attempts", "lessonid", $lesson->id, "userid", $userid)) {
175 // email good
176 $message .= "Successfully deleted attempts from \"".format_string($lesson->name)."\" lesson!<br />";
177 } else {
178 // email couldnt delete
179 $message .= "Failed to delete attempts from \"".format_string($lesson->name)."\" lesson!<br />";
181 if (delete_records("lesson_grades", "lessonid", $lesson->id, "userid", $userid)) {
182 // email good
183 $message .= "Successfully deleted grades from \"".format_string($lesson->name)."\" lesson!<br />";
184 } else {
185 // email couldnt delete
186 $message .= "Failed to delete grades from \"".format_string($lesson->name)."\" lesson!<br />";
188 if (delete_records("lesson_timer", "lessonid", $lesson->id, "userid", $userid)) {
189 // email good
190 $message .= "Successfully deleted time records from \"".format_string($lesson->name)."\" lesson!<br />";
191 } else {
192 // email couldnt delete
193 $message .= "Failed to delete time records from \"".format_string($lesson->name)."\" lesson!<br />";
196 } else {
197 // email couldnt find user
198 $message .= "Could not find user in database.<br />";
200 $message .= "<br /> User ID used: $lesson->deleteattempts <br />";
202 $txt = format_text_email($message, FORMAT_HTML);
204 if ($currentuser = get_record("user", "id", $lesson->deleteattemptsid)) {
205 email_to_user($currentuser, $currentuser, $subject, $txt, $message);
207 // unset lessondefault
209 unset($lesson->deleteattempts);
210 unset($lesson->deleteattemptsid);
212 return update_record("lesson", $lesson);
216 /*******************************************************************/
217 function lesson_delete_instance($id) {
218 /// Given an ID of an instance of this module,
219 /// this function will permanently delete the instance
220 /// and any data that depends on it.
222 if (! $lesson = get_record("lesson", "id", "$id")) {
223 return false;
226 $result = true;
228 if (! delete_records("lesson", "id", "$lesson->id")) {
229 $result = false;
231 if (! delete_records("lesson_pages", "lessonid", "$lesson->id")) {
232 $result = false;
234 if (! delete_records("lesson_answers", "lessonid", "$lesson->id")) {
235 $result = false;
237 if (! delete_records("lesson_attempts", "lessonid", "$lesson->id")) {
238 $result = false;
240 if (! delete_records("lesson_grades", "lessonid", "$lesson->id")) {
241 $result = false;
243 if (! delete_records("lesson_timer", "lessonid", "$lesson->id")) {
244 $result = false;
246 if (! delete_records("lesson_branch", "lessonid", "$lesson->id")) {
247 $result = false;
249 if (! delete_records("lesson_high_scores", "lessonid", "$lesson->id")) {
250 $result = false;
252 if ($events = get_records_select('event', "modulename = 'lesson' and instance = '$lesson->id'")) {
253 foreach($events as $event) {
254 delete_event($event->id);
258 return $result;
261 /*******************************************************************/
262 function lesson_user_outline($course, $user, $mod, $lesson) {
263 /// Return a small object with summary information about what a
264 /// user has done with a given particular instance of this module
265 /// Used for user activity reports.
266 /// $return->time = the time they did it
267 /// $return->info = a short text description
269 if ($grades = get_records_select("lesson_grades", "lessonid = $lesson->id AND userid = $user->id",
270 "grade DESC")) {
271 foreach ($grades as $grade) {
272 $max_grade = number_format($grade->grade * $lesson->grade / 100.0, 1);
273 break;
275 $return->time = $grade->completed;
276 if ($lesson->retake) {
277 $return->info = get_string("gradeis", "lesson", $max_grade)." (".
278 get_string("attempt", "lesson", count($grades)).")";
279 } else {
280 $return->info = get_string("gradeis", "lesson", $max_grade);
282 } else {
283 $return->info = get_string("no")." ".get_string("attempts", "lesson");
285 return $return;
288 /*******************************************************************/
289 function lesson_user_complete($course, $user, $mod, $lesson) {
290 /// Print a detailed representation of what a user has done with
291 /// a given particular instance of this module, for user activity reports.
293 if ($attempts = get_records_select("lesson_attempts", "lessonid = $lesson->id AND userid = $user->id",
294 "retry, timeseen")) {
295 print_simple_box_start();
296 $table->head = array (get_string("attempt", "lesson"), get_string("numberofpagesviewed", "lesson"),
297 get_string("numberofcorrectanswers", "lesson"), get_string("time"));
298 $table->width = "100%";
299 $table->align = array ("center", "center", "center", "center");
300 $table->size = array ("*", "*", "*", "*");
301 $table->cellpadding = 2;
302 $table->cellspacing = 0;
304 $retry = 0;
305 $npages = 0;
306 $ncorrect = 0;
308 foreach ($attempts as $attempt) {
309 if ($attempt->retry == $retry) {
310 $npages++;
311 if ($attempt->correct) {
312 $ncorrect++;
314 $timeseen = $attempt->timeseen;
315 } else {
316 $table->data[] = array($retry + 1, $npages, $ncorrect, userdate($timeseen));
317 $retry++;
318 $npages = 1;
319 if ($attempt->correct) {
320 $ncorrect = 1;
321 } else {
322 $ncorrect = 0;
326 if ($npages) {
327 $table->data[] = array($retry + 1, $npages, $ncorrect, userdate($timeseen));
329 print_table($table);
330 print_simple_box_end();
331 // also print grade summary
332 if ($grades = get_records_select("lesson_grades", "lessonid = $lesson->id AND userid = $user->id",
333 "grade DESC")) {
334 foreach ($grades as $grade) {
335 $max_grade = number_format($grade->grade * $lesson->grade / 100.0, 1);
336 break;
338 if ($lesson->retake) {
339 echo "<p>".get_string("gradeis", "lesson", $max_grade)." (".
340 get_string("attempts", "lesson").": ".count($grades).")</p>";
341 } else {
342 echo "<p>".get_string("gradeis", "lesson", $max_grade)."</p>";
345 } else {
346 echo get_string("no")." ".get_string("attempts", "lesson");
350 return true;
353 /*******************************************************************/
354 function lesson_print_recent_activity($course, $isteacher, $timestart) {
355 /// Given a course and a time, this module should find recent activity
356 /// that has occurred in lesson activities and print it out.
357 /// Return true if there was output, or false is there was none.
359 global $CFG;
361 return false; // True if anything was printed, otherwise false
364 /*******************************************************************/
365 function lesson_cron () {
366 /// Function to be run periodically according to the moodle cron
367 /// This function searches for things that need to be done, such
368 /// as sending out mail, toggling flags etc ...
370 global $CFG;
372 return true;
375 /*******************************************************************/
376 function lesson_grades($lessonid) {
377 /// Must return an array of grades for a given instance of this module,
378 /// indexed by user. It also returns a maximum allowed grade.
379 global $CFG;
381 if (!$lesson = get_record("lesson", "id", $lessonid)) {
382 return NULL;
384 if ($lesson->usemaxgrade) {
385 $grades = get_records_sql_menu("SELECT userid,MAX(grade) FROM {$CFG->prefix}lesson_grades WHERE
386 lessonid = $lessonid GROUP BY userid");
387 } else {
388 $grades = get_records_sql_menu("SELECT userid,AVG(grade) FROM {$CFG->prefix}lesson_grades WHERE
389 lessonid = $lessonid GROUP BY userid");
392 // convert grades from percentages and tidy the numbers
393 if (!$lesson->practice) { // dont display practice lessons CDC-FLAG
394 if ($grades) {
395 foreach ($grades as $userid => $grade) {
396 $return->grades[$userid] = number_format($grade * $lesson->grade / 100.0, 1);
399 $return->maxgrade = $lesson->grade;
401 return $return;
402 } else {
403 return NULL;
407 /*******************************************************************/
408 function lesson_get_participants($lessonid) {
409 //Must return an array of user records (all data) who are participants
410 //for a given instance of lesson. Must include every user involved
411 //in the instance, independient of his role (student, teacher, admin...)
413 global $CFG;
415 //Get students
416 $students = get_records_sql("SELECT DISTINCT u.id, u.id
417 FROM {$CFG->prefix}user u,
418 {$CFG->prefix}lesson_attempts a
419 WHERE a.lessonid = '$lessonid' and
420 u.id = a.userid");
422 //Return students array (it contains an array of unique users)
423 return ($students);