2 // This file is part of Moodle - http://moodle.org/
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.
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));
31 $url->param('start', $start);
34 $url->param('end', $end);
36 if ($deletesession !== 0) {
37 $url->param('deletesession', $deletesession);
39 if ($confirmdelete !== 0) {
40 $url->param('confirmdelete', $confirmdelete);
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'));
65 'context' => $context,
66 'objectid' => $chat->id
,
72 $event = \mod_chat\event\sessions_viewed
::create($params);
73 $event->add_record_snapshot('chat', $chat);
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');
84 $canexportsess = has_capability('mod/chat:exportsession', $context);
86 // Print a session if one has been specified.
88 if ($start and $end and !$confirmdelete) { // Show a full transcript.
89 $PAGE->navbar
->add($strchatreport);
90 $PAGE->set_title(format_string($chat->name
).": $strchatreport");
91 echo $OUTPUT->header();
92 echo $OUTPUT->heading(format_string($chat->name
), 2);
94 // Check to see if groups are being used here.
95 $groupmode = groups_get_activity_groupmode($cm);
96 $currentgroup = groups_get_activity_group($cm, true);
97 groups_print_activity_menu($cm, $CFG->wwwroot
. "/mod/chat/report.php?id=$cm->id");
99 if ($deletesession and has_capability('mod/chat:deletelog', $context)) {
100 echo $OUTPUT->confirm(get_string('deletesessionsure', 'chat'),
101 "report.php?id=$cm->id&deletesession=1&confirmdelete=1&start=$start&end=$end",
102 "report.php?id=$cm->id");
105 if (!$messages = chat_get_session_messages($chat->id
, $currentgroup, $start, $end, 'timestamp ASC')) {
106 echo $OUTPUT->heading(get_string('nomessages', 'chat'));
108 echo '<p class="boxaligncenter">'.userdate($start).' --> '. userdate($end).'</p>';
110 echo $OUTPUT->box_start('center');
111 $participates = array();
112 foreach ($messages as $message) { // We are walking FORWARDS through messages.
113 if (!isset($participates[$message->userid
])) {
114 $participates[$message->userid
] = true;
116 $formatmessage = chat_format_message($message, $course->id
, $USER);
117 if (isset($formatmessage->html
)) {
118 echo $formatmessage->html
;
121 $participatedcap = array_key_exists($USER->id
, $participates)
122 && has_capability('mod/chat:exportparticipatedsession', $context);
124 if (!empty($CFG->enableportfolios
) && ($canexportsess ||
$participatedcap)) {
125 require_once($CFG->libdir
. '/portfoliolib.php');
126 $buttonoptions = array(
131 $button = new portfolio_add_button();
132 $button->set_callback_options('chat_portfolio_caller', $buttonoptions, 'mod_chat');
135 echo $OUTPUT->box_end();
138 if (!$deletesession or !has_capability('mod/chat:deletelog', $context)) {
139 echo $OUTPUT->continue_button("report.php?id=$cm->id");
142 echo $OUTPUT->footer();
147 // Print the Sessions display.
148 $PAGE->navbar
->add($strchatreport);
149 $PAGE->set_title(format_string($chat->name
).": $strchatreport");
150 echo $OUTPUT->header();
152 echo $OUTPUT->heading(format_string($chat->name
).': '.get_string('sessions', 'chat'), 2);
154 // Check to see if groups are being used here
155 if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used.
156 $currentgroup = groups_get_activity_group($cm, true);
157 groups_print_activity_menu($cm, $CFG->wwwroot
. "/mod/chat/report.php?id=$cm->id");
159 $currentgroup = false;
162 $params = array('currentgroup' => $currentgroup, 'chatid' => $chat->id
, 'start' => $start, 'end' => $end);
164 // If the user is allocated to a group, only show discussions with people in
165 // the same group, or no group.
166 if (!empty($currentgroup)) {
167 $groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
172 // Delete a session if one has been specified.
174 if ($deletesession and has_capability('mod/chat:deletelog', $context)
175 and $confirmdelete and $start and $end and confirm_sesskey()) {
177 $DB->delete_records_select('chat_messages', "chatid = :chatid AND timestamp >= :start AND
178 timestamp <= :end $groupselect", $params);
179 $strdeleted = get_string('deleted');
180 echo $OUTPUT->notification("$strdeleted: ".userdate($start).' --> '. userdate($end));
181 unset($deletesession);
185 if (empty($messages)) { // May have already got them above.
186 if (!$messages = chat_get_session_messages($chat->id
, $currentgroup, 0, 0, 'timestamp DESC')) {
187 echo $OUTPUT->heading(get_string('nomessages', 'chat'), 3);
188 echo $OUTPUT->footer();
194 $headingstr = get_string('listing_all_sessions', 'chat') . ' ';
195 $headingstr .= html_writer
::link("report.php?id={$cm->id}&show_all=0", get_string('list_complete_sessions', 'chat'));
196 echo $OUTPUT->heading($headingstr, 3);
199 // Show all the sessions.
200 $completesessions = 0;
202 echo '<div class="list-group">';
204 $sessions = chat_get_sessions($messages, $showall);
206 foreach ($sessions as $session) {
207 echo '<div class="list-group-item">';
208 echo '<p>'.userdate($session->sessionstart
).' --> '. userdate($session->sessionend
).'</p>';
210 echo $OUTPUT->box_start();
212 arsort($session->sessionusers
);
213 foreach ($session->sessionusers
as $sessionuser => $usermessagecount) {
214 if ($user = $DB->get_record('user', array('id' => $sessionuser))) {
215 $OUTPUT->user_picture($user, array('courseid' => $course->id
));
216 echo ' '.fullname($user, true); // XXX TODO use capability instead of true.
217 echo " ($usermessagecount)<br />";
221 echo '<p align="right">';
222 echo "<a href=\"report.php?id=$cm->id&start=$session->sessionstart&end=$session->sessionend\">$strseesession</a>";
223 $participatedcap = (array_key_exists($USER->id
, $session->sessionusers
)
224 && has_capability('mod/chat:exportparticipatedsession', $context));
225 if (!empty($CFG->enableportfolios
) && ($canexportsess ||
$participatedcap)) {
226 require_once($CFG->libdir
. '/portfoliolib.php');
227 $buttonoptions = array(
229 'start' => $session->sessionstart
,
230 'end' => $session->sessionend
,
232 $button = new portfolio_add_button();
233 $button->set_callback_options('chat_portfolio_caller', $buttonoptions, 'mod_chat');
234 $portfoliobutton = $button->to_html(PORTFOLIO_ADD_TEXT_LINK
);
235 if (!empty($portfoliobutton)) {
236 echo '<br />' . $portfoliobutton;
239 if (has_capability('mod/chat:deletelog', $context)) {
240 $deleteurl = "report.php?id=$cm->id&start=$session->sessionstart&end=$session->sessionend&deletesession=1";
241 echo "<br /><a href=\"$deleteurl\">$strdeletesession</a>";
244 echo $OUTPUT->box_end();
247 if ($session->iscomplete
) {
254 if (!empty($CFG->enableportfolios
) && $canexportsess) {
255 require_once($CFG->libdir
. '/portfoliolib.php');
256 $button = new portfolio_add_button();
257 $button->set_callback_options('chat_portfolio_caller', array('id' => $cm->id
), 'mod_chat');
258 $button->render(null, get_string('addalltoportfolio', 'portfolio'));
262 if (!$showall and $completesessions == 0) {
263 echo html_writer
::start_tag('p');
264 echo get_string('no_complete_sessions_found', 'chat') . ' ';
265 echo html_writer
::link('report.php?id='.$cm->id
.'&show_all=1', get_string('list_all_sessions', 'chat'));
266 echo html_writer
::end_tag('p');
270 echo $OUTPUT->footer();