3 require('../../config.php');
4 require_once($CFG->libdir.'/adminlib.php');
5 require_once($CFG->dirroot.'/user/profile/lib.php');
6 require_once($CFG->dirroot.'/user/profile/definelib.php');
8 admin_externalpage_setup('profilefields');
10 $action = optional_param('action', '', PARAM_ALPHA);
12 $redirect = $CFG->wwwroot.'/user/profile/index.php';
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');
21 /// Do we have any actions to perform before printing the header
25 $id = required_param('id', PARAM_INT);
26 $dir = required_param('dir', PARAM_ALPHA);
28 if (confirm_sesskey()) {
29 profile_move_category($id, $dir);
34 $id = required_param('id', PARAM_INT);
35 $dir = required_param('dir', PARAM_ALPHA);
37 if (confirm_sesskey()) {
38 profile_move_field($id, $dir);
42 case 'deletecategory':
43 $id = required_param('id', PARAM_INT);
44 $confirm = optional_param('confirm', 0, PARAM_BOOL);
46 if (data_submitted() and $confirm and confirm_sesskey()) {
47 profile_delete_category($id);
51 //ask for confirmation
52 $fieldcount = $DB->count_records('user_info_field', array('categoryid'=>$id));
53 $optionsyes = array ('id'=>$id, 'confirm'=>1, 'action'=>'deletecategory', 'sesskey'=>sesskey());
54 echo $OUTPUT->header();
55 echo $OUTPUT->heading(get_string('profiledeletecategory', 'admin'));
57 $formcontinue = new single_button(new moodle_url($redirect, $optionsyes), get_string('yes'), 'post');
58 $formcancel = new single_button($redirect, get_string('no'), 'get');
59 echo $OUTPUT->confirm(get_string('profileconfirmcategorydeletion', 'admin', $fieldcount), $formcontinue, $formcancel);
60 echo $OUTPUT->footer();
64 $id = required_param('id', PARAM_INT);
65 $confirm = optional_param('confirm', 0, PARAM_BOOL);
67 if (data_submitted() and $confirm and confirm_sesskey()) {
68 profile_delete_field($id);
72 //ask for confirmation
73 $datacount = $DB->count_records('user_info_data', array('fieldid'=>$id));
74 $optionsyes = array ('id'=>$id, 'confirm'=>1, 'action'=>'deletefield', 'sesskey'=>sesskey());
75 $strheading = get_string('profiledeletefield', 'admin');
76 $PAGE->navbar->add($strheading);
77 echo $OUTPUT->header();
78 echo $OUTPUT->heading($strheading);
79 $formcontinue = new single_button(new moodle_url($redirect, $optionsyes), get_string('yes'), 'post');
80 $formcancel = new single_button(new moodle_url($redirect), get_string('no'), 'get');
81 echo $OUTPUT->confirm(get_string('profileconfirmfielddeletion', 'admin', $datacount), $formcontinue, $formcancel);
82 echo $OUTPUT->footer();
86 $id = optional_param('id', 0, PARAM_INT);
87 $datatype = optional_param('datatype', '', PARAM_ALPHA);
89 profile_edit_field($id, $datatype, $redirect);
93 $id = optional_param('id', 0, PARAM_INT);
95 profile_edit_category($id, $redirect);
103 echo $OUTPUT->header();
104 echo $OUTPUT->heading(get_string('profilefields', 'admin'));
106 /// Check that we have at least one category defined
107 if ($DB->count_records('user_info_category') == 0) {
108 $defaultcategory = new object();
109 $defaultcategory->name = $strdefaultcategory;
110 $defaultcategory->sortorder = 1;
111 $DB->insert_record('user_info_category', $defaultcategory);
115 /// Show all categories
116 $categories = $DB->get_records('user_info_category', null, 'sortorder ASC');
118 foreach ($categories as $category) {
119 $table = new html_table();
120 $table->head = array(get_string('profilefield', 'admin'), get_string('edit'));
121 $table->align = array('left', 'right');
122 $table->width = '95%';
123 $table->attributes['class'] = 'generaltable profilefield';
124 $table->data = array();
126 if ($fields = $DB->get_records('user_info_field', array('categoryid'=>$category->id), 'sortorder ASC')) {
127 foreach ($fields as $field) {
128 $table->data[] = array(format_string($field->name), profile_field_icons($field));
132 echo $OUTPUT->heading(format_string($category->name) .' '.profile_category_icons($category));
133 if (count($table->data)) {
134 echo html_writer::table($table);
136 echo $OUTPUT->notification($strnofields);
139 } /// End of $categories foreach
145 echo '<div class="profileeditor">';
147 /// Create a new field link
148 $options = profile_list_datatypes();
149 $popupurl = new moodle_url('/user/profile/index.php?id=0&action=editfield');
150 echo $OUTPUT->single_select($popupurl, 'datatype', $options, '', array(''=>$strcreatefield), 'newfieldform');
152 //add a div with a class so themers can hide, style or reposition the text
153 html_writer::start_tag('div',array('class'=>'adminuseractionhint'));
154 echo get_string('or', 'lesson');
155 html_writer::end_tag('div');
157 /// Create a new category link
158 $options = array('action'=>'editcategory');
159 echo $OUTPUT->single_button(new moodle_url('index.php', $options), get_string('profilecreatecategory', 'admin'));
163 echo $OUTPUT->footer();
167 /***** Some functions relevant to this script *****/
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
174 function profile_category_icons ($category) {
175 global $CFG, $USER, $DB, $OUTPUT;
177 $strdelete = get_string('delete');
178 $strmoveup = get_string('moveup');
179 $strmovedown = get_string('movedown');
180 $stredit = get_string('edit');
182 $categorycount = $DB->count_records('user_info_category');
183 $fieldcount = $DB->count_records('user_info_field', array('categoryid'=>$category->id));
186 $editstr = '<a title="'.$stredit.'" href="index.php?id='.$category->id.'&action=editcategory"><img src="'.$OUTPUT->pix_url('t/edit') . '" alt="'.$stredit.'" class="iconsmall" /></a> ';
189 /// Can only delete the last category if there are no fields in it
190 if ( ($categorycount > 1) or ($fieldcount == 0) ) {
191 $editstr .= '<a title="'.$strdelete.'" href="index.php?id='.$category->id.'&action=deletecategory';
192 $editstr .= '"><img src="'.$OUTPUT->pix_url('t/delete') . '" alt="'.$strdelete.'" class="iconsmall" /></a> ';
194 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
198 if ($category->sortorder > 1) {
199 $editstr .= '<a title="'.$strmoveup.'" href="index.php?id='.$category->id.'&action=movecategory&dir=up&sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/up') . '" alt="'.$strmoveup.'" class="iconsmall" /></a> ';
201 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
205 if ($category->sortorder < $categorycount) {
206 $editstr .= '<a title="'.$strmovedown.'" href="index.php?id='.$category->id.'&action=movecategory&dir=down&sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/down') . '" alt="'.$strmovedown.'" class="iconsmall" /></a> ';
208 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
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
219 function profile_field_icons($field) {
220 global $CFG, $USER, $DB, $OUTPUT;
223 $strdelete = get_string('delete');
224 $strmoveup = get_string('moveup');
225 $strmovedown = get_string('movedown');
226 $stredit = get_string('edit');
229 $fieldcount = $DB->count_records('user_info_field', array('categoryid'=>$field->categoryid));
230 $datacount = $DB->count_records('user_info_data', array('fieldid'=>$field->id));
233 $editstr = '<a title="'.$stredit.'" href="index.php?id='.$field->id.'&action=editfield"><img src="'.$OUTPUT->pix_url('t/edit') . '" alt="'.$stredit.'" class="iconsmall" /></a> ';
236 $editstr .= '<a title="'.$strdelete.'" href="index.php?id='.$field->id.'&action=deletefield';
237 $editstr .= '"><img src="'.$OUTPUT->pix_url('t/delete') . '" alt="'.$strdelete.'" class="iconsmall" /></a> ';
240 if ($field->sortorder > 1) {
241 $editstr .= '<a title="'.$strmoveup.'" href="index.php?id='.$field->id.'&action=movefield&dir=up&sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/up') . '" alt="'.$strmoveup.'" class="iconsmall" /></a> ';
243 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';
247 if ($field->sortorder < $fieldcount) {
248 $editstr .= '<a title="'.$strmovedown.'" href="index.php?id='.$field->id.'&action=movefield&dir=down&sesskey='.sesskey().'"><img src="'.$OUTPUT->pix_url('t/down') . '" alt="'.$strmovedown.'" class="iconsmall" /></a> ';
250 $editstr .= '<img src="'.$OUTPUT->pix_url('spacer') . '" alt="" class="iconsmall" /> ';