MDL-57922 mod_data: New Web Service mod_data_delete_entry
authorJuan Leyva <juanleyvadelgado@gmail.com>
Mon, 13 Feb 2017 15:13:48 +0000 (16:13 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Thu, 30 Mar 2017 00:15:46 +0000 (02:15 +0200)
mod/data/classes/external.php
mod/data/db/services.php
mod/data/tests/externallib_test.php
mod/data/version.php

index 08f844d..62e4b74 100644 (file)
@@ -873,4 +873,64 @@ class mod_data_external extends external_api {
             )
         );
     }
+
+    /**
+     * Returns description of method parameters
+     *
+     * @return external_function_parameters
+     * @since Moodle 3.3
+     */
+    public static function delete_entry_parameters() {
+        return new external_function_parameters(
+            array(
+                'entryid' => new external_value(PARAM_INT, 'Record entry id.'),
+            )
+        );
+    }
+
+    /**
+     * Deletes an entry.
+     *
+     * @param int $entryid the record entry id
+     * @return array of warnings success status
+     * @since Moodle 3.3
+     * @throws moodle_exception
+     */
+    public static function delete_entry($entryid) {
+        global $PAGE, $DB;
+
+        $params = array('entryid' => $entryid);
+        $params = self::validate_parameters(self::delete_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);
+
+        if (data_user_can_manage_entry($record, $database, $context)) {
+            data_delete_record($record->id, $database, $course->id, $cm->id);
+        } else {
+            throw new moodle_exception('noaccess', 'data');
+        }
+
+        $result = array(
+            'status' => true,
+            'warnings' => $warnings
+        );
+        return $result;
+    }
+
+    /**
+     * Returns description of method result value
+     *
+     * @return external_description
+     * @since Moodle 3.3
+     */
+    public static function delete_entry_returns() {
+        return new external_single_structure(
+            array(
+                'status' => new external_value(PARAM_BOOL, 'Always true. If we see this field it means that the entry was deleted.'),
+                'warnings' => new external_warnings()
+            )
+        );
+    }
 }
index 3949c54..dafc468 100644 (file)
@@ -91,4 +91,12 @@ $functions = array(
         'capabilities'  => 'mod/data:approve',
         'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
     ),
+    'mod_data_delete_entry' => array(
+        'classname'     => 'mod_data_external',
+        'methodname'    => 'delete_entry',
+        'description'   => 'Deletes an entry.',
+        'type'          => 'write',
+        'capabilities'  => 'mod/data:manageentries',
+        'services'      => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
+    ),
 );
index 4289925..cffdef9 100644 (file)
@@ -749,4 +749,63 @@ class mod_data_external_testcase extends externallib_advanced_testcase {
         $this->expectException('moodle_exception');
         mod_data_external::approve_entry($entry13);
     }
+
+    /**
+     * Test delete_entry as teacher. Check I can delete any entry.
+     */
+    public function test_delete_entry_as_teacher() {
+        global $DB;
+        list($entry11, $entry12, $entry13, $entry21) = self::populate_database_with_entries();
+
+        $this->setUser($this->teacher);
+        $result = mod_data_external::delete_entry($entry11);
+        $result = external_api::clean_returnvalue(mod_data_external::delete_entry_returns(), $result);
+        $this->assertEquals(0, $DB->count_records('data_records', array('id' => $entry11)));
+
+        // Entry in other group.
+        $result = mod_data_external::delete_entry($entry21);
+        $result = external_api::clean_returnvalue(mod_data_external::delete_entry_returns(), $result);
+        $this->assertEquals(0, $DB->count_records('data_records', array('id' => $entry21)));
+    }
+
+    /**
+     * Test delete_entry as student. Check I can delete my own entries.
+     */
+    public function test_delete_entry_as_student() {
+        global $DB;
+        list($entry11, $entry12, $entry13, $entry21) = self::populate_database_with_entries();
+
+        $this->setUser($this->student1);
+        $result = mod_data_external::delete_entry($entry11);
+        $result = external_api::clean_returnvalue(mod_data_external::delete_entry_returns(), $result);
+        $this->assertEquals(0, $DB->count_records('data_records', array('id' => $entry11)));
+    }
+
+    /**
+     * Test delete_entry as student in read only mode period. Check I cannot delete my own entries in that period.
+     */
+    public function test_delete_entry_as_student_in_read_only_period() {
+        global $DB;
+        list($entry11, $entry12, $entry13, $entry21) = self::populate_database_with_entries();
+        // Set a time period.
+        $this->data->timeviewfrom = time() - HOURSECS;
+        $this->data->timeviewto = time() + HOURSECS;
+        $DB->update_record('data', $this->data);
+
+        $this->setUser($this->student1);
+        $this->expectException('moodle_exception');
+        mod_data_external::delete_entry($entry11);
+    }
+
+    /**
+     * Test delete_entry with an user missing permissions.
+     */
+    public function test_delete_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::delete_entry($entry21);
+    }
 }
index 2e7042b..e052917 100644 (file)
@@ -24,7 +24,7 @@
 
 defined('MOODLE_INTERNAL') || die();
 
-$plugin->version   = 2016120507;       // The current module version (Date: YYYYMMDDXX)
+$plugin->version   = 2016120508;       // 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;