Merge branch 'MDL-80819' of https://github.com/stronk7/moodle
[moodle.git] / user / profile / index.php
blob1bb1fad0bd8d9fa8c42cfa353734b7685dfd0fe8
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 $strdefaultcategory = get_string('profiledefaultcategory', 'admin');
36 $strcreatefield = get_string('profilecreatefield', 'admin');
39 // Do we have any actions to perform before printing the header.
41 switch ($action) {
42 case 'movecategory':
43 $id = required_param('id', PARAM_INT);
44 $dir = required_param('dir', PARAM_ALPHA);
46 if (confirm_sesskey()) {
47 profile_move_category($id, $dir);
49 redirect($redirect);
50 break;
51 case 'movefield':
52 $id = required_param('id', PARAM_INT);
53 $dir = required_param('dir', PARAM_ALPHA);
55 if (confirm_sesskey()) {
56 profile_move_field($id, $dir);
58 redirect($redirect);
59 break;
60 case 'deletecategory':
61 $id = required_param('id', PARAM_INT);
62 if (confirm_sesskey()) {
63 profile_delete_category($id);
65 redirect($redirect, get_string('deleted'));
66 break;
67 case 'deletefield':
68 $id = required_param('id', PARAM_INT);
69 $confirm = optional_param('confirm', 0, PARAM_BOOL);
71 // If no userdata for profile than don't show confirmation.
72 $datacount = $DB->count_records('user_info_data', array('fieldid' => $id));
73 if (((data_submitted() and $confirm) or ($datacount === 0)) and confirm_sesskey()) {
74 profile_delete_field($id);
75 redirect($redirect, get_string('deleted'));
78 // Ask for confirmation, as there is user data available for field.
79 $fieldname = $DB->get_field('user_info_field', 'name', array('id' => $id));
80 $optionsyes = array ('id' => $id, 'confirm' => 1, 'action' => 'deletefield', 'sesskey' => sesskey());
81 $strheading = get_string('profiledeletefield', 'admin', format_string($fieldname));
82 $PAGE->navbar->add($strheading);
83 echo $OUTPUT->header();
84 echo $OUTPUT->heading($strheading);
85 $formcontinue = new single_button(new moodle_url($redirect, $optionsyes), get_string('yes'), 'post');
86 $formcancel = new single_button(new moodle_url($redirect), get_string('no'), 'get');
87 echo $OUTPUT->confirm(get_string('profileconfirmfielddeletion', 'admin', $datacount), $formcontinue, $formcancel);
88 echo $OUTPUT->footer();
89 die;
90 break;
91 default:
92 // Normal form.
95 // Show all categories.
96 $categories = $DB->get_records('user_info_category', null, 'sortorder ASC');
98 // Check that we have at least one category defined.
99 if (empty($categories)) {
100 $defaultcategory = new stdClass();
101 $defaultcategory->name = $strdefaultcategory;
102 $defaultcategory->sortorder = 1;
103 $DB->insert_record('user_info_category', $defaultcategory);
104 redirect($redirect);
107 // Print the header.
108 echo $OUTPUT->header();
109 echo $OUTPUT->heading(get_string('profilefields', 'admin'));
111 $outputcategories = [];
112 $options = profile_list_datatypes();
114 foreach ($categories as $category) {
115 // Category fields.
116 $outputfields = [];
117 if ($fields = $DB->get_records('user_info_field', array('categoryid' => $category->id), 'sortorder ASC')) {
118 foreach ($fields as $field) {
119 $fieldname = format_string($field->name);
120 $component = 'profilefield_' . $field->datatype;
121 $classname = "\\$component\\helper";
122 if (class_exists($classname) && method_exists($classname, 'get_fieldname')) {
123 $fieldname = $classname::get_fieldname($field->name);
125 $outputfields[] = [
126 'id' => $field->id,
127 'shortname' => $field->shortname,
128 'datatype' => $field->datatype,
129 'name' => $fieldname,
130 'isfirst' => !count($outputfields),
131 'islast' => count($outputfields) == count($fields) - 1,
136 // Add new field menu.
137 $menu = new \action_menu();
138 $menu->set_menu_trigger($strcreatefield);
139 foreach ($options as $type => $fieldname) {
140 $action = new \action_menu_link_secondary(new \moodle_url('#'), null, $fieldname,
141 ['data-action' => 'createfield', 'data-categoryid' => $category->id, 'data-datatype' => $type,
142 'data-datatypename' => $fieldname]);
143 $menu->add($action);
145 $menu->attributes['class'] .= ' float-left mr-1';
147 // Add category information to the template.
148 $outputcategories[] = [
149 'id' => $category->id,
150 'name' => format_string($category->name),
151 'fields' => $outputfields,
152 'hasfields' => count($outputfields),
153 'isfirst' => !count($outputcategories),
154 'islast' => count($outputcategories) == count($categories) - 1,
155 'candelete' => count($categories) > 1,
156 'addfieldmenu' => $menu->export_for_template($OUTPUT),
160 echo $OUTPUT->render_from_template('core_user/edit_profile_fields', [
161 'categories' => $outputcategories,
162 'sesskey' => sesskey(),
163 'baseurl' => (new moodle_url('/user/profile/index.php'))->out(false)
166 echo $OUTPUT->footer();