From 24a76780f633956f3be6847980bcaba9fd232b92 Mon Sep 17 00:00:00 2001 From: Ryan Wyllie Date: Tue, 2 Aug 2016 01:12:15 +0000 Subject: [PATCH] MDL-54708 message: add renderer for get_popup_notification --- message/classes/output/popup_notification.php | 149 ++++++++++++++++++ message/externallib.php | 67 ++------ 2 files changed, 159 insertions(+), 57 deletions(-) create mode 100644 message/classes/output/popup_notification.php diff --git a/message/classes/output/popup_notification.php b/message/classes/output/popup_notification.php new file mode 100644 index 00000000000..5a1260dc397 --- /dev/null +++ b/message/classes/output/popup_notification.php @@ -0,0 +1,149 @@ +. + +/** + * Contains class used to prepare a popup notification for display. + * + * @package core_message + * @copyright 2016 Ryan Wyllie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace core_message\output; + +require_once($CFG->dirroot . '/message/lib.php'); + +use renderable; +use templatable; +use moodle_url; +use core_user; + +/** + * Class to prepare a popup notification for display. + * + * @package core_message + * @copyright 2016 Ryan Wyllie + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class popup_notification implements templatable, renderable { + + /** + * The notification. + */ + protected $notification; + + /** + * Indicates if the receiver of the notification should have their + * details embedded in the output. + */ + protected $embeduserto; + + /** + * Indicates if the sender of the notification should have their + * details embedded in the output. + */ + protected $embeduserfrom; + + /** + * Indicates if the receiver of the notification should have their + * notification preferences embedded in the output. + */ + protected $embedpreference; + + /** + * A cache for the receiver's full name, if it's already known, so that + * a DB lookup isn't required. + */ + protected $usertofullname; + + /** + * Constructor. + * + * @param \stdClass $notification + */ + public function __construct($notification, $embeduserto, + $embeduserfrom, $embedpreference, $usertofullname = '') { + + $this->notification = $notification; + $this->embeduserto = $embeduserto; + $this->embeduserfrom = $embeduserfrom; + $this->embedpreference = $embedpreference; + $this->usertofullname = $usertofullname; + } + + public function export_for_template(\renderer_base $output) { + global $USER; + + $context = clone $this->notification; + + if ($context->useridto == $USER->id && $context->timeusertodeleted) { + $context->deleted = true; + } else { + $context->deleted = false; + } + + // We need to get the user from the query. + if ($this->embeduserfrom) { + // Check for non-reply and support users. + if (core_user::is_real_user($context->useridfrom)) { + $user = new \stdClass(); + $user = username_load_fields_from_object($user, $context, 'userfrom'); + $profileurl = new moodle_url('/user/profile.php', array('id' => $context->useridfrom)); + $context->userfromfullname = fullname($user); + $context->userfromprofileurl = $profileurl->out(); + } else { + $context->userfromfullname = get_string('coresystem'); + } + } + + // We need to get the user from the query. + if ($this->embeduserto) { + if (empty($this->usertofullname)) { + $user = new \stdClass(); + $user = username_load_fields_from_object($user, $context, 'userto'); + $context->usertofullname = fullname($user); + } else { + $context->usertofullname = $this->usertofullname; + } + } + + $context->timecreatedpretty = get_string('ago', 'message', format_time(time() - $context->timecreated)); + $context->text = message_format_message_text($context); + $context->read = $context->timeread ? true : false; + + if (!empty($context->component) && substr($context->component, 0, 4) == 'mod_') { + $iconurl = $output->pix_url('icon', $context->component); + } else { + $iconurl = $output->pix_url('i/marker', 'core'); + } + + $context->iconurl = $iconurl->out(); + + // We only return the logged in user's preferences, so if it isn't the sender or receiver + // of this notification then skip embedding the preferences. + if ($this->embedpreference && !empty($context->component) && !empty($context->eventtype) + && $USER->id == $context->useridto) { + $key = 'message_provider_' . $context->component . '_' . $context->eventtype; + $context->preference = array( + 'key' => $key, + 'loggedin' => get_user_preferences($key . '_loggedin', $USER->id), + 'loggedoff' => get_user_preferences($key . '_loggedoff', $USER->id), + ); + } + + return $context; + } +} diff --git a/message/externallib.php b/message/externallib.php index ade34493510..9b1b8d65061 100644 --- a/message/externallib.php +++ b/message/externallib.php @@ -1238,7 +1238,7 @@ class core_message_external extends external_api { */ public static function get_popup_notifications($useridto, $status, $embedpreference, $embeduserto, $embeduserfrom, $newestfirst, $markasread, $limit, $offset) { - global $CFG, $USER, $OUTPUT; + global $CFG, $USER, $PAGE; $params = self::validate_parameters( self::get_popup_notifications_parameters(), @@ -1268,6 +1268,7 @@ class core_message_external extends external_api { $limit = $params['limit']; $offset = $params['offset']; $issuperuser = has_capability('moodle/site:readallmessages', $context); + $renderer = $PAGE->get_renderer('core_message'); if (!empty($useridto)) { if (core_user::is_real_user($useridto)) { @@ -1286,6 +1287,7 @@ class core_message_external extends external_api { $sort = $newestfirst ? 'DESC' : 'ASC'; $notifications = message_get_popup_notifications($useridto, $status, $embeduserto, $embeduserfrom, $sort, $limit, $offset); + $notificationcontexts = []; if ($notifications) { // In some cases, we don't need to get the to user objects from the sql query. @@ -1298,69 +1300,20 @@ class core_message_external extends external_api { foreach ($notifications as $notification) { - if ($useridto == $USER->id and $notification->timeusertodeleted) { - $notification->deleted = true; - } else { - $notification->deleted = false; - } - - // We need to get the user from the query. - if ($embeduserfrom) { - // Check for non-reply and support users. - if (core_user::is_real_user($notification->useridfrom)) { - $user = new stdClass(); - $user = username_load_fields_from_object($user, $notification, 'userfrom'); - $profileurl = new moodle_url('/user/profile.php', array('id' => $notification->useridfrom)); - $notification->userfromfullname = fullname($user); - $notification->userfromprofileurl = $profileurl->out(); - } else { - $notification->userfromfullname = get_string('coresystem'); - } - } + $notificationoutput = new \core_message\output\popup_notification($notification, $embeduserto, + $embeduserfrom, $embedpreference, $usertofullname); - // We need to get the user from the query. - if ($embeduserto) { - if (empty($usertofullname)) { - $user = new stdClass(); - $user = username_load_fields_from_object($user, $notification, 'userto'); - $notification->usertofullname = fullname($user); - } else { - $notification->usertofullname = $usertofullname; - } - } - - $notification->timecreatedpretty = get_string('ago', 'message', format_time(time() - $notification->timecreated)); - $notification->text = message_format_message_text($notification); - $notification->read = $notification->timeread ? true : false; - - if (!empty($notification->component) && substr($notification->component, 0, 4) == 'mod_') { - $iconurl = $OUTPUT->pix_url('icon', $notification->component); - } else { - $iconurl = $OUTPUT->pix_url('i/marker', 'core'); - } - - $notification->iconurl = $iconurl->out(); - - // We only return the logged in user's preferences, so if it isn't the sender or receiver - // of this notification then skip embedding the preferences. - if ($embedpreference && !empty($notification->component) && !empty($notification->eventtype) && !$issuperuser) { - $key = 'message_provider_' . $notification->component . '_' . $notification->eventtype; - $notification->preference = array( - 'key' => $key, - 'loggedin' => get_user_preferences($key . '_loggedin', $USER->id), - 'loggedoff' => get_user_preferences($key . '_loggedoff', $USER->id), - ); - } + $notificationcontext = $notificationoutput->export_for_template($renderer); + $notificationcontexts[] = $notificationcontext; - if ($markasread && !$notification->read) { - // Have to clone here because this function mutates the given data. Naughty, naughty... - message_mark_message_read(clone $notification, time()); + if ($markasread && !$notificationcontext->read) { + message_mark_message_read($notification, time()); } } } return array( - 'notifications' => $notifications, + 'notifications' => $notificationcontexts, 'unreadcount' => message_count_unread_popup_notifications($useridto), ); } -- 2.17.1