MDL-55393 message: New WS get_user_notification_preferences

This commit is contained in:
Juan Leyva 2016-09-20 17:18:21 +01:00 committed by Eloy Lafuente (stronk7)
parent 53522c6e38
commit e86f0cb4a1
4 changed files with 209 additions and 1 deletions

View file

@ -814,6 +814,15 @@ $functions = array(
'ajax' => true,
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
'core_message_get_user_notification_preferences' => array(
'classname' => 'core_message_external',
'methodname' => 'get_user_notification_preferences',
'classpath' => 'message/externallib.php',
'description' => 'Get the notification 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',

View file

@ -2200,4 +2200,146 @@ class core_message_external extends external_api {
)
);
}
/**
* Returns description of method parameters
*
* @return external_function_parameters
* @since 3.2
*/
public static function get_user_notification_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_notification_preferences($userid = 0) {
global $USER, $PAGE;
$params = self::validate_parameters(
self::get_user_notification_preferences_parameters(),
array(
'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);
}
$processors = get_message_processors();
$providers = message_get_providers_for_user($user->id);
$preferences = get_all_message_preferences($processors, $providers, $user);
$notificationlist = new \core_message\output\preferences\notification_list($processors, $providers, $preferences, $user);
$renderer = $PAGE->get_renderer('core_message');
$result = array(
'warnings' => array(),
'preferences' => $notificationlist->export_for_template($renderer)
);
return $result;
}
/**
* Returns description of method result value
*
* @return external_description
* @since 3.2
*/
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'),
'ispopup' => new external_value(PARAM_BOOL, 'If is a popup'),
'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'),
'ispopup' => new external_value(PARAM_BOOL, 'If is a popup'),
'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'
),
)
),
'warnings' => new external_warnings(),
)
);
}
}

View file

@ -886,4 +886,61 @@ class core_message_externallib_testcase extends externallib_advanced_testcase {
$this->assertCount(6, $readnotifications);
$this->assertCount(0, $unreadnotifications);
}
/**
* Test get_user_notification_preferences
*/
public function test_get_user_notification_preferences() {
$this->resetAfterTest(true);
$user = self::getDataGenerator()->create_user();
$this->setUser($user);
// Set a couple of preferences to test.
set_user_preference('message_provider_mod_assign_assign_notification_loggedin', 'popup', $user);
set_user_preference('message_provider_mod_assign_assign_notification_loggedoff', 'email', $user);
$prefs = core_message_external::get_user_notification_preferences();
$prefs = external_api::clean_returnvalue(core_message_external::get_user_notification_preferences_returns(), $prefs);
// Check processors.
$this->assertCount(2, $prefs['preferences']['processors']);
$this->assertEquals($user->id, $prefs['preferences']['userid']);
// Check components.
$this->assertCount(8, $prefs['preferences']['components']);
// Check some preferences that we previously set.
$found = 0;
foreach ($prefs['preferences']['components'] as $component) {
foreach ($component['notifications'] as $prefdata) {
if ($prefdata['preferencekey'] != 'message_provider_mod_assign_assign_notification') {
continue;
}
foreach ($prefdata['processors'] as $processor) {
if ($processor['name'] == 'popup') {
$this->assertTrue($processor['loggedin']['checked']);
$found++;
} else if ($processor['name'] == 'email') {
$this->assertTrue($processor['loggedoff']['checked']);
$found++;
}
}
}
}
$this->assertEquals(2, $found);
}
/**
* Test get_user_notification_preferences permissions
*/
public function test_get_user_notification_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_notification_preferences($otheruser->id);
}
}

View file

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2016101101.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2016101101.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.