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. *