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  $theme   = optional_param('chat_theme', 'course_theme', PARAM_SAFEDIR); // The value course_theme == the current theme.
  23  
  24  $url = new moodle_url('/mod/chat/gui_ajax/index.php', array('id' => $id));
  25  if ($groupid !== 0) {
  26      $url->param('groupid', $groupid);
  27  }
  28  $PAGE->set_url($url);
  29  $PAGE->set_popup_notification_allowed(false); // No popup notifications in the chat window.
  30  $PAGE->requires->strings_for_js(array('coursetheme', 'bubble', 'compact'), 'mod_chat');
  31  
  32  $chat = $DB->get_record('chat', array('id' => $id), '*', MUST_EXIST);
  33  $course = $DB->get_record('course', array('id' => $chat->course), '*', MUST_EXIST);
  34  $cm = get_coursemodule_from_instance('chat', $chat->id, $course->id, false, MUST_EXIST);
  35  
  36  $context = context_module::instance($cm->id);
  37  require_login($course, false, $cm);
  38  require_capability('mod/chat:chat', $context);
  39  
  40  // Check to see if groups are being used here.
  41  if ($groupmode = groups_get_activity_groupmode($cm)) {   // Groups are being used.
  42      if ($groupid = groups_get_activity_group($cm)) {
  43          if (!$group = groups_get_group($groupid)) {
  44              throw new \moodle_exception('invalidgroupid');
  45          }
  46          $groupname = ': '.$group->name;
  47      } else {
  48          $groupname = ': '.get_string('allparticipants');
  49      }
  50  } else {
  51      $groupid = 0;
  52      $groupname = '';
  53  }
  54  // If requested theme doesn't exist, use default 'bubble' theme.
  55  if ($theme != 'course_theme' and !file_exists(__DIR__ . '/theme/'.$theme.'/chat.css')) {
  56      $theme = 'compact';
  57  }
  58  
  59  // Log into the chat room.
  60  if (!$chatsid = chat_login_user($chat->id, 'ajax', $groupid, $course)) {
  61      throw new \moodle_exception('cantlogin', 'chat');
  62  }
  63  $courseshortname = format_string($course->shortname, true, array('context' => context_course::instance($course->id)));
  64  $module = array(
  65      'name'      => 'mod_chat_ajax', // Chat gui's are not real plugins, we have to break the naming standards for JS modules here.
  66      'fullpath'  => '/mod/chat/gui_ajax/module.js',
  67      'requires'  => array('base', 'dom', 'event', 'event-mouseenter', 'event-key', 'json-parse', 'io', 'overlay', 'yui2-resize',
  68                           'yui2-layout', 'yui2-menu'),
  69      'strings'   => array(array('send', 'chat'), array('sending', 'chat'), array('inputarea', 'chat'), array('userlist', 'chat'),
  70                           array('modulename', 'chat'), array('beep', 'chat'), array('talk', 'chat'))
  71  );
  72  $modulecfg = array(
  73      'home' => $CFG->wwwroot.'/mod/chat/view.php?id='.$cm->id,
  74      'chaturl' => $CFG->wwwroot.'/mod/chat/gui_ajax/index.php?id='.$id,
  75      'theme' => $theme,
  76      'userid' => $USER->id,
  77      'sid' => $chatsid,
  78      'timer' => 3000,
  79      'chat_lasttime' => 0,
  80      'chat_lastrow' => null,
  81      'chatroom_name' => $courseshortname . ": " . format_string($chat->name, true) . $groupname
  82  );
  83  $PAGE->requires->js_init_call('M.mod_chat_ajax.init', array($modulecfg), false, $module);
  84  
  85  $PAGE->set_title(get_string('modulename', 'chat').": $courseshortname: ".format_string($chat->name, true)."$groupname");
  86  $PAGE->add_body_class('yui-skin-sam');
  87  $PAGE->activityheader->disable();
  88  $PAGE->set_pagelayout('embedded');
  89  if ( $theme != 'course_theme') {
  90      $PAGE->requires->css('/mod/chat/gui_ajax/theme/'.$theme.'/chat.css');
  91  }
  92  
  93  echo $OUTPUT->header();
  94  echo $OUTPUT->box(html_writer::tag('h2',  get_string('participants'), array('class' => 'accesshide')) .
  95          '<ul id="users-list" class="list-group"></ul>', '', 'chat-userlist');
  96  echo $OUTPUT->box('', '', 'chat-options');
  97  echo $OUTPUT->box(html_writer::tag('h2',  get_string('messages', 'chat'), array('class' => 'accesshide')) .
  98          '<ul id="messages-list"></ul>', '', 'chat-messages');
  99  $table = new html_table();
 100  $table->data = array(
 101      array('<div class="form-inline"><div class="d-flex"><label class="accesshide" for="input-message">'.
 102            get_string('entermessage', 'chat').' </label>'.
 103            '<span class="form-group"><input type="text" disabled="true" class="form-control" ' .
 104            'id="input-message" value="Loading..." size="48" /></span>'.
 105            '<span class="form-group"><input type="button" id="button-send" class="btn btn-secondary mx-1" ' .
 106            'value="'.get_string('send', 'chat').'" />' .$OUTPUT->help_icon('usingchat', 'chat'). '</span></div>' .
 107            ' <div class="form-group d-flex ml-auto"><a id="choosetheme" href="###">'.
 108            get_string('themes').
 109            ' &raquo; </a></div></div>'));
 110  echo $OUTPUT->box(html_writer::tag('h2',  get_string('composemessage', 'chat'), array('class' => 'accesshide')) .
 111          html_writer::table($table), '', 'chat-input-area');
 112  echo $OUTPUT->box('', '', 'chat-notify');
 113  echo $OUTPUT->footer();