MDL-59239 mod_workshop: New WS mod_workshop_view_submission
authorJuan Leyva <juanleyvadelgado@gmail.com>
Fri, 8 Sep 2017 10:07:20 +0000 (12:07 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 3 Oct 2017 17:25:32 +0000 (19:25 +0200)
mod/workshop/classes/external.php
mod/workshop/db/services.php
mod/workshop/locallib.php
mod/workshop/submission.php
mod/workshop/tests/external_test.php
mod/workshop/version.php

index 2f0145d..70063c4 100644 (file)
@@ -1918,4 +1918,62 @@ class mod_workshop_external extends external_api {
             )
         );
     }
+
+    /**
+     * Describes the parameters for view_submission.
+     *
+     * @return external_function_parameters
+     * @since Moodle 3.4
+     */
+    public static function view_submission_parameters() {
+        return new external_function_parameters (
+            array(
+                'submissionid' => new external_value(PARAM_INT, 'Submission id'),
+            )
+        );
+    }
+
+    /**
+     * Trigger the submission viewed event.
+     *
+     * @param int $submissionid submission id
+     * @return array of warnings and status result
+     * @since Moodle 3.4
+     * @throws moodle_exception
+     */
+    public static function view_submission($submissionid) {
+        global $DB;
+
+        $params = self::validate_parameters(self::view_submission_parameters(), array('submissionid' => $submissionid));
+        $warnings = array();
+
+        // Get and validate the submission and workshop.
+        $submission = $DB->get_record('workshop_submissions', array('id' => $params['submissionid']), '*', MUST_EXIST);
+        list($workshop, $course, $cm, $context) = self::validate_workshop($submission->workshopid);
+
+        self::validate_submission($submission, $workshop);
+
+        $workshop->set_submission_viewed($submission);
+
+        $result = array(
+            'status' => true,
+            'warnings' => $warnings,
+        );
+        return $result;
+    }
+
+    /**
+     * Describes the view_submission return value.
+     *
+     * @return external_single_structure
+     * @since Moodle 3.4
+     */
+    public static function view_submission_returns() {
+        return new external_single_structure(
+            array(
+                'status' => new external_value(PARAM_BOOL, 'status: true if success'),
+                'warnings' => new external_warnings(),
+            )
+        );
+    }
 }
index e9bf366..d25ce62 100644 (file)
@@ -155,4 +155,12 @@ $functions = array(
         'type'          => 'read',
         'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
     ),
+    'mod_workshop_view_submission' => array(
+        'classname'     => 'mod_workshop_external',
+        'methodname'    => 'view_submission',
+        'description'   => 'Trigger the submission viewed event.',
+        'type'          => 'write',
+        'capabilities'  => 'mod/workshop:view',
+        'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
+    ),
 );
index 926ef7d..6de355d 100644 (file)
@@ -3118,6 +3118,27 @@ class workshop {
         $DB->update_record('workshop_assessments', $record);
     }
 
+    /**
+     * Trigger submission viewed event.
+     *
+     * @param stdClass $submission submission object
+     * @since  Moodle 3.4
+     */
+    public function set_submission_viewed($submission) {
+        $params = array(
+            'objectid' => $submission->id,
+            'context' => $this->context,
+            'courseid' => $this->course->id,
+            'relateduserid' => $submission->authorid,
+            'other' => array(
+                'workshopid' => $this->id
+            )
+        );
+
+        $event = \mod_workshop\event\submission_viewed::create($params);
+        $event->trigger();
+    }
+
     ////////////////////////////////////////////////////////////////////////////////
     // Internal methods (implementation details)                                  //
     ////////////////////////////////////////////////////////////////////////////////
index f93967b..5acbd59 100644 (file)
@@ -51,19 +51,8 @@ if ($edit) {
 
 if ($id) { // submission is specified
     $submission = $workshop->get_submission_by_id($id);
-
-    $params = array(
-        'objectid' => $submission->id,
-        'context' => $workshop->context,
-        'courseid' => $workshop->course->id,
-        'relateduserid' => $submission->authorid,
-        'other' => array(
-            'workshopid' => $workshop->id
-        )
-    );
-
-    $event = \mod_workshop\event\submission_viewed::create($params);
-    $event->trigger();
+    // Trigger submission viewed event.
+    $workshop->set_submission_viewed($submission);
 
 } else { // no submission specified
     if (!$submission = $workshop->get_submission_by_author($USER->id)) {
index 2657a96..2ccc99e 100644 (file)
@@ -1738,4 +1738,34 @@ class mod_workshop_external_testcase extends externallib_advanced_testcase {
         $this->expectException('required_capability_exception');
         mod_workshop_external::get_grades_report($this->workshop->id);
     }
+
+    /**
+     * Test test_view_submission.
+     */
+    public function test_view_submission() {
+
+        // Create a couple of submissions with files.
+        $firstsubmissionid = $this->create_test_submission($this->student);  // Create submission with files.
+
+        // Trigger and capture the event.
+        $sink = $this->redirectEvents();
+
+        $this->setUser($this->student);
+        $result = mod_workshop_external::view_submission($firstsubmissionid);
+        $result = external_api::clean_returnvalue(mod_workshop_external::view_submission_returns(), $result);
+
+        $events = $sink->get_events();
+        $this->assertCount(1, $events);
+        $event = array_shift($events);
+
+        // Checking that the event contains the expected values.
+        $this->assertInstanceOf('\mod_workshop\event\submission_viewed', $event);
+        $this->assertEquals($this->context, $event->get_context());
+        $moodleworkshop = new \moodle_url('/mod/workshop/submission.php', array('id' => $firstsubmissionid,
+            'cmid' => $this->cm->id));
+        $this->assertEquals($moodleworkshop, $event->get_url());
+        $this->assertEventContextNotUsed($event);
+        $this->assertNotEmpty($event->get_name());
+
+    }
 }
index bb1bc3a..d3cb91c 100644 (file)
@@ -24,7 +24,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2017051517;        // The current module version (YYYYMMDDXX)
+$plugin->version   = 2017051518;        // The current module version (YYYYMMDDXX)
 $plugin->requires  = 2017050500;        // Requires this Moodle version.
 $plugin->component = 'mod_workshop';
 $plugin->cron      = 60;                // Give as a chance every minute.