FIxed an annoying little artifact showing up on the second line of the last tab ...
[moodle.git] / user / profile / index.php
CommitLineData
d0c33889 1<?php //$Id$
2
3require('../../config.php');
4require_once($CFG->dirroot.'/user/profile/lib.php');
5require_once($CFG->libdir.'/adminlib.php');
6
7$adminroot = admin_get_root();
8admin_externalpage_setup('profilefields', $adminroot);
9
10require_login();
11
12require_capability('moodle/user:update', get_context_instance(CONTEXT_SYSTEM, SITEID));
13
14
15$id = optional_param('id', 0, PARAM_INT);
16$action = optional_param('action', '', PARAM_ALPHA);
17$dir = optional_param('dir', '', PARAM_ALPHA);
18$confirm = optional_param('confirm', 0, PARAM_BOOL);
19$type = optional_param('type', '', PARAM_ALPHANUM);
20
21$datatypes = profile_list_datatypes();
22$redirect = $CFG->wwwroot.'/user/profile/index.php';
5c11b818 23
24$strchangessaved = get_string('changessaved');
25$strcancelled = get_string('cancelled');
26$strdefaultcategory = get_string('profiledefaultcategory', 'admin');
27$strnofields = get_string('profilenofieldsdefined', 'admin');
28$strcreatefield = get_string('profilecreatefield', 'admin');
d0c33889 29
30
31/// Do we have any actions to perform before printing the header
32
33switch ($action) {
34 case 'movecategory':
35 if (confirm_sesskey()) {
36 profile_move_category($id, $dir);
37 }
38 redirect($redirect);
39 exit;
40 break;
41 case 'movefield':
42 if (confirm_sesskey()) {
43 profile_move_field($id, $dir);
44 }
45 redirect($redirect);
46 exit;
47 break;
48 case 'deletecategory':
49 if ($confirm and confirm_sesskey()) {
2cf089c9 50 $categorycount = count_records('user_info_category');
d0c33889 51 $fieldcount = count_records('user_info_field', 'categoryid', $id);
52
53 /// Can only delete the last category if there are no fields in it
54 if ( ($categorycount > 1) or ($fieldcount == 0) ) {
55 profile_delete_category($id);
56 }
57 redirect($redirect);
58 exit;
59 }
60 break;
61 case 'deletefield':
62
63 if ($confirm and confirm_sesskey()) {
64 if ($field = get_record('user_info_field', 'id', $id)) {
65 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
66 $newfield = 'profile_field_'.$field->datatype;
67 $formfield = new $newfield($field->id);
68 $formfield->edit_remove_field();
69 }
70 redirect($redirect);
71 exit;
72 }
73
74 break;
75 case 'editfield':
76 unset($field);
77 if ($id == 0) {
78 if (!isset($datatypes[$type])) {
79 redirect($redirect);
80 exit;
81 }
82 $field->id = 0;
c517d8f0 83 $field->datatype = $type;
d0c33889 84 $field->categoryid = 0;
85 } elseif (!($field = get_record('user_info_field', 'id', $id))) {
86 redirect($redirect);
87 exit;
88 }
89 break;
90 case 'editcategory':
91 unset($category);
92 if ($id == 0) {
93 $category->id = 0;
94 $category->name = '';
95 } elseif (!($category = get_record('user_info_category', 'id', $id))) {
96 redirect($redirect);
97 exit;
98 }
99 break;
100 default:
101}
102
103
104
d0c33889 105
d0c33889 106
107
108/// Are we adding or editing a cateogory?
109if ( ($action == 'editcategory' )) {
5c11b818 110
c517d8f0 111
d0c33889 112 require_once('index_category_form.php');
478caed8 113 $categoryform = new category_form(null);
114 $categoryform->set_data($category);
d0c33889 115 if ($categoryform->is_cancelled()) {
c517d8f0 116 redirect($redirect);
117 exit;
d0c33889 118 } else {
beac4717 119 if ($data = $categoryform->get_data()) {
d0c33889 120 if ($data->id == 0) {
121 unset($data->id);
2cf089c9 122 $data->sortorder = count_records('user_info_category') + 1;
d0c33889 123 if (!insert_record('user_info_category', $data, false)) {
124 error('There was a problem adding the record to the database');
125 exit;
126 }
127 } else {
128 if (!update_record('user_info_category', $data)) {
129 error('There was a problem updating the record in the database');
130 exit;
131 }
132 }
c517d8f0 133 redirect($redirect);
134 exit;
135
d0c33889 136 } else {
c517d8f0 137
138 if ($id == 0) {
139 $strheading = get_string('profilecreatenewcategory', 'admin');
140 } else {
141 $strheading = get_string('profileeditcategory', 'admin', $category->name);
142 }
143
144 /// Print the header
145 admin_externalpage_print_header($adminroot);
146
147 print_heading($strheading);
148
d0c33889 149 $categoryform->display();
150 }
151 }
152
153/// Are we adding or editing a field?
154} elseif ( $action == 'editfield' ) {
c517d8f0 155
d0c33889 156 require_once('index_field_form.php');
478caed8 157 $fieldform = new field_form(null, $field->datatype);
158 $fieldform->set_data($field);
d0c33889 159 if ($fieldform->is_cancelled()) {
c517d8f0 160 redirect($redirect);
161 exit;
d0c33889 162 } else {
beac4717 163 if ($data = $fieldform->get_data()) {
d0c33889 164 require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php');
165 $newfield = 'profile_field_'.$field->datatype;
166 $formfield = new $newfield($field->id);
167 if (!$formfield->edit_save($data)) {
168 error('There was an error updating the database');
169 } else {
c517d8f0 170 redirect($redirect);
171 exit;
d0c33889 172 }
173
174 } else {
c517d8f0 175
176 if ($id == 0) {
253625c5 177 $strheading = get_string('profilecreatenewfield', 'admin', $datatypes[$type]);
c517d8f0 178 } else {
179 $strheading = get_string('profileeditfield', 'admin', $field->name);
180 }
181
182 /// Print the header
183 admin_externalpage_print_header($adminroot);
184
185 print_heading($strheading);
186
d0c33889 187 $fieldform->display();
188 }
189 }
190
191/// Deleting a category that has fields in it, print confirm screen?
192} elseif ( ($action == 'deletecategory') and !$confirm ) {
5c11b818 193
c517d8f0 194 /// Print the header
195 admin_externalpage_print_header($adminroot);
196
5c11b818 197 print_heading('profiledeletecategory', 'admin');
c517d8f0 198
d0c33889 199 $fieldcount = count_records('user_info_field', 'categoryid', $id);
200 echo '<center>'.get_string('profileconfirmcategorydeletion', 'admin', $fieldcount).'<br /><a href="index.php?id='.$id.'&amp;action=deletecategory&amp;sesskey='.$USER->sesskey.'&amp;confirm=1">'.get_string('yes').' <a href="index.php">'.get_string('no').'</center>';
201
202
203/// Deleting a field that has user data, print confirm screen
204} elseif ( ($action == 'deletefield') and !$confirm ) {
5c11b818 205
c517d8f0 206 /// Print the header
207 admin_externalpage_print_header($adminroot);
208
5c11b818 209 print_heading('profiledeletefield', 'admin');
210
d0c33889 211 $datacount = count_records('user_info_data', 'fieldid', $id);
212 echo '<center>'.get_string('profileconfirmfielddeletion', 'admin', $datacount).'<br /><a href="index.php?id='.$id.'&amp;action=deletefield&amp;sesskey='.$USER->sesskey.'&amp;confirm=1">'.get_string('yes').' <a href="index.php">'.get_string('no').'</center>';
213
214
215
216/// Print the table of categories and fields
217} else {
218
c517d8f0 219 /// Print the header
220 admin_externalpage_print_header($adminroot);
221
222 print_heading(get_string('profilefields', 'admin'));
223
5c11b818 224 /// Check that we have at least one category defined
2cf089c9 225 if (count_records('user_info_category') == 0) {
5c11b818 226 unset($defaultcategory);
227 $defaultcategory->name = $strdefaultcategory;
228 $defaultcategory->sortorder = 1;
229 insert_record('user_info_category', $defaultcategory);
230 }
231
5c11b818 232 /// We only displaying if there are fields defined or there is a category with a name different to the default name
233 if ( ( (count_records_select('user_info_category', "name<>'$strdefaultcategory'") > 0) or
2cf089c9 234 (count_records('user_info_field') > 0) ) and
235 ( $categories = get_records_select('user_info_category', '', 'sortorder ASC')) ) {
dbb3ecdb 236
d0c33889 237
238 foreach ($categories as $category) {
239
a7568895 240 unset ($table);
241 $table->head = array(get_string('profilefield','admin'),get_string('edit'));
242 $table->align = array('left','right');
243 $table->width = '95%';
244 $table->class = 'generaltable profilefield';
245 $table->data = array();
dbb3ecdb 246
d0c33889 247 if ($fields = get_records_select('user_info_field', "categoryid=$category->id", 'sortorder ASC')) {
248 foreach ($fields as $field) {
249
a7568895 250 $table->data[] = array($field->name, profile_field_icons($field));
d0c33889 251
252 } /// End of $fields foreach
253 } /// End of $fields if
d0c33889 254
a7568895 255 print_heading($category->name.' '.profile_category_icons($category));
d0c33889 256 print_table($table);
a7568895 257
258 } /// End of $categories foreach
5c11b818 259
d0c33889 260 } else {
5c11b818 261
262 notify($strnofields);
d0c33889 263
264 } /// End of $categories if
d0c33889 265
266
267 echo '<hr />';
dbb3ecdb 268 echo '<div class="profileeditor">';
d0c33889 269
270 /// Create a new field link
5c11b818 271 $options = profile_list_datatypes();
272 popup_form($CFG->wwwroot.'/user/profile/index.php?id=0&amp;action=editfield&amp;type=', $options, 'newfieldform','','choose','','',false,'self',$strcreatefield);
d0c33889 273
274/// Create a new category link
275 $options = array('action'=>'editcategory', 'id'=>'0');
276 print_single_button('index.php',$options,get_string('profilecreatecategory', 'admin'));
277
dbb3ecdb 278 echo '</div>';
d0c33889 279}
280
281admin_externalpage_print_footer($adminroot);
282
283
284
285/***** Some functions relevant to this script *****/
286
287/**
288 * Create a string containing the editing icons for the user profile categories
289 * @param object the category object
290 * @return string the icon string
291 */
292function profile_category_icons ($category) {
293 global $CFG, $USER;
294
295 $str->delete = get_string("delete");
296 $str->moveup = get_string("moveup");
297 $str->movedown = get_string("movedown");
298 $str->edit = get_string("edit");
299
300 $editstr = '';
2cf089c9 301 $categorycount = count_records('user_info_category');
d0c33889 302 $fieldcount = count_records('user_info_field', 'categoryid', $category->id);
303
304 /// Edit
63dbbf06 305 $editstr .= '<a title="'.$str->edit.'" href="index.php?id='.$category->id.'&amp;action=editcategory&amp;sesskey='.$USER->sesskey.'"><img src="'.$CFG->pixpath.'/t/edit.gif" alt="'.$str->edit.'" class="iconsmall" /></a> ';
d0c33889 306
307 /// Delete
308 /// Can only delete the last category if there are no fields in it
309 if ( ($categorycount > 1) or ($fieldcount == 0) ) {
310 $editstr .= '<a title="'.$str->delete.'" href="index.php?id='.$category->id.'&amp;action=deletecategory&amp;sesskey='.$USER->sesskey;
311 if ($fieldcount == 0) $editstr .= '&amp;confirm=1';
63dbbf06 312 $editstr .= '"><img src="'.$CFG->pixpath.'/t/delete.gif" alt="'.$str->delete.'" class="iconsmall" /></a> ';
d0c33889 313 } else {
63dbbf06 314 $editstr .= '<img src="'.$CFG->pixpath.'/spacer.gif" alt="" class="iconsmall" /> ';
d0c33889 315 }
316
317 /// Move up
318 if ($category->sortorder > 1) {
63dbbf06 319 $editstr .= '<a title="'.$str->moveup.'" href="index.php?id='.$category->id.'&amp;action=movecategory&amp;dir=up&amp;sesskey='.$USER->sesskey.'"><img src="'.$CFG->pixpath.'/t/up.gif" alt="'.$str->moveup.'" class="iconsmall" /></a> ';
d0c33889 320 } else {
63dbbf06 321 $editstr .= '<img src="'.$CFG->pixpath.'/spacer.gif" alt="" class="iconsmall" /> ';
d0c33889 322 }
323
324 /// Move down
325 if ($category->sortorder < $categorycount) {
63dbbf06 326 $editstr .= '<a title="'.$str->movedown.'" href="index.php?id='.$category->id.'&amp;action=movecategory&amp;dir=down&amp;sesskey='.$USER->sesskey.'"><img src="'.$CFG->pixpath.'/t/down.gif" alt="'.$str->movedown.'" class="iconsmall" /></a> ';
d0c33889 327 } else {
63dbbf06 328 $editstr .= '<img src="'.$CFG->pixpath.'/spacer.gif" alt="" class="iconsmall" /> ';
d0c33889 329 }
330
331
332 return $editstr;
333}
334
335/**
336 * Create a string containing the editing icons for the user profile fields
337 * @param object the field object
338 * @return string the icon string
339 */
340function profile_field_icons ($field) {
341 global $CFG, $USER;
342
343 if (empty($str)) {
344 $str->delete = get_string("delete");
345 $str->moveup = get_string("moveup");
346 $str->movedown = get_string("movedown");
347 $str->edit = get_string("edit");
348 }
349
350 $editstr = '';
351 $fieldcount = count_records('user_info_field', 'categoryid',$field->categoryid);
352 $datacount = count_records('user_info_data', 'fieldid', $field->id);
353
354 /// Edit
63dbbf06 355 $editstr .= '<a title="'.$str->edit.'" href="index.php?id='.$field->id.'&amp;action=editfield&amp;sesskey='.$USER->sesskey.'"><img src="'.$CFG->pixpath.'/t/edit.gif" alt="'.$str->edit.'" class="iconsmall" /></a> ';
d0c33889 356
357 /// Delete
358 $editstr .= '<a title="'.$str->delete.'" href="index.php?id='.$field->id.'&amp;action=deletefield&amp;sesskey='.$USER->sesskey;
359 if ($datacount == 0) $editstr .= '&amp;confirm=1'; /// Immediate delete if there is no user data
63dbbf06 360 $editstr .= '"><img src="'.$CFG->pixpath.'/t/delete.gif" alt="'.$str->delete.'" class="iconsmall" /></a> ';
d0c33889 361
362 /// Move up
363 if ($field->sortorder > 1) {
63dbbf06 364 $editstr .= '<a title="'.$str->moveup.'" href="index.php?id='.$field->id.'&amp;action=movefield&amp;dir=up&amp;sesskey='.$USER->sesskey.'"><img src="'.$CFG->pixpath.'/t/up.gif" alt="'.$str->moveup.'" class="iconsmall" /></a> ';
d0c33889 365 } else {
63dbbf06 366 $editstr .= '<img src="'.$CFG->pixpath.'/spacer.gif" alt="" class="iconsmall" /> ';
d0c33889 367 }
368
369 /// Move down
370 if ($field->sortorder < $fieldcount) {
63dbbf06 371 $editstr .= '<a title="'.$str->movedown.'" href="index.php?id='.$field->id.'&amp;action=movefield&amp;dir=down&amp;sesskey='.$USER->sesskey.'"><img src="'.$CFG->pixpath.'/t/down.gif" alt="'.$str->movedown.'" class="iconsmall" /></a> ';
d0c33889 372 } else {
63dbbf06 373 $editstr .= '<img src="'.$CFG->pixpath.'/spacer.gif" alt="" class="iconsmall" /> ';
d0c33889 374 }
375
376 return $editstr;
377}
378
379
380?>