2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 namespace core_user\form
;
20 use core_form\dynamic_form
;
22 use profile_define_base
;
25 * Class field_form used for profile fields.
28 * @copyright 2007 onwards Shane Elliot {@link http://pukunui.com}
29 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 class profile_field_form
extends dynamic_form
{
33 /** @var profile_define_base $field */
36 protected $fieldrecord;
41 public function definition() {
43 require_once($CFG->dirroot
.'/user/profile/definelib.php');
45 $mform = $this->_form
;
47 // Everything else is dependant on the data type.
48 $datatype = $this->get_field_record()->datatype
;
49 require_once($CFG->dirroot
.'/user/profile/field/'.$datatype.'/define.class.php');
50 $newfield = 'profile_define_'.$datatype;
51 $this->field
= new $newfield();
53 // Add some extra hidden fields.
54 $mform->addElement('hidden', 'id');
55 $mform->setType('id', PARAM_INT
);
56 $mform->addElement('hidden', 'action', 'editfield');
57 $mform->setType('action', PARAM_ALPHANUMEXT
);
58 $mform->addElement('hidden', 'datatype', $datatype);
59 $mform->setType('datatype', PARAM_ALPHA
);
61 $this->field
->define_form($mform);
66 * Alter definition based on existing or submitted data
68 public function definition_after_data() {
69 $mform = $this->_form
;
70 $this->field
->define_after_data($mform);
75 * Perform some moodle validation.
80 public function validation($data, $files) {
81 return $this->field
->define_validate($data, $files);
85 * Returns the defined editors for the field.
88 public function editors(): array {
89 $editors = $this->field
->define_editors();
90 return is_array($editors) ?
$editors : [];
94 * Returns context where this form is used
98 protected function get_context_for_dynamic_submission(): context
{
99 return \context_system
::instance();
103 * Checks if current user has access to this form, otherwise throws exception
105 protected function check_access_for_dynamic_submission(): void
{
106 require_capability('moodle/site:config', $this->get_context_for_dynamic_submission());
110 * Process the form submission, used if form was submitted via AJAX
112 public function process_dynamic_submission() {
114 require_once($CFG->dirroot
.'/user/profile/definelib.php');
115 profile_save_field($this->get_data(), $this->editors());
119 * Load in existing data as form defaults
121 public function set_data_for_dynamic_submission(): void
{
122 $field = $this->get_field_record();
124 // Clean and prepare description for the editor.
125 $description = clean_text($field->description
, $field->descriptionformat
);
126 $field->description
= ['text' => $description, 'format' => $field->descriptionformat
, 'itemid' => 0];
127 // Convert the data format for.
128 if (is_array($this->editors())) {
129 foreach ($this->editors() as $editor) {
130 if (isset($field->$editor)) {
131 $editordesc = clean_text($field->$editor, $field->{$editor.'format'});
132 $field->$editor = ['text' => $editordesc, 'format' => $field->{$editor.'format'}, 'itemid' => 0];
137 $this->set_data($field);
141 * Returns url to set in $PAGE->set_url() when form is being rendered or submitted via AJAX
145 protected function get_page_url_for_dynamic_submission(): moodle_url
{
146 $id = $this->optional_param('id', 0, PARAM_INT
);
147 $datatype = $this->optional_param('datatype', 'text', PARAM_PLUGIN
);
148 return new moodle_url('/user/profile/index.php',
149 ['action' => 'editfield', 'id' => $id, 'datatype' => $id ?
null : $datatype]);
153 * Record for the field from the database (or generic record for a new field)
155 * @return false|mixed|\stdClass
156 * @throws \coding_exception
157 * @throws \dml_exception
159 public function get_field_record() {
162 if (!$this->fieldrecord
) {
163 $id = $this->optional_param('id', 0, PARAM_INT
);
164 if (!$id ||
!($this->fieldrecord
= $DB->get_record('user_info_field', ['id' => $id]))) {
165 $datatype = $this->optional_param('datatype', 'text', PARAM_PLUGIN
);
166 $this->fieldrecord
= new \
stdClass();
167 $this->fieldrecord
->datatype
= $datatype;
168 $this->fieldrecord
->description
= '';
169 $this->fieldrecord
->descriptionformat
= FORMAT_HTML
;
170 $this->fieldrecord
->defaultdata
= '';
171 $this->fieldrecord
->defaultdataformat
= FORMAT_HTML
;
172 $this->fieldrecord
->categoryid
= $this->optional_param('categoryid', 0, PARAM_INT
);
174 if (!\core_component
::get_component_directory('profilefield_'.$this->fieldrecord
->datatype
)) {
175 throw new \
moodle_exception('fieldnotfound', 'customfield');
179 return $this->fieldrecord
;