MDL-47162 core_message: Remove changes to small messages
[moodle.git] / user / profile / definelib.php
bloba9a1a12c645c815a57971d8d3c523f8a382299a9
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.
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 /**
18 * This file contains the profile_define_base class.
20 * @package core_user
21 * @copyright 2007 onwards Shane Elliot {@link http://pukunui.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 /**
26 * Class profile_define_base
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_define_base {
33 /**
34 * Prints out the form snippet for creating or editing a profile field
35 * @param moodleform $form instance of the moodleform class
37 public function define_form(&$form) {
38 $form->addElement('header', '_commonsettings', get_string('profilecommonsettings', 'admin'));
39 $this->define_form_common($form);
41 $form->addElement('header', '_specificsettings', get_string('profilespecificsettings', 'admin'));
42 $this->define_form_specific($form);
45 /**
46 * Prints out the form snippet for the part of creating or editing a profile field common to all data types.
48 * @param moodleform $form instance of the moodleform class
50 public function define_form_common(&$form) {
52 $strrequired = get_string('required');
54 $form->addElement('text', 'shortname', get_string('profileshortname', 'admin'), 'maxlength="100" size="25"');
55 $form->addRule('shortname', $strrequired, 'required', null, 'client');
56 $form->setType('shortname', PARAM_ALPHANUM);
58 $form->addElement('text', 'name', get_string('profilename', 'admin'), 'size="50"');
59 $form->addRule('name', $strrequired, 'required', null, 'client');
60 $form->setType('name', PARAM_TEXT);
62 $form->addElement('editor', 'description', get_string('profiledescription', 'admin'), null, null);
64 $form->addElement('selectyesno', 'required', get_string('profilerequired', 'admin'));
66 $form->addElement('selectyesno', 'locked', get_string('profilelocked', 'admin'));
68 $form->addElement('selectyesno', 'forceunique', get_string('profileforceunique', 'admin'));
70 $form->addElement('selectyesno', 'signup', get_string('profilesignup', 'admin'));
72 $choices = array();
73 $choices[PROFILE_VISIBLE_NONE] = get_string('profilevisiblenone', 'admin');
74 $choices[PROFILE_VISIBLE_PRIVATE] = get_string('profilevisibleprivate', 'admin');
75 $choices[PROFILE_VISIBLE_ALL] = get_string('profilevisibleall', 'admin');
76 $form->addElement('select', 'visible', get_string('profilevisible', 'admin'), $choices);
77 $form->addHelpButton('visible', 'profilevisible', 'admin');
78 $form->setDefault('visible', PROFILE_VISIBLE_ALL);
80 $choices = profile_list_categories();
81 $form->addElement('select', 'categoryid', get_string('profilecategory', 'admin'), $choices);
84 /**
85 * Prints out the form snippet for the part of creating or editing a profile field specific to the current data type.
86 * @param moodleform $form instance of the moodleform class
88 public function define_form_specific($form) {
89 // Do nothing - overwrite if necessary.
92 /**
93 * Validate the data from the add/edit profile field form.
95 * Generally this method should not be overwritten by child classes.
97 * @param stdClass|array $data from the add/edit profile field form
98 * @param array $files
99 * @return array associative array of error messages
101 public function define_validate($data, $files) {
103 $data = (object)$data;
104 $err = array();
106 $err += $this->define_validate_common($data, $files);
107 $err += $this->define_validate_specific($data, $files);
109 return $err;
113 * Validate the data from the add/edit profile field form that is common to all data types.
115 * Generally this method should not be overwritten by child classes.
117 * @param stdClass|array $data from the add/edit profile field form
118 * @param array $files
119 * @return array associative array of error messages
121 public function define_validate_common($data, $files) {
122 global $DB;
124 $err = array();
126 // Check the shortname was not truncated by cleaning.
127 if (empty($data->shortname)) {
128 $err['shortname'] = get_string('required');
130 } else {
131 // Fetch field-record from DB.
132 $field = $DB->get_record('user_info_field', array('shortname' => $data->shortname));
133 // Check the shortname is unique.
134 if ($field and $field->id <> $data->id) {
135 $err['shortname'] = get_string('profileshortnamenotunique', 'admin');
137 // NOTE: since 2.0 the shortname may collide with existing fields in $USER because we load these fields into
138 // $USER->profile array instead.
141 // No further checks necessary as the form class will take care of it.
142 return $err;
146 * Validate the data from the add/edit profile field form
147 * that is specific to the current data type
148 * @param array $data
149 * @param array $files
150 * @return array associative array of error messages
152 public function define_validate_specific($data, $files) {
153 // Do nothing - overwrite if necessary.
154 return array();
158 * Alter form based on submitted or existing data
159 * @param moodleform $mform
161 public function define_after_data(&$mform) {
162 // Do nothing - overwrite if necessary.
166 * Add a new profile field or save changes to current field
167 * @param array|stdClass $data from the add/edit profile field form
169 public function define_save($data) {
170 global $DB;
172 $data = $this->define_save_preprocess($data); // Hook for child classes.
174 $old = false;
175 if (!empty($data->id)) {
176 $old = $DB->get_record('user_info_field', array('id' => (int)$data->id));
179 // Check to see if the category has changed.
180 if (!$old or $old->categoryid != $data->categoryid) {
181 $data->sortorder = $DB->count_records('user_info_field', array('categoryid' => $data->categoryid)) + 1;
184 if (empty($data->id)) {
185 unset($data->id);
186 $data->id = $DB->insert_record('user_info_field', $data);
187 } else {
188 $DB->update_record('user_info_field', $data);
193 * Preprocess data from the add/edit profile field form before it is saved.
195 * This method is a hook for the child classes to overwrite.
197 * @param array|stdClass $data from the add/edit profile field form
198 * @return array|stdClass processed data object
200 public function define_save_preprocess($data) {
201 // Do nothing - overwrite if necessary.
202 return $data;
206 * Provides a method by which we can allow the default data in profile_define_* to use an editor
208 * This should return an array of editor names (which will need to be formatted/cleaned)
210 * @return array
212 public function define_editors() {
213 return array();
220 * Reorder the profile fields within a given category starting at the field at the given startorder.
222 function profile_reorder_fields() {
223 global $DB;
225 if ($categories = $DB->get_records('user_info_category')) {
226 foreach ($categories as $category) {
227 $i = 1;
228 if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
229 foreach ($fields as $field) {
230 $f = new stdClass();
231 $f->id = $field->id;
232 $f->sortorder = $i++;
233 $DB->update_record('user_info_field', $f);
241 * Reorder the profile categoriess starting at the category at the given startorder.
243 function profile_reorder_categories() {
244 global $DB;
246 $i = 1;
247 if ($categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
248 foreach ($categories as $cat) {
249 $c = new stdClass();
250 $c->id = $cat->id;
251 $c->sortorder = $i++;
252 $DB->update_record('user_info_category', $c);
258 * Delete a profile category
259 * @param int $id of the category to be deleted
260 * @return bool success of operation
262 function profile_delete_category($id) {
263 global $DB;
265 // Retrieve the category.
266 if (!$category = $DB->get_record('user_info_category', array('id' => $id))) {
267 print_error('invalidcategoryid');
270 if (!$categories = $DB->get_records('user_info_category', null, 'sortorder ASC')) {
271 print_error('nocate', 'debug');
274 unset($categories[$category->id]);
276 if (!count($categories)) {
277 return false; // We can not delete the last category.
280 // Does the category contain any fields.
281 if ($DB->count_records('user_info_field', array('categoryid' => $category->id))) {
282 if (array_key_exists($category->sortorder - 1, $categories)) {
283 $newcategory = $categories[$category->sortorder - 1];
284 } else if (array_key_exists($category->sortorder + 1, $categories)) {
285 $newcategory = $categories[$category->sortorder + 1];
286 } else {
287 $newcategory = reset($categories); // Get first category if sortorder broken.
290 $sortorder = $DB->count_records('user_info_field', array('categoryid' => $newcategory->id)) + 1;
292 if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
293 foreach ($fields as $field) {
294 $f = new stdClass();
295 $f->id = $field->id;
296 $f->sortorder = $sortorder++;
297 $f->categoryid = $newcategory->id;
298 $DB->update_record('user_info_field', $f);
303 // Finally we get to delete the category.
304 $DB->delete_records('user_info_category', array('id' => $category->id));
305 profile_reorder_categories();
306 return true;
310 * Deletes a profile field.
311 * @param int $id
313 function profile_delete_field($id) {
314 global $DB;
316 // Remove any user data associated with this field.
317 if (!$DB->delete_records('user_info_data', array('fieldid' => $id))) {
318 print_error('cannotdeletecustomfield');
321 // Note: Any availability conditions that depend on this field will remain,
322 // but show the field as missing until manually corrected to something else.
324 // Need to rebuild course cache to update the info.
325 rebuild_course_cache(0, true);
327 // Try to remove the record from the database.
328 $DB->delete_records('user_info_field', array('id' => $id));
330 // Reorder the remaining fields in the same category.
331 profile_reorder_fields();
335 * Change the sort order of a field
337 * @param int $id of the field
338 * @param string $move direction of move
339 * @return bool success of operation
341 function profile_move_field($id, $move) {
342 global $DB;
344 // Get the field object.
345 if (!$field = $DB->get_record('user_info_field', array('id' => $id), 'id, sortorder, categoryid')) {
346 return false;
348 // Count the number of fields in this category.
349 $fieldcount = $DB->count_records('user_info_field', array('categoryid' => $field->categoryid));
351 // Calculate the new sortorder.
352 if ( ($move == 'up') and ($field->sortorder > 1)) {
353 $neworder = $field->sortorder - 1;
354 } else if (($move == 'down') and ($field->sortorder < $fieldcount)) {
355 $neworder = $field->sortorder + 1;
356 } else {
357 return false;
360 // Retrieve the field object that is currently residing in the new position.
361 $params = array('categoryid' => $field->categoryid, 'sortorder' => $neworder);
362 if ($swapfield = $DB->get_record('user_info_field', $params, 'id, sortorder')) {
364 // Swap the sortorders.
365 $swapfield->sortorder = $field->sortorder;
366 $field->sortorder = $neworder;
368 // Update the field records.
369 $DB->update_record('user_info_field', $field);
370 $DB->update_record('user_info_field', $swapfield);
373 profile_reorder_fields();
374 return true;
378 * Change the sort order of a category.
380 * @param int $id of the category
381 * @param string $move direction of move
382 * @return bool success of operation
384 function profile_move_category($id, $move) {
385 global $DB;
386 // Get the category object.
387 if (!($category = $DB->get_record('user_info_category', array('id' => $id), 'id, sortorder'))) {
388 return false;
391 // Count the number of categories.
392 $categorycount = $DB->count_records('user_info_category');
394 // Calculate the new sortorder.
395 if (($move == 'up') and ($category->sortorder > 1)) {
396 $neworder = $category->sortorder - 1;
397 } else if (($move == 'down') and ($category->sortorder < $categorycount)) {
398 $neworder = $category->sortorder + 1;
399 } else {
400 return false;
403 // Retrieve the category object that is currently residing in the new position.
404 if ($swapcategory = $DB->get_record('user_info_category', array('sortorder' => $neworder), 'id, sortorder')) {
406 // Swap the sortorders.
407 $swapcategory->sortorder = $category->sortorder;
408 $category->sortorder = $neworder;
410 // Update the category records.
411 $DB->update_record('user_info_category', $category) and $DB->update_record('user_info_category', $swapcategory);
412 return true;
415 return false;
419 * Retrieve a list of all the available data types
420 * @return array a list of the datatypes suitable to use in a select statement
422 function profile_list_datatypes() {
423 $datatypes = array();
425 $plugins = core_component::get_plugin_list('profilefield');
426 foreach ($plugins as $type => $unused) {
427 $datatypes[$type] = get_string('pluginname', 'profilefield_'.$type);
429 asort($datatypes);
431 return $datatypes;
435 * Retrieve a list of categories and ids suitable for use in a form
436 * @return array
438 function profile_list_categories() {
439 global $DB;
440 $categories = $DB->get_records_menu('user_info_category', null, 'sortorder ASC', 'id, name');
441 return array_map('format_string', $categories);
446 * Edit a category
448 * @param int $id
449 * @param string $redirect
451 function profile_edit_category($id, $redirect) {
452 global $DB, $OUTPUT, $CFG;
454 require_once($CFG->dirroot.'/user/profile/index_category_form.php');
455 $categoryform = new category_form();
457 if ($category = $DB->get_record('user_info_category', array('id' => $id))) {
458 $categoryform->set_data($category);
461 if ($categoryform->is_cancelled()) {
462 redirect($redirect);
463 } else {
464 if ($data = $categoryform->get_data()) {
465 if (empty($data->id)) {
466 unset($data->id);
467 $data->sortorder = $DB->count_records('user_info_category') + 1;
468 $DB->insert_record('user_info_category', $data, false);
469 } else {
470 $DB->update_record('user_info_category', $data);
472 profile_reorder_categories();
473 redirect($redirect);
477 if (empty($id)) {
478 $strheading = get_string('profilecreatenewcategory', 'admin');
479 } else {
480 $strheading = get_string('profileeditcategory', 'admin', format_string($category->name));
483 // Print the page.
484 echo $OUTPUT->header();
485 echo $OUTPUT->heading($strheading);
486 $categoryform->display();
487 echo $OUTPUT->footer();
488 die;
494 * Edit a profile field.
496 * @param int $id
497 * @param string $datatype
498 * @param string $redirect
500 function profile_edit_field($id, $datatype, $redirect) {
501 global $CFG, $DB, $OUTPUT, $PAGE;
503 if (!$field = $DB->get_record('user_info_field', array('id' => $id))) {
504 $field = new stdClass();
505 $field->datatype = $datatype;
506 $field->description = '';
507 $field->descriptionformat = FORMAT_HTML;
508 $field->defaultdata = '';
509 $field->defaultdataformat = FORMAT_HTML;
512 // Clean and prepare description for the editor.
513 $field->description = clean_text($field->description, $field->descriptionformat);
514 $field->description = array('text' => $field->description, 'format' => $field->descriptionformat, 'itemid' => 0);
516 require_once($CFG->dirroot.'/user/profile/index_field_form.php');
517 $fieldform = new field_form(null, $field->datatype);
519 // Convert the data format for.
520 if (is_array($fieldform->editors())) {
521 foreach ($fieldform->editors() as $editor) {
522 if (isset($field->$editor)) {
523 $field->$editor = clean_text($field->$editor, $field->{$editor.'format'});
524 $field->$editor = array('text' => $field->$editor, 'format' => $field->{$editor.'format'}, 'itemid' => 0);
529 $fieldform->set_data($field);
531 if ($fieldform->is_cancelled()) {
532 redirect($redirect);
534 } else {
535 if ($data = $fieldform->get_data()) {
536 require_once($CFG->dirroot.'/user/profile/field/'.$datatype.'/define.class.php');
537 $newfield = 'profile_define_'.$datatype;
538 $formfield = new $newfield();
540 // Collect the description and format back into the proper data structure from the editor.
541 // Note: This field will ALWAYS be an editor.
542 $data->descriptionformat = $data->description['format'];
543 $data->description = $data->description['text'];
545 // Check whether the default data is an editor, this is (currently) only the textarea field type.
546 if (is_array($data->defaultdata) && array_key_exists('text', $data->defaultdata)) {
547 // Collect the default data and format back into the proper data structure from the editor.
548 $data->defaultdataformat = $data->defaultdata['format'];
549 $data->defaultdata = $data->defaultdata['text'];
552 // Convert the data format for.
553 if (is_array($fieldform->editors())) {
554 foreach ($fieldform->editors() as $editor) {
555 if (isset($field->$editor)) {
556 $field->{$editor.'format'} = $field->{$editor}['format'];
557 $field->$editor = $field->{$editor}['text'];
562 $formfield->define_save($data);
563 profile_reorder_fields();
564 profile_reorder_categories();
565 redirect($redirect);
568 $datatypes = profile_list_datatypes();
570 if (empty($id)) {
571 $strheading = get_string('profilecreatenewfield', 'admin', $datatypes[$datatype]);
572 } else {
573 $strheading = get_string('profileeditfield', 'admin', format_string($field->name));
576 // Print the page.
577 $PAGE->navbar->add($strheading);
578 echo $OUTPUT->header();
579 echo $OUTPUT->heading($strheading);
580 $fieldform->display();
581 echo $OUTPUT->footer();
582 die;