Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [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  namespace mod_bigbluebuttonbn\task;
  18  
  19  use core\message\message;
  20  use core\task\adhoc_task;
  21  use mod_bigbluebuttonbn\instance;
  22  use moodle_exception;
  23  use stdClass;
  24  
  25  /**
  26   * Class containing the abstract class for notification processes in BBB.
  27   *
  28   * @package   mod_bigbluebuttonbn
  29   * @copyright 2023 onwards, Blindside Networks Inc
  30   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  abstract class base_send_notification extends adhoc_task {
  33  
  34      /** @var instance */
  35      protected $instance = null;
  36  
  37      /** @var object */
  38      protected $coursecontact = null;
  39  
  40      /**
  41       * Execute the task.
  42       */
  43      public function execute() {
  44          $this->send_all_notifications();
  45      }
  46  
  47      /**
  48       * Append additional elements of custom data
  49       *
  50       * @param array $newdata
  51       */
  52      protected function append_custom_data(array $newdata): void {
  53          if ($currentdata = (array) $this->get_custom_data()) {
  54              $newdata = array_merge($currentdata, $newdata);
  55          }
  56  
  57          $this->set_custom_data($newdata);
  58      }
  59  
  60      /**
  61       * Set the instanceid in the custom data.
  62       *
  63       * @param int $instanceid
  64       */
  65      public function set_instance_id(int $instanceid): void {
  66          $this->append_custom_data(['instanceid' => $instanceid]);
  67      }
  68  
  69      /**
  70       * Get the bigbluebutton instance that this notification is for.
  71       *
  72       * @return instance
  73       */
  74      protected function get_instance(): instance {
  75          if ($this->instance === null) {
  76              $this->instance = instance::get_from_instanceid($this->get_custom_data()->instanceid);
  77          }
  78  
  79          return $this->instance;
  80      }
  81  
  82      /**
  83       * Get the preferred course contact for this notification.
  84       *
  85       * @return stdClass
  86       */
  87      protected function get_course_contact(): stdClass {
  88          global $DB;
  89  
  90          if ($this->coursecontact === null) {
  91              // Get course managers so they can be highlighted in the list.
  92              $coursecontext = $this->get_instance()->get_course_context();
  93  
  94              if ($managerroles = get_config('', 'coursecontact')) {
  95                  $coursecontactroles = explode(',', $managerroles);
  96                  foreach ($coursecontactroles as $roleid) {
  97                      $contacts = get_role_users($roleid, $coursecontext, true, 'u.id', 'u.id ASC');
  98                      foreach ($contacts as $contact) {
  99                          $this->coursecontact = $contact;
 100                          break;
 101                      }
 102                  }
 103              }
 104  
 105              if ($this->coursecontact === null) {
 106                  $this->coursecontact = \core_user::get_noreply_user();
 107              }
 108          }
 109  
 110          return $this->coursecontact;
 111      }
 112  
 113      /**
 114       * Get the list of recipients for the notification.
 115       *
 116       * @return stdClass[]
 117       */
 118      protected function get_recipients(): array {
 119          // Potential users should be active users only.
 120          return get_enrolled_users(
 121              $this->get_instance()->get_course_context(),
 122              'mod/bigbluebuttonbn:view',
 123              0,
 124              'u.*',
 125              null,
 126              0,
 127              0,
 128              true
 129          );
 130      }
 131  
 132      /**
 133       * Get the HTML message content.
 134       *
 135       * @return string
 136       */
 137      abstract protected function get_html_message(): string;
 138  
 139      /**
 140       * Get the plain text message content.
 141       *
 142       * @return string
 143       */
 144      protected function get_message(): string {
 145          return html_to_text($this->get_html_message());
 146      }
 147  
 148      /**
 149       * Get the short summary message.
 150       *
 151       * @return string
 152       */
 153      abstract protected function get_small_message(): string;
 154  
 155      /**
 156       * Get the preferred message format
 157       *
 158       * @return string
 159       */
 160      protected function get_message_format(): string {
 161          return FORMAT_HTML;
 162      }
 163  
 164      /**
 165       * Get the notification type.
 166       *
 167       * @return string
 168       */
 169      abstract protected function get_notification_type(): string;
 170  
 171      /**
 172       * Get the subject of the notification.
 173       *
 174       * @return string
 175       */
 176      abstract protected function get_subject(): string;
 177  
 178      /**
 179       * Send all of the notifications
 180       */
 181      protected function send_all_notifications(): void {
 182          $instance = $this->get_instance();
 183          foreach ($this->get_recipients() as $recipient) {
 184              try {
 185                  \core_user::require_active_user($recipient, true, true);
 186                  \core\cron::setup_user($recipient);
 187              } catch (moodle_exception $e) {
 188                  // Skip sending.
 189                  continue;
 190              }
 191  
 192              $this->send_notification_to_current_user();
 193          }
 194  
 195          \core\cron::setup_user();
 196      }
 197  
 198      /**
 199       * Send the notificiation to the current user.
 200       */
 201      protected function send_notification_to_current_user(): void {
 202          global $USER;
 203  
 204          $instance = $this->get_instance();
 205  
 206          $eventdata = new message();
 207          $eventdata->courseid            = $instance->get_course_id();
 208          $eventdata->component           = 'mod_bigbluebuttonbn';
 209          $eventdata->name                = $this->get_notification_type();
 210          $eventdata->userfrom            = $this->get_course_contact();
 211          $eventdata->userto              = $USER;
 212  
 213          $eventdata->subject             = $this->get_subject();
 214          $eventdata->smallmessage        = $this->get_small_message();
 215          $eventdata->fullmessage         = $this->get_message();
 216          $eventdata->fullmessageformat   = $this->get_message_format();
 217          $eventdata->fullmessagehtml     = $this->get_html_message();
 218          $eventdata->notification        = 1;
 219          $eventdata->contexturl          = $this->get_instance()->get_view_url();
 220          $eventdata->contexturlname      = $this->get_instance()->get_meeting_name();
 221  
 222          message_send($eventdata);
 223      }
 224  }