MDL-59401 mod_workshop: New WS get_reviewer_assessments
authorJuan Leyva <juanleyvadelgado@gmail.com>
Tue, 27 Jun 2017 16:18:11 +0000 (17:18 +0100)
committerJuan Leyva <juanleyvadelgado@gmail.com>
Tue, 3 Oct 2017 07:02:05 +0000 (09:02 +0200)
mod/workshop/classes/external.php
mod/workshop/db/services.php
mod/workshop/tests/external_test.php
mod/workshop/version.php

index 6a94cf1..ae00327 100644 (file)
@@ -1320,4 +1320,96 @@ class mod_workshop_external extends external_api {
             )
         );
     }
+
+    /**
+     * Returns the description of the external function parameters.
+     *
+     * @return external_function_parameters
+     * @since Moodle 3.4
+     */
+    public static function get_reviewer_assessments_parameters() {
+        return new external_function_parameters(
+            array(
+                'workshopid' => new external_value(PARAM_INT, 'Workshop instance id.'),
+                'userid' => new external_value(PARAM_INT, 'User id who did the assessment review (empty or 0 for current user).',
+                    VALUE_DEFAULT, 0),
+            )
+        );
+    }
+
+
+    /**
+     * Retrieves all the assessments reviewed by the given user.
+     *
+     * @param int $workshopid   the workshop instance id
+     * @param int $userid       the reviewer user id
+     * @return array containing the user assessments and warnings.
+     * @since Moodle 3.4
+     * @throws moodle_exception
+     */
+    public static function get_reviewer_assessments($workshopid, $userid = 0) {
+        global $USER, $DB, $PAGE;
+
+        $params = self::validate_parameters(
+            self::get_reviewer_assessments_parameters(), array('workshopid' => $workshopid, 'userid' => $userid)
+        );
+        $warnings = $assessments = array();
+
+        // Get and validate the submission and workshop.
+        list($workshop, $course, $cm, $context) = self::validate_workshop($params['workshopid']);
+
+        // Extra checks so only users with permissions can view other users assessments.
+        if (empty($params['userid']) || $params['userid'] == $USER->id) {
+            $userid = $USER->id;
+            list($assessed, $notice) = $workshop->check_examples_assessed_before_assessment($userid);
+            if (!$assessed) {
+                throw new moodle_exception($notice, 'mod_workshop');
+            }
+            if ($workshop->phase < workshop::PHASE_ASSESSMENT) {    // Can view assessments only in assessment phase onwards.
+                throw new moodle_exception('nopermissions', 'error', '', 'view assessments');
+            }
+        } else {
+            require_capability('mod/workshop:viewallassessments', $context);
+            $user = core_user::get_user($params['userid'], '*', MUST_EXIST);
+            core_user::require_active_user($user);
+            if (!$workshop->check_group_membership($user->id)) {
+                throw new moodle_exception('notingroup');
+            }
+            $userid = $user->id;
+        }
+        // Now get all my assessments (includes those pending review).
+        $assessmentsrecords = $workshop->get_assessments_by_reviewer($userid);
+
+        $related = array('context' => $context);
+        foreach ($assessmentsrecords as $assessment) {
+            $assessment = self::prepare_assessment_for_external($assessment, $workshop);
+            if (empty($assessment)) {
+                continue;
+            }
+            $exporter = new assessment_exporter($assessment, $related);
+            $assessments[] = $exporter->export($PAGE->get_renderer('core'));
+        }
+
+        return array(
+            'assessments' => $assessments,
+            'warnings' => $warnings
+        );
+    }
+
+    /**
+     * Returns description of method result value
+     *
+     * @return external_description
+     * @since Moodle 3.4
+     */
+    public static function get_reviewer_assessments_returns() {
+        return new external_single_structure(
+            array(
+                'assessments' => new external_multiple_structure(
+                    assessment_exporter::get_read_structure()
+                ),
+                'warnings' => new external_warnings()
+            )
+        );
+    }
 }
index 94b4c19..4521134 100644 (file)
@@ -120,4 +120,11 @@ $functions = array(
         'type'          => 'read',
         'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
     ),
+    'mod_workshop_get_user_assessments' => array(
+        'classname'     => 'mod_workshop_external',
+        'methodname'    => 'get_user_assessments',
+        'description'   => 'Retrieves all the assessments reviewed by the given user.',
+        'type'          => 'read',
+        'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
+    ),
 );
index c319b0a..e64fd8c 100644 (file)
@@ -1346,4 +1346,72 @@ class mod_workshop_external_testcase extends externallib_advanced_testcase {
         $this->setExpectedException('moodle_exception');
         mod_workshop_external::get_assessment_form_definition($assessmentid);
     }
+
+    /**
+     * Test get_reviewer_assessments.
+     */
+    public function test_get_reviewer_assessments() {
+        global $DB;
+
+        // Create the submission.
+        $submissionid1 = $this->create_test_submission($this->student);
+        $submissionid2 = $this->create_test_submission($this->anotherstudentg1);
+
+        $workshopgenerator = $this->getDataGenerator()->get_plugin_generator('mod_workshop');
+        $assessmentid1 = $workshopgenerator->create_assessment($submissionid1, $this->student->id, array(
+            'weight' => 2,
+            'grade' => 90,
+        ));
+        $assessmentid2 = $workshopgenerator->create_assessment($submissionid2, $this->student->id, array(
+            'weight' => 3,
+            'grade' => 80,
+        ));
+
+        // Switch to assessment phase.
+        $DB->set_field('workshop', 'phase', workshop::PHASE_ASSESSMENT, array('id' => $this->workshop->id));
+        $this->setUser($this->student);
+        // Get my assessments.
+        $result = mod_workshop_external::get_reviewer_assessments($this->workshop->id);
+        $result = external_api::clean_returnvalue(mod_workshop_external::get_reviewer_assessments_returns(), $result);
+        $this->assertCount(2, $result['assessments']);
+        foreach ($result['assessments'] as $assessment) {
+            if ($assessment['id'] == $assessmentid1) {
+                $this->assertEquals(90, $assessment['grade']);
+            } else {
+                $this->assertEquals($assessmentid2, $assessment['id']);
+                $this->assertEquals(80, $assessment['grade']);
+            }
+        }
+
+        // Now, as teacher try to get the same student assessments.
+        $result = mod_workshop_external::get_reviewer_assessments($this->workshop->id, $this->student->id);
+        $result = external_api::clean_returnvalue(mod_workshop_external::get_reviewer_assessments_returns(), $result);
+        $this->assertCount(2, $result['assessments']);
+    }
+
+    /**
+     * Test get_reviewer_assessments_other_student.
+     */
+    public function test_get_reviewer_assessments_other_student() {
+        global $DB;
+
+        $DB->set_field('workshop', 'phase', workshop::PHASE_ASSESSMENT, array('id' => $this->workshop->id));
+        // Try to get other user assessments.
+        $this->setUser($this->student);
+        $this->setExpectedException('moodle_exception');
+        mod_workshop_external::get_reviewer_assessments($this->workshop->id, $this->anotherstudentg1->id);
+    }
+
+    /**
+     * Test get_reviewer_assessments_invalid_phase.
+     */
+    public function test_get_reviewer_assessments_invalid_phase() {
+        global $DB;
+
+        $DB->set_field('workshop', 'phase', workshop::PHASE_SUBMISSION, array('id' => $this->workshop->id));
+        // Try to get other user assessments.
+        $this->setUser($this->student);
+        $this->setExpectedException('moodle_exception');
+        mod_workshop_external::get_reviewer_assessments($this->workshop->id, $this->anotherstudentg1->id);
+    }
 }
index 6bb09d7..db0082c 100644 (file)
@@ -24,7 +24,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2017051512;        // The current module version (YYYYMMDDXX)
+$plugin->version   = 2017051513;        // 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.