PageRenderTime 56ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/message/lib.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 2540 lines | 1557 code | 389 blank | 594 comment | 359 complexity | 8574035bd86cb2b217b9ea7f19ffeedb MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Library functions for messaging
  18. *
  19. * @package core_message
  20. * @copyright 2008 Luis Rodrigues
  21. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  22. */
  23. require_once($CFG->libdir.'/eventslib.php');
  24. define ('MESSAGE_SHORTLENGTH', 300);
  25. define ('MESSAGE_DISCUSSION_WIDTH',600);
  26. define ('MESSAGE_DISCUSSION_HEIGHT',500);
  27. define ('MESSAGE_SHORTVIEW_LIMIT', 8);//the maximum number of messages to show on the short message history
  28. define('MESSAGE_HISTORY_SHORT',0);
  29. define('MESSAGE_HISTORY_ALL',1);
  30. define('MESSAGE_VIEW_UNREAD_MESSAGES','unread');
  31. define('MESSAGE_VIEW_RECENT_CONVERSATIONS','recentconversations');
  32. define('MESSAGE_VIEW_RECENT_NOTIFICATIONS','recentnotifications');
  33. define('MESSAGE_VIEW_CONTACTS','contacts');
  34. define('MESSAGE_VIEW_BLOCKED','blockedusers');
  35. define('MESSAGE_VIEW_COURSE','course_');
  36. define('MESSAGE_VIEW_SEARCH','search');
  37. define('MESSAGE_SEARCH_MAX_RESULTS', 200);
  38. define('MESSAGE_CONTACTS_PER_PAGE',10);
  39. define('MESSAGE_MAX_COURSE_NAME_LENGTH', 30);
  40. /**
  41. * Define contants for messaging default settings population. For unambiguity of
  42. * plugin developer intentions we use 4-bit value (LSB numbering):
  43. * bit 0 - whether to send message when user is loggedin (MESSAGE_DEFAULT_LOGGEDIN)
  44. * bit 1 - whether to send message when user is loggedoff (MESSAGE_DEFAULT_LOGGEDOFF)
  45. * bit 2..3 - messaging permission (MESSAGE_DISALLOWED|MESSAGE_PERMITTED|MESSAGE_FORCED)
  46. *
  47. * MESSAGE_PERMITTED_MASK contains the mask we use to distinguish permission setting
  48. */
  49. define('MESSAGE_DEFAULT_LOGGEDIN', 0x01); // 0001
  50. define('MESSAGE_DEFAULT_LOGGEDOFF', 0x02); // 0010
  51. define('MESSAGE_DISALLOWED', 0x04); // 0100
  52. define('MESSAGE_PERMITTED', 0x08); // 1000
  53. define('MESSAGE_FORCED', 0x0c); // 1100
  54. define('MESSAGE_PERMITTED_MASK', 0x0c); // 1100
  55. /**
  56. * Set default value for default outputs permitted setting
  57. */
  58. define('MESSAGE_DEFAULT_PERMITTED', 'permitted');
  59. /**
  60. * Print the selector that allows the user to view their contacts, course participants, their recent
  61. * conversations etc
  62. *
  63. * @param int $countunreadtotal how many unread messages does the user have?
  64. * @param int $viewing What is the user viewing? ie MESSAGE_VIEW_UNREAD_MESSAGES, MESSAGE_VIEW_SEARCH etc
  65. * @param object $user1 the user whose messages are being viewed
  66. * @param object $user2 the user $user1 is talking to
  67. * @param array $blockedusers an array of users blocked by $user1
  68. * @param array $onlinecontacts an array of $user1's online contacts
  69. * @param array $offlinecontacts an array of $user1's offline contacts
  70. * @param array $strangers an array of users who have messaged $user1 who aren't contacts
  71. * @param bool $showactionlinks show action links (add/remove contact etc)
  72. * @param int $page if there are so many users listed that they have to be split into pages what page are we viewing
  73. * @return void
  74. */
  75. function message_print_contact_selector($countunreadtotal, $viewing, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showactionlinks, $page=0) {
  76. global $PAGE;
  77. echo html_writer::start_tag('div', array('class' => 'contactselector mdl-align'));
  78. //if 0 unread messages and they've requested unread messages then show contacts
  79. if ($countunreadtotal == 0 && $viewing == MESSAGE_VIEW_UNREAD_MESSAGES) {
  80. $viewing = MESSAGE_VIEW_CONTACTS;
  81. }
  82. //if they have no blocked users and they've requested blocked users switch them over to contacts
  83. if (count($blockedusers) == 0 && $viewing == MESSAGE_VIEW_BLOCKED) {
  84. $viewing = MESSAGE_VIEW_CONTACTS;
  85. }
  86. $onlyactivecourses = true;
  87. $courses = enrol_get_users_courses($user1->id, $onlyactivecourses);
  88. $coursecontexts = message_get_course_contexts($courses);//we need one of these again so holding on to them
  89. $strunreadmessages = null;
  90. if ($countunreadtotal>0) { //if there are unread messages
  91. $strunreadmessages = get_string('unreadmessages','message', $countunreadtotal);
  92. }
  93. message_print_usergroup_selector($viewing, $courses, $coursecontexts, $countunreadtotal, count($blockedusers), $strunreadmessages, $user1);
  94. if ($viewing == MESSAGE_VIEW_UNREAD_MESSAGES) {
  95. message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $PAGE->url, 1, $showactionlinks,$strunreadmessages, $user2);
  96. } else if ($viewing == MESSAGE_VIEW_CONTACTS || $viewing == MESSAGE_VIEW_SEARCH || $viewing == MESSAGE_VIEW_RECENT_CONVERSATIONS || $viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) {
  97. message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $PAGE->url, 0, $showactionlinks, $strunreadmessages, $user2);
  98. } else if ($viewing == MESSAGE_VIEW_BLOCKED) {
  99. message_print_blocked_users($blockedusers, $PAGE->url, $showactionlinks, null, $user2);
  100. } else if (substr($viewing, 0, 7) == MESSAGE_VIEW_COURSE) {
  101. $courseidtoshow = intval(substr($viewing, 7));
  102. if (!empty($courseidtoshow)
  103. && array_key_exists($courseidtoshow, $coursecontexts)
  104. && has_capability('moodle/course:viewparticipants', $coursecontexts[$courseidtoshow])) {
  105. message_print_participants($coursecontexts[$courseidtoshow], $courseidtoshow, $PAGE->url, $showactionlinks, null, $page, $user2);
  106. } else {
  107. //shouldn't get here. User trying to access a course they're not in perhaps.
  108. add_to_log(SITEID, 'message', 'view', 'index.php', $viewing);
  109. }
  110. }
  111. // Only show the search button if we're viewing our own messages.
  112. // Search isn't currently able to deal with user A wanting to search user B's messages.
  113. if ($showactionlinks) {
  114. echo html_writer::start_tag('form', array('action' => 'index.php','method' => 'GET'));
  115. echo html_writer::start_tag('fieldset');
  116. $managebuttonclass = 'visible';
  117. if ($viewing == MESSAGE_VIEW_SEARCH) {
  118. $managebuttonclass = 'hiddenelement';
  119. }
  120. $strmanagecontacts = get_string('search','message');
  121. echo html_writer::empty_tag('input', array('type' => 'hidden','name' => 'viewing','value' => MESSAGE_VIEW_SEARCH));
  122. echo html_writer::empty_tag('input', array('type' => 'submit','value' => $strmanagecontacts,'class' => $managebuttonclass));
  123. echo html_writer::end_tag('fieldset');
  124. echo html_writer::end_tag('form');
  125. }
  126. echo html_writer::end_tag('div');
  127. }
  128. /**
  129. * Print course participants. Called by message_print_contact_selector()
  130. *
  131. * @param object $context the course context
  132. * @param int $courseid the course ID
  133. * @param string $contactselecturl the url to send the user to when a contact's name is clicked
  134. * @param bool $showactionlinks show action links (add/remove contact etc) next to the users
  135. * @param string $titletodisplay Optionally specify a title to display above the participants
  136. * @param int $page if there are so many users listed that they have to be split into pages what page are we viewing
  137. * @param object $user2 the user $user1 is talking to. They will be highlighted if they appear in the list of participants
  138. * @return void
  139. */
  140. function message_print_participants($context, $courseid, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null, $page=0, $user2=null) {
  141. global $DB, $USER, $PAGE, $OUTPUT;
  142. if (empty($titletodisplay)) {
  143. $titletodisplay = get_string('participants');
  144. }
  145. $countparticipants = count_enrolled_users($context);
  146. list($esql, $params) = get_enrolled_sql($context);
  147. $params['mcuserid'] = $USER->id;
  148. $ufields = user_picture::fields('u');
  149. $sql = "SELECT $ufields, mc.id as contactlistid, mc.blocked
  150. FROM {user} u
  151. JOIN ($esql) je ON je.id = u.id
  152. LEFT JOIN {message_contacts} mc ON mc.contactid = u.id AND mc.userid = :mcuserid
  153. WHERE u.deleted = 0";
  154. $participants = $DB->get_records_sql($sql, $params, $page * MESSAGE_CONTACTS_PER_PAGE, MESSAGE_CONTACTS_PER_PAGE);
  155. $pagingbar = new paging_bar($countparticipants, $page, MESSAGE_CONTACTS_PER_PAGE, $PAGE->url, 'page');
  156. echo $OUTPUT->render($pagingbar);
  157. echo html_writer::start_tag('table', array('id' => 'message_participants', 'class' => 'boxaligncenter', 'cellspacing' => '2', 'cellpadding' => '0', 'border' => '0'));
  158. echo html_writer::start_tag('tr');
  159. echo html_writer::tag('td', $titletodisplay, array('colspan' => 3, 'class' => 'heading'));
  160. echo html_writer::end_tag('tr');
  161. foreach ($participants as $participant) {
  162. if ($participant->id != $USER->id) {
  163. $iscontact = false;
  164. $isblocked = false;
  165. if ( $participant->contactlistid ) {
  166. if ($participant->blocked == 0) {
  167. // Is contact. Is not blocked.
  168. $iscontact = true;
  169. $isblocked = false;
  170. } else {
  171. // Is blocked.
  172. $iscontact = false;
  173. $isblocked = true;
  174. }
  175. }
  176. $participant->messagecount = 0;//todo it would be nice if the course participant could report new messages
  177. message_print_contactlist_user($participant, $iscontact, $isblocked, $contactselecturl, $showactionlinks, $user2);
  178. }
  179. }
  180. echo html_writer::end_tag('table');
  181. }
  182. /**
  183. * Retrieve users blocked by $user1
  184. *
  185. * @param object $user1 the user whose messages are being viewed
  186. * @param object $user2 the user $user1 is talking to. If they are being blocked
  187. * they will have a variable called 'isblocked' added to their user object
  188. * @return array the users blocked by $user1
  189. */
  190. function message_get_blocked_users($user1=null, $user2=null) {
  191. global $DB, $USER;
  192. if (empty($user1)) {
  193. $user1 = $USER;
  194. }
  195. if (!empty($user2)) {
  196. $user2->isblocked = false;
  197. }
  198. $blockedusers = array();
  199. $userfields = user_picture::fields('u', array('lastaccess'));
  200. $blockeduserssql = "SELECT $userfields, COUNT(m.id) AS messagecount
  201. FROM {message_contacts} mc
  202. JOIN {user} u ON u.id = mc.contactid
  203. LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = :user1id1
  204. WHERE mc.userid = :user1id2 AND mc.blocked = 1
  205. GROUP BY $userfields
  206. ORDER BY u.firstname ASC";
  207. $rs = $DB->get_recordset_sql($blockeduserssql, array('user1id1' => $user1->id, 'user1id2' => $user1->id));
  208. foreach($rs as $rd) {
  209. $blockedusers[] = $rd;
  210. if (!empty($user2) && $user2->id == $rd->id) {
  211. $user2->isblocked = true;
  212. }
  213. }
  214. $rs->close();
  215. return $blockedusers;
  216. }
  217. /**
  218. * Print users blocked by $user1. Called by message_print_contact_selector()
  219. *
  220. * @param array $blockedusers the users blocked by $user1
  221. * @param string $contactselecturl the url to send the user to when a contact's name is clicked
  222. * @param bool $showactionlinks show action links (add/remove contact etc) next to the users
  223. * @param string $titletodisplay Optionally specify a title to display above the participants
  224. * @param object $user2 the user $user1 is talking to. They will be highlighted if they appear in the list of blocked users
  225. * @return void
  226. */
  227. function message_print_blocked_users($blockedusers, $contactselecturl=null, $showactionlinks=true, $titletodisplay=null, $user2=null) {
  228. global $DB, $USER;
  229. $countblocked = count($blockedusers);
  230. echo html_writer::start_tag('table', array('id' => 'message_contacts', 'class' => 'boxaligncenter'));
  231. if (!empty($titletodisplay)) {
  232. echo html_writer::start_tag('tr');
  233. echo html_writer::tag('td', $titletodisplay, array('colspan' => 3, 'class' => 'heading'));
  234. echo html_writer::end_tag('tr');
  235. }
  236. if ($countblocked) {
  237. echo html_writer::start_tag('tr');
  238. echo html_writer::tag('td', get_string('blockedusers', 'message', $countblocked), array('colspan' => 3, 'class' => 'heading'));
  239. echo html_writer::end_tag('tr');
  240. $isuserblocked = true;
  241. $isusercontact = false;
  242. foreach ($blockedusers as $blockeduser) {
  243. message_print_contactlist_user($blockeduser, $isusercontact, $isuserblocked, $contactselecturl, $showactionlinks, $user2);
  244. }
  245. }
  246. echo html_writer::end_tag('table');
  247. }
  248. /**
  249. * Retrieve $user1's contacts (online, offline and strangers)
  250. *
  251. * @param object $user1 the user whose messages are being viewed
  252. * @param object $user2 the user $user1 is talking to. If they are a contact
  253. * they will have a variable called 'iscontact' added to their user object
  254. * @return array containing 3 arrays. array($onlinecontacts, $offlinecontacts, $strangers)
  255. */
  256. function message_get_contacts($user1=null, $user2=null) {
  257. global $DB, $CFG, $USER;
  258. if (empty($user1)) {
  259. $user1 = $USER;
  260. }
  261. if (!empty($user2)) {
  262. $user2->iscontact = false;
  263. }
  264. $timetoshowusers = 300; //Seconds default
  265. if (isset($CFG->block_online_users_timetosee)) {
  266. $timetoshowusers = $CFG->block_online_users_timetosee * 60;
  267. }
  268. // time which a user is counting as being active since
  269. $timefrom = time()-$timetoshowusers;
  270. // people in our contactlist who are online
  271. $onlinecontacts = array();
  272. // people in our contactlist who are offline
  273. $offlinecontacts = array();
  274. // people who are not in our contactlist but have sent us a message
  275. $strangers = array();
  276. $userfields = user_picture::fields('u', array('lastaccess'));
  277. // get all in our contactlist who are not blocked in our contact list
  278. // and count messages we have waiting from each of them
  279. $contactsql = "SELECT $userfields, COUNT(m.id) AS messagecount
  280. FROM {message_contacts} mc
  281. JOIN {user} u ON u.id = mc.contactid
  282. LEFT OUTER JOIN {message} m ON m.useridfrom = mc.contactid AND m.useridto = ?
  283. WHERE mc.userid = ? AND mc.blocked = 0
  284. GROUP BY $userfields
  285. ORDER BY u.firstname ASC";
  286. $rs = $DB->get_recordset_sql($contactsql, array($user1->id, $user1->id));
  287. foreach ($rs as $rd) {
  288. if ($rd->lastaccess >= $timefrom) {
  289. // they have been active recently, so are counted online
  290. $onlinecontacts[] = $rd;
  291. } else {
  292. $offlinecontacts[] = $rd;
  293. }
  294. if (!empty($user2) && $user2->id == $rd->id) {
  295. $user2->iscontact = true;
  296. }
  297. }
  298. $rs->close();
  299. // get messages from anyone who isn't in our contact list and count the number
  300. // of messages we have from each of them
  301. $strangersql = "SELECT $userfields, count(m.id) as messagecount
  302. FROM {message} m
  303. JOIN {user} u ON u.id = m.useridfrom
  304. LEFT OUTER JOIN {message_contacts} mc ON mc.contactid = m.useridfrom AND mc.userid = m.useridto
  305. WHERE mc.id IS NULL AND m.useridto = ?
  306. GROUP BY $userfields
  307. ORDER BY u.firstname ASC";
  308. $rs = $DB->get_recordset_sql($strangersql, array($USER->id));
  309. // Add user id as array index, so supportuser and noreply user don't get duplicated (if they are real users).
  310. foreach ($rs as $rd) {
  311. $strangers[$rd->id] = $rd;
  312. }
  313. $rs->close();
  314. // Add noreply user and support user to the list, if they don't exist.
  315. $supportuser = core_user::get_support_user();
  316. if (!isset($strangers[$supportuser->id])) {
  317. $supportuser->messagecount = message_count_unread_messages($USER, $supportuser);
  318. if ($supportuser->messagecount > 0) {
  319. $strangers[$supportuser->id] = $supportuser;
  320. }
  321. }
  322. $noreplyuser = core_user::get_noreply_user();
  323. if (!isset($strangers[$noreplyuser->id])) {
  324. $noreplyuser->messagecount = message_count_unread_messages($USER, $noreplyuser);
  325. if ($noreplyuser->messagecount > 0) {
  326. $strangers[$noreplyuser->id] = $noreplyuser;
  327. }
  328. }
  329. return array($onlinecontacts, $offlinecontacts, $strangers);
  330. }
  331. /**
  332. * Print $user1's contacts. Called by message_print_contact_selector()
  333. *
  334. * @param array $onlinecontacts $user1's contacts which are online
  335. * @param array $offlinecontacts $user1's contacts which are offline
  336. * @param array $strangers users which are not contacts but who have messaged $user1
  337. * @param string $contactselecturl the url to send the user to when a contact's name is clicked
  338. * @param int $minmessages The minimum number of unread messages required from a user for them to be displayed
  339. * Typically 0 (show all contacts) or 1 (only show contacts from whom we have a new message)
  340. * @param bool $showactionlinks show action links (add/remove contact etc) next to the users
  341. * @param string $titletodisplay Optionally specify a title to display above the participants
  342. * @param object $user2 the user $user1 is talking to. They will be highlighted if they appear in the list of contacts
  343. * @return void
  344. */
  345. function message_print_contacts($onlinecontacts, $offlinecontacts, $strangers, $contactselecturl=null, $minmessages=0, $showactionlinks=true, $titletodisplay=null, $user2=null) {
  346. global $CFG, $PAGE, $OUTPUT;
  347. $countonlinecontacts = count($onlinecontacts);
  348. $countofflinecontacts = count($offlinecontacts);
  349. $countstrangers = count($strangers);
  350. $isuserblocked = null;
  351. if ($countonlinecontacts + $countofflinecontacts == 0) {
  352. echo html_writer::tag('div', get_string('contactlistempty', 'message'), array('class' => 'heading'));
  353. }
  354. echo html_writer::start_tag('table', array('id' => 'message_contacts', 'class' => 'boxaligncenter'));
  355. if (!empty($titletodisplay)) {
  356. message_print_heading($titletodisplay);
  357. }
  358. if($countonlinecontacts) {
  359. // Print out list of online contacts.
  360. if (empty($titletodisplay)) {
  361. message_print_heading(get_string('onlinecontacts', 'message', $countonlinecontacts));
  362. }
  363. $isuserblocked = false;
  364. $isusercontact = true;
  365. foreach ($onlinecontacts as $contact) {
  366. if ($minmessages == 0 || $contact->messagecount >= $minmessages) {
  367. message_print_contactlist_user($contact, $isusercontact, $isuserblocked, $contactselecturl, $showactionlinks, $user2);
  368. }
  369. }
  370. }
  371. if ($countofflinecontacts) {
  372. // Print out list of offline contacts.
  373. if (empty($titletodisplay)) {
  374. message_print_heading(get_string('offlinecontacts', 'message', $countofflinecontacts));
  375. }
  376. $isuserblocked = false;
  377. $isusercontact = true;
  378. foreach ($offlinecontacts as $contact) {
  379. if ($minmessages == 0 || $contact->messagecount >= $minmessages) {
  380. message_print_contactlist_user($contact, $isusercontact, $isuserblocked, $contactselecturl, $showactionlinks, $user2);
  381. }
  382. }
  383. }
  384. // Print out list of incoming contacts.
  385. if ($countstrangers) {
  386. message_print_heading(get_string('incomingcontacts', 'message', $countstrangers));
  387. $isuserblocked = false;
  388. $isusercontact = false;
  389. foreach ($strangers as $stranger) {
  390. if ($minmessages == 0 || $stranger->messagecount >= $minmessages) {
  391. message_print_contactlist_user($stranger, $isusercontact, $isuserblocked, $contactselecturl, $showactionlinks, $user2);
  392. }
  393. }
  394. }
  395. echo html_writer::end_tag('table');
  396. if ($countstrangers && ($countonlinecontacts + $countofflinecontacts == 0)) { // Extra help
  397. echo html_writer::tag('div','('.get_string('addsomecontactsincoming', 'message').')',array('class' => 'note'));
  398. }
  399. }
  400. /**
  401. * Print a select box allowing the user to choose to view new messages, course participants etc.
  402. *
  403. * Called by message_print_contact_selector()
  404. * @param int $viewing What page is the user viewing ie MESSAGE_VIEW_UNREAD_MESSAGES, MESSAGE_VIEW_RECENT_CONVERSATIONS etc
  405. * @param array $courses array of course objects. The courses the user is enrolled in.
  406. * @param array $coursecontexts array of course contexts. Keyed on course id.
  407. * @param int $countunreadtotal how many unread messages does the user have?
  408. * @param int $countblocked how many users has the current user blocked?
  409. * @param stdClass $user1 The user whose messages we are viewing.
  410. * @param string $strunreadmessages a preconstructed message about the number of unread messages the user has
  411. * @return void
  412. */
  413. function message_print_usergroup_selector($viewing, $courses, $coursecontexts, $countunreadtotal, $countblocked, $strunreadmessages, $user1 = null) {
  414. $options = array();
  415. if ($countunreadtotal>0) { //if there are unread messages
  416. $options[MESSAGE_VIEW_UNREAD_MESSAGES] = $strunreadmessages;
  417. }
  418. $str = get_string('contacts', 'message');
  419. $options[MESSAGE_VIEW_CONTACTS] = $str;
  420. $options[MESSAGE_VIEW_RECENT_CONVERSATIONS] = get_string('mostrecentconversations', 'message');
  421. $options[MESSAGE_VIEW_RECENT_NOTIFICATIONS] = get_string('mostrecentnotifications', 'message');
  422. if (!empty($courses)) {
  423. $courses_options = array();
  424. foreach($courses as $course) {
  425. if (has_capability('moodle/course:viewparticipants', $coursecontexts[$course->id])) {
  426. //Not using short_text() as we want the end of the course name. Not the beginning.
  427. $shortname = format_string($course->shortname, true, array('context' => $coursecontexts[$course->id]));
  428. if (core_text::strlen($shortname) > MESSAGE_MAX_COURSE_NAME_LENGTH) {
  429. $courses_options[MESSAGE_VIEW_COURSE.$course->id] = '...'.core_text::substr($shortname, -MESSAGE_MAX_COURSE_NAME_LENGTH);
  430. } else {
  431. $courses_options[MESSAGE_VIEW_COURSE.$course->id] = $shortname;
  432. }
  433. }
  434. }
  435. if (!empty($courses_options)) {
  436. $options[] = array(get_string('courses') => $courses_options);
  437. }
  438. }
  439. if ($countblocked>0) {
  440. $str = get_string('blockedusers','message', $countblocked);
  441. $options[MESSAGE_VIEW_BLOCKED] = $str;
  442. }
  443. echo html_writer::start_tag('form', array('id' => 'usergroupform','method' => 'get','action' => ''));
  444. echo html_writer::start_tag('fieldset');
  445. if ( !empty($user1) && !empty($user1->id) ) {
  446. echo html_writer::empty_tag('input', array('type' => 'hidden','name' => 'user1','value' => $user1->id));
  447. }
  448. echo html_writer::label(get_string('messagenavigation', 'message'), 'viewing');
  449. echo html_writer::select($options, 'viewing', $viewing, false, array('id' => 'viewing','onchange' => 'this.form.submit()'));
  450. echo html_writer::end_tag('fieldset');
  451. echo html_writer::end_tag('form');
  452. }
  453. /**
  454. * Load the course contexts for all of the users courses
  455. *
  456. * @param array $courses array of course objects. The courses the user is enrolled in.
  457. * @return array of course contexts
  458. */
  459. function message_get_course_contexts($courses) {
  460. $coursecontexts = array();
  461. foreach($courses as $course) {
  462. $coursecontexts[$course->id] = context_course::instance($course->id);
  463. }
  464. return $coursecontexts;
  465. }
  466. /**
  467. * strip off action parameters like 'removecontact'
  468. *
  469. * @param moodle_url/string $moodleurl a URL. Typically the current page URL.
  470. * @return string the URL minus parameters that perform actions (like adding/removing/blocking a contact).
  471. */
  472. function message_remove_url_params($moodleurl) {
  473. $newurl = new moodle_url($moodleurl);
  474. $newurl->remove_params('addcontact','removecontact','blockcontact','unblockcontact');
  475. return $newurl->out();
  476. }
  477. /**
  478. * Count the number of messages with a field having a specified value.
  479. * if $field is empty then return count of the whole array
  480. * if $field is non-existent then return 0
  481. *
  482. * @param array $messagearray array of message objects
  483. * @param string $field the field to inspect on the message objects
  484. * @param string $value the value to test the field against
  485. */
  486. function message_count_messages($messagearray, $field='', $value='') {
  487. if (!is_array($messagearray)) return 0;
  488. if ($field == '' or empty($messagearray)) return count($messagearray);
  489. $count = 0;
  490. foreach ($messagearray as $message) {
  491. $count += ($message->$field == $value) ? 1 : 0;
  492. }
  493. return $count;
  494. }
  495. /**
  496. * Returns the count of unread messages for user. Either from a specific user or from all users.
  497. *
  498. * @param object $user1 the first user. Defaults to $USER
  499. * @param object $user2 the second user. If null this function will count all of user 1's unread messages.
  500. * @return int the count of $user1's unread messages
  501. */
  502. function message_count_unread_messages($user1=null, $user2=null) {
  503. global $USER, $DB;
  504. if (empty($user1)) {
  505. $user1 = $USER;
  506. }
  507. if (!empty($user2)) {
  508. return $DB->count_records_select('message', "useridto = ? AND useridfrom = ?",
  509. array($user1->id, $user2->id), "COUNT('id')");
  510. } else {
  511. return $DB->count_records_select('message', "useridto = ?",
  512. array($user1->id), "COUNT('id')");
  513. }
  514. }
  515. /**
  516. * Count the number of users blocked by $user1
  517. *
  518. * @param object $user1 user object
  519. * @return int the number of blocked users
  520. */
  521. function message_count_blocked_users($user1=null) {
  522. global $USER, $DB;
  523. if (empty($user1)) {
  524. $user1 = $USER;
  525. }
  526. $sql = "SELECT count(mc.id)
  527. FROM {message_contacts} mc
  528. WHERE mc.userid = :userid AND mc.blocked = 1";
  529. $params = array('userid' => $user1->id);
  530. return $DB->count_records_sql($sql, $params);
  531. }
  532. /**
  533. * Print the search form and search results if a search has been performed
  534. *
  535. * @param boolean $advancedsearch show basic or advanced search form
  536. * @param object $user1 the current user
  537. * @return boolean true if a search was performed
  538. */
  539. function message_print_search($advancedsearch = false, $user1=null) {
  540. $frm = data_submitted();
  541. $doingsearch = false;
  542. if ($frm) {
  543. if (confirm_sesskey()) {
  544. $doingsearch = !empty($frm->combinedsubmit) || !empty($frm->keywords) || (!empty($frm->personsubmit) and !empty($frm->name));
  545. } else {
  546. $frm = false;
  547. }
  548. }
  549. if (!empty($frm->combinedsearch)) {
  550. $combinedsearchstring = $frm->combinedsearch;
  551. } else {
  552. //$combinedsearchstring = get_string('searchcombined','message').'...';
  553. $combinedsearchstring = '';
  554. }
  555. if ($doingsearch) {
  556. if ($advancedsearch) {
  557. $messagesearch = '';
  558. if (!empty($frm->keywords)) {
  559. $messagesearch = $frm->keywords;
  560. }
  561. $personsearch = '';
  562. if (!empty($frm->name)) {
  563. $personsearch = $frm->name;
  564. }
  565. include('search_advanced.html');
  566. } else {
  567. include('search.html');
  568. }
  569. $showicontext = false;
  570. message_print_search_results($frm, $showicontext, $user1);
  571. return true;
  572. } else {
  573. if ($advancedsearch) {
  574. $personsearch = $messagesearch = '';
  575. include('search_advanced.html');
  576. } else {
  577. include('search.html');
  578. }
  579. return false;
  580. }
  581. }
  582. /**
  583. * Get the users recent conversations meaning all the people they've recently
  584. * sent or received a message from plus the most recent message sent to or received from each other user
  585. *
  586. * @param object $user the current user
  587. * @param int $limitfrom can be used for paging
  588. * @param int $limitto can be used for paging
  589. * @return array
  590. */
  591. function message_get_recent_conversations($user, $limitfrom=0, $limitto=100) {
  592. global $DB;
  593. $userfields = user_picture::fields('otheruser', array('lastaccess'));
  594. // This query retrieves the most recent message received from or sent to
  595. // seach other user.
  596. //
  597. // If two messages have the same timecreated, we take the one with the
  598. // larger id.
  599. //
  600. // There is a separate query for read and unread messages as they are stored
  601. // in different tables. They were originally retrieved in one query but it
  602. // was so large that it was difficult to be confident in its correctness.
  603. $sql = "SELECT $userfields,
  604. message.id as mid, message.notification, message.smallmessage, message.fullmessage,
  605. message.fullmessagehtml, message.fullmessageformat, message.timecreated,
  606. contact.id as contactlistid, contact.blocked
  607. FROM {message_read} message
  608. JOIN {user} otheruser ON otheruser.id = CASE
  609. WHEN message.useridto = :userid1 THEN message.useridfrom
  610. ELSE message.useridto END
  611. LEFT JOIN {message_contacts} contact ON contact.userid = :userid2 AND contact.contactid = otheruser.id
  612. WHERE otheruser.deleted = 0
  613. AND (message.useridto = :userid3 OR message.useridfrom = :userid4)
  614. AND message.notification = 0
  615. AND NOT EXISTS (
  616. SELECT 1
  617. FROM {message_read} othermessage
  618. WHERE ((othermessage.useridto = :userid5 AND othermessage.useridfrom = otheruser.id) OR
  619. (othermessage.useridfrom = :userid6 AND othermessage.useridto = otheruser.id))
  620. AND (othermessage.timecreated > message.timecreated OR (
  621. othermessage.timecreated = message.timecreated AND othermessage.id > message.id))
  622. )
  623. ORDER BY message.timecreated DESC";
  624. $params = array('userid1' => $user->id, 'userid2' => $user->id, 'userid3' => $user->id,
  625. 'userid4' => $user->id, 'userid5' => $user->id, 'userid6' => $user->id);
  626. $read = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);
  627. // We want to get the messages that have not been read. These are stored in the 'message' table. It is the
  628. // exact same query as the one above, except for the table we are querying. So, simply replace references to
  629. // the 'message_read' table with the 'message' table.
  630. $sql = str_replace('{message_read}', '{message}', $sql);
  631. $unread = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);
  632. $conversations = array();
  633. // Union the 2 result sets together looking for the message with the most
  634. // recent timecreated for each other user.
  635. // $conversation->id (the array key) is the other user's ID.
  636. $conversation_arrays = array($unread, $read);
  637. foreach ($conversation_arrays as $conversation_array) {
  638. foreach ($conversation_array as $conversation) {
  639. if (empty($conversations[$conversation->id]) || $conversations[$conversation->id]->timecreated < $conversation->timecreated ) {
  640. $conversations[$conversation->id] = $conversation;
  641. }
  642. }
  643. }
  644. // Sort the conversations by $conversation->timecreated, newest to oldest
  645. // There may be multiple conversations with the same timecreated
  646. // The conversations array contains both read and unread messages (different tables) so sorting by ID won't work
  647. $result = core_collator::asort_objects_by_property($conversations, 'timecreated', core_collator::SORT_NUMERIC);
  648. $conversations = array_reverse($conversations);
  649. return $conversations;
  650. }
  651. /**
  652. * Get the users recent event notifications
  653. *
  654. * @param object $user the current user
  655. * @param int $limitfrom can be used for paging
  656. * @param int $limitto can be used for paging
  657. * @return array
  658. */
  659. function message_get_recent_notifications($user, $limitfrom=0, $limitto=100) {
  660. global $DB;
  661. $userfields = user_picture::fields('u', array('lastaccess'));
  662. $sql = "SELECT mr.id AS message_read_id, $userfields, mr.notification, mr.smallmessage, mr.fullmessage, mr.fullmessagehtml, mr.fullmessageformat, mr.timecreated as timecreated, mr.contexturl, mr.contexturlname
  663. FROM {message_read} mr
  664. JOIN {user} u ON u.id=mr.useridfrom
  665. WHERE mr.useridto = :userid1 AND u.deleted = '0' AND mr.notification = :notification
  666. ORDER BY mr.timecreated DESC";
  667. $params = array('userid1' => $user->id, 'notification' => 1);
  668. $notifications = $DB->get_records_sql($sql, $params, $limitfrom, $limitto);
  669. return $notifications;
  670. }
  671. /**
  672. * Print the user's recent conversations
  673. *
  674. * @param stdClass $user the current user
  675. * @param bool $showicontext flag indicating whether or not to show text next to the action icons
  676. */
  677. function message_print_recent_conversations($user1 = null, $showicontext = false, $showactionlinks = true) {
  678. global $USER;
  679. echo html_writer::start_tag('p', array('class' => 'heading'));
  680. echo get_string('mostrecentconversations', 'message');
  681. echo html_writer::end_tag('p');
  682. if (empty($user1)) {
  683. $user1 = $USER;
  684. }
  685. $conversations = message_get_recent_conversations($user1);
  686. // Attach context url information to create the "View this conversation" type links
  687. foreach($conversations as $conversation) {
  688. $conversation->contexturl = new moodle_url("/message/index.php?user1={$user1->id}&user2={$conversation->id}");
  689. $conversation->contexturlname = get_string('thisconversation', 'message');
  690. }
  691. $showotheruser = true;
  692. message_print_recent_messages_table($conversations, $user1, $showotheruser, $showicontext, false, $showactionlinks);
  693. }
  694. /**
  695. * Print the user's recent notifications
  696. *
  697. * @param stdClass $user the current user
  698. */
  699. function message_print_recent_notifications($user=null) {
  700. global $USER;
  701. echo html_writer::start_tag('p', array('class' => 'heading'));
  702. echo get_string('mostrecentnotifications', 'message');
  703. echo html_writer::end_tag('p');
  704. if (empty($user)) {
  705. $user = $USER;
  706. }
  707. $notifications = message_get_recent_notifications($user);
  708. $showicontext = false;
  709. $showotheruser = false;
  710. message_print_recent_messages_table($notifications, $user, $showotheruser, $showicontext, true);
  711. }
  712. /**
  713. * Print a list of recent messages
  714. *
  715. * @access private
  716. *
  717. * @param array $messages the messages to display
  718. * @param stdClass $user the current user
  719. * @param bool $showotheruser display information on the other user?
  720. * @param bool $showicontext show text next to the action icons?
  721. * @param bool $forcetexttohtml Force text to go through @see text_to_html() via @see format_text()
  722. * @param bool $showactionlinks
  723. * @return void
  724. */
  725. function message_print_recent_messages_table($messages, $user = null, $showotheruser = true, $showicontext = false, $forcetexttohtml = false, $showactionlinks = true) {
  726. global $OUTPUT;
  727. static $dateformat;
  728. if (empty($dateformat)) {
  729. $dateformat = get_string('strftimedatetimeshort');
  730. }
  731. echo html_writer::start_tag('div', array('class' => 'messagerecent'));
  732. foreach ($messages as $message) {
  733. echo html_writer::start_tag('div', array('class' => 'singlemessage'));
  734. if ($showotheruser) {
  735. $strcontact = $strblock = $strhistory = null;
  736. if ($showactionlinks) {
  737. if ( $message->contactlistid ) {
  738. if ($message->blocked == 0) { // The other user isn't blocked.
  739. $strcontact = message_contact_link($message->id, 'remove', true, null, $showicontext);
  740. $strblock = message_contact_link($message->id, 'block', true, null, $showicontext);
  741. } else { // The other user is blocked.
  742. $strcontact = message_contact_link($message->id, 'add', true, null, $showicontext);
  743. $strblock = message_contact_link($message->id, 'unblock', true, null, $showicontext);
  744. }
  745. } else {
  746. $strcontact = message_contact_link($message->id, 'add', true, null, $showicontext);
  747. $strblock = message_contact_link($message->id, 'block', true, null, $showicontext);
  748. }
  749. //should we show just the icon or icon and text?
  750. $histicontext = 'icon';
  751. if ($showicontext) {
  752. $histicontext = 'both';
  753. }
  754. $strhistory = message_history_link($user->id, $message->id, true, '', '', $histicontext);
  755. }
  756. echo html_writer::start_tag('span', array('class' => 'otheruser'));
  757. echo html_writer::start_tag('span', array('class' => 'pix'));
  758. echo $OUTPUT->user_picture($message, array('size' => 20, 'courseid' => SITEID));
  759. echo html_writer::end_tag('span');
  760. echo html_writer::start_tag('span', array('class' => 'contact'));
  761. $link = new moodle_url("/message/index.php?user1={$user->id}&user2=$message->id");
  762. $action = null;
  763. echo $OUTPUT->action_link($link, fullname($message), $action, array('title' => get_string('sendmessageto', 'message', fullname($message))));
  764. echo html_writer::end_tag('span');//end contact
  765. if ($showactionlinks) {
  766. echo $strcontact.$strblock.$strhistory;
  767. }
  768. echo html_writer::end_tag('span');//end otheruser
  769. }
  770. $messagetext = message_format_message_text($message, $forcetexttohtml);
  771. echo html_writer::tag('span', userdate($message->timecreated, $dateformat), array('class' => 'messagedate'));
  772. echo html_writer::tag('span', $messagetext, array('class' => 'themessage'));
  773. echo message_format_contexturl($message);
  774. echo html_writer::end_tag('div');//end singlemessage
  775. }
  776. echo html_writer::end_tag('div');//end messagerecent
  777. }
  778. /**
  779. * Try to guess how to convert the message to html.
  780. *
  781. * @access private
  782. *
  783. * @param stdClass $message
  784. * @param bool $forcetexttohtml
  785. * @return string html fragment
  786. */
  787. function message_format_message_text($message, $forcetexttohtml = false) {
  788. // Note: this is a very nasty hack that tries to work around the weird messaging rules and design.
  789. $options = new stdClass();
  790. $options->para = false;
  791. $format = $message->fullmessageformat;
  792. if ($message->smallmessage !== '') {
  793. if ($message->notification == 1) {
  794. if ($message->fullmessagehtml !== '' or $message->fullmessage !== '') {
  795. $format = FORMAT_PLAIN;
  796. }
  797. }
  798. $messagetext = $message->smallmessage;
  799. } else if ($message->fullmessageformat == FORMAT_HTML) {
  800. if ($message->fullmessagehtml !== '') {
  801. $messagetext = $message->fullmessagehtml;
  802. } else {
  803. $messagetext = $message->fullmessage;
  804. $format = FORMAT_MOODLE;
  805. }
  806. } else {
  807. if ($message->fullmessage !== '') {
  808. $messagetext = $message->fullmessage;
  809. } else {
  810. $messagetext = $message->fullmessagehtml;
  811. $format = FORMAT_HTML;
  812. }
  813. }
  814. if ($forcetexttohtml) {
  815. // This is a crazy hack, why not set proper format when creating the notifications?
  816. if ($format === FORMAT_PLAIN) {
  817. $format = FORMAT_MOODLE;
  818. }
  819. }
  820. return format_text($messagetext, $format, $options);
  821. }
  822. /**
  823. * Add the selected user as a contact for the current user
  824. *
  825. * @param int $contactid the ID of the user to add as a contact
  826. * @param int $blocked 1 if you wish to block the contact
  827. * @return bool/int false if the $contactid isnt a valid user id. True if no changes made.
  828. * Otherwise returns the result of update_record() or insert_record()
  829. */
  830. function message_add_contact($contactid, $blocked=0) {
  831. global $USER, $DB;
  832. if (!$DB->record_exists('user', array('id' => $contactid))) { // invalid userid
  833. return false;
  834. }
  835. if (($contact = $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid))) !== false) {
  836. // A record already exists. We may be changing blocking status.
  837. if ($contact->blocked !== $blocked) {
  838. // Blocking status has been changed.
  839. $contact->blocked = $blocked;
  840. return $DB->update_record('message_contacts', $contact);
  841. } else {
  842. // No change to blocking status.
  843. return true;
  844. }
  845. } else {
  846. // New contact record.
  847. $contact = new stdClass();
  848. $contact->userid = $USER->id;
  849. $contact->contactid = $contactid;
  850. $contact->blocked = $blocked;
  851. return $DB->insert_record('message_contacts', $contact, false);
  852. }
  853. }
  854. /**
  855. * remove a contact
  856. *
  857. * @param int $contactid the user ID of the contact to remove
  858. * @return bool returns the result of delete_records()
  859. */
  860. function message_remove_contact($contactid) {
  861. global $USER, $DB;
  862. return $DB->delete_records('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid));
  863. }
  864. /**
  865. * Unblock a contact. Note that this reverts the previously blocked user back to a non-contact.
  866. *
  867. * @param int $contactid the user ID of the contact to unblock
  868. * @return bool returns the result of delete_records()
  869. */
  870. function message_unblock_contact($contactid) {
  871. global $USER, $DB;
  872. return $DB->delete_records('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid));
  873. }
  874. /**
  875. * block a user
  876. *
  877. * @param int $contactid the user ID of the user to block
  878. */
  879. function message_block_contact($contactid) {
  880. return message_add_contact($contactid, 1);
  881. }
  882. /**
  883. * Load a user's contact record
  884. *
  885. * @param int $contactid the user ID of the user whose contact record you want
  886. * @return array message contacts
  887. */
  888. function message_get_contact($contactid) {
  889. global $USER, $DB;
  890. return $DB->get_record('message_contacts', array('userid' => $USER->id, 'contactid' => $contactid));
  891. }
  892. /**
  893. * Print the results of a message search
  894. *
  895. * @param mixed $frm submitted form data
  896. * @param bool $showicontext show text next to action icons?
  897. * @param object $currentuser the current user
  898. * @return void
  899. */
  900. function message_print_search_results($frm, $showicontext=false, $currentuser=null) {
  901. global $USER, $DB, $OUTPUT;
  902. if (empty($currentuser)) {
  903. $currentuser = $USER;
  904. }
  905. echo html_writer::start_tag('div', array('class' => 'mdl-left'));
  906. $personsearch = false;
  907. $personsearchstring = null;
  908. if (!empty($frm->personsubmit) and !empty($frm->name)) {
  909. $personsearch = true;
  910. $personsearchstring = $frm->name;
  911. } else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) {
  912. $personsearch = true;
  913. $personsearchstring = $frm->combinedsearch;
  914. }
  915. // Search for person.
  916. if ($personsearch) {
  917. if (optional_param('mycourses', 0, PARAM_BOOL)) {
  918. $users = array();
  919. $mycourses = enrol_get_my_courses('id');
  920. $mycoursesids = array();
  921. foreach ($mycourses as $mycourse) {
  922. $mycoursesids[] = $mycourse->id;
  923. }
  924. $susers = message_search_users($mycoursesids, $personsearchstring);
  925. foreach ($susers as $suser) {
  926. $users[$suser->id] = $suser;
  927. }
  928. } else {
  929. $users = message_search_users(SITEID, $personsearchstring);
  930. }
  931. if (!empty($users)) {
  932. echo html_writer::start_tag('p', array('class' => 'heading searchresultcount'));
  933. echo get_string('userssearchresults', 'message', count($users));
  934. echo html_writer::end_tag('p');
  935. echo html_writer::start_tag('table', array('class' => 'messagesearchresults'));
  936. foreach ($users as $user) {
  937. if ( $user->contactlistid ) {
  938. if ($user->blocked == 0) { // User is not blocked.
  939. $strcontact = message_contact_link($user->id, 'remove', true, null, $showicontext);
  940. $strblock = message_contact_link($user->id, 'block', true, null, $showicontext);
  941. } else { // blocked
  942. $strcontact = message_contact_link($user->id, 'add', true, null, $showicontext);
  943. $strblock = message_contact_link($user->id, 'unblock', true, null, $showicontext);
  944. }
  945. } else {
  946. $strcontact = message_contact_link($user->id, 'add', true, null, $showicontext);
  947. $strblock = message_contact_link($user->id, 'block', true, null, $showicontext);
  948. }
  949. // Should we show just the icon or icon and text?
  950. $histicontext = 'icon';
  951. if ($showicontext) {
  952. $histicontext = 'both';
  953. }
  954. $strhistory = message_history_link($USER->id, $user->id, true, '', '', $histicontext);
  955. echo html_writer::start_tag('tr');
  956. echo html_writer::start_tag('td', array('class' => 'pix'));
  957. echo $OUTPUT->user_picture($user, array('size' => 20, 'courseid' => SITEID));
  958. echo html_writer::end_tag('td');
  959. echo html_writer::start_tag('td',array('class' => 'contact'));
  960. $action = null;
  961. $link = new moodle_url("/message/index.php?id=$user->id");
  962. echo $OUTPUT->action_link($link, fullname($user), $action, array('title' => get_string('sendmessageto', 'message', fullname($user))));
  963. echo html_writer::end_tag('td');
  964. echo html_writer::tag('td', $strcontact, array('class' => 'link'));
  965. echo html_writer::tag('td', $strblock, array('class' => 'link'));
  966. echo html_writer::tag('td', $strhistory, array('class' => 'link'));
  967. echo html_writer::end_tag('tr');
  968. }
  969. echo html_writer::end_tag('table');
  970. } else {
  971. echo html_writer::start_tag('p', array('class' => 'heading searchresultcount'));
  972. echo get_string('userssearchresults', 'message', 0).'<br /><br />';
  973. echo html_writer::end_tag('p');
  974. }
  975. }
  976. // search messages for keywords
  977. $messagesearch = false;
  978. $messagesearchstring = null;
  979. if (!empty($frm->keywords)) {
  980. $messagesearch = true;
  981. $messagesearchstring = clean_text(trim($frm->keywords));
  982. } else if (!empty($frm->combinedsubmit) and !empty($frm->combinedsearch)) {
  983. $messagesearch = true;
  984. $messagesearchstring = clean_text(trim($frm->combinedsearch));
  985. }
  986. if ($messagesearch) {
  987. if ($messagesearchstring) {
  988. $keywords = explode(' ', $messagesearchstring);
  989. } else {
  990. $keywords = array();
  991. }
  992. $tome = false;
  993. $fromme = false;
  994. $courseid = 'none';
  995. if (empty($frm->keywordsoption)) {
  996. $frm->keywordsoption = 'allmine';
  997. }
  998. switch ($frm->keywordsoption) {
  999. case 'tome':
  1000. $tome = true;
  1001. break;
  1002. case 'fromme':
  1003. $fromme = true;
  1004. break;
  1005. case 'allmine':
  1006. $tome = true;
  1007. $fromme = true;
  1008. break;
  1009. case 'allusers':
  1010. $courseid = SITEID;
  1011. break;
  1012. case 'courseusers':
  1013. $courseid = $frm->courseid;
  1014. break;
  1015. default:
  1016. $tome = true;
  1017. $fromme = true;
  1018. }
  1019. if (($messages = message_search($keywords, $fromme, $tome, $courseid)) !== false) {
  1020. // Get a list of contacts.
  1021. if (($contacts = $DB->get_records('message_contacts', array('userid' => $USER->id), '', 'contactid, blocked') ) === false) {
  1022. $contacts = array();
  1023. }
  1024. // Print heading with number of results.
  1025. echo html_writer::start_tag('p', array('class' => 'heading searchresultcount'));
  1026. $countresults = count($messages);
  1027. if ($countresults == MESSAGE_SEARCH_MAX_RESULTS) {
  1028. echo get_string('keywordssearchresultstoomany', 'message', $countresults).' ("'.s($messagesearchstring).'")';
  1029. } else {
  1030. echo get_string('keywordssearchresults', 'message', $countresults);
  1031. }
  1032. echo html_writer::end_tag('p');
  1033. // Print table headings.
  1034. echo html_writer::start_tag('table', array('class' => 'messagesearchresults', 'cellspacing' => '0'));
  1035. $headertdstart = html_writer::start_tag('td', array('class' => 'messagesearchresultscol'));
  1036. $headertdend = html_writer::end_tag('td');
  1037. echo html_writer::start_tag('tr');
  1038. echo $headertdstart.get_string('from').$headertdend;
  1039. echo $headertdstart.get_string('to').$headertdend;
  1040. echo $headertdstart.get_string('message', 'message').$headertdend;
  1041. echo $headertdstart.get_string('timesent', 'message').$headertdend;
  1042. echo html_writer::end_t

Large files files are truncated, but you can click here to view the full file