MDL-50132 mod_choice: New WS mod_choice_get_choices_by_courses
authorJuan Leyva <juanleyvadelgado@gmail.com>
Wed, 16 Sep 2015 09:02:20 +0000 (11:02 +0200)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Mon, 21 Sep 2015 07:27:42 +0000 (09:27 +0200)
lib/db/services.php
mod/choice/classes/external.php
mod/choice/db/services.php
mod/choice/tests/externallib_test.php
mod/choice/version.php
version.php

index 0d279b0..ae5b87d 100644 (file)
@@ -1226,6 +1226,7 @@ $services = array(
             'mod_choice_get_choice_options',
             'mod_choice_submit_choice_response',
             'mod_choice_view_choice',
+            'mod_choice_get_choices_by_courses',
             'mod_imscp_view_imscp',
             ),
         'enabled' => 0,
index 7a835a1..3332e61 100644 (file)
@@ -451,4 +451,137 @@ class mod_choice_external extends external_api {
         );
     }
 
+    /**
+     * Describes the parameters for get_choices_by_courses.
+     *
+     * @return external_external_function_parameters
+     * @since Moodle 3.0
+     */
+    public static function get_choices_by_courses_parameters() {
+        return new external_function_parameters (
+            array(
+                'courseids' => new external_multiple_structure(
+                    new external_value(PARAM_INT, 'course id'), 'Array of course ids', VALUE_DEFAULT, array()
+                ),
+            )
+        );
+    }
+
+    /**
+     * Returns a list of choices in a provided list of courses,
+     * if no list is provided all choices that the user can view will be returned.
+     *
+     * @param array $courseids the course ids
+     * @return array of choices details
+     * @since Moodle 3.0
+     */
+    public static function get_choices_by_courses($courseids = array()) {
+        global $CFG;
+
+        $returnedchoices = array();
+        $warnings = array();
+
+        $params = self::validate_parameters(self::get_choices_by_courses_parameters(), array('courseids' => $courseids));
+
+        if (empty($params['courseids'])) {
+            $params['courseids'] = array_keys(enrol_get_my_courses());
+        }
+
+        // Ensure there are courseids to loop through.
+        if (!empty($params['courseids'])) {
+
+            list($courses, $warnings) = external_util::validate_courses($params['courseids']);
+
+            // Get the choices in this course, this function checks users visibility permissions.
+            // We can avoid then additional validate_context calls.
+            $choices = get_all_instances_in_courses("choice", $courses);
+            foreach ($choices as $choice) {
+                $context = context_module::instance($choice->coursemodule);
+                // Entry to return.
+                $choicedetails = array();
+                // First, we return information that any user can see in the web interface.
+                $choicedetails['id'] = $choice->id;
+                $choicedetails['coursemodule'] = $choice->coursemodule;
+                $choicedetails['course'] = $choice->course;
+                $choicedetails['name']  = format_string($choice->name, true, array('context' => $context));;
+                // Format intro.
+                list($choicedetails['intro'], $choicedetails['introformat']) =
+                    external_format_text($choice->intro, $choice->introformat,
+                                            $context->id, 'mod_choice', 'intro', null);
+
+                if (has_capability('mod/choice:choose', $context)) {
+                    $choicedetails['publish']  = $choice->publish;
+                    $choicedetails['showresults']  = $choice->showresults;
+                    $choicedetails['showpreview']  = $choice->showpreview;
+                    $choicedetails['timeopen']  = $choice->timeopen;
+                    $choicedetails['timeclose']  = $choice->timeclose;
+                    $choicedetails['display']  = $choice->display;
+                    $choicedetails['allowupdate']  = $choice->allowupdate;
+                    $choicedetails['allowmultiple']  = $choice->allowmultiple;
+                    $choicedetails['limitanswers']  = $choice->limitanswers;
+                    $choicedetails['showunanswered']  = $choice->showunanswered;
+                    $choicedetails['includeinactive']  = $choice->includeinactive;
+                }
+
+                if (has_capability('moodle/course:manageactivities', $context)) {
+                    $choicedetails['timemodified']  = $choice->timemodified;
+                    $choicedetails['completionsubmit']  = $choice->completionsubmit;
+                    $choicedetails['section']  = $choice->section;
+                    $choicedetails['visible']  = $choice->visible;
+                    $choicedetails['groupmode']  = $choice->groupmode;
+                    $choicedetails['groupingid']  = $choice->groupingid;
+                }
+                $returnedchoices[] = $choicedetails;
+            }
+        }
+        $result = array();
+        $result['choices'] = $returnedchoices;
+        $result['warnings'] = $warnings;
+        return $result;
+    }
+
+    /**
+     * Describes the mod_choice_get_choices_by_courses return value.
+     *
+     * @return external_single_structure
+     * @since Moodle 3.0
+     */
+    public static function get_choices_by_courses_returns() {
+        return new external_single_structure(
+            array(
+                'choices' => new external_multiple_structure(
+                    new external_single_structure(
+                        array(
+                            'id' => new external_value(PARAM_INT, 'Choice instance id'),
+                            'coursemodule' => new external_value(PARAM_INT, 'Course module id'),
+                            'course' => new external_value(PARAM_INT, 'Course id'),
+                            'name' => new external_value(PARAM_TEXT, 'Choice name'),
+                            'intro' => new external_value(PARAM_RAW, 'The choice intro'),
+                            'introformat' => new external_format_value('intro'),
+                            'publish' => new external_value(PARAM_BOOL, 'If choice is published', VALUE_OPTIONAL),
+                            'showresults' => new external_value(PARAM_INT, '0 never, 1 after answer, 2 after close, 3 always',
+                                                                VALUE_OPTIONAL),
+                            'display' => new external_value(PARAM_INT, 'Display mode (vertical, horizontal)', VALUE_OPTIONAL),
+                            'allowupdate' => new external_value(PARAM_BOOL, 'Allow update', VALUE_OPTIONAL),
+                            'allowmultiple' => new external_value(PARAM_BOOL, 'Allow multiple choices', VALUE_OPTIONAL),
+                            'showunanswered' => new external_value(PARAM_BOOL, 'Show users who not answered yet', VALUE_OPTIONAL),
+                            'includeinactive' => new external_value(PARAM_BOOL, 'Include inactive users', VALUE_OPTIONAL),
+                            'limitanswers' => new external_value(PARAM_BOOL, 'Limit unswers', VALUE_OPTIONAL),
+                            'timeopen' => new external_value(PARAM_INT, 'Date of opening validity', VALUE_OPTIONAL),
+                            'timeclose' => new external_value(PARAM_INT, 'Date of closing validity', VALUE_OPTIONAL),
+                            'showpreview' => new external_value(PARAM_BOOL, 'Show preview before timeopen', VALUE_OPTIONAL),
+                            'timemodified' => new external_value(PARAM_INT, 'Time of last modification', VALUE_OPTIONAL),
+                            'completionsubmit' => new external_value(PARAM_BOOL, 'Completion on user submission', VALUE_OPTIONAL),
+                            'section' => new external_value(PARAM_INT, 'Course section id', VALUE_OPTIONAL),
+                            'visible' => new external_value(PARAM_BOOL, 'Visible', VALUE_OPTIONAL),
+                            'groupmode' => new external_value(PARAM_INT, 'Group mode', VALUE_OPTIONAL),
+                            'groupingid' => new external_value(PARAM_INT, 'Group id', VALUE_OPTIONAL),
+                        ), 'Choices'
+                    )
+                ),
+                'warnings' => new external_warnings(),
+            )
+        );
+    }
+
 }
index bbe3181..c88cf98 100644 (file)
@@ -59,4 +59,13 @@ $functions = array(
         'type'          => 'write',
         'capabilities'  => ''
     ),
+
+    'mod_choice_get_choices_by_courses' => array(
+        'classname'     => 'mod_choice_external',
+        'methodname'    => 'get_choices_by_courses',
+        'description'   => 'Returns a list of choice instances in a provided set of courses,
+                            if no courses are provided then all the choice instances the user has access to will be returned.',
+        'type'          => 'read',
+        'capabilities'  => ''
+    ),
 );
index f227c32..34bc06a 100644 (file)
@@ -350,4 +350,69 @@ class mod_choice_externallib_testcase extends externallib_advanced_testcase {
         $this->assertNotEmpty($event->get_name());
 
     }
+
+    /**
+     * Test get_choices_by_courses
+     */
+    public function test_get_choices_by_courses() {
+        global $DB;
+        $this->resetAfterTest(true);
+        // As admin.
+        $this->setAdminUser();
+        $course1 = self::getDataGenerator()->create_course();
+        $choiceoptions1 = array(
+          'course' => $course1->id,
+          'name' => 'First IMSCP'
+        );
+        $choice1 = self::getDataGenerator()->create_module('choice', $choiceoptions1);
+        $course2 = self::getDataGenerator()->create_course();
+
+        $choiceoptions2 = array(
+          'course' => $course2->id,
+          'name' => 'Second IMSCP'
+        );
+        $choice2 = self::getDataGenerator()->create_module('choice', $choiceoptions2);
+        $student1 = $this->getDataGenerator()->create_user();
+
+        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
+
+        // Enroll Student1 in Course1.
+        self::getDataGenerator()->enrol_user($student1->id,  $course1->id, $studentrole->id);
+
+        $this->setUser($student1);
+        $choices = mod_choice_external::get_choices_by_courses(array());
+        $choices = external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices);
+        $this->assertCount(1, $choices['choices']);
+        $this->assertEquals('First IMSCP', $choices['choices'][0]['name']);
+        // As Student you cannot see some IMSCP properties like 'section'.
+        $this->assertFalse(isset($choices['choices'][0]['section']));
+
+        // Student1 is not enrolled in this Course.
+        // The webservice will give a warning!
+        $choices = mod_choice_external::get_choices_by_courses(array($course2->id));
+        $choices = external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices);
+        $this->assertCount(0, $choices['choices']);
+        $this->assertEquals(1, $choices['warnings'][0]['warningcode']);
+
+        // Now as admin.
+        $this->setAdminUser();
+        // As Admin we can see this IMSCP.
+        $choices = mod_choice_external::get_choices_by_courses(array($course2->id));
+        $choices = external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices);
+        $this->assertCount(1, $choices['choices']);
+        $this->assertEquals('Second IMSCP', $choices['choices'][0]['name']);
+        // As an Admin you can see some IMSCP properties like 'section'.
+        $this->assertEquals(0, $choices['choices'][0]['section']);
+
+        // Now, prohibit capabilities.
+        $this->setUser($student1);
+        $contextcourse1 = context_course::instance($course1->id);
+        // Prohibit capability = mod:choice:choose on Course1 for students.
+        assign_capability('mod/choice:choose', CAP_PROHIBIT, $studentrole->id, $contextcourse1->id);
+        accesslib_clear_all_caches_for_unit_testing();
+
+        $choices = mod_choice_external::get_choices_by_courses(array($course1->id));
+        $choices = external_api::clean_returnvalue(mod_choice_external::get_choices_by_courses_returns(), $choices);
+        $this->assertFalse(isset($choices['choices'][0]['timeopen']));
+    }
 }
index 0263b8d..135d434 100644 (file)
@@ -24,7 +24,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2015051101;       // The current module version (Date: YYYYMMDDXX)
+$plugin->version   = 2015051102;       // The current module version (Date: YYYYMMDDXX)
 $plugin->requires  = 2015050500;    // Requires this Moodle version
 $plugin->component = 'mod_choice';     // Full name of the plugin (used for diagnostics)
 $plugin->cron      = 0;
index a827fdb..260b8fc 100644 (file)
@@ -29,7 +29,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$version  = 2015091800.00;              // YYYYMMDD      = weekly release date of this DEV branch.
+$version  = 2015091800.01;              // YYYYMMDD      = weekly release date of this DEV branch.
                                         //         RR    = release increments - 00 in DEV branches.
                                         //           .XX = incremental changes.