diff --git a/mod/quiz/classes/external.php b/mod/quiz/classes/external.php index 79be9ea2f4f..6e364e6bfe6 100644 --- a/mod/quiz/classes/external.php +++ b/mod/quiz/classes/external.php @@ -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(), + ) + ); + } + } diff --git a/mod/quiz/db/services.php b/mod/quiz/db/services.php index c2c062d2182..d67c240e9c3 100644 --- a/mod/quiz/db/services.php +++ b/mod/quiz/db/services.php @@ -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) + ), ); diff --git a/mod/quiz/tests/external_test.php b/mod/quiz/tests/external_test.php index 6a1245afd3f..741a92bf674 100644 --- a/mod/quiz/tests/external_test.php +++ b/mod/quiz/tests/external_test.php @@ -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); + + } } diff --git a/mod/quiz/version.php b/mod/quiz/version.php index 5b07a667eb2..38893804c31 100644 --- a/mod/quiz/version.php +++ b/mod/quiz/version.php @@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016032108; +$plugin->version = 2016032109; $plugin->requires = 2015111000; $plugin->component = 'mod_quiz'; $plugin->cron = 60;