Differences Between: [Versions 310 and 311] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]
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 * Customfield package 19 * 20 * @package core_customfield 21 * @copyright 2018 David Matamoros <davidmc@moodle.com> 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 namespace core_customfield; 26 27 defined('MOODLE_INTERNAL') || die; 28 29 /** 30 * Class field_config_form 31 * 32 * @package core_customfield 33 * @copyright 2018 David Matamoros <davidmc@moodle.com> 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class field_config_form extends \core_form\dynamic_form { 37 38 /** @var field_controller */ 39 protected $field; 40 41 /** 42 * Class definition 43 * 44 * @throws \coding_exception 45 */ 46 public function definition() { 47 $mform = $this->_form; 48 49 $field = $this->get_field(); 50 $handler = $field->get_handler(); 51 52 $mform->addElement('header', '_commonsettings', get_string('commonsettings', 'core_customfield')); 53 54 $mform->addElement('text', 'name', get_string('fieldname', 'core_customfield'), 'size="50"'); 55 $mform->addRule('name', null, 'required', null, 'client'); 56 $mform->setType('name', PARAM_TEXT); 57 58 // Accepted values for 'shortname' would follow [a-z0-9_] pattern, 59 // but we are accepting any PARAM_TEXT value here, 60 // and checking [a-zA-Z0-9_] pattern in validation() function to throw an error when needed. 61 $mform->addElement('text', 'shortname', get_string('fieldshortname', 'core_customfield'), 'size=20'); 62 $mform->addHelpButton('shortname', 'shortname', 'core_customfield'); 63 $mform->addRule('shortname', null, 'required', null, 'client'); 64 $mform->setType('shortname', PARAM_TEXT); 65 66 $desceditoroptions = $handler->get_description_text_options(); 67 $mform->addElement('editor', 'description_editor', get_string('description', 'core_customfield'), null, $desceditoroptions); 68 $mform->addHelpButton('description_editor', 'description', 'core_customfield'); 69 70 // If field is required. 71 $mform->addElement('selectyesno', 'configdata[required]', get_string('isfieldrequired', 'core_customfield')); 72 $mform->addHelpButton('configdata[required]', 'isfieldrequired', 'core_customfield'); 73 $mform->setType('configdata[required]', PARAM_BOOL); 74 75 // If field data is unique. 76 $mform->addElement('selectyesno', 'configdata[uniquevalues]', get_string('isdataunique', 'core_customfield')); 77 $mform->addHelpButton('configdata[uniquevalues]', 'isdataunique', 'core_customfield'); 78 $mform->setType('configdata[uniquevalues]', PARAM_BOOL); 79 80 // Field specific settings from field type. 81 $field->config_form_definition($mform); 82 83 // Handler/component settings. 84 $handler->config_form_definition($mform); 85 86 // We add hidden fields. 87 $mform->addElement('hidden', 'categoryid'); 88 $mform->setType('categoryid', PARAM_INT); 89 90 $mform->addElement('hidden', 'type'); 91 $mform->setType('type', PARAM_COMPONENT); 92 93 $mform->addElement('hidden', 'id'); 94 $mform->setType('id', PARAM_INT); 95 96 // This form is only used inside modal dialogues and never needs action buttons. 97 } 98 99 /** 100 * Field data validation 101 * 102 * @param array $data 103 * @param array $files 104 * @return array 105 */ 106 public function validation($data, $files = array()) { 107 global $DB; 108 109 $errors = array(); 110 $field = $this->get_field(); 111 $handler = $field->get_handler(); 112 113 // Check the shortname is specified and is unique for this component-area-itemid combination. 114 if (!preg_match('/^[a-z0-9_]+$/', $data['shortname'])) { 115 // Check allowed pattern (numbers, letters and underscore). 116 $errors['shortname'] = get_string('invalidshortnameerror', 'core_customfield'); 117 } else if ($DB->record_exists_sql('SELECT 1 FROM {customfield_field} f ' . 118 'JOIN {customfield_category} c ON c.id = f.categoryid ' . 119 'WHERE f.shortname = ? AND f.id <> ? AND c.component = ? AND c.area = ? AND c.itemid = ?', 120 [$data['shortname'], $data['id'], 121 $handler->get_component(), $handler->get_area(), $handler->get_itemid()])) { 122 $errors['shortname'] = get_string('formfieldcheckshortname', 'core_customfield'); 123 } 124 125 $errors = array_merge($errors, $field->config_form_validation($data, $files)); 126 127 return $errors; 128 } 129 130 /** 131 * Get field 132 * 133 * @return field_controller 134 * @throws \moodle_exception 135 */ 136 protected function get_field(): field_controller { 137 if ($this->field === null) { 138 if (!empty($this->_ajaxformdata['id'])) { 139 $this->field = \core_customfield\field_controller::create((int)$this->_ajaxformdata['id']); 140 } else if (!empty($this->_ajaxformdata['categoryid']) && !empty($this->_ajaxformdata['type'])) { 141 $category = \core_customfield\category_controller::create((int)$this->_ajaxformdata['categoryid']); 142 $type = clean_param($this->_ajaxformdata['type'], PARAM_PLUGIN); 143 $this->field = \core_customfield\field_controller::create(0, (object)['type' => $type], $category); 144 } else { 145 throw new \moodle_exception('fieldnotfound', 'core_customfield'); 146 } 147 } 148 return $this->field; 149 } 150 151 /** 152 * Check if current user has access to this form, otherwise throw exception 153 * 154 * Sometimes permission check may depend on the action and/or id of the entity. 155 * If necessary, form data is available in $this->_ajaxformdata 156 */ 157 protected function check_access_for_dynamic_submission(): void { 158 $field = $this->get_field(); 159 $handler = $field->get_handler(); 160 if (!$handler->can_configure()) { 161 print_error('nopermissionconfigure', 'core_customfield'); 162 } 163 } 164 165 /** 166 * Load in existing data as form defaults 167 * 168 * Can be overridden to retrieve existing values from db by entity id and also 169 * to preprocess editor and filemanager elements 170 * 171 * Example: 172 * $this->set_data(get_entity($this->_ajaxformdata['id'])); 173 */ 174 public function set_data_for_dynamic_submission(): void { 175 $this->set_data(api::prepare_field_for_config_form($this->get_field())); 176 } 177 178 /** 179 * Process the form submission 180 * 181 * This method can return scalar values or arrays that can be json-encoded, they will be passed to the caller JS. 182 * 183 * @return mixed 184 */ 185 public function process_dynamic_submission() { 186 $data = $this->get_data(); 187 $field = $this->get_field(); 188 $handler = $field->get_handler(); 189 $handler->save_field_configuration($field, $data); 190 return null; 191 } 192 193 /** 194 * Form context 195 * @return \context 196 */ 197 protected function get_context_for_dynamic_submission(): \context { 198 return $this->get_field()->get_handler()->get_configuration_context(); 199 } 200 201 /** 202 * Page url 203 * @return \moodle_url 204 */ 205 protected function get_page_url_for_dynamic_submission(): \moodle_url { 206 $field = $this->get_field(); 207 if ($field->get('id')) { 208 $params = ['action' => 'editfield', 'id' => $field->get('id')]; 209 } else { 210 $params = ['action' => 'addfield', 'categoryid' => $field->get('categoryid'), 'type' => $field->get('type')]; 211 } 212 return new \moodle_url($field->get_handler()->get_configuration_url(), $params); 213 } 214 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body