Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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  /**
  18   * Library of functions for chat outside of the core api
  19   */
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  require_once($CFG->dirroot . '/mod/chat/lib.php');
  24  require_once($CFG->libdir . '/portfolio/caller.php');
  25  
  26  /**
  27   * @package   mod_chat
  28   * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
  29   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  30   */
  31  class chat_portfolio_caller extends portfolio_module_caller_base {
  32      /** @var object */
  33      private $chat;
  34      /** @var int Timestamp */
  35      protected $start;
  36      /** @var int Timestamp */
  37      protected $end;
  38      /** @var array Chat messages */
  39      protected $messages = [];
  40      /** @var bool True if participated, otherwise false. */
  41      protected $participated;
  42  
  43      /**
  44       * @return array
  45       */
  46      public static function expected_callbackargs() {
  47          return array(
  48              'id'    => true,
  49              'start' => false,
  50              'end'   => false,
  51          );
  52      }
  53      /**
  54       * @global object
  55       */
  56      public function load_data() {
  57          global $DB;
  58  
  59          if (!$this->cm = get_coursemodule_from_id('chat', $this->id)) {
  60              throw new portfolio_caller_exception('invalidid', 'chat');
  61          }
  62          $this->chat = $DB->get_record('chat', array('id' => $this->cm->instance));
  63          $select = 'chatid = ?';
  64          $params = array($this->chat->id);
  65          if ($this->start && $this->end) {
  66              $select .= ' AND timestamp >= ? AND timestamp <= ?';
  67              $params[] = $this->start;
  68              $params[] = $this->end;
  69          }
  70          $this->messages = $DB->get_records_select(
  71                  'chat_messages',
  72                  $select,
  73                  $params,
  74                  'timestamp ASC'
  75              );
  76          $select .= ' AND userid = ?';
  77          $params[] = $this->user->id;
  78          $this->participated = $DB->record_exists_select(
  79              'chat_messages',
  80              $select,
  81              $params
  82          );
  83      }
  84      /**
  85       * @return array
  86       */
  87      public static function base_supported_formats() {
  88          return array(PORTFOLIO_FORMAT_PLAINHTML);
  89      }
  90      /**
  91       *
  92       */
  93      public function expected_time() {
  94          return portfolio_expected_time_db(count($this->messages));
  95      }
  96      /**
  97       * @return string
  98       */
  99      public function get_sha1() {
 100          $str = '';
 101          ksort($this->messages);
 102          foreach ($this->messages as $m) {
 103              $str .= implode('', (array)$m);
 104          }
 105          return sha1($str);
 106      }
 107  
 108      /**
 109       * @return bool
 110       */
 111      public function check_permissions() {
 112          $context = context_module::instance($this->cm->id);
 113          return has_capability('mod/chat:exportsession', $context)
 114              || ($this->participated
 115                  && has_capability('mod/chat:exportparticipatedsession', $context));
 116      }
 117  
 118      /**
 119       * @todo Document this function
 120       */
 121      public function prepare_package() {
 122          $content = '';
 123          $lasttime = 0;
 124          foreach ($this->messages as $message) {  // We are walking FORWARDS through messages
 125              $m = clone $message; // grrrrrr - this causes the sha1 to change as chat_format_message changes what it's passed.
 126              $formatmessage = chat_format_message($m, $this->cm->course, $this->user);
 127              if (!isset($formatmessage->html)) {
 128                  continue;
 129              }
 130              if (empty($lasttime) || (($message->timestamp - $lasttime) > CHAT_SESSION_GAP)) {
 131                  $content .= '<hr />';
 132                  $content .= userdate($message->timestamp);
 133              }
 134              $content .= $formatmessage->html;
 135              $lasttime = $message->timestamp;
 136          }
 137          $content = preg_replace('/\<img[^>]*\>/', '', $content);
 138  
 139          $this->exporter->write_new_file($content, clean_filename($this->cm->name . '-session.html'), false);
 140      }
 141  
 142      /**
 143       * @return string
 144       */
 145      public static function display_name() {
 146          return get_string('modulename', 'chat');
 147      }
 148  
 149      /**
 150       * @global object
 151       * @return string
 152       */
 153      public function get_return_url() {
 154          global $CFG;
 155  
 156          return $CFG->wwwroot . '/mod/chat/report.php?id='
 157              . $this->cm->id . ((isset($this->start)) ? '&start=' . $this->start . '&end=' . $this->end : '');
 158      }
 159  }
 160  
 161  /**
 162   * A chat event such a user entering or leaving a chat activity
 163   *
 164   * @package    mod_chat
 165   * @copyright  2012 Andrew Davis
 166   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 167   */
 168  class event_message implements renderable {
 169  
 170      /** @var string The URL of the profile of the user who caused the event */
 171      public $senderprofile;
 172  
 173      /** @var string The ready to display name of the user who caused the event */
 174      public $sendername;
 175  
 176      /** @var string Ready to display event time */
 177      public $time;
 178  
 179      /** @var string Event description */
 180      public $event;
 181  
 182      /** @var string The chat theme name */
 183      public $theme;
 184  
 185      /**
 186       * event_message constructor
 187       *
 188       * @param string $senderprofile The URL of the profile of the user who caused the event
 189       * @param string $sendername The ready to display name of the user who caused the event
 190       * @param string $time Ready to display event time
 191       * @param string $theme The chat theme name
 192       */
 193      public function __construct($senderprofile, $sendername, $time, $event, $theme) {
 194  
 195          $this->senderprofile = $senderprofile;
 196          $this->sendername = $sendername;
 197          $this->time = $time;
 198          $this->event = $event;
 199          $this->theme = $theme;
 200      }
 201  }
 202  
 203  /**
 204   * A chat message from a user
 205   *
 206   * @package    mod_chat
 207   * @copyright  2012 Andrew Davis
 208   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 209   */
 210  class user_message implements renderable {
 211  
 212      /** @var string The URL of the profile of the user sending the message */
 213      public $senderprofile;
 214  
 215      /** @var string The ready to display name of the user sending the message */
 216      public $sendername;
 217  
 218      /** @var string HTML for the avatar of the user sending the message */
 219      public $avatar;
 220  
 221      /** @var string Empty or a html class definition to append to the html */
 222      public $mymessageclass;
 223  
 224      /** @var string Ready to display message time */
 225      public $time;
 226  
 227      /** @var string The message */
 228      public $message;
 229  
 230      /** @var string The name of the chat theme to use */
 231      public $theme;
 232  
 233      /**
 234       * user_message constructor
 235       *
 236       * @param string $senderprofile The URL of the profile of the user sending the message
 237       * @param string $sendername The ready to display name of the user sending the message
 238       * @param string $avatar HTML for the avatar of the user sending the message
 239       * @param string $mymessageclass Empty or a html class definition to append to the html
 240       * @param string $time Ready to display message time
 241       * @param string $message The message
 242       * @param string $theme The name of the chat theme to use
 243       */
 244      public function __construct($senderprofile, $sendername, $avatar, $mymessageclass, $time, $message, $theme) {
 245  
 246          $this->senderprofile = $senderprofile;
 247          $this->sendername = $sendername;
 248          $this->avatar = $avatar;
 249          $this->mymessageclass = $mymessageclass;
 250          $this->time = $time;
 251          $this->message = $message;
 252          $this->theme = $theme;
 253      }
 254  }