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
namespace gradereport_user\output;
18
 
19
use moodle_url;
20
use core_grades\output\general_action_bar;
21
 
22
/**
23
 * Renderable class for the action bar elements in the user report page.
24
 *
25
 * @package    gradereport_user
26
 * @copyright  2022 Mihail Geshoski <mihail@moodle.com>
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
class action_bar extends \core_grades\output\action_bar {
30
 
31
    /** @var int|null $userid The user ID. */
32
    protected $userid;
33
 
34
    /** @var int $userview The user report view mode. */
35
    protected $userview;
36
 
37
    /** @var int|null $currentgroupid The user report view mode. */
38
    protected $currentgroupid;
39
 
1441 ariadna 40
    /** @var string $usersearch String to search matching users. */
41
    protected $usersearch;
42
 
1 efrain 43
    /**
44
     * The class constructor.
45
     *
46
     * @param \context $context The context object.
47
     * @param int $userview The user report view mode.
48
     * @param int|null $userid The user ID or 0 if displaying all users.
49
     * @param int|null $currentgroupid The ID of the current group.
1441 ariadna 50
     * @param string $usersearch String to search matching user.
1 efrain 51
     */
1441 ariadna 52
    public function __construct(
53
        \context $context,
54
        int $userview,
55
        ?int $userid = null,
56
        ?int $currentgroupid = null,
57
        string $usersearch = ''
58
    ) {
1 efrain 59
        parent::__construct($context);
60
        $this->userview = $userview;
61
        $this->userid = $userid;
62
        $this->currentgroupid = $currentgroupid;
1441 ariadna 63
        $this->usersearch = $usersearch;
1 efrain 64
    }
65
 
66
    /**
67
     * Returns the template for the action bar.
68
     *
69
     * @return string
70
     */
71
    public function get_template(): string {
72
        return 'gradereport_user/action_bar';
73
    }
74
 
75
    /**
76
     * Export the data for the mustache template.
77
     *
78
     * @param \renderer_base $output renderer to be used to render the action bar elements.
79
     * @return array
80
     */
81
    public function export_for_template(\renderer_base $output): array {
82
        global $PAGE, $USER;
83
 
84
        // If in the course context, we should display the general navigation selector in gradebook.
85
        $courseid = $this->context->instanceid;
86
        // Get the data used to output the general navigation selector.
87
        $generalnavselector = new general_action_bar($this->context,
88
            new moodle_url('/grade/report/user/index.php', ['id' => $courseid]), 'gradereport', 'user');
89
        $data = $generalnavselector->export_for_template($output);
90
 
91
        // If the user has the capability to view all grades, display the group selector (if applicable), the user selector
92
        // and the view mode selector (if applicable).
93
        if (has_capability('moodle/grade:viewall', $this->context)) {
1441 ariadna 94
            $course = get_course($courseid);
95
            if ($course->groupmode) {
96
                $groupselector = new \core_course\output\actionbar\group_selector($this->context);
97
                $data['groupselector'] = $groupselector->export_for_template($output);
98
            }
99
 
100
            $resetlink = new moodle_url('/grade/report/user/index.php', ['id' => $courseid, 'group' => 0]);
101
            $baseurl = new moodle_url('/grade/report/user/index.php', ['id' => $courseid]);
102
            $PAGE->requires->js_call_amd('gradereport_user/user', 'init', [$baseurl->out(false)]);
103
 
104
            $userselector = new \core_course\output\actionbar\user_selector(
105
                course: $course,
106
                resetlink: $resetlink,
107
                userid: $this->userid,
108
                groupid: $this->currentgroupid,
109
                usersearch: $this->usersearch
110
            );
1 efrain 111
            $data['userselector'] = [
112
                'courseid' => $courseid,
1441 ariadna 113
                'content' => $userselector->export_for_template($output),
1 efrain 114
            ];
115
 
116
            // Do not output the 'view mode' selector when in zero state or when the current user is viewing its own report.
117
            if (!is_null($this->userid) && $USER->id != $this->userid) {
1441 ariadna 118
                $viewasotheruser = new moodle_url('/grade/report/user/index.php',
119
                    ['id' => $courseid, 'userid' => $this->userid, 'userview' => GRADE_REPORT_USER_VIEW_USER]);
120
                $viewasmyself = new moodle_url('/grade/report/user/index.php',
121
                    ['id' => $courseid, 'userid' => $this->userid, 'userview' => GRADE_REPORT_USER_VIEW_SELF]);
122
 
123
                $selectoroptions = [
124
                    $viewasotheruser->out(false) => get_string('otheruser', 'core_grades'),
125
                    $viewasmyself->out(false) => get_string('myself', 'core_grades')
126
                ];
127
 
128
                $selectoractiveurl = $this->userview === GRADE_REPORT_USER_VIEW_USER ? $viewasotheruser : $viewasmyself;
129
 
130
                $viewasselect = new \core\output\select_menu('viewas', $selectoroptions, $selectoractiveurl->out(false));
131
                $viewasselect->set_label(get_string('viewas', 'core_grades'));
132
 
133
                $data['viewasselector'] = $viewasselect->export_for_template($output);
1 efrain 134
            }
135
        }
136
 
137
        return $data;
138
    }
139
}