source: moodle/trunk/fuentes/grade/report/user/index.php @ 136

Last change on this file since 136 was 136, checked in by mabarracus, 3 years ago

Ported code to xenial

File size: 6.7 KB
Line 
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 * The gradebook user report
19 *
20 * @package   gradereport_user
21 * @copyright 2007 Moodle Pty Ltd (http://moodle.com)
22 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25require_once '../../../config.php';
26require_once $CFG->libdir.'/gradelib.php';
27require_once $CFG->dirroot.'/grade/lib.php';
28require_once $CFG->dirroot.'/grade/report/user/lib.php';
29
30$courseid = required_param('id', PARAM_INT);
31$userid   = optional_param('userid', $USER->id, PARAM_INT);
32
33$PAGE->set_url(new moodle_url('/grade/report/user/index.php', array('id'=>$courseid)));
34
35/// basic access checks
36if (!$course = $DB->get_record('course', array('id' => $courseid))) {
37    print_error('nocourseid');
38}
39require_login($course);
40$PAGE->set_pagelayout('report');
41
42$context = context_course::instance($course->id);
43require_capability('gradereport/user:view', $context);
44
45if (empty($userid)) {
46    require_capability('moodle/grade:viewall', $context);
47
48} else {
49    if (!$DB->get_record('user', array('id'=>$userid, 'deleted'=>0)) or isguestuser($userid)) {
50        print_error('invaliduser');
51    }
52}
53
54$access = false;
55if (has_capability('moodle/grade:viewall', $context)) {
56    //ok - can view all course grades
57    $access = true;
58
59} else if ($userid == $USER->id and has_capability('moodle/grade:view', $context) and $course->showgrades) {
60    //ok - can view own grades
61    $access = true;
62
63} else if (has_capability('moodle/grade:viewall', context_user::instance($userid)) and $course->showgrades) {
64    // ok - can view grades of this user- parent most probably
65    $access = true;
66}
67
68if (!$access) {
69    // no access to grades!
70    print_error('nopermissiontoviewgrades', 'error',  $CFG->wwwroot.'/course/view.php?id='.$courseid);
71}
72
73/// return tracking object
74$gpr = new grade_plugin_return(array('type'=>'report', 'plugin'=>'user', 'courseid'=>$courseid, 'userid'=>$userid));
75
76/// last selected report session tracking
77if (!isset($USER->grade_last_report)) {
78    $USER->grade_last_report = array();
79}
80$USER->grade_last_report[$course->id] = 'user';
81
82
83//first make sure we have proper final grades - this must be done before constructing of the grade tree
84grade_regrade_final_grades($courseid);
85
86if (has_capability('moodle/grade:viewall', $context)) { //Teachers will see all student reports
87    $groupmode    = groups_get_course_groupmode($course);   // Groups are being used
88    $currentgroup = groups_get_course_group($course, true);
89
90    if (!$currentgroup) {      // To make some other functions work better later
91        $currentgroup = NULL;
92    }
93
94    $isseparategroups = ($course->groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $context));
95
96    if ($isseparategroups and (!$currentgroup)) {
97        // no separate group access, can view only self
98        $userid = $USER->id;
99        $user_selector = false;
100    } else {
101        $user_selector = true;
102    }
103
104    $defaultgradeshowactiveenrol = !empty($CFG->grade_report_showonlyactiveenrol);
105    $showonlyactiveenrol = get_user_preferences('grade_report_showonlyactiveenrol', $defaultgradeshowactiveenrol);
106    $showonlyactiveenrol = $showonlyactiveenrol || !has_capability('moodle/course:viewsuspendedusers', $context);
107    if (empty($userid)) {
108        $gui = new graded_users_iterator($course, null, $currentgroup);
109        $gui->require_active_enrolment($showonlyactiveenrol);
110        $gui->init();
111        // Add tabs
112        print_grade_page_head($courseid, 'report', 'user');
113        groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
114
115        if ($user_selector) {
116            $renderer = $PAGE->get_renderer('gradereport_user');
117            echo $renderer->graded_users_selector('user', $course, $userid, $currentgroup, true);
118        }
119
120        while ($userdata = $gui->next_user()) {
121            $user = $userdata->user;
122            $report = new grade_report_user($courseid, $gpr, $context, $user->id);
123
124            $studentnamelink = html_writer::link(new moodle_url('/user/view.php', array('id' => $report->user->id, 'course' => $courseid)), fullname($report->user));
125            echo $OUTPUT->heading(get_string('pluginname', 'gradereport_user') . ' - ' . $studentnamelink);
126
127            if ($report->fill_table()) {
128                echo '<br />'.$report->print_table(true);
129            }
130            echo "<p style = 'page-break-after: always;'></p>";
131        }
132        $gui->close();
133    } else { // Only show one user's report
134        $report = new grade_report_user($courseid, $gpr, $context, $userid);
135
136        $studentnamelink = html_writer::link(new moodle_url('/user/view.php', array('id' => $report->user->id, 'course' => $courseid)), fullname($report->user));
137        print_grade_page_head($courseid, 'report', 'user', get_string('pluginname', 'gradereport_user') . ' - ' . $studentnamelink);
138        groups_print_course_menu($course, $gpr->get_return_url('index.php?id='.$courseid, array('userid'=>0)));
139
140        if ($user_selector) {
141            $renderer = $PAGE->get_renderer('gradereport_user');
142            $showallusersoptions = true;
143            echo $renderer->graded_users_selector('user', $course, $userid, $currentgroup, $showallusersoptions);
144        }
145
146        if ($currentgroup and !groups_is_member($currentgroup, $userid)) {
147            echo $OUTPUT->notification(get_string('groupusernotmember', 'error'));
148        } else {
149            if ($report->fill_table()) {
150                echo '<br />'.$report->print_table(true);
151            }
152        }
153    }
154} else { //Students will see just their own report
155
156    // Create a report instance
157    $report = new grade_report_user($courseid, $gpr, $context, $userid);
158
159    // print the page
160    print_grade_page_head($courseid, 'report', 'user', get_string('pluginname', 'gradereport_user'). ' - '.fullname($report->user));
161
162    if ($report->fill_table()) {
163        echo '<br />'.$report->print_table(true);
164    }
165}
166
167$event = \gradereport_user\event\grade_report_viewed::create(
168    array(
169        'context' => $context,
170        'courseid' => $courseid,
171        'relateduserid' => $userid,
172    )
173);
174$event->trigger();
175
176echo $OUTPUT->footer();
Note: See TracBrowser for help on using the repository browser.