MDL-57394 mod_chat: Move get session messages code to new function

This commit is contained in:
Juan Leyva 2017-09-22 16:12:21 +02:00
parent 26b4e8580d
commit 2971d8421c
4 changed files with 43 additions and 29 deletions

View file

@ -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) {

View file

@ -1537,4 +1537,41 @@ function chat_get_sessions($messages, $showall = false) {
$lasttime = $message->timestamp;
}
return $sessions;
}
}
/**
* 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);
}

View file

@ -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 '<p class="boxaligncenter">'.userdate($start).' --> '. userdate($end).'</p>';
@ -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;

View file

@ -145,7 +145,7 @@ if (has_capability('mod/chat:chat', $context)) {
echo '</p>';
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 '<p>';
echo html_writer::link(new moodle_url('/mod/chat/report.php', array('id' => $cm->id)),
get_string('viewreport', 'chat'));