Commit e745a9ff authored by Eloy Lafuente's avatar Eloy Lafuente
Browse files

Merge branch 'MDL-64655-master' of git://github.com/jleyva/moodle

parents ec819146 4daa0d08
......@@ -105,4 +105,12 @@ $functions = array(
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
),
'mod_forum_get_forum_access_information' => array(
'classname' => 'mod_forum_external',
'methodname' => 'get_forum_access_information',
'description' => 'Return capabilities information for a given forum.',
'type' => 'read',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
);
......@@ -1189,4 +1189,73 @@ class mod_forum_external extends external_api {
);
}
/**
* Describes the parameters for get_forum_access_information.
*
* @return external_external_function_parameters
* @since Moodle 3.7
*/
public static function get_forum_access_information_parameters() {
return new external_function_parameters (
array(
'forumid' => new external_value(PARAM_INT, 'Forum instance id.')
)
);
}
/**
* Return access information for a given forum.
*
* @param int $forumid forum instance id
* @return array of warnings and the access information
* @since Moodle 3.7
* @throws moodle_exception
*/
public static function get_forum_access_information($forumid) {
global $DB;
$params = self::validate_parameters(self::get_forum_access_information_parameters(), array('forumid' => $forumid));
// Request and permission validation.
$forum = $DB->get_record('forum', array('id' => $params['forumid']), '*', MUST_EXIST);
$cm = get_coursemodule_from_instance('forum', $forum->id);
$context = context_module::instance($cm->id);
self::validate_context($context);
$result = array();
// Return all the available capabilities.
$capabilities = load_capability_def('mod_forum');
foreach ($capabilities as $capname => $capdata) {
// Get fields like cansubmit so it is consistent with the access_information function implemented in other modules.
$field = 'can' . str_replace('mod/forum:', '', $capname);
$result[$field] = has_capability($capname, $context);
}
$result['warnings'] = array();
return $result;
}
/**
* Describes the get_forum_access_information return value.
*
* @return external_single_structure
* @since Moodle 3.7
*/
public static function get_forum_access_information_returns() {
$structure = array(
'warnings' => new external_warnings()
);
$capabilities = load_capability_def('mod_forum');
foreach ($capabilities as $capname => $capdata) {
// Get fields like cansubmit so it is consistent with the access_information function implemented in other modules.
$field = 'can' . str_replace('mod/forum:', '', $capname);
$structure[$field] = new external_value(PARAM_BOOL, 'Whether the user has the capability ' . $capname . ' allowed.',
VALUE_OPTIONAL);
}
return new external_single_structure($structure);
}
}
......@@ -1248,4 +1248,57 @@ class mod_forum_external_testcase extends externallib_advanced_testcase {
$this->assertEquals(2, $posts['ratinginfo']['ratings'][0]['count']);
$this->assertEquals(($rating1->rating + $rating2->rating) / 2, $posts['ratinginfo']['ratings'][0]['aggregate']);
}
/**
* Test mod_forum_get_forum_access_information.
*/
public function test_mod_forum_get_forum_access_information() {
global $DB;
$this->resetAfterTest(true);
$student = self::getDataGenerator()->create_user();
$course = self::getDataGenerator()->create_course();
// Create the forum.
$record = new stdClass();
$record->course = $course->id;
$forum = self::getDataGenerator()->create_module('forum', $record);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id, 'manual');
self::setUser($student);
$result = mod_forum_external::get_forum_access_information($forum->id);
$result = external_api::clean_returnvalue(mod_forum_external::get_forum_access_information_returns(), $result);
// Check default values for capabilities.
$enabledcaps = array('canviewdiscussion', 'canstartdiscussion', 'canreplypost', 'canviewrating', 'cancreateattachment',
'canexportownpost', 'candeleteownpost', 'canallowforcesubscribe');
unset($result['warnings']);
foreach ($result as $capname => $capvalue) {
if (in_array($capname, $enabledcaps)) {
$this->assertTrue($capvalue);
} else {
$this->assertFalse($capvalue);
}
}
// Now, unassign some capabilities.
unassign_capability('mod/forum:deleteownpost', $studentrole->id);
unassign_capability('mod/forum:allowforcesubscribe', $studentrole->id);
array_pop($enabledcaps);
array_pop($enabledcaps);
accesslib_clear_all_caches_for_unit_testing();
$result = mod_forum_external::get_forum_access_information($forum->id);
$result = external_api::clean_returnvalue(mod_forum_external::get_forum_access_information_returns(), $result);
unset($result['warnings']);
foreach ($result as $capname => $capvalue) {
if (in_array($capname, $enabledcaps)) {
$this->assertTrue($capvalue);
} else {
$this->assertFalse($capvalue);
}
}
}
}
......@@ -24,6 +24,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2018120300; // The current module version (Date: YYYYMMDDXX)
$plugin->version = 2018120301; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2018112800; // Requires this Moodle version
$plugin->component = 'mod_forum'; // Full name of the plugin (used for diagnostics)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment