Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 310 and 400] [Versions 311 and 400] [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   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.
  13  //
  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/>.
  16  
  17  // This page prints reports and info about chats.
  18  
  19  require_once('../../config.php');
  20  require_once ('lib.php');
  21  
  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);
  28  
  29  $url = new moodle_url('/mod/chat/report.php', array('id' => $id));
  30  if ($start !== 0) {
  31      $url->param('start', $start);
  32  }
  33  if ($end !== 0) {
  34      $url->param('end', $end);
  35  }
  36  if ($deletesession !== 0) {
  37      $url->param('deletesession', $deletesession);
  38  }
  39  if ($confirmdelete !== 0) {
  40      $url->param('confirmdelete', $confirmdelete);
  41  }
  42  $PAGE->set_url($url);
  43  
  44  if (! $cm = get_coursemodule_from_id('chat', $id)) {
  45      print_error('invalidcoursemodule');
  46  }
  47  if (! $chat = $DB->get_record('chat', array('id' => $cm->instance))) {
  48      print_error('invalidcoursemodule');
  49  }
  50  if (! $course = $DB->get_record('course', array('id' => $chat->course))) {
  51      print_error('coursemisconf');
  52  }
  53  
  54  $context = context_module::instance($cm->id);
  55  $PAGE->set_context($context);
  56  $PAGE->set_heading($course->fullname);
  57  
  58  require_login($course, false, $cm);
  59  
  60  if (empty($chat->studentlogs) && !has_capability('mod/chat:readlog', $context)) {
  61      notice(get_string('nopermissiontoseethechatlog', 'chat'));
  62  }
  63  
  64  $params = array(
  65      'context' => $context,
  66      'objectid' => $chat->id,
  67      'other' => array(
  68          'start' => $start,
  69          'end' => $end
  70      )
  71  );
  72  $event = \mod_chat\event\sessions_viewed::create($params);
  73  $event->add_record_snapshot('chat', $chat);
  74  $event->trigger();
  75  
  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');
  81  
  82  $navlinks = array();
  83  
  84  $canexportsess = has_capability('mod/chat:exportsession', $context);
  85  $canviewfullnames = has_capability('moodle/site:viewfullnames', $context);
  86  
  87  $PAGE->activityheader->set_attrs([
  88      'title' => '',
  89      'description' => '',
  90      'hidecompletion' => true,
  91      'hideoverflow' => true,
  92  ]);
  93  // Print a session if one has been specified.
  94  
  95  if ($start and $end and !$confirmdelete) {   // Show a full transcript.
  96      $PAGE->navbar->add($strchatreport);
  97      echo $OUTPUT->header();
  98  
  99      // Check to see if groups are being used here.
 100      $groupmode = groups_get_activity_groupmode($cm);
 101      $currentgroup = groups_get_activity_group($cm, true);
 102      groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/report.php?id=$cm->id");
 103  
 104      if ($deletesession and has_capability('mod/chat:deletelog', $context)) {
 105          echo $OUTPUT->confirm(get_string('deletesessionsure', 'chat'),
 106                       "report.php?id=$cm->id&deletesession=1&confirmdelete=1&start=$start&end=$end",
 107                       "report.php?id=$cm->id");
 108      }
 109  
 110      if (!$messages = chat_get_session_messages($chat->id, $currentgroup, $start, $end, 'timestamp ASC')) {
 111          echo $OUTPUT->heading(get_string('nomessages', 'chat'));
 112      } else {
 113          echo '<p class="boxaligncenter">'.userdate($start).' --> '. userdate($end).'</p>';
 114  
 115          echo $OUTPUT->box_start('center');
 116          $participates = array();
 117          foreach ($messages as $message) {  // We are walking FORWARDS through messages.
 118              if (!isset($participates[$message->userid])) {
 119                  $participates[$message->userid] = true;
 120              }
 121              $formatmessage = chat_format_message($message, $course->id, $USER);
 122              if (isset($formatmessage->html)) {
 123                  echo $formatmessage->html;
 124              }
 125          }
 126          $participatedcap = array_key_exists($USER->id, $participates)
 127                             && has_capability('mod/chat:exportparticipatedsession', $context);
 128  
 129          if (!empty($CFG->enableportfolios) && ($canexportsess || $participatedcap)) {
 130              require_once($CFG->libdir . '/portfoliolib.php');
 131              $buttonoptions  = array(
 132                  'id'    => $cm->id,
 133                  'start' => $start,
 134                  'end'   => $end,
 135              );
 136              $button = new portfolio_add_button();
 137              $button->set_callback_options('chat_portfolio_caller', $buttonoptions, 'mod_chat');
 138              $button->render();
 139          }
 140          echo $OUTPUT->box_end();
 141      }
 142  
 143      if (!$deletesession or !has_capability('mod/chat:deletelog', $context)) {
 144          echo $OUTPUT->continue_button("report.php?id=$cm->id");
 145      }
 146  
 147      echo $OUTPUT->footer();
 148      exit;
 149  }
 150  
 151  
 152  // Print the Sessions display.
 153  $PAGE->navbar->add($strchatreport);
 154  $PAGE->set_title($strchatreport);
 155  echo $OUTPUT->header();
 156  
 157  echo $OUTPUT->heading(get_string('sessions', 'chat'), 2);
 158  
 159  // Check to see if groups are being used here
 160  if ($groupmode = groups_get_activity_groupmode($cm)) {   // Groups are being used.
 161      $currentgroup = groups_get_activity_group($cm, true);
 162      groups_print_activity_menu($cm, $CFG->wwwroot . "/mod/chat/report.php?id=$cm->id");
 163  } else {
 164      $currentgroup = false;
 165  }
 166  
 167  $params = array('currentgroup' => $currentgroup, 'chatid' => $chat->id, 'start' => $start, 'end' => $end);
 168  
 169  // If the user is allocated to a group, only show discussions with people in
 170  // the same group, or no group.
 171  if (!empty($currentgroup)) {
 172      $groupselect = " AND (groupid = :currentgroup OR groupid = 0)";
 173  } else {
 174      $groupselect = "";
 175  }
 176  
 177  // Delete a session if one has been specified.
 178  
 179  if ($deletesession and has_capability('mod/chat:deletelog', $context)
 180      and $confirmdelete and $start and $end and confirm_sesskey()) {
 181  
 182      $DB->delete_records_select('chat_messages', "chatid = :chatid AND timestamp >= :start AND
 183                                                   timestamp <= :end $groupselect", $params);
 184      $strdeleted  = get_string('deleted');
 185      echo $OUTPUT->notification("$strdeleted: ".userdate($start).' --> '. userdate($end));
 186      unset($deletesession);
 187  }
 188  
 189  // Get the messages.
 190  if (empty($messages)) {   // May have already got them above.
 191      if (!$messages = chat_get_session_messages($chat->id, $currentgroup, 0, 0, 'timestamp DESC')) {
 192          echo $OUTPUT->heading(get_string('nomessages', 'chat'), 3);
 193          echo $OUTPUT->footer();
 194          exit;
 195      }
 196  }
 197  
 198  if ($showall) {
 199      $headingstr = get_string('listing_all_sessions', 'chat') . '&nbsp;';
 200      $headingstr .= html_writer::link("report.php?id={$cm->id}&show_all=0", get_string('list_complete_sessions', 'chat'));
 201      echo  $OUTPUT->heading($headingstr, 3);
 202  }
 203  
 204  // Show all the sessions.
 205  $completesessions  = 0;
 206  
 207  echo '<div class="list-group">';
 208  
 209  $sessions = chat_get_sessions($messages, $showall);
 210  
 211  foreach ($sessions as $session) {
 212      echo '<div class="list-group-item">';
 213      echo '<p>'.userdate($session->sessionstart).' --> '. userdate($session->sessionend).'</p>';
 214  
 215      echo $OUTPUT->box_start();
 216  
 217      arsort($session->sessionusers);
 218      foreach ($session->sessionusers as $sessionuser => $usermessagecount) {
 219          if ($user = $DB->get_record('user', array('id' => $sessionuser))) {
 220              $OUTPUT->user_picture($user, array('courseid' => $course->id));
 221              echo '&nbsp;' . fullname($user, $canviewfullnames);
 222              echo "&nbsp;($usermessagecount)<br />";
 223          }
 224      }
 225  
 226      echo '<p align="right">';
 227      echo "<a href=\"report.php?id=$cm->id&amp;start=$session->sessionstart&amp;end=$session->sessionend\">$strseesession</a>";
 228      $participatedcap = (array_key_exists($USER->id, $session->sessionusers)
 229                         && has_capability('mod/chat:exportparticipatedsession', $context));
 230      if (!empty($CFG->enableportfolios) && ($canexportsess || $participatedcap)) {
 231          require_once($CFG->libdir . '/portfoliolib.php');
 232          $buttonoptions  = array(
 233              'id'    => $cm->id,
 234              'start' => $session->sessionstart,
 235              'end'   => $session->sessionend,
 236          );
 237          $button = new portfolio_add_button();
 238          $button->set_callback_options('chat_portfolio_caller', $buttonoptions, 'mod_chat');
 239          $portfoliobutton = $button->to_html(PORTFOLIO_ADD_TEXT_LINK);
 240          if (!empty($portfoliobutton)) {
 241              echo '<br />' . $portfoliobutton;
 242          }
 243      }
 244      if (has_capability('mod/chat:deletelog', $context)) {
 245          $deleteurl = "report.php?id=$cm->id&amp;start=$session->sessionstart&amp;end=$session->sessionend&amp;deletesession=1";
 246          echo "<br /><a href=\"$deleteurl\">$strdeletesession</a>";
 247      }
 248      echo '</p>';
 249      echo $OUTPUT->box_end();
 250      echo '</div>';
 251  
 252      if ($session->iscomplete) {
 253          $completesessions++;
 254      }
 255  }
 256  
 257  echo '</div>';
 258  
 259  if (!empty($CFG->enableportfolios) && $canexportsess) {
 260      require_once($CFG->libdir . '/portfoliolib.php');
 261      $button = new portfolio_add_button();
 262      $button->set_callback_options('chat_portfolio_caller', array('id' => $cm->id), 'mod_chat');
 263      $button->render(null, get_string('addalltoportfolio', 'portfolio'));
 264  }
 265  
 266  
 267  if (!$showall and $completesessions == 0) {
 268      echo html_writer::start_tag('p');
 269      echo get_string('no_complete_sessions_found', 'chat') . '&nbsp;';
 270      echo html_writer::link('report.php?id='.$cm->id.'&show_all=1', get_string('list_all_sessions', 'chat'));
 271      echo html_writer::end_tag('p');
 272  }
 273  
 274  // Finish the page.
 275  echo $OUTPUT->footer();