From 2ff504c6b2e350fed4fd9052d52c15455e1ea8f2 Mon Sep 17 00:00:00 2001 From: Stephen Bourget Date: Wed, 27 Jul 2016 07:18:54 -0400 Subject: [PATCH] MDL-55140 mod_choice: Allow independent open and close dates --- mod/choice/classes/external.php | 40 ++++----- mod/choice/lang/en/choice.php | 5 +- mod/choice/lib.php | 27 ++---- mod/choice/locallib.php | 8 +- mod/choice/mod_form.php | 19 ++-- mod/choice/tests/behat/allow_preview.feature | 3 +- .../tests/behat/choice_availability.feature | 89 +++++++++++++++++++ mod/choice/tests/behat/my_home.feature | 3 +- .../tests/behat/publish_results.feature | 3 +- mod/choice/view.php | 22 +++-- 10 files changed, 142 insertions(+), 77 deletions(-) create mode 100644 mod/choice/tests/behat/choice_availability.feature diff --git a/mod/choice/classes/external.php b/mod/choice/classes/external.php index 6732b9494ce61..48053d354156c 100644 --- a/mod/choice/classes/external.php +++ b/mod/choice/classes/external.php @@ -204,20 +204,19 @@ public static function get_choice_options($choiceid) { $choiceopen = true; $showpreview = false; - if ($choice->timeclose != 0) { - if ($choice->timeopen > $timenow) { - $choiceopen = false; - $warnings[1] = get_string("notopenyet", "choice", userdate($choice->timeopen)); - if ($choice->showpreview) { - $warnings[2] = get_string('previewonly', 'choice', userdate($choice->timeopen)); - $showpreview = true; - } - } - if ($timenow > $choice->timeclose) { - $choiceopen = false; - $warnings[3] = get_string("expired", "choice", userdate($choice->timeclose)); + if (!empty($choice->timeopen) && ($choice->timeopen > $timenow)) { + $choiceopen = false; + $warnings[1] = get_string("notopenyet", "choice", userdate($choice->timeopen)); + if ($choice->showpreview) { + $warnings[2] = get_string('previewonly', 'choice', userdate($choice->timeopen)); + $showpreview = true; } } + if (!empty($choice->timeclose) && ($timenow > $choice->timeclose)) { + $choiceopen = false; + $warnings[3] = get_string("expired", "choice", userdate($choice->timeclose)); + } + $optionsarray = array(); if ($choiceopen or $showpreview) { @@ -333,13 +332,12 @@ public static function submit_choice_response($choiceid, $responses) { require_capability('mod/choice:choose', $context); $timenow = time(); - if ($choice->timeclose != 0) { - if ($choice->timeopen > $timenow) { - throw new moodle_exception("notopenyet", "choice", '', userdate($choice->timeopen)); - } else if ($timenow > $choice->timeclose) { - throw new moodle_exception("expired", "choice", '', userdate($choice->timeclose)); - } + if (!empty($choice->timeopen) && ($choice->timeopen > $timenow)) { + throw new moodle_exception("notopenyet", "choice", '', userdate($choice->timeopen)); + } else if (!empty($choice->timeclose) && ($timenow > $choice->timeclose)) { + throw new moodle_exception("expired", "choice", '', userdate($choice->timeclose)); } + if (!choice_get_my_response($choice) or $choice->allowupdate) { // When a single response is given, we convert the array to a simple variable // in order to avoid choice_user_submit_response to check with allowmultiple even @@ -645,10 +643,8 @@ public static function delete_choice_responses($choiceid, $responses = array()) } else if ($choice->allowupdate) { // Check if we can delate our own responses. $timenow = time(); - if ($choice->timeclose != 0) { - if ($timenow > $choice->timeclose) { - throw new moodle_exception("expired", "choice", '', userdate($choice->timeclose)); - } + if (!empty($choice->timeclose) && ($timenow > $choice->timeclose)) { + throw new moodle_exception("expired", "choice", '', userdate($choice->timeclose)); } // Delete only our responses. $myresponses = array_keys(choice_get_my_response($choice)); diff --git a/mod/choice/lang/en/choice.php b/mod/choice/lang/en/choice.php index 8e259b032a447..dcd01e5b84e66 100644 --- a/mod/choice/lang/en/choice.php +++ b/mod/choice/lang/en/choice.php @@ -47,14 +47,14 @@ $string['choice'] = 'Choice'; $string['choiceactivityname'] = 'Choice: {$a}'; $string['choice:addinstance'] = 'Add a new choice'; -$string['choiceclose'] = 'Until'; +$string['choiceclose'] = 'Allow responses until'; $string['choice:deleteresponses'] = 'Delete responses'; $string['choice:downloadresponses'] = 'Download responses'; $string['choicefull'] = 'This choice is full and there are no available places.'; $string['choice:choose'] = 'Record a choice'; $string['choicecloseson'] = 'Choice closes on {$a}'; $string['choicename'] = 'Choice name'; -$string['choiceopen'] = 'Open'; +$string['choiceopen'] = 'Allow responses from'; $string['choiceoptions'] = 'Choice options'; $string['choiceoptions_help'] = 'Here is where you specify the options that participants have to choose from. @@ -123,7 +123,6 @@ $string['spaceleft'] = 'space available'; $string['spacesleft'] = 'spaces available'; $string['taken'] = 'Taken'; -$string['timerestrict'] = 'Restrict answering to this time period'; $string['viewallresponses'] = 'View {$a} responses'; $string['withselected'] = 'With selected'; $string['userchoosethisoption'] = 'Users who chose this option'; diff --git a/mod/choice/lib.php b/mod/choice/lib.php index 5d221128b485c..25944029626dc 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -117,11 +117,6 @@ function choice_add_instance($choice) { $choice->timemodified = time(); - if (empty($choice->timerestrict)) { - $choice->timeopen = 0; - $choice->timeclose = 0; - } - //insert answers $choice->id = $DB->insert_record("choice", $choice); foreach ($choice->option as $key => $value) { @@ -160,12 +155,6 @@ function choice_update_instance($choice) { $choice->id = $choice->instance; $choice->timemodified = time(); - - if (empty($choice->timerestrict)) { - $choice->timeopen = 0; - $choice->timeclose = 0; - } - //update, delete or insert answers foreach ($choice->option as $key => $value) { $value = trim($value); @@ -1065,16 +1054,14 @@ function choice_get_availability_status($choice) { $available = true; $warnings = array(); - if ($choice->timeclose != 0) { - $timenow = time(); + $timenow = time(); - if ($choice->timeopen > $timenow) { - $available = false; - $warnings['notopenyet'] = userdate($choice->timeopen); - } else if ($timenow > $choice->timeclose) { - $available = false; - $warnings['expired'] = userdate($choice->timeclose); - } + if (!empty($choice->timeopen) && ($choice->timeopen > $timenow)) { + $available = false; + $warnings['notopenyet'] = userdate($choice->timeopen); + } else if (!empty($choice->timeclose) && ($timenow > $choice->timeclose)) { + $available = false; + $warnings['expired'] = userdate($choice->timeclose); } if (!$choice->allowupdate && choice_get_my_response($choice)) { $available = false; diff --git a/mod/choice/locallib.php b/mod/choice/locallib.php index b3291ba167543..4dd6418eae063 100644 --- a/mod/choice/locallib.php +++ b/mod/choice/locallib.php @@ -45,7 +45,7 @@ function choice_set_events($choice) { $event = new stdClass(); if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'choice', 'instance' => $choice->id, 'eventtype' => 'open'))) { - if ($choice->timeopen > 0) { + if ((!empty($choice->timeopen)) && ($choice->timeopen > 0)) { // Calendar event exists so update it. $event->name = get_string('calendarstart', 'choice', $choice->name); $event->description = format_module_intro('choice', $choice, $choice->coursemodule); @@ -61,7 +61,7 @@ function choice_set_events($choice) { } } else { // Event doesn't exist so create one. - if ($choice->timeopen > 0) { + if ((!empty($choice->timeopen)) && ($choice->timeopen > 0)) { $event->name = get_string('calendarstart', 'choice', $choice->name); $event->description = format_module_intro('choice', $choice, $choice->coursemodule); $event->courseid = $choice->course; @@ -81,7 +81,7 @@ function choice_set_events($choice) { $event = new stdClass(); if ($event->id = $DB->get_field('event', 'id', array('modulename' => 'choice', 'instance' => $choice->id, 'eventtype' => 'close'))) { - if ($choice->timeclose > 0) { + if ((!empty($choice->timeclose)) && ($choice->timeclose > 0)) { // Calendar event exists so update it. $event->name = get_string('calendarend', 'choice', $choice->name); $event->description = format_module_intro('choice', $choice, $choice->coursemodule); @@ -97,7 +97,7 @@ function choice_set_events($choice) { } } else { // Event doesn't exist so create one. - if ($choice->timeclose > 0) { + if ((!empty($choice->timeclose)) && ($choice->timeclose > 0)) { $event = new stdClass(); $event->name = get_string('calendarend', 'choice', $choice->name); $event->description = format_module_intro('choice', $choice, $choice->coursemodule); diff --git a/mod/choice/mod_form.php b/mod/choice/mod_form.php index e15ad2df714d8..825639060268c 100644 --- a/mod/choice/mod_form.php +++ b/mod/choice/mod_form.php @@ -70,18 +70,16 @@ function definition() { } //------------------------------------------------------------------------------- - $mform->addElement('header', 'timerestricthdr', get_string('availability')); - $mform->addElement('checkbox', 'timerestrict', get_string('timerestrict', 'choice')); + $mform->addElement('header', 'availabilityhdr', get_string('availability')); + $mform->addElement('date_time_selector', 'timeopen', get_string("choiceopen", "choice"), + array('optional' => true)); - $mform->addElement('date_time_selector', 'timeopen', get_string("choiceopen", "choice")); - $mform->disabledIf('timeopen', 'timerestrict'); - - $mform->addElement('date_time_selector', 'timeclose', get_string("choiceclose", "choice")); - $mform->disabledIf('timeclose', 'timerestrict'); + $mform->addElement('date_time_selector', 'timeclose', get_string("choiceclose", "choice"), + array('optional' => true)); $mform->addElement('advcheckbox', 'showpreview', get_string('showpreview', 'choice')); $mform->addHelpButton('showpreview', 'showpreview', 'choice'); - $mform->disabledIf('showpreview', 'timerestrict'); + $mform->disabledIf('showpreview', 'timeopen[enabled]'); //------------------------------------------------------------------------------- $mform->addElement('header', 'resultshdr', get_string('results', 'choice')); @@ -117,11 +115,6 @@ function data_preprocessing(&$default_values){ } } - if (empty($default_values['timeopen'])) { - $default_values['timerestrict'] = 0; - } else { - $default_values['timerestrict'] = 1; - } } diff --git a/mod/choice/tests/behat/allow_preview.feature b/mod/choice/tests/behat/allow_preview.feature index 9d2c1d2961e08..32023fbf84ca7 100644 --- a/mod/choice/tests/behat/allow_preview.feature +++ b/mod/choice/tests/behat/allow_preview.feature @@ -26,7 +26,8 @@ Feature: Allow choice preview | Description | Choice Description | | option[0] | Option 1 | | option[1] | Option 2 | - | Restrict answering to this time period | 1 | + | timeopen[enabled] | 1 | + | timeclose[enabled] | 1 | | timeopen[day] | 30 | | timeopen[month] | December | | timeopen[year] | 2037 | diff --git a/mod/choice/tests/behat/choice_availability.feature b/mod/choice/tests/behat/choice_availability.feature new file mode 100644 index 0000000000000..1894ac0e7f273 --- /dev/null +++ b/mod/choice/tests/behat/choice_availability.feature @@ -0,0 +1,89 @@ +@mod @mod_choice +Feature: Restrict availability of the choice module to a deadline + In order to limit the time a student can mace a selection + As a teacher + I need to restrict answering to within a time period + + Background: + Given the following "users" exist: + | username | firstname | lastname | email | + | teacher1 | Teacher | 1 | teacher1@example.com | + | student1 | Student | 1 | student1@example.com | + And the following "courses" exist: + | fullname | shortname | category | + | Course 1 | C1 | 0 | + And the following "course enrolments" exist: + | user | course | role | + | teacher1 | C1 | editingteacher | + | student1 | C1 | student | + And I log in as "teacher1" + And I follow "Course 1" + And I turn editing mode on + + Scenario: Enable the choice activity with a start deadline in the future + Given I add a "Choice" to section "1" and I fill the form with: + | Choice name | Choice name | + | Description | Choice Description | + | option[0] | Option 1 | + | option[1] | Option 2 | + | timeopen[enabled] | 1 | + | timeopen[day] | 30 | + | timeopen[month] | December | + | timeopen[year] | 2037 | + And I log out + When I log in as "student1" + And I follow "Course 1" + And I follow "Choice name" + Then I should see "Sorry, this activity is not available until" + + Scenario: Enable the choice activity with a start deadline in the past + Given I add a "Choice" to section "1" and I fill the form with: + | Choice name | Choice name | + | Description | Choice Description | + | option[0] | Option 1 | + | option[1] | Option 2 | + | timeopen[enabled] | 1 | + | timeopen[day] | 30 | + | timeopen[month] | December | + | timeopen[year] | 2007 | + And I log out + When I log in as "student1" + And I follow "Course 1" + And I follow "Choice name" + And "choice_1" "radio" should exist + And "choice_2" "radio" should exist + And "Save my choice" "button" should exist + + Scenario: Enable the choice activity with a end deadline in the future + Given I add a "Choice" to section "1" and I fill the form with: + | Choice name | Choice name | + | Description | Choice Description | + | option[0] | Option 1 | + | option[1] | Option 2 | + | timeclose[enabled] | 1 | + | timeclose[day] | 30 | + | timeclose[month] | December | + | timeclose[year] | 2037 | + And I log out + When I log in as "student1" + And I follow "Course 1" + And I follow "Choice name" + And "choice_1" "radio" should exist + And "choice_2" "radio" should exist + And "Save my choice" "button" should exist + + Scenario: Enable the choice activity with a end deadline in the past + Given I add a "Choice" to section "1" and I fill the form with: + | Choice name | Choice name | + | Description | Choice Description | + | option[0] | Option 1 | + | option[1] | Option 2 | + | timeclose[enabled] | 1 | + | timeclose[day] | 30 | + | timeclose[month] | December | + | timeclose[year] | 2007 | + And I log out + When I log in as "student1" + And I follow "Course 1" + And I follow "Choice name" + Then I should see "Sorry, this activity closed on" diff --git a/mod/choice/tests/behat/my_home.feature b/mod/choice/tests/behat/my_home.feature index 17f571bbfd5ab..2ff3cc948cda2 100644 --- a/mod/choice/tests/behat/my_home.feature +++ b/mod/choice/tests/behat/my_home.feature @@ -24,7 +24,8 @@ Feature: Test the display of the choice module on my home And I set the following fields to these values: | Choice name | Test choice name | | Description | Test choice description | - | id_timerestrict| 1 | + | timeopen[enabled] | 1 | + | timeclose[enabled] | 1 | | timeclose[day] | 1 | | timeclose[month] | January | | timeclose[year] | 2030 | diff --git a/mod/choice/tests/behat/publish_results.feature b/mod/choice/tests/behat/publish_results.feature index 16faeab5165ee..2205dd0833141 100644 --- a/mod/choice/tests/behat/publish_results.feature +++ b/mod/choice/tests/behat/publish_results.feature @@ -75,10 +75,11 @@ Feature: A teacher can choose one of 4 options for publishing choice results And I follow "Edit settings" And I expand all fieldsets And I set the following fields to these values: - | Restrict answering to this time period | 1 | + | timeopen[enabled] | 1 | | timeopen[day] | 1 | | timeopen[month] | January | | timeopen[year] | 2010 | + | timeclose[enabled] | 1 | | timeclose[day] | 2 | | timeclose[month] | January | | timeclose[year] | 2010 | diff --git a/mod/choice/view.php b/mod/choice/view.php index 7ced17e3a3f21..59cc9df28f155 100644 --- a/mod/choice/view.php +++ b/mod/choice/view.php @@ -141,19 +141,17 @@ /// Print the form $choiceopen = true; -if ($choice->timeclose !=0) { - if ($choice->timeopen > $timenow ) { - if ($choice->showpreview) { - echo $OUTPUT->box(get_string('previewonly', 'choice', userdate($choice->timeopen)), 'generalbox alert'); - } else { - echo $OUTPUT->box(get_string("notopenyet", "choice", userdate($choice->timeopen)), "generalbox notopenyet"); - echo $OUTPUT->footer(); - exit; - } - } else if ($timenow > $choice->timeclose) { - echo $OUTPUT->box(get_string("expired", "choice", userdate($choice->timeclose)), "generalbox expired"); - $choiceopen = false; +if ((!empty($choice->timeopen)) && ($choice->timeopen > $timenow)) { + if ($choice->showpreview) { + echo $OUTPUT->box(get_string('previewonly', 'choice', userdate($choice->timeopen)), 'generalbox alert'); + } else { + echo $OUTPUT->box(get_string("notopenyet", "choice", userdate($choice->timeopen)), "generalbox notopenyet"); + echo $OUTPUT->footer(); + exit; } +} else if ((!empty($choice->timeclose)) && ($timenow > $choice->timeclose)) { + echo $OUTPUT->box(get_string("expired", "choice", userdate($choice->timeclose)), "generalbox expired"); + $choiceopen = false; } if ( (!$current or $choice->allowupdate) and $choiceopen and is_enrolled($context, NULL, 'mod/choice:choose')) {