Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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