Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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  require_once('../../../config.php');
  18  require_once ('../lib.php');
  19  
  20  $id      = required_param('id', PARAM_INT);
  21  $groupid = optional_param('groupid', 0, PARAM_INT);  // Only for teachers.
  22  $message = optional_param('message', '', PARAM_CLEANHTML);
  23  $refresh = optional_param('refresh', '', PARAM_RAW); // Force refresh.
  24  $last    = optional_param('last', 0, PARAM_INT);     // Last time refresh or sending.
  25  $newonly = optional_param('newonly', 0, PARAM_BOOL); // Show only new messages.
  26  
  27  $url = new moodle_url('/mod/chat/gui_basic/index.php', array('id' => $id));
  28  if ($groupid !== 0) {
  29      $url->param('groupid', $groupid);
  30  }
  31  if ($message !== 0) {
  32      $url->param('message', $message);
  33  }
  34  if ($refresh !== 0) {
  35      $url->param('refresh', $refresh);
  36  }
  37  if ($last !== 0) {
  38      $url->param('last', $last);
  39  }
  40  if ($newonly !== 0) {
  41      $url->param('newonly', $newonly);
  42  }
  43  $PAGE->set_url($url);
  44  
  45  if (!$chat = $DB->get_record('chat', array('id' => $id))) {
  46      throw new \moodle_exception('invalidid', 'chat');
  47  }
  48  
  49  if (!$course = $DB->get_record('course', array('id' => $chat->course))) {
  50      throw new \moodle_exception('invalidcourseid');
  51  }
  52  
  53  if (!$cm = get_coursemodule_from_instance('chat', $chat->id, $course->id)) {
  54      throw new \moodle_exception('invalidcoursemodule');
  55  }
  56  
  57  $context = context_module::instance($cm->id);
  58  require_login($course, false, $cm);
  59  require_capability('mod/chat:chat', $context);
  60  $PAGE->set_pagelayout('popup');
  61  $PAGE->set_popup_notification_allowed(false);
  62  
  63  // Check to see if groups are being used here.
  64  if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used.
  65      if ($groupid = groups_get_activity_group($cm)) {
  66          if (!$group = groups_get_group($groupid)) {
  67              throw new \moodle_exception('invalidgroupid');
  68          }
  69          $groupname = ': '.$group->name;
  70      } else {
  71          $groupname = ': '.get_string('allparticipants');
  72      }
  73  } else {
  74      $groupid = 0;
  75      $groupname = '';
  76  }
  77  
  78  $strchat  = get_string('modulename', 'chat'); // Must be before current_language() in chat_login_user() to force course language!
  79  $strchats = get_string('modulenameplural', 'chat');
  80  $stridle  = get_string('idle', 'chat');
  81  if (!$chatsid = chat_login_user($chat->id, 'basic', $groupid, $course)) {
  82      throw new \moodle_exception('cantlogin', 'chat');
  83  }
  84  
  85  if (!$chatusers = chat_get_users($chat->id, $groupid, $cm->groupingid)) {
  86      throw new \moodle_exception('errornousers', 'chat');
  87  }
  88  
  89  $DB->set_field('chat_users', 'lastping', time(), array('sid' => $chatsid));
  90  
  91  if (!isset($SESSION->chatprefs)) {
  92      $SESSION->chatprefs = array();
  93  }
  94  if (!isset($SESSION->chatprefs[$chat->id])) {
  95      $SESSION->chatprefs[$chat->id] = array();
  96      $SESSION->chatprefs[$chat->id]['chatentered'] = time();
  97  }
  98  $chatentered = $SESSION->chatprefs[$chat->id]['chatentered'];
  99  
 100  $refreshedmessage = '';
 101  
 102  if (!empty($refresh) and data_submitted()) {
 103      $refreshedmessage = $message;
 104  
 105      chat_delete_old_users();
 106  
 107  } else if (empty($refresh) and data_submitted() and confirm_sesskey()) {
 108  
 109      if ($message != '') {
 110  
 111          $chatuser = $DB->get_record('chat_users', array('sid' => $chatsid));
 112          chat_send_chatmessage($chatuser, $message, 0, $cm);
 113  
 114          $DB->set_field('chat_users', 'lastmessageping', time(), array('sid' => $chatsid));
 115      }
 116  
 117      chat_delete_old_users();
 118  
 119      $url = new moodle_url('/mod/chat/gui_basic/index.php', array('id' => $id, 'newonly' => $newonly, 'last' => $last));
 120      redirect($url);
 121  }
 122  
 123  $PAGE->set_title("$strchat: $course->shortname: ".format_string($chat->name, true)."$groupname");
 124  echo $OUTPUT->header();
 125  echo $OUTPUT->container_start(null, 'page-mod-chat-gui_basic');
 126  
 127  echo $OUTPUT->heading(format_string($course->shortname), 1);
 128  echo $OUTPUT->heading(format_string($chat->name), 2);
 129  
 130  echo $OUTPUT->heading(get_string('participants'), 3);
 131  
 132  echo $OUTPUT->box_start('generalbox', 'participants');
 133  echo '<ul>';
 134  foreach ($chatusers as $chu) {
 135      echo '<li class="clearfix">';
 136      echo $OUTPUT->user_picture($chu, array('size' => 24, 'courseid' => $course->id));
 137      echo '<div class="userinfo">';
 138      echo fullname($chu).' ';
 139      if ($idle = time() - $chu->lastmessageping) {
 140          echo '<span class="idle">'.$stridle.' '.format_time($idle).'</span>';
 141      } else {
 142          echo '<span class="idle" />';
 143      }
 144      echo '</div>';
 145      echo '</li>';
 146  }
 147  echo '</ul>';
 148  echo $OUTPUT->box_end();
 149  echo '<div id="send">';
 150  echo '<form id="editing" method="post" action="index.php">';
 151  
 152  echo '<h2><label for="message">' . get_string('sendmessage', 'message');
 153  echo $OUTPUT->help_icon('usingchat', 'chat');
 154  echo '</label></h2>';
 155  echo '<div class="mb-1">';
 156  echo '<input type="text" id="message" class="form-control" name="message" value="'.s($refreshedmessage, true).'" size="60" />';
 157  echo '</div><div class="mb-1">';
 158  echo '<input type="hidden" name="id" value="'.$id.'" />';
 159  echo '<input type="hidden" name="groupid" value="'.$groupid.'" />';
 160  echo '<input type="hidden" name="last" value="'.time().'" />';
 161  echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
 162  echo '<input type="submit" class="btn btn-primary" value="'.get_string('submit').'" />&nbsp;';
 163  echo '<input type="submit" class="btn btn-secondary" name="refresh" value="'.get_string('refresh').'" />';
 164  echo '<input type="checkbox" class="mx-1" name="newonly" id="newonly" '.($newonly ? 'checked="checked" ' : '').'/>';
 165  echo '<label for="newonly">'.get_string('newonlymsg', 'message').'</label>';
 166  echo '</div>';
 167  echo '</form>';
 168  echo '</div>';
 169  
 170  echo '<div id="messages">';
 171  echo $OUTPUT->heading(get_string('messages', 'chat'), 3);
 172  
 173  $allmessages = array();
 174  $options = new stdClass();
 175  $options->para = false;
 176  $options->newlines = true;
 177  
 178  $params = array('last' => $last, 'groupid' => $groupid, 'chatid' => $chat->id, 'chatentered' => $chatentered);
 179  
 180  if ($newonly) {
 181      $lastsql = "AND timestamp > :last";
 182  } else {
 183      $lastsql = "";
 184  }
 185  
 186  $groupselect = $groupid ? "AND (groupid=:groupid OR groupid=0)" : "";
 187  
 188  $messages = $DB->get_records_select("chat_messages_current",
 189                      "chatid = :chatid AND timestamp > :chatentered $lastsql $groupselect", $params,
 190                      "timestamp DESC");
 191  
 192  if ($messages) {
 193      foreach ($messages as $message) {
 194          $allmessages[] = chat_format_message($message, $course->id, $USER);
 195      }
 196  }
 197  echo '<table class="generaltable"><tbody>';
 198  echo '<tr>
 199          <th scope="col" class="cell">' . get_string('from') . '</th>
 200          <th scope="col" class="cell">' . get_string('message', 'message') . '</th>
 201          <th scope="col" class="cell">' . get_string('time') . '</th>
 202        </tr>';
 203  if (empty($allmessages)) {
 204      echo get_string('nomessagesfound', 'message');
 205  } else {
 206      foreach ($allmessages as $message) {
 207          echo $message->basic;
 208      }
 209  }
 210  echo '</tbody></table>';
 211  echo '</div>';
 212  echo $OUTPUT->container_end();
 213  echo $OUTPUT->footer();