MDL-55140 mod_choice: Allow independent open and close dates

This commit is contained in:
Stephen Bourget
2016-07-27 07:18:54 -04:00
parent 90a8bdbfc0
commit 2ff504c6b2
10 changed files with 142 additions and 77 deletions
+18 -22
View File
@@ -204,20 +204,19 @@ class mod_choice_external extends external_api {
$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 @@ class mod_choice_external extends external_api {
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 @@ class mod_choice_external extends external_api {
} 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));
+2 -3
View File
@@ -47,14 +47,14 @@ $string['havetologin'] = 'You have to log in before you can submit your choice';
$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['showunanswered'] = 'Show column for unanswered';
$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';
+7 -20
View File
@@ -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;
+4 -4
View File
@@ -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);
+6 -13
View File
@@ -70,18 +70,16 @@ class mod_choice_mod_form extends moodleform_mod {
}
//-------------------------------------------------------------------------------
$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 @@ class mod_choice_mod_form extends moodleform_mod {
}
}
if (empty($default_values['timeopen'])) {
$default_values['timerestrict'] = 0;
} else {
$default_values['timerestrict'] = 1;
}
}
+2 -1
View File
@@ -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 |
@@ -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"
+2 -1
View File
@@ -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 |
@@ -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 |
+10 -12
View File
@@ -141,19 +141,17 @@ if (isloggedin() && (!empty($current)) &&
/// 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')) {