Merge branch 'MDL-73971_311' of https://github.com/stronk7/moodle into MOODLE_311_STABLE
[moodle.git] / mod / chat / report.php
blob939cd75e0b3751f26dbbd9371824c1a26b0295b5
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 // This page prints reports and info about chats.
19 require_once('../../config.php');
20 require_once('lib.php');
22 $id = required_param('id', PARAM_INT);
23 $start = optional_param('start', 0, PARAM_INT); // Start of period.
24 $end = optional_param('end', 0, PARAM_INT); // End of period.
25 $deletesession = optional_param('deletesession', 0, PARAM_BOOL);
26 $confirmdelete = optional_param('confirmdelete', 0, PARAM_BOOL);
27 $showall = optional_param('show_all', 0, PARAM_BOOL);
29 $url = new moodle_url('/mod/chat/report.php', array('id' => $id));
30 if ($start !== 0) {
31 $url->param('start', $start);
33 if ($end !== 0) {
34 $url->param('end', $end);
36 if ($deletesession !== 0) {
37 $url->param('deletesession', $deletesession);
39 if ($confirmdelete !== 0) {
40 $url->param('confirmdelete', $confirmdelete);
42 $PAGE->set_url($url);
44 if (! $cm = get_coursemodule_from_id('chat', $id)) {
45 print_error('invalidcoursemodule');
47 if (! $chat = $DB->get_record('chat', array('id' => $cm->instance))) {
48 print_error('invalidcoursemodule');
50 if (! $course = $DB->get_record('course', array('id' => $chat->course))) {
51 print_error('coursemisconf');
54 $context = context_module::instance($cm->id);
55 $PAGE->set_context($context);
56 $PAGE->set_heading($course->fullname);
58 require_login($course, false, $cm);
60 if (empty($chat->studentlogs) && !has_capability('mod/chat:readlog', $context)) {
61 notice(get_string('nopermissiontoseethechatlog', 'chat'));
64 $params = array(
65 'context' => $context,
66 'objectid' => $chat->id,
67 'other' => array(
68 'start' => $start,
69 'end' => $end
72 $event = \mod_chat\event\sessions_viewed::create($params);
73 $event->add_record_snapshot('chat', $chat);
74 $event->trigger();
76 $strchats = get_string('modulenameplural', 'chat');
77 $strchat = get_string('modulename', 'chat');
78 $strchatreport = get_string('chatreport', 'chat');
79 $strseesession = get_string('seesession', 'chat');
80 $strdeletesession = get_string('deletesession', 'chat');
82 $navlinks = array();
84 $canexportsess = has_capability('mod/chat:exportsession', $context);
85 $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
87 // Print a session if one has been specified.
89 if ($start and $end and !$confirmdelete) { // Show a full transcript.
90 $PAGE->navbar->add($strchatreport);
91 $PAGE->set_title(format_string($chat->name).": $strchatreport");
92 echo $OUTPUT->header();
93 echo $OUTPUT->heading(format_string($chat->name), 2);
95 // Check to see if groups are being used here.
96 $groupmode = groups_get_activity_groupmode($cm);
97 $currentgroup = groups_get_activity_group($cm, true);
98 groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/report.php?id=$cm->id");
100 if ($deletesession and has_capability('mod/chat:deletelog', $context)) {
101 echo $OUTPUT->confirm(get_string('deletesessionsure', 'chat'),
102 "report.php?id=$cm->id&deletesession=1&confirmdelete=1&start=$start&end=$end",
103 "report.php?id=$cm->id");
106 if (!$messages = chat_get_session_messages($chat->id, $currentgroup, $start, $end, 'timestamp ASC')) {
107 echo $OUTPUT->heading(get_string('nomessages', 'chat'));
108 } else {
109 echo '<p class="boxaligncenter">'.userdate($start).' --> '. userdate($end).'</p>';
111 echo $OUTPUT->box_start('center');
112 $participates = array();
113 foreach ($messages as $message) { // We are walking FORWARDS through messages.
114 if (!isset($participates[$message->userid])) {
115 $participates[$message->userid] = true;
117 $formatmessage = chat_format_message($message, $course->id, $USER);
118 if (isset($formatmessage->html)) {
119 echo $formatmessage->html;
122 $participatedcap = array_key_exists($USER->id, $participates)
123 && has_capability('mod/chat:exportparticipatedsession', $context);
125 if (!empty($CFG->enableportfolios) && ($canexportsess || $participatedcap)) {
126 require_once($CFG->libdir . '/portfoliolib.php');
127 $buttonoptions = array(
128 'id' => $cm->id,
129 'start' => $start,
130 'end' => $end,
132 $button = new portfolio_add_button();
133 $button->set_callback_options('chat_portfolio_caller', $buttonoptions, 'mod_chat');
134 $button->render();
136 echo $OUTPUT->box_end();
139 if (!$deletesession or !has_capability('mod/chat:deletelog', $context)) {
140 echo $OUTPUT->continue_button("report.php?id=$cm->id");
143 echo $OUTPUT->footer();
144 exit;
148 // Print the Sessions display.
149 $PAGE->navbar->add($strchatreport);
150 $PAGE->set_title(format_string($chat->name).": $strchatreport");
151 echo $OUTPUT->header();
153 echo $OUTPUT->heading(format_string($chat->name).': '.get_string('sessions', 'chat'), 2);
155 // Check to see if groups are being used here
156 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used.
157 $currentgroup = groups_get_activity_group($cm, true);
158 groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/report.php?id=$cm->id");
159 } else {
160 $currentgroup = false;
163 $params = array('currentgroup' => $currentgroup, 'chatid' => $chat->id, 'start' => $start, 'end' => $end);
165 // If the user is allocated to a group, only show discussions with people in
166 // the same group, or no group.
167 if (!empty($currentgroup)) {
168 $groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
169 } else {
170 $groupselect = "";
173 // Delete a session if one has been specified.
175 if ($deletesession and has_capability('mod/chat:deletelog', $context)
176 and $confirmdelete and $start and $end and confirm_sesskey()) {
178 $DB->delete_records_select('chat_messages', "chatid = :chatid AND timestamp >= :start AND
179 timestamp <= :end $groupselect", $params);
180 $strdeleted = get_string('deleted');
181 echo $OUTPUT->notification("$strdeleted: ".userdate($start).' --> '. userdate($end));
182 unset($deletesession);
185 // Get the messages.
186 if (empty($messages)) { // May have already got them above.
187 if (!$messages = chat_get_session_messages($chat->id, $currentgroup, 0, 0, 'timestamp DESC')) {
188 echo $OUTPUT->heading(get_string('nomessages', 'chat'), 3);
189 echo $OUTPUT->footer();
190 exit;
194 if ($showall) {
195 $headingstr = get_string('listing_all_sessions', 'chat') . '&nbsp;';
196 $headingstr .= html_writer::link("report.php?id={$cm->id}&show_all=0", get_string('list_complete_sessions', 'chat'));
197 echo $OUTPUT->heading($headingstr, 3);
200 // Show all the sessions.
201 $completesessions = 0;
203 echo '<div class="list-group">';
205 $sessions = chat_get_sessions($messages, $showall);
207 foreach ($sessions as $session) {
208 echo '<div class="list-group-item">';
209 echo '<p>'.userdate($session->sessionstart).' --> '. userdate($session->sessionend).'</p>';
211 echo $OUTPUT->box_start();
213 arsort($session->sessionusers);
214 foreach ($session->sessionusers as $sessionuser => $usermessagecount) {
215 if ($user = $DB->get_record('user', array('id' => $sessionuser))) {
216 $OUTPUT->user_picture($user, array('courseid' => $course->id));
217 echo '&nbsp;' . fullname($user, $canviewfullnames);
218 echo "&nbsp;($usermessagecount)<br />";
222 echo '<p align="right">';
223 echo "<a href=\"report.php?id=$cm->id&amp;start=$session->sessionstart&amp;end=$session->sessionend\">$strseesession</a>";
224 $participatedcap = (array_key_exists($USER->id, $session->sessionusers)
225 && has_capability('mod/chat:exportparticipatedsession', $context));
226 if (!empty($CFG->enableportfolios) && ($canexportsess || $participatedcap)) {
227 require_once($CFG->libdir . '/portfoliolib.php');
228 $buttonoptions = array(
229 'id' => $cm->id,
230 'start' => $session->sessionstart,
231 'end' => $session->sessionend,
233 $button = new portfolio_add_button();
234 $button->set_callback_options('chat_portfolio_caller', $buttonoptions, 'mod_chat');
235 $portfoliobutton = $button->to_html(PORTFOLIO_ADD_TEXT_LINK);
236 if (!empty($portfoliobutton)) {
237 echo '<br />' . $portfoliobutton;
240 if (has_capability('mod/chat:deletelog', $context)) {
241 $deleteurl = "report.php?id=$cm->id&amp;start=$session->sessionstart&amp;end=$session->sessionend&amp;deletesession=1";
242 echo "<br /><a href=\"$deleteurl\">$strdeletesession</a>";
244 echo '</p>';
245 echo $OUTPUT->box_end();
246 echo '</div>';
248 if ($session->iscomplete) {
249 $completesessions++;
253 echo '</div>';
255 if (!empty($CFG->enableportfolios) && $canexportsess) {
256 require_once($CFG->libdir . '/portfoliolib.php');
257 $button = new portfolio_add_button();
258 $button->set_callback_options('chat_portfolio_caller', array('id' => $cm->id), 'mod_chat');
259 $button->render(null, get_string('addalltoportfolio', 'portfolio'));
263 if (!$showall and $completesessions == 0) {
264 echo html_writer::start_tag('p');
265 echo get_string('no_complete_sessions_found', 'chat') . '&nbsp;';
266 echo html_writer::link('report.php?id='.$cm->id.'&show_all=1', get_string('list_all_sessions', 'chat'));
267 echo html_writer::end_tag('p');
270 // Finish the page.
271 echo $OUTPUT->footer();