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.
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
);
32 if (!empty($lesson->password
)) {
33 $lesson->password
= md5($lesson->password
);
35 unset($lesson->password
);
38 if (!$lesson->id
= insert_record("lesson", $lesson)) {
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);
52 insert_record("lesson_default", $lessondefault);
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
;
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
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').')';
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
;
110 if (!empty($lesson->password
)) {
111 $lesson->password
= md5($lesson->password
);
113 unset($lesson->password
);
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);
127 insert_record("lesson_default", $lessondefault);
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
;
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
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').')';
169 if (!empty($lesson->deleteattempts
)) {
170 $subject = "Delete User Attempts";
173 if ($userid = get_field("user", "id", "username", $lesson->deleteattempts
)) {
174 if (delete_records("lesson_attempts", "lessonid", $lesson->id
, "userid", $userid)) {
176 $message .= "Successfully deleted attempts from \"".format_string($lesson->name
)."\" lesson!<br />";
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)) {
183 $message .= "Successfully deleted grades from \"".format_string($lesson->name
)."\" lesson!<br />";
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)) {
190 $message .= "Successfully deleted time records from \"".format_string($lesson->name
)."\" lesson!<br />";
192 // email couldnt delete
193 $message .= "Failed to delete time records from \"".format_string($lesson->name
)."\" lesson!<br />";
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")) {
228 if (! delete_records("lesson", "id", "$lesson->id")) {
231 if (! delete_records("lesson_pages", "lessonid", "$lesson->id")) {
234 if (! delete_records("lesson_answers", "lessonid", "$lesson->id")) {
237 if (! delete_records("lesson_attempts", "lessonid", "$lesson->id")) {
240 if (! delete_records("lesson_grades", "lessonid", "$lesson->id")) {
243 if (! delete_records("lesson_timer", "lessonid", "$lesson->id")) {
246 if (! delete_records("lesson_branch", "lessonid", "$lesson->id")) {
249 if (! delete_records("lesson_high_scores", "lessonid", "$lesson->id")) {
252 if ($events = get_records_select('event', "modulename = 'lesson' and instance = '$lesson->id'")) {
253 foreach($events as $event) {
254 delete_event($event->id
);
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",
271 foreach ($grades as $grade) {
272 $max_grade = number_format($grade->grade
* $lesson->grade
/ 100.0, 1);
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)).")";
280 $return->info
= get_string("gradeis", "lesson", $max_grade);
283 $return->info
= get_string("no")." ".get_string("attempts", "lesson");
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;
308 foreach ($attempts as $attempt) {
309 if ($attempt->retry
== $retry) {
311 if ($attempt->correct
) {
314 $timeseen = $attempt->timeseen
;
316 $table->data
[] = array($retry +
1, $npages, $ncorrect, userdate($timeseen));
319 if ($attempt->correct
) {
327 $table->data
[] = array($retry +
1, $npages, $ncorrect, userdate($timeseen));
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",
334 foreach ($grades as $grade) {
335 $max_grade = number_format($grade->grade
* $lesson->grade
/ 100.0, 1);
338 if ($lesson->retake
) {
339 echo "<p>".get_string("gradeis", "lesson", $max_grade)." (".
340 get_string("attempts", "lesson").": ".count($grades).")</p>";
342 echo "<p>".get_string("gradeis", "lesson", $max_grade)."</p>";
346 echo get_string("no")." ".get_string("attempts", "lesson");
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.
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 ...
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.
381 if (!$lesson = get_record("lesson", "id", $lessonid)) {
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");
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
395 foreach ($grades as $userid => $grade) {
396 $return->grades
[$userid] = number_format($grade * $lesson->grade
/ 100.0, 1);
399 $return->maxgrade
= $lesson->grade
;
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...)
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
422 //Return students array (it contains an array of unique users)