MDL-52767 mod_quiz: New Web Service get_quiz_required_qtypes

This commit is contained in:
Juan Leyva 2016-03-11 15:46:09 +01:00 committed by Eloy Lafuente (stronk7)
parent b6c538416b
commit efca239e17
4 changed files with 114 additions and 1 deletions

View 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(),
)
);
}
}

View 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)
),
);

View 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);
}
}

View 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;