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');
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 = groupmode($course, $cm);
54 $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id");
57 $groupselect = " AND groupid = '$currentgroup'";
62 if ($deletesession and has_capability('mod/chat:deletelog', $context)) {
63 notice_yesno(get_string('deletesessionsure', 'chat'),
64 "report.php?id=$cm->id&deletesession=1&confirmdelete=1&start=$start&end=$end&sesskey=$USER->sesskey",
65 "report.php?id=$cm->id");
68 if (!$messages = get_records_select('chat_messages', "chatid = $chat->id AND
69 timestamp >= '$start' AND
70 timestamp <= '$end' $groupselect", "timestamp ASC")) {
71 print_heading(get_string('nomessages', 'chat'));
74 echo '<p class="boxaligncenter">'.userdate($start).' --> '. userdate($end).'</p>';
76 print_simple_box_start('center');
77 foreach ($messages as $message) { // We are walking FORWARDS through messages
78 $formatmessage = chat_format_message($message, $course->id
, $USER);
79 if (isset($formatmessage->html
)) {
80 echo $formatmessage->html
;
83 print_simple_box_end();
86 if (!$deletesession or !has_capability('mod/chat:deletelog', $context)) {
87 print_continue("report.php?id=$cm->id");
90 print_footer($course);
95 /// Print the Sessions display
96 $navlinks[] = array('name' => $strchats, 'link' => "index.php?id=$course->id", 'type' => 'activity');
97 $navlinks[] = array('name' => format_string($chat->name
,true), 'link' => "view.php?id=$cm->id", 'type' => 'activityinstance');
98 $navlinks[] = array('name' => $strchatreport, 'link' => '', 'type' => 'title');
100 $navigation = build_navigation($navlinks);
102 print_header_simple(format_string($chat->name
).": $strchatreport", '', $navigation,
103 '', '', true, '', navmenu($course, $cm));
105 print_heading(format_string($chat->name
).': '.get_string('sessions', 'chat'));
108 /// Check to see if groups are being used here
109 if ($groupmode = groupmode($course, $cm)) { // Groups are being used
110 $currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id");
112 $currentgroup = false;
115 if (!empty($currentgroup)) {
116 $groupselect = " AND groupid = '$currentgroup'";
121 /// Delete a session if one has been specified
123 if ($deletesession and has_capability('mod/chat:deletelog', $context) and $confirmdelete and $start and $end and confirm_sesskey()) {
124 delete_records_select('chat_messages', "chatid = $chat->id AND
125 timestamp >= '$start' AND
126 timestamp <= '$end' $groupselect");
127 $strdeleted = get_string('deleted');
128 notify("$strdeleted: ".userdate($start).' --> '. userdate($end));
129 unset($deletesession);
135 if (empty($messages)) { /// May have already got them above
136 if (!$messages = get_records_select('chat_messages', "chatid = '$chat->id' $groupselect", "timestamp DESC")) {
137 print_heading(get_string('nomessages', 'chat'));
138 print_footer($course);
143 /// Show all the sessions
145 $sessiongap = 5 * 60; // 5 minutes silence means a new session
148 $sessionusers = array();
151 $messagesleft = count($messages);
153 foreach ($messages as $message) { // We are walking BACKWARDS through the messages
155 $messagesleft --; // Countdown
158 $lasttime = $message->timestamp
;
161 $sessionend = $message->timestamp
;
163 if ((($lasttime - $message->timestamp
) < $sessiongap) and $messagesleft) { // Same session
164 if ($message->userid
and !$message->system
) { // Remember user and count messages
165 if (empty($sessionusers[$message->userid
])) {
166 $sessionusers[$message->userid
] = 1;
168 $sessionusers[$message->userid
] ++
;
172 $sessionstart = $lasttime;
174 if ($sessionend - $sessionstart > 60 and count($sessionusers) > 1) {
176 echo '<p align="center">'.userdate($sessionstart).' --> '. userdate($sessionend).'</p>';
178 print_simple_box_start('center');
180 arsort($sessionusers);
181 foreach ($sessionusers as $sessionuser => $usermessagecount) {
182 if ($user = get_record('user', 'id', $sessionuser)) {
183 print_user_picture($user->id
, $course->id
, $user->picture
);
184 echo ' '.fullname($user, true); // XXX TODO use capability instead of true
185 echo " ($usermessagecount)<br />";
189 echo '<p align="right">';
190 echo "<a href=\"report.php?id=$cm->id&start=$sessionstart&end=$sessionend\">$strseesession</a>";
191 if (has_capability('mod/chat:deletelog', $context)) {
192 echo "<br /><a href=\"report.php?id=$cm->id&start=$sessionstart&end=$sessionend&deletesession=1\">$strdeletesession</a>";
195 print_simple_box_end();
198 $sessionend = $message->timestamp
;
199 $sessionusers = array();
200 $sessionusers[$message->userid
] = 1;
202 $lasttime = $message->timestamp
;
206 print_footer($course);