weekly release 2.3dev
[moodle.git] / mod / chat / report.php
blob0c1da708934de81e396f3397c3003569d394e53f
1 <?php
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);
13 $show_all = optional_param('show_all', 0, PARAM_BOOL);
15 $url = new moodle_url('/mod/chat/report.php', array('id'=>$id));
16 if ($start !== 0) {
17 $url->param('start', $start);
19 if ($end !== 0) {
20 $url->param('end', $end);
22 if ($deletesession !== 0) {
23 $url->param('deletesession', $deletesession);
25 if ($confirmdelete !== 0) {
26 $url->param('confirmdelete', $confirmdelete);
28 $PAGE->set_url($url);
30 if (! $cm = get_coursemodule_from_id('chat', $id)) {
31 print_error('invalidcoursemodule');
33 if (! $chat = $DB->get_record('chat', array('id'=>$cm->instance))) {
34 print_error('invalidcoursemodule');
36 if (! $course = $DB->get_record('course', array('id'=>$chat->course))) {
37 print_error('coursemisconf');
40 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
41 $PAGE->set_context($context);
43 require_login($course->id, false, $cm);
45 if (empty($chat->studentlogs) && !has_capability('mod/chat:readlog', $context)) {
46 notice(get_string('nopermissiontoseethechatlog', 'chat'));
49 add_to_log($course->id, 'chat', 'report', "report.php?id=$cm->id", $chat->id, $cm->id);
51 $strchats = get_string('modulenameplural', 'chat');
52 $strchat = get_string('modulename', 'chat');
53 $strchatreport = get_string('chatreport', 'chat');
54 $strseesession = get_string('seesession', 'chat');
55 $strdeletesession = get_string('deletesession', 'chat');
57 $navlinks = array();
59 $canexportsess = has_capability('mod/chat:exportsession', $context);
61 /// Print a session if one has been specified
63 if ($start and $end and !$confirmdelete) { // Show a full transcript
64 $PAGE->navbar->add($strchatreport);
65 $PAGE->set_title(format_string($chat->name).": $strchatreport");
66 echo $OUTPUT->header();
68 /// Check to see if groups are being used here
69 $groupmode = groups_get_activity_groupmode($cm);
70 $currentgroup = groups_get_activity_group($cm, true);
71 groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/report.php?id=$cm->id");
73 $params = array('currentgroup'=>$currentgroup, 'chatid'=>$chat->id, 'start'=>$start, 'end'=>$end);
75 // If the user is allocated to a group, only show messages from people
76 // in the same group, or no group
77 if ($currentgroup) {
78 $groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
79 } else {
80 $groupselect = "";
83 if ($deletesession and has_capability('mod/chat:deletelog', $context)) {
84 echo $OUTPUT->confirm(get_string('deletesessionsure', 'chat'),
85 "report.php?id=$cm->id&deletesession=1&confirmdelete=1&start=$start&end=$end",
86 "report.php?id=$cm->id");
89 if (!$messages = $DB->get_records_select('chat_messages', "chatid = :chatid AND timestamp >= :start AND timestamp <= :end $groupselect", $params, "timestamp ASC")) {
90 echo $OUTPUT->heading(get_string('nomessages', 'chat'));
92 } else {
93 echo '<p class="boxaligncenter">'.userdate($start).' --> '. userdate($end).'</p>';
95 echo $OUTPUT->box_start('center');
96 $participates = array();
97 foreach ($messages as $message) { // We are walking FORWARDS through messages
98 if (!isset($participates[$message->userid])) {
99 $participates[$message->userid] = true;
101 $formatmessage = chat_format_message($message, $course->id, $USER);
102 if (isset($formatmessage->html)) {
103 echo $formatmessage->html;
106 $participatedcap = array_key_exists($USER->id, $participates) && has_capability('mod/chat:exportparticipatedsession', $context);
107 if (!empty($CFG->enableportfolios) && ($canexportsess || $participatedcap)) {
108 require_once($CFG->libdir . '/portfoliolib.php');
109 $buttonoptions = array(
110 'id' => $cm->id,
111 'start' => $start,
112 'end' => $end,
114 $button = new portfolio_add_button();
115 $button->set_callback_options('chat_portfolio_caller', $buttonoptions, '/mod/chat/locallib.php');
116 $button->render();
118 echo $OUTPUT->box_end();
121 if (!$deletesession or !has_capability('mod/chat:deletelog', $context)) {
122 echo $OUTPUT->continue_button("report.php?id=$cm->id");
125 echo $OUTPUT->footer();
126 exit;
130 /// Print the Sessions display
131 $PAGE->navbar->add($strchatreport);
132 $PAGE->set_title(format_string($chat->name).": $strchatreport");
133 echo $OUTPUT->header();
135 echo $OUTPUT->heading(format_string($chat->name).': '.get_string('sessions', 'chat'));
138 /// Check to see if groups are being used here
139 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
140 $currentgroup = groups_get_activity_group($cm, true);
141 groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/report.php?id=$cm->id");
142 } else {
143 $currentgroup = false;
146 $params = array('currentgroup'=>$currentgroup, 'chatid'=>$chat->id, 'start'=>$start, 'end'=>$end);
148 // If the user is allocated to a group, only show discussions with people in
149 // the same group, or no group
150 if (!empty($currentgroup)) {
151 $groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
152 } else {
153 $groupselect = "";
156 /// Delete a session if one has been specified
158 if ($deletesession and has_capability('mod/chat:deletelog', $context) and $confirmdelete and $start and $end and confirm_sesskey()) {
159 $DB->delete_records_select('chat_messages', "chatid = :chatid AND timestamp >= :start AND
160 timestamp <= :end $groupselect", $params);
161 $strdeleted = get_string('deleted');
162 echo $OUTPUT->notification("$strdeleted: ".userdate($start).' --> '. userdate($end));
163 unset($deletesession);
167 /// Get the messages
168 if (empty($messages)) { /// May have already got them above
169 if (!$messages = $DB->get_records_select('chat_messages', "chatid = :chatid $groupselect", $params, "timestamp DESC")) {
170 echo $OUTPUT->heading(get_string('nomessages', 'chat'));
171 echo $OUTPUT->footer();
172 exit;
176 if ($show_all) {
177 echo $OUTPUT->heading(get_string('listing_all_sessions', 'chat') .
178 '&nbsp;<a href="report.php?id='.$cm->id.'&amp;show_all=0">' .
179 get_string('list_complete_sessions', 'chat') . '</a>');
182 /// Show all the sessions
184 $sessiongap = 5 * 60; // 5 minutes silence means a new session
185 $sessionend = 0;
186 $sessionstart = 0;
187 $sessionusers = array();
188 $lasttime = 0;
189 $complete_sessions = 0;
191 $messagesleft = count($messages);
193 foreach ($messages as $message) { // We are walking BACKWARDS through the messages
195 $messagesleft --; // Countdown
197 if (!$lasttime) {
198 $lasttime = $message->timestamp;
200 if (!$sessionend) {
201 $sessionend = $message->timestamp;
203 if ((($lasttime - $message->timestamp) < $sessiongap) and $messagesleft) { // Same session
204 if ($message->userid and !$message->system) { // Remember user and count messages
205 if (empty($sessionusers[$message->userid])) {
206 $sessionusers[$message->userid] = 1;
207 } else {
208 $sessionusers[$message->userid] ++;
211 } else {
212 $sessionstart = $lasttime;
214 $is_complete = ($sessionend - $sessionstart > 60 and count($sessionusers) > 1);
215 if ($show_all or $is_complete) {
217 echo '<p align="center">'.userdate($sessionstart).' --> '. userdate($sessionend).'</p>';
219 echo $OUTPUT->box_start();
221 arsort($sessionusers);
222 foreach ($sessionusers as $sessionuser => $usermessagecount) {
223 if ($user = $DB->get_record('user', array('id'=>$sessionuser))) {
224 $OUTPUT->user_picture($user, array('courseid'=>$course->id));
225 echo '&nbsp;'.fullname($user, true); // XXX TODO use capability instead of true
226 echo "&nbsp;($usermessagecount)<br />";
230 echo '<p align="right">';
231 echo "<a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend\">$strseesession</a>";
232 $participatedcap = (array_key_exists($USER->id, $sessionusers) && has_capability('mod/chat:exportparticipatedsession', $context));
233 if (!empty($CFG->enableportfolios) && ($canexportsess || $participatedcap)) {
234 require_once($CFG->libdir . '/portfoliolib.php');
235 $buttonoptions = array(
236 'id' => $cm->id,
237 'start' => $sessionstart,
238 'end' => $sessionend,
240 $button = new portfolio_add_button();
241 $button->set_callback_options('chat_portfolio_caller', $buttonoptions, '/mod/chat/locallib.php');
242 $portfoliobutton = $button->to_html(PORTFOLIO_ADD_TEXT_LINK);
243 if (!empty($portfoliobutton)) {
244 echo '<br />' . $portfoliobutton;
247 if (has_capability('mod/chat:deletelog', $context)) {
248 echo "<br /><a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend&amp;deletesession=1\">$strdeletesession</a>";
250 echo '</p>';
251 echo $OUTPUT->box_end();
253 if ($is_complete) {
254 $complete_sessions++;
257 $sessionend = $message->timestamp;
258 $sessionusers = array();
259 $sessionusers[$message->userid] = 1;
261 $lasttime = $message->timestamp;
264 if (!empty($CFG->enableportfolios) && $canexportsess) {
265 require_once($CFG->libdir . '/portfoliolib.php');
266 $button = new portfolio_add_button();
267 $button->set_callback_options('chat_portfolio_caller', array('id' => $cm->id), '/mod/chat/locallib.php');
268 $button->render(null, get_string('addalltoportfolio', 'portfolio'));
272 if (!$show_all and $complete_sessions == 0) {
273 echo $OUTPUT->heading(get_string('no_complete_sessions_found', 'chat') .
274 '&nbsp;<a href="report.php?id='.$cm->id.'&amp;show_all=1">' .
275 get_string('list_all_sessions', 'chat') .
276 '</a>');
279 /// Finish the page
280 echo $OUTPUT->footer();