Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 
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');
28
 
29
admin_externalpage_setup('profilefields');
30
 
31
$action   = optional_param('action', '', PARAM_ALPHA);
32
 
33
$redirect = $CFG->wwwroot.'/user/profile/index.php';
34
 
35
$strdefaultcategory = get_string('profiledefaultcategory', 'admin');
36
$strcreatefield     = get_string('profilecreatefield', 'admin');
37
 
38
 
39
// Do we have any actions to perform before printing the header.
40
 
41
switch ($action) {
42
    case 'movecategory':
43
        $id  = required_param('id', PARAM_INT);
44
        $dir = required_param('dir', PARAM_ALPHA);
45
 
46
        if (confirm_sesskey()) {
47
            profile_move_category($id, $dir);
48
        }
49
        redirect($redirect);
50
        break;
51
    case 'movefield':
52
        $id  = required_param('id', PARAM_INT);
53
        $dir = required_param('dir', PARAM_ALPHA);
54
 
55
        if (confirm_sesskey()) {
56
            profile_move_field($id, $dir);
57
        }
58
        redirect($redirect);
59
        break;
60
    case 'deletecategory':
61
        $id      = required_param('id', PARAM_INT);
62
        if (confirm_sesskey()) {
63
            profile_delete_category($id);
64
        }
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);
70
 
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'));
76
        }
77
 
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.
93
}
94
 
95
// Show all categories.
96
$categories = $DB->get_records('user_info_category', null, 'sortorder ASC');
97
 
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);
105
}
106
 
107
// Print the header.
108
echo $OUTPUT->header();
109
echo $OUTPUT->heading(get_string('profilefields', 'admin'));
110
 
111
$outputcategories = [];
112
$options = profile_list_datatypes();
113
 
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);
124
            }
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,
132
            ];
133
        }
134
    }
135
 
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);
144
    }
145
    $menu->attributes['class'] .= ' float-left mr-1';
146
 
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),
157
    ];
158
}
159
 
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)
164
]);
165
 
166
echo $OUTPUT->footer();