MDL-14679 towards /admin conversion
[moodle.git] / user / profile / definelib.php
CommitLineData
bb6d3d34 1<?php //$Id$
2
3class profile_define_base {
4
5 /**
6 * Prints out the form snippet for creating or editing a profile field
7 * @param object instance of the moodleform class
8 */
9 function define_form(&$form) {
10 $form->addElement('header', '_commonsettings', get_string('profilecommonsettings', 'admin'));
11 $this->define_form_common($form);
12
13 $form->addElement('header', '_specificsettings', get_string('profilespecificsettings', 'admin'));
14 $this->define_form_specific($form);
15 }
16
17 /**
18 * Prints out the form snippet for the part of creating or
19 * editing a profile field common to all data types
20 * @param object instance of the moodleform class
21 */
22 function define_form_common(&$form) {
23
24 $strrequired = get_string('required');
25
1bf65e58 26 $form->addElement('text', 'shortname', get_string('profileshortname', 'admin'), 'maxlength="100" size="25"');
bb6d3d34 27 $form->addRule('shortname', $strrequired, 'required', null, 'client');
28 $form->setType('shortname', PARAM_ALPHANUM);
29
1bf65e58 30 $form->addElement('text', 'name', get_string('profilename', 'admin'), 'size="50"');
bb6d3d34 31 $form->addRule('name', $strrequired, 'required', null, 'client');
32 $form->setType('name', PARAM_MULTILANG);
33
34 $form->addElement('htmleditor', 'description', get_string('profiledescription', 'admin'));
35 $form->setHelpButton('description', array('text', get_string('helptext')));
bb6d3d34 36
37 $form->addElement('selectyesno', 'required', get_string('profilerequired', 'admin'));
38
39 $form->addElement('selectyesno', 'locked', get_string('profilelocked', 'admin'));
40
62ebb000 41 $form->addElement('selectyesno', 'forceunique', get_string('profileforceunique', 'admin'));
42
43 $form->addElement('selectyesno', 'signup', get_string('profilesignup', 'admin'));
44
bb6d3d34 45 $choices = array();
46 $choices[PROFILE_VISIBLE_NONE] = get_string('profilevisiblenone', 'admin');
47 $choices[PROFILE_VISIBLE_PRIVATE] = get_string('profilevisibleprivate', 'admin');
48 $choices[PROFILE_VISIBLE_ALL] = get_string('profilevisibleall', 'admin');
49 $form->addElement('select', 'visible', get_string('profilevisible', 'admin'), $choices);
50 $form->setHelpButton('visible', array('profilevisible', get_string('profilevisible','admin')));
51 $form->setDefault('visible', PROFILE_VISIBLE_ALL);
52
53 $choices = profile_list_categories();
54 $form->addElement('select', 'categoryid', get_string('profilecategory', 'admin'), $choices);
55 }
56
57 /**
58 * Prints out the form snippet for the part of creating or
59 * editing a profile field specific to the current data type
60 * @param object instance of the moodleform class
61 */
62 function define_form_specific(&$form) {
63 /// do nothing - overwrite if necessary
64 }
65
66 /**
67 * Validate the data from the add/edit profile field form.
68 * Generally this method should not be overwritten by child
69 * classes.
70 * @param object data from the add/edit profile field form
71 * @return array associative array of error messages
72 */
a78890d5 73 function define_validate($data, $files) {
bb6d3d34 74
75 $data = (object)$data;
76 $err = array();
77
a78890d5 78 $err += $this->define_validate_common($data, $files);
79 $err += $this->define_validate_specific($data, $files);
bb6d3d34 80
a78890d5 81 return $err;
bb6d3d34 82 }
83
84 /**
85 * Validate the data from the add/edit profile field form
86 * that is common to all data types. Generally this method
87 * should not be overwritten by child classes.
88 * @param object data from the add/edit profile field form
89 * @return array associative array of error messages
90 */
a78890d5 91 function define_validate_common($data, $files) {
093c3b90 92
cfdb1706 93 global $USER;
093c3b90 94
bb6d3d34 95 $err = array();
96
1bf65e58 97 /// Check the shortname was not truncated by cleaning
98 if (empty($data->shortname)) {
99 $err['shortname'] = get_string('required');
100
093c3b90 101 } else {
102 /// Fetch field-record from DB
103 $field = get_record('user_info_field', 'shortname', $data->shortname);
bb6d3d34 104 /// Check the shortname is unique
093c3b90 105 if ($field and $field->id <> $data->id) {
106 $err['shortname'] = get_string('profileshortnamenotunique', 'admin');
cfdb1706 107
108 /// Shortname must also be unique compared to the standard user fields
093c3b90 109 } else if (!$field and isset($USER->{$data->shortname})) {
110 $err['shortname'] = get_string('profileshortnamenotunique', 'admin');
111 }
bb6d3d34 112 }
113
114 /// No further checks necessary as the form class will take care of it
bb6d3d34 115 return $err;
116 }
117
118 /**
119 * Validate the data from the add/edit profile field form
120 * that is specific to the current data type
121 * @param object data from the add/edit profile field form
122 * @return array associative array of error messages
123 */
a78890d5 124 function define_validate_specific($data, $files) {
bb6d3d34 125 /// do nothing - overwrite if necessary
126 return array();
127 }
128
129 /**
130 * Alter form based on submitted or existing data
131 * @param object form
132 */
133 function define_after_data(&$mform) {
134 /// do nothing - overwrite if necessary
135 }
136
137 /**
138 * Add a new profile field or save changes to current field
139 * @param object data from the add/edit profile field form
140 * @return boolean status of the insert/update record
141 */
142 function define_save($data) {
143
144 $data = $this->define_save_preprocess($data); /// hook for child classes
145
146 $old = get_record('user_info_field', 'id', $data->id);
147
148 /// check to see if the category has changed
149 if (!$old or $old->categoryid != $data->categoryid) {
150 $data->sortorder = count_records_select('user_info_field', 'categoryid='.$data->categoryid) + 1;
151 }
152
153
154 if (empty($data->id)) {
155 unset($data->id);
156 if (!$data->id = insert_record('user_info_field', $data)) {
5a2a5331 157 print_error('Error creating new field');
bb6d3d34 158 }
159 } else {
160 if (!update_record('user_info_field', $data)) {
5a2a5331 161 print_error('Error updating field');
bb6d3d34 162 }
163 }
164 }
165
166 /**
167 * Preprocess data from the add/edit profile field form
168 * before it is saved. This method is a hook for the child
169 * classes to overwrite.
170 * @param object data from the add/edit profile field form
171 * @return object processed data object
172 */
173 function define_save_preprocess($data) {
174 /// do nothing - overwrite if necessary
175 return $data;
176 }
177
178}
179
180
181
182/**
183 * Reorder the profile fields within a given category starting
184 * at the field at the given startorder
185 */
186function profile_reorder_fields() {
187 if ($categories = get_records_select('user_info_category')) {
bb6d3d34 188 foreach ($categories as $category) {
258db59a 189 $i = 1;
bb6d3d34 190 if ($fields = get_records_select('user_info_field', 'categoryid='.$category->id, 'sortorder ASC')) {
191 foreach ($fields as $field) {
192 $f = new object();
258db59a 193 $f->id = $field->id;
bb6d3d34 194 $f->sortorder = $i++;
195 update_record('user_info_field', $f);
196 }
197 }
198 }
199 }
200}
201
202/**
203 * Reorder the profile categoriess starting at the category
204 * at the given startorder
205 */
206function profile_reorder_categories() {
207 $i = 1;
208 if ($categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
209 foreach ($categories as $cat) {
210 $c = new object();
211 $c->id = $cat->id;
212 $c->sortorder = $i++;
213 update_record('user_info_category', $c);
214 }
215 }
216}
217
218/**
219 * Delete a profile category
220 * @param integer id of the category to be deleted
221 * @return boolean success of operation
222 */
223function profile_delete_category($id) {
224 /// Retrieve the category
225 if (!$category = get_record('user_info_category', 'id', $id)) {
5a2a5331 226 print_error('Incorrect category id');
bb6d3d34 227 }
228
229 if (!$categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
5a2a5331 230 print_error('Error no categories!?!?');
bb6d3d34 231 }
232
233 unset($categories[$category->id]);
234
235 if (!count($categories)) {
236 return; //we can not delete the last category
237 }
238
239 /// Does the category contain any fields
240 if (count_records('user_info_field', 'categoryid', $category->id)) {
241 if (array_key_exists($category->sortorder-1, $categories)) {
242 $newcategory = $categories[$category->sortorder-1];
243 } else if (array_key_exists($category->sortorder+1, $categories)) {
244 $newcategory = $categories[$category->sortorder+1];
245 } else {
246 $newcategory = reset($categories); // get first category if sortorder broken
247 }
248
249 $sortorder = count_records('user_info_field', 'categoryid', $newcategory->id) + 1;
250
251 if ($fields = get_records_select('user_info_field', 'categoryid='.$category->id, 'sortorder ASC')) {
252 foreach ($fields as $field) {
253 $f = new object();
254 $f->id = $field->id;
255 $f->sortorder = $sortorder++;
256 $f->categoryid = $newcategory->id;
257 update_record('user_info_field', $f);
258 echo "<pre>";var_dump($f);echo"</pre>";
259 }
260 }
261 }
262
263 /// Finally we get to delete the category
264 if (!delete_records('user_info_category', 'id', $category->id)) {
5a2a5331 265 print_error('Error while deliting category');
bb6d3d34 266 }
267 profile_reorder_categories();
268 return true;
269}
270
271
272function profile_delete_field($id) {
273
274 /// Remove any user data associated with this field
275 if (!delete_records('user_info_data', 'fieldid', $id)) {
5a2a5331 276 print_error('Error deleting custom field data');
bb6d3d34 277 }
278
279 /// Try to remove the record from the database
280 delete_records('user_info_field', 'id', $id);
281
282 /// Reorder the remaining fields in the same category
283 profile_reorder_fields();
284}
285
286/**
287 * Change the sortorder of a field
288 * @param integer id of the field
289 * @param string direction of move
290 * @return boolean success of operation
291 */
a5d3b072 292function profile_move_field($id, $move) {
bb6d3d34 293 /// Get the field object
294 if (!$field = get_record('user_info_field', 'id', $id, '', '', '', '', 'id, sortorder, categoryid')) {
295 return false;
296 }
297 /// Count the number of fields in this category
298 $fieldcount = count_records_select('user_info_field', 'categoryid='.$field->categoryid);
299
300 /// Calculate the new sortorder
301 if ( ($move == 'up') and ($field->sortorder > 1)) {
302 $neworder = $field->sortorder - 1;
303 } elseif ( ($move == 'down') and ($field->sortorder < $fieldcount)) {
304 $neworder = $field->sortorder + 1;
305 } else {
306 return false;
307 }
308
309 /// Retrieve the field object that is currently residing in the new position
310 if ($swapfield = get_record('user_info_field', 'categoryid', $field->categoryid, 'sortorder', $neworder, '', '', 'id, sortorder')) {
311
312 /// Swap the sortorders
313 $swapfield->sortorder = $field->sortorder;
314 $field->sortorder = $neworder;
315
316 /// Update the field records
317 update_record('user_info_field', $field);
318 update_record('user_info_field', $swapfield);
319 }
320
321 profile_reorder_fields();
322}
323
324/**
325 * Change the sortorder of a category
326 * @param integer id of the category
327 * @param string direction of move
328 * @return boolean success of operation
329 */
a5d3b072 330function profile_move_category($id, $move) {
bb6d3d34 331 /// Get the category object
332 if (!($category = get_record('user_info_category', 'id', $id, '', '', '', '', 'id, sortorder'))) {
333 return false;
334 }
335
336 /// Count the number of categories
337 $categorycount = count_records('user_info_category');
338
339 /// Calculate the new sortorder
340 if ( ($move == 'up') and ($category->sortorder > 1)) {
341 $neworder = $category->sortorder - 1;
342 } elseif ( ($move == 'down') and ($category->sortorder < $categorycount)) {
343 $neworder = $category->sortorder + 1;
344 } else {
345 return false;
346 }
347
348 /// Retrieve the category object that is currently residing in the new position
349 if ($swapcategory = get_record('user_info_category', 'sortorder', $neworder, '', '', '', '', 'id, sortorder')) {
350
351 /// Swap the sortorders
352 $swapcategory->sortorder = $category->sortorder;
353 $category->sortorder = $neworder;
354
355 /// Update the category records
356 if (update_record('user_info_category', $category) and update_record('user_info_category', $swapcategory)) {
357 return true;
358 }
359 }
360
361 return false;
362}
363
364/**
365 * Retrieve a list of all the available data types
366 * @return array a list of the datatypes suitable to use in a select statement
367 */
368function profile_list_datatypes() {
369 global $CFG;
370
371 $datatypes = array();
372
373 if ($dirlist = get_directory_list($CFG->dirroot.'/user/profile/field', '', false, true, false)) {
374 foreach ($dirlist as $type) {
b77bbd2f 375 $datatypes[$type] = get_string('profilefieldtype'.$type, 'profilefield_'.$type);
376 if (strpos($datatypes[$type], '[[') !== false) {
377 $datatypes[$type] = get_string('profilefieldtype'.$type, 'admin');
378 }
bb6d3d34 379 }
380 }
b77bbd2f 381 asort($datatypes);
382
bb6d3d34 383 return $datatypes;
384}
385
386/**
387 * Retrieve a list of categories and ids suitable for use in a form
388 * @return array
389 */
390function profile_list_categories() {
391 if (!$categories = get_records_select_menu('user_info_category', '', 'sortorder ASC', 'id, name')) {
392 $categories = array();
393 }
394 return $categories;
395}
396
397
398/// Are we adding or editing a cateogory?
1ae083e4 399function profile_edit_category($id, $redirect) {
bb6d3d34 400 global $CFG;
401
402 require_once('index_category_form.php');
403 $categoryform = new category_form();
404
405 if ($category = get_record('user_info_category', 'id', $id)) {
406 $categoryform->set_data($category);
407 }
408
409 if ($categoryform->is_cancelled()) {
410 redirect($redirect);
411 } else {
412 if ($data = $categoryform->get_data()) {
413 if (empty($data->id)) {
414 unset($data->id);
415 $data->sortorder = count_records('user_info_category') + 1;
416 if (!insert_record('user_info_category', $data, false)) {
5a2a5331 417 print_error('There was a problem adding the record to the database');
bb6d3d34 418 }
419 } else {
420 if (!update_record('user_info_category', $data)) {
5a2a5331 421 print_error('There was a problem updating the record in the database');
bb6d3d34 422 }
423 }
424 profile_reorder_categories();
425 redirect($redirect);
426
427 }
428
429 if (empty($id)) {
430 $strheading = get_string('profilecreatenewcategory', 'admin');
431 } else {
6ba65fa0 432 $strheading = get_string('profileeditcategory', 'admin', format_string($category->name));
bb6d3d34 433 }
434
435 /// Print the page
1ae083e4 436 admin_externalpage_print_header();
bb6d3d34 437 print_heading($strheading);
438 $categoryform->display();
1ae083e4 439 admin_externalpage_print_footer();
bb6d3d34 440 die;
441 }
442
443}
444
1ae083e4 445function profile_edit_field($id, $datatype, $redirect) {
bb6d3d34 446 global $CFG;
447
448 if (!$field = get_record('user_info_field', 'id', $id)) {
449 $field = new object();
450 $field->datatype = $datatype;
451 }
452
453 require_once('index_field_form.php');
454 $fieldform = new field_form(null, $field->datatype);
455 $fieldform->set_data($field);
456
457 if ($fieldform->is_cancelled()) {
458 redirect($redirect);
459
460 } else {
461 if ($data = $fieldform->get_data()) {
462 require_once($CFG->dirroot.'/user/profile/field/'.$datatype.'/define.class.php');
463 $newfield = 'profile_define_'.$datatype;
464 $formfield = new $newfield();
465 $formfield->define_save($data);
466 profile_reorder_fields();
467 profile_reorder_categories();
468 redirect($redirect);
469 }
470
471 $datatypes = profile_list_datatypes();
472
473 if (empty($id)) {
474 $strheading = get_string('profilecreatenewfield', 'admin', $datatypes[$datatype]);
475 } else {
476 $strheading = get_string('profileeditfield', 'admin', $field->name);
477 }
478
479 /// Print the page
1ae083e4 480 admin_externalpage_print_header();
bb6d3d34 481 print_heading($strheading);
482 $fieldform->display();
1ae083e4 483 admin_externalpage_print_footer();
bb6d3d34 484 die;
485 }
486}
487
62ebb000 488?>