/** * Renders the popup. * * @param renderer_base $renderer * @return string The HTML */ function message_popup_render_navbar_output(\renderer_base $renderer) { global $USER, $CFG; // Early bail out conditions. if (!isloggedin() || isguestuser() || user_not_fully_set_up($USER) || get_user_preferences('auth_forcepasswordchange') || $CFG->sitepolicy && !$USER->policyagreed && !is_siteadmin()) { return ''; } $output = ''; // Add the messages popover. if (!empty($CFG->messaging)) { $context = ['userid' => $USER->id, 'urls' => ['seeall' => (new moodle_url('/message/index.php'))->out(), 'writeamessage' => (new moodle_url('/message/index.php', ['contactsfirst' => 1]))->out(), 'preferences' => (new moodle_url('/message/edit.php', ['id' => $USER->id]))->out()]]; $output .= $renderer->render_from_template('message_popup/message_popover', $context); } // Add the notifications popover. $enabled = \core_message\api::is_processor_enabled("popup"); if ($enabled) { $context = ['userid' => $USER->id, 'urls' => ['seeall' => (new moodle_url('/message/output/popup/notifications.php'))->out(), 'preferences' => (new moodle_url('/message/notificationpreferences.php', ['userid' => $USER->id]))->out()]]; $output .= $renderer->render_from_template('message_popup/notification_popover', $context); } return $output; }
/** * 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)); }
/** * Checks if the recipient has specifically blocked the sending user. * * Note: This function will always return false if the sender has the * readallmessages capability at the system context level. * * @deprecated since Moodle 3.2 * @param object $recipient User object. * @param object $sender User object. * @return bool true if $sender is blocked, false otherwise. */ function message_is_user_blocked($recipient, $sender = null) { debugging('message_is_user_blocked() is deprecated and is no longer used, please use \\core_message\\api::is_user_blocked() instead.', DEBUG_DEVELOPER); return \core_message\api::is_user_blocked($recipient, $sender); }
public function export_for_template(\renderer_base $output) { global $USER; $data = new \stdClass(); $data->iscurrentuser = $USER->id == $this->currentuserid; $data->currentuserid = $this->currentuserid; if ($this->otheruserid) { $data->otheruserid = $this->otheruserid; $data->otheruserfullname = fullname($this->otheruser); } $data->isonline = null; if ($this->otheruserid) { if (\core_message\helper::show_online_status($this->otheruser)) { $data->isonline = \core_message\helper::is_online($this->otheruser->lastaccess); } } $data->showonlinestatus = is_null($data->isonline) ? false : true; $data->messages = array(); foreach ($this->messages as $message) { $message = new message($message); $data->messages[] = $message->export_for_template($output); } $data->isblocked = api::is_user_blocked($this->currentuserid, $this->otheruserid); return $data; }
/** * Tests that the admin is not blocked even if someone has chosen to block them. */ public function test_is_user_blocked_as_admin() { // Create a user. $user1 = self::getDataGenerator()->create_user(); // Set the user. $this->setUser($user1); // Block the admin user. message_block_contact(2); // Now change to the admin user. $this->setAdminUser(); // As the admin you should still be able to send messages to the user. $this->assertFalse(\core_message\api::is_user_blocked($user1)); }
/** * 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; }
if (!empty($conversations)) { $contact = reset($conversations); $user2->id = $contact->userid; } } else { // The user has specifically requested to see a conversation. Add the flag to // the context so that we can render the messaging app appropriately - this is // used for smaller screens as it allows the UI to be responsive. $requestedconversation = true; } // Mark the conversation as read. if (!empty($user2->id)) { if ($currentuser && isset($conversations[$user2->id])) { // Mark the conversation we are loading as read. \core_message\api::mark_all_read_for_user($user1->id, $user2->id); // Ensure the UI knows it's read as well. $conversations[$user2->id]->isread = 1; } $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 20, 'timecreated DESC'); } $messagearea = new \core_message\output\messagearea\message_area($user1->id, $user2->id, $conversations, $messages, $requestedconversation); // Now the page contents. echo $OUTPUT->header(); echo $OUTPUT->heading(get_string('messages', 'message')); // Display a message that the user is viewing someone else's messages. if (!$currentuser) { $notify = new \core\output\notification(get_string('viewinganotherusersmessagearea', 'message'), \core\output\notification::NOTIFY_WARNING); echo $OUTPUT->render($notify); } echo $renderer->render($messagearea); echo $OUTPUT->footer();
/** * Test the message deleted event is fired when deleting a conversation. */ public function test_message_deleted_whole_conversation() { global $DB; // Create a message. $message = new stdClass(); $message->useridfrom = '1'; $message->useridto = '2'; $message->subject = 'Subject'; $message->message = 'Message'; $message->timeuserfromdeleted = 0; $message->timeusertodeleted = 0; $message->timecreated = 1; $messages = []; // Send this a few times. $messages[] = $DB->insert_record('message', $message); $message->timecreated++; $messages[] = $DB->insert_record('message', $message); $message->timecreated++; $messages[] = $DB->insert_record('message', $message); $message->timecreated++; $messages[] = $DB->insert_record('message', $message); // Create a read message. $message->timeread = time(); // Send this a few times. $message->timecreated++; $messages[] = $DB->insert_record('message_read', $message); $message->timecreated++; $messages[] = $DB->insert_record('message_read', $message); $message->timecreated++; $messages[] = $DB->insert_record('message_read', $message); $message->timecreated++; $messages[] = $DB->insert_record('message_read', $message); // Trigger and capture the event. $sink = $this->redirectEvents(); \core_message\api::delete_conversation(1, 2); $events = $sink->get_events(); // Check that there were the correct number of events triggered. $this->assertEquals(8, count($events)); // Check that the event data is valid. $i = 0; foreach ($events as $event) { $table = $i > 3 ? 'message_read' : 'message'; $this->assertInstanceOf('\\core\\event\\message_deleted', $event); $this->assertEquals($message->useridfrom, $event->userid); $this->assertEquals($message->useridto, $event->relateduserid); $this->assertEquals($table, $event->other['messagetable']); $this->assertEquals($messages[$i], $event->other['messageid']); $this->assertEquals($message->useridfrom, $event->other['useridfrom']); $this->assertEquals($message->useridto, $event->other['useridto']); $i++; } }
/** * Test retrieving messages by providing a minimum and maximum timecreated value. */ public function test_get_messages_time_from_and_to() { // Create some users. $user1 = self::getDataGenerator()->create_user(); $user2 = self::getDataGenerator()->create_user(); // The person doing the search. $this->setUser($user1); // Send some messages back and forth. $time = 1; $this->send_fake_message($user1, $user2, 'Message 1', 0, $time + 1); $this->send_fake_message($user2, $user1, 'Message 2', 0, $time + 2); $this->send_fake_message($user1, $user2, 'Message 3', 0, $time + 3); $this->send_fake_message($user2, $user1, 'Message 4', 0, $time + 4); // Retrieve the messages from $time + 2 up until $time + 3, which should be 2nd and 3rd message. $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 0, 'timecreated ASC', $time + 2, $time + 3); // Confirm the message data is correct. $this->assertEquals(2, count($messages)); $message1 = $messages[0]; $message2 = $messages[1]; $this->assertContains('Message 2', $message1->text); $this->assertContains('Message 3', $message2->text); }
$disable = optional_param('disable', 0, PARAM_INT); $enable = optional_param('enable', 0, PARAM_INT); $headingtitle = get_string('managemessageoutputs', 'message'); if (!empty($disable) && confirm_sesskey()) { if (!($processor = $DB->get_record('message_processors', array('id' => $disable)))) { print_error('outputdoesnotexist', 'message'); } \core_message\api::update_processor_status($processor, 0); // Disable output. core_plugin_manager::reset_caches(); } if (!empty($enable) && confirm_sesskey()) { if (!($processor = $DB->get_record('message_processors', array('id' => $enable)))) { print_error('outputdoesnotexist', 'message'); } \core_message\api::update_processor_status($processor, 1); // Enable output. core_plugin_manager::reset_caches(); } if ($disable || $enable) { $url = new moodle_url('message.php'); redirect($url); } // Page settings $PAGE->set_context(context_system::instance()); // Grab the renderer $renderer = $PAGE->get_renderer('core', 'message'); // Display the manage message outputs interface $processors = get_message_processors(); $messageoutputs = $renderer->manage_messageoutputs($processors); // Display the page
/** * Test message_unblock_contact. */ public function test_message_unblock_contact() { // Set this user as the admin. $this->setAdminUser(); // Create a user to add to the admin's contact list. $user1 = $this->getDataGenerator()->create_user(); $user2 = $this->getDataGenerator()->create_user(); // Add users to the admin's contact list. message_add_contact($user1->id); message_add_contact($user2->id, 1); // Add blocked contact. $this->assertEquals(1, \core_message\api::count_blocked_users()); // Unblock user. message_unblock_contact($user2->id); $this->assertEquals(0, \core_message\api::count_blocked_users()); }
/** * 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; }