diff --git a/message/amd/src/message_area_contacts.js b/message/amd/src/message_area_contacts.js index 76a95101cec..a8b5f408448 100644 --- a/message/amd/src/message_area_contacts.js +++ b/message/amd/src/message_area_contacts.js @@ -284,6 +284,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust this._numConversationsDisplayed, this._numConversationsToRetrieve); }.bind(this)).then(function(data) { numberreceived = data.contacts.length; + data.isconversation = true; return Templates.render('core_message/message_area_contacts', data); }).then(function(html, js) { // Remove the loading icon. @@ -338,6 +339,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/cust this._numContactsDisplayed, this._numContactsToRetrieve); }.bind(this)).then(function(data) { numberreceived = data.contacts.length; + data.isconversation = false; return Templates.render('core_message/message_area_contacts', data); }).then(function(html, js) { // Remove the loading icon. diff --git a/message/amd/src/message_area_search.js b/message/amd/src/message_area_search.js index a952f4bdb60..086c52f37a3 100644 --- a/message/amd/src/message_area_search.js +++ b/message/amd/src/message_area_search.js @@ -249,7 +249,7 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str' return promises[0]; }.bind(this)).then(function(data) { numberreceived = data.contacts.length; - return Templates.render('core_message/message_area_messages_search_results', data); + return Templates.render('core_message/message_area_message_search_results', data); }).then(function(html, js) { // Remove the loading icon. this.messageArea.find(SELECTORS.SEARCHRESULTSAREA + " " + @@ -294,7 +294,16 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str' "
" + html + "
", js); return promises[0]; }.bind(this)).then(function(data) { - return Templates.render('core_message/message_area_users_search_results', data); + if (data.contacts.length > 0) { + data.hascontacts = true; + } + if (data.courses.length > 0) { + data.hascourses = true; + } + if (data.noncontacts.length > 0) { + data.hasnoncontacts = true; + } + return Templates.render('core_message/message_area_user_search_results', data); }).then(function(html, js) { Templates.replaceNodeContents(this.messageArea.find(SELECTORS.SEARCHRESULTSAREA), html, js); }.bind(this)).fail(Notification.exception); @@ -337,7 +346,10 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str' return promises[0]; }.bind(this)).then(function(data) { numberreceived = data.contacts.length; - return Templates.render('core_message/message_area_users_search_results', data); + if (numberreceived > 0) { + data.hascontacts = true; + } + return Templates.render('core_message/message_area_user_search_results', data); }).then(function(html, js) { // Remove the loading icon. this.messageArea.find(SELECTORS.SEARCHRESULTSAREA + " " + diff --git a/message/classes/api.php b/message/classes/api.php index d6baed9652b..8e6a4e900e6 100644 --- a/message/classes/api.php +++ b/message/classes/api.php @@ -43,7 +43,7 @@ class api { * @param string $search The string the user is searching * @param int $limitfrom * @param int $limitnum - * @return \core_message\output\messagearea\message_search_results + * @return array */ public static function search_messages($userid, $search, $limitfrom = 0, $limitnum = 0) { global $DB; @@ -95,7 +95,7 @@ class api { $userid, $userid, $userid, $userid, '%' . $search . '%'); // Convert the messages into searchable contacts with their last message being the message that was searched. - $contacts = array(); + $conversations = array(); if ($messages = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum)) { foreach ($messages as $message) { $prefix = 'userfrom_'; @@ -108,11 +108,11 @@ class api { $message->blocked = $message->$blockedcol; $message->messageid = $message->id; - $contacts[] = \core_message\helper::create_contact($message, $prefix); + $conversations[] = helper::create_contact($message, $prefix); } } - return new \core_message\output\messagearea\message_search_results($userid, $contacts); + return $conversations; } /** @@ -123,7 +123,7 @@ class api { * @param string $search The string the user is searching * @param int $limitfrom * @param int $limitnum - * @return \core_message\output\messagearea\user_search_results + * @return array */ public static function search_users_in_course($userid, $courseid, $search, $limitfrom = 0, $limitnum = 0) { global $DB; @@ -148,11 +148,11 @@ class api { $contacts = array(); if ($users = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum)) { foreach ($users as $user) { - $contacts[] = \core_message\helper::create_contact($user); + $contacts[] = helper::create_contact($user); } } - return new \core_message\output\messagearea\user_search_results($contacts); + return $contacts; } /** @@ -161,7 +161,7 @@ class api { * @param int $userid The user id doing the searching * @param string $search The string the user is searching * @param int $limitnum - * @return \core_message\output\messagearea\user_search_results + * @return array */ public static function search_users($userid, $search, $limitnum = 0) { global $CFG, $DB; @@ -191,7 +191,7 @@ class api { if ($users = $DB->get_records_sql($sql, array('userid' => $userid, 'search' => '%' . $search . '%') + $excludeparams, 0, $limitnum)) { foreach ($users as $user) { - $contacts[] = \core_message\helper::create_contact($user); + $contacts[] = helper::create_contact($user); } } @@ -224,31 +224,30 @@ class api { if ($users = $DB->get_records_sql($sql, array('userid' => $userid, 'search' => '%' . $search . '%') + $excludeparams, 0, $limitnum)) { foreach ($users as $user) { - $noncontacts[] = \core_message\helper::create_contact($user); + $noncontacts[] = helper::create_contact($user); } } - return new \core_message\output\messagearea\user_search_results($contacts, $courses, $noncontacts); + return array($contacts, $courses, $noncontacts); } /** * Returns the contacts and their conversation to display in the contacts area. * * @param int $userid The user id - * @param int $otheruserid The id of the user we have selected, 0 if none have been selected * @param int $limitfrom * @param int $limitnum - * @return \core_message\output\messagearea\contacts + * @return array */ - public static function get_conversations($userid, $otheruserid = 0, $limitfrom = 0, $limitnum = 0) { - $arrcontacts = array(); + public static function get_conversations($userid, $limitfrom = 0, $limitnum = 0) { + $arrconversations = array(); if ($conversations = message_get_recent_conversations($userid, $limitfrom, $limitnum)) { foreach ($conversations as $conversation) { - $arrcontacts[] = \core_message\helper::create_contact($conversation); + $arrconversations[$conversation->id] = helper::create_contact($conversation); } } - return new \core_message\output\messagearea\contacts($userid, $otheruserid, $arrcontacts); + return $arrconversations; } /** @@ -257,7 +256,7 @@ class api { * @param int $userid The user id * @param int $limitfrom * @param int $limitnum - * @return \core_message\output\messagearea\contacts + * @return array */ public static function get_contacts($userid, $limitfrom = 0, $limitnum = 0) { global $DB; @@ -272,11 +271,11 @@ class api { ORDER BY " . $DB->sql_fullname(); if ($contacts = $DB->get_records_sql($sql, array('userid' => $userid), $limitfrom, $limitnum)) { foreach ($contacts as $contact) { - $arrcontacts[] = \core_message\helper::create_contact($contact); + $arrcontacts[] = helper::create_contact($contact); } } - return new \core_message\output\messagearea\contacts($userid, 0, $arrcontacts, false); + return $arrcontacts; } /** @@ -287,15 +286,15 @@ class api { * @param int $limitfrom * @param int $limitnum * @param string $sort - * @return \core_message\output\messagearea\messages + * @return array */ public static function get_messages($userid, $otheruserid, $limitfrom = 0, $limitnum = 0, $sort = 'timecreated ASC') { $arrmessages = array(); - if ($messages = \core_message\helper::get_messages($userid, $otheruserid, 0, $limitfrom, $limitnum, $sort)) { - $arrmessages = \core_message\helper::create_messages($userid, $messages); + if ($messages = helper::get_messages($userid, $otheruserid, 0, $limitfrom, $limitnum, $sort)) { + $arrmessages = helper::create_messages($userid, $messages); } - return new \core_message\output\messagearea\messages($userid, $otheruserid, $arrmessages); + return $arrmessages; } /** @@ -303,14 +302,14 @@ class api { * * @param int $userid the current user * @param int $otheruserid the other user - * @return \core_message\output\messagearea\message|null + * @return \stdClass|null */ public static function get_most_recent_message($userid, $otheruserid) { // We want two messages here so we get an accurate 'blocktime' value. - if ($messages = \core_message\helper::get_messages($userid, $otheruserid, 0, 0, 2, 'timecreated DESC')) { + if ($messages = helper::get_messages($userid, $otheruserid, 0, 0, 2, 'timecreated DESC')) { // Swap the order so we now have them in historical order. $messages = array_reverse($messages); - $arrmessages = \core_message\helper::create_messages($userid, $messages); + $arrmessages = helper::create_messages($userid, $messages); return array_pop($arrmessages); } @@ -322,7 +321,7 @@ class api { * * @param int $userid The user id * @param int $otheruserid The id of the user whose profile we want to view. - * @return \core_message\output\messagearea\profile + * @return \stdClass */ public static function get_profile($userid, $otheruserid) { global $CFG, $DB; @@ -347,7 +346,7 @@ class api { $data->profileimageurlsmall = ''; } if (isset($userfields['lastaccess'])) { - $data->isonline = \core_message\helper::is_online($userfields['lastaccess']); + $data->isonline = helper::is_online($userfields['lastaccess']); } else { $data->isonline = 0; } @@ -375,7 +374,7 @@ class api { $data->iscontact = false; } - return new \core_message\output\messagearea\profile($userid, $data); + return $data; } } @@ -448,7 +447,7 @@ class api { $DB->execute($sql, array('time' => $now, 'userid' => $userid, 'otheruserid' => $otheruserid)); // Now we need to trigger events for these. - if ($messages = \core_message\helper::get_messages($userid, $otheruserid, $now)) { + if ($messages = helper::get_messages($userid, $otheruserid, $now)) { // Loop through and trigger a deleted event. foreach ($messages as $message) { $messagetable = 'message'; diff --git a/message/classes/helper.php b/message/classes/helper.php index ead946503d8..410ce6a7890 100644 --- a/message/classes/helper.php +++ b/message/classes/helper.php @@ -72,11 +72,11 @@ class helper { } /** - * Helper function to return an array of messages renderables to display in the message area. + * Helper function to return an array of messages. * * @param int $userid * @param array $messages - * @return \core_message\output\messagearea\message[] + * @return array */ public static function create_messages($userid, $messages) { // Store the messages. @@ -113,18 +113,18 @@ class helper { $msg->displayblocktime = $displayblocktime; $msg->timecreated = $message->timecreated; $msg->timeread = $message->timeread; - $arrmessages[] = new \core_message\output\messagearea\message($msg); + $arrmessages[] = $msg; } return $arrmessages; } /** - * Helper function for creating a contact renderable. + * Helper function for creating a contact object. * * @param \stdClass $contact * @param string $prefix - * @return \core_message\output\messagearea\contact + * @return \stdClass */ public static function create_contact($contact, $prefix = '') { global $PAGE; @@ -159,7 +159,7 @@ class helper { $data->isread = isset($contact->isread) ? $contact->isread : 0; $data->unreadcount = isset($contact->unreadcount) ? $contact->unreadcount : null; - return new \core_message\output\messagearea\contact($data); + return $data; } /** diff --git a/message/classes/output/messagearea/contacts.php b/message/classes/output/messagearea/contacts.php index 6164cbc88cf..db276d67492 100644 --- a/message/classes/output/messagearea/contacts.php +++ b/message/classes/output/messagearea/contacts.php @@ -38,71 +38,56 @@ use templatable; */ class contacts implements templatable, renderable { - /** - * @var int The id of the user that the contacts belong to. - */ - public $userid; - /** * @var int The id of the user that has been selected. */ - public $otheruserid; + public $contactuserid; /** - * @var \core_message\output\messagearea\contact[] The contacts. + * @var array The contacts. */ public $contacts; - /** - * @var bool Are we storing conversations or contacts? - */ - public $isconversation; - /** * Constructor. * - * @param int $userid The id of the user the contacts belong to - * @param int $otheruserid The id of the user we are viewing - * @param \core_message\output\messagearea\contact[] $contacts - * @param bool $isconversation Are we storing conversations or contacts? + * @param int|null $contactuserid The id of the user that has been selected + * @param array $contacts */ - public function __construct($userid, $otheruserid, $contacts, $isconversation = true) { - $this->userid = $userid; - $this->otheruserid = $otheruserid; + public function __construct($contactuserid, $contacts) { + $this->contactuserid = $contactuserid; $this->contacts = $contacts; - $this->isconversation = $isconversation; } public function export_for_template(\renderer_base $output) { $data = new \stdClass(); - $data->userid = $this->userid; - $data->otheruserid = $this->otheruserid; $data->contacts = array(); $userids = array(); foreach ($this->contacts as $contact) { + $contact = new contact($contact); $contactdata = $contact->export_for_template($output); $userids[$contactdata->userid] = $contactdata->userid; // Check if the contact was selected. - if ($this->otheruserid == $contactdata->userid) { + if ($this->contactuserid == $contactdata->userid) { $contactdata->selected = true; } $data->contacts[] = $contactdata; } // Check if the other user is not part of the contacts. We may be sending a message to someone // we have not had a conversation with, so we want to add a new item to the contacts array. - if ($this->otheruserid && !isset($userids[$this->otheruserid])) { - $user = \core_user::get_user($this->otheruserid); + if ($this->contactuserid && !isset($userids[$this->contactuserid])) { + $user = \core_user::get_user($this->contactuserid); // Set an empty message so that we know we are messaging the user, and not viewing their profile. $user->smallmessage = ''; $user->useridfrom = $user->id; $contact = \core_message\helper::create_contact($user); + $contact = new contact($contact); $contactdata = $contact->export_for_template($output); $contactdata->selected = true; // Put the contact at the front. array_unshift($data->contacts, $contactdata); } - $data->isconversation = $this->isconversation; return $data; } } diff --git a/message/classes/output/messagearea/message_area.php b/message/classes/output/messagearea/message_area.php index 58b85cff449..2e64c4b4bc4 100644 --- a/message/classes/output/messagearea/message_area.php +++ b/message/classes/output/messagearea/message_area.php @@ -44,12 +44,17 @@ class message_area implements templatable, renderable { public $userid; /** - * @var \core_message\output\messagearea\contacts The contacts for the users. + * @var int The other user id. + */ + public $otheruserid; + + /** + * @var array The contacts for the users. */ public $contacts; /** - * @var \core_message\output\messagearea\messages The messages for the user. + * @var array The messages for the user. */ public $messages; @@ -62,12 +67,14 @@ class message_area implements templatable, renderable { * Constructor. * * @param int $userid The ID of the user whose contacts and messages we are viewing - * @param \core_message\output\messagearea\contacts $contacts - * @param \core_message\output\messagearea\messages|null $messages + * @param int|null $otheruserid The id of the user we are viewing, null if none + * @param array $contacts + * @param array|null $messages * @param bool $requestedconversation */ - public function __construct($userid, $contacts, $messages, $requestedconversation) { + public function __construct($userid, $otheruserid, $contacts, $messages, $requestedconversation) { $this->userid = $userid; + $this->otheruserid = $otheruserid; $this->contacts = $contacts; $this->messages = $messages; $this->requestedconversation = $requestedconversation; @@ -76,10 +83,13 @@ class message_area implements templatable, renderable { public function export_for_template(\renderer_base $output) { $data = new \stdClass(); $data->userid = $this->userid; - $data->contacts = $this->contacts->export_for_template($output); + $contacts = new contacts($this->otheruserid, $this->contacts); + $data->contacts = $contacts->export_for_template($output); if ($this->messages) { - $data->messages = $this->messages->export_for_template($output); + $messages = new messages($this->userid, $this->otheruserid, $this->messages); + $data->messages = $messages->export_for_template($output); } + $data->isconversation = true; $data->requestedconversation = $this->requestedconversation; return $data; diff --git a/message/classes/output/messagearea/message_search_results.php b/message/classes/output/messagearea/message_search_results.php index a2547ecc94c..85c47c04f80 100644 --- a/message/classes/output/messagearea/message_search_results.php +++ b/message/classes/output/messagearea/message_search_results.php @@ -39,31 +39,24 @@ use templatable; class message_search_results implements templatable, renderable { /** - * @var int The id of the user that the contacts belong to. - */ - public $userid; - - /** - * @var \core_message\output\messagearea\contact[] The list of contacts. + * @var array The list of contacts. */ public $contacts; /** * Constructor. * - * @param int $userid The id of the user the search results belong to - * @param \core_message\output\messagearea\contact[] $contacts + * @param array $contacts */ - public function __construct($userid, $contacts) { - $this->userid = $userid; + public function __construct($contacts) { $this->contacts = $contacts; } public function export_for_template(\renderer_base $output) { $data = new \stdClass(); - $data->userid = $this->userid; $data->contacts = array(); foreach ($this->contacts as $contact) { + $contact = new contact($contact); $data->contacts[] = $contact->export_for_template($output); } diff --git a/message/classes/output/messagearea/messages.php b/message/classes/output/messagearea/messages.php index adf07b406ec..1f1842ce6ef 100644 --- a/message/classes/output/messagearea/messages.php +++ b/message/classes/output/messagearea/messages.php @@ -39,7 +39,7 @@ use templatable; class messages implements templatable, renderable { /** - * @var \core_message\output\messagearea\message[] The messages. + * @var array The messages. */ public $messages; @@ -63,7 +63,7 @@ class messages implements templatable, renderable { * * @param int $currentuserid The current user we are wanting to view messages for * @param int $otheruserid The other user we are wanting to view messages for - * @param \core_message\output\messagearea\message[] $messages + * @param array $messages */ public function __construct($currentuserid, $otheruserid, $messages) { $ufields = get_all_user_name_fields(true) . ', lastaccess'; @@ -85,6 +85,7 @@ class messages implements templatable, renderable { $data->isonline = \core_message\helper::is_online($this->otheruser->lastaccess); $data->messages = array(); foreach ($this->messages as $message) { + $message = new message($message); $data->messages[] = $message->export_for_template($output); } diff --git a/message/classes/output/messagearea/profile.php b/message/classes/output/messagearea/profile.php index 53a473f4a6f..e36cf556a2a 100644 --- a/message/classes/output/messagearea/profile.php +++ b/message/classes/output/messagearea/profile.php @@ -38,11 +38,6 @@ use templatable; */ class profile implements templatable, renderable { - /** - * @var int The id of the user who is viewing the profile. - */ - public $currentuserid; - /** * @var int The id of the user we are going to view. */ @@ -96,30 +91,24 @@ class profile implements templatable, renderable { /** * Constructor. * - * @param int $currentuserid - * @param \stdClass $otheruser + * @param \stdClass $profile */ - public function __construct($currentuserid, $otheruser) { - $this->currentuserid = $currentuserid; - $this->userid = $otheruser->userid; - $this->fullname = $otheruser->fullname; - $this->isonline = $otheruser->isonline; - $this->email = $otheruser->email; - $this->country = $otheruser->country; - $this->city = $otheruser->city; - $this->profileimageurl = $otheruser->profileimageurl; - $this->profileimageurlsmall = $otheruser->profileimageurlsmall; - $this->isblocked = $otheruser->isblocked; - $this->iscontact = $otheruser->iscontact; + public function __construct($profile) { + $this->userid = $profile->userid; + $this->fullname = $profile->fullname; + $this->isonline = $profile->isonline; + $this->email = $profile->email; + $this->country = $profile->country; + $this->city = $profile->city; + $this->profileimageurl = $profile->profileimageurl; + $this->profileimageurlsmall = $profile->profileimageurlsmall; + $this->isblocked = $profile->isblocked; + $this->iscontact = $profile->iscontact; } public function export_for_template(\renderer_base $output) { - global $USER; - $data = new \stdClass(); - $data->iscurrentuser = $USER->id == $this->userid; - $data->currentuserid = $this->currentuserid; - $data->otheruserid = $this->userid; + $data->userid = $this->userid; $data->fullname = $this->fullname; $data->isonline = $this->isonline; $data->email = $this->email; diff --git a/message/classes/output/messagearea/user_search_results.php b/message/classes/output/messagearea/user_search_results.php index e4d1c045b39..c4fbdad48f2 100644 --- a/message/classes/output/messagearea/user_search_results.php +++ b/message/classes/output/messagearea/user_search_results.php @@ -39,7 +39,7 @@ use templatable; class user_search_results implements templatable, renderable { /** - * @var \core_message\output\messagearea\contact[] The list of contacts. + * @var array The list of contacts. */ public $contacts; @@ -49,16 +49,16 @@ class user_search_results implements templatable, renderable { public $courses; /** - * @var \core_message\output\messagearea\contact[] The list of non-contacts. + * @var array The list of non-contacts. */ public $noncontacts; /** * Constructor. * - * @param \core_message\output\messagearea\contact[] $contacts + * @param array $contacts * @param array $courses - * @param \core_message\output\messagearea\contact[] $noncontacts + * @param array $noncontacts */ public function __construct($contacts, $courses = array(), $noncontacts = array()) { $this->contacts = $contacts; @@ -80,6 +80,7 @@ class user_search_results implements templatable, renderable { if (!empty($this->contacts)) { $data->hascontacts = true; foreach ($this->contacts as $contact) { + $contact = new contact($contact); $data->contacts[] = $contact->export_for_template($output); } } @@ -94,6 +95,7 @@ class user_search_results implements templatable, renderable { if (!empty($this->noncontacts)) { $data->hasnoncontacts = true; foreach ($this->noncontacts as $noncontact) { + $noncontact = new contact($noncontact); $data->noncontacts[] = $noncontact->export_for_template($output); } } diff --git a/message/externallib.php b/message/externallib.php index 326283d260f..4bed642ffdc 100644 --- a/message/externallib.php +++ b/message/externallib.php @@ -499,7 +499,7 @@ class core_message_external extends external_api { * @since 3.2 */ public static function data_for_messagearea_search_users_in_course($userid, $courseid, $search, $limitfrom = 0, - $limitnum = 0) { + $limitnum = 0) { global $CFG, $PAGE, $USER; // Check if messaging is enabled. @@ -523,10 +523,11 @@ class core_message_external extends external_api { throw new moodle_exception('You do not have permission to perform this action.'); } - $search = \core_message\api::search_users_in_course($userid, $courseid, $search, $limitfrom, $limitnum); + $users = \core_message\api::search_users_in_course($userid, $courseid, $search, $limitfrom, $limitnum); + $results = new \core_message\output\messagearea\user_search_results($users); $renderer = $PAGE->get_renderer('core_message'); - return $search->export_for_template($renderer); + return $results->export_for_template($renderer); } /** @@ -538,7 +539,6 @@ class core_message_external extends external_api { public static function data_for_messagearea_search_users_in_course_returns() { return new external_single_structure( array( - 'hascontacts' => new external_value(PARAM_BOOL, 'Are there contacts?'), 'contacts' => new external_multiple_structure( self::get_messagearea_contact_structure() ), @@ -594,7 +594,8 @@ class core_message_external extends external_api { throw new moodle_exception('You do not have permission to perform this action.'); } - $search = \core_message\api::search_users($userid, $search, $limitnum); + list($contacts, $courses, $noncontacts) = \core_message\api::search_users($userid, $search, $limitnum); + $search = new \core_message\output\messagearea\user_search_results($contacts, $courses, $noncontacts); $renderer = $PAGE->get_renderer('core_message'); return $search->export_for_template($renderer); @@ -609,11 +610,9 @@ class core_message_external extends external_api { public static function data_for_messagearea_search_users_returns() { return new external_single_structure( array( - 'hascontacts' => new external_value(PARAM_BOOL, 'Are there contacts?'), 'contacts' => new external_multiple_structure( self::get_messagearea_contact_structure() ), - 'hascourses' => new external_value(PARAM_BOOL, 'Are there courses?'), 'courses' => new external_multiple_structure( new external_single_structure( array( @@ -623,7 +622,6 @@ class core_message_external extends external_api { ) ) ), - 'hasnoncontacts' => new external_value(PARAM_BOOL, 'Are there non-contacts?'), 'noncontacts' => new external_multiple_structure( self::get_messagearea_contact_structure() ) @@ -683,10 +681,11 @@ class core_message_external extends external_api { throw new moodle_exception('You do not have permission to perform this action.'); } - $search = \core_message\api::search_messages($userid, $search, $limitfrom, $limitnum); + $messages = \core_message\api::search_messages($userid, $search, $limitfrom, $limitnum); + $results = new \core_message\output\messagearea\message_search_results($messages); $renderer = $PAGE->get_renderer('core_message'); - return $search->export_for_template($renderer); + return $results->export_for_template($renderer); } /** @@ -698,7 +697,6 @@ class core_message_external extends external_api { public static function data_for_messagearea_search_messages_returns() { return new external_single_structure( array( - 'userid' => new external_value(PARAM_INT, 'The id of the user who we are viewing conversations for'), 'contacts' => new external_multiple_structure( self::get_messagearea_contact_structure() ) @@ -754,10 +752,11 @@ class core_message_external extends external_api { throw new moodle_exception('You do not have permission to perform this action.'); } - $contacts = \core_message\api::get_conversations($userid, 0, $limitfrom, $limitnum); + $conversations = \core_message\api::get_conversations($userid, $limitfrom, $limitnum); + $conversations = new \core_message\output\messagearea\contacts(null, $conversations); $renderer = $PAGE->get_renderer('core_message'); - return $contacts->export_for_template($renderer); + return $conversations->export_for_template($renderer); } /** @@ -769,8 +768,6 @@ class core_message_external extends external_api { public static function data_for_messagearea_conversations_returns() { return new external_single_structure( array( - 'userid' => new external_value(PARAM_INT, 'The id of the user who we are viewing conversations for'), - 'isconversation' => new external_value(PARAM_BOOL, 'Are we storing conversations or contacts?'), 'contacts' => new external_multiple_structure( self::get_messagearea_contact_structure() ) @@ -821,6 +818,7 @@ class core_message_external extends external_api { } $contacts = \core_message\api::get_contacts($userid, $limitfrom, $limitnum); + $contacts = new \core_message\output\messagearea\contacts(null, $contacts); $renderer = $PAGE->get_renderer('core_message'); return $contacts->export_for_template($renderer); @@ -897,6 +895,7 @@ class core_message_external extends external_api { $sort = 'timecreated ASC'; } $messages = \core_message\api::get_messages($currentuserid, $otheruserid, $limitfrom, $limitnum, $sort); + $messages = new \core_message\output\messagearea\messages($currentuserid, $otheruserid, $messages); $renderer = $PAGE->get_renderer('core_message'); return $messages->export_for_template($renderer); @@ -970,6 +969,7 @@ class core_message_external extends external_api { } $message = \core_message\api::get_most_recent_message($currentuserid, $otheruserid); + $message = new \core_message\output\messagearea\message($message); $renderer = $PAGE->get_renderer('core_message'); return $message->export_for_template($renderer); @@ -1031,6 +1031,7 @@ class core_message_external extends external_api { } $profile = \core_message\api::get_profile($currentuserid, $otheruserid); + $profile = new \core_message\output\messagearea\profile($profile); $renderer = $PAGE->get_renderer('core_message'); return $profile->export_for_template($renderer); @@ -1045,10 +1046,7 @@ class core_message_external extends external_api { public static function data_for_messagearea_get_profile_returns() { return new external_single_structure( array( - 'iscurrentuser' => new external_value(PARAM_BOOL, 'Is the currently logged in user the user we are viewing - the profile on behalf of?'), - 'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'), - 'otheruserid' => new external_value(PARAM_INT, 'The id of the user whose profile we are viewing'), + 'userid' => new external_value(PARAM_INT, 'The id of the user whose profile we are viewing'), 'email' => new external_value(core_user::get_property_type('email'), 'An email address'), 'country' => new external_value(core_user::get_property_type('country'), 'Home country code of the user'), 'city' => new external_value(core_user::get_property_type('city'), 'Home city of the user'), diff --git a/message/index.php b/message/index.php index 3a060ad3c2a..38bc5d73b53 100644 --- a/message/index.php +++ b/message/index.php @@ -98,39 +98,37 @@ $settings->make_active(); // Get the renderer and the information we are going to be use. $renderer = $PAGE->get_renderer('core_message'); $requestedconversation = false; +$conversations = \core_message\api::get_conversations($user1->id, 0, 20); +$messages = null; if (!$user2realuser) { - $conversations = \core_message\api::get_conversations($user1->id, 0, 0, 20); - $contacts = $conversations->contacts; - - if (!empty($contacts)) { - // If there are conversations then render the most recent one by default. - $contact = reset($contacts); - $otheruserid = $contact->userid; - $conversations->otheruserid = $otheruserid; - - // Mark the conversation as read. - if ($currentuser) { - $contact->isread = 1; - \core_message\api::mark_all_read_for_user($user1->id, $otheruserid); - } - - $messages = \core_message\api::get_messages($user1->id, $otheruserid, 0, 20, 'timecreated DESC'); - } else { - $messages = null; + // If there are conversations, but the user has not chosen a particular one, then render the most recent one. + $user2 = new stdClass(); + $user2->id = null; + if (!empty($conversations)) { + $contact = reset($conversations); + $user2->id = $contact->userid; } } else { - // Mark the conversation as read. - if ($currentuser) { - \core_message\api::mark_all_read_for_user($user1->id, $user2->id); - } - - $conversations = \core_message\api::get_conversations($user1->id, $user2->id, 0, 20); - $messages = \core_message\api::get_messages($user1->id, $user2->id); - // The user has specifically requested to see this conversation. Add the flag - // to the context so that we can render the messaging app appropriately. + // 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; } -$messagearea = new \core_message\output\messagearea\message_area($user1->id, $conversations, $messages, $requestedconversation); + +// Mark the conversation as read. +if (!empty($user2->id)) { + if ($currentuser) { + // 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(); diff --git a/message/templates/message_area_messages_search_results.mustache b/message/templates/message_area_message_search_results.mustache similarity index 100% rename from message/templates/message_area_messages_search_results.mustache rename to message/templates/message_area_message_search_results.mustache diff --git a/message/templates/message_area_profile.mustache b/message/templates/message_area_profile.mustache index e719105aba9..722aa87aecf 100644 --- a/message/templates/message_area_profile.mustache +++ b/message/templates/message_area_profile.mustache @@ -3,7 +3,7 @@ -
+
@@ -24,12 +24,7 @@ {{#str}}viewprofile{{/str}}
- {{#iscurrentuser}} - {{#str}}sendmessage, message{{/str}} - {{/iscurrentuser}} - {{^iscurrentuser}} - {{#str}}viewconversation, message{{/str}} - {{/iscurrentuser}} + {{#str}}viewconversation, message{{/str}}
{{#isblocked}} diff --git a/message/templates/message_area_users_search_results.mustache b/message/templates/message_area_user_search_results.mustache similarity index 100% rename from message/templates/message_area_users_search_results.mustache rename to message/templates/message_area_user_search_results.mustache