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.
13//
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/>.
16
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
22 */
23
24require('../../config.php');
25require_once($CFG->libdir.'/adminlib.php');
26require_once($CFG->dirroot.'/user/profile/lib.php');
27require_once($CFG->dirroot.'/user/profile/definelib.php');
28
29admin_externalpage_setup('profilefields');
30
31$action   = optional_param('action', '', PARAM_ALPHA);
32
33$redirect = $CFG->wwwroot.'/user/profile/index.php';
34
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');
40
41
42// Do we have any actions to perform before printing the header.
43
44switch ($action) {
45    case 'movecategory':
46        $id  = required_param('id', PARAM_INT);
47        $dir = required_param('dir', PARAM_ALPHA);
48
49        if (confirm_sesskey()) {
50            profile_move_category($id, $dir);
51        }
52        redirect($redirect);
53        break;
54    case 'movefield':
55        $id  = required_param('id', PARAM_INT);
56        $dir = required_param('dir', PARAM_ALPHA);
57
58        if (confirm_sesskey()) {
59            profile_move_field($id, $dir);
60        }
61        redirect($redirect);
62        break;
63    case 'deletecategory':
64        $id      = required_param('id', PARAM_INT);
65        if (confirm_sesskey()) {
66            profile_delete_category($id);
67        }
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);
73
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'));
79        }
80
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);
97
98        profile_edit_field($id, $datatype, $redirect);
99        die;
100        break;
101    case 'editcategory':
102        $id = optional_param('id', 0, PARAM_INT);
103
104        profile_edit_category($id, $redirect);
105        die;
106        break;
107    default:
108        // Normal form.
109}
110
111// Show all categories.
112$categories = $DB->get_records('user_info_category', null, 'sortorder ASC');
113
114// Check that we have at least one category defined.
115if (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);
121}
122
123// Print the header.
124echo $OUTPUT->header();
125echo $OUTPUT->heading(get_string('profilefields', 'admin'));
126
127foreach ($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();
134
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));
138        }
139    }
140
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);
146    }
147
148} // End of $categories foreach.
149
150echo '<hr />';
151echo '<div class="profileeditor">';
152
153// Create a new field link.
154$options = profile_list_datatypes();
155$popupurl = new moodle_url('/user/profile/index.php?id=0&action=editfield');
156echo $OUTPUT->single_select($popupurl, 'datatype', $options, '', array('' => get_string('choosedots')), 'newfieldform', array('label' => $strcreatefield));
157
158// Add a div with a class so themers can hide, style or reposition the text.
159html_writer::start_tag('div', array('class' => 'adminuseractionhint'));
160echo get_string('or', 'lesson');
161html_writer::end_tag('div');
162
163// Create a new category link.
164$options = array('action' => 'editcategory');
165echo $OUTPUT->single_button(new moodle_url('index.php', $options), get_string('profilecreatecategory', 'admin'));
166
167echo '</div>';
168
169echo $OUTPUT->footer();
170die;
171
172
173/***** Some functions relevant to this script *****/
174
175/**
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
179 */
180function profile_category_icons($category) {
181    global $CFG, $USER, $DB, $OUTPUT;
182
183    $strdelete   = get_string('delete');
184    $strmoveup   = get_string('moveup');
185    $strmovedown = get_string('movedown');
186    $stredit     = get_string('edit');
187
188    $categorycount = $DB->count_records('user_info_category');
189    $fieldcount    = $DB->count_records('user_info_field', array('categoryid' => $category->id));
190
191    // Edit.
192    $editstr = '<a title="'.$stredit.'" href="index.php?id='.$category->id.'&amp;action=editcategory">' .
193               $OUTPUT->pix_icon('t/edit', $stredit) .'</a> ';
194
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() . ' ';
203    }
204
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() . ' ';
212    }
213
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() . ' ';
221    }
222
223    return $editstr;
224}
225
226/**
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
230 */
231function profile_field_icons($field) {
232    global $CFG, $USER, $DB, $OUTPUT;
233
234    $strdelete   = get_string('delete');
235    $strmoveup   = get_string('moveup');
236    $strmovedown = get_string('movedown');
237    $stredit     = get_string('edit');
238
239    $fieldcount = $DB->count_records('user_info_field', array('categoryid' => $field->categoryid));
240    $datacount  = $DB->count_records('user_info_data', array('fieldid' => $field->id));
241
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> ';
245
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> ';
249
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() . ' ';
257    }
258
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() . ' ';
266    }
267
268    return $editstr;
269}
270
271
272
273