MDL-52767 mod_quiz: New Web Service get_quiz_required_qtypes
authorJuan Leyva <juanleyvadelgado@gmail.com>
Fri, 11 Mar 2016 14:46:09 +0000 (15:46 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 22 Mar 2016 23:57:07 +0000 (00:57 +0100)
mod/quiz/classes/external.php
mod/quiz/db/services.php
mod/quiz/tests/external_test.php
mod/quiz/version.php

index 79be9ea..6e364e6 100644 (file)
@@ -1808,4 +1808,66 @@ class mod_quiz_external extends external_api {
         );
     }
 
+    /**
+     * Describes the parameters for get_quiz_required_qtypes.
+     *
+     * @return external_external_function_parameters
+     * @since Moodle 3.1
+     */
+    public static function get_quiz_required_qtypes_parameters() {
+        return new external_function_parameters (
+            array(
+                'quizid' => new external_value(PARAM_INT, 'quiz instance id')
+            )
+        );
+    }
+
+    /**
+     * Return the potential question types that would be required for a given quiz.
+     * Please note that for random question types we return the potential question types in the category choosen.
+     *
+     * @param int $quizid quiz instance id
+     * @return array of warnings and the access information
+     * @since Moodle 3.1
+     * @throws  moodle_quiz_exception
+     */
+    public static function get_quiz_required_qtypes($quizid) {
+        global $DB, $USER;
+
+        $warnings = array();
+
+        $params = array(
+            'quizid' => $quizid
+        );
+        $params = self::validate_parameters(self::get_quiz_required_qtypes_parameters(), $params);
+
+        list($quiz, $course, $cm, $context) = self::validate_quiz($params['quizid']);
+
+        $quizobj = quiz::create($cm->instance, $USER->id);
+        $quizobj->preload_questions();
+        $quizobj->load_questions();
+
+        // Question types used.
+        $result = array();
+        $result['questiontypes'] = $quizobj->get_all_question_types_used(true);
+        $result['warnings'] = $warnings;
+        return $result;
+    }
+
+    /**
+     * Describes the get_quiz_required_qtypes return value.
+     *
+     * @return external_single_structure
+     * @since Moodle 3.1
+     */
+    public static function get_quiz_required_qtypes_returns() {
+        return new external_single_structure(
+            array(
+                'questiontypes' => new external_multiple_structure(
+                                    new external_value(PARAM_PLUGIN, 'question type'), 'list of question types used in the quiz'),
+                'warnings' => new external_warnings(),
+            )
+        );
+    }
+
 }
index c2c062d..d67c240 100644 (file)
@@ -182,4 +182,13 @@ $functions = array(
         'capabilities'  => 'mod/quiz:view',
         'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
     ),
+
+    'mod_quiz_get_quiz_required_qtypes' => array(
+        'classname'     => 'mod_quiz_external',
+        'methodname'    => 'get_quiz_required_qtypes',
+        'description'   => 'Return the potential question types that would be required for a given quiz.',
+        'type'          => 'read',
+        'capabilities'  => 'mod/quiz:view',
+        'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
+    ),
 );
index 6a1245a..741a92b 100644 (file)
@@ -1546,4 +1546,46 @@ class mod_quiz_external_testcase extends externallib_advanced_testcase {
         $this->assertEquals(get_string('nomoreattempts', 'quiz'), $result['preventnewattemptreasons'][0]);
 
     }
+
+    /**
+     * Test get_quiz_required_qtypes
+     */
+    public function test_get_quiz_required_qtypes() {
+        global $DB;
+
+        // Create a new quiz.
+        $quizgenerator = $this->getDataGenerator()->get_plugin_generator('mod_quiz');
+        $data = array('course' => $this->course->id);
+        $quiz = $quizgenerator->create_instance($data);
+
+        // Create some questions.
+        $questiongenerator = $this->getDataGenerator()->get_plugin_generator('core_question');
+
+        $cat = $questiongenerator->create_question_category();
+        $question = $questiongenerator->create_question('numerical', null, array('category' => $cat->id));
+        quiz_add_quiz_question($question->id, $quiz);
+
+        $question = $questiongenerator->create_question('shortanswer', null, array('category' => $cat->id));
+        quiz_add_quiz_question($question->id, $quiz);
+
+        // Add new question types in the category (for the random one).
+        $question = $questiongenerator->create_question('truefalse', null, array('category' => $cat->id));
+        $question = $questiongenerator->create_question('essay', null, array('category' => $cat->id));
+
+        $question = $questiongenerator->create_question('random', null, array('category' => $cat->id));
+        quiz_add_quiz_question($question->id, $quiz);
+
+        $this->setUser($this->student);
+
+        $result = mod_quiz_external::get_quiz_required_qtypes($quiz->id);
+        $result = external_api::clean_returnvalue(mod_quiz_external::get_quiz_required_qtypes_returns(), $result);
+
+        $expected = array(
+            'questiontypes' => ['essay', 'numerical', 'random', 'shortanswer', 'truefalse'],
+            'warnings' => []
+        );
+
+        $this->assertEquals($expected, $result);
+
+    }
 }
index 5b07a66..3889380 100644 (file)
@@ -24,7 +24,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2016032108;
+$plugin->version   = 2016032109;
 $plugin->requires  = 2015111000;
 $plugin->component = 'mod_quiz';
 $plugin->cron      = 60;