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.
   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  namespace mod_bigbluebuttonbn\form;
  17  
  18  use context;
  19  use core_form\dynamic_form;
  20  use mod_bigbluebuttonbn\instance;
  21  use mod_bigbluebuttonbn\local\exceptions\bigbluebutton_exception;
  22  use mod_bigbluebuttonbn\task\send_guest_emails;
  23  use moodle_exception;
  24  use moodle_url;
  25  use MoodleQuickForm;
  26  
  27  /**
  28   * Popup form to add new guests to a meeting and show/copy credential to access the guest login page.
  29   *
  30   * @package    mod_bigbluebuttonbn
  31   * @copyright  2022 onwards, Blindside Networks Inc
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   * @author     Laurent David  (laurent [at] call-learning [dt] fr)
  34   */
  35  class guest_add extends dynamic_form {
  36  
  37      /**
  38       * Max length for credential and url fields.
  39       */
  40      const MAX_INPUT_LENGTH = 35;
  41  
  42      /**
  43       * Process the form submission, used if form was submitted via AJAX.
  44       *
  45       * @return array
  46       */
  47      public function process_dynamic_submission(): array {
  48          global $USER;
  49          $data = $this->get_data();
  50          $allmails = [];
  51          if (!empty($data->emails)) {
  52              $emails = explode(',', $data->emails);
  53              foreach ($emails as $email) {
  54                  $email = trim($email);
  55                  if (validate_email($email)) {
  56                      $allmails[] = $email;
  57                  }
  58              }
  59              $adhoctask = new send_guest_emails();
  60              $adhoctask->set_custom_data(
  61                  [
  62                      'emails' => $allmails,
  63                      'useridfrom' => $USER->id
  64                  ]
  65              );
  66              $adhoctask->set_instance_id($data->id);
  67              \core\task\manager::queue_adhoc_task($adhoctask);
  68          }
  69          return [
  70              'result' => true,
  71              'emails' => join(', ', $allmails),
  72              'emailcount' => count($allmails),
  73              'errors' => ''
  74          ];
  75      }
  76  
  77      /**
  78       * Perform some validation.
  79       *
  80       * @param array $formdata
  81       * @param array $files
  82       * @return array
  83       */
  84      public function validation($formdata, $files): array {
  85          $errors = [];
  86          $emailserrors = [];
  87          if (!empty($formdata['emails'])) {
  88              $emails = explode(',', $formdata['emails']);
  89              foreach ($emails as $email) {
  90                  $email = trim($email);
  91                  if (!validate_email($email)) {
  92                      $emailserrors[] .= get_string('guestaccess_emails_invalidemail', 'mod_bigbluebuttonbn', $email);
  93                  }
  94              }
  95          }
  96          if (!empty($emailserrors)) {
  97              $errors['emails'] = \html_writer::alist($emailserrors);
  98          }
  99          return $errors;
 100      }
 101  
 102      /**
 103       * Load in existing data as form defaults (not applicable).
 104       *
 105       * @return void
 106       */
 107      public function set_data_for_dynamic_submission(): void {
 108          $instance = $this->get_instance_from_params();
 109          $data = [
 110              'id' => $instance->get_instance_id(),
 111              'groupid' => $instance->get_group_id(),
 112              'guestjoinurl' => $instance->get_guest_access_url(),
 113              'guestpassword' => $instance->get_guest_access_password(),
 114          ];
 115          $this->set_data($data);
 116  
 117      }
 118  
 119      /**
 120       * Get BigblueButton instance from context params
 121       *
 122       * @return instance
 123       * @throws moodle_exception
 124       */
 125      protected function get_instance_from_params(): instance {
 126          $bbid = $this->optional_param('id', null, PARAM_INT);
 127          $groupid = $this->optional_param('groupid', null, PARAM_INT);
 128          if (empty($bbid)) {
 129              throw new moodle_exception('guestaccess_add_no_id', 'mod_bigbluebuttonbn');
 130          }
 131          $instance = instance::get_from_instanceid($bbid);
 132          if ($groupid) {
 133              $instance->set_group_id($groupid);
 134          }
 135          return $instance;
 136      }
 137  
 138      /**
 139       * Form definition
 140       */
 141      protected function definition() {
 142          self::add_meeting_links_elements($this->_form);
 143          $mform = $this->_form;
 144          $mform->addElement('text', 'emails',
 145              get_string('guestaccess_emails', 'mod_bigbluebuttonbn'),
 146          );
 147          $mform->addHelpButton('emails', 'guestaccess_emails', 'mod_bigbluebuttonbn');
 148          $mform->setDefault('emails', '');
 149          $mform->setType('emails', PARAM_RAW);
 150          $mform->addElement('hidden', 'id');
 151          $mform->setType('id', PARAM_INT);
 152          $mform->addElement('hidden', 'groupid');
 153          $mform->setType('groupid', PARAM_INT);
 154      }
 155  
 156      /**
 157       * Add meeting links element. Helper for this form and the mod_form (module form)
 158       *
 159       * @param MoodleQuickForm $mform
 160       * @return void
 161       */
 162      public static function add_meeting_links_elements(MoodleQuickForm &$mform): void {
 163          global $CFG;
 164          MoodleQuickForm::registerElementType('text_with_copy',
 165              "$CFG->dirroot/mod/bigbluebuttonbn/classes/form/text_with_copy_element.php",
 166              text_with_copy_element::class);
 167          $mform->addElement('text_with_copy', 'guestjoinurl',
 168              get_string('guestaccess_meeting_link', 'mod_bigbluebuttonbn'),
 169              [
 170                  'copylabel' => get_string('guestaccess_copy_link', 'mod_bigbluebuttonbn'),
 171                  'size' => self::MAX_INPUT_LENGTH,
 172                  'readonly' => 'readonly'
 173              ]
 174          );
 175          $mform->setType('guestjoinurl', PARAM_URL);
 176          $mform->addElement('text_with_copy', 'guestpassword',
 177              get_string('guestaccess_meeting_password', 'mod_bigbluebuttonbn'),
 178              [
 179                  'copylabel' => get_string('guestaccess_copy_password', 'mod_bigbluebuttonbn'),
 180                  'readonly' => 'readonly',
 181                  'size' => self::MAX_INPUT_LENGTH,
 182              ]
 183          );
 184          $mform->setType('guestpassword', PARAM_RAW);
 185      }
 186  
 187      /**
 188       * Check if current user has access to this form, otherwise throw exception.
 189       *
 190       * @return void
 191       * @throws moodle_exception
 192       */
 193      protected function check_access_for_dynamic_submission(): void {
 194          $context = $this->get_context_for_dynamic_submission();
 195          $instance = instance::get_from_cmid($context->instanceid);
 196          if (!$instance->is_moderator()) {
 197              throw new \restricted_context_exception();
 198          }
 199      }
 200  
 201      /**
 202       * Return form context
 203       *
 204       * @return context
 205       */
 206      protected function get_context_for_dynamic_submission(): context {
 207          $instance = $this->get_instance_from_params();
 208          return $instance->get_context();
 209      }
 210  
 211      /**
 212       * Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX.
 213       *
 214       * @return moodle_url
 215       */
 216      protected function get_page_url_for_dynamic_submission(): moodle_url {
 217          $context = $this->get_context_for_dynamic_submission();
 218          return new moodle_url('/mod/bigbluebuttonbn/view.php', ['id' => $context->instanceid]);
 219      }
 220  }