Merge branch 'MDL-56666-master' of git://github.com/jleyva/moodle
This commit is contained in:
@@ -832,6 +832,15 @@ $functions = array(
|
||||
'capabilities' => 'moodle/user:editownmessageprofile',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'core_message_get_user_message_preferences' => array(
|
||||
'classname' => 'core_message_external',
|
||||
'methodname' => 'get_user_message_preferences',
|
||||
'classpath' => 'message/externallib.php',
|
||||
'description' => 'Get the message preferences for a given user.',
|
||||
'type' => 'read',
|
||||
'capabilities' => 'moodle/user:editownmessageprofile',
|
||||
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
|
||||
),
|
||||
'core_notes_create_notes' => array(
|
||||
'classname' => 'core_notes_external',
|
||||
'methodname' => 'create_notes',
|
||||
|
||||
+182
-81
@@ -2281,6 +2281,108 @@ class core_message_external extends external_api {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that the user has enough permission to retrieve message or notifications preferences.
|
||||
*
|
||||
* @param int $userid the user id requesting the preferences
|
||||
* @return stdClass full user object
|
||||
* @throws moodle_exception
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
protected static function validate_preferences_permissions($userid) {
|
||||
global $USER;
|
||||
|
||||
if (empty($userid)) {
|
||||
$user = $USER;
|
||||
} else {
|
||||
$user = core_user::get_user($userid, '*', MUST_EXIST);
|
||||
core_user::require_active_user($user);
|
||||
}
|
||||
|
||||
$systemcontext = context_system::instance();
|
||||
self::validate_context($systemcontext);
|
||||
|
||||
// Check access control.
|
||||
if ($user->id == $USER->id) {
|
||||
// Editing own message profile.
|
||||
require_capability('moodle/user:editownmessageprofile', $systemcontext);
|
||||
} else {
|
||||
// Teachers, parents, etc.
|
||||
$personalcontext = context_user::instance($user->id);
|
||||
require_capability('moodle/user:editmessageprofile', $personalcontext);
|
||||
}
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a notification or message preference structure.
|
||||
*
|
||||
* @return external_single_structure the structure
|
||||
* @since Moodle 3.2
|
||||
*/
|
||||
protected static function get_preferences_structure() {
|
||||
return new external_single_structure(
|
||||
array(
|
||||
'userid' => new external_value(PARAM_INT, 'User id'),
|
||||
'disableall' => new external_value(PARAM_INT, 'Whether all the preferences are disabled'),
|
||||
'processors' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'displayname' => new external_value(PARAM_TEXT, 'Display name'),
|
||||
'name' => new external_value(PARAM_PLUGIN, 'Processor name'),
|
||||
'hassettings' => new external_value(PARAM_BOOL, 'Whether has settings'),
|
||||
'contextid' => new external_value(PARAM_INT, 'Context id'),
|
||||
'userconfigured' => new external_value(PARAM_INT, 'Whether is configured by the user'),
|
||||
)
|
||||
),
|
||||
'Config form values'
|
||||
),
|
||||
'components' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'displayname' => new external_value(PARAM_TEXT, 'Display name'),
|
||||
'notifications' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'displayname' => new external_value(PARAM_TEXT, 'Display name'),
|
||||
'preferencekey' => new external_value(PARAM_ALPHANUMEXT, 'Preference key'),
|
||||
'processors' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'displayname' => new external_value(PARAM_TEXT, 'Display name'),
|
||||
'name' => new external_value(PARAM_PLUGIN, 'Processor name'),
|
||||
'locked' => new external_value(PARAM_BOOL, 'Is locked by admin?'),
|
||||
'userconfigured' => new external_value(PARAM_INT, 'Is configured?'),
|
||||
'loggedin' => new external_single_structure(
|
||||
array(
|
||||
'name' => new external_value(PARAM_NOTAGS, 'Name'),
|
||||
'displayname' => new external_value(PARAM_TEXT, 'Display name'),
|
||||
'checked' => new external_value(PARAM_BOOL, 'Is checked?'),
|
||||
)
|
||||
),
|
||||
'loggedoff' => new external_single_structure(
|
||||
array(
|
||||
'name' => new external_value(PARAM_NOTAGS, 'Name'),
|
||||
'displayname' => new external_value(PARAM_TEXT, 'Display name'),
|
||||
'checked' => new external_value(PARAM_BOOL, 'Is checked?'),
|
||||
)
|
||||
),
|
||||
)
|
||||
),
|
||||
'Processors values for this notification'
|
||||
),
|
||||
)
|
||||
),
|
||||
'List of notificaitons for the component'
|
||||
),
|
||||
)
|
||||
),
|
||||
'Available components'
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method parameters
|
||||
*
|
||||
@@ -2304,7 +2406,7 @@ class core_message_external extends external_api {
|
||||
* @since 3.2
|
||||
*/
|
||||
public static function get_user_notification_preferences($userid = 0) {
|
||||
global $USER, $PAGE, $CFG;
|
||||
global $PAGE;
|
||||
|
||||
$params = self::validate_parameters(
|
||||
self::get_user_notification_preferences_parameters(),
|
||||
@@ -2312,26 +2414,7 @@ class core_message_external extends external_api {
|
||||
'userid' => $userid,
|
||||
)
|
||||
);
|
||||
|
||||
if (empty($params['userid'])) {
|
||||
$user = $USER;
|
||||
} else {
|
||||
$user = core_user::get_user($params['userid'], '*', MUST_EXIST);
|
||||
core_user::require_active_user($user);
|
||||
}
|
||||
|
||||
$systemcontext = context_system::instance();
|
||||
self::validate_context($systemcontext);
|
||||
|
||||
// Check access control.
|
||||
if ($user->id == $USER->id) {
|
||||
// Editing own message profile.
|
||||
require_capability('moodle/user:editownmessageprofile', $systemcontext);
|
||||
} else {
|
||||
// Teachers, parents, etc.
|
||||
$personalcontext = context_user::instance($user->id);
|
||||
require_capability('moodle/user:editmessageprofile', $personalcontext);
|
||||
}
|
||||
$user = self::validate_preferences_permissions($params['userid']);
|
||||
|
||||
$processors = get_message_processors();
|
||||
$providers = message_get_providers_for_user($user->id);
|
||||
@@ -2356,66 +2439,84 @@ class core_message_external extends external_api {
|
||||
public static function get_user_notification_preferences_returns() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'preferences' => new external_single_structure(
|
||||
array(
|
||||
'userid' => new external_value(PARAM_INT, 'User id'),
|
||||
'disableall' => new external_value(PARAM_INT, 'Whether all the preferences are disabled'),
|
||||
'processors' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'displayname' => new external_value(PARAM_TEXT, 'Display name'),
|
||||
'name' => new external_value(PARAM_PLUGIN, 'Processor name'),
|
||||
'hassettings' => new external_value(PARAM_BOOL, 'Whether has settings'),
|
||||
'contextid' => new external_value(PARAM_INT, 'Context id'),
|
||||
'userconfigured' => new external_value(PARAM_INT, 'Whether is configured by the user'),
|
||||
)
|
||||
),
|
||||
'Config form values'
|
||||
),
|
||||
'components' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'displayname' => new external_value(PARAM_TEXT, 'Display name'),
|
||||
'notifications' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'displayname' => new external_value(PARAM_TEXT, 'Display name'),
|
||||
'preferencekey' => new external_value(PARAM_ALPHANUMEXT, 'Preference key'),
|
||||
'processors' => new external_multiple_structure(
|
||||
new external_single_structure(
|
||||
array(
|
||||
'displayname' => new external_value(PARAM_TEXT, 'Display name'),
|
||||
'name' => new external_value(PARAM_PLUGIN, 'Processor name'),
|
||||
'locked' => new external_value(PARAM_BOOL, 'Is locked by admin?'),
|
||||
'userconfigured' => new external_value(PARAM_INT, 'Is configured?'),
|
||||
'loggedin' => new external_single_structure(
|
||||
array(
|
||||
'name' => new external_value(PARAM_NOTAGS, 'Name'),
|
||||
'displayname' => new external_value(PARAM_TEXT, 'Display name'),
|
||||
'checked' => new external_value(PARAM_BOOL, 'Is checked?'),
|
||||
)
|
||||
),
|
||||
'loggedoff' => new external_single_structure(
|
||||
array(
|
||||
'name' => new external_value(PARAM_NOTAGS, 'Name'),
|
||||
'displayname' => new external_value(PARAM_TEXT, 'Display name'),
|
||||
'checked' => new external_value(PARAM_BOOL, 'Is checked?'),
|
||||
)
|
||||
),
|
||||
)
|
||||
),
|
||||
'Processors values for this notification'
|
||||
),
|
||||
)
|
||||
),
|
||||
'List of notificaitons for the component'
|
||||
),
|
||||
)
|
||||
),
|
||||
'Available components'
|
||||
),
|
||||
)
|
||||
),
|
||||
'preferences' => self::get_preferences_structure(),
|
||||
'warnings' => new external_warnings(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns description of method parameters
|
||||
*
|
||||
* @return external_function_parameters
|
||||
* @since 3.2
|
||||
*/
|
||||
public static function get_user_message_preferences_parameters() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'userid' => new external_value(PARAM_INT, 'id of the user, 0 for current user', VALUE_DEFAULT, 0)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification preferences for a given user.
|
||||
*
|
||||
* @param int $userid id of the user, 0 for current user
|
||||
* @return external_description
|
||||
* @throws moodle_exception
|
||||
* @since 3.2
|
||||
*/
|
||||
public static function get_user_message_preferences($userid = 0) {
|
||||
global $PAGE;
|
||||
|
||||
$params = self::validate_parameters(
|
||||
self::get_user_message_preferences_parameters(),
|
||||
array(
|
||||
'userid' => $userid,
|
||||
)
|
||||
);
|
||||
|
||||
$user = self::validate_preferences_permissions($params['userid']);
|
||||
|
||||
// Filter out enabled, available system_configured and user_configured processors only.
|
||||
$readyprocessors = array_filter(get_message_processors(), function($processor) {
|
||||
return $processor->enabled &&
|
||||
$processor->configured &&
|
||||
$processor->object->is_user_configured() &&
|
||||
// Filter out processors that don't have and message preferences to configure.
|
||||
$processor->object->has_message_preferences();
|
||||
});
|
||||
|
||||
$providers = array_filter(message_get_providers_for_user($user->id), function($provider) {
|
||||
return $provider->component === 'moodle';
|
||||
});
|
||||
$preferences = \core_message\api::get_all_message_preferences($readyprocessors, $providers, $user);
|
||||
$notificationlistoutput = new \core_message\output\preferences\message_notification_list($readyprocessors,
|
||||
$providers, $preferences, $user);
|
||||
|
||||
$renderer = $PAGE->get_renderer('core_message');
|
||||
|
||||
$result = array(
|
||||
'warnings' => array(),
|
||||
'preferences' => $notificationlistoutput->export_for_template($renderer),
|
||||
'blocknoncontacts' => get_user_preferences('message_blocknoncontacts', '', $user->id) ? true : false,
|
||||
);
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns description of method result value
|
||||
*
|
||||
* @return external_description
|
||||
* @since 3.2
|
||||
*/
|
||||
public static function get_user_message_preferences_returns() {
|
||||
return new external_function_parameters(
|
||||
array(
|
||||
'preferences' => self::get_preferences_structure(),
|
||||
'blocknoncontacts' => new external_value(PARAM_BOOL, 'Whether to block or not messages from non contacts'),
|
||||
'warnings' => new external_warnings(),
|
||||
)
|
||||
);
|
||||
|
||||
@@ -2625,4 +2625,59 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
|
||||
$this->assertNotEmpty($result['systemconfigured']);
|
||||
$this->assertNotEmpty($result['userconfigured']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_user_notification_preferences
|
||||
*/
|
||||
public function test_get_user_message_preferences() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$user = self::getDataGenerator()->create_user();
|
||||
$this->setUser($user);
|
||||
|
||||
// Set a couple of preferences to test.
|
||||
set_user_preference('message_provider_moodle_instantmessage_loggedin', 'email', $user);
|
||||
set_user_preference('message_provider_moodle_instantmessage_loggedoff', 'email', $user);
|
||||
set_user_preference('message_blocknoncontacts', 1, $user);
|
||||
|
||||
$prefs = core_message_external::get_user_message_preferences();
|
||||
$prefs = external_api::clean_returnvalue(core_message_external::get_user_message_preferences_returns(), $prefs);
|
||||
$this->assertEquals($user->id, $prefs['preferences']['userid']);
|
||||
|
||||
// Check components.
|
||||
$this->assertCount(1, $prefs['preferences']['components']);
|
||||
$this->assertTrue($prefs['blocknoncontacts']);
|
||||
|
||||
// Check some preferences that we previously set.
|
||||
$found = false;
|
||||
foreach ($prefs['preferences']['components'] as $component) {
|
||||
foreach ($component['notifications'] as $prefdata) {
|
||||
if ($prefdata['preferencekey'] != 'message_provider_moodle_instantmessage') {
|
||||
continue;
|
||||
}
|
||||
foreach ($prefdata['processors'] as $processor) {
|
||||
if ($processor['name'] == 'email') {
|
||||
$this->assertTrue($processor['loggedin']['checked']);
|
||||
$this->assertTrue($processor['loggedoff']['checked']);
|
||||
$found = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->assertTrue($found);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get_user_message_preferences permissions
|
||||
*/
|
||||
public function test_get_user_message_preferences_permissions() {
|
||||
$this->resetAfterTest(true);
|
||||
|
||||
$user = self::getDataGenerator()->create_user();
|
||||
$otheruser = self::getDataGenerator()->create_user();
|
||||
$this->setUser($user);
|
||||
|
||||
$this->expectException('moodle_exception');
|
||||
$prefs = core_message_external::get_user_message_preferences($otheruser->id);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$version = 2016110202.00; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
$version = 2016110202.01; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
// RR = release increments - 00 in DEV branches.
|
||||
// .XX = incremental changes.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user