MDL-20830 workshop requires sesskey when deleting submission
[moodle.git] / user / profile / definelib.php
blobf9f96cc5ec48d9f611cf52f83e52e6f2fc0003c5
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 = false;
147 if (!empty($data->id)) {
148 $old = get_record('user_info_field', 'id', $data->id);
151 /// check to see if the category has changed
152 if (!$old or $old->categoryid != $data->categoryid) {
153 $data->sortorder = count_records_select('user_info_field', 'categoryid='.$data->categoryid) + 1;
157 if (empty($data->id)) {
158 unset($data->id);
159 if (!$data->id = insert_record('user_info_field', $data)) {
160 error('Error creating new field');
162 } else {
163 if (!update_record('user_info_field', $data)) {
164 error('Error updating field');
170 * Preprocess data from the add/edit profile field form
171 * before it is saved. This method is a hook for the child
172 * classes to overwrite.
173 * @param object data from the add/edit profile field form
174 * @return object processed data object
176 function define_save_preprocess($data) {
177 /// do nothing - overwrite if necessary
178 return $data;
186 * Reorder the profile fields within a given category starting
187 * at the field at the given startorder
189 function profile_reorder_fields() {
190 if ($categories = get_records_select('user_info_category')) {
191 foreach ($categories as $category) {
192 $i = 1;
193 if ($fields = get_records_select('user_info_field', 'categoryid='.$category->id, 'sortorder ASC')) {
194 foreach ($fields as $field) {
195 $f = new object();
196 $f->id = $field->id;
197 $f->sortorder = $i++;
198 update_record('user_info_field', $f);
206 * Reorder the profile categoriess starting at the category
207 * at the given startorder
209 function profile_reorder_categories() {
210 $i = 1;
211 if ($categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
212 foreach ($categories as $cat) {
213 $c = new object();
214 $c->id = $cat->id;
215 $c->sortorder = $i++;
216 update_record('user_info_category', $c);
222 * Delete a profile category
223 * @param integer id of the category to be deleted
224 * @return boolean success of operation
226 function profile_delete_category($id) {
227 /// Retrieve the category
228 if (!$category = get_record('user_info_category', 'id', $id)) {
229 error('Incorrect category id');
232 if (!$categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
233 error('Error no categories!?!?');
236 unset($categories[$category->id]);
238 if (!count($categories)) {
239 return; //we can not delete the last category
242 /// Does the category contain any fields
243 if (count_records('user_info_field', 'categoryid', $category->id)) {
244 if (array_key_exists($category->sortorder-1, $categories)) {
245 $newcategory = $categories[$category->sortorder-1];
246 } else if (array_key_exists($category->sortorder+1, $categories)) {
247 $newcategory = $categories[$category->sortorder+1];
248 } else {
249 $newcategory = reset($categories); // get first category if sortorder broken
252 $sortorder = count_records('user_info_field', 'categoryid', $newcategory->id) + 1;
254 if ($fields = get_records_select('user_info_field', 'categoryid='.$category->id, 'sortorder ASC')) {
255 foreach ($fields as $field) {
256 $f = new object();
257 $f->id = $field->id;
258 $f->sortorder = $sortorder++;
259 $f->categoryid = $newcategory->id;
260 update_record('user_info_field', $f);
261 echo "<pre>";var_dump($f);echo"</pre>";
266 /// Finally we get to delete the category
267 if (!delete_records('user_info_category', 'id', $category->id)) {
268 error('Error while deliting category');
270 profile_reorder_categories();
271 return true;
275 function profile_delete_field($id) {
277 /// Remove any user data associated with this field
278 if (!delete_records('user_info_data', 'fieldid', $id)) {
279 error('Error deleting custom field data');
282 /// Try to remove the record from the database
283 delete_records('user_info_field', 'id', $id);
285 /// Reorder the remaining fields in the same category
286 profile_reorder_fields();
290 * Change the sortorder of a field
291 * @param integer id of the field
292 * @param string direction of move
293 * @return boolean success of operation
295 function profile_move_field($id, $move) {
296 /// Get the field object
297 if (!$field = get_record('user_info_field', 'id', $id, '', '', '', '', 'id, sortorder, categoryid')) {
298 return false;
300 /// Count the number of fields in this category
301 $fieldcount = count_records_select('user_info_field', 'categoryid='.$field->categoryid);
303 /// Calculate the new sortorder
304 if ( ($move == 'up') and ($field->sortorder > 1)) {
305 $neworder = $field->sortorder - 1;
306 } elseif ( ($move == 'down') and ($field->sortorder < $fieldcount)) {
307 $neworder = $field->sortorder + 1;
308 } else {
309 return false;
312 /// Retrieve the field object that is currently residing in the new position
313 if ($swapfield = get_record('user_info_field', 'categoryid', $field->categoryid, 'sortorder', $neworder, '', '', 'id, sortorder')) {
315 /// Swap the sortorders
316 $swapfield->sortorder = $field->sortorder;
317 $field->sortorder = $neworder;
319 /// Update the field records
320 update_record('user_info_field', $field);
321 update_record('user_info_field', $swapfield);
324 profile_reorder_fields();
328 * Change the sortorder of a category
329 * @param integer id of the category
330 * @param string direction of move
331 * @return boolean success of operation
333 function profile_move_category($id, $move) {
334 /// Get the category object
335 if (!($category = get_record('user_info_category', 'id', $id, '', '', '', '', 'id, sortorder'))) {
336 return false;
339 /// Count the number of categories
340 $categorycount = count_records('user_info_category');
342 /// Calculate the new sortorder
343 if ( ($move == 'up') and ($category->sortorder > 1)) {
344 $neworder = $category->sortorder - 1;
345 } elseif ( ($move == 'down') and ($category->sortorder < $categorycount)) {
346 $neworder = $category->sortorder + 1;
347 } else {
348 return false;
351 /// Retrieve the category object that is currently residing in the new position
352 if ($swapcategory = get_record('user_info_category', 'sortorder', $neworder, '', '', '', '', 'id, sortorder')) {
354 /// Swap the sortorders
355 $swapcategory->sortorder = $category->sortorder;
356 $category->sortorder = $neworder;
358 /// Update the category records
359 if (update_record('user_info_category', $category) and update_record('user_info_category', $swapcategory)) {
360 return true;
364 return false;
368 * Retrieve a list of all the available data types
369 * @return array a list of the datatypes suitable to use in a select statement
371 function profile_list_datatypes() {
372 global $CFG;
374 $datatypes = array();
376 if ($dirlist = get_directory_list($CFG->dirroot.'/user/profile/field', '', false, true, false)) {
377 foreach ($dirlist as $type) {
378 $datatypes[$type] = get_string('profilefieldtype'.$type, 'profilefield_'.$type);
379 if (strpos($datatypes[$type], '[[') !== false) {
380 $datatypes[$type] = get_string('profilefieldtype'.$type, 'admin');
384 asort($datatypes);
386 return $datatypes;
390 * Retrieve a list of categories and ids suitable for use in a form
391 * @return array
393 function profile_list_categories() {
394 if (!$categories = get_records_select_menu('user_info_category', '', 'sortorder ASC', 'id, name')) {
395 $categories = array();
397 return $categories;
401 /// Are we adding or editing a cateogory?
402 function profile_edit_category($id, $redirect) {
403 global $CFG;
405 require_once('index_category_form.php');
406 $categoryform = new category_form();
408 if ($category = get_record('user_info_category', 'id', $id)) {
409 $categoryform->set_data($category);
412 if ($categoryform->is_cancelled()) {
413 redirect($redirect);
414 } else {
415 if ($data = $categoryform->get_data()) {
416 if (empty($data->id)) {
417 unset($data->id);
418 $data->sortorder = count_records('user_info_category') + 1;
419 if (!insert_record('user_info_category', $data, false)) {
420 error('There was a problem adding the record to the database');
422 } else {
423 if (!update_record('user_info_category', $data)) {
424 error('There was a problem updating the record in the database');
427 profile_reorder_categories();
428 redirect($redirect);
432 if (empty($id)) {
433 $strheading = get_string('profilecreatenewcategory', 'admin');
434 } else {
435 $strheading = get_string('profileeditcategory', 'admin', format_string($category->name));
438 /// Print the page
439 admin_externalpage_print_header();
440 print_heading($strheading);
441 $categoryform->display();
442 admin_externalpage_print_footer();
443 die;
448 function profile_edit_field($id, $datatype, $redirect) {
449 global $CFG;
451 if (!$field = get_record('user_info_field', 'id', $id)) {
452 $field = new object();
453 $field->datatype = $datatype;
456 require_once('index_field_form.php');
457 $fieldform = new field_form(null, $field->datatype);
458 $fieldform->set_data($field);
460 if ($fieldform->is_cancelled()) {
461 redirect($redirect);
463 } else {
464 if ($data = $fieldform->get_data()) {
465 require_once($CFG->dirroot.'/user/profile/field/'.$datatype.'/define.class.php');
466 $newfield = 'profile_define_'.$datatype;
467 $formfield = new $newfield();
468 $formfield->define_save($data);
469 profile_reorder_fields();
470 profile_reorder_categories();
471 redirect($redirect);
474 $datatypes = profile_list_datatypes();
476 if (empty($id)) {
477 $strheading = get_string('profilecreatenewfield', 'admin', $datatypes[$datatype]);
478 } else {
479 $strheading = get_string('profileeditfield', 'admin', $field->name);
482 /// Print the page
483 admin_externalpage_print_header();
484 print_heading($strheading);
485 $fieldform->display();
486 admin_externalpage_print_footer();
487 die;