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
defined('MOODLE_INTERNAL') || die;
18
 
19
use core\output\comboboxsearch;
1441 ariadna 20
use core_grades\output\action_bar;
21
use core_grades\output\penalty_indicator;
1 efrain 22
use core_message\helper;
23
use core_message\api;
24
 
25
/**
26
 * Renderer class for the grade pages.
27
 *
28
 * @package    core_grades
29
 * @copyright  2021 Mihail Geshoski <mihail@moodle.com>
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class core_grades_renderer extends plugin_renderer_base {
33
 
34
    /**
35
     * Renders the action bar for a given page.
36
     *
37
     * @param action_bar $actionbar
38
     * @return string The HTML output
39
     */
40
    public function render_action_bar(action_bar $actionbar): string {
41
        $data = $actionbar->export_for_template($this);
42
        return $this->render_from_template($actionbar->get_template(), $data);
43
    }
44
 
45
    /**
46
     * Renders the group selector trigger element.
47
     *
48
     * @param object $course The course object.
49
     * @param string|null $groupactionbaseurl This parameter has been deprecated since 4.4 and should not be used anymore.
50
     * @return string|null The raw HTML to render.
1441 ariadna 51
     * @deprecated since 4.5. See replacement renderable \core_course\output\actionbar\group_selector instead.
52
     * @todo Final deprecation in Moodle 6.0. See MDL-82116.
1 efrain 53
     */
1441 ariadna 54
    #[\core\attribute\deprecated(
55
        replacement: null,
56
        since: '4.5',
57
        reason: 'See replacement renderable \core_course\output\actionbar\group_selector.'
58
    )]
1 efrain 59
    public function group_selector(object $course, ?string $groupactionbaseurl = null): ?string {
60
        global $USER;
61
 
1441 ariadna 62
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
63
 
1 efrain 64
        if ($groupactionbaseurl !== null) {
65
            debugging(
66
                'The $groupactionbaseurl argument has been deprecated. Please remove it from your method calls.',
67
                DEBUG_DEVELOPER,
68
            );
69
        }
70
        // Make sure that group mode is enabled.
71
        if (!$groupmode = $course->groupmode) {
72
            return null;
73
        }
74
 
75
        $sbody = $this->render_from_template('core_group/comboboxsearch/searchbody', [
76
            'courseid' => $course->id,
77
            'currentvalue' => optional_param('groupsearchvalue', '', PARAM_NOTAGS),
78
            'instance' => rand(),
79
        ]);
80
 
81
        $label = $groupmode == VISIBLEGROUPS ? get_string('selectgroupsvisible') : get_string('selectgroupsseparate');
82
 
83
        $buttondata = ['label' => $label];
84
 
85
        $context = context_course::instance($course->id);
86
 
87
        if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $context)) {
88
            $allowedgroups = groups_get_all_groups($course->id, 0, $course->defaultgroupingid);
89
        } else {
90
            $allowedgroups = groups_get_all_groups($course->id, $USER->id, $course->defaultgroupingid);
91
        }
92
 
93
        $activegroup = groups_get_course_group($course, true, $allowedgroups);
94
        $buttondata['group'] = $activegroup;
95
 
96
        if ($activegroup) {
97
            $group = groups_get_group($activegroup);
98
            $buttondata['selectedgroup'] = format_string($group->name, true, ['context' => $context]);
99
        } else if ($activegroup === 0) {
100
            $buttondata['selectedgroup'] = get_string('allparticipants');
101
        }
102
 
103
        $groupdropdown = new comboboxsearch(
104
            false,
105
            $this->render_from_template('core_group/comboboxsearch/group_selector', $buttondata),
106
            $sbody,
107
            'group-search',
108
            'groupsearchwidget',
109
            'groupsearchdropdown overflow-auto',
110
            null,
111
            true,
112
            $label,
113
            'group',
114
            $activegroup
115
        );
1441 ariadna 116
        return $this->render($groupdropdown);
1 efrain 117
    }
118
 
119
    /**
120
     * Build the data to render the initials bar filter within the gradebook.
121
     * Using this initials selector means you'll have to retain the use of the templates & JS to handle form submission.
122
     * If a simple redirect on each selection is desired the standard user_search() within the user renderer is what you are after.
123
     *
124
     * @param object $course The course object.
125
     * @param context $context Our current context.
126
     * @param string $slug The slug for the report that called this function.
127
     * @return stdClass The data to output.
1441 ariadna 128
     * @deprecated since 4.5. See replacement renderable \core_course\output\actionbar\initials_selector instead.
129
     * @todo Final deprecation in Moodle 6.0. See MDL-82421.
1 efrain 130
     */
1441 ariadna 131
    #[\core\attribute\deprecated(
132
        replacement: null,
133
        since: '4.5',
134
        reason: 'See replacement renderable \core_course\output\actionbar\initials_selector.'
135
    )]
1 efrain 136
    public function initials_selector(
137
        object $course,
138
        context $context,
139
        string $slug
140
    ): stdClass {
141
        global $SESSION, $COURSE;
1441 ariadna 142
 
143
        \core\deprecation::emit_deprecation([$this, __FUNCTION__]);
1 efrain 144
        // User search.
145
        $searchvalue = optional_param('gpr_search', null, PARAM_NOTAGS);
146
        $userid = optional_param('grp_userid', null, PARAM_INT);
147
        $url = new moodle_url($slug, ['id' => $course->id]);
148
        $firstinitial = $SESSION->gradereport["filterfirstname-{$context->id}"] ?? '';
149
        $lastinitial  = $SESSION->gradereport["filtersurname-{$context->id}"] ?? '';
150
 
151
        $renderer = $this->page->get_renderer('core_user');
152
        $initialsbar = $renderer->partial_user_search($url, $firstinitial, $lastinitial, true);
153
 
154
        $currentfilter = '';
155
        if ($firstinitial !== '' && $lastinitial !== '') {
156
            $currentfilter = get_string('filterbothactive', 'grades', ['first' => $firstinitial, 'last' => $lastinitial]);
157
        } else if ($firstinitial !== '') {
158
            $currentfilter = get_string('filterfirstactive', 'grades', ['first' => $firstinitial]);
159
        } else if ($lastinitial !== '') {
160
            $currentfilter = get_string('filterlastactive', 'grades', ['last' => $lastinitial]);
161
        }
162
 
163
        $this->page->requires->js_call_amd('core_grades/searchwidget/initials', 'init', [$slug, $userid, $searchvalue]);
164
 
165
        $formdata = (object) [
166
            'courseid' => $COURSE->id,
167
            'initialsbars' => $initialsbar,
168
        ];
169
        $dropdowncontent = $this->render_from_template('core_grades/initials_dropdown_form', $formdata);
170
 
171
        return (object) [
172
             'buttoncontent' => $currentfilter !== '' ? $currentfilter : get_string('filterbyname', 'core_grades'),
173
             'buttonheader' => $currentfilter !== '' ? get_string('name') : null,
174
             'dropdowncontent' => $dropdowncontent,
175
        ];
176
    }
177
 
178
    /**
179
     * Creates and renders a custom user heading.
180
     *
181
     * @param stdClass $user The user object.
182
     * @param int $courseid The course ID.
183
     * @param bool $showbuttons Whether to display buttons (message, add to contacts) within the heading.
184
     * @return string The raw HTML to render.
185
     */
186
    public function user_heading(stdClass $user, int $courseid, bool $showbuttons = true): string {
187
        global $USER;
188
 
189
        $headingdata = [
190
            'userprofileurl' => (new moodle_url('/user/view.php', ['id' => $user->id, 'course' => $courseid]))->out(false),
191
            'name' => fullname($user),
192
            'image' => $this->user_picture($user, ['size' => 50, 'link' => false])
193
        ];
194
 
195
        if ($showbuttons) {
196
            // Generate the data for the 'message' button.
197
            $messagelinkattributes = array_map(function($name, $value) {
198
                return ['name' => $name, 'value' => $value];
199
            }, array_keys(helper::messageuser_link_params($user->id)), helper::messageuser_link_params($user->id));
200
            $messagelinkattributes[] = ['name' => 'class', 'value' => 'btn px-0'];
201
 
202
            $headingdata['buttons'][] = [
203
                'title' => get_string('message', 'message'),
204
                'url' => (new moodle_url('/message/index.php', ['id' => $user->id]))->out(false),
205
                'icon' => ['name' => 't/message', 'component' => 'core'],
206
                'linkattributes' => $messagelinkattributes
207
            ];
208
            // Include js for messaging.
209
            helper::messageuser_requirejs();
210
 
211
            if ($USER->id != $user->id) {
212
                // Generate the data for the 'contact' button.
213
                $iscontact = api::is_contact($USER->id, $user->id);
214
                $contacttitle = $iscontact ? 'removefromyourcontacts' : 'addtoyourcontacts';
215
                $contacturlaction = $iscontact ? 'removecontact' : 'addcontact';
216
                $contacticon = $iscontact ? 't/removecontact' : 't/addcontact';
217
 
218
                $togglelinkparams = helper::togglecontact_link_params($user, $iscontact, false);
219
                $togglecontactlinkattributes = array_map(function($name, $value) {
220
                    if ($name === 'class') {
221
                        $value .= ' btn px-0';
222
                    }
223
                    return ['name' => $name, 'value' => $value];
224
                }, array_keys($togglelinkparams), $togglelinkparams);
225
 
226
                $headingdata['buttons'][] = [
227
                    'title' => get_string($contacttitle, 'message'),
228
                    'url' => (new moodle_url('/message/index.php', ['user1' => $USER->id, 'user2' => $user->id,
229
                        $contacturlaction => $user->id, 'sesskey' => sesskey()]))->out(false),
230
                    'icon' => ['name' => $contacticon, 'component' => 'core'],
231
                    'linkattributes' => $togglecontactlinkattributes
232
                ];
233
                // Include js for contact toggle.
234
                helper::togglecontact_requirejs();
235
            }
236
        }
237
 
238
        return $this->render_from_template('core_grades/user_heading', $headingdata);
239
    }
1441 ariadna 240
 
241
    /**
242
     * Renders the penalty indicator.
243
     *
244
     * @param penalty_indicator $penaltyindicator
245
     * @return string The HTML output
246
     */
247
    public function render_penalty_indicator(penalty_indicator $penaltyindicator): string {
248
        $data = $penaltyindicator->export_for_template($this);
249
        return $this->render_from_template($penaltyindicator->get_template(), $data);
250
    }
1 efrain 251
}