Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / user / profile / definelib.php
blob1b889c620807bd4e0158b936bda750e38390d7c5
1 <?php //$Id$
3 class profile_define_base {
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);
13 $form->addElement('header', '_specificsettings', get_string('profilespecificsettings', 'admin'));
14 $this->define_form_specific($form);
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
22 function define_form_common(&$form) {
24 $strrequired = get_string('required');
26 $form->addElement('text', 'shortname', get_string('profileshortname', 'admin'), 'maxlength="100" size="25"');
27 $form->addRule('shortname', $strrequired, 'required', null, 'client');
28 $form->setType('shortname', PARAM_ALPHANUM);
30 $form->addElement('text', 'name', get_string('profilename', 'admin'), 'size="50"');
31 $form->addRule('name', $strrequired, 'required', null, 'client');
32 $form->setType('name', PARAM_MULTILANG);
34 $form->addElement('htmleditor', 'description', get_string('profiledescription', 'admin'));
35 $form->setHelpButton('description', array('text', get_string('helptext')));
37 $form->addElement('selectyesno', 'required', get_string('profilerequired', 'admin'));
39 $form->addElement('selectyesno', 'locked', get_string('profilelocked', 'admin'));
41 $form->addElement('selectyesno', 'forceunique', get_string('profileforceunique', 'admin'));
43 $form->addElement('selectyesno', 'signup', get_string('profilesignup', 'admin'));
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);
53 $choices = profile_list_categories();
54 $form->addElement('select', 'categoryid', get_string('profilecategory', 'admin'), $choices);
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
62 function define_form_specific(&$form) {
63 /// do nothing - overwrite if necessary
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
73 function define_validate($data, $files) {
75 $data = (object)$data;
76 $err = array();
78 $err += $this->define_validate_common($data, $files);
79 $err += $this->define_validate_specific($data, $files);
81 return $err;
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
91 function define_validate_common($data, $files) {
93 global $USER;
95 $err = array();
97 /// Check the shortname was not truncated by cleaning
98 if (empty($data->shortname)) {
99 $err['shortname'] = get_string('required');
101 } else {
102 /// Fetch field-record from DB
103 $field = get_record('user_info_field', 'shortname', $data->shortname);
104 /// Check the shortname is unique
105 if ($field and $field->id <> $data->id) {
106 $err['shortname'] = get_string('profileshortnamenotunique', 'admin');
108 /// Shortname must also be unique compared to the standard user fields
109 } else if (!$field and isset($USER->{$data->shortname})) {
110 $err['shortname'] = get_string('profileshortnamenotunique', 'admin');
114 /// No further checks necessary as the form class will take care of it
115 return $err;
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
124 function define_validate_specific($data, $files) {
125 /// do nothing - overwrite if necessary
126 return array();
130 * Alter form based on submitted or existing data
131 * @param object form
133 function define_after_data(&$mform) {
134 /// do nothing - overwrite if necessary
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
142 function define_save($data) {
144 $data = $this->define_save_preprocess($data); /// hook for child classes
146 $old = get_record('user_info_field', 'id', $data->id);
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;
154 if (empty($data->id)) {
155 unset($data->id);
156 if (!$data->id = insert_record('user_info_field', $data)) {
157 error('Error creating new field');
159 } else {
160 if (!update_record('user_info_field', $data)) {
161 error('Error updating field');
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
173 function define_save_preprocess($data) {
174 /// do nothing - overwrite if necessary
175 return $data;
183 * Reorder the profile fields within a given category starting
184 * at the field at the given startorder
186 function profile_reorder_fields() {
187 if ($categories = get_records_select('user_info_category')) {
188 foreach ($categories as $category) {
189 $i = 1;
190 if ($fields = get_records_select('user_info_field', 'categoryid='.$category->id, 'sortorder ASC')) {
191 foreach ($fields as $field) {
192 $f = new object();
193 $f->id = $field->id;
194 $f->sortorder = $i++;
195 update_record('user_info_field', $f);
203 * Reorder the profile categoriess starting at the category
204 * at the given startorder
206 function 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);
219 * Delete a profile category
220 * @param integer id of the category to be deleted
221 * @return boolean success of operation
223 function profile_delete_category($id) {
224 /// Retrieve the category
225 if (!$category = get_record('user_info_category', 'id', $id)) {
226 error('Incorrect category id');
229 if (!$categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
230 error('Error no categories!?!?');
233 unset($categories[$category->id]);
235 if (!count($categories)) {
236 return; //we can not delete the last category
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
249 $sortorder = count_records('user_info_field', 'categoryid', $newcategory->id) + 1;
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>";
263 /// Finally we get to delete the category
264 if (!delete_records('user_info_category', 'id', $category->id)) {
265 error('Error while deliting category');
267 profile_reorder_categories();
268 return true;
272 function profile_delete_field($id) {
274 /// Remove any user data associated with this field
275 if (!delete_records('user_info_data', 'fieldid', $id)) {
276 error('Error deleting custom field data');
279 /// Try to remove the record from the database
280 delete_records('user_info_field', 'id', $id);
282 /// Reorder the remaining fields in the same category
283 profile_reorder_fields();
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
292 function profile_move_field($id, $move) {
293 /// Get the field object
294 if (!$field = get_record('user_info_field', 'id', $id, '', '', '', '', 'id, sortorder, categoryid')) {
295 return false;
297 /// Count the number of fields in this category
298 $fieldcount = count_records_select('user_info_field', 'categoryid='.$field->categoryid);
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;
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')) {
312 /// Swap the sortorders
313 $swapfield->sortorder = $field->sortorder;
314 $field->sortorder = $neworder;
316 /// Update the field records
317 update_record('user_info_field', $field);
318 update_record('user_info_field', $swapfield);
321 profile_reorder_fields();
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
330 function profile_move_category($id, $move) {
331 /// Get the category object
332 if (!($category = get_record('user_info_category', 'id', $id, '', '', '', '', 'id, sortorder'))) {
333 return false;
336 /// Count the number of categories
337 $categorycount = count_records('user_info_category');
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;
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')) {
351 /// Swap the sortorders
352 $swapcategory->sortorder = $category->sortorder;
353 $category->sortorder = $neworder;
355 /// Update the category records
356 if (update_record('user_info_category', $category) and update_record('user_info_category', $swapcategory)) {
357 return true;
361 return false;
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
368 function profile_list_datatypes() {
369 global $CFG;
371 $datatypes = array();
373 if ($dirlist = get_directory_list($CFG->dirroot.'/user/profile/field', '', false, true, false)) {
374 foreach ($dirlist as $type) {
375 $datatypes[$type] = get_string('profilefieldtype'.$type, 'profilefield_'.$type);
376 if (strpos($datatypes[$type], '[[') !== false) {
377 $datatypes[$type] = get_string('profilefieldtype'.$type, 'admin');
381 asort($datatypes);
383 return $datatypes;
387 * Retrieve a list of categories and ids suitable for use in a form
388 * @return array
390 function profile_list_categories() {
391 if (!$categories = get_records_select_menu('user_info_category', '', 'sortorder ASC', 'id, name')) {
392 $categories = array();
394 return $categories;
398 /// Are we adding or editing a cateogory?
399 function profile_edit_category($id, $redirect) {
400 global $CFG;
402 require_once('index_category_form.php');
403 $categoryform = new category_form();
405 if ($category = get_record('user_info_category', 'id', $id)) {
406 $categoryform->set_data($category);
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)) {
417 error('There was a problem adding the record to the database');
419 } else {
420 if (!update_record('user_info_category', $data)) {
421 error('There was a problem updating the record in the database');
424 profile_reorder_categories();
425 redirect($redirect);
429 if (empty($id)) {
430 $strheading = get_string('profilecreatenewcategory', 'admin');
431 } else {
432 $strheading = get_string('profileeditcategory', 'admin', format_string($category->name));
435 /// Print the page
436 admin_externalpage_print_header();
437 print_heading($strheading);
438 $categoryform->display();
439 admin_externalpage_print_footer();
440 die;
445 function profile_edit_field($id, $datatype, $redirect) {
446 global $CFG;
448 if (!$field = get_record('user_info_field', 'id', $id)) {
449 $field = new object();
450 $field->datatype = $datatype;
453 require_once('index_field_form.php');
454 $fieldform = new field_form(null, $field->datatype);
455 $fieldform->set_data($field);
457 if ($fieldform->is_cancelled()) {
458 redirect($redirect);
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);
471 $datatypes = profile_list_datatypes();
473 if (empty($id)) {
474 $strheading = get_string('profilecreatenewfield', 'admin', $datatypes[$datatype]);
475 } else {
476 $strheading = get_string('profileeditfield', 'admin', $field->name);
479 /// Print the page
480 admin_externalpage_print_header();
481 print_heading($strheading);
482 $fieldform->display();
483 admin_externalpage_print_footer();
484 die;