MDL-57921 mod_data: New WS mod_data_approve_entry
authorJuan Leyva <juanleyvadelgado@gmail.com>
Mon, 13 Feb 2017 14:55:04 +0000 (15:55 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Thu, 30 Mar 2017 00:12:53 +0000 (02:12 +0200)
mod/data/classes/external.php
mod/data/db/services.php
mod/data/tests/externallib_test.php
mod/data/version.php

index 6f6829e..08f844d 100644 (file)
@@ -810,4 +810,67 @@ class mod_data_external extends external_api {
             )
         );
     }
+
+    /**
+     * Returns description of method parameters
+     *
+     * @return external_function_parameters
+     * @since Moodle 3.3
+     */
+    public static function approve_entry_parameters() {
+        return new external_function_parameters(
+            array(
+                'entryid' => new external_value(PARAM_INT, 'Record entry id.'),
+                'approve' => new external_value(PARAM_BOOL, 'Whether to approve (true) or unapprove the entry.',
+                                                VALUE_DEFAULT, true),
+            )
+        );
+    }
+
+    /**
+     * Approves or unapproves an entry.
+     *
+     * @param int $entryid          the record entry id id
+     * @param bool $approve         whether to approve (true) or unapprove the entry
+     * @return array of warnings and the entries
+     * @since Moodle 3.3
+     * @throws moodle_exception
+     */
+    public static function approve_entry($entryid, $approve = true) {
+        global $PAGE, $DB;
+
+        $params = array('entryid' => $entryid, 'approve' => $approve);
+        $params = self::validate_parameters(self::approve_entry_parameters(), $params);
+        $warnings = array();
+
+        $record = $DB->get_record('data_records', array('id' => $params['entryid']), '*', MUST_EXIST);
+        list($database, $course, $cm, $context) = self::validate_database($record->dataid);
+        // Check database is open in time.
+        data_require_time_available($database, null, $context);
+        // Check specific capabilities.
+        require_capability('mod/data:approve', $context);
+
+        data_approve_entry($record->id, $params['approve']);
+
+        $result = array(
+            'status' => true,
+            'warnings' => $warnings
+        );
+        return $result;
+    }
+
+    /**
+     * Returns description of method result value
+     *
+     * @return external_description
+     * @since Moodle 3.3
+     */
+    public static function approve_entry_returns() {
+        return new external_single_structure(
+            array(
+                'status' => new external_value(PARAM_BOOL, 'status: true if success'),
+                'warnings' => new external_warnings()
+            )
+        );
+    }
 }
index d95612d..3949c54 100644 (file)
@@ -83,4 +83,12 @@ $functions = array(
         'capabilities'  => 'mod/data:viewentry',
         'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
     ),
+    'mod_data_approve_entry' => array(
+        'classname'     => 'mod_data_external',
+        'methodname'    => 'approve_entry',
+        'description'   => 'Approves or unapproves an entry.',
+        'type'          => 'write',
+        'capabilities'  => 'mod/data:approve',
+        'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
+    ),
 );
index 99e549f..4289925 100644 (file)
@@ -709,4 +709,44 @@ class mod_data_external_testcase extends externallib_advanced_testcase {
         $this->assertCount(0, $result['entries']);  // Only one matching everything.
         $this->assertEquals(0, $result['totalcount']);
     }
+
+    /**
+     * Test approve_entry.
+     */
+    public function test_approve_entry() {
+        global $DB;
+        list($entry11, $entry12, $entry13, $entry21) = self::populate_database_with_entries();
+
+        $this->setUser($this->teacher);
+        $this->assertEquals(0, $DB->get_field('data_records', 'approved', array('id' => $entry13)));
+        $result = mod_data_external::approve_entry($entry13);
+        $result = external_api::clean_returnvalue(mod_data_external::approve_entry_returns(), $result);
+        $this->assertEquals(1, $DB->get_field('data_records', 'approved', array('id' => $entry13)));
+    }
+
+    /**
+     * Test unapprove_entry.
+     */
+    public function test_unapprove_entry() {
+        global $DB;
+        list($entry11, $entry12, $entry13, $entry21) = self::populate_database_with_entries();
+
+        $this->setUser($this->teacher);
+        $this->assertEquals(1, $DB->get_field('data_records', 'approved', array('id' => $entry11)));
+        $result = mod_data_external::approve_entry($entry11, false);
+        $result = external_api::clean_returnvalue(mod_data_external::approve_entry_returns(), $result);
+        $this->assertEquals(0, $DB->get_field('data_records', 'approved', array('id' => $entry11)));
+    }
+
+    /**
+     * Test approve_entry missing permissions.
+     */
+    public function test_approve_entry_missing_permissions() {
+        global $DB;
+        list($entry11, $entry12, $entry13, $entry21) = self::populate_database_with_entries();
+
+        $this->setUser($this->student1);
+        $this->expectException('moodle_exception');
+        mod_data_external::approve_entry($entry13);
+    }
 }
index bc85847..2e7042b 100644 (file)
@@ -24,7 +24,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2016120506;       // The current module version (Date: YYYYMMDDXX)
+$plugin->version   = 2016120507;       // The current module version (Date: YYYYMMDDXX)
 $plugin->requires  = 2016112900;       // Requires this Moodle version
 $plugin->component = 'mod_data';       // Full name of the plugin (used for diagnostics)
 $plugin->cron      = 0;