From 100645941574df5d916dfa436dbc0a3682d065a9 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Wed, 29 Jul 2015 11:19:39 +0200 Subject: [PATCH] MDL-50944 mod_choice: New Web Service mod_choice_submit_choice_response --- lib/db/services.php | 1 + mod/choice/classes/external.php | 98 +++++++++++++++++++++++++++++++++ mod/choice/db/services.php | 8 +++ 3 files changed, 107 insertions(+) diff --git a/lib/db/services.php b/lib/db/services.php index 3c9e3a440d4..7ed63ff6135 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -1146,6 +1146,7 @@ $services = array( 'mod_folder_view_folder', 'mod_choice_get_choice_results', 'mod_choice_get_choice_options', + 'mod_choice_submit_choice_response', ), 'enabled' => 0, 'restrictedusers' => 0, diff --git a/mod/choice/classes/external.php b/mod/choice/classes/external.php index 80d47e76c88..60571750cfb 100644 --- a/mod/choice/classes/external.php +++ b/mod/choice/classes/external.php @@ -289,4 +289,102 @@ class mod_choice_external extends external_api { ); } + /** + * Describes the parameters for submit_choice_response. + * + * @return external_external_function_parameters + * @since Moodle 3.0 + */ + public static function submit_choice_response_parameters() { + return new external_function_parameters ( + array( + 'choiceid' => new external_value(PARAM_INT, 'choice instance id'), + 'responses' => new external_multiple_structure( + new external_value(PARAM_INT, 'answer id'), + 'Array of response ids' + ), + ) + ); + } + + /** + * Submit choice responses + * + * @param int $choiceid the choice instance id + * @param array $responses the response ids + * @return array ansers informatinon and warnings + * @since Moodle 3.0 + */ + public static function submit_choice_response($choiceid, $responses) { + global $USER; + + $warnings = array(); + $params = self::validate_parameters(self::submit_choice_response_parameters(), + array( + 'choiceid' => $choiceid, + 'responses' => $responses + )); + + if (!$choice = choice_get_choice($params['choiceid'])) { + throw new moodle_exception("invalidcoursemodule", "error"); + } + list($course, $cm) = get_course_and_cm_from_instance($choice, 'choice'); + + $context = context_module::instance($cm->id); + self::validate_context($context); + + require_capability('mod/choice:choose', $context); + + $timenow = time(); + if ($choice->timeclose != 0) { + if ($choice->timeopen > $timenow) { + throw new moodle_exception("notopenyet", "choice", '', userdate($choice->timeopen)); + } else if ($timenow > $choice->timeclose) { + throw new moodle_exception("expired", "choice", '', userdate($choice->timeclose)); + } + } + if (!choice_get_my_response($choice) or $choice->allowupdate) { + // When a single response is given, we convert the array to a simple variable + // in order to avoid choice_user_submit_response to check with allowmultiple even + // for a single response. + if (count($params['responses']) == 1) { + $params['responses'] = reset($params['responses']); + } + choice_user_submit_response($params['responses'], $choice, $USER->id, $course, $cm); + } else { + throw new moodle_exception('missingrequiredcapability', 'webservice', '', 'allowupdate'); + } + $answers = choice_get_my_response($choice); + + return array( + 'answers' => $answers, + 'warnings' => $warnings + ); + } + + /** + * Describes the submit_choice_response return value. + * + * @return external_multiple_structure + * @since Moodle 3.0 + */ + public static function submit_choice_response_returns() { + return new external_single_structure( + array( + 'answers' => new external_multiple_structure( + new external_single_structure( + array( + 'id' => new external_value(PARAM_INT, 'answer id'), + 'choiceid' => new external_value(PARAM_INT, 'choiceid'), + 'userid' => new external_value(PARAM_INT, 'user id'), + 'optionid' => new external_value(PARAM_INT, 'optionid'), + 'timemodified' => new external_value(PARAM_INT, 'time of last modification') + ), 'Answers' + ) + ), + 'warnings' => new external_warnings(), + ) + ); + } + } diff --git a/mod/choice/db/services.php b/mod/choice/db/services.php index bdd0a4470fb..1be2665133e 100644 --- a/mod/choice/db/services.php +++ b/mod/choice/db/services.php @@ -43,4 +43,12 @@ $functions = array( 'type' => 'read', 'capabilities' => 'mod/choice:choose' ), + + 'mod_choice_submit_choice_response' => array( + 'classname' => 'mod_choice_external', + 'methodname' => 'submit_choice_response', + 'description' => 'Submit responses to a specific choice item.', + 'type' => 'write', + 'capabilities' => 'mod/choice:choose' + ), ); -- 2.43.0