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 core_user\form; 18 19 defined('MOODLE_INTERNAL') || die; 20 21 require_once($CFG->dirroot.'/lib/formslib.php'); 22 23 /** 24 * Contact site support form. 25 * 26 * @package core_user 27 * @copyright 2022 Simey Lameze <simey@moodle.com> 28 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 29 */ 30 class contactsitesupport_form extends \moodleform { 31 32 /** 33 * Define the contact site support form. 34 */ 35 public function definition(): void { 36 global $CFG; 37 38 $mform = $this->_form; 39 $user = $this->_customdata; 40 $strrequired = get_string('required'); 41 42 // Name. 43 $mform->addElement('text', 'name', get_string('name')); 44 $mform->addRule('name', $strrequired, 'required', null, 'client'); 45 $mform->setType('name', PARAM_TEXT); 46 47 // Email. 48 $mform->addElement('text', 'email', get_string('email')); 49 $mform->addRule('email', get_string('missingemail'), 'required', null, 'client'); 50 $mform->setType('email', PARAM_EMAIL); 51 52 // Subject. 53 $mform->addElement('text', 'subject', get_string('subject')); 54 $mform->addRule('subject', $strrequired, 'required', null, 'client'); 55 $mform->setType('subject', PARAM_TEXT); 56 57 // Message. 58 $mform->addElement('textarea', 'message', get_string('message')); 59 $mform->addRule('message', $strrequired, 'required', null, 'client'); 60 $mform->setType('message', PARAM_TEXT); 61 62 // If the user is logged in set name and email fields to the current user info. 63 if (isloggedin() && !isguestuser()) { 64 $mform->setDefault('name', fullname($user)); 65 $mform->hardFreeze('name'); 66 67 $mform->setDefault('email', $user->email); 68 $mform->hardFreeze('email'); 69 } 70 71 if (!empty($CFG->recaptchapublickey) && !empty($CFG->recaptchaprivatekey)) { 72 $mform->addElement('recaptcha', 'recaptcha_element', get_string('security_question', 'auth')); 73 $mform->addHelpButton('recaptcha_element', 'recaptcha', 'auth'); 74 $mform->closeHeaderBefore('recaptcha_element'); 75 } 76 77 $this->add_action_buttons(true, get_string('submit')); 78 } 79 80 /** 81 * Validate user supplied data on the contact site support form. 82 * 83 * @param array $data array of ("fieldname"=>value) of submitted data 84 * @param array $files array of uploaded files "element_name"=>tmp_file_path 85 * @return array of "element_name"=>"error_description" if there are errors, 86 * or an empty array if everything is OK (true allowed for backwards compatibility too). 87 */ 88 public function validation($data, $files): array { 89 $errors = parent::validation($data, $files); 90 if (!validate_email($data['email'])) { 91 $errors['email'] = get_string('invalidemail'); 92 } 93 if ($this->_form->elementExists('recaptcha_element')) { 94 $recaptchaelement = $this->_form->getElement('recaptcha_element'); 95 96 if (!empty($this->_form->_submitValues['g-recaptcha-response'])) { 97 $response = $this->_form->_submitValues['g-recaptcha-response']; 98 if (!$recaptchaelement->verify($response)) { 99 $errors['recaptcha_element'] = get_string('incorrectpleasetryagain', 'auth'); 100 } 101 } else { 102 $errors['recaptcha_element'] = get_string('missingrecaptchachallengefield'); 103 } 104 } 105 106 return $errors; 107 } 108 109 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body