MDL-50944 mod_choice: New Web Service mod_choice_view_choice
authorJuan Leyva <juanleyvadelgado@gmail.com>
Wed, 29 Jul 2015 09:29:19 +0000 (11:29 +0200)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Wed, 2 Sep 2015 08:35:37 +0000 (10:35 +0200)
lib/db/services.php
mod/choice/classes/external.php
mod/choice/db/services.php
mod/choice/lib.php
mod/choice/view.php

index 7ed63ff..f50b6ed 100644 (file)
@@ -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,
index 6057175..7a835a1 100644 (file)
@@ -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()
+            )
+        );
+    }
+
 }
index 1be2665..bbe3181 100644 (file)
@@ -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'  => ''
+    ),
 );
index 51cda3b..b04ce60 100644 (file)
@@ -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);
+}
index 6c3c53a..d40beba 100644 (file)
@@ -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);