MDL-57921 mod_data: New WS mod_data_approve_entry

This commit is contained in:
Juan Leyva
2017-03-30 02:12:53 +02:00
committed by Eloy Lafuente (stronk7)
parent b8e0e64455
commit 229158fe74
4 changed files with 112 additions and 1 deletions
+63
View 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()
)
);
}
}
+8
View 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)
),
);
+40
View 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);
}
}
+1 -1
View 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;