MDL-16043: manage case when administrator set enrol_remotecoursefield or enrol_remote...
[moodle-linuxchix.git] / mod / chat / report.php
blob75bf79d1477b783feb1bc209564b669555dd9426
1 <?php // $Id$
3 /// This page prints reports and info about chats
5 require_once('../../config.php');
6 require_once('lib.php');
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);
14 if (! $cm = get_coursemodule_from_id('chat', $id)) {
15 error('Course Module ID was incorrect');
17 if (! $chat = get_record('chat', 'id', $cm->instance)) {
18 error('Course module is incorrect');
20 if (! $course = get_record('course', 'id', $chat->course)) {
21 error('Course is misconfigured');
24 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
25 require_login($course->id, false, $cm);
27 require_capability('mod/chat:readlog', $context);
29 add_to_log($course->id, 'chat', 'report', "report.php?id=$cm->id", $chat->id, $cm->id);
31 $strchats = get_string('modulenameplural', 'chat');
32 $strchat = get_string('modulename', 'chat');
33 $strchatreport = get_string('chatreport', 'chat');
34 $strseesession = get_string('seesession', 'chat');
35 $strdeletesession = get_string('deletesession', 'chat');
37 $navlinks = array();
39 /// Print a session if one has been specified
41 if ($start and $end and !$confirmdelete) { // Show a full transcript
42 $navigation = build_navigation($strchatreport, $cm);
43 print_header_simple(format_string($chat->name).": $strchatreport", '', $navigation,
44 '', '', true, '', navmenu($course, $cm));
46 /// Check to see if groups are being used here
47 $groupmode = groups_get_activity_groupmode($cm);
48 $currentgroup = groups_get_activity_group($cm, true);
49 groups_print_activity_menu($cm, "report.php?id=$cm->id");
52 if ($currentgroup) {
53 $groupselect = " AND (groupid = '$currentgroup' OR groupid = 0)";
54 } else {
55 $groupselect = "";
58 if ($deletesession and has_capability('mod/chat:deletelog', $context)) {
59 notice_yesno(get_string('deletesessionsure', 'chat'),
60 "report.php?id=$cm->id&amp;deletesession=1&amp;confirmdelete=1&amp;start=$start&amp;end=$end&amp;sesskey=$USER->sesskey",
61 "report.php?id=$cm->id");
64 if (!$messages = get_records_select('chat_messages', "chatid = $chat->id AND
65 timestamp >= '$start' AND
66 timestamp <= '$end' $groupselect", "timestamp ASC")) {
67 print_heading(get_string('nomessages', 'chat'));
69 } else {
70 echo '<p class="boxaligncenter">'.userdate($start).' --> '. userdate($end).'</p>';
72 print_simple_box_start('center');
73 foreach ($messages as $message) { // We are walking FORWARDS through messages
74 $formatmessage = chat_format_message($message, $course->id, $USER);
75 if (isset($formatmessage->html)) {
76 echo $formatmessage->html;
79 print_simple_box_end();
82 if (!$deletesession or !has_capability('mod/chat:deletelog', $context)) {
83 print_continue("report.php?id=$cm->id");
86 print_footer($course);
87 exit;
91 /// Print the Sessions display
92 $navigation = build_navigation($strchatreport, $cm);
93 print_header_simple(format_string($chat->name).": $strchatreport", '', $navigation,
94 '', '', true, '', navmenu($course, $cm));
96 print_heading(format_string($chat->name).': '.get_string('sessions', 'chat'));
99 /// Check to see if groups are being used here
100 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
101 $currentgroup = groups_get_activity_group($cm, true);
102 groups_print_activity_menu($cm, "report.php?id=$cm->id");
103 } else {
104 $currentgroup = false;
107 if (!empty($currentgroup)) {
108 $groupselect = " AND (groupid = '$currentgroup' OR groupid = 0)";
109 } else {
110 $groupselect = "";
113 /// Delete a session if one has been specified
115 if ($deletesession and has_capability('mod/chat:deletelog', $context) and $confirmdelete and $start and $end and confirm_sesskey()) {
116 delete_records_select('chat_messages', "chatid = $chat->id AND
117 timestamp >= '$start' AND
118 timestamp <= '$end' $groupselect");
119 $strdeleted = get_string('deleted');
120 notify("$strdeleted: ".userdate($start).' --> '. userdate($end));
121 unset($deletesession);
125 /// Get the messages
127 if (empty($messages)) { /// May have already got them above
128 if (!$messages = get_records_select('chat_messages', "chatid = '$chat->id' $groupselect", "timestamp DESC")) {
129 print_heading(get_string('nomessages', 'chat'));
130 print_footer($course);
131 exit;
135 /// Show all the sessions
137 $sessiongap = 5 * 60; // 5 minutes silence means a new session
138 $sessionend = 0;
139 $sessionstart = 0;
140 $sessionusers = array();
141 $lasttime = 0;
143 $messagesleft = count($messages);
145 foreach ($messages as $message) { // We are walking BACKWARDS through the messages
147 $messagesleft --; // Countdown
149 if (!$lasttime) {
150 $lasttime = $message->timestamp;
152 if (!$sessionend) {
153 $sessionend = $message->timestamp;
155 if ((($lasttime - $message->timestamp) < $sessiongap) and $messagesleft) { // Same session
156 if ($message->userid and !$message->system) { // Remember user and count messages
157 if (empty($sessionusers[$message->userid])) {
158 $sessionusers[$message->userid] = 1;
159 } else {
160 $sessionusers[$message->userid] ++;
163 } else {
164 $sessionstart = $lasttime;
166 if ($sessionend - $sessionstart > 60 and count($sessionusers) > 1) {
168 echo '<p align="center">'.userdate($sessionstart).' --> '. userdate($sessionend).'</p>';
170 print_simple_box_start('center');
172 arsort($sessionusers);
173 foreach ($sessionusers as $sessionuser => $usermessagecount) {
174 if ($user = get_record('user', 'id', $sessionuser)) {
175 print_user_picture($user, $course->id, $user->picture);
176 echo '&nbsp;'.fullname($user, true); // XXX TODO use capability instead of true
177 echo "&nbsp;($usermessagecount)<br />";
181 echo '<p align="right">';
182 echo "<a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend\">$strseesession</a>";
183 if (has_capability('mod/chat:deletelog', $context)) {
184 echo "<br /><a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend&amp;deletesession=1\">$strdeletesession</a>";
186 echo '</p>';
187 print_simple_box_end();
190 $sessionend = $message->timestamp;
191 $sessionusers = array();
192 $sessionusers[$message->userid] = 1;
194 $lasttime = $message->timestamp;
197 /// Finish the page
198 print_footer($course);