public function test_message_get_providers_for_user_more() { global $DB; $this->resetAfterTest(); // Create a course. $course = $this->getDataGenerator()->create_course(); $coursecontext = context_course::instance($course->id); // It would probably be better to use a quiz instance as it has capability controlled messages // however mod_quiz doesn't have a data generator. // Instead we're going to use backup notifications and give and take away the capability at various levels. $assign = $this->getDataGenerator()->create_module('assign', array('course' => $course->id)); $modulecontext = context_module::instance($assign->cmid); // Create and enrol a teacher. $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'), '*', MUST_EXIST); $teacher = $this->getDataGenerator()->create_user(); role_assign($teacherrole->id, $teacher->id, $coursecontext); $enrolplugin = enrol_get_plugin('manual'); $enrolplugin->add_instance($course); $enrolinstances = enrol_get_instances($course->id, false); foreach ($enrolinstances as $enrolinstance) { if ($enrolinstance->enrol === 'manual') { break; } } $enrolplugin->enrol_user($enrolinstance, $teacher->id); // Make the teacher the current user. $this->setUser($teacher); // Teacher shouldn't have the required capability so they shouldn't be able to see the backup message. $this->assertFalse(has_capability('moodle/site:config', $modulecontext)); $providers = message_get_providers_for_user($teacher->id); $this->assertFalse($this->message_type_present('moodle', 'backup', $providers)); // Give the user the required capability in an activity module. // They should now be able to see the backup message. assign_capability('moodle/site:config', CAP_ALLOW, $teacherrole->id, $modulecontext->id, true); accesslib_clear_all_caches_for_unit_testing(); $modulecontext = context_module::instance($assign->cmid); $this->assertTrue(has_capability('moodle/site:config', $modulecontext)); $providers = message_get_providers_for_user($teacher->id); $this->assertTrue($this->message_type_present('moodle', 'backup', $providers)); // Prohibit the capability for the user at the course level. // This overrules the CAP_ALLOW at the module level. // They should not be able to see the backup message. assign_capability('moodle/site:config', CAP_PROHIBIT, $teacherrole->id, $coursecontext->id, true); accesslib_clear_all_caches_for_unit_testing(); $modulecontext = context_module::instance($assign->cmid); $this->assertFalse(has_capability('moodle/site:config', $modulecontext)); $providers = message_get_providers_for_user($teacher->id); // Actually, handling PROHIBITs would be too expensive. We do not // care if users with PROHIBITs see a few more preferences than they should. // $this->assertFalse($this->message_type_present('moodle', 'backup', $providers)); }
/** * This function has been deprecated please use {@link message_get_providers_for_user()} instead. * * Returns the active providers for the current user, based on capability * * @see message_get_providers_for_user() * @deprecated since 2.1 * @todo Remove in 2.5 (MDL-34454) * @return array An array of message providers */ function message_get_my_providers() { global $USER; debugging('message_get_my_providers is deprecated please update your code', DEBUG_DEVELOPER); return message_get_providers_for_user($USER->id); }
/** * Handles displaying processor settings in a fragment. * * @param array $args * @return bool|string * @throws moodle_exception */ function message_output_fragment_processor_settings($args = []) { global $PAGE; if (!isset($args['type'])) { throw new moodle_exception('Must provide a processor type'); } if (!isset($args['userid'])) { throw new moodle_exception('Must provide a userid'); } $type = $args['type']; $userid = $args['userid']; $user = core_user::get_user($userid, '*', MUST_EXIST); $processor = get_message_processor($type); $providers = message_get_providers_for_user($userid); $processorwrapper = new stdClass(); $processorwrapper->object = $processor; $preferences = \core_message\api::get_all_message_preferences([$processorwrapper], $providers, $user); $processoroutput = new \core_message\output\preferences\processor($processor, $preferences, $user, $type); $renderer = $PAGE->get_renderer('core', 'message'); return $renderer->render_from_template('core_message/preferences_processor', $processoroutput->export_for_template($renderer)); }
/** * This function has been deprecated please use {@link message_get_providers_for_user()} instead. * * Returns the active providers for the current user, based on capability * * @see message_get_providers_for_user() * @deprecated since 2.1 * @todo Remove in 2.2 (MDL-31031) * @return array An array of message providers */ function message_get_my_providers() { global $USER; return message_get_providers_for_user($USER->id); }
} else { // teachers, parents, etc. require_capability('moodle/user:editmessageprofile', $personalcontext); // no editing of guest user account if (isguestuser($user->id)) { print_error('guestnoeditmessageother', 'message'); } // no editing of admins by non admins! if (is_siteadmin($user) and !is_siteadmin($USER)) { print_error('useradmineditadmin'); } $PAGE->navbar->includesettingsbase = true; $PAGE->navigation->extend_for_user($user); } // Fetch message providers $providers = message_get_providers_for_user($user->id); /// Save new preferences if data was submitted if (($form = data_submitted()) && confirm_sesskey()) { $preferences = array(); //only update the user's "emailstop" if its actually changed if ($user->emailstop != $disableall) { $user->emailstop = $disableall; $DB->set_field('user', 'emailstop', $user->emailstop, array("id" => $user->id)); } // Turning on emailstop disables the preference checkboxes in the browser. // Disabled checkboxes may not be submitted with the form making them look (incorrectly) like they've been unchecked. // Only alter the messaging preferences if emailstop is turned off if (!$user->emailstop) { foreach ($providers as $provider) { $componentproviderbase = $provider->component . '_' . $provider->name; foreach (array('loggedin', 'loggedoff') as $state) {
/** * Check if the users have notification preferences configured for the airnotifier plugin * * @param array $userids Array of user ids * @since Moodle 2.7 */ public static function are_notification_preferences_configured($userids) { global $CFG, $USER, $DB; require_once $CFG->dirroot . '/message/lib.php'; $params = self::validate_parameters(self::are_notification_preferences_configured_parameters(), array('userids' => $userids)); list($sqluserids, $params) = $DB->get_in_or_equal($params['userids'], SQL_PARAMS_NAMED); $uselect = ', ' . context_helper::get_preload_record_columns_sql('ctx'); $ujoin = "LEFT JOIN {context} ctx ON (ctx.instanceid = u.id AND ctx.contextlevel = :contextlevel)"; $params['contextlevel'] = CONTEXT_USER; $usersql = "SELECT u.* {$uselect}\n FROM {user} u {$ujoin}\n WHERE u.id {$sqluserids}"; $users = $DB->get_recordset_sql($usersql, $params); $result = array('users' => array(), 'warnings' => array()); $hasuserupdatecap = has_capability('moodle/user:update', context_system::instance()); foreach ($users as $user) { $currentuser = $user->id == $USER->id; if ($currentuser or $hasuserupdatecap) { if (!empty($user->deleted)) { $warning = array(); $warning['item'] = 'user'; $warning['itemid'] = $user->id; $warning['warningcode'] = '1'; $warning['message'] = "User {$user->id} was deleted"; $result['warnings'][] = $warning; continue; } $preferences = array(); $preferences['userid'] = $user->id; $preferences['configured'] = 0; // Now we get for all the providers and all the states // the user preferences to check if at least one is enabled for airnotifier plugin. $providers = message_get_providers_for_user($user->id); $configured = false; foreach ($providers as $provider) { if ($configured) { break; } foreach (array('loggedin', 'loggedoff') as $state) { $prefstocheck = array(); $prefname = 'message_provider_' . $provider->component . '_' . $provider->name . '_' . $state; // First get forced settings. if ($forcedpref = get_config('message', $prefname)) { $prefstocheck = array_merge($prefstocheck, explode(',', $forcedpref)); } // Then get user settings. if ($userpref = get_user_preferences($prefname, '', $user->id)) { $prefstocheck = array_merge($prefstocheck, explode(',', $userpref)); } if (in_array('airnotifier', $prefstocheck)) { $preferences['configured'] = 1; $configured = true; break; } } } $result['users'][] = $preferences; } else { if (!$hasuserupdatecap) { $warning = array(); $warning['item'] = 'user'; $warning['itemid'] = $user->id; $warning['warningcode'] = '2'; $warning['message'] = "You don't have permissions for view user {$user->id} preferences"; $result['warnings'][] = $warning; } } } $users->close(); return $result; }
/** * Tests deleting a conversation. */ public function test_get_all_message_preferences() { $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); $processors = get_message_processors(); $providers = message_get_providers_for_user($user->id); $prefs = \core_message\api::get_all_message_preferences($processors, $providers, $user); $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedin['popup']); $this->assertEquals(1, $prefs->mod_assign_assign_notification_loggedoff['email']); }
/** * 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() && $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; }
/** * 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 = \core_message\api::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; }