MDL-42464 mod_chat: Correct use of amp; in session link
[moodle.git] / mod / chat / report.php
CommitLineData
eb588b22 1<?php
22a4491a 2
3/// This page prints reports and info about chats
4
516121bd 5 require_once('../../config.php');
6 require_once('lib.php');
7
8 $id = required_param('id', PARAM_INT);
9 $start = optional_param('start', 0, PARAM_INT); // Start of period
10 $end = optional_param('end', 0, PARAM_INT); // End of period
11 $deletesession = optional_param('deletesession', 0, PARAM_BOOL);
12 $confirmdelete = optional_param('confirmdelete', 0, PARAM_BOOL);
23677956 13 $show_all = optional_param('show_all', 0, PARAM_BOOL);
516121bd 14
a6855934 15 $url = new moodle_url('/mod/chat/report.php', array('id'=>$id));
eb588b22 16 if ($start !== 0) {
17 $url->param('start', $start);
18 }
19 if ($end !== 0) {
20 $url->param('end', $end);
21 }
22 if ($deletesession !== 0) {
23 $url->param('deletesession', $deletesession);
24 }
25 if ($confirmdelete !== 0) {
26 $url->param('confirmdelete', $confirmdelete);
27 }
28 $PAGE->set_url($url);
29
f9d5371b 30 if (! $cm = get_coursemodule_from_id('chat', $id)) {
2f52a088 31 print_error('invalidcoursemodule');
84a2fdd7 32 }
d3bf6f92 33 if (! $chat = $DB->get_record('chat', array('id'=>$cm->instance))) {
2f52a088 34 print_error('invalidcoursemodule');
22a4491a 35 }
d3bf6f92 36 if (! $course = $DB->get_record('course', array('id'=>$chat->course))) {
2f52a088 37 print_error('coursemisconf');
22a4491a 38 }
22a4491a 39
6536217c 40 $context = context_module::instance($cm->id);
6abbf02d 41 $PAGE->set_context($context);
4ff8dccc 42 $PAGE->set_heading($course->fullname);
6abbf02d 43
cdbea7ee 44 require_login($course, false, $cm);
22a4491a 45
708c2e04
DC
46 if (empty($chat->studentlogs) && !has_capability('mod/chat:readlog', $context)) {
47 notice(get_string('nopermissiontoseethechatlog', 'chat'));
48 }
49
f49d814a
FM
50 $params = array(
51 'context' => $context,
52 'objectid' => $chat->id,
53 'other' => array(
54 'start' => $start,
55 'end' => $end
56 )
57 );
58 $event = \mod_chat\event\sessions_viewed::create($params);
59 $event->add_record_snapshot('chat', $chat);
60 $event->trigger();
22a4491a 61
516121bd 62 $strchats = get_string('modulenameplural', 'chat');
63 $strchat = get_string('modulename', 'chat');
64 $strchatreport = get_string('chatreport', 'chat');
65 $strseesession = get_string('seesession', 'chat');
66 $strdeletesession = get_string('deletesession', 'chat');
27ac57c0 67
01e38388 68 $navlinks = array();
6089d5a0 69
6708a1f5
SH
70 $canexportsess = has_capability('mod/chat:exportsession', $context);
71
2defdcd7 72/// Print a session if one has been specified
a669b1f5 73
27ac57c0 74 if ($start and $end and !$confirmdelete) { // Show a full transcript
296b589e 75 $PAGE->navbar->add($strchatreport);
76 $PAGE->set_title(format_string($chat->name).": $strchatreport");
77 echo $OUTPUT->header();
66e2b9f8 78 echo $OUTPUT->heading(format_string($chat->name), 2);
516121bd 79
80 /// Check to see if groups are being used here
a12e11c1 81 $groupmode = groups_get_activity_groupmode($cm);
82 $currentgroup = groups_get_activity_group($cm, true);
f1035deb 83 groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/report.php?id=$cm->id");
a12e11c1 84
d3bf6f92 85 $params = array('currentgroup'=>$currentgroup, 'chatid'=>$chat->id, 'start'=>$start, 'end'=>$end);
516121bd 86
f1644b99 87 // If the user is allocated to a group, only show messages from people
88 // in the same group, or no group
3b27b0fe 89 if ($currentgroup) {
f1644b99 90 $groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
b46792fe 91 } else {
92 $groupselect = "";
b46792fe 93 }
94
0468976c 95 if ($deletesession and has_capability('mod/chat:deletelog', $context)) {
af7bad79 96 echo $OUTPUT->confirm(get_string('deletesessionsure', 'chat'),
97 "report.php?id=$cm->id&deletesession=1&confirmdelete=1&start=$start&end=$end",
84a2fdd7 98 "report.php?id=$cm->id");
27ac57c0 99 }
100
6089d5a0 101 if (!$messages = $DB->get_records_select('chat_messages', "chatid = :chatid AND timestamp >= :start AND timestamp <= :end $groupselect", $params, "timestamp ASC")) {
b2dc6880 102 echo $OUTPUT->heading(get_string('nomessages', 'chat'));
a669b1f5 103
104 } else {
d3981e38 105 echo '<p class="boxaligncenter">'.userdate($start).' --> '. userdate($end).'</p>';
a669b1f5 106
29cd201f 107 echo $OUTPUT->box_start('center');
29ea0080 108 $participates = array();
a669b1f5 109 foreach ($messages as $message) { // We are walking FORWARDS through messages
29ea0080
DC
110 if (!isset($participates[$message->userid])) {
111 $participates[$message->userid] = true;
112 }
516121bd 113 $formatmessage = chat_format_message($message, $course->id, $USER);
78c98892 114 if (isset($formatmessage->html)) {
115 echo $formatmessage->html;
116 }
a669b1f5 117 }
6708a1f5
SH
118 $participatedcap = array_key_exists($USER->id, $participates) && has_capability('mod/chat:exportparticipatedsession', $context);
119 if (!empty($CFG->enableportfolios) && ($canexportsess || $participatedcap)) {
29ea0080
DC
120 require_once($CFG->libdir . '/portfoliolib.php');
121 $buttonoptions = array(
122 'id' => $cm->id,
123 'start' => $start,
124 'end' => $end,
125 );
126 $button = new portfolio_add_button();
37743241 127 $button->set_callback_options('chat_portfolio_caller', $buttonoptions, 'mod_chat');
29ea0080
DC
128 $button->render();
129 }
29cd201f 130 echo $OUTPUT->box_end();
a669b1f5 131 }
132
0468976c 133 if (!$deletesession or !has_capability('mod/chat:deletelog', $context)) {
29cd201f 134 echo $OUTPUT->continue_button("report.php?id=$cm->id");
27ac57c0 135 }
136
d8772689 137 echo $OUTPUT->footer();
a669b1f5 138 exit;
139 }
140
141
142/// Print the Sessions display
296b589e 143 $PAGE->navbar->add($strchatreport);
144 $PAGE->set_title(format_string($chat->name).": $strchatreport");
145 echo $OUTPUT->header();
22a4491a 146
66e2b9f8 147 echo $OUTPUT->heading(format_string($chat->name).': '.get_string('sessions', 'chat'), 2);
22a4491a 148
84a2fdd7 149/// Check to see if groups are being used here
a12e11c1 150 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
151 $currentgroup = groups_get_activity_group($cm, true);
f1035deb 152 groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/report.php?id=$cm->id");
84a2fdd7 153 } else {
154 $currentgroup = false;
155 }
156
d3bf6f92 157 $params = array('currentgroup'=>$currentgroup, 'chatid'=>$chat->id, 'start'=>$start, 'end'=>$end);
158
f1644b99 159 // If the user is allocated to a group, only show discussions with people in
160 // the same group, or no group
1604a0fc 161 if (!empty($currentgroup)) {
f1644b99 162 $groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
84a2fdd7 163 } else {
164 $groupselect = "";
84a2fdd7 165 }
22a4491a 166
27ac57c0 167/// Delete a session if one has been specified
168
0468976c 169 if ($deletesession and has_capability('mod/chat:deletelog', $context) and $confirmdelete and $start and $end and confirm_sesskey()) {
d3bf6f92 170 $DB->delete_records_select('chat_messages', "chatid = :chatid AND timestamp >= :start AND
171 timestamp <= :end $groupselect", $params);
516121bd 172 $strdeleted = get_string('deleted');
29cd201f 173 echo $OUTPUT->notification("$strdeleted: ".userdate($start).' --> '. userdate($end));
27ac57c0 174 unset($deletesession);
175 }
176
177
178/// Get the messages
27ac57c0 179 if (empty($messages)) { /// May have already got them above
d3bf6f92 180 if (!$messages = $DB->get_records_select('chat_messages', "chatid = :chatid $groupselect", $params, "timestamp DESC")) {
66e2b9f8 181 echo $OUTPUT->heading(get_string('nomessages', 'chat'), 3);
d8772689 182 echo $OUTPUT->footer();
27ac57c0 183 exit;
184 }
22a4491a 185 }
186
23677956 187 if ($show_all) {
66e2b9f8 188 $headingstr = get_string('listing_all_sessions', 'chat') . '&nbsp;';
4816e19a 189 $headingstr .= html_writer::link("report.php?id={$cm->id}&show_all=0", get_string('list_complete_sessions', 'chat'));
66e2b9f8 190 echo $OUTPUT->heading($headingstr, 3);
23677956
DC
191 }
192
27ac57c0 193/// Show all the sessions
a669b1f5 194
23677956
DC
195 $sessiongap = 5 * 60; // 5 minutes silence means a new session
196 $sessionend = 0;
197 $sessionstart = 0;
198 $sessionusers = array();
199 $lasttime = 0;
200 $complete_sessions = 0;
22a4491a 201
68c3b077 202 $messagesleft = count($messages);
203
a669b1f5 204 foreach ($messages as $message) { // We are walking BACKWARDS through the messages
68c3b077 205
206 $messagesleft --; // Countdown
207
9df9138a 208 if (!$lasttime) {
209 $lasttime = $message->timestamp;
22a4491a 210 }
9df9138a 211 if (!$sessionend) {
212 $sessionend = $message->timestamp;
22a4491a 213 }
68c3b077 214 if ((($lasttime - $message->timestamp) < $sessiongap) and $messagesleft) { // Same session
a669b1f5 215 if ($message->userid and !$message->system) { // Remember user and count messages
216 if (empty($sessionusers[$message->userid])) {
217 $sessionusers[$message->userid] = 1;
218 } else {
219 $sessionusers[$message->userid] ++;
220 }
9df9138a 221 }
516121bd 222 } else {
9df9138a 223 $sessionstart = $lasttime;
22a4491a 224
23677956
DC
225 $is_complete = ($sessionend - $sessionstart > 60 and count($sessionusers) > 1);
226 if ($show_all or $is_complete) {
22a4491a 227
516121bd 228 echo '<p align="center">'.userdate($sessionstart).' --> '. userdate($sessionend).'</p>';
22a4491a 229
29cd201f 230 echo $OUTPUT->box_start();
22a4491a 231
a669b1f5 232 arsort($sessionusers);
233 foreach ($sessionusers as $sessionuser => $usermessagecount) {
d3bf6f92 234 if ($user = $DB->get_record('user', array('id'=>$sessionuser))) {
812dbaf7 235 $OUTPUT->user_picture($user, array('courseid'=>$course->id));
0468976c 236 echo '&nbsp;'.fullname($user, true); // XXX TODO use capability instead of true
319ea041 237 echo "&nbsp;($usermessagecount)<br />";
9df9138a 238 }
239 }
240
516121bd 241 echo '<p align="right">';
242 echo "<a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend\">$strseesession</a>";
6708a1f5
SH
243 $participatedcap = (array_key_exists($USER->id, $sessionusers) && has_capability('mod/chat:exportparticipatedsession', $context));
244 if (!empty($CFG->enableportfolios) && ($canexportsess || $participatedcap)) {
47cfd331 245 require_once($CFG->libdir . '/portfoliolib.php');
0d06b6fd 246 $buttonoptions = array(
47cfd331 247 'id' => $cm->id,
248 'start' => $sessionstart,
249 'end' => $sessionend,
250 );
0d06b6fd 251 $button = new portfolio_add_button();
37743241 252 $button->set_callback_options('chat_portfolio_caller', $buttonoptions, 'mod_chat');
9e23487a
ARN
253 $portfoliobutton = $button->to_html(PORTFOLIO_ADD_TEXT_LINK);
254 if (!empty($portfoliobutton)) {
255 echo '<br />' . $portfoliobutton;
256 }
47cfd331 257 }
0468976c 258 if (has_capability('mod/chat:deletelog', $context)) {
516121bd 259 echo "<br /><a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend&amp;deletesession=1\">$strdeletesession</a>";
27ac57c0 260 }
516121bd 261 echo '</p>';
29cd201f 262 echo $OUTPUT->box_end();
9df9138a 263 }
23677956
DC
264 if ($is_complete) {
265 $complete_sessions++;
266 }
22a4491a 267
9df9138a 268 $sessionend = $message->timestamp;
22a4491a 269 $sessionusers = array();
a669b1f5 270 $sessionusers[$message->userid] = 1;
22a4491a 271 }
9df9138a 272 $lasttime = $message->timestamp;
22a4491a 273 }
274
6708a1f5 275 if (!empty($CFG->enableportfolios) && $canexportsess) {
47cfd331 276 require_once($CFG->libdir . '/portfoliolib.php');
0d06b6fd 277 $button = new portfolio_add_button();
37743241 278 $button->set_callback_options('chat_portfolio_caller', array('id' => $cm->id), 'mod_chat');
0d06b6fd 279 $button->render(null, get_string('addalltoportfolio', 'portfolio'));
47cfd331 280 }
281
282
23677956 283 if (!$show_all and $complete_sessions == 0) {
66e2b9f8
AD
284 echo html_writer::start_tag('p');
285 echo get_string('no_complete_sessions_found', 'chat') . '&nbsp;';
087c5d43 286 echo html_writer::link('report.php?id='.$cm->id.'&show_all=1', get_string('list_all_sessions', 'chat'));
66e2b9f8 287 echo html_writer::end_tag('p');
23677956
DC
288 }
289
22a4491a 290/// Finish the page
d8772689 291 echo $OUTPUT->footer();