From 2db90397f1ec0a49c63de14610bcb8afeff1aee4 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Tue, 24 Jan 2017 17:08:03 +0100 Subject: [PATCH] MDL-57759 mod_lesson: Handle offline attempts in view and WS - Notify the user if there are previous offline attempts - Set the offline attempts and return additional information via Web Services --- mod/lesson/classes/external.php | 10 +++++++--- mod/lesson/lang/en/lesson.php | 1 + mod/lesson/locallib.php | 16 +++++++++++++--- mod/lesson/tests/external_test.php | 4 +++- mod/lesson/view.php | 10 ++++++++++ 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/mod/lesson/classes/external.php b/mod/lesson/classes/external.php index 55f428c7f7b1b..6945716afeba7 100644 --- a/mod/lesson/classes/external.php +++ b/mod/lesson/classes/external.php @@ -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()) { @@ -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), @@ -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' ) @@ -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}; diff --git a/mod/lesson/lang/en/lesson.php b/mod/lesson/lang/en/lesson.php index d0f361803b384..b61512ee5a035 100644 --- a/mod/lesson/lang/en/lesson.php +++ b/mod/lesson/lang/en/lesson.php @@ -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.'; diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index 24d85cdc144a8..721e56842a2cc 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -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) @@ -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'); @@ -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); diff --git a/mod/lesson/tests/external_test.php b/mod/lesson/tests/external_test.php index 66e7ad91c1bd3..c86ff3bd60507 100644 --- a/mod/lesson/tests/external_test.php +++ b/mod/lesson/tests/external_test.php @@ -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; @@ -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; @@ -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. diff --git a/mod/lesson/view.php b/mod/lesson/view.php index b308cec5c69f7..0ca0495cc92c2 100644 --- a/mod/lesson/view.php +++ b/mod/lesson/view.php @@ -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.