Differences Between: [Versions 310 and 403] [Versions 39 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 * @deprecated since Moodle 3.11 - MDL-71460 The form elements using this have been converted to checkboxes 198 * @todo MDL-71472 - Will be deleted in 4.3 199 * @see \core\hub\site_registration_form::add_checkbox_with_email() 200 * @param string $elementname 201 * @param string $stridentifier 202 * @param array|null $options options for the select element 203 * @param bool $highlight highlight as a new field 204 */ 205 protected function add_select_with_email($elementname, $stridentifier, $options = null, $highlight = false) { 206 debugging('add_select_with_email() is deprecated. Please use add_checkbox_with_email() instead.', DEBUG_DEVELOPER); 207 208 $mform = $this->_form; 209 210 if ($options === null) { 211 $options = [0 => get_string('no'), 1 => get_string('yes')]; 212 } 213 214 $group = [ 215 $mform->createElement('select', $elementname, get_string($stridentifier, 'hub'), $options), 216 $mform->createElement('static', $elementname . 'sep', '', '<br/>'), 217 $mform->createElement('advcheckbox', $elementname . 'newemail', '', get_string('usedifferentemail', 'hub'), 218 ['onchange' => "this.form.elements['{$elementname}email'].focus();"]), 219 $mform->createElement('text', $elementname . 'email', get_string('email')) 220 ]; 221 222 $element = $mform->addElement('group', $elementname . 'group', get_string($stridentifier, 'hub'), $group, '', false); 223 if ($highlight) { 224 $element->setAttributes(['class' => $element->getAttribute('class') . ' needsconfirmation mark']); 225 } 226 $mform->hideIf($elementname . 'email', $elementname, 'eq', 0); 227 $mform->hideIf($elementname . 'newemail', $elementname, 'eq', 0); 228 $mform->hideIf($elementname . 'email', $elementname . 'newemail', 'notchecked'); 229 $mform->setType($elementname, PARAM_INT); 230 $mform->setType($elementname . 'email', PARAM_RAW_TRIMMED); // E-mail will be validated in validation(). 231 $mform->addHelpButton($elementname . 'group', $stridentifier, 'hub'); 232 233 } 234 235 /** 236 * Add yes/no checkbox with additional checkbox allowing to specify another email 237 * 238 * @param string $elementname 239 * @param string $stridentifier 240 * @param bool $highlight highlight as a new field 241 * @param string $checkboxtext The text to show after the text. 242 */ 243 protected function add_checkbox_with_email($elementname, $stridentifier, $highlight = false, string $checkboxtext = '') { 244 $mform = $this->_form; 245 246 $group = [ 247 $mform->createElement('advcheckbox', $elementname, '', $checkboxtext, ['class' => 'pt-2']), 248 $mform->createElement('static', $elementname . 'sep', '', '<br/>'), 249 $mform->createElement('advcheckbox', $elementname . 'newemail', '', get_string('usedifferentemail', 'hub'), 250 ['onchange' => "this.form.elements['{$elementname}email'].focus();"]), 251 $mform->createElement('text', $elementname . 'email', get_string('email')) 252 ]; 253 254 $element = $mform->addElement('group', $elementname . 'group', get_string($stridentifier, 'hub'), $group, '', false); 255 if ($highlight) { 256 $element->setAttributes(['class' => $element->getAttribute('class') . ' needsconfirmation mark']); 257 } 258 $mform->hideif($elementname . 'email', $elementname, 'eq', 0); 259 $mform->hideif($elementname . 'newemail', $elementname, 'eq', 0); 260 $mform->hideif($elementname . 'email', $elementname . 'newemail', 'notchecked'); 261 $mform->setType($elementname, PARAM_INT); 262 $mform->setType($elementname . 'email', PARAM_RAW_TRIMMED); // E-mail will be validated in validation(). 263 $mform->addHelpButton($elementname . 'group', $stridentifier, 'hub'); 264 265 } 266 267 /** 268 * Validation of the form data 269 * 270 * @param array $data array of ("fieldname"=>value) of submitted data 271 * @param array $files array of uploaded files "element_name"=>tmp_file_path 272 * @return array of "element_name"=>"error_description" if there are errors, 273 * or an empty array if everything is OK 274 */ 275 public function validation($data, $files) { 276 $errors = parent::validation($data, $files); 277 // Validate optional emails. We do not use PARAM_EMAIL because it blindly clears the field if it is not a valid email. 278 if (!empty($data['emailalert']) && !empty($data['emailalertnewemail']) && !validate_email($data['emailalertemail'])) { 279 $errors['emailalertgroup'] = get_string('invalidemail'); 280 } 281 if (!empty($data['commnews']) && !empty($data['commnewsnewemail']) && !validate_email($data['commnewsemail'])) { 282 $errors['commnewsgroup'] = get_string('invalidemail'); 283 } 284 return $errors; 285 } 286 287 /** 288 * Returns the form data 289 * 290 * @return stdClass 291 */ 292 public function get_data() { 293 if ($data = parent::get_data()) { 294 // Never return '*newemail' checkboxes, always return 'emailalertemail' and 'commnewsemail' even if not applicable. 295 if (empty($data->emailalert) || empty($data->emailalertnewemail)) { 296 $data->emailalertemail = null; 297 } 298 unset($data->emailalertnewemail); 299 if (empty($data->commnews) || empty($data->commnewsnewemail)) { 300 $data->commnewsemail = null; 301 } 302 unset($data->commnewsnewemail); 303 // Always return 'contactable'. 304 $data->contactable = empty($data->contactable) ? 0 : 1; 305 306 if (debugging('', DEBUG_DEVELOPER)) { 307 // Display debugging message for developers who added fields to the form and forgot to add them to registration::FORM_FIELDS. 308 $keys = array_diff(array_keys((array)$data), 309 ['returnurl', 'mform_isexpanded_id_sitestats', 'submitbutton', 'update']); 310 if ($extrafields = array_diff($keys, registration::FORM_FIELDS)) { 311 debugging('Found extra fields in the form results: ' . join(', ', $extrafields), DEBUG_DEVELOPER); 312 } 313 if ($missingfields = array_diff(registration::FORM_FIELDS, $keys)) { 314 debugging('Some fields are missing in the form results: ' . join(', ', $missingfields), DEBUG_DEVELOPER); 315 } 316 } 317 } 318 return $data; 319 } 320 321 } 322
title
Description
Body
title
Description
Body
title
Description
Body
title
Body