moodle: Time Spent in a lesson saved in the lesson timer table is distorted, wrecked

October 29, 2016

updated 2020-05-17

Moodle: Time Spent in a lesson is being falsified since mdl 2.3

note: As of moodle 3.0, a time limit on the lesson can be seconds, minutes, hours, days or weeks.
A person can leave and re-enter a lesson at will, but the termination time is set with the first entry into the lesson.

Moodle: To require a minimum time spent in a lesson

the time spent in a lesson was (years ago) preserved in a table that shows the time entering the lesson and the time leaving(finishing?) the lesson.

Also, in the first page of a lesson, if a person refreshes the page, it writes a new record wiping out credit for the time spent so far in the lesson. This has been happening in the moodle lesson module since even before moodle 1.9

The database lesson-timer table looks like this (small sample random data set):
mdl_lesson_timer
+--------+----------+--------+------------+------------+
| id | lessonid | userid | starttime | lessontime |
+--------+----------+--------+------------+------------+
| 551003 | 5189 | 16025 | 1466276070 | 1466276223 |
| 551002 | 7393 | 16339 | 1466275543 | 1466278305 |
| 551001 | 6354 | 16334 | 1466274984 | 1466276873 |
| 551000 | 3127 | 16329 | 1466274771 | 1466278575 |
| 550999 | 6448 | 16300 | 1466273952 | 1466276162 |
| 550998 | 5135 | 16040 | 1466273894 | 1466278542 |
| 550997 | 3134 | 16338 | 1466273559 | 1466273563 |
+--------+----------+--------+------------+------------+

[there is a “completed” field that is set sometimes but, still unused.]

To fix the error back in mdl 2.x

The first change needed is to initialize $duration ( =0 ) and to sum it ( += ), not replace it.

change lines 122 and 124 in mod/lesson/view.php in moodle 3.x (till 3.3)
(lines 110,112 in moodle 2.x etc.);
the first line, below, is added, and the 3rd line has been changed from “=” to “+=
$duration=0; // go through all the times and see if the total will satisfy the condition
foreach($attempttimes as $attempttime) {
$duration += $attempttime->lessontime - $attempttime->starttime;

the second step is to find this comment (below) on or near line # 191:
// if no pageid given see if the lesson has been started

Insert a few blank lines just above the comment line and, on those blanks lines, insert:

// This is for writing a new mdl_lesson_timer record
– {your name}
if (!$canmanage) {
$lesson->start_timer();
}

[ given that the person is a student, not a teacher or admin etc.: (!canmanage). ]

The “time spent” feature provides a simple solution to the common requirement. Students must spend a minimum amount of time in a lesson (in order to meet the time spent requirement for the whole course). The bug is in the code that disallows any lesson work to be done “on the users own time” instead, of being unnecessarily forced to accomplish each lesson in one sitting “all-at-once”.

This problem was submitted to the bug tracker, but the programmer involved had a temper-tantrum(!) and changed the outcome of the lesson timer table records. Till then, the records kept a log of when a person worked on a lesson. Since then, it only gives a total time and blindly assigns all of it to the date the record was last updated. It is no longer an accurate record. It has been falsified. For example, a student may do all but the very last page of a lesson on a Monday and come back in on Fri. to finish and moodle will change the record to claim the student did the entire lesson on that Fri.

edit mod/lesson/locallib.php and find
public function update_timer( . . .
down around line 2000

then about a dozen lines lower, you will see

if ($restart) {
if ($continue) {

comment out the block of code containing
$timer->starttime = time() - ($timer->lessontime - $timer->starttime);

which is the key to the problem.
and adjust the conditions to look like this:


if ($restart) {
# if ($continue) {
# // continue a previous test, need to update the clock . . .
# $timer->starttime = time() - ($timer->lessontime - $timer->starttime);
# ###### this line above is the wrecking line - supported by this paragraph
# // Trigger lesson resumed event.
# $event = \mod_lesson\event\lesson_resumed::create(array(
# 'objectid' => $this->properties->id,
# 'context' => context_module::instance($cm->id),
# 'courseid' => $this->properties->course
# ));
# $event->trigger();
# } else {
// starting over, so reset the clock ################ write new record! ####
## $timer->starttime = time(); ############ update record commented out ####
$this->start_timer() ; ################################ write new record! ####
// Trigger lesson restarted event.
$event = \mod_lesson\event\lesson_restarted::create(array(
'objectid' => $this->properties->id,
'context' => context_module::instance($cm->id),
'courseid' => $this->properties->course
));
$event->trigger();
}else { #################################################### update record! ####
$timenow = time();
$timer->lessontime = $timenow;
if (WS_SERVER) {
$timer->timemodifiedoffline = $timenow;
}
$timer->completed = $endreached;
$DB->update_record('lesson_timer', $timer);
// Update completion state.
$cm = get_coursemodule_from_instance('lesson', $this->properties()->id,
$this->properties()->course, false, MUST_EXIST);
$course = get_course($cm->course);
$completion = new completion_info($course);
if ($completion->is_enabled($cm) && $this->properties()->completiontimespent > 0) {
$completion->update_state($cm, COMPLETION_COMPLETE);
}
}
return $timer;

Comments are closed.

We try to post all comments within 1 business day