MDL-20792 renamed moodle_progress_trace to progress_trace too - the general consensus...
[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());
1ae083e4 54 admin_externalpage_print_header();
f24ca3ce 55 echo $OUTPUT->heading('profiledeletecategory', 'admin');
aa6c1ced 56
f2f085ee 57 $formcontinue = html_form::make_button($redirect, $optionsyes, get_string('yes'), 'post');
58 $formcancel = html_form::make_button($redirect, array(), get_string('no'), 'get');
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);
1ae083e4 77 admin_externalpage_print_header();
8bdc9cac 78 echo $OUTPUT->heading($strheading);
f2f085ee 79 $formcontinue = html_form::make_button($redirect, $optionsyes, get_string('yes'), 'post');
80 $formcancel = html_form::make_button($redirect, array(), get_string('no'), 'get');
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
1ae083e4 103admin_externalpage_print_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%';
f2f085ee 123 $table->add_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) {
128 $table->data[] = array($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)) {
f2f085ee 134 echo $OUTPUT->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();
d7270311 149$popupurl = $CFG->wwwroot.'/user/profile/index.php?id=0&action=editfield';
7b1f2c82 150echo $OUTPUT->select(html_select::make_popup_form($popupurl, 'datatype', $options, 'newfieldform', $strcreatefield));
d0c33889 151
152/// Create a new category link
bb6d3d34 153$options = array('action'=>'editcategory');
f2f085ee 154echo $OUTPUT->button(html_form::make_button('index.php', $options, get_string('profilecreatecategory', 'admin')));
d0c33889 155
bb6d3d34 156echo '</div>';
d0c33889 157
f24ca3ce 158echo $OUTPUT->footer();
bb6d3d34 159die;
d0c33889 160
161
162/***** Some functions relevant to this script *****/
163
164/**
165 * Create a string containing the editing icons for the user profile categories
166 * @param object the category object
167 * @return string the icon string
168 */
169function profile_category_icons ($category) {
f2a1963c 170 global $CFG, $USER, $DB, $OUTPUT;
d0c33889 171
bb6d3d34 172 $strdelete = get_string('delete');
173 $strmoveup = get_string('moveup');
174 $strmovedown = get_string('movedown');
175 $stredit = get_string('edit');
d0c33889 176
5d910388 177 $categorycount = $DB->count_records('user_info_category');
178 $fieldcount = $DB->count_records('user_info_field', array('categoryid'=>$category->id));
d0c33889 179
180 /// Edit
f2a1963c 181 $editstr = '<a title="'.$stredit.'" href="index.php?id='.$category->id.'&amp;action=editcategory"><img src="'.$OUTPUT->old_icon_url('t/edit') . '" alt="'.$stredit.'" class="iconsmall" /></a> ';
d0c33889 182
183 /// Delete
184 /// Can only delete the last category if there are no fields in it
185 if ( ($categorycount > 1) or ($fieldcount == 0) ) {
bb6d3d34 186 $editstr .= '<a title="'.$strdelete.'" href="index.php?id='.$category->id.'&amp;action=deletecategory';
f2a1963c 187 $editstr .= '"><img src="'.$OUTPUT->old_icon_url('t/delete') . '" alt="'.$strdelete.'" class="iconsmall" /></a> ';
d0c33889 188 } else {
e63f88c9 189 $editstr .= '<img src="'.$OUTPUT->old_icon_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 190 }
191
192 /// Move up
193 if ($category->sortorder > 1) {
f2a1963c 194 $editstr .= '<a title="'.$strmoveup.'" href="index.php?id='.$category->id.'&amp;action=movecategory&amp;dir=up&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->old_icon_url('t/up') . '" alt="'.$strmoveup.'" class="iconsmall" /></a> ';
d0c33889 195 } else {
e63f88c9 196 $editstr .= '<img src="'.$OUTPUT->old_icon_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 197 }
198
199 /// Move down
200 if ($category->sortorder < $categorycount) {
f2a1963c 201 $editstr .= '<a title="'.$strmovedown.'" href="index.php?id='.$category->id.'&amp;action=movecategory&amp;dir=down&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->old_icon_url('t/down') . '" alt="'.$strmovedown.'" class="iconsmall" /></a> ';
d0c33889 202 } else {
e63f88c9 203 $editstr .= '<img src="'.$OUTPUT->old_icon_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 204 }
d0c33889 205
206 return $editstr;
207}
208
209/**
210 * Create a string containing the editing icons for the user profile fields
211 * @param object the field object
212 * @return string the icon string
213 */
5d910388 214function profile_field_icons($field) {
f2a1963c 215 global $CFG, $USER, $DB, $OUTPUT;
d0c33889 216
217 if (empty($str)) {
bb6d3d34 218 $strdelete = get_string('delete');
219 $strmoveup = get_string('moveup');
220 $strmovedown = get_string('movedown');
221 $stredit = get_string('edit');
d0c33889 222 }
223
5d910388 224 $fieldcount = $DB->count_records('user_info_field', array('categoryid'=>$field->categoryid));
225 $datacount = $DB->count_records('user_info_data', array('fieldid'=>$field->id));
d0c33889 226
227 /// Edit
f2a1963c 228 $editstr = '<a title="'.$stredit.'" href="index.php?id='.$field->id.'&amp;action=editfield"><img src="'.$OUTPUT->old_icon_url('t/edit') . '" alt="'.$stredit.'" class="iconsmall" /></a> ';
d0c33889 229
230 /// Delete
bb6d3d34 231 $editstr .= '<a title="'.$strdelete.'" href="index.php?id='.$field->id.'&amp;action=deletefield';
f2a1963c 232 $editstr .= '"><img src="'.$OUTPUT->old_icon_url('t/delete') . '" alt="'.$strdelete.'" class="iconsmall" /></a> ';
d0c33889 233
234 /// Move up
235 if ($field->sortorder > 1) {
f2a1963c 236 $editstr .= '<a title="'.$strmoveup.'" href="index.php?id='.$field->id.'&amp;action=movefield&amp;dir=up&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->old_icon_url('t/up') . '" alt="'.$strmoveup.'" class="iconsmall" /></a> ';
d0c33889 237 } else {
e63f88c9 238 $editstr .= '<img src="'.$OUTPUT->old_icon_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 239 }
240
241 /// Move down
242 if ($field->sortorder < $fieldcount) {
f2a1963c 243 $editstr .= '<a title="'.$strmovedown.'" href="index.php?id='.$field->id.'&amp;action=movefield&amp;dir=down&amp;sesskey='.sesskey().'"><img src="'.$OUTPUT->old_icon_url('t/down') . '" alt="'.$strmovedown.'" class="iconsmall" /></a> ';
d0c33889 244 } else {
e63f88c9 245 $editstr .= '<img src="'.$OUTPUT->old_icon_url('spacer') . '" alt="" class="iconsmall" /> ';
d0c33889 246 }
247
248 return $editstr;
249}
250
251
aa6c1ced 252