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