forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.php
204 lines (158 loc) · 7.8 KB
/
report.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php // $Id$
/// This page prints reports and info about chats
require_once('../../config.php');
require_once('lib.php');
$id = required_param('id', PARAM_INT);
$start = optional_param('start', 0, PARAM_INT); // Start of period
$end = optional_param('end', 0, PARAM_INT); // End of period
$deletesession = optional_param('deletesession', 0, PARAM_BOOL);
$confirmdelete = optional_param('confirmdelete', 0, PARAM_BOOL);
if (! $cm = get_coursemodule_from_id('chat', $id)) {
error('Course Module ID was incorrect');
}
if (! $chat = get_record('chat', 'id', $cm->instance)) {
error('Course module is incorrect');
}
if (! $course = get_record('course', 'id', $chat->course)) {
error('Course is misconfigured');
}
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_login($course->id, false, $cm);
require_capability('mod/chat:readlog', $context);
add_to_log($course->id, 'chat', 'report', "report.php?id=$cm->id", $chat->id, $cm->id);
$strchats = get_string('modulenameplural', 'chat');
$strchat = get_string('modulename', 'chat');
$strchatreport = get_string('chatreport', 'chat');
$strseesession = get_string('seesession', 'chat');
$strdeletesession = get_string('deletesession', 'chat');
/// Print a session if one has been specified
if ($start and $end and !$confirmdelete) { // Show a full transcript
print_header_simple(format_string($chat->name).": $strchatreport", '',
"<a href=\"index.php?id=$course->id\">$strchats</a> ->
<a href=\"view.php?id=$cm->id\">".format_string($chat->name,true)."</a> ->
<a href=\"report.php?id=$cm->id\">$strchatreport</a>",
'', '', true, '', navmenu($course, $cm));
/// Check to see if groups are being used here
if ($groupmode = groupmode($course, $cm)) { // Groups are being used
$currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id");
} else {
$currentgroup = false;
}
if (!empty($currentgroup)) {
$groupselect = " AND groupid = '$currentgroup'";
} else {
$groupselect = "";
}
if ($deletesession and has_capability('mod/chat:deletelog', $context)) {
notice_yesno(get_string('deletesessionsure', 'chat'),
"report.php?id=$cm->id&deletesession=1&confirmdelete=1&start=$start&end=$end&sesskey=$USER->sesskey",
"report.php?id=$cm->id");
}
if (!$messages = get_records_select('chat_messages', "chatid = $chat->id AND
timestamp >= '$start' AND
timestamp <= '$end' $groupselect", "timestamp ASC")) {
print_heading(get_string('nomessages', 'chat'));
} else {
echo '<p align="center">'.userdate($start).' --> '. userdate($end).'</p>';
print_simple_box_start('center');
foreach ($messages as $message) { // We are walking FORWARDS through messages
$formatmessage = chat_format_message($message, $course->id, $USER);
if (isset($formatmessage->html)) {
echo $formatmessage->html;
}
}
print_simple_box_end('center');
}
if (!$deletesession or !has_capability('mod/chat:deletelog', $context)) {
print_continue("report.php?id=$cm->id");
}
print_footer($course);
exit;
}
/// Print the Sessions display
print_header_simple(format_string($chat->name).": $strchatreport", '',
"<a href=\"index.php?id=$course->id\">$strchats</a> ->
<a href=\"view.php?id=$cm->id\">".format_string($chat->name,true)."</a> -> $strchatreport",
'', '', true, '', navmenu($course, $cm));
print_heading(format_string($chat->name).': '.get_string('sessions', 'chat'));
/// Check to see if groups are being used here
if ($groupmode = groupmode($course, $cm)) { // Groups are being used
$currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id");
} else {
$currentgroup = false;
}
if (!empty($currentgroup)) {
$groupselect = " AND groupid = '$currentgroup'";
} else {
$groupselect = "";
}
/// Delete a session if one has been specified
if ($deletesession and has_capability('mod/chat:deletelog', $context) and $confirmdelete and $start and $end and confirm_sesskey()) {
delete_records_select('chat_messages', "chatid = $chat->id AND
timestamp >= '$start' AND
timestamp <= '$end' $groupselect");
$strdeleted = get_string('deleted');
notify("$strdeleted: ".userdate($start).' --> '. userdate($end));
unset($deletesession);
}
/// Get the messages
if (empty($messages)) { /// May have already got them above
if (!$messages = get_records_select('chat_messages', "chatid = '$chat->id' $groupselect", "timestamp DESC")) {
print_heading(get_string('nomessages', 'chat'));
print_footer($course);
exit;
}
}
/// Show all the sessions
$sessiongap = 5 * 60; // 5 minutes silence means a new session
$sessionend = 0;
$sessionstart = 0;
$sessionusers = array();
$lasttime = 0;
$messagesleft = count($messages);
foreach ($messages as $message) { // We are walking BACKWARDS through the messages
$messagesleft --; // Countdown
if (!$lasttime) {
$lasttime = $message->timestamp;
}
if (!$sessionend) {
$sessionend = $message->timestamp;
}
if ((($lasttime - $message->timestamp) < $sessiongap) and $messagesleft) { // Same session
if ($message->userid and !$message->system) { // Remember user and count messages
if (empty($sessionusers[$message->userid])) {
$sessionusers[$message->userid] = 1;
} else {
$sessionusers[$message->userid] ++;
}
}
} else {
$sessionstart = $lasttime;
if ($sessionend - $sessionstart > 60 and count($sessionusers) > 1) {
echo '<p align="center">'.userdate($sessionstart).' --> '. userdate($sessionend).'</p>';
print_simple_box_start('center');
arsort($sessionusers);
foreach ($sessionusers as $sessionuser => $usermessagecount) {
if ($user = get_record('user', 'id', $sessionuser)) {
print_user_picture($user->id, $course->id, $user->picture);
echo ' '.fullname($user, true); // XXX TODO use capability instead of true
echo " ($usermessagecount)<br />";
}
}
echo '<p align="right">';
echo "<a href=\"report.php?id=$cm->id&start=$sessionstart&end=$sessionend\">$strseesession</a>";
if (has_capability('mod/chat:deletelog', $context)) {
echo "<br /><a href=\"report.php?id=$cm->id&start=$sessionstart&end=$sessionend&deletesession=1\">$strdeletesession</a>";
}
echo '</p>';
print_simple_box_end();
}
$sessionend = $message->timestamp;
$sessionusers = array();
$sessionusers[$message->userid] = 1;
}
$lasttime = $message->timestamp;
}
/// Finish the page
print_footer($course);
?>