diff --git a/mod/lesson/backup/moodle2/backup_lesson_stepslib.php b/mod/lesson/backup/moodle2/backup_lesson_stepslib.php index ae17a64b253..c1467c4e644 100644 --- a/mod/lesson/backup/moodle2/backup_lesson_stepslib.php +++ b/mod/lesson/backup/moodle2/backup_lesson_stepslib.php @@ -72,7 +72,7 @@ class backup_lesson_activity_structure_step extends backup_activity_structure_st 'course', 'name', 'practice', 'modattempts', 'usepassword', 'password', 'dependency', 'conditions', 'grade', 'custom', 'ongoing', 'usemaxgrade', 'maxanswers', 'maxattempts', 'review', 'nextpagedefault', 'feedback', - 'minquestions', 'maxpages', 'timed', 'maxtime', 'retake', 'activitylink', + 'minquestions', 'maxpages', 'timelimit', 'retake', 'activitylink', 'mediafile', 'mediaheight', 'mediawidth', 'mediaclose', 'slideshow', 'width', 'height', 'bgcolor', 'displayleft', 'displayleftif', 'progressbar', 'showhighscores', 'maxhighscores', 'available', 'deadline', 'timemodified', diff --git a/mod/lesson/backup/moodle2/restore_lesson_stepslib.php b/mod/lesson/backup/moodle2/restore_lesson_stepslib.php index 7623598d989..b39350623c5 100644 --- a/mod/lesson/backup/moodle2/restore_lesson_stepslib.php +++ b/mod/lesson/backup/moodle2/restore_lesson_stepslib.php @@ -77,6 +77,14 @@ class restore_lesson_activity_structure_step extends restore_activity_structure_ $data->completionendreached = 0; } + // Compatibility with old backups with maxtime and timed fields. + if (!isset($data->timelimit)) { + if (isset($data->timed) && isset($data->maxtime) && $data->timed) { + $data->timelimit = 60 * $data->maxtime; + } else { + $data->timelimit = 0; + } + } // insert the lesson record $newitemid = $DB->insert_record('lesson', $data); // immediately after inserting "activity" record, call this diff --git a/mod/lesson/continue.php b/mod/lesson/continue.php index a1bc146f6a4..8dbac306cef 100644 --- a/mod/lesson/continue.php +++ b/mod/lesson/continue.php @@ -50,8 +50,8 @@ $PAGE->navbar->add(get_string('continue', 'lesson')); if (!$canmanage) { $lesson->displayleft = lesson_displayleftif($lesson); $timer = $lesson->update_timer(); - if ($lesson->timed) { - $timeleft = ($timer->starttime + $lesson->maxtime * 60) - time(); + if ($lesson->timelimit) { + $timeleft = ($timer->starttime + $lesson->timelimit) - time(); if ($timeleft <= 0) { // Out of time $lesson->add_message(get_string('eolstudentoutoftime', 'lesson')); @@ -157,7 +157,7 @@ if ($canmanage) { $lesson->add_message(get_string("teacherjumpwarning", "lesson", $warningvars)); } // Inform teacher that s/he will not see the timer - if ($lesson->timed) { + if ($lesson->timelimit) { $lesson->add_message(get_string("teachertimerwarning", "lesson")); } } diff --git a/mod/lesson/db/install.xml b/mod/lesson/db/install.xml index 46fbbbb2442..faf356f53ff 100644 --- a/mod/lesson/db/install.xml +++ b/mod/lesson/db/install.xml @@ -28,8 +28,7 @@ - - + diff --git a/mod/lesson/db/upgrade.php b/mod/lesson/db/upgrade.php index 97f71000632..cc4e631e3b4 100644 --- a/mod/lesson/db/upgrade.php +++ b/mod/lesson/db/upgrade.php @@ -178,5 +178,44 @@ function xmldb_lesson_upgrade($oldversion) { upgrade_mod_savepoint(true, 2015030301, 'lesson'); } + if ($oldversion < 2015030400) { + + // Creating new field timelimit in lesson table. + $table = new xmldb_table('lesson'); + $field = new xmldb_field('timelimit', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, null, '0', 'maxpages'); + + // Conditionally launch add field timelimit. + if (!$dbman->field_exists($table, $field)) { + $dbman->add_field($table, $field); + } + + // Lesson savepoint reached. + upgrade_mod_savepoint(true, 2015030400, 'lesson'); + } + + if ($oldversion < 2015030401) { + + // Convert maxtime (minutes) to timelimit (seconds). + $table = new xmldb_table('lesson'); + $oldfield = new xmldb_field('maxtime'); + $newfield = new xmldb_field('timelimit'); + if ($dbman->field_exists($table, $oldfield) && $dbman->field_exists($table, $newfield)) { + $sql = 'UPDATE {lesson} SET timelimit = 60 * maxtime'; + $DB->execute($sql); + // Drop field maxtime. + $dbman->drop_field($table, $oldfield); + } + + $oldfield = new xmldb_field('timed'); + if ($dbman->field_exists($table, $oldfield) && $dbman->field_exists($table, $newfield)) { + // Set timelimit to 0 for non timed lessons. + $DB->set_field_select('lesson', 'timelimit', 0, 'timed = 0'); + // Drop field timed. + $dbman->drop_field($table, $oldfield); + } + // Lesson savepoint reached. + upgrade_mod_savepoint(true, 2015030401, 'lesson'); + } + return true; } diff --git a/mod/lesson/lang/en/lesson.php b/mod/lesson/lang/en/lesson.php index c2d11367a35..32822e074ab 100644 --- a/mod/lesson/lang/en/lesson.php +++ b/mod/lesson/lang/en/lesson.php @@ -261,8 +261,6 @@ $string['maximumnumberofanswersbranches_help'] = 'This setting specifies the max $string['maximumnumberofattempts'] = 'Maximum number of attempts'; $string['maximumnumberofattempts_help'] = 'This setting specifies the maximum number of attempts allowed for each question. If answered incorrectly repeatedly, when the maximum is reached, the next page of the lesson is displayed.'; $string['maximumnumberofattemptsreached'] = 'Maximum number of attempts reached - Moving to next page'; -$string['maxtime'] = 'Time limit (minutes)'; -$string['maxtimewarning'] = 'You have {$a} minute(s) to finish the lesson.'; $string['mediaclose'] = 'Show close button:'; $string['mediafile'] = 'File pop-up'; $string['mediafile_help'] = 'To include a pop-up window at the beginning of a lesson, choose the appropriate file to display. Every lesson page will include a link to re-open the pop-up if necessary.'; @@ -435,6 +433,9 @@ $string['thatsthewronganswer'] = 'That\'s the wrong answer'; $string['thefollowingpagesjumptothispage'] = 'The following pages jump to this page'; $string['thispage'] = 'This page'; $string['timeisup'] = 'Time is up'; +$string['timelimit'] = 'Time limit'; +$string['timelimit_help'] = 'If enabled, a warning about the time limit is displayed at the beginning of the lesson and a countdown timer is displayed. Answer given after time is elapsed aren\'t graded'; +$string['timelimitwarning'] = 'You have {$a} to finish the lesson.'; $string['timeremaining'] = 'Time remaining'; $string['timespenterror'] = 'Spend at least {$a} minutes in the lesson'; $string['timespentminutes'] = 'Time spent (minutes)'; diff --git a/mod/lesson/lib.php b/mod/lesson/lib.php index 238ccde9596..06bea600962 100644 --- a/mod/lesson/lib.php +++ b/mod/lesson/lib.php @@ -523,8 +523,8 @@ function lesson_process_pre_save(&$lesson) { $lesson->timemodified = time(); - if (empty($lesson->timed)) { - $lesson->timed = 0; + if (empty($lesson->timelimit)) { + $lesson->timelimit = 0; } if (empty($lesson->timespent) or !is_numeric($lesson->timespent) or $lesson->timespent < 0) { $lesson->timespent = 0; diff --git a/mod/lesson/locallib.php b/mod/lesson/locallib.php index 45a0dbf6178..47d7ac6b93e 100644 --- a/mod/lesson/locallib.php +++ b/mod/lesson/locallib.php @@ -486,7 +486,7 @@ function lesson_mediafile_block_contents($cmid, $lesson) { function lesson_clock_block_contents($cmid, $lesson, $timer, $page) { // Display for timed lessons and for students only $context = context_module::instance($cmid); - if(!$lesson->timed || has_capability('mod/lesson:manage', $context)) { + if ($lesson->timelimit == 0 || has_capability('mod/lesson:manage', $context)) { return null; } @@ -494,7 +494,7 @@ function lesson_clock_block_contents($cmid, $lesson, $timer, $page) { $content .= $lesson->time_remaining($timer->starttime); $content .= ''; - $clocksettings = array('starttime'=>$timer->starttime, 'servertime'=>time(),'testlength'=>($lesson->maxtime * 60)); + $clocksettings = array('starttime' => $timer->starttime, 'servertime' => time(), 'testlength' => $lesson->timelimit); $page->requires->data_for_js('clocksettings', $clocksettings, true); $page->requires->strings_for_js(array('timeisup'), 'lesson'); $page->requires->js('/mod/lesson/timer.js'); @@ -1241,8 +1241,8 @@ class lesson extends lesson_base { $startlesson->starttime = time(); $startlesson->lessontime = time(); $DB->insert_record('lesson_timer', $startlesson); - if ($this->properties->timed) { - $this->add_message(get_string('maxtimewarning', 'lesson', $this->properties->maxtime), 'center'); + if ($this->properties->timelimit) { + $this->add_message(get_string('timelimitwarning', 'lesson', format_time($this->properties->timelimit)), 'center'); } return true; } @@ -1412,7 +1412,7 @@ class lesson extends lesson_base { * @return string */ public function time_remaining($starttime) { - $timeleft = $starttime + $this->maxtime * 60 - time(); + $timeleft = $starttime + $this->timelimit - time(); $hours = floor($timeleft/3600); $timeleft = $timeleft - ($hours * 3600); $minutes = floor($timeleft/60); diff --git a/mod/lesson/mod_form.php b/mod/lesson/mod_form.php index 8475ddc37c5..b8cc50d8955 100644 --- a/mod/lesson/mod_form.php +++ b/mod/lesson/mod_form.php @@ -171,22 +171,11 @@ class mod_lesson_mod_form extends moodleform_mod { $mform->addElement('date_time_selector', 'deadline', get_string('deadline', 'lesson'), array('optional'=>true)); $mform->setDefault('deadline', 0); - // Create a text box that can be enabled/disabled for lesson time limit - $timedgrp = array(); - $timedgrp[] = &$mform->createElement('text', 'maxtime'); - $timedgrp[] = &$mform->createElement('checkbox', 'timed', '', get_string('enable')); - $mform->addGroup($timedgrp, 'timedgrp', get_string('maxtime', 'lesson'), array(' '), false); - $mform->disabledIf('timedgrp', 'timed'); - - // Add numeric rule to text field - $timedgrprules = array(); - $timedgrprules['maxtime'][] = array(null, 'numeric', null, 'client'); - $mform->addGroupRule('timedgrp', $timedgrprules); - - // Rest of group setup - $mform->setDefault('timed', 0); - $mform->setDefault('maxtime', 20); - $mform->setType('maxtime', PARAM_INT); + // Time limit. + $mform->addElement('duration', 'timelimit', get_string('timelimit', 'lesson'), + array('optional' => true)); + $mform->addHelpButton('timelimit', 'timelimit', 'lesson'); + $mform->setDefault('timelimit', 1200); $mform->addElement('selectyesno', 'usepassword', get_string('usepassword', 'lesson')); $mform->addHelpButton('usepassword', 'usepassword', 'lesson'); @@ -334,9 +323,6 @@ class mod_lesson_mod_form extends moodleform_mod { function validation($data, $files) { $errors = parent::validation($data, $files); - if (empty($data['maxtime']) and !empty($data['timed'])) { - $errors['timedgrp'] = get_string('err_numeric', 'form'); - } if (!empty($data['usepassword']) && empty($data['password'])) { $errors['password'] = get_string('emptypassword', 'lesson'); } diff --git a/mod/lesson/tests/behat/time_limit.feature b/mod/lesson/tests/behat/time_limit.feature index 53fbfc1c1d8..800c5e8cec6 100644 --- a/mod/lesson/tests/behat/time_limit.feature +++ b/mod/lesson/tests/behat/time_limit.feature @@ -23,8 +23,7 @@ Feature: A teacher can set a time limit for a lesson And I add a "Lesson" to section "1" and I fill the form with: | Name | Test lesson | | Description | Test lesson description | - | timed | 1 | - | maxtime | 1 | + | timelimit | 60 | And I follow "Test lesson" And I follow "Add a content page" And I set the following fields to these values: diff --git a/mod/lesson/timer.js b/mod/lesson/timer.js index 4f08317d66c..3f3c8f0d9ed 100644 --- a/mod/lesson/timer.js +++ b/mod/lesson/timer.js @@ -11,7 +11,7 @@ // echo "\n"; diff --git a/mod/lesson/version.php b/mod/lesson/version.php index be9ad6bd8af..f1cca9e6db8 100644 --- a/mod/lesson/version.php +++ b/mod/lesson/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015030301; // The current module version (Date: YYYYMMDDXX) +$plugin->version = 2015030401; // The current module version (Date: YYYYMMDDXX) $plugin->requires = 2014110400; // Requires this Moodle version $plugin->component = 'mod_lesson'; // Full name of the plugin (used for diagnostics) $plugin->cron = 0; diff --git a/mod/lesson/view.php b/mod/lesson/view.php index 662b2ee022b..3daf7f8d5e0 100644 --- a/mod/lesson/view.php +++ b/mod/lesson/view.php @@ -240,7 +240,7 @@ if (empty($pageid)) { || $DB->count_records('lesson_branch', array("lessonid" => $lesson->id, "userid" => $USER->id, "retry" => $retries)) > 0) { echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('leftduringtimedsession', 'lesson')); - if ($lesson->timed) { + if ($lesson->timelimit) { if ($lesson->retake) { $continuelink = new single_button(new moodle_url('/mod/lesson/view.php', array('id' => $cm->id, 'pageid' => $lesson->firstpageid, 'startlastseen' => 'no')), @@ -325,8 +325,8 @@ if ($pageid != LESSON_EOL) { $restart = ($continue && $startlastseen == 'yes'); $timer = $lesson->update_timer($continue, $restart); - if ($lesson->timed) { - $timeleft = ($timer->starttime + $lesson->maxtime * 60) - time(); + if ($lesson->timelimit) { + $timeleft = $timer->starttime + $lesson->timelimit - time(); if ($timeleft <= 0) { // Out of time $lesson->add_message(get_string('eolstudentoutoftime', 'lesson')); @@ -363,7 +363,7 @@ if ($pageid != LESSON_EOL) { } } else { $timer = null; - if ($lesson->timed) { + if ($lesson->timelimit) { $lesson->add_message(get_string('teachertimerwarning', 'lesson')); } if (lesson_display_teacher_warning($lesson)) { @@ -510,7 +510,7 @@ if ($pageid != LESSON_EOL) { $DB->delete_records("lesson_attempts", array("lessonid" => $lesson->id, "userid" => $USER->id, "retry" => $ntries)); } } else { - if ($lesson->timed) { + if ($lesson->timelimit) { if ($outoftime == 'normal') { $grade = new stdClass(); $grade->lessonid = $lesson->id;