Merge branch 'MDL-66032-MOODLE_37_STABLE_awarded_badges_criteria' of https://github...
[moodle.git] / user / profile / index.php
blob799980c875f92841252afc8f43cdabb1171d8796
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Manage user profile fields.
19 * @package core_user
20 * @copyright 2007 onwards Shane Elliot {@link http://pukunui.com}
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 require('../../config.php');
25 require_once($CFG->libdir.'/adminlib.php');
26 require_once($CFG->dirroot.'/user/profile/lib.php');
27 require_once($CFG->dirroot.'/user/profile/definelib.php');
29 admin_externalpage_setup('profilefields');
31 $action = optional_param('action', '', PARAM_ALPHA);
33 $redirect = $CFG->wwwroot.'/user/profile/index.php';
35 $strchangessaved = get_string('changessaved');
36 $strcancelled = get_string('cancelled');
37 $strdefaultcategory = get_string('profiledefaultcategory', 'admin');
38 $strnofields = get_string('profilenofieldsdefined', 'admin');
39 $strcreatefield = get_string('profilecreatefield', 'admin');
42 // Do we have any actions to perform before printing the header.
44 switch ($action) {
45 case 'movecategory':
46 $id = required_param('id', PARAM_INT);
47 $dir = required_param('dir', PARAM_ALPHA);
49 if (confirm_sesskey()) {
50 profile_move_category($id, $dir);
52 redirect($redirect);
53 break;
54 case 'movefield':
55 $id = required_param('id', PARAM_INT);
56 $dir = required_param('dir', PARAM_ALPHA);
58 if (confirm_sesskey()) {
59 profile_move_field($id, $dir);
61 redirect($redirect);
62 break;
63 case 'deletecategory':
64 $id = required_param('id', PARAM_INT);
65 if (confirm_sesskey()) {
66 profile_delete_category($id);
68 redirect($redirect, get_string('deleted'));
69 break;
70 case 'deletefield':
71 $id = required_param('id', PARAM_INT);
72 $confirm = optional_param('confirm', 0, PARAM_BOOL);
74 // If no userdata for profile than don't show confirmation.
75 $datacount = $DB->count_records('user_info_data', array('fieldid' => $id));
76 if (((data_submitted() and $confirm) or ($datacount === 0)) and confirm_sesskey()) {
77 profile_delete_field($id);
78 redirect($redirect, get_string('deleted'));
81 // Ask for confirmation, as there is user data available for field.
82 $fieldname = $DB->get_field('user_info_field', 'name', array('id' => $id));
83 $optionsyes = array ('id' => $id, 'confirm' => 1, 'action' => 'deletefield', 'sesskey' => sesskey());
84 $strheading = get_string('profiledeletefield', 'admin', format_string($fieldname));
85 $PAGE->navbar->add($strheading);
86 echo $OUTPUT->header();
87 echo $OUTPUT->heading($strheading);
88 $formcontinue = new single_button(new moodle_url($redirect, $optionsyes), get_string('yes'), 'post');
89 $formcancel = new single_button(new moodle_url($redirect), get_string('no'), 'get');
90 echo $OUTPUT->confirm(get_string('profileconfirmfielddeletion', 'admin', $datacount), $formcontinue, $formcancel);
91 echo $OUTPUT->footer();
92 die;
93 break;
94 case 'editfield':
95 $id = optional_param('id', 0, PARAM_INT);
96 $datatype = optional_param('datatype', '', PARAM_ALPHA);
98 profile_edit_field($id, $datatype, $redirect);
99 die;
100 break;
101 case 'editcategory':
102 $id = optional_param('id', 0, PARAM_INT);
104 profile_edit_category($id, $redirect);
105 die;
106 break;
107 default:
108 // Normal form.
111 // Show all categories.
112 $categories = $DB->get_records('user_info_category', null, 'sortorder ASC');
114 // Check that we have at least one category defined.
115 if (empty($categories)) {
116 $defaultcategory = new stdClass();
117 $defaultcategory->name = $strdefaultcategory;
118 $defaultcategory->sortorder = 1;
119 $DB->insert_record('user_info_category', $defaultcategory);
120 redirect($redirect);
123 // Print the header.
124 echo $OUTPUT->header();
125 echo $OUTPUT->heading(get_string('profilefields', 'admin'));
127 foreach ($categories as $category) {
128 $table = new html_table();
129 $table->head = array(get_string('profilefield', 'admin'), get_string('edit'));
130 $table->align = array('left', 'right');
131 $table->width = '95%';
132 $table->attributes['class'] = 'generaltable profilefield';
133 $table->data = array();
135 if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
136 foreach ($fields as $field) {
137 $table->data[] = array(format_string($field->name), profile_field_icons($field));
141 echo $OUTPUT->heading(format_string($category->name) .' '.profile_category_icons($category));
142 if (count($table->data)) {
143 echo html_writer::table($table);
144 } else {
145 echo $OUTPUT->notification($strnofields);
148 } // End of $categories foreach.
150 echo '<hr />';
151 echo '<div class="profileeditor">';
153 // Create a new field link.
154 $options = profile_list_datatypes();
155 $popupurl = new moodle_url('/user/profile/index.php?id=0&action=editfield');
156 echo $OUTPUT->single_select($popupurl, 'datatype', $options, '', array('' => get_string('choosedots')), 'newfieldform', array('label' => $strcreatefield));
158 // Add a div with a class so themers can hide, style or reposition the text.
159 html_writer::start_tag('div', array('class' => 'adminuseractionhint'));
160 echo get_string('or', 'lesson');
161 html_writer::end_tag('div');
163 // Create a new category link.
164 $options = array('action' => 'editcategory');
165 echo $OUTPUT->single_button(new moodle_url('index.php', $options), get_string('profilecreatecategory', 'admin'));
167 echo '</div>';
169 echo $OUTPUT->footer();
170 die;
173 /***** Some functions relevant to this script *****/
176 * Create a string containing the editing icons for the user profile categories
177 * @param stdClass $category the category object
178 * @return string the icon string
180 function profile_category_icons($category) {
181 global $CFG, $USER, $DB, $OUTPUT;
183 $strdelete = get_string('delete');
184 $strmoveup = get_string('moveup');
185 $strmovedown = get_string('movedown');
186 $stredit = get_string('edit');
188 $categorycount = $DB->count_records('user_info_category');
189 $fieldcount = $DB->count_records('user_info_field', array('categoryid' => $category->id));
191 // Edit.
192 $editstr = '<a title="'.$stredit.'" href="index.php?id='.$category->id.'&amp;action=editcategory">' .
193 $OUTPUT->pix_icon('t/edit', $stredit) .'</a> ';
195 // Delete.
196 // Can only delete the last category if there are no fields in it.
197 if (($categorycount > 1) or ($fieldcount == 0)) {
198 $editstr .= '<a title="'.$strdelete.'"';
199 $editstr .= ' href="index.php?id='.$category->id.'&amp;action=deletecategory&amp;sesskey='.sesskey() . '">';
200 $editstr .= $OUTPUT->pix_icon('t/delete', $strdelete).'</a> ';
201 } else {
202 $editstr .= $OUTPUT->spacer() . ' ';
205 // Move up.
206 if ($category->sortorder > 1) {
207 $editstr .= '<a title="'.$strmoveup.'" ';
208 $editstr .= ' href="index.php?id='.$category->id.'&amp;action=movecategory&amp;dir=up&amp;sesskey='.sesskey().'">';
209 $editstr .= $OUTPUT->pix_icon('t/up', $strmoveup) . '</a> ';
210 } else {
211 $editstr .= $OUTPUT->spacer() . ' ';
214 // Move down.
215 if ($category->sortorder < $categorycount) {
216 $editstr .= '<a title="'.$strmovedown.'" ';
217 $editstr .= ' href="index.php?id='.$category->id.'&amp;action=movecategory&amp;dir=down&amp;sesskey='.sesskey().'">';
218 $editstr .= $OUTPUT->pix_icon('t/down', $strmovedown) . '</a> ';
219 } else {
220 $editstr .= $OUTPUT->spacer() . ' ';
223 return $editstr;
227 * Create a string containing the editing icons for the user profile fields
228 * @param stdClass $field the field object
229 * @return string the icon string
231 function profile_field_icons($field) {
232 global $CFG, $USER, $DB, $OUTPUT;
234 $strdelete = get_string('delete');
235 $strmoveup = get_string('moveup');
236 $strmovedown = get_string('movedown');
237 $stredit = get_string('edit');
239 $fieldcount = $DB->count_records('user_info_field', array('categoryid' => $field->categoryid));
240 $datacount = $DB->count_records('user_info_data', array('fieldid' => $field->id));
242 // Edit.
243 $editstr = '<a title="'.$stredit.'" href="index.php?id='.$field->id.'&amp;action=editfield">';
244 $editstr .= $OUTPUT->pix_icon('t/edit', $stredit) . '</a> ';
246 // Delete.
247 $editstr .= '<a title="'.$strdelete.'" href="index.php?id='.$field->id.'&amp;action=deletefield&amp;sesskey='.sesskey().'">';
248 $editstr .= $OUTPUT->pix_icon('t/delete', $strdelete) . '</a> ';
250 // Move up.
251 if ($field->sortorder > 1) {
252 $editstr .= '<a title="'.$strmoveup.'" ';
253 $editstr .= ' href="index.php?id='.$field->id.'&amp;action=movefield&amp;dir=up&amp;sesskey='.sesskey().'">';
254 $editstr .= $OUTPUT->pix_icon('t/up', $strmoveup) . '</a> ';
255 } else {
256 $editstr .= $OUTPUT->spacer() . ' ';
259 // Move down.
260 if ($field->sortorder < $fieldcount) {
261 $editstr .= '<a title="'.$strmovedown.'" ';
262 $editstr .= ' href="index.php?id='.$field->id.'&amp;action=movefield&amp;dir=down&amp;sesskey='.sesskey().'">';
263 $editstr .= $OUTPUT->pix_icon('t/down', $strmovedown) . '</a> ';
264 } else {
265 $editstr .= $OUTPUT->spacer() . ' ';
268 return $editstr;