MDL-46647 core_grades: Adding tests for fetch_all_helper()
[moodle.git] / user / profile / definelib.php
blobb60d5add2d27e61aae59c01cf59826795498828f
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();
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 if (!$categories = $DB->get_records_menu('user_info_category', null, 'sortorder ASC', 'id, name')) {
441 $categories = array();
443 return $categories;
448 * Edit a category
450 * @param int $id
451 * @param string $redirect
453 function profile_edit_category($id, $redirect) {
454 global $DB, $OUTPUT, $CFG;
456 require_once($CFG->dirroot.'/user/profile/index_category_form.php');
457 $categoryform = new category_form();
459 if ($category = $DB->get_record('user_info_category', array('id' => $id))) {
460 $categoryform->set_data($category);
463 if ($categoryform->is_cancelled()) {
464 redirect($redirect);
465 } else {
466 if ($data = $categoryform->get_data()) {
467 if (empty($data->id)) {
468 unset($data->id);
469 $data->sortorder = $DB->count_records('user_info_category') + 1;
470 $DB->insert_record('user_info_category', $data, false);
471 } else {
472 $DB->update_record('user_info_category', $data);
474 profile_reorder_categories();
475 redirect($redirect);
479 if (empty($id)) {
480 $strheading = get_string('profilecreatenewcategory', 'admin');
481 } else {
482 $strheading = get_string('profileeditcategory', 'admin', format_string($category->name));
485 // Print the page.
486 echo $OUTPUT->header();
487 echo $OUTPUT->heading($strheading);
488 $categoryform->display();
489 echo $OUTPUT->footer();
490 die;
496 * Edit a profile field.
498 * @param int $id
499 * @param string $datatype
500 * @param string $redirect
502 function profile_edit_field($id, $datatype, $redirect) {
503 global $CFG, $DB, $OUTPUT, $PAGE;
505 if (!$field = $DB->get_record('user_info_field', array('id' => $id))) {
506 $field = new stdClass();
507 $field->datatype = $datatype;
508 $field->description = '';
509 $field->descriptionformat = FORMAT_HTML;
510 $field->defaultdata = '';
511 $field->defaultdataformat = FORMAT_HTML;
514 // Clean and prepare description for the editor.
515 $field->description = clean_text($field->description, $field->descriptionformat);
516 $field->description = array('text' => $field->description, 'format' => $field->descriptionformat, 'itemid' => 0);
518 require_once($CFG->dirroot.'/user/profile/index_field_form.php');
519 $fieldform = new field_form(null, $field->datatype);
521 // Convert the data format for.
522 if (is_array($fieldform->editors())) {
523 foreach ($fieldform->editors() as $editor) {
524 if (isset($field->$editor)) {
525 $field->$editor = clean_text($field->$editor, $field->{$editor.'format'});
526 $field->$editor = array('text' => $field->$editor, 'format' => $field->{$editor.'format'}, 'itemid' => 0);
531 $fieldform->set_data($field);
533 if ($fieldform->is_cancelled()) {
534 redirect($redirect);
536 } else {
537 if ($data = $fieldform->get_data()) {
538 require_once($CFG->dirroot.'/user/profile/field/'.$datatype.'/define.class.php');
539 $newfield = 'profile_define_'.$datatype;
540 $formfield = new $newfield();
542 // Collect the description and format back into the proper data structure from the editor.
543 // Note: This field will ALWAYS be an editor.
544 $data->descriptionformat = $data->description['format'];
545 $data->description = $data->description['text'];
547 // Check whether the default data is an editor, this is (currently) only the textarea field type.
548 if (is_array($data->defaultdata) && array_key_exists('text', $data->defaultdata)) {
549 // Collect the default data and format back into the proper data structure from the editor.
550 $data->defaultdataformat = $data->defaultdata['format'];
551 $data->defaultdata = $data->defaultdata['text'];
554 // Convert the data format for.
555 if (is_array($fieldform->editors())) {
556 foreach ($fieldform->editors() as $editor) {
557 if (isset($field->$editor)) {
558 $field->{$editor.'format'} = $field->{$editor}['format'];
559 $field->$editor = $field->{$editor}['text'];
564 $formfield->define_save($data);
565 profile_reorder_fields();
566 profile_reorder_categories();
567 redirect($redirect);
570 $datatypes = profile_list_datatypes();
572 if (empty($id)) {
573 $strheading = get_string('profilecreatenewfield', 'admin', $datatypes[$datatype]);
574 } else {
575 $strheading = get_string('profileeditfield', 'admin', $field->name);
578 // Print the page.
579 $PAGE->navbar->add($strheading);
580 echo $OUTPUT->header();
581 echo $OUTPUT->heading($strheading);
582 $fieldform->display();
583 echo $OUTPUT->footer();
584 die;