mirror of
https://github.com/moodle/moodle.git
synced 2025-08-04 16:36:37 +02:00
MDL-57394 mod_chat: Move get session messages code to new function
This commit is contained in:
parent
26b4e8580d
commit
2971d8421c
4 changed files with 43 additions and 29 deletions
|
@ -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.
|
$messages = chat_get_session_messages($chat->id, $groupid, 0, 0, 'timestamp DESC');
|
||||||
$queryparams = array('chatid' => $chat->id);
|
if ($messages) {
|
||||||
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")) {
|
|
||||||
$chatsessions = chat_get_sessions($messages, $params['showall']);
|
$chatsessions = chat_get_sessions($messages, $params['showall']);
|
||||||
// Format sessions for external.
|
// Format sessions for external.
|
||||||
foreach ($chatsessions as $session) {
|
foreach ($chatsessions as $session) {
|
||||||
|
|
|
@ -1537,4 +1537,41 @@ function chat_get_sessions($messages, $showall = false) {
|
||||||
$lasttime = $message->timestamp;
|
$lasttime = $message->timestamp;
|
||||||
}
|
}
|
||||||
return $sessions;
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -96,29 +96,14 @@ if ($start and $end and !$confirmdelete) { // Show a full transcript.
|
||||||
$currentgroup = groups_get_activity_group($cm, true);
|
$currentgroup = groups_get_activity_group($cm, true);
|
||||||
groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/report.php?id=$cm->id");
|
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)) {
|
if ($deletesession and has_capability('mod/chat:deletelog', $context)) {
|
||||||
echo $OUTPUT->confirm(get_string('deletesessionsure', 'chat'),
|
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&deletesession=1&confirmdelete=1&start=$start&end=$end",
|
||||||
"report.php?id=$cm->id");
|
"report.php?id=$cm->id");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$messages = $DB->get_records_select('chat_messages',
|
if (!$messages = chat_get_session_messages($chat->id, $currentgroup, $start, $end, 'timestamp ASC')) {
|
||||||
"chatid = :chatid AND timestamp >= :start AND timestamp <= :end $groupselect",
|
|
||||||
$params,
|
|
||||||
"timestamp ASC")) {
|
|
||||||
|
|
||||||
echo $OUTPUT->heading(get_string('nomessages', 'chat'));
|
echo $OUTPUT->heading(get_string('nomessages', 'chat'));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
echo '<p class="boxaligncenter">'.userdate($start).' --> '. userdate($end).'</p>';
|
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.
|
// Get the messages.
|
||||||
if (empty($messages)) { // May have already got them above.
|
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->heading(get_string('nomessages', 'chat'), 3);
|
||||||
echo $OUTPUT->footer();
|
echo $OUTPUT->footer();
|
||||||
exit;
|
exit;
|
||||||
|
|
|
@ -145,7 +145,7 @@ if (has_capability('mod/chat:chat', $context)) {
|
||||||
echo '</p>';
|
echo '</p>';
|
||||||
|
|
||||||
if ($chat->studentlogs or has_capability('mod/chat:readlog', $context)) {
|
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 '<p>';
|
||||||
echo html_writer::link(new moodle_url('/mod/chat/report.php', array('id' => $cm->id)),
|
echo html_writer::link(new moodle_url('/mod/chat/report.php', array('id' => $cm->id)),
|
||||||
get_string('viewreport', 'chat'));
|
get_string('viewreport', 'chat'));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue