Skip to content

Commit

Permalink
MDL-57759 mod_lesson: Handle offline attempts in view and WS
Browse files Browse the repository at this point in the history
- Notify the user if there are previous offline attempts
- Set the offline attempts and return additional information via Web
Services
  • Loading branch information
jleyva committed Mar 27, 2017
1 parent c6f9f06 commit 2db9039
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
10 changes: 7 additions & 3 deletions mod/lesson/classes/external.php
Expand Up @@ -118,7 +118,7 @@ public static function get_lessons_by_courses($courseids = array()) {
'maxanswers', 'maxattempts', 'review', 'nextpagedefault', 'feedback', 'minquestions',
'maxpages', 'timelimit', 'retake', 'mediafile', 'mediaheight', 'mediawidth',
'mediaclose', 'slideshow', 'width', 'height', 'bgcolor', 'displayleft', 'displayleftif',
'progressbar');
'progressbar', 'allowofflineattempts');

// Fields only for managers.
if ($lesson->can_manage()) {
Expand Down Expand Up @@ -199,6 +199,8 @@ public static function get_lessons_by_courses_returns() {
VALUE_OPTIONAL),
'completiontimespent' => new external_value(PARAM_INT, 'Student must do this activity at least for',
VALUE_OPTIONAL),
'allowofflineattempts' => new external_value(PARAM_INT, 'Whether to allow the lesson to be attempted
offline in the mobile app', VALUE_OPTIONAL),
'visible' => new external_value(PARAM_INT, 'Visible?', VALUE_OPTIONAL),
'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL),
'groupingid' => new external_value(PARAM_INT, 'Grouping id', VALUE_OPTIONAL),
Expand Down Expand Up @@ -959,6 +961,7 @@ public static function get_user_timers_returns() {
'starttime' => new external_value(PARAM_INT, 'First access time for a new timer session'),
'lessontime' => new external_value(PARAM_INT, 'Last access time to the lesson during the timer session'),
'completed' => new external_value(PARAM_INT, 'If the lesson for this timer was completed'),
'timemodifiedoffline' => new external_value(PARAM_INT, 'Last modified time via webservices.'),
),
'The timers'
)
Expand Down Expand Up @@ -1332,8 +1335,9 @@ public static function get_page_data($lessonid, $pageid, $password = '', $revie
'answerfiles' => external_util::get_area_files($context->id, 'mod_lesson', 'page_answers', $a->id),
'responsefiles' => external_util::get_area_files($context->id, 'mod_lesson', 'page_responses', $a->id),
);
// For managers, return all the information (including scoring, jumps).
if ($lesson->can_manage()) {
// For managers, return all the information (including correct answers, jumps).
// If the teacher enabled offline attempts, this information will be downloaded too.
if ($lesson->can_manage() || $lesson->allowofflineattempts) {
$extraproperties = array('jumpto', 'grade', 'score', 'flags', 'timecreated', 'timemodified');
foreach ($extraproperties as $prop) {
$answer[$prop] = $a->{$prop};
Expand Down
1 change: 1 addition & 0 deletions mod/lesson/lang/en/lesson.php
Expand Up @@ -359,6 +359,7 @@
$string['numberofpagesviewedheader'] = 'Number of questions answered';
$string['numberofpagesviewednotice'] = 'Number of questions answered: {$a->nquestions} (You should answer at least {$a->minquestions})';
$string['numerical'] = 'Numerical';
$string['offlinedatamessage'] = 'You have worked on this attempt using a mobile device. Data was last saved to this site {$a} ago. Please check that you do not have any unsaved work.';
$string['ongoing'] = 'Display ongoing score';
$string['ongoing_help'] = 'If enabled, each page will display the student\'s current points earned out of the total possible thus far.';
$string['ongoingcustom'] = 'You have earned {$a->score} point(s) out of {$a->currenthigh} point(s) thus far.';
Expand Down
16 changes: 13 additions & 3 deletions mod/lesson/locallib.php
Expand Up @@ -1448,6 +1448,7 @@ public function construction_override($pageid, lesson $lesson) {
* @property int $available Timestamp of when this lesson becomes available
* @property int $deadline Timestamp of when this lesson is no longer available
* @property int $timemodified Timestamp when lesson was last modified
* @property int $allowofflineattempts Whether to allow the lesson to be attempted offline in the mobile app
*
* These properties are calculated
* @property int $firstpageid Id of the first page of this lesson (prevpageid=0)
Expand Down Expand Up @@ -2035,11 +2036,16 @@ public function start_timer() {
$event->trigger();

$USER->startlesson[$this->properties->id] = true;

$timenow = time();
$startlesson = new stdClass;
$startlesson->lessonid = $this->properties->id;
$startlesson->userid = $USER->id;
$startlesson->starttime = time();
$startlesson->lessontime = time();
$startlesson->starttime = $timenow;
$startlesson->lessontime = $timenow;
if (WS_SERVER) {
$startlesson->timemodifiedoffline = $timenow;
}
$DB->insert_record('lesson_timer', $startlesson);
if ($this->properties->timelimit) {
$this->add_message(get_string('timelimitwarning', 'lesson', format_time($this->properties->timelimit)), 'center');
Expand Down Expand Up @@ -2095,7 +2101,11 @@ public function update_timer($restart=false, $continue=false, $endreached =false
}
}

$timer->lessontime = time();
$timenow = time();
$timer->lessontime = $timenow;
if (WS_SERVER) {
$timer->timemodifiedoffline = $timenow;
}
$timer->completed = $endreached;
$DB->update_record('lesson_timer', $timer);

Expand Down
4 changes: 3 additions & 1 deletion mod/lesson/tests/external_test.php
Expand Up @@ -131,7 +131,7 @@ public function test_mod_lesson_get_lessons_by_courses() {
'maxanswers', 'maxattempts', 'review', 'nextpagedefault', 'feedback', 'minquestions',
'maxpages', 'timelimit', 'retake', 'mediafile', 'mediafiles', 'mediaheight', 'mediawidth',
'mediaclose', 'slideshow', 'width', 'height', 'bgcolor', 'displayleft', 'displayleftif',
'progressbar');
'progressbar', 'allowofflineattempts');

// Add expected coursemodule and data.
$lesson1 = $this->lesson;
Expand Down Expand Up @@ -661,6 +661,7 @@ public function test_get_user_timers() {
$timer1->completed = 1;
$timer1->starttime = time() - WEEKSECS;
$timer1->lessontime = time();
$timer1->timemodifiedoffline = time();
$timer1->id = $DB->insert_record("lesson_timer", $timer1);

$timer2 = new stdClass;
Expand All @@ -669,6 +670,7 @@ public function test_get_user_timers() {
$timer2->completed = 0;
$timer2->starttime = time() - DAYSECS;
$timer2->lessontime = time() + 1;
$timer2->timemodifiedoffline = time() + 1;
$timer2->id = $DB->insert_record("lesson_timer", $timer2);

// Test retrieve timers.
Expand Down
10 changes: 10 additions & 0 deletions mod/lesson/view.php
Expand Up @@ -121,6 +121,16 @@

$lastpageseen = $lesson->get_last_page_seen($retries);

// Check if the lesson was attempted in an external device like the mobile app.
// This check makes sense only when the lesson allows offline attempts.
if ($lesson->allowofflineattempts && $timers = $lesson->get_user_timers($USER->id, 'starttime DESC', '*', 0, 1)) {
$timer = current($timers);
if (!empty($timer->timemodifiedoffline)) {
$lasttime = format_time(time() - $timer->timemodifiedoffline);
$lesson->add_message(get_string('offlinedatamessage', 'lesson', $lasttime), 'warning');
}
}

// Check to see if end of lesson was reached.
if (($lastpageseen !== false && ($lastpageseen != LESSON_EOL))) {
// End not reached. Check if the user left.
Expand Down

0 comments on commit 2db9039

Please sign in to comment.