Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/chat/report.php
Go to the documentation of this file.
00001 <?php
00002 
00004 
00005     require_once('../../config.php');
00006     require_once('lib.php');
00007 
00008     $id            = required_param('id', PARAM_INT);
00009     $start         = optional_param('start', 0, PARAM_INT);   // Start of period
00010     $end           = optional_param('end', 0, PARAM_INT);     // End of period
00011     $deletesession = optional_param('deletesession', 0, PARAM_BOOL);
00012     $confirmdelete = optional_param('confirmdelete', 0, PARAM_BOOL);
00013     $show_all      = optional_param('show_all', 0, PARAM_BOOL);
00014 
00015     $url = new moodle_url('/mod/chat/report.php', array('id'=>$id));
00016     if ($start !== 0) {
00017         $url->param('start', $start);
00018     }
00019     if ($end !== 0) {
00020         $url->param('end', $end);
00021     }
00022     if ($deletesession !== 0) {
00023         $url->param('deletesession', $deletesession);
00024     }
00025     if ($confirmdelete !== 0) {
00026         $url->param('confirmdelete', $confirmdelete);
00027     }
00028     $PAGE->set_url($url);
00029 
00030     if (! $cm = get_coursemodule_from_id('chat', $id)) {
00031         print_error('invalidcoursemodule');
00032     }
00033     if (! $chat = $DB->get_record('chat', array('id'=>$cm->instance))) {
00034         print_error('invalidcoursemodule');
00035     }
00036     if (! $course = $DB->get_record('course', array('id'=>$chat->course))) {
00037         print_error('coursemisconf');
00038     }
00039 
00040     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00041     $PAGE->set_context($context);
00042 
00043     require_login($course->id, false, $cm);
00044 
00045     if (empty($chat->studentlogs) && !has_capability('mod/chat:readlog', $context)) {
00046         notice(get_string('nopermissiontoseethechatlog', 'chat'));
00047     }
00048 
00049     add_to_log($course->id, 'chat', 'report', "report.php?id=$cm->id", $chat->id, $cm->id);
00050 
00051     $strchats         = get_string('modulenameplural', 'chat');
00052     $strchat          = get_string('modulename', 'chat');
00053     $strchatreport    = get_string('chatreport', 'chat');
00054     $strseesession    = get_string('seesession', 'chat');
00055     $strdeletesession = get_string('deletesession', 'chat');
00056 
00057     $navlinks = array();
00058 
00059     $canexportsess = has_capability('mod/chat:exportsession', $context);
00060 
00062 
00063     if ($start and $end and !$confirmdelete) {   // Show a full transcript
00064         $PAGE->navbar->add($strchatreport);
00065         $PAGE->set_title(format_string($chat->name).": $strchatreport");
00066         echo $OUTPUT->header();
00067 
00069         $groupmode = groups_get_activity_groupmode($cm);
00070         $currentgroup = groups_get_activity_group($cm, true);
00071         groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/report.php?id=$cm->id");
00072 
00073         $params = array('currentgroup'=>$currentgroup, 'chatid'=>$chat->id, 'start'=>$start, 'end'=>$end);
00074 
00075         // If the user is allocated to a group, only show messages from people
00076         // in the same group, or no group
00077         if ($currentgroup) {
00078             $groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
00079         } else {
00080             $groupselect = "";
00081         }
00082 
00083         if ($deletesession and has_capability('mod/chat:deletelog', $context)) {
00084             echo $OUTPUT->confirm(get_string('deletesessionsure', 'chat'),
00085                          "report.php?id=$cm->id&deletesession=1&confirmdelete=1&start=$start&end=$end",
00086                          "report.php?id=$cm->id");
00087         }
00088 
00089         if (!$messages = $DB->get_records_select('chat_messages', "chatid = :chatid AND timestamp >= :start AND timestamp <= :end $groupselect", $params, "timestamp ASC")) {
00090             echo $OUTPUT->heading(get_string('nomessages', 'chat'));
00091 
00092         } else {
00093             echo '<p class="boxaligncenter">'.userdate($start).' --> '. userdate($end).'</p>';
00094 
00095             echo $OUTPUT->box_start('center');
00096             $participates = array();
00097             foreach ($messages as $message) {  // We are walking FORWARDS through messages
00098                 if (!isset($participates[$message->userid])) {
00099                     $participates[$message->userid] = true;
00100                 }
00101                 $formatmessage = chat_format_message($message, $course->id, $USER);
00102                 if (isset($formatmessage->html)) {
00103                     echo $formatmessage->html;
00104                 }
00105             }
00106             $participatedcap = array_key_exists($USER->id, $participates) && has_capability('mod/chat:exportparticipatedsession', $context);
00107             if (!empty($CFG->enableportfolios) && ($canexportsess || $participatedcap)) {
00108                 require_once($CFG->libdir . '/portfoliolib.php');
00109                 $buttonoptions  = array(
00110                     'id'    => $cm->id,
00111                     'start' => $start,
00112                     'end'   => $end,
00113                 );
00114                 $button = new portfolio_add_button();
00115                 $button->set_callback_options('chat_portfolio_caller', $buttonoptions, '/mod/chat/locallib.php');
00116                 $button->render();
00117             }
00118             echo $OUTPUT->box_end();
00119         }
00120 
00121         if (!$deletesession or !has_capability('mod/chat:deletelog', $context)) {
00122             echo $OUTPUT->continue_button("report.php?id=$cm->id");
00123         }
00124 
00125         echo $OUTPUT->footer();
00126         exit;
00127     }
00128 
00129 
00131     $PAGE->navbar->add($strchatreport);
00132     $PAGE->set_title(format_string($chat->name).": $strchatreport");
00133     echo $OUTPUT->header();
00134 
00135     echo $OUTPUT->heading(format_string($chat->name).': '.get_string('sessions', 'chat'));
00136 
00137 
00139     if ($groupmode = groups_get_activity_groupmode($cm)) {   // Groups are being used
00140         $currentgroup = groups_get_activity_group($cm, true);
00141         groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/report.php?id=$cm->id");
00142     } else {
00143         $currentgroup = false;
00144     }
00145 
00146     $params = array('currentgroup'=>$currentgroup, 'chatid'=>$chat->id, 'start'=>$start, 'end'=>$end);
00147 
00148     // If the user is allocated to a group, only show discussions with people in
00149     // the same group, or no group
00150     if (!empty($currentgroup)) {
00151         $groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
00152     } else {
00153         $groupselect = "";
00154     }
00155 
00157 
00158     if ($deletesession and has_capability('mod/chat:deletelog', $context) and $confirmdelete and $start and $end and confirm_sesskey()) {
00159         $DB->delete_records_select('chat_messages', "chatid = :chatid AND timestamp >= :start AND
00160                                                      timestamp <= :end $groupselect", $params);
00161         $strdeleted  = get_string('deleted');
00162         echo $OUTPUT->notification("$strdeleted: ".userdate($start).' --> '. userdate($end));
00163         unset($deletesession);
00164     }
00165 
00166 
00168     if (empty($messages)) {   
00169         if (!$messages = $DB->get_records_select('chat_messages', "chatid = :chatid $groupselect", $params, "timestamp DESC")) {
00170             echo $OUTPUT->heading(get_string('nomessages', 'chat'));
00171             echo $OUTPUT->footer();
00172             exit;
00173         }
00174     }
00175 
00176     if ($show_all) {
00177         echo $OUTPUT->heading(get_string('listing_all_sessions', 'chat') .
00178                       '&nbsp;<a href="report.php?id='.$cm->id.'&amp;show_all=0">' .
00179                       get_string('list_complete_sessions', 'chat') .  '</a>');
00180     }
00181 
00183 
00184     $sessiongap        = 5 * 60;    // 5 minutes silence means a new session
00185     $sessionend        = 0;
00186     $sessionstart      = 0;
00187     $sessionusers      = array();
00188     $lasttime          = 0;
00189     $complete_sessions = 0;
00190 
00191     $messagesleft = count($messages);
00192 
00193     foreach ($messages as $message) {  // We are walking BACKWARDS through the messages
00194 
00195         $messagesleft --;              // Countdown
00196 
00197         if (!$lasttime) {
00198             $lasttime = $message->timestamp;
00199         }
00200         if (!$sessionend) {
00201             $sessionend = $message->timestamp;
00202         }
00203         if ((($lasttime - $message->timestamp) < $sessiongap) and $messagesleft) {  // Same session
00204             if ($message->userid and !$message->system) {       // Remember user and count messages
00205                 if (empty($sessionusers[$message->userid])) {
00206                     $sessionusers[$message->userid] = 1;
00207                 } else {
00208                     $sessionusers[$message->userid] ++;
00209                 }
00210             }
00211         } else {
00212             $sessionstart = $lasttime;
00213 
00214             $is_complete = ($sessionend - $sessionstart > 60 and count($sessionusers) > 1);
00215             if ($show_all or $is_complete) {
00216 
00217                 echo '<p align="center">'.userdate($sessionstart).' --> '. userdate($sessionend).'</p>';
00218 
00219                 echo $OUTPUT->box_start();
00220 
00221                 arsort($sessionusers);
00222                 foreach ($sessionusers as $sessionuser => $usermessagecount) {
00223                     if ($user = $DB->get_record('user', array('id'=>$sessionuser))) {
00224                         $OUTPUT->user_picture($user, array('courseid'=>$course->id));
00225                         echo '&nbsp;'.fullname($user, true); // XXX TODO  use capability instead of true
00226                         echo "&nbsp;($usermessagecount)<br />";
00227                     }
00228                 }
00229 
00230                 echo '<p align="right">';
00231                 echo "<a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend\">$strseesession</a>";
00232                 $participatedcap = (array_key_exists($USER->id, $sessionusers) && has_capability('mod/chat:exportparticipatedsession', $context));
00233                 if (!empty($CFG->enableportfolios) && ($canexportsess || $participatedcap)) {
00234                     require_once($CFG->libdir . '/portfoliolib.php');
00235                     $buttonoptions  = array(
00236                         'id'    => $cm->id,
00237                         'start' => $sessionstart,
00238                         'end'   => $sessionend,
00239                     );
00240                     $button = new portfolio_add_button();
00241                     $button->set_callback_options('chat_portfolio_caller', $buttonoptions, '/mod/chat/locallib.php');
00242                     $portfoliobutton = $button->to_html(PORTFOLIO_ADD_TEXT_LINK);
00243                     if (!empty($portfoliobutton)) {
00244                         echo '<br />' . $portfoliobutton;
00245                     }
00246                 }
00247                 if (has_capability('mod/chat:deletelog', $context)) {
00248                     echo "<br /><a href=\"report.php?id=$cm->id&amp;start=$sessionstart&amp;end=$sessionend&amp;deletesession=1\">$strdeletesession</a>";
00249                 }
00250                 echo '</p>';
00251                 echo $OUTPUT->box_end();
00252             }
00253             if ($is_complete) {
00254                 $complete_sessions++;
00255             }
00256 
00257             $sessionend = $message->timestamp;
00258             $sessionusers = array();
00259             $sessionusers[$message->userid] = 1;
00260         }
00261         $lasttime = $message->timestamp;
00262     }
00263 
00264     if (!empty($CFG->enableportfolios) && $canexportsess) {
00265         require_once($CFG->libdir . '/portfoliolib.php');
00266         $button = new portfolio_add_button();
00267         $button->set_callback_options('chat_portfolio_caller', array('id' => $cm->id), '/mod/chat/locallib.php');
00268         $button->render(null, get_string('addalltoportfolio', 'portfolio'));
00269     }
00270 
00271 
00272     if (!$show_all and $complete_sessions == 0) {
00273         echo $OUTPUT->heading(get_string('no_complete_sessions_found', 'chat') .
00274                       '&nbsp;<a href="report.php?id='.$cm->id.'&amp;show_all=1">' .
00275                       get_string('list_all_sessions', 'chat') .
00276                       '</a>');
00277     }
00278 
00280     echo $OUTPUT->footer();
 All Data Structures Namespaces Files Functions Variables Enumerations