adding some strings
[moodle-linuxchix.git] / user / profile / definelib.php
blob60ff80baf2ce4fa824d52ec082b215a7c92c7be2
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 $choices = array();
42 $choices[PROFILE_VISIBLE_NONE] = get_string('profilevisiblenone', 'admin');
43 $choices[PROFILE_VISIBLE_PRIVATE] = get_string('profilevisibleprivate', 'admin');
44 $choices[PROFILE_VISIBLE_ALL] = get_string('profilevisibleall', 'admin');
45 $form->addElement('select', 'visible', get_string('profilevisible', 'admin'), $choices);
46 $form->setHelpButton('visible', array('profilevisible', get_string('profilevisible','admin')));
47 $form->setDefault('visible', PROFILE_VISIBLE_ALL);
49 $choices = profile_list_categories();
50 $form->addElement('select', 'categoryid', get_string('profilecategory', 'admin'), $choices);
53 /**
54 * Prints out the form snippet for the part of creating or
55 * editing a profile field specific to the current data type
56 * @param object instance of the moodleform class
58 function define_form_specific(&$form) {
59 /// do nothing - overwrite if necessary
62 /**
63 * Validate the data from the add/edit profile field form.
64 * Generally this method should not be overwritten by child
65 * classes.
66 * @param object data from the add/edit profile field form
67 * @return array associative array of error messages
69 function define_validate($data) {
71 $data = (object)$data;
72 $err = array();
74 $err += $this->define_validate_common($data);
75 $err += $this->define_validate_specific($data);
77 if (count($err) == 0){
78 return true;
79 } else {
80 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) {
92 $err = array();
94 /// Check the shortname was not truncated by cleaning
95 if (empty($data->shortname)) {
96 $err['shortname'] = get_string('required');
98 /// Check the shortname is unique
99 } else if (($field = get_record('user_info_field', 'shortname', $data->shortname)) and ($field->id <> $data->id)) {
100 $err['shortname'] = get_string('profileshortnamenotunique', 'admin');
103 /// No further checks necessary as the form class will take care of it
105 return $err;
109 * Validate the data from the add/edit profile field form
110 * that is specific to the current data type
111 * @param object data from the add/edit profile field form
112 * @return array associative array of error messages
114 function define_validate_specific($data) {
115 /// do nothing - overwrite if necessary
116 return array();
120 * Alter form based on submitted or existing data
121 * @param object form
123 function define_after_data(&$mform) {
124 /// do nothing - overwrite if necessary
128 * Add a new profile field or save changes to current field
129 * @param object data from the add/edit profile field form
130 * @return boolean status of the insert/update record
132 function define_save($data) {
134 $data = $this->define_save_preprocess($data); /// hook for child classes
136 $old = get_record('user_info_field', 'id', $data->id);
138 /// check to see if the category has changed
139 if (!$old or $old->categoryid != $data->categoryid) {
140 $data->sortorder = count_records_select('user_info_field', 'categoryid='.$data->categoryid) + 1;
144 if (empty($data->id)) {
145 unset($data->id);
146 if (!$data->id = insert_record('user_info_field', $data)) {
147 error('Error creating new field');
149 } else {
150 if (!update_record('user_info_field', $data)) {
151 error('Error updating field');
157 * Preprocess data from the add/edit profile field form
158 * before it is saved. This method is a hook for the child
159 * classes to overwrite.
160 * @param object data from the add/edit profile field form
161 * @return object processed data object
163 function define_save_preprocess($data) {
164 /// do nothing - overwrite if necessary
165 return $data;
173 * Reorder the profile fields within a given category starting
174 * at the field at the given startorder
176 function profile_reorder_fields() {
177 if ($categories = get_records_select('user_info_category')) {
178 $i = 1;
179 foreach ($categories as $category) {
180 if ($fields = get_records_select('user_info_field', 'categoryid='.$category->id, 'sortorder ASC')) {
181 foreach ($fields as $field) {
182 $f = new object();
183 $f->if = $field->id;
184 $f->sortorder = $i++;
185 update_record('user_info_field', $f);
193 * Reorder the profile categoriess starting at the category
194 * at the given startorder
196 function profile_reorder_categories() {
197 $i = 1;
198 if ($categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
199 foreach ($categories as $cat) {
200 $c = new object();
201 $c->id = $cat->id;
202 $c->sortorder = $i++;
203 update_record('user_info_category', $c);
209 * Delete a profile category
210 * @param integer id of the category to be deleted
211 * @return boolean success of operation
213 function profile_delete_category($id) {
214 /// Retrieve the category
215 if (!$category = get_record('user_info_category', 'id', $id)) {
216 error('Incorrect category id');
219 if (!$categories = get_records_select('user_info_category', '', 'sortorder ASC')) {
220 error('Error no categories!?!?');
223 unset($categories[$category->id]);
225 if (!count($categories)) {
226 return; //we can not delete the last category
229 /// Does the category contain any fields
230 if (count_records('user_info_field', 'categoryid', $category->id)) {
231 if (array_key_exists($category->sortorder-1, $categories)) {
232 $newcategory = $categories[$category->sortorder-1];
233 } else if (array_key_exists($category->sortorder+1, $categories)) {
234 $newcategory = $categories[$category->sortorder+1];
235 } else {
236 $newcategory = reset($categories); // get first category if sortorder broken
239 $sortorder = count_records('user_info_field', 'categoryid', $newcategory->id) + 1;
241 if ($fields = get_records_select('user_info_field', 'categoryid='.$category->id, 'sortorder ASC')) {
242 foreach ($fields as $field) {
243 $f = new object();
244 $f->id = $field->id;
245 $f->sortorder = $sortorder++;
246 $f->categoryid = $newcategory->id;
247 update_record('user_info_field', $f);
248 echo "<pre>";var_dump($f);echo"</pre>";
253 /// Finally we get to delete the category
254 if (!delete_records('user_info_category', 'id', $category->id)) {
255 error('Error while deliting category');
257 profile_reorder_categories();
258 return true;
262 function profile_delete_field($id) {
264 /// Remove any user data associated with this field
265 if (!delete_records('user_info_data', 'fieldid', $id)) {
266 error('Error deleting custom field data');
269 /// Try to remove the record from the database
270 delete_records('user_info_field', 'id', $id);
272 /// Reorder the remaining fields in the same category
273 profile_reorder_fields();
277 * Change the sortorder of a field
278 * @param integer id of the field
279 * @param string direction of move
280 * @return boolean success of operation
282 function profile_move_field($id, $move) {
283 /// Get the field object
284 if (!$field = get_record('user_info_field', 'id', $id, '', '', '', '', 'id, sortorder, categoryid')) {
285 return false;
287 /// Count the number of fields in this category
288 $fieldcount = count_records_select('user_info_field', 'categoryid='.$field->categoryid);
290 /// Calculate the new sortorder
291 if ( ($move == 'up') and ($field->sortorder > 1)) {
292 $neworder = $field->sortorder - 1;
293 } elseif ( ($move == 'down') and ($field->sortorder < $fieldcount)) {
294 $neworder = $field->sortorder + 1;
295 } else {
296 return false;
299 /// Retrieve the field object that is currently residing in the new position
300 if ($swapfield = get_record('user_info_field', 'categoryid', $field->categoryid, 'sortorder', $neworder, '', '', 'id, sortorder')) {
302 /// Swap the sortorders
303 $swapfield->sortorder = $field->sortorder;
304 $field->sortorder = $neworder;
306 /// Update the field records
307 update_record('user_info_field', $field);
308 update_record('user_info_field', $swapfield);
311 profile_reorder_fields();
315 * Change the sortorder of a category
316 * @param integer id of the category
317 * @param string direction of move
318 * @return boolean success of operation
320 function profile_move_category($id, $move) {
321 /// Get the category object
322 if (!($category = get_record('user_info_category', 'id', $id, '', '', '', '', 'id, sortorder'))) {
323 return false;
326 /// Count the number of categories
327 $categorycount = count_records('user_info_category');
329 /// Calculate the new sortorder
330 if ( ($move == 'up') and ($category->sortorder > 1)) {
331 $neworder = $category->sortorder - 1;
332 } elseif ( ($move == 'down') and ($category->sortorder < $categorycount)) {
333 $neworder = $category->sortorder + 1;
334 } else {
335 return false;
338 /// Retrieve the category object that is currently residing in the new position
339 if ($swapcategory = get_record('user_info_category', 'sortorder', $neworder, '', '', '', '', 'id, sortorder')) {
341 /// Swap the sortorders
342 $swapcategory->sortorder = $category->sortorder;
343 $category->sortorder = $neworder;
345 /// Update the category records
346 if (update_record('user_info_category', $category) and update_record('user_info_category', $swapcategory)) {
347 return true;
351 return false;
355 * Retrieve a list of all the available data types
356 * @return array a list of the datatypes suitable to use in a select statement
358 function profile_list_datatypes() {
359 global $CFG;
361 $datatypes = array();
363 if ($dirlist = get_directory_list($CFG->dirroot.'/user/profile/field', '', false, true, false)) {
364 foreach ($dirlist as $type) {
365 $datatypes[$type] = get_string('profilefieldtype'.$type, 'admin');
368 return $datatypes;
372 * Retrieve a list of categories and ids suitable for use in a form
373 * @return array
375 function profile_list_categories() {
376 if (!$categories = get_records_select_menu('user_info_category', '', 'sortorder ASC', 'id, name')) {
377 $categories = array();
379 return $categories;
383 /// Are we adding or editing a cateogory?
384 function profile_edit_category($id, $redirect) {
385 global $CFG;
387 require_once('index_category_form.php');
388 $categoryform = new category_form();
390 if ($category = get_record('user_info_category', 'id', $id)) {
391 $categoryform->set_data($category);
394 if ($categoryform->is_cancelled()) {
395 redirect($redirect);
396 } else {
397 if ($data = $categoryform->get_data()) {
398 if (empty($data->id)) {
399 unset($data->id);
400 $data->sortorder = count_records('user_info_category') + 1;
401 if (!insert_record('user_info_category', $data, false)) {
402 error('There was a problem adding the record to the database');
404 } else {
405 if (!update_record('user_info_category', $data)) {
406 error('There was a problem updating the record in the database');
409 profile_reorder_categories();
410 redirect($redirect);
414 if (empty($id)) {
415 $strheading = get_string('profilecreatenewcategory', 'admin');
416 } else {
417 $strheading = get_string('profileeditcategory', 'admin', format_string($category->name));
420 /// Print the page
421 admin_externalpage_print_header();
422 print_heading($strheading);
423 $categoryform->display();
424 admin_externalpage_print_footer();
425 die;
430 function profile_edit_field($id, $datatype, $redirect) {
431 global $CFG;
433 if (!$field = get_record('user_info_field', 'id', $id)) {
434 $field = new object();
435 $field->datatype = $datatype;
438 require_once('index_field_form.php');
439 $fieldform = new field_form(null, $field->datatype);
440 $fieldform->set_data($field);
442 if ($fieldform->is_cancelled()) {
443 redirect($redirect);
445 } else {
446 if ($data = $fieldform->get_data()) {
447 require_once($CFG->dirroot.'/user/profile/field/'.$datatype.'/define.class.php');
448 $newfield = 'profile_define_'.$datatype;
449 $formfield = new $newfield();
450 $formfield->define_save($data);
451 profile_reorder_fields();
452 profile_reorder_categories();
453 redirect($redirect);
456 $datatypes = profile_list_datatypes();
458 if (empty($id)) {
459 $strheading = get_string('profilecreatenewfield', 'admin', $datatypes[$datatype]);
460 } else {
461 $strheading = get_string('profileeditfield', 'admin', $field->name);
464 /// Print the page
465 admin_externalpage_print_header();
466 print_heading($strheading);
467 $fieldform->display();
468 admin_externalpage_print_footer();
469 die;