Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 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   * Class site_registration_form
  19   *
  20   * @package    core
  21   * @copyright  2017 Marina Glancy
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core\hub;
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  use context_course;
  29  use stdClass;
  30  
  31  global $CFG;
  32  require_once($CFG->libdir . '/formslib.php');
  33  
  34  /**
  35   * The site registration form. Information will be sent to the sites directory.
  36   *
  37   * @author     Jerome Mouneyrac <jerome@mouneyrac.com>
  38   * @package    core
  39   * @copyright  2017 Marina Glancy
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class site_registration_form extends \moodleform {
  43  
  44      /**
  45       * Form definition
  46       */
  47      public function definition() {
  48          global $CFG;
  49  
  50          $strrequired = get_string('required');
  51          $mform = & $this->_form;
  52          $admin = get_admin();
  53          $site = get_site();
  54  
  55          $siteinfo = registration::get_site_info([
  56              'name' => format_string($site->fullname, true, array('context' => context_course::instance(SITEID))),
  57              'description' => $site->summary,
  58              'contactname' => fullname($admin, true),
  59              'contactemail' => $admin->email,
  60              'contactphone' => $admin->phone1,
  61              'street' => '',
  62              'countrycode' => $admin->country ?: $CFG->country,
  63              'regioncode' => '-', // Not supported yet.
  64              'language' => explode('_', current_language())[0],
  65              'geolocation' => '',
  66              'emailalert' => 0,
  67              'commnews' => 0,
  68              'policyagreed' => 0
  69  
  70          ]);
  71  
  72          // Fields that need to be highlighted.
  73          $highlightfields = registration::get_new_registration_fields();
  74  
  75          $mform->addElement('header', 'moodle', get_string('registrationinfo', 'hub'));
  76  
  77          $mform->addElement('text', 'name', get_string('sitename', 'hub'),
  78              array('class' => 'registration_textfield', 'maxlength' => 255));
  79          $mform->setType('name', PARAM_TEXT);
  80          $mform->addHelpButton('name', 'sitename', 'hub');
  81  
  82          $mform->addElement('select', 'privacy', get_string('siteprivacy', 'hub'), registration::site_privacy_options());
  83          $mform->setType('privacy', PARAM_ALPHA);
  84          $mform->addHelpButton('privacy', 'siteprivacy', 'hub');
  85          unset($options);
  86  
  87          $mform->addElement('textarea', 'description', get_string('sitedesc', 'hub'),
  88              array('rows' => 3, 'cols' => 41));
  89          $mform->setType('description', PARAM_TEXT);
  90          $mform->addHelpButton('description', 'sitedesc', 'hub');
  91  
  92          $languages = get_string_manager()->get_list_of_languages();
  93          \core_collator::asort($languages);
  94          $mform->addElement('select', 'language', get_string('sitelang', 'hub'), $languages);
  95          $mform->setType('language', PARAM_ALPHANUMEXT);
  96          $mform->addHelpButton('language', 'sitelang', 'hub');
  97  
  98          // Postal address was part of this form before but not any more.
  99          $mform->addElement('hidden', 'street');
 100          $mform->setType('street', PARAM_TEXT);
 101          $mform->addHelpButton('street', 'postaladdress', 'hub');
 102  
 103          $mform->addElement('hidden', 'regioncode', '-');
 104          $mform->setType('regioncode', PARAM_ALPHANUMEXT);
 105  
 106          $countries = ['' => ''] + get_string_manager()->get_list_of_countries();
 107          $mform->addElement('select', 'countrycode', get_string('sitecountry', 'hub'), $countries);
 108          $mform->setType('countrycode', PARAM_ALPHANUMEXT);
 109          $mform->addHelpButton('countrycode', 'sitecountry', 'hub');
 110          $mform->addRule('countrycode', $strrequired, 'required', null, 'client');
 111  
 112          // Geolocation was part of this form before but not any more.
 113          $mform->addElement('hidden', 'geolocation');
 114          $mform->setType('geolocation', PARAM_RAW);
 115          $mform->addHelpButton('geolocation', 'sitegeolocation', 'hub');
 116  
 117          // Admin name was part of this form before but not any more.
 118          $mform->addElement('hidden', 'contactname');
 119          $mform->setType('contactname', PARAM_TEXT);
 120          $mform->addHelpButton('contactname', 'siteadmin', 'hub');
 121  
 122          $mform->addElement('hidden', 'contactphone');
 123          $mform->setType('contactphone', PARAM_TEXT);
 124          $mform->addHelpButton('contactphone', 'sitephone', 'hub');
 125  
 126          $mform->addElement('text', 'contactemail', get_string('siteemail', 'hub'),
 127              array('class' => 'registration_textfield'));
 128          $mform->addRule('contactemail', $strrequired, 'required', null, 'client');
 129          $mform->setType('contactemail', PARAM_EMAIL);
 130          $mform->addHelpButton('contactemail', 'siteemail', 'hub');
 131  
 132          $options = array();
 133          $options[0] = get_string("registrationcontactno");
 134          $options[1] = get_string("registrationcontactyes");
 135          $mform->addElement('select', 'contactable', get_string('siteregistrationcontact', 'hub'), $options);
 136          $mform->setType('contactable', PARAM_INT);
 137          $mform->addHelpButton('contactable', 'siteregistrationcontact', 'hub');
 138          $mform->hideIf('contactable', 'privacy', 'eq', registration::HUB_SITENOTPUBLISHED);
 139          unset($options);
 140  
 141          $this->add_checkbox_with_email('emailalert', 'siteregistrationemail', false, get_string('registrationyes'));
 142  
 143          $this->add_checkbox_with_email(
 144              'commnews',
 145              'sitecommnews',
 146              in_array('commnews', $highlightfields),
 147              get_string('sitecommnewsyes', 'hub')
 148          );
 149  
 150          // TODO site logo.
 151          $mform->addElement('hidden', 'imageurl', ''); // TODO: temporary.
 152          $mform->setType('imageurl', PARAM_URL);
 153  
 154          $mform->addElement('checkbox', 'policyagreed', get_string('policyagreed', 'hub'),
 155              get_string('policyagreeddesc', 'hub', HUB_MOODLEORGHUBURL . '/privacy'));
 156          $mform->addRule('policyagreed', $strrequired, 'required', null, 'client');
 157  
 158          $mform->addElement('header', 'sitestats', get_string('sendfollowinginfo', 'hub'));
 159          $mform->setExpanded('sitestats', !empty($highlightfields));
 160          $mform->addElement('static', 'urlstring', get_string('siteurl', 'hub'), $siteinfo['url']);
 161          $mform->addHelpButton('urlstring', 'siteurl', 'hub');
 162  
 163          // Display statistic that are going to be retrieve by the sites directory.
 164          $mform->addElement('static', 'siteinfosummary', get_string('sendfollowinginfo', 'hub'), registration::get_stats_summary($siteinfo));
 165  
 166          // Check if it's a first registration or update.
 167          if (registration::is_registered()) {
 168              $buttonlabel = get_string('updatesiteregistration', 'core_hub');
 169              $mform->addElement('hidden', 'update', true);
 170              $mform->setType('update', PARAM_BOOL);
 171          } else {
 172              $buttonlabel = get_string('register', 'core_admin');
 173          }
 174  
 175          $this->add_action_buttons(false, $buttonlabel);
 176  
 177          $mform->addElement('hidden', 'returnurl');
 178          $mform->setType('returnurl', PARAM_LOCALURL);
 179  
 180          // Prepare and set data.
 181          $siteinfo['emailalertnewemail'] = !empty($siteinfo['emailalert']) && !empty($siteinfo['emailalertemail']);
 182          if (empty($siteinfo['emailalertnewemail'])) {
 183              $siteinfo['emailalertemail'] = '';
 184          }
 185          $siteinfo['commnewsnewemail'] = !empty($siteinfo['commnews']) && !empty($siteinfo['commnewsemail']);
 186          if (empty($siteinfo['commnewsnewemail'])) {
 187              $siteinfo['commnewsemail'] = '';
 188          }
 189  
 190          // Set data. Always require to check policyagreed even if it was checked earlier.
 191          $this->set_data(['policyagreed' => 0] + $siteinfo);
 192      }
 193  
 194      /**
 195       * Add yes/no select with additional checkbox allowing to specify another email
 196       *
 197       * @param string $elementname
 198       * @param string $stridentifier
 199       * @param array|null $options options for the select element
 200       * @param bool $highlight highlight as a new field
 201       */
 202      protected function add_select_with_email($elementname, $stridentifier, $options = null, $highlight = false) {
 203          $mform = $this->_form;
 204  
 205          if ($options === null) {
 206              $options = [0 => get_string('no'), 1 => get_string('yes')];
 207          }
 208  
 209          $group = [
 210              $mform->createElement('select', $elementname, get_string($stridentifier, 'hub'), $options),
 211              $mform->createElement('static', $elementname . 'sep', '', '<br/>'),
 212              $mform->createElement('advcheckbox', $elementname . 'newemail', '', get_string('usedifferentemail', 'hub'),
 213                  ['onchange' => "this.form.elements['{$elementname}email'].focus();"]),
 214              $mform->createElement('text', $elementname . 'email', get_string('email'))
 215          ];
 216  
 217          $element = $mform->addElement('group', $elementname . 'group', get_string($stridentifier, 'hub'), $group, '', false);
 218          if ($highlight) {
 219              $element->setAttributes(['class' => $element->getAttribute('class') . ' needsconfirmation mark']);
 220          }
 221          $mform->hideIf($elementname . 'email', $elementname, 'eq', 0);
 222          $mform->hideIf($elementname . 'newemail', $elementname, 'eq', 0);
 223          $mform->hideIf($elementname . 'email', $elementname . 'newemail', 'notchecked');
 224          $mform->setType($elementname, PARAM_INT);
 225          $mform->setType($elementname . 'email', PARAM_RAW_TRIMMED); // E-mail will be validated in validation().
 226          $mform->addHelpButton($elementname . 'group', $stridentifier, 'hub');
 227  
 228      }
 229  
 230      /**
 231       * Add yes/no checkbox with additional checkbox allowing to specify another email
 232       *
 233       * @param string $elementname
 234       * @param string $stridentifier
 235       * @param bool $highlight highlight as a new field
 236       * @param string $checkboxtext The text to show after the text.
 237       */
 238      protected function add_checkbox_with_email($elementname, $stridentifier, $highlight = false, string $checkboxtext = '') {
 239          $mform = $this->_form;
 240  
 241          $group = [
 242              $mform->createElement('advcheckbox', $elementname, '', $checkboxtext, ['class' => 'pt-2']),
 243              $mform->createElement('static', $elementname . 'sep', '', '<br/>'),
 244              $mform->createElement('advcheckbox', $elementname . 'newemail', '', get_string('usedifferentemail', 'hub'),
 245                  ['onchange' => "this.form.elements['{$elementname}email'].focus();"]),
 246              $mform->createElement('text', $elementname . 'email', get_string('email'))
 247          ];
 248  
 249          $element = $mform->addElement('group', $elementname . 'group', get_string($stridentifier, 'hub'), $group, '', false);
 250          if ($highlight) {
 251              $element->setAttributes(['class' => $element->getAttribute('class') . ' needsconfirmation mark']);
 252          }
 253          $mform->hideif($elementname . 'email', $elementname, 'eq', 0);
 254          $mform->hideif($elementname . 'newemail', $elementname, 'eq', 0);
 255          $mform->hideif($elementname . 'email', $elementname . 'newemail', 'notchecked');
 256          $mform->setType($elementname, PARAM_INT);
 257          $mform->setType($elementname . 'email', PARAM_RAW_TRIMMED); // E-mail will be validated in validation().
 258          $mform->addHelpButton($elementname . 'group', $stridentifier, 'hub');
 259  
 260      }
 261  
 262      /**
 263       * Validation of the form data
 264       *
 265       * @param array $data array of ("fieldname"=>value) of submitted data
 266       * @param array $files array of uploaded files "element_name"=>tmp_file_path
 267       * @return array of "element_name"=>"error_description" if there are errors,
 268       *         or an empty array if everything is OK
 269       */
 270      public function validation($data, $files) {
 271          $errors = parent::validation($data, $files);
 272          // Validate optional emails. We do not use PARAM_EMAIL because it blindly clears the field if it is not a valid email.
 273          if (!empty($data['emailalert']) && !empty($data['emailalertnewemail']) && !validate_email($data['emailalertemail'])) {
 274              $errors['emailalertgroup'] = get_string('invalidemail');
 275          }
 276          if (!empty($data['commnews']) && !empty($data['commnewsnewemail']) && !validate_email($data['commnewsemail'])) {
 277              $errors['commnewsgroup'] = get_string('invalidemail');
 278          }
 279          return $errors;
 280      }
 281  
 282      /**
 283       * Returns the form data
 284       *
 285       * @return stdClass
 286       */
 287      public function get_data() {
 288          if ($data = parent::get_data()) {
 289              // Never return '*newemail' checkboxes, always return 'emailalertemail' and 'commnewsemail' even if not applicable.
 290              if (empty($data->emailalert) || empty($data->emailalertnewemail)) {
 291                  $data->emailalertemail = null;
 292              }
 293              unset($data->emailalertnewemail);
 294              if (empty($data->commnews) || empty($data->commnewsnewemail)) {
 295                  $data->commnewsemail = null;
 296              }
 297              unset($data->commnewsnewemail);
 298              // Always return 'contactable'.
 299              $data->contactable = empty($data->contactable) ? 0 : 1;
 300  
 301              if (debugging('', DEBUG_DEVELOPER)) {
 302                  // Display debugging message for developers who added fields to the form and forgot to add them to registration::FORM_FIELDS.
 303                  $keys = array_diff(array_keys((array)$data),
 304                      ['returnurl', 'mform_isexpanded_id_sitestats', 'submitbutton', 'update']);
 305                  if ($extrafields = array_diff($keys, registration::FORM_FIELDS)) {
 306                      debugging('Found extra fields in the form results: ' . join(', ', $extrafields), DEBUG_DEVELOPER);
 307                  }
 308                  if ($missingfields = array_diff(registration::FORM_FIELDS, $keys)) {
 309                      debugging('Some fields are missing in the form results: ' . join(', ', $missingfields), DEBUG_DEVELOPER);
 310                  }
 311              }
 312          }
 313          return $data;
 314      }
 315  
 316  }
 317