moodle/mod/chat/report.php

239 lines
9.7 KiB
PHP
Raw Normal View History

<?php // $Id$
/// This page prints reports and info about chats
require_once('../../config.php');
require_once('lib.php');
$id = required_param('id', PARAM_INT);
$start = optional_param('start', 0, PARAM_INT); // Start of period
$end = optional_param('end', 0, PARAM_INT); // End of period
$deletesession = optional_param('deletesession', 0, PARAM_BOOL);
$confirmdelete = optional_param('confirmdelete', 0, PARAM_BOOL);
if (! $cm = get_coursemodule_from_id('chat', $id)) {
2008-06-06 08:46:48 +00:00
print_error('invalidcoursemodule');
}
2008-06-08 15:49:36 +00:00
if (! $chat = $DB->get_record('chat', array('id'=>$cm->instance))) {
2008-06-06 08:46:48 +00:00
print_error('invalidcoursemodule');
}
2008-06-08 15:49:36 +00:00
if (! $course = $DB->get_record('course', array('id'=>$chat->course))) {
2008-06-06 08:46:48 +00:00
print_error('coursemisconf');
}
2006-08-09 13:45:49 +00:00
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
require_login($course->id, false, $cm);
2008-06-18 01:45:01 +00:00
require_capability('mod/chat:readlog', $context);
add_to_log($course->id, 'chat', 'report', "report.php?id=$cm->id", $chat->id, $cm->id);
$strchats = get_string('modulenameplural', 'chat');
$strchat = get_string('modulename', 'chat');
$strchatreport = get_string('chatreport', 'chat');
$strseesession = get_string('seesession', 'chat');
$strdeletesession = get_string('deletesession', 'chat');
2004-01-16 13:14:42 +00:00
$navlinks = array();
2008-06-18 01:45:01 +00:00
/// Print a session if one has been specified
2004-01-16 13:14:42 +00:00
if ($start and $end and !$confirmdelete) { // Show a full transcript
$navigation = build_navigation($strchatreport, $cm);
print_header_simple(format_string($chat->name).": $strchatreport", '', $navigation,
'', '', true, '', navmenu($course, $cm));
/// Check to see if groups are being used here
$groupmode = groups_get_activity_groupmode($cm);
$currentgroup = groups_get_activity_group($cm, true);
groups_print_activity_menu($cm, "report.php?id=$cm->id");
2008-06-08 15:49:36 +00:00
$params = array('currentgroup'=>$currentgroup, 'chatid'=>$chat->id, 'start'=>$start, 'end'=>$end);
// If the user is allocated to a group, only show messages from people
// in the same group, or no group
if ($currentgroup) {
$groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
2004-02-24 07:21:52 +00:00
} else {
$groupselect = "";
}
if ($deletesession and has_capability('mod/chat:deletelog', $context)) {
notice_yesno(get_string('deletesessionsure', 'chat'),
2009-01-02 10:51:26 +00:00
"report.php?id=$cm->id&amp;deletesession=1&amp;confirmdelete=1&amp;start=$start&amp;end=$end&amp;sesskey=".sesskey(),
"report.php?id=$cm->id");
2004-01-16 13:14:42 +00:00
}
2008-06-18 01:45:01 +00:00
if (!$messages = $DB->get_records_select('chat_messages', "chatid = :chatid AND timestamp >= :start AND timestamp <= :end $groupselect", $params, "timestamp ASC")) {
print_heading(get_string('nomessages', 'chat'));
} else {
echo '<p class="boxaligncenter">'.userdate($start).' --> '. userdate($end).'</p>';
print_simple_box_start('center');
foreach ($messages as $message) { // We are walking FORWARDS through messages
$formatmessage = chat_format_message($message, $course->id, $USER);
if (isset($formatmessage->html)) {
echo $formatmessage->html;
}
}
if (has_capability('mod/chat:exportsession', $context)
|| (array_key_exists($USER->id, $sessionusers)
&& has_capability('mod/chat:exportparticipatedsession', $context))) {
require_once($CFG->libdir . '/portfoliolib.php');
$buttonoptions = array(
'id' => $cm->id,
'start' => $start,
'end' => $end,
);
$button = new portfolio_add_button();
$button->set_callback_options('chat_portfolio_caller', $buttonoptions, '/mod/chat/lib.php');
$button->render();
}
print_simple_box_end();
}
if (!$deletesession or !has_capability('mod/chat:deletelog', $context)) {
print_continue("report.php?id=$cm->id");
2004-01-16 13:14:42 +00:00
}
print_footer($course);
exit;
}
/// Print the Sessions display
$navigation = build_navigation($strchatreport, $cm);
print_header_simple(format_string($chat->name).": $strchatreport", '', $navigation,
'', '', true, '', navmenu($course, $cm));
2005-03-27 11:36:54 +00:00
print_heading(format_string($chat->name).': '.get_string('sessions', 'chat'));
2004-02-24 07:00:13 +00:00
/// Check to see if groups are being used here
if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
$currentgroup = groups_get_activity_group($cm, true);
groups_print_activity_menu($cm, "report.php?id=$cm->id");
} else {
$currentgroup = false;
}
2008-06-08 15:49:36 +00:00
$params = array('currentgroup'=>$currentgroup, 'chatid'=>$chat->id, 'start'=>$start, 'end'=>$end);
// If the user is allocated to a group, only show discussions with people in
// the same group, or no group
if (!empty($currentgroup)) {
$groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
} else {
$groupselect = "";
}
2004-01-16 13:14:42 +00:00
/// Delete a session if one has been specified
if ($deletesession and has_capability('mod/chat:deletelog', $context) and $confirmdelete and $start and $end and confirm_sesskey()) {
2008-06-08 15:49:36 +00:00
$DB->delete_records_select('chat_messages', "chatid = :chatid AND timestamp >= :start AND
timestamp <= :end $groupselect", $params);
$strdeleted = get_string('deleted');
notify("$strdeleted: ".userdate($start).' --> '. userdate($end));
2004-01-16 13:14:42 +00:00
unset($deletesession);
}
/// Get the messages
if (empty($messages)) { /// May have already got them above
2008-06-08 15:49:36 +00:00
if (!$messages = $DB->get_records_select('chat_messages', "chatid = :chatid $groupselect", $params, "timestamp DESC")) {
print_heading(get_string('nomessages', 'chat'));
2004-01-16 13:14:42 +00:00
print_footer($course);
exit;
}
}
2004-01-16 13:14:42 +00:00
/// Show all the sessions
$sessiongap = 5 * 60; // 5 minutes silence means a new session
2003-07-21 03:37:53 +00:00
$sessionend = 0;
$sessionstart = 0;
$sessionusers = array();
2003-07-21 03:37:53 +00:00
$lasttime = 0;
$messagesleft = count($messages);
foreach ($messages as $message) { // We are walking BACKWARDS through the messages
$messagesleft --; // Countdown
2003-07-21 03:37:53 +00:00
if (!$lasttime) {
$lasttime = $message->timestamp;
}
2003-07-21 03:37:53 +00:00
if (!$sessionend) {
$sessionend = $message->timestamp;
}
if ((($lasttime - $message->timestamp) < $sessiongap) and $messagesleft) { // Same session
if ($message->userid and !$message->system) { // Remember user and count messages
if (empty($sessionusers[$message->userid])) {
$sessionusers[$message->userid] = 1;
} else {
$sessionusers[$message->userid] ++;
}
2003-07-21 03:37:53 +00:00
}
} else {
2003-07-21 03:37:53 +00:00
$sessionstart = $lasttime;
2003-07-21 03:37:53 +00:00
if ($sessionend - $sessionstart > 60 and count($sessionusers) > 1) {
echo '<p align="center">'.userdate($sessionstart).' --> '. userdate($sessionend).'</p>';
print_simple_box_start('center');
arsort($sessionusers);
foreach ($sessionusers as $sessionuser => $usermessagecount) {
2008-06-08 15:49:36 +00:00
if ($user = $DB->get_record('user', array('id'=>$sessionuser))) {
print_user_picture($user, $course->id, $user->picture);
echo '&nbsp;'.fullname($user, true); // XXX TODO use capability instead of true
2003-07-21 05:31:23 +00:00
echo "&nbsp;($usermessagecount)<br />";
2003-07-21 03:37:53 +00:00
}
}
echo '<p align="right">';
echo "<a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend\">$strseesession</a>";
if (has_capability('mod/chat:exportsession', $context)
|| (array_key_exists($USER->id, $sessionusers)
&& has_capability('mod/chat:exportparticipatedsession', $context))) {
require_once($CFG->libdir . '/portfoliolib.php');
$buttonoptions = array(
'id' => $cm->id,
'start' => $sessionstart,
'end' => $sessionend,
);
$button = new portfolio_add_button();
$button->set_callback_options('chat_portfolio_caller', $buttonoptions, '/mod/chat/lib.php');
$button->render(PORTFOLIO_ADD_TEXT_LINK);
}
if (has_capability('mod/chat:deletelog', $context)) {
echo "<br /><a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend&amp;deletesession=1\">$strdeletesession</a>";
2004-01-16 13:14:42 +00:00
}
echo '</p>';
2003-07-21 03:37:53 +00:00
print_simple_box_end();
}
2003-07-21 03:37:53 +00:00
$sessionend = $message->timestamp;
$sessionusers = array();
$sessionusers[$message->userid] = 1;
}
2003-07-21 03:37:53 +00:00
$lasttime = $message->timestamp;
}
if (has_capability('mod/chat:exportsession', $context)) {
require_once($CFG->libdir . '/portfoliolib.php');
$button = new portfolio_add_button();
$button->set_callback_options('chat_portfolio_caller', array('id' => $cm->id), '/mod/chat/lib.php');
$button->render(null, get_string('addalltoportfolio', 'portfolio'));
}
/// Finish the page
print_footer($course);
?>