admin MDL-23259 made the add user profile field/add category UI a little more logical...
[moodle.git] / user / profile / index.php
CommitLineData
8bdc9cac 1<?php
d0c33889 2
3require('../../config.php');
d0c33889 4require_once($CFG->libdir.'/adminlib.php');
bb6d3d34 5require_once($CFG->dirroot.'/user/profile/lib.php');
6require_once($CFG->dirroot.'/user/profile/definelib.php');
d0c33889 7
1ae083e4 8admin_externalpage_setup('profilefields');
d0c33889 9
d0c33889 10$action = optional_param('action', '', PARAM_ALPHA);
d0c33889 11
bb6d3d34 12$redirect = $CFG->wwwroot.'/user/profile/index.php';
5c11b818 13
14$strchangessaved = get_string('changessaved');
15$strcancelled = get_string('cancelled');
16$strdefaultcategory = get_string('profiledefaultcategory', 'admin');
17$strnofields = get_string('profilenofieldsdefined', 'admin');
18$strcreatefield = get_string('profilecreatefield', 'admin');
d0c33889 19
20
21/// Do we have any actions to perform before printing the header
22
23switch ($action) {
24 case 'movecategory':
bb6d3d34 25 $id = required_param('id', PARAM_INT);
26 $dir = required_param('dir', PARAM_ALPHA);
27
d0c33889 28 if (confirm_sesskey()) {
29 profile_move_category($id, $dir);
30 }
31 redirect($redirect);
bb6d3d34 32 break;
d0c33889 33 case 'movefield':
bb6d3d34 34 $id = required_param('id', PARAM_INT);
35 $dir = required_param('dir', PARAM_ALPHA);
36
d0c33889 37 if (confirm_sesskey()) {
38 profile_move_field($id, $dir);
39 }
40 redirect($redirect);
d0c33889 41 break;
42 case 'deletecategory':
bb6d3d34 43 $id = required_param('id', PARAM_INT);
44 $confirm = optional_param('confirm', 0, PARAM_BOOL);
45
46 if (data_submitted() and $confirm and confirm_sesskey()) {
47 profile_delete_category($id);
d0c33889 48 redirect($redirect);
d0c33889 49 }
bb6d3d34 50
51 //ask for confirmation
c7da4357 52 $fieldcount = $DB->count_records('user_info_field', array('categoryid'=>$id));
bb6d3d34 53 $optionsyes = array ('id'=>$id, 'confirm'=>1, 'action'=>'deletecategory', 'sesskey'=>sesskey());
61ef8f9f 54 echo $OUTPUT->header();
d354ba09 55 echo $OUTPUT->heading(get_string('profiledeletecategory', 'admin'));
aa6c1ced 56
dc6896ef
PS
57 $formcontinue = new single_button(new moodle_url($redirect, $optionsyes), get_string('yes'), 'post');
58 $formcancel = new single_button($redirect, get_string('no'), 'get');
f2f085ee 59 echo $OUTPUT->confirm(get_string('profileconfirmcategorydeletion', 'admin', $fieldcount), $formcontinue, $formcancel);
f24ca3ce 60 echo $OUTPUT->footer();
bb6d3d34 61 die;
62 break;
d0c33889 63 case 'deletefield':
bb6d3d34 64 $id = required_param('id', PARAM_INT);
65 $confirm = optional_param('confirm', 0, PARAM_BOOL);
66
67 if (data_submitted() and $confirm and confirm_sesskey()) {
68 profile_delete_field($id);
d0c33889 69 redirect($redirect);
d0c33889 70 }
bb6d3d34 71
72 //ask for confirmation
c7da4357 73 $datacount = $DB->count_records('user_info_data', array('fieldid'=>$id));
bb6d3d34 74 $optionsyes = array ('id'=>$id, 'confirm'=>1, 'action'=>'deletefield', 'sesskey'=>sesskey());
8bdc9cac
SH
75 $strheading = get_string('profiledeletefield', 'admin');
76 $PAGE->navbar->add($strheading);
61ef8f9f 77 echo $OUTPUT->header();
8bdc9cac 78 echo $OUTPUT->heading($strheading);
dc6896ef 79 $formcontinue = new single_button(new moodle_url($redirect, $optionsyes), get_string('yes'), 'post');
a6bfa66c 80 $formcancel = new single_button(new moodle_url($redirect), get_string('no'), 'get');
f2f085ee 81 echo $OUTPUT->confirm(get_string('profileconfirmfielddeletion', 'admin', $datacount), $formcontinue, $formcancel);
f24ca3ce 82 echo $OUTPUT->footer();
bb6d3d34 83 die;
d0c33889 84 break;
85 case 'editfield':
bb6d3d34 86 $id = optional_param('id', 0, PARAM_INT);
87 $datatype = optional_param('datatype', '', PARAM_ALPHA);
88
1ae083e4 89 profile_edit_field($id, $datatype, $redirect);
bb6d3d34 90 die;
d0c33889 91 break;
92 case 'editcategory':
bb6d3d34 93 $id = optional_param('id', 0, PARAM_INT);
94
1ae083e4 95 profile_edit_category($id, $redirect);
bb6d3d34 96 die;
d0c33889 97 break;
98 default:
bb6d3d34 99 //normal form
d0c33889 100}
101
bb6d3d34 102/// Print the header
61ef8f9f 103echo $OUTPUT->header();
f24ca3ce 104echo $OUTPUT->heading(get_string('profilefields', 'admin'));
bb6d3d34 105
106/// Check that we have at least one category defined
1829e015 107if ($DB->count_records('user_info_category') == 0) {
bb6d3d34 108 $defaultcategory = new object();
109 $defaultcategory->name = $strdefaultcategory;
110 $defaultcategory->sortorder = 1;
1829e015 111 $DB->insert_record('user_info_category', $defaultcategory);
bb6d3d34 112 redirect($redirect);
113}
d0c33889 114
bb6d3d34 115/// Show all categories
5d910388 116$categories = $DB->get_records('user_info_category', null, 'sortorder ASC');
d0c33889 117
bb6d3d34 118foreach ($categories as $category) {
f2f085ee 119 $table = new html_table();
bb6d3d34 120 $table->head = array(get_string('profilefield', 'admin'), get_string('edit'));
121 $table->align = array('left', 'right');
122 $table->width = '95%';
16be8974 123 $table->attributes['class'] = 'generaltable profilefield';
bb6d3d34 124 $table->data = array();
d0c33889 125
5d910388 126 if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) {
bb6d3d34 127 foreach ($fields as $field) {
2ecf267c 128 $table->data[] = array(format_string($field->name), profile_field_icons($field));
d0c33889 129 }
130 }
131
f24ca3ce 132 echo $OUTPUT->heading(format_string($category->name) .' '.profile_category_icons($category));
bb6d3d34 133 if (count($table->data)) {
16be8974 134 echo html_writer::table($table);
d0c33889 135 } else {
3aac07d8 136 echo $OUTPUT->notification($strnofields);
5c11b818 137 }
138
bb6d3d34 139} /// End of $categories foreach
5c11b818 140
5c11b818 141
d0c33889 142
143
bb6d3d34 144echo '<hr />';
145echo '<div class="profileeditor">';
d0c33889 146
bb6d3d34 147/// Create a new field link
148$options = profile_list_datatypes();
f8dab966
PS
149$popupurl = new moodle_url('/user/profile/index.php?id=0&action=editfield');
150echo $OUTPUT->single_select($popupurl, 'datatype', $options, '', array(''=>$strcreatefield), 'newfieldform');
d0c33889 151
d203d08f
AD
152//add a div with a class so themers can hide, style or reposition the text
153html_writer::start_tag('div',array('class'=>'adminuseractionhint'));
154echo get_string('or', 'lesson');
155html_writer::end_tag('div');
156
d0c33889 157/// Create a new category link
bb6d3d34 158$options = array('action'=>'editcategory');
5c2ed7e2 159echo $OUTPUT->single_button(new moodle_url('index.php', $options), get_string('profilecreatecategory', 'admin'));
d0c33889 160
bb6d3d34 161echo '</div>';
d0c33889 162
f24ca3ce 163echo $OUTPUT->footer();
bb6d3d34 164die;
d0c33889 165
166
167/***** Some functions relevant to this script *****/
168
169/**
170 * Create a string containing the editing icons for the user profile categories
171 * @param object the category object
172 * @return string the icon string
173 */
174function profile_category_icons ($category) {
f2a1963c 175 global $CFG, $USER, $DB, $OUTPUT;
d0c33889 176
bb6d3d34 177 $strdelete = get_string('delete');
178 $strmoveup = get_string('moveup');
179 $strmovedown = get_string('movedown');
180 $stredit = get_string('edit');
d0c33889 181
5d910388 182 $categorycount = $DB->count_records('user_info_category');
183 $fieldcount = $DB->count_records('user_info_field', array('categoryid'=>$category->id));
d0c33889 184
185 /// Edit
b5d0cafc 186 $editstr = '<a title="'.$stredit.'" href="index.php?id='.$category->id.'&amp;action=editcategory"><img src="'.$OUTPUT->pix_url('t/edit') . '" alt="'.$stredit.'" class="iconsmall" /></a> ';
d0c33889 187
188 /// Delete
189 /// Can only delete the last category if there are no fields in it
190 if ( ($categorycount > 1) or ($fieldcount == 0) ) {
bb6d3d34 191 $editstr .= '<a title="'.$strdelete.'" href="index.php?id='.$category->id.'&amp;action=deletecategory';
b5d0cafc 192 $editstr .= '"><img src="'.$OUTPUT->pix_url('t/delete') . '" alt="'.$strdelete.'" class="iconsmall" /></a> ';
d0c33889 193 } else {
b5d0cafc 194 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 195 }
196
197 /// Move up
198 if ($category->sortorder > 1) {
b5d0cafc 199 $editstr .= '<a title="'.$strmoveup.'" href="index.php?id='.$category->id.'&amp;action=movecategory&amp;dir=up&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/up') . '" alt="'.$strmoveup.'" class="iconsmall" /></a> ';
d0c33889 200 } else {
b5d0cafc 201 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 202 }
203
204 /// Move down
205 if ($category->sortorder < $categorycount) {
b5d0cafc 206 $editstr .= '<a title="'.$strmovedown.'" href="index.php?id='.$category->id.'&amp;action=movecategory&amp;dir=down&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/down') . '" alt="'.$strmovedown.'" class="iconsmall" /></a> ';
d0c33889 207 } else {
b5d0cafc 208 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 209 }
d0c33889 210
211 return $editstr;
212}
213
214/**
215 * Create a string containing the editing icons for the user profile fields
216 * @param object the field object
217 * @return string the icon string
218 */
5d910388 219function profile_field_icons($field) {
f2a1963c 220 global $CFG, $USER, $DB, $OUTPUT;
d0c33889 221
222 if (empty($str)) {
bb6d3d34 223 $strdelete = get_string('delete');
224 $strmoveup = get_string('moveup');
225 $strmovedown = get_string('movedown');
226 $stredit = get_string('edit');
d0c33889 227 }
228
5d910388 229 $fieldcount = $DB->count_records('user_info_field', array('categoryid'=>$field->categoryid));
230 $datacount = $DB->count_records('user_info_data', array('fieldid'=>$field->id));
d0c33889 231
232 /// Edit
b5d0cafc 233 $editstr = '<a title="'.$stredit.'" href="index.php?id='.$field->id.'&amp;action=editfield"><img src="'.$OUTPUT->pix_url('t/edit') . '" alt="'.$stredit.'" class="iconsmall" /></a> ';
d0c33889 234
235 /// Delete
bb6d3d34 236 $editstr .= '<a title="'.$strdelete.'" href="index.php?id='.$field->id.'&amp;action=deletefield';
b5d0cafc 237 $editstr .= '"><img src="'.$OUTPUT->pix_url('t/delete') . '" alt="'.$strdelete.'" class="iconsmall" /></a> ';
d0c33889 238
239 /// Move up
240 if ($field->sortorder > 1) {
b5d0cafc 241 $editstr .= '<a title="'.$strmoveup.'" href="index.php?id='.$field->id.'&amp;action=movefield&amp;dir=up&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/up') . '" alt="'.$strmoveup.'" class="iconsmall" /></a> ';
d0c33889 242 } else {
b5d0cafc 243 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 244 }
245
246 /// Move down
247 if ($field->sortorder < $fieldcount) {
b5d0cafc 248 $editstr .= '<a title="'.$strmovedown.'" href="index.php?id='.$field->id.'&amp;action=movefield&amp;dir=down&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/down') . '" alt="'.$strmovedown.'" class="iconsmall" /></a> ';
d0c33889 249 } else {
b5d0cafc 250 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 251 }
252
253 return $editstr;
254}
255
256
aa6c1ced 257