Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
 * Definition of the grade_user_report class is defined
19
 *
20
 * @package gradereport_user
21
 * @copyright 2007 Nicolas Connault
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
use core_user\output\myprofile\tree;
26
 
27
require_once($CFG->dirroot . '/grade/report/lib.php');
28
require_once($CFG->libdir.'/tablelib.php');
29
 
30
define("GRADE_REPORT_USER_HIDE_HIDDEN", 0);
31
define("GRADE_REPORT_USER_HIDE_UNTIL", 1);
32
define("GRADE_REPORT_USER_SHOW_HIDDEN", 2);
33
 
34
define("GRADE_REPORT_USER_VIEW_SELF", 1);
35
define("GRADE_REPORT_USER_VIEW_USER", 2);
36
 
37
function grade_report_user_settings_definition(&$mform) {
38
    global $CFG;
39
 
40
    $options = [
41
        -1 => get_string('default', 'grades'),
42
 
43
        1 => get_string('show')
44
    ];
45
 
46
    if (empty($CFG->grade_report_user_showrank)) {
47
        $options[-1] = get_string('defaultprev', 'grades', $options[0]);
48
    } else {
49
        $options[-1] = get_string('defaultprev', 'grades', $options[1]);
50
    }
51
 
52
    $mform->addElement('select', 'report_user_showrank', get_string('showrank', 'grades'), $options);
53
    $mform->addHelpButton('report_user_showrank', 'showrank', 'grades');
54
 
55
    if (empty($CFG->grade_report_user_showpercentage)) {
56
        $options[-1] = get_string('defaultprev', 'grades', $options[0]);
57
    } else {
58
        $options[-1] = get_string('defaultprev', 'grades', $options[1]);
59
    }
60
 
61
    $mform->addElement('select', 'report_user_showpercentage', get_string('showpercentage', 'grades'), $options);
62
    $mform->addHelpButton('report_user_showpercentage', 'showpercentage', 'grades');
63
 
64
    if (empty($CFG->grade_report_user_showgrade)) {
65
        $options[-1] = get_string('defaultprev', 'grades', $options[0]);
66
    } else {
67
        $options[-1] = get_string('defaultprev', 'grades', $options[1]);
68
    }
69
 
70
    $mform->addElement('select', 'report_user_showgrade', get_string('showgrade', 'grades'), $options);
71
 
72
    if (empty($CFG->grade_report_user_showfeedback)) {
73
        $options[-1] = get_string('defaultprev', 'grades', $options[0]);
74
    } else {
75
        $options[-1] = get_string('defaultprev', 'grades', $options[1]);
76
    }
77
 
78
    $mform->addElement('select', 'report_user_showfeedback', get_string('showfeedback', 'grades'), $options);
79
 
80
    if (empty($CFG->grade_report_user_showweight)) {
81
        $options[-1] = get_string('defaultprev', 'grades', $options[0]);
82
    } else {
83
        $options[-1] = get_string('defaultprev', 'grades', $options[1]);
84
    }
85
 
86
    $mform->addElement('select', 'report_user_showweight', get_string('showweight', 'grades'), $options);
87
 
88
    if (empty($CFG->grade_report_user_showaverage)) {
89
        $options[-1] = get_string('defaultprev', 'grades', $options[0]);
90
    } else {
91
        $options[-1] = get_string('defaultprev', 'grades', $options[1]);
92
    }
93
 
94
    $mform->addElement('select', 'report_user_showaverage', get_string('showaverage', 'grades'), $options);
95
    $mform->addHelpButton('report_user_showaverage', 'showaverage', 'grades');
96
 
97
    if (empty($CFG->grade_report_user_showlettergrade)) {
98
        $options[-1] = get_string('defaultprev', 'grades', $options[0]);
99
    } else {
100
        $options[-1] = get_string('defaultprev', 'grades', $options[1]);
101
    }
102
 
103
    $mform->addElement('select', 'report_user_showlettergrade', get_string('showlettergrade', 'grades'), $options);
104
    if (empty($CFG->grade_report_user_showcontributiontocoursetotal)) {
105
        $options[-1] = get_string('defaultprev', 'grades', $options[0]);
106
    } else {
107
        $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_user_showcontributiontocoursetotal]);
108
    }
109
 
110
    $mform->addElement('select', 'report_user_showcontributiontocoursetotal', get_string('showcontributiontocoursetotal', 'grades'), $options);
111
    $mform->addHelpButton('report_user_showcontributiontocoursetotal', 'showcontributiontocoursetotal', 'grades');
112
 
113
    if (empty($CFG->grade_report_user_showrange)) {
114
        $options[-1] = get_string('defaultprev', 'grades', $options[0]);
115
    } else {
116
        $options[-1] = get_string('defaultprev', 'grades', $options[1]);
117
    }
118
 
119
    $mform->addElement('select', 'report_user_showrange', get_string('showrange', 'grades'), $options);
120
 
121
    $options = [
122
 
123
        1 => 1,
124
        2 => 2,
125
        3 => 3,
126
        4 => 4,
127
        5 => 5
128
    ];
129
 
130
    if (!empty($CFG->grade_report_user_rangedecimals)) {
131
        $options[-1] = $options[$CFG->grade_report_user_rangedecimals];
132
    }
133
    $mform->addElement('select', 'report_user_rangedecimals', get_string('rangedecimals', 'grades'), $options);
134
 
135
    $options = [
136
        -1 => get_string('default', 'grades'),
137
 
138
        1 => get_string('showhiddenuntilonly', 'grades'),
139
        2 => get_string('showallhidden', 'grades')
140
    ];
141
 
142
    if (empty($CFG->grade_report_user_showhiddenitems)) {
143
        $options[-1] = get_string('defaultprev', 'grades', $options[0]);
144
    } else {
145
        $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_user_showhiddenitems]);
146
    }
147
 
148
    $mform->addElement('select', 'report_user_showhiddenitems', get_string('showhiddenitems', 'grades'), $options);
149
    $mform->addHelpButton('report_user_showhiddenitems', 'showhiddenitems', 'grades');
150
 
151
    $options = [
152
        -1 => get_string('default', 'grades'),
153
        GRADE_REPORT_HIDE_TOTAL_IF_CONTAINS_HIDDEN => get_string('hide'),
154
        GRADE_REPORT_SHOW_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowexhiddenitems', 'grades'),
155
        GRADE_REPORT_SHOW_REAL_TOTAL_IF_CONTAINS_HIDDEN => get_string('hidetotalshowinchiddenitems', 'grades')
156
    ];
157
 
158
    if (empty($CFG->grade_report_user_showtotalsifcontainhidden)) {
159
        $options[-1] = get_string('defaultprev', 'grades', $options[0]);
160
    } else {
161
        $options[-1] = get_string('defaultprev', 'grades', $options[$CFG->grade_report_user_showtotalsifcontainhidden]);
162
    }
163
 
164
    $mform->addElement('select', 'report_user_showtotalsifcontainhidden', get_string('hidetotalifhiddenitems', 'grades'), $options);
165
    $mform->addHelpButton('report_user_showtotalsifcontainhidden', 'hidetotalifhiddenitems', 'grades');
166
 
167
}
168
 
169
/**
170
 * Profile report callback.
171
 *
172
 * @param object $course The course.
173
 * @param object $user The user.
174
 * @param boolean $viewasuser True when we are viewing this as the targetted user sees it.
175
 */
176
function grade_report_user_profilereport(object $course, object $user, bool $viewasuser = false) {
1441 ariadna 177
    global $OUTPUT;
1 efrain 178
 
1441 ariadna 179
    $taskindicator = new \core\output\task_indicator(
180
        \core_course\task\regrade_final_grades::create($course->id),
181
        get_string('recalculatinggrades', 'grades'),
182
        get_string('recalculatinggradesadhoc', 'grades'),
183
        new \core\url('/course/user.php', ['mode' => 'grade', 'id' => $course->id, 'user' => $user->id]),
184
    );
185
 
186
    if ($taskindicator->has_task_record()) {
187
        echo $OUTPUT->render($taskindicator);
188
    } else if (!empty($course->showgrades)) {
189
 
1 efrain 190
        $context = context_course::instance($course->id);
191
 
192
        // Fetch the return tracking object.
193
        $gpr = new grade_plugin_return(
194
            ['type' => 'report', 'plugin' => 'user', 'courseid' => $course->id, 'userid' => $user->id]
195
        );
196
        // Create a report instance.
197
        $report = new gradereport_user\report\user($course->id, $gpr, $context, $user->id, $viewasuser);
198
 
199
        // Print the page.
200
        // A css fix to share styles with real report page.
201
        echo '<div class="grade-report-user">';
202
        if ($report->fill_table()) {
203
            echo $report->print_table(true);
204
        }
205
        echo '</div>';
206
    }
207
}
208
 
209
/**
210
 * Add nodes to myprofile page.
211
 *
212
 * @param tree $tree Tree object
213
 * @param stdClass $user user object
214
 * @param bool $iscurrentuser
215
 * @param null|stdClass $course Course object
216
 */
217
function gradereport_user_myprofile_navigation(tree $tree, stdClass $user, bool $iscurrentuser, ?stdClass $course) {
218
    if (empty($course)) {
219
        // We want to display these reports under the site context.
220
        $course = get_fast_modinfo(SITEID)->get_course();
221
    }
222
    $usercontext = context_user::instance($user->id);
223
    $anyreport = has_capability('moodle/user:viewuseractivitiesreport', $usercontext);
224
 
225
    // Start capability checks.
226
    if ($anyreport || $iscurrentuser) {
227
        // Add grade hardcoded grade report if necessary.
228
        $gradeaccess = false;
229
        $coursecontext = context_course::instance($course->id);
230
        if (has_capability('moodle/grade:viewall', $coursecontext)) {
231
            // Can view all course grades.
232
            $gradeaccess = true;
233
        } else if ($course->showgrades) {
234
            if ($iscurrentuser && has_capability('moodle/grade:view', $coursecontext)) {
235
                // Can view own grades.
236
                $gradeaccess = true;
237
            } else if (has_capability('moodle/grade:viewall', $usercontext)) {
238
                // Can view grades of this user - parent most probably.
239
                $gradeaccess = true;
240
            } else if ($anyreport) {
241
                // Can view grades of this user - parent most probably.
242
                $gradeaccess = true;
243
            }
244
        }
245
        if ($gradeaccess) {
246
            $url = new moodle_url('/course/user.php', array('mode' => 'grade', 'id' => $course->id, 'user' => $user->id));
247
            $node = new core_user\output\myprofile\node('reports', 'grade', get_string('grades'), null, $url);
248
            $tree->add_node($node);
249
        }
250
    }
251
}
252
 
253
/**
254
 * Returns link to user report for the current element
255
 *
256
 * @param context_course $context Course context
257
 * @param int $courseid Course ID
258
 * @param array  $element An array representing an element in the grade_tree
259
 * @param grade_plugin_return $gpr A grade_plugin_return object
260
 * @param string $mode Mode - gradeitem or user
261
 * @param ?stdClass $templatecontext Template context
262
 * @return stdClass|null
263
 */
264
function gradereport_user_get_report_link(context_course $context, int $courseid, array $element,
265
        grade_plugin_return $gpr, string $mode, ?stdClass $templatecontext): ?stdClass {
266
    global $CFG;
267
 
268
    if ($mode == 'user') {
269
        $reportstring = get_string('userreport_' . $mode, 'gradereport_user');
270
 
271
        if (!isset($templatecontext)) {
272
            $templatecontext = new stdClass();
273
        }
274
 
275
        // FIXME: MDL-52678 This get_capability_info is hacky and we should have an API for inserting grade row links instead.
276
        $canseeuserreport = false;
277
        if (get_capability_info('gradereport/' . $CFG->grade_profilereport . ':view')) {
278
            $canseeuserreport = has_capability('gradereport/' . $CFG->grade_profilereport . ':view', $context);
279
        }
280
 
281
        if ($canseeuserreport) {
282
            $url = new moodle_url('/grade/report/' . $CFG->grade_profilereport . '/index.php',
283
                ['userid' => $element['userid'], 'id' => $courseid]);
284
            $gpr->add_url_params($url);
285
            $templatecontext->reporturl1 = html_writer::link($url, $reportstring,
286
                ['class' => 'dropdown-item', 'aria-label' => $reportstring, 'role' => 'menuitem']);
287
            return $templatecontext;
288
        }
289
    }
290
    return null;
291
}