MDL-29012 Show which user profile field is being deleted
[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);
3cb462d2 44 profile_delete_category($id);
45 redirect($redirect,get_string('deleted'));
bb6d3d34 46 break;
d0c33889 47 case 'deletefield':
bb6d3d34 48 $id = required_param('id', PARAM_INT);
49 $confirm = optional_param('confirm', 0, PARAM_BOOL);
50
bd08252b 51 $datacount = $DB->count_records('user_info_data', array('fieldid'=>$id));
e2690844 52 if (data_submitted() and ($confirm and confirm_sesskey()) or $datacount===0) {
bb6d3d34 53 profile_delete_field($id);
bd08252b 54 redirect($redirect,get_string('deleted'));
d0c33889 55 }
bb6d3d34 56
57 //ask for confirmation
192a8aef 58 $fieldname = $DB->get_field('user_info_field', 'name', array('id'=>$id));
bb6d3d34 59 $optionsyes = array ('id'=>$id, 'confirm'=>1, 'action'=>'deletefield', 'sesskey'=>sesskey());
192a8aef 60 $strheading = get_string('profiledeletefield', 'admin', $fieldname);
8bdc9cac 61 $PAGE->navbar->add($strheading);
61ef8f9f 62 echo $OUTPUT->header();
8bdc9cac 63 echo $OUTPUT->heading($strheading);
dc6896ef 64 $formcontinue = new single_button(new moodle_url($redirect, $optionsyes), get_string('yes'), 'post');
a6bfa66c 65 $formcancel = new single_button(new moodle_url($redirect), get_string('no'), 'get');
f2f085ee 66 echo $OUTPUT->confirm(get_string('profileconfirmfielddeletion', 'admin', $datacount), $formcontinue, $formcancel);
f24ca3ce 67 echo $OUTPUT->footer();
bb6d3d34 68 die;
d0c33889 69 break;
70 case 'editfield':
bb6d3d34 71 $id = optional_param('id', 0, PARAM_INT);
72 $datatype = optional_param('datatype', '', PARAM_ALPHA);
73
1ae083e4 74 profile_edit_field($id, $datatype, $redirect);
bb6d3d34 75 die;
d0c33889 76 break;
77 case 'editcategory':
bb6d3d34 78 $id = optional_param('id', 0, PARAM_INT);
79
1ae083e4 80 profile_edit_category($id, $redirect);
bb6d3d34 81 die;
d0c33889 82 break;
83 default:
bb6d3d34 84 //normal form
d0c33889 85}
86
bb6d3d34 87/// Print the header
61ef8f9f 88echo $OUTPUT->header();
f24ca3ce 89echo $OUTPUT->heading(get_string('profilefields', 'admin'));
bb6d3d34 90
91/// Check that we have at least one category defined
1829e015 92if ($DB->count_records('user_info_category') == 0) {
bf718f50 93 $defaultcategory = new stdClass();
bb6d3d34 94 $defaultcategory->name = $strdefaultcategory;
95 $defaultcategory->sortorder = 1;
1829e015 96 $DB->insert_record('user_info_category', $defaultcategory);
bb6d3d34 97 redirect($redirect);
98}
d0c33889 99
bb6d3d34 100/// Show all categories
5d910388 101$categories = $DB->get_records('user_info_category', null, 'sortorder ASC');
d0c33889 102
bb6d3d34 103foreach ($categories as $category) {
f2f085ee 104 $table = new html_table();
bb6d3d34 105 $table->head = array(get_string('profilefield', 'admin'), get_string('edit'));
106 $table->align = array('left', 'right');
107 $table->width = '95%';
16be8974 108 $table->attributes['class'] = 'generaltable profilefield';
bb6d3d34 109 $table->data = array();
d0c33889 110
5d910388 111 if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) {
bb6d3d34 112 foreach ($fields as $field) {
2ecf267c 113 $table->data[] = array(format_string($field->name), profile_field_icons($field));
d0c33889 114 }
115 }
116
f24ca3ce 117 echo $OUTPUT->heading(format_string($category->name) .' '.profile_category_icons($category));
bb6d3d34 118 if (count($table->data)) {
16be8974 119 echo html_writer::table($table);
d0c33889 120 } else {
3aac07d8 121 echo $OUTPUT->notification($strnofields);
5c11b818 122 }
123
bb6d3d34 124} /// End of $categories foreach
5c11b818 125
5c11b818 126
d0c33889 127
128
bb6d3d34 129echo '<hr />';
130echo '<div class="profileeditor">';
d0c33889 131
bb6d3d34 132/// Create a new field link
133$options = profile_list_datatypes();
f8dab966
PS
134$popupurl = new moodle_url('/user/profile/index.php?id=0&action=editfield');
135echo $OUTPUT->single_select($popupurl, 'datatype', $options, '', array(''=>$strcreatefield), 'newfieldform');
d0c33889 136
d203d08f
AD
137//add a div with a class so themers can hide, style or reposition the text
138html_writer::start_tag('div',array('class'=>'adminuseractionhint'));
139echo get_string('or', 'lesson');
140html_writer::end_tag('div');
141
d0c33889 142/// Create a new category link
bb6d3d34 143$options = array('action'=>'editcategory');
5c2ed7e2 144echo $OUTPUT->single_button(new moodle_url('index.php', $options), get_string('profilecreatecategory', 'admin'));
d0c33889 145
bb6d3d34 146echo '</div>';
d0c33889 147
f24ca3ce 148echo $OUTPUT->footer();
bb6d3d34 149die;
d0c33889 150
151
152/***** Some functions relevant to this script *****/
153
154/**
155 * Create a string containing the editing icons for the user profile categories
156 * @param object the category object
157 * @return string the icon string
158 */
a1248ca4 159function profile_category_icons($category) {
f2a1963c 160 global $CFG, $USER, $DB, $OUTPUT;
d0c33889 161
bb6d3d34 162 $strdelete = get_string('delete');
163 $strmoveup = get_string('moveup');
164 $strmovedown = get_string('movedown');
165 $stredit = get_string('edit');
d0c33889 166
5d910388 167 $categorycount = $DB->count_records('user_info_category');
168 $fieldcount = $DB->count_records('user_info_field', array('categoryid'=>$category->id));
d0c33889 169
170 /// Edit
b5d0cafc 171 $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 172
173 /// Delete
174 /// Can only delete the last category if there are no fields in it
175 if ( ($categorycount > 1) or ($fieldcount == 0) ) {
bb6d3d34 176 $editstr .= '<a title="'.$strdelete.'" href="index.php?id='.$category->id.'&amp;action=deletecategory';
b5d0cafc 177 $editstr .= '"><img src="'.$OUTPUT->pix_url('t/delete') . '" alt="'.$strdelete.'" class="iconsmall" /></a> ';
d0c33889 178 } else {
b5d0cafc 179 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 180 }
181
182 /// Move up
183 if ($category->sortorder > 1) {
b5d0cafc 184 $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 185 } else {
b5d0cafc 186 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 187 }
188
189 /// Move down
190 if ($category->sortorder < $categorycount) {
b5d0cafc 191 $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 192 } else {
b5d0cafc 193 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 194 }
d0c33889 195
196 return $editstr;
197}
198
199/**
200 * Create a string containing the editing icons for the user profile fields
201 * @param object the field object
202 * @return string the icon string
203 */
5d910388 204function profile_field_icons($field) {
f2a1963c 205 global $CFG, $USER, $DB, $OUTPUT;
d0c33889 206
a8ff9488
PS
207 $strdelete = get_string('delete');
208 $strmoveup = get_string('moveup');
209 $strmovedown = get_string('movedown');
210 $stredit = get_string('edit');
d0c33889 211
5d910388 212 $fieldcount = $DB->count_records('user_info_field', array('categoryid'=>$field->categoryid));
213 $datacount = $DB->count_records('user_info_data', array('fieldid'=>$field->id));
d0c33889 214
215 /// Edit
b5d0cafc 216 $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 217
218 /// Delete
bb6d3d34 219 $editstr .= '<a title="'.$strdelete.'" href="index.php?id='.$field->id.'&amp;action=deletefield';
b5d0cafc 220 $editstr .= '"><img src="'.$OUTPUT->pix_url('t/delete') . '" alt="'.$strdelete.'" class="iconsmall" /></a> ';
d0c33889 221
222 /// Move up
223 if ($field->sortorder > 1) {
b5d0cafc 224 $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 225 } else {
b5d0cafc 226 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 227 }
228
229 /// Move down
230 if ($field->sortorder < $fieldcount) {
b5d0cafc 231 $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 232 } else {
b5d0cafc 233 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 234 }
235
236 return $editstr;
237}
238
239
aa6c1ced 240