diff --git a/mod/data/classes/external.php b/mod/data/classes/external.php index 6f6829e6929..08f844d975b 100644 --- a/mod/data/classes/external.php +++ b/mod/data/classes/external.php @@ -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() + ) + ); + } } diff --git a/mod/data/db/services.php b/mod/data/db/services.php index d95612daee1..3949c548d67 100644 --- a/mod/data/db/services.php +++ b/mod/data/db/services.php @@ -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) + ), ); diff --git a/mod/data/tests/externallib_test.php b/mod/data/tests/externallib_test.php index 99e549f9f88..42899259c82 100644 --- a/mod/data/tests/externallib_test.php +++ b/mod/data/tests/externallib_test.php @@ -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); + } } diff --git a/mod/data/version.php b/mod/data/version.php index bc85847f93d..2e7042bf02f 100644 --- a/mod/data/version.php +++ b/mod/data/version.php @@ -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;