From 4c4d3b7303d2db530ebaededaca3e7f0d817b4be Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Wed, 29 Jul 2015 11:29:19 +0200 Subject: [PATCH] MDL-50944 mod_choice: New Web Service mod_choice_view_choice --- lib/db/services.php | 1 + mod/choice/classes/external.php | 66 ++++++++++++++++++++++++++++++++- mod/choice/db/services.php | 8 ++++ mod/choice/lib.php | 28 ++++++++++++++ mod/choice/view.php | 12 ++---- 5 files changed, 105 insertions(+), 10 deletions(-) diff --git a/lib/db/services.php b/lib/db/services.php index 7ed63ff6135..f50b6ed5b60 100644 --- a/lib/db/services.php +++ b/lib/db/services.php @@ -1147,6 +1147,7 @@ $services = array( 'mod_choice_get_choice_results', 'mod_choice_get_choice_options', 'mod_choice_submit_choice_response', + 'mod_choice_view_choice', ), 'enabled' => 0, 'restrictedusers' => 0, diff --git a/mod/choice/classes/external.php b/mod/choice/classes/external.php index 60571750cfb..7a835a1a89a 100644 --- a/mod/choice/classes/external.php +++ b/mod/choice/classes/external.php @@ -207,7 +207,7 @@ class mod_choice_external extends external_api { $choiceopen = true; $showpreview = false; - if ($choice->timeclose !== 0) { + if ($choice->timeclose != 0) { if ($choice->timeopen > $timenow) { $choiceopen = false; $warnings[1] = get_string("notopenyet", "choice", userdate($choice->timeopen)); @@ -387,4 +387,68 @@ class mod_choice_external extends external_api { ); } + /** + * Returns description of method parameters + * + * @return external_function_parameters + * @since Moodle 3.0 + */ + public static function view_choice_parameters() { + return new external_function_parameters( + array( + 'choiceid' => new external_value(PARAM_INT, 'choice instance id') + ) + ); + } + + /** + * Trigger the course module viewed event and update the module completion status. + * + * @param int $choiceid the choice instance id + * @return array of warnings and status result + * @since Moodle 3.0 + * @throws moodle_exception + */ + public static function view_choice($choiceid) { + global $CFG; + + $params = self::validate_parameters(self::view_choice_parameters(), + array( + 'choiceid' => $choiceid + )); + $warnings = array(); + + // Request and permission validation. + 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); + + // Trigger course_module_viewed event and completion. + choice_view($choice, $course, $cm, $context); + + $result = array(); + $result['status'] = true; + $result['warnings'] = $warnings; + return $result; + } + + /** + * Returns description of method result value + * + * @return external_description + * @since Moodle 3.0 + */ + public static function view_choice_returns() { + return new external_single_structure( + array( + 'status' => new external_value(PARAM_BOOL, 'status: true if success'), + 'warnings' => new external_warnings() + ) + ); + } + } diff --git a/mod/choice/db/services.php b/mod/choice/db/services.php index 1be2665133e..bbe3181b585 100644 --- a/mod/choice/db/services.php +++ b/mod/choice/db/services.php @@ -51,4 +51,12 @@ $functions = array( 'type' => 'write', 'capabilities' => 'mod/choice:choose' ), + + 'mod_choice_view_choice' => array( + 'classname' => 'mod_choice_external', + 'methodname' => 'view_choice', + 'description' => 'Trigger the course module viewed event and update the module completion status.', + 'type' => 'write', + 'capabilities' => '' + ), ); diff --git a/mod/choice/lib.php b/mod/choice/lib.php index 51cda3b0f9c..b04ce600982 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -965,3 +965,31 @@ function choice_can_view_results($choice, $current = null, $choiceopen = null) { } return false; } + +/** + * Mark the activity completed (if required) and trigger the course_module_viewed event. + * + * @param stdClass $choice choice object + * @param stdClass $course course object + * @param stdClass $cm course module object + * @param stdClass $context context object + * @since Moodle 3.0 + */ +function choice_view($choice, $course, $cm, $context) { + + // Trigger course_module_viewed event. + $params = array( + 'context' => $context, + 'objectid' => $choice->id + ); + + $event = \mod_choice\event\course_module_viewed::create($params); + $event->add_record_snapshot('course_modules', $cm); + $event->add_record_snapshot('course', $course); + $event->add_record_snapshot('choice', $choice); + $event->trigger(); + + // Completion. + $completion = new completion_info($course); + $completion->set_module_viewed($cm); +} diff --git a/mod/choice/view.php b/mod/choice/view.php index 6c3c53af7c3..d40bebaea96 100644 --- a/mod/choice/view.php +++ b/mod/choice/view.php @@ -51,10 +51,6 @@ if ($action == 'delchoice' and confirm_sesskey() and is_enrolled($context, NULL, $PAGE->set_title($choice->name); $PAGE->set_heading($course->fullname); -// Mark viewed by user (if required) -$completion = new completion_info($course); -$completion->set_module_viewed($cm); - /// Submit any new data if there is any if (data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && confirm_sesskey()) { $timenow = time(); @@ -83,6 +79,9 @@ if (data_submitted() && is_enrolled($context, NULL, 'mod/choice:choose') && conf } } +// Completion and trigger events. +choice_view($choice, $course, $cm, $context); + echo $OUTPUT->header(); echo $OUTPUT->heading(format_string($choice->name), 2, null); @@ -99,11 +98,6 @@ $eventdata = array(); $eventdata['objectid'] = $choice->id; $eventdata['context'] = $context; -$event = \mod_choice\event\course_module_viewed::create($eventdata); -$event->add_record_snapshot('course_modules', $cm); -$event->add_record_snapshot('course', $course); -$event->trigger(); - /// Check to see if groups are being used in this choice $groupmode = groups_get_activity_groupmode($cm); -- 2.43.0