From 0ec076ef3457c8ca96ee0d5a8dd3915ed99c1c75 Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Fri, 17 Aug 2018 15:12:10 +1000 Subject: [PATCH 1/3] MDL-63135 mod_choice: Added choice_get_user_response function A new function to return choice answers records of a given user in a given choice activity. --- mod/choice/lib.php | 17 +++++++++++++-- mod/choice/tests/lib_test.php | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/mod/choice/lib.php b/mod/choice/lib.php index 3953d43b42b..a0ed6f67857 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -1011,6 +1011,19 @@ function choice_print_overview($courses, &$htmlarray) { } +/** + * Get responses of a given user on a given choice. + * + * @param stdClass $choice Choice record + * @param int $userid User id + * @return array of choice answers records + * @since Moodle 3.6 + */ +function choice_get_user_response($choice, $userid) { + global $DB; + return $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $userid), 'optionid'); +} + /** * Get my responses on a given choice. * @@ -1019,8 +1032,8 @@ function choice_print_overview($courses, &$htmlarray) { * @since Moodle 3.0 */ function choice_get_my_response($choice) { - global $DB, $USER; - return $DB->get_records('choice_answers', array('choiceid' => $choice->id, 'userid' => $USER->id), 'optionid'); + global $USER; + return choice_get_user_response($choice, $USER->id); } diff --git a/mod/choice/tests/lib_test.php b/mod/choice/tests/lib_test.php index eb452e16dc1..dff09cff39c 100644 --- a/mod/choice/tests/lib_test.php +++ b/mod/choice/tests/lib_test.php @@ -162,6 +162,47 @@ class mod_choice_lib_testcase extends externallib_advanced_testcase { choice_user_submit_response($optionids2[0], $choice1, $USER->id, $course, $cm); } + /** + * Test choice_get_user_response + * @return void + */ + public function test_choice_get_user_response() { + $this->resetAfterTest(); + + $this->setAdminUser(); + // Setup test data. + $course = $this->getDataGenerator()->create_course(); + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id)); + $cm = get_coursemodule_from_instance('choice', $choice->id); + + $choicewithoptions = choice_get_choice($choice->id); + $optionids = array_keys($choicewithoptions->option); + + choice_user_submit_response($optionids[0], $choice, $student->id, $course, $cm); + $responses = choice_get_user_response($choice, $student->id); + $this->assertCount(1, $responses); + $response = array_shift($responses); + $this->assertEquals($optionids[0], $response->optionid); + + // Multiple responses. + $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id, 'allowmultiple' => 1)); + $cm = get_coursemodule_from_instance('choice', $choice->id); + + $choicewithoptions = choice_get_choice($choice->id); + $optionids = array_keys($choicewithoptions->option); + + // Submit a response with the options reversed. + $selections = $optionids; + rsort($selections); + choice_user_submit_response($selections, $choice, $student->id, $course, $cm); + $responses = choice_get_user_response($choice, $student->id); + $this->assertCount(count($optionids), $responses); + foreach ($responses as $resp) { + $this->assertEquals(array_shift($optionids), $resp->optionid); + } + } + /** * Test choice_get_my_response * @return void From 1b82088f984499c854287c24c8a8a867598af803 Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Fri, 17 Aug 2018 16:06:24 +1000 Subject: [PATCH 2/3] MDL-63135 mod_choice: Add userid param to mod_choice calendar callback --- mod/choice/lib.php | 13 ++- mod/choice/tests/lib_test.php | 198 +++++++++++++++++++++++++++++++--- 2 files changed, 196 insertions(+), 15 deletions(-) diff --git a/mod/choice/lib.php b/mod/choice/lib.php index a0ed6f67857..72ecc8f2da3 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -1229,12 +1229,19 @@ function choice_check_updates_since(cm_info $cm, $from, $filter = array()) { * * @param calendar_event $event * @param \core_calendar\action_factory $factory + * @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default). * @return \core_calendar\local\event\entities\action_interface|null */ function mod_choice_core_calendar_provide_event_action(calendar_event $event, - \core_calendar\action_factory $factory) { + \core_calendar\action_factory $factory, + int $userid = 0) { + global $USER; - $cm = get_fast_modinfo($event->courseid)->instances['choice'][$event->instance]; + if (!$userid) { + $userid = $USER->id; + } + + $cm = get_fast_modinfo($event->courseid, $userid)->instances['choice'][$event->instance]; $now = time(); if (!empty($cm->customdata['timeclose']) && $cm->customdata['timeclose'] < $now) { @@ -1246,7 +1253,7 @@ function mod_choice_core_calendar_provide_event_action(calendar_event $event, // in the past. $actionable = (empty($cm->customdata['timeopen']) || $cm->customdata['timeopen'] <= $now); - if ($actionable && choice_get_my_response((object)['id' => $event->instance])) { + if ($actionable && choice_get_user_response((object)['id' => $event->instance], $userid)) { // There is no action if the user has already submitted their choice. return null; } diff --git a/mod/choice/tests/lib_test.php b/mod/choice/tests/lib_test.php index dff09cff39c..a17c456d977 100644 --- a/mod/choice/tests/lib_test.php +++ b/mod/choice/tests/lib_test.php @@ -335,28 +335,56 @@ class mod_choice_lib_testcase extends externallib_advanced_testcase { $this->assertTrue($actionevent->is_actionable()); } - /** - * An event should not have an action if the user has already submitted a response - * to the choice activity. - */ - public function test_choice_core_calendar_provide_event_action_already_submitted() { - global $DB; - + public function test_choice_core_calendar_provide_event_action_open_for_user() { $this->resetAfterTest(); $this->setAdminUser(); // Create a course. $course = $this->getDataGenerator()->create_course(); - // Create user. - $student = $this->getDataGenerator()->create_user(); - $studentrole = $DB->get_record('role', array('shortname' => 'student')); - $this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual'); + + // Create a student. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // Create a choice. + $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id, + 'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS)); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $choice->id, CHOICE_EVENT_TYPE_OPEN); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event for the student. + $actionevent = mod_choice_core_calendar_provide_event_action($event, $factory, $student->id); + + // Confirm the event was decorated. + $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); + $this->assertEquals(get_string('viewchoices', 'choice'), $actionevent->get_name()); + $this->assertInstanceOf('moodle_url', $actionevent->get_url()); + $this->assertEquals(1, $actionevent->get_item_count()); + $this->assertTrue($actionevent->is_actionable()); + } + + /** + * An event should not have an action if the user has already submitted a response + * to the choice activity. + */ + public function test_choice_core_calendar_provide_event_action_already_submitted() { + $this->resetAfterTest(); + + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a student. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); // Create a choice. $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id, 'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS)); - $context = context_module::instance($choice->cmid); $cm = get_coursemodule_from_instance('choice', $choice->id); $choicewithoptions = choice_get_choice($choice->id); @@ -380,6 +408,45 @@ class mod_choice_lib_testcase extends externallib_advanced_testcase { $this->assertNull($action); } + /** + * An event should not have an action if the user has already submitted a response + * to the choice activity. + */ + public function test_choice_core_calendar_provide_event_action_already_submitted_for_user() { + $this->resetAfterTest(); + + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a student. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // Create a choice. + $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id, + 'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS)); + $cm = get_coursemodule_from_instance('choice', $choice->id); + + $choicewithoptions = choice_get_choice($choice->id); + $optionids = array_keys($choicewithoptions->option); + + choice_user_submit_response($optionids[0], $choice, $student->id, $course, $cm); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $choice->id, CHOICE_EVENT_TYPE_OPEN); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event for the student. + $action = mod_choice_core_calendar_provide_event_action($event, $factory, $student->id); + + // Confirm no action was returned if the user has already submitted the + // choice activity. + $this->assertNull($action); + } + public function test_choice_core_calendar_provide_event_action_closed() { $this->resetAfterTest(); @@ -406,6 +473,35 @@ class mod_choice_lib_testcase extends externallib_advanced_testcase { $this->assertNull($action); } + public function test_choice_core_calendar_provide_event_action_closed_for_user() { + $this->resetAfterTest(); + + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a student. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + $timeclose = time() - DAYSECS; + // Create a choice. + $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id, + 'timeclose' => $timeclose)); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $choice->id, CHOICE_EVENT_TYPE_OPEN, $timeclose - 1); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event for the student. + $action = mod_choice_core_calendar_provide_event_action($event, $factory, $student->id); + + // Confirm not action was provided for a closed activity. + $this->assertNull($action); + } + public function test_choice_core_calendar_provide_event_action_open_in_future() { $this->resetAfterTest(); @@ -438,6 +534,47 @@ class mod_choice_lib_testcase extends externallib_advanced_testcase { $this->assertFalse($actionevent->is_actionable()); } + public function test_choice_core_calendar_provide_event_action_open_in_future_for_user() { + global $CFG; + + $this->resetAfterTest(); + + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a student. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + $timeopen = time() + DAYSECS; + $timeclose = $timeopen + DAYSECS; + + // Create a choice. + $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id, + 'timeopen' => $timeopen, 'timeclose' => $timeclose)); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $choice->id, CHOICE_EVENT_TYPE_OPEN, $timeopen); + + // Now, log out. + $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. + $this->setUser(); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event for the student. + $actionevent = mod_choice_core_calendar_provide_event_action($event, $factory, $student->id); + + // Confirm the event was decorated. + $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); + $this->assertEquals(get_string('viewchoices', 'choice'), $actionevent->get_name()); + $this->assertInstanceOf('moodle_url', $actionevent->get_url()); + $this->assertEquals(1, $actionevent->get_item_count()); + $this->assertFalse($actionevent->is_actionable()); + } + public function test_choice_core_calendar_provide_event_action_no_time_specified() { $this->resetAfterTest(); @@ -466,6 +603,43 @@ class mod_choice_lib_testcase extends externallib_advanced_testcase { $this->assertTrue($actionevent->is_actionable()); } + public function test_choice_core_calendar_provide_event_action_no_time_specified_for_user() { + global $CFG; + + $this->resetAfterTest(); + + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a student. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // Create a choice. + $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id)); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $choice->id, CHOICE_EVENT_TYPE_OPEN); + + // Now, log out. + $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. + $this->setUser(); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event for the student. + $actionevent = mod_choice_core_calendar_provide_event_action($event, $factory, $student->id); + + // Confirm the event was decorated. + $this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent); + $this->assertEquals(get_string('viewchoices', 'choice'), $actionevent->get_name()); + $this->assertInstanceOf('moodle_url', $actionevent->get_url()); + $this->assertEquals(1, $actionevent->get_item_count()); + $this->assertTrue($actionevent->is_actionable()); + } + /** * Creates an action event. * From a8df987e07e62768d7b4d0b59f4e8778cf917b71 Mon Sep 17 00:00:00 2001 From: Shamim Rezaie Date: Thu, 6 Sep 2018 20:14:04 +1000 Subject: [PATCH 3/3] MDL-63135 mod_choice: Check if the module is visible to the user --- mod/choice/lib.php | 6 +++ mod/choice/tests/lib_test.php | 74 +++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) diff --git a/mod/choice/lib.php b/mod/choice/lib.php index 72ecc8f2da3..3256a8c1f97 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -1242,6 +1242,12 @@ function mod_choice_core_calendar_provide_event_action(calendar_event $event, } $cm = get_fast_modinfo($event->courseid, $userid)->instances['choice'][$event->instance]; + + if (!$cm->uservisible) { + // The module is not visible to the user for any reason. + return null; + } + $now = time(); if (!empty($cm->customdata['timeclose']) && $cm->customdata['timeclose'] < $now) { diff --git a/mod/choice/tests/lib_test.php b/mod/choice/tests/lib_test.php index a17c456d977..c34af8c7230 100644 --- a/mod/choice/tests/lib_test.php +++ b/mod/choice/tests/lib_test.php @@ -306,6 +306,80 @@ class mod_choice_lib_testcase extends externallib_advanced_testcase { $this->assertEquals('expired', array_keys($warnings)[0]); } + /* + * The choice's event should not be shown to a user when the user cannot view the choice activity at all. + */ + public function test_choice_core_calendar_provide_event_action_in_hidden_section() { + global $CFG; + + $this->resetAfterTest(); + + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a student. + $student = $this->getDataGenerator()->create_and_enrol($course, 'student'); + + // Create a choice. + $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id, + 'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS)); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $choice->id, CHOICE_EVENT_TYPE_OPEN); + + // Set sections 0 as hidden. + set_section_visible($course->id, 0, 0); + + // Now, log out. + $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. + $this->setUser(); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event for the student. + $actionevent = mod_choice_core_calendar_provide_event_action($event, $factory, $student->id); + + // Confirm the event is not shown at all. + $this->assertNull($actionevent); + } + + /* + * The choice's event should not be shown to a user who does not have permission to view the choice. + */ + public function test_choice_core_calendar_provide_event_action_for_non_user() { + global $CFG; + + $this->resetAfterTest(); + + $this->setAdminUser(); + + // Create a course. + $course = $this->getDataGenerator()->create_course(); + + // Create a choice. + $choice = $this->getDataGenerator()->create_module('choice', array('course' => $course->id, + 'timeopen' => time() - DAYSECS, 'timeclose' => time() + DAYSECS)); + + // Create a calendar event. + $event = $this->create_action_event($course->id, $choice->id, CHOICE_EVENT_TYPE_OPEN); + + // Now, log out. + $CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities. + $this->setUser(); + + // Create an action factory. + $factory = new \core_calendar\action_factory(); + + // Decorate action event. + $actionevent = mod_choice_core_calendar_provide_event_action($event, $factory); + + // Confirm the event is not shown at all. + $this->assertNull($actionevent); + } + public function test_choice_core_calendar_provide_event_action_open() { $this->resetAfterTest();