Moodle release 2.6beta
[moodle.git] / mod / chat / report.php
blobeab88ed9ccc759c90750ab9bc1a0b167654136ef
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 = context_module::instance($cm->id);
41 $PAGE->set_context($context);
42 $PAGE->set_heading($course->fullname);
44 require_login($course, false, $cm);
46 if (empty($chat->studentlogs) && !has_capability('mod/chat:readlog', $context)) {
47 notice(get_string('nopermissiontoseethechatlog', 'chat'));
50 $params = array(
51 'context' => $context,
52 'objectid' => $chat->id,
53 'other' => array(
54 'start' => $start,
55 'end' => $end
58 $event = \mod_chat\event\sessions_viewed::create($params);
59 $event->add_record_snapshot('chat', $chat);
60 $event->trigger();
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');
68 $navlinks = array();
70 $canexportsess = has_capability('mod/chat:exportsession', $context);
72 /// Print a session if one has been specified
74 if ($start and $end and !$confirmdelete) { // Show a full transcript
75 $PAGE->navbar->add($strchatreport);
76 $PAGE->set_title(format_string($chat->name).": $strchatreport");
77 echo $OUTPUT->header();
78 echo $OUTPUT->heading(format_string($chat->name), 2);
80 /// Check to see if groups are being used here
81 $groupmode = groups_get_activity_groupmode($cm);
82 $currentgroup = groups_get_activity_group($cm, true);
83 groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/report.php?id=$cm->id");
85 $params = array('currentgroup'=>$currentgroup, 'chatid'=>$chat->id, 'start'=>$start, 'end'=>$end);
87 // If the user is allocated to a group, only show messages from people
88 // in the same group, or no group
89 if ($currentgroup) {
90 $groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
91 } else {
92 $groupselect = "";
95 if ($deletesession and has_capability('mod/chat:deletelog', $context)) {
96 echo $OUTPUT->confirm(get_string('deletesessionsure', 'chat'),
97 "report.php?id=$cm->id&deletesession=1&confirmdelete=1&start=$start&end=$end",
98 "report.php?id=$cm->id");
101 if (!$messages = $DB->get_records_select('chat_messages', "chatid = :chatid AND timestamp >= :start AND timestamp <= :end $groupselect", $params, "timestamp ASC")) {
102 echo $OUTPUT->heading(get_string('nomessages', 'chat'));
104 } else {
105 echo '<p class="boxaligncenter">'.userdate($start).' --> '. userdate($end).'</p>';
107 echo $OUTPUT->box_start('center');
108 $participates = array();
109 foreach ($messages as $message) { // We are walking FORWARDS through messages
110 if (!isset($participates[$message->userid])) {
111 $participates[$message->userid] = true;
113 $formatmessage = chat_format_message($message, $course->id, $USER);
114 if (isset($formatmessage->html)) {
115 echo $formatmessage->html;
118 $participatedcap = array_key_exists($USER->id, $participates) && has_capability('mod/chat:exportparticipatedsession', $context);
119 if (!empty($CFG->enableportfolios) && ($canexportsess || $participatedcap)) {
120 require_once($CFG->libdir . '/portfoliolib.php');
121 $buttonoptions = array(
122 'id' => $cm->id,
123 'start' => $start,
124 'end' => $end,
126 $button = new portfolio_add_button();
127 $button->set_callback_options('chat_portfolio_caller', $buttonoptions, 'mod_chat');
128 $button->render();
130 echo $OUTPUT->box_end();
133 if (!$deletesession or !has_capability('mod/chat:deletelog', $context)) {
134 echo $OUTPUT->continue_button("report.php?id=$cm->id");
137 echo $OUTPUT->footer();
138 exit;
142 /// Print the Sessions display
143 $PAGE->navbar->add($strchatreport);
144 $PAGE->set_title(format_string($chat->name).": $strchatreport");
145 echo $OUTPUT->header();
147 echo $OUTPUT->heading(format_string($chat->name).': '.get_string('sessions', 'chat'), 2);
149 /// Check to see if groups are being used here
150 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
151 $currentgroup = groups_get_activity_group($cm, true);
152 groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/report.php?id=$cm->id");
153 } else {
154 $currentgroup = false;
157 $params = array('currentgroup'=>$currentgroup, 'chatid'=>$chat->id, 'start'=>$start, 'end'=>$end);
159 // If the user is allocated to a group, only show discussions with people in
160 // the same group, or no group
161 if (!empty($currentgroup)) {
162 $groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
163 } else {
164 $groupselect = "";
167 /// Delete a session if one has been specified
169 if ($deletesession and has_capability('mod/chat:deletelog', $context) and $confirmdelete and $start and $end and confirm_sesskey()) {
170 $DB->delete_records_select('chat_messages', "chatid = :chatid AND timestamp >= :start AND
171 timestamp <= :end $groupselect", $params);
172 $strdeleted = get_string('deleted');
173 echo $OUTPUT->notification("$strdeleted: ".userdate($start).' --> '. userdate($end));
174 unset($deletesession);
178 /// Get the messages
179 if (empty($messages)) { /// May have already got them above
180 if (!$messages = $DB->get_records_select('chat_messages', "chatid = :chatid $groupselect", $params, "timestamp DESC")) {
181 echo $OUTPUT->heading(get_string('nomessages', 'chat'), 3);
182 echo $OUTPUT->footer();
183 exit;
187 if ($show_all) {
188 $headingstr = get_string('listing_all_sessions', 'chat') . '&nbsp;';
189 $headingstr .= html_writer::link("report.php?id={$cm->id}&amp;show_all=0", get_string('list_complete_sessions', 'chat'));
190 echo $OUTPUT->heading($headingstr, 3);
193 /// Show all the sessions
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;
202 $messagesleft = count($messages);
204 foreach ($messages as $message) { // We are walking BACKWARDS through the messages
206 $messagesleft --; // Countdown
208 if (!$lasttime) {
209 $lasttime = $message->timestamp;
211 if (!$sessionend) {
212 $sessionend = $message->timestamp;
214 if ((($lasttime - $message->timestamp) < $sessiongap) and $messagesleft) { // Same session
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] ++;
222 } else {
223 $sessionstart = $lasttime;
225 $is_complete = ($sessionend - $sessionstart > 60 and count($sessionusers) > 1);
226 if ($show_all or $is_complete) {
228 echo '<p align="center">'.userdate($sessionstart).' --> '. userdate($sessionend).'</p>';
230 echo $OUTPUT->box_start();
232 arsort($sessionusers);
233 foreach ($sessionusers as $sessionuser => $usermessagecount) {
234 if ($user = $DB->get_record('user', array('id'=>$sessionuser))) {
235 $OUTPUT->user_picture($user, array('courseid'=>$course->id));
236 echo '&nbsp;'.fullname($user, true); // XXX TODO use capability instead of true
237 echo "&nbsp;($usermessagecount)<br />";
241 echo '<p align="right">';
242 echo "<a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend\">$strseesession</a>";
243 $participatedcap = (array_key_exists($USER->id, $sessionusers) && has_capability('mod/chat:exportparticipatedsession', $context));
244 if (!empty($CFG->enableportfolios) && ($canexportsess || $participatedcap)) {
245 require_once($CFG->libdir . '/portfoliolib.php');
246 $buttonoptions = array(
247 'id' => $cm->id,
248 'start' => $sessionstart,
249 'end' => $sessionend,
251 $button = new portfolio_add_button();
252 $button->set_callback_options('chat_portfolio_caller', $buttonoptions, 'mod_chat');
253 $portfoliobutton = $button->to_html(PORTFOLIO_ADD_TEXT_LINK);
254 if (!empty($portfoliobutton)) {
255 echo '<br />' . $portfoliobutton;
258 if (has_capability('mod/chat:deletelog', $context)) {
259 echo "<br /><a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend&amp;deletesession=1\">$strdeletesession</a>";
261 echo '</p>';
262 echo $OUTPUT->box_end();
264 if ($is_complete) {
265 $complete_sessions++;
268 $sessionend = $message->timestamp;
269 $sessionusers = array();
270 $sessionusers[$message->userid] = 1;
272 $lasttime = $message->timestamp;
275 if (!empty($CFG->enableportfolios) && $canexportsess) {
276 require_once($CFG->libdir . '/portfoliolib.php');
277 $button = new portfolio_add_button();
278 $button->set_callback_options('chat_portfolio_caller', array('id' => $cm->id), 'mod_chat');
279 $button->render(null, get_string('addalltoportfolio', 'portfolio'));
283 if (!$show_all and $complete_sessions == 0) {
284 echo html_writer::start_tag('p');
285 echo get_string('no_complete_sessions_found', 'chat') . '&nbsp;';
286 echo html_writer::link('report.php?id='.$cm->id.'&amp;show_all=1', get_string('list_all_sessions', 'chat'));
287 echo html_writer::end_tag('p');
290 /// Finish the page
291 echo $OUTPUT->footer();