Merge branch 'MDL-63135-master' of git://github.com/rezaies/moodle
authorDavid Monllao <davidm@moodle.com>
Wed, 19 Sep 2018 11:12:40 +0000 (13:12 +0200)
committerDavid Monllao <davidm@moodle.com>
Wed, 19 Sep 2018 11:12:40 +0000 (13:12 +0200)
mod/choice/lib.php
mod/choice/tests/lib_test.php

index 3953d43..3256a8c 100644 (file)
@@ -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);
 }
 
 
@@ -1216,12 +1229,25 @@ 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;
+
+    if (!$userid) {
+        $userid = $USER->id;
+    }
+
+    $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;
+    }
 
-    $cm = get_fast_modinfo($event->courseid)->instances['choice'][$event->instance];
     $now = time();
 
     if (!empty($cm->customdata['timeclose']) && $cm->customdata['timeclose'] < $now) {
@@ -1233,7 +1259,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;
     }
index 72a32fe..316e0a6 100644 (file)
@@ -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
@@ -265,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();
 
@@ -294,28 +409,56 @@ class mod_choice_lib_testcase extends externallib_advanced_testcase {
         $this->assertTrue($actionevent->is_actionable());
     }
 
+    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 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() {
-        global $DB;
-
         $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));
-        $context = context_module::instance($choice->cmid);
         $cm = get_coursemodule_from_instance('choice', $choice->id);
 
         $choicewithoptions = choice_get_choice($choice->id);
@@ -339,6 +482,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();
 
@@ -365,6 +547,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();
 
@@ -397,6 +608,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();
 
@@ -425,6 +677,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.
      *