adding current groupid to grade_export class - soon to be used in plugins
[moodle-pu.git] / mod / chat / report.php
blob17ca573b63ccd76dbabce045aca95df6c5f061f1
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
43 $navlinks[] = array('name' => $strchats, 'link' => "index.php?id=$course->id", 'type' => 'activity');
44 $navlinks[] = array('name' => format_string($chat->name,true), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
45 $navlinks[] = array('name' => $strchatreport, 'link' => "report.php?id=$cm->id", 'type' => 'title');
47 $navigation = build_navigation($navlinks);
49 print_header_simple(format_string($chat->name).": $strchatreport", '', $navigation,
50 '', '', true, '', navmenu($course, $cm));
52 /// Check to see if groups are being used here
53 $groupmode = groups_get_activity_groupmode($cm);
54 $currentgroup = groups_get_activity_group($cm, true);
55 groups_print_activity_menu($cm, "report.php?id=$cm->id");
58 if ($currentgroup) {
59 $groupselect = " AND groupid = '$currentgroup'";
60 } else {
61 $groupselect = "";
64 if ($deletesession and has_capability('mod/chat:deletelog', $context)) {
65 notice_yesno(get_string('deletesessionsure', 'chat'),
66 "report.php?id=$cm->id&amp;deletesession=1&amp;confirmdelete=1&amp;start=$start&amp;end=$end&amp;sesskey=$USER->sesskey",
67 "report.php?id=$cm->id");
70 if (!$messages = get_records_select('chat_messages', "chatid = $chat->id AND
71 timestamp >= '$start' AND
72 timestamp <= '$end' $groupselect", "timestamp ASC")) {
73 print_heading(get_string('nomessages', 'chat'));
75 } else {
76 echo '<p class="boxaligncenter">'.userdate($start).' --> '. userdate($end).'</p>';
78 print_simple_box_start('center');
79 foreach ($messages as $message) { // We are walking FORWARDS through messages
80 $formatmessage = chat_format_message($message, $course->id, $USER);
81 if (isset($formatmessage->html)) {
82 echo $formatmessage->html;
85 print_simple_box_end();
88 if (!$deletesession or !has_capability('mod/chat:deletelog', $context)) {
89 print_continue("report.php?id=$cm->id");
92 print_footer($course);
93 exit;
97 /// Print the Sessions display
98 $navlinks[] = array('name' => $strchats, 'link' => "index.php?id=$course->id", 'type' => 'activity');
99 $navlinks[] = array('name' => format_string($chat->name,true), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
100 $navlinks[] = array('name' => $strchatreport, 'link' => '', 'type' => 'title');
102 $navigation = build_navigation($navlinks);
104 print_header_simple(format_string($chat->name).": $strchatreport", '', $navigation,
105 '', '', true, '', navmenu($course, $cm));
107 print_heading(format_string($chat->name).': '.get_string('sessions', 'chat'));
110 /// Check to see if groups are being used here
111 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
112 $currentgroup = groups_get_activity_group($cm, true);
113 groups_print_activity_menu($cm, "report.php?id=$cm->id");
114 } else {
115 $currentgroup = false;
118 if (!empty($currentgroup)) {
119 $groupselect = " AND groupid = '$currentgroup'";
120 } else {
121 $groupselect = "";
124 /// Delete a session if one has been specified
126 if ($deletesession and has_capability('mod/chat:deletelog', $context) and $confirmdelete and $start and $end and confirm_sesskey()) {
127 delete_records_select('chat_messages', "chatid = $chat->id AND
128 timestamp >= '$start' AND
129 timestamp <= '$end' $groupselect");
130 $strdeleted = get_string('deleted');
131 notify("$strdeleted: ".userdate($start).' --> '. userdate($end));
132 unset($deletesession);
136 /// Get the messages
138 if (empty($messages)) { /// May have already got them above
139 if (!$messages = get_records_select('chat_messages', "chatid = '$chat->id' $groupselect", "timestamp DESC")) {
140 print_heading(get_string('nomessages', 'chat'));
141 print_footer($course);
142 exit;
146 /// Show all the sessions
148 $sessiongap = 5 * 60; // 5 minutes silence means a new session
149 $sessionend = 0;
150 $sessionstart = 0;
151 $sessionusers = array();
152 $lasttime = 0;
154 $messagesleft = count($messages);
156 foreach ($messages as $message) { // We are walking BACKWARDS through the messages
158 $messagesleft --; // Countdown
160 if (!$lasttime) {
161 $lasttime = $message->timestamp;
163 if (!$sessionend) {
164 $sessionend = $message->timestamp;
166 if ((($lasttime - $message->timestamp) < $sessiongap) and $messagesleft) { // Same session
167 if ($message->userid and !$message->system) { // Remember user and count messages
168 if (empty($sessionusers[$message->userid])) {
169 $sessionusers[$message->userid] = 1;
170 } else {
171 $sessionusers[$message->userid] ++;
174 } else {
175 $sessionstart = $lasttime;
177 if ($sessionend - $sessionstart > 60 and count($sessionusers) > 1) {
179 echo '<p align="center">'.userdate($sessionstart).' --> '. userdate($sessionend).'</p>';
181 print_simple_box_start('center');
183 arsort($sessionusers);
184 foreach ($sessionusers as $sessionuser => $usermessagecount) {
185 if ($user = get_record('user', 'id', $sessionuser)) {
186 print_user_picture($user->id, $course->id, $user->picture);
187 echo '&nbsp;'.fullname($user, true); // XXX TODO use capability instead of true
188 echo "&nbsp;($usermessagecount)<br />";
192 echo '<p align="right">';
193 echo "<a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend\">$strseesession</a>";
194 if (has_capability('mod/chat:deletelog', $context)) {
195 echo "<br /><a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend&amp;deletesession=1\">$strdeletesession</a>";
197 echo '</p>';
198 print_simple_box_end();
201 $sessionend = $message->timestamp;
202 $sessionusers = array();
203 $sessionusers[$message->userid] = 1;
205 $lasttime = $message->timestamp;
208 /// Finish the page
209 print_footer($course);