d0c33889 |
1 | <?php //$Id$ |
2 | |
3 | require('../../config.php'); |
4 | require_once($CFG->dirroot.'/user/profile/lib.php'); |
5 | require_once($CFG->libdir.'/adminlib.php'); |
6 | |
7 | $adminroot = admin_get_root(); |
8 | admin_externalpage_setup('profilefields', $adminroot); |
9 | |
10 | require_login(); |
11 | |
12 | require_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'; |
23 | $strchangessaved = get_string('changessaved'); |
24 | $strcancelled = get_string('cancelled'); |
25 | |
26 | |
27 | /// Do we have any actions to perform before printing the header |
28 | |
29 | switch ($action) { |
30 | case 'movecategory': |
31 | if (confirm_sesskey()) { |
32 | profile_move_category($id, $dir); |
33 | } |
34 | redirect($redirect); |
35 | exit; |
36 | break; |
37 | case 'movefield': |
38 | if (confirm_sesskey()) { |
39 | profile_move_field($id, $dir); |
40 | } |
41 | redirect($redirect); |
42 | exit; |
43 | break; |
44 | case 'deletecategory': |
45 | if ($confirm and confirm_sesskey()) { |
46 | $categorycount = count_records_select('user_info_category', '1'); |
47 | $fieldcount = count_records('user_info_field', 'categoryid', $id); |
48 | |
49 | /// Can only delete the last category if there are no fields in it |
50 | if ( ($categorycount > 1) or ($fieldcount == 0) ) { |
51 | profile_delete_category($id); |
52 | } |
53 | redirect($redirect); |
54 | exit; |
55 | } |
56 | break; |
57 | case 'deletefield': |
58 | |
59 | if ($confirm and confirm_sesskey()) { |
60 | if ($field = get_record('user_info_field', 'id', $id)) { |
61 | require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php'); |
62 | $newfield = 'profile_field_'.$field->datatype; |
63 | $formfield = new $newfield($field->id); |
64 | $formfield->edit_remove_field(); |
65 | } |
66 | redirect($redirect); |
67 | exit; |
68 | } |
69 | |
70 | break; |
71 | case 'editfield': |
72 | unset($field); |
73 | if ($id == 0) { |
74 | if (!isset($datatypes[$type])) { |
75 | redirect($redirect); |
76 | exit; |
77 | } |
78 | $field->id = 0; |
79 | $field->datatype = $datatypes[$type]; |
80 | $field->categoryid = 0; |
81 | } elseif (!($field = get_record('user_info_field', 'id', $id))) { |
82 | redirect($redirect); |
83 | exit; |
84 | } |
85 | break; |
86 | case 'editcategory': |
87 | unset($category); |
88 | if ($id == 0) { |
89 | $category->id = 0; |
90 | $category->name = ''; |
91 | } elseif (!($category = get_record('user_info_category', 'id', $id))) { |
92 | redirect($redirect); |
93 | exit; |
94 | } |
95 | break; |
96 | default: |
97 | } |
98 | |
99 | |
100 | |
101 | /// Print the header |
102 | admin_externalpage_print_header($adminroot); |
103 | |
104 | print_heading(get_string('profilefields', 'admin')); |
105 | |
106 | |
107 | /// Are we adding or editing a cateogory? |
108 | if ( ($action == 'editcategory' )) { |
109 | require_once('index_category_form.php'); |
110 | $categoryform = new category_form(null, compact('category')); |
111 | if ($categoryform->is_cancelled()) { |
112 | redirect($redirect, $strcancelled); |
113 | } else { |
114 | if ($data = $categoryform->data_submitted()) { |
115 | if ($data->id == 0) { |
116 | unset($data->id); |
117 | $data->sortorder = count_records_select('user_info_category', '1') + 1; |
118 | if (!insert_record('user_info_category', $data, false)) { |
119 | error('There was a problem adding the record to the database'); |
120 | exit; |
121 | } |
122 | } else { |
123 | if (!update_record('user_info_category', $data)) { |
124 | error('There was a problem updating the record in the database'); |
125 | exit; |
126 | } |
127 | } |
128 | redirect($redirect, $strchangessaved); |
129 | } else { |
130 | $categoryform->display(); |
131 | } |
132 | } |
133 | |
134 | /// Are we adding or editing a field? |
135 | } elseif ( $action == 'editfield' ) { |
136 | require_once('index_field_form.php'); |
137 | $fieldform = new field_form(null, compact('field')); |
138 | if ($fieldform->is_cancelled()) { |
139 | redirect($redirect, $strcancelled); |
140 | } else { |
141 | if ($data = $fieldform->data_submitted()) { |
142 | require_once($CFG->dirroot.'/user/profile/field/'.$field->datatype.'/field.class.php'); |
143 | $newfield = 'profile_field_'.$field->datatype; |
144 | $formfield = new $newfield($field->id); |
145 | if (!$formfield->edit_save($data)) { |
146 | error('There was an error updating the database'); |
147 | } else { |
148 | redirect($redirect, $strchangessaved); |
149 | } |
150 | |
151 | } else { |
152 | $fieldform->display(); |
153 | } |
154 | } |
155 | |
156 | /// Deleting a category that has fields in it, print confirm screen? |
157 | } elseif ( ($action == 'deletecategory') and !$confirm ) { |
158 | $fieldcount = count_records('user_info_field', 'categoryid', $id); |
159 | echo '<center>'.get_string('profileconfirmcategorydeletion', 'admin', $fieldcount).'<br /><a href="index.php?id='.$id.'&action=deletecategory&sesskey='.$USER->sesskey.'&confirm=1">'.get_string('yes').' <a href="index.php">'.get_string('no').'</center>'; |
160 | |
161 | |
162 | /// Deleting a field that has user data, print confirm screen |
163 | } elseif ( ($action == 'deletefield') and !$confirm ) { |
164 | $datacount = count_records('user_info_data', 'fieldid', $id); |
165 | echo '<center>'.get_string('profileconfirmfielddeletion', 'admin', $datacount).'<br /><a href="index.php?id='.$id.'&action=deletefield&sesskey='.$USER->sesskey.'&confirm=1">'.get_string('yes').' <a href="index.php">'.get_string('no').'</center>'; |
166 | |
167 | |
168 | |
169 | /// Print the table of categories and fields |
170 | } else { |
171 | |
172 | if ($categories = get_records_select('user_info_category', '1', 'sortorder ASC')) { |
173 | |
174 | unset ($table); |
175 | $table->align = array('left', 'right'); |
176 | $table->width = '95%'; |
177 | $table->data = array(); |
178 | |
179 | foreach ($categories as $category) { |
180 | |
181 | $table->data[] = array($category->name, profile_category_icons($category)); |
182 | |
183 | unset($table2); |
184 | $table2->align = array('left', 'right'); |
185 | $table2->width = '100%'; |
186 | $table2->data = array(); |
187 | |
188 | if ($fields = get_records_select('user_info_field', "categoryid=$category->id", 'sortorder ASC')) { |
189 | foreach ($fields as $field) { |
190 | |
191 | $table2->data[] = array($field->shortname, profile_field_icons($field)); |
192 | |
193 | } /// End of $fields foreach |
194 | } /// End of $fields if |
195 | |
196 | if (!empty($table2->data)) { |
197 | $table->data[] = array('',print_table($table2,true)); |
198 | } |
199 | |
200 | } /// End of $categories foreach |
201 | |
202 | print_table($table); |
203 | |
204 | } else { |
205 | |
206 | notify(get_string('profilenocategoriesdefined', 'admin')); |
207 | |
208 | } /// End of $categories if |
209 | |
210 | |
211 | |
212 | |
213 | echo '<hr />'; |
214 | echo '<center>'; |
215 | |
216 | /// Create a new field link |
217 | if ($categories) { |
218 | $options = profile_list_datatypes(); |
219 | print_string('profilecreatefield', 'admin'); |
220 | popup_form($CFG->wwwroot.'/user/profile/index.php?id=0&action=editfield&type=', $options, 'newfieldform'); |
221 | } |
222 | |
223 | /// Create a new category link |
224 | $options = array('action'=>'editcategory', 'id'=>'0'); |
225 | print_single_button('index.php',$options,get_string('profilecreatecategory', 'admin')); |
226 | |
227 | echo '</center>'; |
228 | } |
229 | |
230 | admin_externalpage_print_footer($adminroot); |
231 | |
232 | |
233 | |
234 | /***** Some functions relevant to this script *****/ |
235 | |
236 | /** |
237 | * Create a string containing the editing icons for the user profile categories |
238 | * @param object the category object |
239 | * @return string the icon string |
240 | */ |
241 | function profile_category_icons ($category) { |
242 | global $CFG, $USER; |
243 | |
244 | $str->delete = get_string("delete"); |
245 | $str->moveup = get_string("moveup"); |
246 | $str->movedown = get_string("movedown"); |
247 | $str->edit = get_string("edit"); |
248 | |
249 | $editstr = ''; |
250 | $categorycount = count_records_select('user_info_category', '1'); |
251 | $fieldcount = count_records('user_info_field', 'categoryid', $category->id); |
252 | |
253 | /// Edit |
254 | $editstr .= '<a title="'.$str->edit.'" href="index.php?id='.$category->id.'&action=editcategory&sesskey='.$USER->sesskey.'"><img src="'.$CFG->pixpath.'/t/edit.gif" height="11" width="11" border="0" alt="'.$str->edit.'" /></a> '; |
255 | |
256 | /// Delete |
257 | /// Can only delete the last category if there are no fields in it |
258 | if ( ($categorycount > 1) or ($fieldcount == 0) ) { |
259 | $editstr .= '<a title="'.$str->delete.'" href="index.php?id='.$category->id.'&action=deletecategory&sesskey='.$USER->sesskey; |
260 | if ($fieldcount == 0) $editstr .= '&confirm=1'; |
261 | $editstr .= '"><img src="'.$CFG->pixpath.'/t/delete.gif" height="11" width="11" border="0" alt="'.$str->delete.'" /></a> '; |
262 | } else { |
263 | $editstr .= '<img src="'.$CFG->pixpath.'/spacer.gif" height="11" width="11" border="0" alt="" /> '; |
264 | } |
265 | |
266 | /// Move up |
267 | if ($category->sortorder > 1) { |
268 | $editstr .= '<a title="'.$str->moveup.'" href="index.php?id='.$category->id.'&action=movecategory&dir=up&sesskey='.$USER->sesskey.'"><img src="'.$CFG->pixpath.'/t/up.gif" height="11" width="11" border="0" alt="'.$str->moveup.'" /></a> '; |
269 | } else { |
270 | $editstr .= '<img src="'.$CFG->pixpath.'/spacer.gif" height="11" width="11" border="0" alt="" /> '; |
271 | } |
272 | |
273 | /// Move down |
274 | if ($category->sortorder < $categorycount) { |
275 | $editstr .= '<a title="'.$str->movedown.'" href="index.php?id='.$category->id.'&action=movecategory&dir=down&sesskey='.$USER->sesskey.'"><img src="'.$CFG->pixpath.'/t/down.gif" height="11" width="11" border="0" alt="'.$str->movedown.'" /></a> '; |
276 | } else { |
277 | $editstr .= '<img src="'.$CFG->pixpath.'/spacer.gif" height="11" width="11" border="0" alt="" /> '; |
278 | } |
279 | |
280 | |
281 | return $editstr; |
282 | } |
283 | |
284 | /** |
285 | * Create a string containing the editing icons for the user profile fields |
286 | * @param object the field object |
287 | * @return string the icon string |
288 | */ |
289 | function profile_field_icons ($field) { |
290 | global $CFG, $USER; |
291 | |
292 | if (empty($str)) { |
293 | $str->delete = get_string("delete"); |
294 | $str->moveup = get_string("moveup"); |
295 | $str->movedown = get_string("movedown"); |
296 | $str->edit = get_string("edit"); |
297 | } |
298 | |
299 | $editstr = ''; |
300 | $fieldcount = count_records('user_info_field', 'categoryid',$field->categoryid); |
301 | $datacount = count_records('user_info_data', 'fieldid', $field->id); |
302 | |
303 | /// Edit |
304 | $editstr .= '<a title="'.$str->edit.'" href="index.php?id='.$field->id.'&action=editfield&sesskey='.$USER->sesskey.'"><img src="'.$CFG->pixpath.'/t/edit.gif" height="11" width="11" border="0" alt="'.$str->edit.'" /></a> '; |
305 | |
306 | /// Delete |
307 | $editstr .= '<a title="'.$str->delete.'" href="index.php?id='.$field->id.'&action=deletefield&sesskey='.$USER->sesskey; |
308 | if ($datacount == 0) $editstr .= '&confirm=1'; /// Immediate delete if there is no user data |
309 | $editstr .= '"><img src="'.$CFG->pixpath.'/t/delete.gif" height="11" width="11" border="0" alt="'.$str->delete.'" /></a> '; |
310 | |
311 | /// Move up |
312 | if ($field->sortorder > 1) { |
313 | $editstr .= '<a title="'.$str->moveup.'" href="index.php?id='.$field->id.'&action=movefield&dir=up&sesskey='.$USER->sesskey.'"><img src="'.$CFG->pixpath.'/t/up.gif" height="11" width="11" border="0" alt="'.$str->moveup.'" /></a> '; |
314 | } else { |
315 | $editstr .= '<img src="'.$CFG->pixpath.'/spacer.gif" height="11" width="11" border="0" alt="" /> '; |
316 | } |
317 | |
318 | /// Move down |
319 | if ($field->sortorder < $fieldcount) { |
320 | $editstr .= '<a title="'.$str->movedown.'" href="index.php?id='.$field->id.'&action=movefield&dir=down&sesskey='.$USER->sesskey.'"><img src="'.$CFG->pixpath.'/t/down.gif" height="11" width="11" border="0" alt="'.$str->movedown.'" /></a> '; |
321 | } else { |
322 | $editstr .= '<img src="'.$CFG->pixpath.'/spacer.gif" height="11" width="11" border="0" alt="" /> '; |
323 | } |
324 | |
325 | return $editstr; |
326 | } |
327 | |
328 | |
329 | ?> |