From 2971d8421c06220abded953c5e95981a71494286 Mon Sep 17 00:00:00 2001
From: Juan Leyva
Date: Fri, 22 Sep 2017 16:12:21 +0200
Subject: [PATCH] MDL-57394 mod_chat: Move get session messages code to new
function
---
mod/chat/classes/external.php | 12 ++---------
mod/chat/lib.php | 39 ++++++++++++++++++++++++++++++++++-
mod/chat/report.php | 19 ++---------------
mod/chat/view.php | 2 +-
4 files changed, 43 insertions(+), 29 deletions(-)
diff --git a/mod/chat/classes/external.php b/mod/chat/classes/external.php
index d993484ffd6..8bb4b15a979 100644
--- a/mod/chat/classes/external.php
+++ b/mod/chat/classes/external.php
@@ -681,16 +681,8 @@ class mod_chat_external extends external_api {
}
}
- // If the user is allocated to a group, only show messages from people in the same group, or no group.
- $queryparams = array('chatid' => $chat->id);
- if ($groupid) {
- $groupselect = " AND (groupid = :groupid OR groupid = 0)";
- $queryparams['groupid'] = $groupid;
- } else {
- $groupselect = "";
- }
-
- if ($messages = $DB->get_records_select('chat_messages', "chatid = :chatid $groupselect", $queryparams, "timestamp DESC")) {
+ $messages = chat_get_session_messages($chat->id, $groupid, 0, 0, 'timestamp DESC');
+ if ($messages) {
$chatsessions = chat_get_sessions($messages, $params['showall']);
// Format sessions for external.
foreach ($chatsessions as $session) {
diff --git a/mod/chat/lib.php b/mod/chat/lib.php
index 49c76f4745c..7de8f0709e4 100644
--- a/mod/chat/lib.php
+++ b/mod/chat/lib.php
@@ -1537,4 +1537,41 @@ function chat_get_sessions($messages, $showall = false) {
$lasttime = $message->timestamp;
}
return $sessions;
-}
\ No newline at end of file
+}
+
+/**
+ * Return the messages of the given chat session.
+ *
+ * @param int $chatid the chat id
+ * @param mixed $group false if groups not used, int if groups used, 0 means all groups
+ * @param int $start the session start timestamp (0 to not filter by time)
+ * @param int $end the session end timestamp (0 to not filter by time)
+ * @param string $sort an order to sort the results in (optional, a valid SQL ORDER BY parameter)
+ * @return array session messages
+ * @since Moodle 3.4
+ */
+function chat_get_session_messages($chatid, $group = false, $start = 0, $end = 0, $sort = '') {
+ global $DB;
+
+ $params = array('chatid' => $chatid);
+
+ // If the user is allocated to a group, only show messages from people in the same group, or no group.
+ if ($group) {
+ $groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
+ $params['currentgroup'] = $group;
+ } else {
+ $groupselect = "";
+ }
+
+ $select = "chatid = :chatid $groupselect";
+ if (!empty($start)) {
+ $select .= ' AND timestamp >= :start';
+ $params['start'] = $start;
+ }
+ if (!empty($end)) {
+ $select .= ' AND timestamp <= :end';
+ $params['end'] = $end;
+ }
+
+ return $DB->get_records_select('chat_messages', $select, $params, $sort);
+}
diff --git a/mod/chat/report.php b/mod/chat/report.php
index 8d57ca4bb97..d41a1216cde 100644
--- a/mod/chat/report.php
+++ b/mod/chat/report.php
@@ -96,29 +96,14 @@ if ($start and $end and !$confirmdelete) { // Show a full transcript.
$currentgroup = groups_get_activity_group($cm, true);
groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/report.php?id=$cm->id");
- $params = array('currentgroup' => $currentgroup, 'chatid' => $chat->id, 'start' => $start, 'end' => $end);
-
- // If the user is allocated to a group, only show messages from people
- // in the same group, or no group.
- if ($currentgroup) {
- $groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
- } else {
- $groupselect = "";
- }
-
if ($deletesession and has_capability('mod/chat:deletelog', $context)) {
echo $OUTPUT->confirm(get_string('deletesessionsure', 'chat'),
"report.php?id=$cm->id&deletesession=1&confirmdelete=1&start=$start&end=$end",
"report.php?id=$cm->id");
}
- if (!$messages = $DB->get_records_select('chat_messages',
- "chatid = :chatid AND timestamp >= :start AND timestamp <= :end $groupselect",
- $params,
- "timestamp ASC")) {
-
+ if (!$messages = chat_get_session_messages($chat->id, $currentgroup, $start, $end, 'timestamp ASC')) {
echo $OUTPUT->heading(get_string('nomessages', 'chat'));
-
} else {
echo ''.userdate($start).' --> '. userdate($end).'
';
@@ -198,7 +183,7 @@ if ($deletesession and has_capability('mod/chat:deletelog', $context)
// Get the messages.
if (empty($messages)) { // May have already got them above.
- if (!$messages = $DB->get_records_select('chat_messages', "chatid = :chatid $groupselect", $params, "timestamp DESC")) {
+ if (!$messages = chat_get_session_messages($chat->id, $currentgroup, 0, 0, 'timestamp DESC')) {
echo $OUTPUT->heading(get_string('nomessages', 'chat'), 3);
echo $OUTPUT->footer();
exit;
diff --git a/mod/chat/view.php b/mod/chat/view.php
index 8dfabf3fbd6..74ef55ce13e 100644
--- a/mod/chat/view.php
+++ b/mod/chat/view.php
@@ -145,7 +145,7 @@ if (has_capability('mod/chat:chat', $context)) {
echo '
';
if ($chat->studentlogs or has_capability('mod/chat:readlog', $context)) {
- if ($msg = $DB->get_records_select('chat_messages', "chatid = ? $groupselect", array($chat->id))) {
+ if ($msg = chat_get_session_messages($chat->id, $currentgroup)) {
echo '';
echo html_writer::link(new moodle_url('/mod/chat/report.php', array('id' => $cm->id)),
get_string('viewreport', 'chat'));