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
namespace gradereport_singleview\report;
18
 
19
use context_course;
20
use grade_report;
21
use moodle_url;
22
use renderer_base;
23
use stdClass;
24
 
25
defined('MOODLE_INTERNAL') || die;
26
 
27
require_once($CFG->dirroot . '/grade/report/lib.php');
28
 
29
/**
30
 * This class is the main class that must be implemented by a grade report plugin.
31
 *
32
 * @package   gradereport_singleview
33
 * @copyright 2014 Moodle Pty Ltd (http://moodle.com)
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class singleview extends grade_report {
37
 
38
    /** @var string|null $itemselector The raw HTML of the item selector based on the selected single view item type. */
39
    public ?string $itemselector = null;
40
 
41
    /** @var \gradereport_singleview\local\screen\screen screen type. */
42
    public $screen;
43
 
44
    /**
45
     * Return the list of valid screens, used to validate the input.
46
     *
47
     * @return array List of screens.
48
     */
49
    public static function valid_screens(): array {
50
        // This is a list of all the known classes representing a screen in this plugin.
51
        return ['user', 'select', 'grade', 'user_select', 'grade_select'];
52
    }
53
 
54
    /**
55
     * Process data from a form submission. Delegated to the current screen.
56
     *
57
     * @param array $data The data from the form
58
     * @return array|object List of warnings
59
     */
60
    public function process_data($data) {
61
        if (has_capability('moodle/grade:edit', $this->context)) {
62
            return $this->screen->process($data);
63
        }
64
    }
65
 
66
    /**
67
     * Unused - abstract function declared in the parent class.
68
     *
69
     * @param string $target
70
     * @param string $action
71
     */
72
    public function process_action($target, $action) {
73
    }
74
 
75
    /**
76
     * Constructor for this report. Creates the appropriate screen class based on itemtype.
77
     *
78
     * @param int $courseid The course id.
79
     * @param object $gpr grade plugin return tracking object
80
     * @param context_course $context
81
     * @param string $itemtype Should be user, select or grade
82
     * @param int|null $itemid The id of the user or grade item
83
     * @param string|null $unused Used to be group id but that was removed and this is now unused.
84
     */
85
    public function __construct(
86
        int $courseid,
87
        object $gpr,
88
        context_course $context,
89
        string $itemtype,
90
        ?int $itemid,
91
        ?string $unused = null
92
    ) {
93
        parent::__construct($courseid, $gpr, $context);
94
 
95
        $base = '/grade/report/singleview/index.php';
96
 
97
        $idparams = ['id' => $courseid];
98
 
99
        $this->baseurl = new moodle_url($base, $idparams);
100
 
101
        $this->pbarurl = new moodle_url($base, $idparams + [
102
                'item' => $itemtype,
103
                'itemid' => $itemid
104
            ]);
105
 
106
        //  The setup_group method is used to validate group mode and permissions and define the currentgroup value.
107
        $this->setup_groups();
108
 
109
        if (($itemtype !== 'grade') && ($itemtype !== 'user')) {
110
            $itemid = null;
111
        }
112
 
113
        $this->setup_item_selector($itemtype, $itemid);
114
 
115
        $screenclass = "\\gradereport_singleview\\local\\screen\\{$itemtype}";
116
 
117
        $this->screen = new $screenclass($courseid, $itemid, $this->currentgroup);
118
 
119
        // Load custom or predifined js.
120
        $this->screen->js();
121
    }
122
 
123
    /**
124
     * Build the html for the screen.
125
     * @return string HTML to display
126
     */
127
    public function output(): string {
128
        global $OUTPUT;
129
        return $OUTPUT->container($this->screen->html());
130
    }
131
 
132
    protected function setup_groups() {
133
        parent::setup_groups();
134
        $this->group_selector = static::groups_course_menu($this->course);
135
    }
136
 
137
    /**
138
     * Ideally we should move this function to the base class and call it from the setup_groups in the base class,
139
     * so all reports would automatically use it.
140
     *
141
     * @param stdClass $course
142
     * @return string
143
     */
144
    protected static function groups_course_menu(stdClass $course) {
145
        global $PAGE;
146
 
147
        $renderer = $PAGE->get_renderer('core_grades');
148
        return $renderer->group_selector($course);
149
    }
150
 
151
    /**
152
     * Function used to set the appropriate item selector (raw HTML) based on the selected single view item type.
153
     *
154
     * @param string $itemtype The single view item type.
155
     * @param int|null $itemid The item ID.
156
     */
157
    protected function setup_item_selector(string $itemtype, ?int $itemid) {
158
        global $PAGE;
159
 
160
        $renderer = $PAGE->get_renderer('gradereport_singleview');
161
 
162
        if ($itemtype === 'user' || $itemtype === 'user_select' ) {
163
            $this->itemselector = $renderer->users_selector($this->course, $itemid, $this->currentgroup);
164
        } else if ($itemtype === 'grade' || $itemtype === 'grade_select' ) {
165
            $this->itemselector = $renderer->grade_items_selector($this->course, $itemid);
166
        }
167
    }
168
 
169
    /**
170
     * Adds bulk actions menu.
171
     *
172
     * @param renderer_base $output
173
     * @return string HTML to display
174
     */
175
    public function bulk_actions_menu(renderer_base $output): string {
176
        $options = [
177
            'overrideallgrades' => get_string('overrideallgrades', 'gradereport_singleview'),
178
            'overridenonegrades' => get_string('overridenonegrades', 'gradereport_singleview'),
179
            'excludeallgrades' => get_string('excludeallgrades', 'gradereport_singleview'),
180
            'excludenonegrades' => get_string('excludenonegrades', 'gradereport_singleview'),
181
            'bulklegend' => get_string('bulklegend', 'gradereport_singleview')
182
        ];
183
 
184
        $menu = new \action_menu();
185
        $menu->set_menu_trigger(get_string('actions'), 'text-dark');
186
 
187
        foreach ($options as $type => $option) {
188
            $action = new \action_menu_link_secondary(new \moodle_url('#'), null, $option,
189
                ['data-action' => $type, 'data-role' => 'bulkaction']);
190
            $menu->add($action);
191
        }
192
        $menu->attributes['class'] .= ' float-left my-auto';
193
 
194
        return $output->render($menu);
195
    }
196
}