mirrored from git://git.moodle.org/moodle.git
/
lib.php
451 lines (387 loc) · 15.4 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
<?php // $Id$
/**
* Standard library of functions and constants for lesson
*
* @version $Id$
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package lesson
**/
define("LESSON_MAX_EVENT_LENGTH", "432000"); // 5 days maximum
/*******************************************************************/
function lesson_add_instance($lesson) {
/// Given an object containing all the necessary data,
/// (defined by the form in mod.html) this function
/// will create a new instance and return the id number
/// of the new instance.
global $SESSION;
$lesson->timemodified = time();
if(empty($lesson->timespent) or !is_numeric($lesson->timespent) or $lesson->timespent < 0) {
$lesson->timespent = 0;
}
if(!isset($lesson->completed)) {
$lesson->completed = 0;
}
if(empty($lesson->gradebetterthan) or !is_numeric($lesson->gradebetterthan) or $lesson->gradebetterthan < 0) {
$lesson->gradebetterthan = 0;
} else if ($lesson->gradebetterthan > 100) {
$lesson->gradebetterthan = 100;
}
// conditions for dependency
$conditions = new stdClass;
$conditions->timespent = $lesson->timespent;
$conditions->completed = $lesson->completed;
$conditions->gradebetterthan = clean_param($lesson->gradebetterthan, PARAM_INT);
$lesson->conditions = addslashes(serialize($conditions));
unset($lesson->timespent);
unset($lesson->completed);
unset($lesson->gradebetterthan);
if (!empty($lesson->password)) {
$lesson->password = md5($lesson->password);
} else {
unset($lesson->password);
}
if (!$lesson->id = insert_record("lesson", $lesson)) {
return false; // bad
}
if ($lesson->lessondefault) {
$default = new stdClass;
$default = clone($lesson);
unset($default->lessondefault);
unset($default->name);
unset($default->timemodified);
unset($default->available);
unset($default->deadline);
if ($default->id = get_field("lesson_default", "id", "course", $default->course)) {
update_record("lesson_default", $default);
} else {
insert_record("lesson_default", $default);
}
} else {
unset($lesson->lessondefault);
}
// got this code from quiz, thanks quiz!!!
delete_records('event', 'modulename', 'lesson', 'instance', $lesson->id); // Just in case
$event = new stdClass;
$event->name = $lesson->name;
$event->description = $lesson->name;
$event->courseid = $lesson->course;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'lesson';
$event->instance = $lesson->id;
$event->eventtype = 'open';
$event->timestart = $lesson->available;
$event->visible = instance_is_visible('lesson', $lesson);
$event->timeduration = ($lesson->deadline - $lesson->available);
if ($event->timeduration > LESSON_MAX_EVENT_LENGTH) { /// Long durations create two events
$event2 = clone($event);
$event->name .= ' ('.get_string('lessonopens', 'lesson').')';
$event->timeduration = 0;
$event2->timestart = $lesson->deadline;
$event2->eventtype = 'close';
$event2->timeduration = 0;
$event2->name .= ' ('.get_string('lessoncloses', 'lesson').')';
add_event($event2);
}
add_event($event);
return $lesson->id;
}
/*******************************************************************/
function lesson_update_instance($lesson) {
/// Given an object containing all the necessary data,
/// (defined by the form in mod.html) this function
/// will update an existing instance with new data.
$lesson->timemodified = time();
$lesson->id = $lesson->instance;
if(empty($lesson->timespent) or !is_numeric($lesson->timespent) or $lesson->timespent < 0) {
$lesson->timespent = 0;
}
if(!isset($lesson->completed)) {
$lesson->completed = 0;
}
if(empty($lesson->gradebetterthan) or !is_numeric($lesson->gradebetterthan) or $lesson->gradebetterthan < 0) {
$lesson->gradebetterthan = 0;
} else if ($lesson->gradebetterthan > 100) {
$lesson->gradebetterthan = 100;
}
// conditions for dependency
$conditions = new stdClass;
$conditions->timespent = $lesson->timespent;
$conditions->completed = $lesson->completed;
$conditions->gradebetterthan = $lesson->gradebetterthan;
$lesson->conditions = addslashes(serialize($conditions));
unset($lesson->timespent);
unset($lesson->completed);
unset($lesson->gradebetterthan);
if (!empty($lesson->password)) {
$lesson->password = md5($lesson->password);
} else {
unset($lesson->password);
}
if ($lesson->lessondefault) {
$default = new stdClass;
$default = clone($lesson);
unset($default->lessondefault);
unset($default->name);
unset($default->timemodified);
unset($default->available);
unset($default->deadline);
if ($default->id = get_field("lesson_default", "id", "course", $default->course)) {
update_record("lesson_default", $default);
} else {
insert_record("lesson_default", $default);
}
} else {
unset($lesson->lessondefault);
}
// update the calendar events (credit goes to quiz module)
if ($events = get_records_select('event', "modulename = 'lesson' and instance = '$lesson->id'")) {
foreach($events as $event) {
delete_event($event->id);
}
}
$event = new stdClass;
$event->name = $lesson->name;
$event->description = $lesson->name;
$event->courseid = $lesson->course;
$event->groupid = 0;
$event->userid = 0;
$event->modulename = 'lesson';
$event->instance = $lesson->id;
$event->eventtype = 'open';
$event->timestart = $lesson->available;
$event->visible = instance_is_visible('lesson', $lesson);
$event->timeduration = ($lesson->deadline - $lesson->available);
if ($event->timeduration > LESSON_MAX_EVENT_LENGTH) { /// Long durations create two events
$event2 = clone($event);
$event->name .= ' ('.get_string('lessonopens', 'lesson').')';
$event->timeduration = 0;
$event2->timestart = $lesson->deadline;
$event2->eventtype = 'close';
$event2->timeduration = 0;
$event2->name .= ' ('.get_string('lessoncloses', 'lesson').')';
add_event($event2);
}
add_event($event);
return update_record("lesson", $lesson);
}
/*******************************************************************/
function lesson_delete_instance($id) {
/// Given an ID of an instance of this module,
/// this function will permanently delete the instance
/// and any data that depends on it.
if (! $lesson = get_record("lesson", "id", "$id")) {
return false;
}
$result = true;
if (! delete_records("lesson", "id", "$lesson->id")) {
$result = false;
}
if (! delete_records("lesson_pages", "lessonid", "$lesson->id")) {
$result = false;
}
if (! delete_records("lesson_answers", "lessonid", "$lesson->id")) {
$result = false;
}
if (! delete_records("lesson_attempts", "lessonid", "$lesson->id")) {
$result = false;
}
if (! delete_records("lesson_grades", "lessonid", "$lesson->id")) {
$result = false;
}
if (! delete_records("lesson_timer", "lessonid", "$lesson->id")) {
$result = false;
}
if (! delete_records("lesson_branch", "lessonid", "$lesson->id")) {
$result = false;
}
if (! delete_records("lesson_high_scores", "lessonid", "$lesson->id")) {
$result = false;
}
if ($events = get_records_select('event', "modulename = 'lesson' and instance = '$lesson->id'")) {
foreach($events as $event) {
delete_event($event->id);
}
}
return $result;
}
/**
* Given a course object, this function will clean up anything that
* would be leftover after all the instances were deleted.
*
* As of now, this function just cleans the lesson_default table
*
* @param object $course an object representing the course that is being deleted
* @param boolean $feedback to specify if the process must output a summary of its work
* @return boolean
*/
function lesson_delete_course($course, $feedback=true) {
$count = count_records('lesson_default', 'course', $course->id);
delete_records('lesson_default', 'course', $course->id);
//Inform about changes performed if feedback is enabled
if ($feedback) {
notify(get_string('deletedefaults', 'lesson', $count));
}
return true;
}
/*******************************************************************/
function lesson_user_outline($course, $user, $mod, $lesson) {
/// Return a small object with summary information about what a
/// user has done with a given particular instance of this module
/// Used for user activity reports.
/// $return->time = the time they did it
/// $return->info = a short text description
if ($grades = get_records_select("lesson_grades", "lessonid = $lesson->id AND userid = $user->id",
"grade DESC")) {
foreach ($grades as $grade) {
$max_grade = number_format($grade->grade * $lesson->grade / 100.0, 1);
break;
}
$return->time = $grade->completed;
if ($lesson->retake) {
$return->info = get_string("gradeis", "lesson", $max_grade)." (".
get_string("attempt", "lesson", count($grades)).")";
} else {
$return->info = get_string("gradeis", "lesson", $max_grade);
}
} else {
$return->info = get_string("no")." ".get_string("attempts", "lesson");
}
return $return;
}
/*******************************************************************/
function lesson_user_complete($course, $user, $mod, $lesson) {
/// Print a detailed representation of what a user has done with
/// a given particular instance of this module, for user activity reports.
if ($attempts = get_records_select("lesson_attempts", "lessonid = $lesson->id AND userid = $user->id",
"retry, timeseen")) {
print_simple_box_start();
$table->head = array (get_string("attempt", "lesson"), get_string("numberofpagesviewed", "lesson"),
get_string("numberofcorrectanswers", "lesson"), get_string("time"));
$table->width = "100%";
$table->align = array ("center", "center", "center", "center");
$table->size = array ("*", "*", "*", "*");
$table->cellpadding = 2;
$table->cellspacing = 0;
$retry = 0;
$npages = 0;
$ncorrect = 0;
foreach ($attempts as $attempt) {
if ($attempt->retry == $retry) {
$npages++;
if ($attempt->correct) {
$ncorrect++;
}
$timeseen = $attempt->timeseen;
} else {
$table->data[] = array($retry + 1, $npages, $ncorrect, userdate($timeseen));
$retry++;
$npages = 1;
if ($attempt->correct) {
$ncorrect = 1;
} else {
$ncorrect = 0;
}
}
}
if ($npages) {
$table->data[] = array($retry + 1, $npages, $ncorrect, userdate($timeseen));
}
print_table($table);
print_simple_box_end();
// also print grade summary
if ($grades = get_records_select("lesson_grades", "lessonid = $lesson->id AND userid = $user->id",
"grade DESC")) {
foreach ($grades as $grade) {
$max_grade = number_format($grade->grade * $lesson->grade / 100.0, 1);
break;
}
if ($lesson->retake) {
echo "<p>".get_string("gradeis", "lesson", $max_grade)." (".
get_string("attempts", "lesson").": ".count($grades).")</p>";
} else {
echo "<p>".get_string("gradeis", "lesson", $max_grade)."</p>";
}
}
} else {
echo get_string("no")." ".get_string("attempts", "lesson");
}
return true;
}
/*******************************************************************/
function lesson_print_recent_activity($course, $isteacher, $timestart) {
/// Given a course and a time, this module should find recent activity
/// that has occurred in lesson activities and print it out.
/// Return true if there was output, or false is there was none.
global $CFG;
return false; // True if anything was printed, otherwise false
}
/*******************************************************************/
function lesson_cron () {
/// Function to be run periodically according to the moodle cron
/// This function searches for things that need to be done, such
/// as sending out mail, toggling flags etc ...
global $CFG;
return true;
}
/*******************************************************************/
function lesson_grades($lessonid) {
/// Must return an array of grades for a given instance of this module,
/// indexed by user. It also returns a maximum allowed grade.
global $CFG;
if (!$lesson = get_record("lesson", "id", $lessonid)) {
return NULL;
}
if ($lesson->retake) {
if ($lesson->usemaxgrade) {
$grades = get_records_sql_menu("SELECT userid,MAX(grade) FROM {$CFG->prefix}lesson_grades WHERE
lessonid = $lessonid GROUP BY userid");
} else {
$grades = get_records_sql_menu("SELECT userid,AVG(grade) FROM {$CFG->prefix}lesson_grades WHERE
lessonid = $lessonid GROUP BY userid");
}
} else {
// Retakes is turned Off; only count first attempt
$firstgrades = get_records_sql("SELECT userid, MIN(completed), grade FROM {$CFG->prefix}lesson_grades WHERE lessonid = $lessonid GROUP BY userid");
$grades = array();
if (!empty($firstgrades)) {
foreach($firstgrades as $userid => $info) {
$grades[$userid] = $info->grade;
}
}
}
// convert grades from percentages and tidy the numbers
if (!$lesson->practice) { // dont display practice lessons
if ($grades) {
foreach ($grades as $userid => $grade) {
$return->grades[$userid] = number_format($grade * $lesson->grade / 100.0, 1);
}
}
$return->maxgrade = $lesson->grade;
return $return;
} else {
return NULL;
}
}
/*******************************************************************/
function lesson_get_participants($lessonid) {
//Must return an array of user records (all data) who are participants
//for a given instance of lesson. Must include every user involved
//in the instance, independient of his role (student, teacher, admin...)
global $CFG;
//Get students
$students = get_records_sql("SELECT DISTINCT u.id, u.id
FROM {$CFG->prefix}user u,
{$CFG->prefix}lesson_attempts a
WHERE a.lessonid = '$lessonid' and
u.id = a.userid");
//Return students array (it contains an array of unique users)
return ($students);
}
function lesson_get_view_actions() {
return array('view','view all');
}
function lesson_get_post_actions() {
return array('end','start', 'update grade attempt');
}
?>