| 1 | 
           efrain | 
           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|null null if the instance could not be loaded.
  | 
        
        
            | 
            | 
           73 | 
                */
  | 
        
        
            | 
            | 
           74 | 
               protected function get_instance(): ?instance {
  | 
        
        
            | 
            | 
           75 | 
                   // This means the customdata is broken, and needs to be fixed.
  | 
        
        
            | 
            | 
           76 | 
                   if (empty($this->get_custom_data()->instanceid)) {
  | 
        
        
            | 
            | 
           77 | 
                       throw new \coding_exception("Task custom data was missing instanceid");
  | 
        
        
            | 
            | 
           78 | 
                   }
  | 
        
        
            | 
            | 
           79 | 
              | 
        
        
            | 
            | 
           80 | 
                   if ($this->instance === null) {
  | 
        
        
            | 
            | 
           81 | 
                       $this->instance = instance::get_from_instanceid($this->get_custom_data()->instanceid);
  | 
        
        
            | 
            | 
           82 | 
                   }
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
                   return $this->instance;
  | 
        
        
            | 
            | 
           85 | 
               }
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
               /**
  | 
        
        
            | 
            | 
           88 | 
                * Get the preferred course contact for this notification.
  | 
        
        
            | 
            | 
           89 | 
                *
  | 
        
        
            | 
            | 
           90 | 
                * @return stdClass
  | 
        
        
            | 
            | 
           91 | 
                */
  | 
        
        
            | 
            | 
           92 | 
               protected function get_course_contact(): stdClass {
  | 
        
        
            | 
            | 
           93 | 
                   global $DB;
  | 
        
        
            | 
            | 
           94 | 
              | 
        
        
            | 
            | 
           95 | 
                   if ($this->coursecontact === null) {
  | 
        
        
            | 
            | 
           96 | 
                       // Get course managers so they can be highlighted in the list.
  | 
        
        
            | 
            | 
           97 | 
                       $coursecontext = $this->get_instance()->get_course_context();
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                       if ($managerroles = get_config('', 'coursecontact')) {
  | 
        
        
            | 
            | 
           100 | 
                           $coursecontactroles = explode(',', $managerroles);
  | 
        
        
            | 
            | 
           101 | 
                           foreach ($coursecontactroles as $roleid) {
  | 
        
        
            | 
            | 
           102 | 
                               $contacts = get_role_users($roleid, $coursecontext, true, 'u.id', 'u.id ASC');
  | 
        
        
            | 
            | 
           103 | 
                               foreach ($contacts as $contact) {
  | 
        
        
            | 
            | 
           104 | 
                                   $this->coursecontact = $contact;
  | 
        
        
            | 
            | 
           105 | 
                                   break;
  | 
        
        
            | 
            | 
           106 | 
                               }
  | 
        
        
            | 
            | 
           107 | 
                           }
  | 
        
        
            | 
            | 
           108 | 
                       }
  | 
        
        
            | 
            | 
           109 | 
              | 
        
        
            | 
            | 
           110 | 
                       if ($this->coursecontact === null) {
  | 
        
        
            | 
            | 
           111 | 
                           $this->coursecontact = \core_user::get_noreply_user();
  | 
        
        
            | 
            | 
           112 | 
                       }
  | 
        
        
            | 
            | 
           113 | 
                   }
  | 
        
        
            | 
            | 
           114 | 
              | 
        
        
            | 
            | 
           115 | 
                   return $this->coursecontact;
  | 
        
        
            | 
            | 
           116 | 
               }
  | 
        
        
            | 
            | 
           117 | 
              | 
        
        
            | 
            | 
           118 | 
               /**
  | 
        
        
            | 
            | 
           119 | 
                * Get the list of recipients for the notification.
  | 
        
        
            | 
            | 
           120 | 
                *
  | 
        
        
            | 
            | 
           121 | 
                * @return stdClass[]
  | 
        
        
            | 
            | 
           122 | 
                */
  | 
        
        
            | 
            | 
           123 | 
               protected function get_recipients(): array {
  | 
        
        
            | 
            | 
           124 | 
                   // Potential users should be active users only.
  | 
        
        
            | 
            | 
           125 | 
                   return get_enrolled_users(
  | 
        
        
            | 
            | 
           126 | 
                       $this->get_instance()->get_course_context(),
  | 
        
        
            | 
            | 
           127 | 
                       'mod/bigbluebuttonbn:view',
  | 
        
        
            | 
            | 
           128 | 
                       0,
  | 
        
        
            | 
            | 
           129 | 
                       'u.*',
  | 
        
        
            | 
            | 
           130 | 
                       null,
  | 
        
        
            | 
            | 
           131 | 
                       0,
  | 
        
        
            | 
            | 
           132 | 
                       0,
  | 
        
        
            | 
            | 
           133 | 
                       true
  | 
        
        
            | 
            | 
           134 | 
                   );
  | 
        
        
            | 
            | 
           135 | 
               }
  | 
        
        
            | 
            | 
           136 | 
              | 
        
        
            | 
            | 
           137 | 
               /**
  | 
        
        
            | 
            | 
           138 | 
                * Get the HTML message content.
  | 
        
        
            | 
            | 
           139 | 
                *
  | 
        
        
            | 
            | 
           140 | 
                * @return string
  | 
        
        
            | 
            | 
           141 | 
                */
  | 
        
        
            | 
            | 
           142 | 
               abstract protected function get_html_message(): string;
  | 
        
        
            | 
            | 
           143 | 
              | 
        
        
            | 
            | 
           144 | 
               /**
  | 
        
        
            | 
            | 
           145 | 
                * Get the plain text message content.
  | 
        
        
            | 
            | 
           146 | 
                *
  | 
        
        
            | 
            | 
           147 | 
                * @return string
  | 
        
        
            | 
            | 
           148 | 
                */
  | 
        
        
            | 
            | 
           149 | 
               protected function get_message(): string {
  | 
        
        
            | 
            | 
           150 | 
                   return html_to_text($this->get_html_message());
  | 
        
        
            | 
            | 
           151 | 
               }
  | 
        
        
            | 
            | 
           152 | 
              | 
        
        
            | 
            | 
           153 | 
               /**
  | 
        
        
            | 
            | 
           154 | 
                * Get the short summary message.
  | 
        
        
            | 
            | 
           155 | 
                *
  | 
        
        
            | 
            | 
           156 | 
                * @return string
  | 
        
        
            | 
            | 
           157 | 
                */
  | 
        
        
            | 
            | 
           158 | 
               abstract protected function get_small_message(): string;
  | 
        
        
            | 
            | 
           159 | 
              | 
        
        
            | 
            | 
           160 | 
               /**
  | 
        
        
            | 
            | 
           161 | 
                * Get the preferred message format
  | 
        
        
            | 
            | 
           162 | 
                *
  | 
        
        
            | 
            | 
           163 | 
                * @return string
  | 
        
        
            | 
            | 
           164 | 
                */
  | 
        
        
            | 
            | 
           165 | 
               protected function get_message_format(): string {
  | 
        
        
            | 
            | 
           166 | 
                   return FORMAT_HTML;
  | 
        
        
            | 
            | 
           167 | 
               }
  | 
        
        
            | 
            | 
           168 | 
              | 
        
        
            | 
            | 
           169 | 
               /**
  | 
        
        
            | 
            | 
           170 | 
                * Get the notification type.
  | 
        
        
            | 
            | 
           171 | 
                *
  | 
        
        
            | 
            | 
           172 | 
                * @return string
  | 
        
        
            | 
            | 
           173 | 
                */
  | 
        
        
            | 
            | 
           174 | 
               abstract protected function get_notification_type(): string;
  | 
        
        
            | 
            | 
           175 | 
              | 
        
        
            | 
            | 
           176 | 
               /**
  | 
        
        
            | 
            | 
           177 | 
                * Get the subject of the notification.
  | 
        
        
            | 
            | 
           178 | 
                *
  | 
        
        
            | 
            | 
           179 | 
                * @return string
  | 
        
        
            | 
            | 
           180 | 
                */
  | 
        
        
            | 
            | 
           181 | 
               abstract protected function get_subject(): string;
  | 
        
        
            | 
            | 
           182 | 
              | 
        
        
            | 
            | 
           183 | 
               /**
  | 
        
        
            | 
            | 
           184 | 
                * Send all of the notifications
  | 
        
        
            | 
            | 
           185 | 
                */
  | 
        
        
            | 
            | 
           186 | 
               protected function send_all_notifications(): void {
  | 
        
        
            | 
            | 
           187 | 
                   $instance = $this->get_instance();
  | 
        
        
            | 
            | 
           188 | 
              | 
        
        
            | 
            | 
           189 | 
                   // Cannot do anything without a valid instance.
  | 
        
        
            | 
            | 
           190 | 
                   if (empty($instance)) {
  | 
        
        
            | 
            | 
           191 | 
                       mtrace("Instance was empty, skipping");
  | 
        
        
            | 
            | 
           192 | 
                       return;
  | 
        
        
            | 
            | 
           193 | 
                   }
  | 
        
        
            | 
            | 
           194 | 
              | 
        
        
            | 
            | 
           195 | 
                   foreach ($this->get_recipients() as $recipient) {
  | 
        
        
            | 
            | 
           196 | 
                       try {
  | 
        
        
            | 
            | 
           197 | 
                           \core_user::require_active_user($recipient, true, true);
  | 
        
        
            | 
            | 
           198 | 
                           \core\cron::setup_user($recipient);
  | 
        
        
            | 
            | 
           199 | 
                       } catch (moodle_exception $e) {
  | 
        
        
            | 
            | 
           200 | 
                           // Skip sending.
  | 
        
        
            | 
            | 
           201 | 
                           continue;
  | 
        
        
            | 
            | 
           202 | 
                       }
  | 
        
        
            | 
            | 
           203 | 
              | 
        
        
            | 
            | 
           204 | 
                       $this->send_notification_to_current_user();
  | 
        
        
            | 
            | 
           205 | 
                   }
  | 
        
        
            | 
            | 
           206 | 
              | 
        
        
            | 
            | 
           207 | 
                   \core\cron::setup_user();
  | 
        
        
            | 
            | 
           208 | 
               }
  | 
        
        
            | 
            | 
           209 | 
              | 
        
        
            | 
            | 
           210 | 
               /**
  | 
        
        
            | 
            | 
           211 | 
                * Send the notificiation to the current user.
  | 
        
        
            | 
            | 
           212 | 
                */
  | 
        
        
            | 
            | 
           213 | 
               protected function send_notification_to_current_user(): void {
  | 
        
        
            | 
            | 
           214 | 
                   global $USER;
  | 
        
        
            | 
            | 
           215 | 
              | 
        
        
            | 
            | 
           216 | 
                   $instance = $this->get_instance();
  | 
        
        
            | 
            | 
           217 | 
              | 
        
        
            | 
            | 
           218 | 
                   $eventdata = new message();
  | 
        
        
            | 
            | 
           219 | 
                   $eventdata->courseid            = $instance->get_course_id();
  | 
        
        
            | 
            | 
           220 | 
                   $eventdata->component           = 'mod_bigbluebuttonbn';
  | 
        
        
            | 
            | 
           221 | 
                   $eventdata->name                = $this->get_notification_type();
  | 
        
        
            | 
            | 
           222 | 
                   $eventdata->userfrom            = $this->get_course_contact();
  | 
        
        
            | 
            | 
           223 | 
                   $eventdata->userto              = $USER;
  | 
        
        
            | 
            | 
           224 | 
              | 
        
        
            | 
            | 
           225 | 
                   $eventdata->subject             = $this->get_subject();
  | 
        
        
            | 
            | 
           226 | 
                   $eventdata->smallmessage        = $this->get_small_message();
  | 
        
        
            | 
            | 
           227 | 
                   $eventdata->fullmessage         = $this->get_message();
  | 
        
        
            | 
            | 
           228 | 
                   $eventdata->fullmessageformat   = $this->get_message_format();
  | 
        
        
            | 
            | 
           229 | 
                   $eventdata->fullmessagehtml     = $this->get_html_message();
  | 
        
        
            | 
            | 
           230 | 
                   $eventdata->notification        = 1;
  | 
        
        
            | 
            | 
           231 | 
                   $eventdata->contexturl          = $this->get_instance()->get_view_url();
  | 
        
        
            | 
            | 
           232 | 
                   $eventdata->contexturlname      = $this->get_instance()->get_meeting_name();
  | 
        
        
            | 
            | 
           233 | 
              | 
        
        
            | 
            | 
           234 | 
                   message_send($eventdata);
  | 
        
        
            | 
            | 
           235 | 
               }
  | 
        
        
            | 
            | 
           236 | 
           }
  |