Skip to content

Commit

Permalink
MDL-55140 mod_choice: Allow independent open and close dates
Browse files Browse the repository at this point in the history
  • Loading branch information
sbourget committed Jul 27, 2016
1 parent 90a8bdb commit 2ff504c
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 77 deletions.
40 changes: 18 additions & 22 deletions mod/choice/classes/external.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down
5 changes: 2 additions & 3 deletions mod/choice/lang/en/choice.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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';
Expand Down
27 changes: 7 additions & 20 deletions mod/choice/lib.php
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions mod/choice/locallib.php
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand Down
19 changes: 6 additions & 13 deletions mod/choice/mod_form.php
Expand Up @@ -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'));
Expand Down Expand Up @@ -117,11 +115,6 @@ function data_preprocessing(&$default_values){
}

}
if (empty($default_values['timeopen'])) {
$default_values['timerestrict'] = 0;
} else {
$default_values['timerestrict'] = 1;
}

}

Expand Down
3 changes: 2 additions & 1 deletion mod/choice/tests/behat/allow_preview.feature
Expand Up @@ -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 |
Expand Down
89 changes: 89 additions & 0 deletions 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"
3 changes: 2 additions & 1 deletion mod/choice/tests/behat/my_home.feature
Expand Up @@ -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 |
Expand Down
3 changes: 2 additions & 1 deletion mod/choice/tests/behat/publish_results.feature
Expand Up @@ -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 |
Expand Down
22 changes: 10 additions & 12 deletions mod/choice/view.php
Expand Up @@ -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')) {
Expand Down

0 comments on commit 2ff504c

Please sign in to comment.