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 theme_boost\output;
18
 
19
use moodle_url;
20
use html_writer;
21
use get_string;
22
 
23
defined('MOODLE_INTERNAL') || die;
24
 
25
/**
26
 * Renderers to align Moodle's HTML with that expected by Bootstrap
27
 *
28
 * @package    theme_boost
29
 * @copyright  2012 Bas Brands, www.basbrands.nl
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
32
class core_renderer extends \core_renderer {
33
 
34
    /**
35
     * Returns HTML to display a "Turn editing on/off" button in a form.
36
     *
37
     * @param moodle_url $url The URL + params to send through when clicking the button
38
     * @param string $method
39
     * @return string HTML the button
40
     */
41
    public function edit_button(moodle_url $url, string $method = 'post') {
42
        if ($this->page->theme->haseditswitch) {
43
            return;
44
        }
45
        $url->param('sesskey', sesskey());
46
        if ($this->page->user_is_editing()) {
47
            $url->param('edit', 'off');
48
            $editstring = get_string('turneditingoff');
49
        } else {
50
            $url->param('edit', 'on');
51
            $editstring = get_string('turneditingon');
52
        }
53
        $button = new \single_button($url, $editstring, $method, \single_button::BUTTON_PRIMARY);
54
        return $this->render_single_button($button);
55
    }
56
 
57
    /**
58
     * Renders the "breadcrumb" for all pages in boost.
59
     *
60
     * @return string the HTML for the navbar.
61
     */
62
    public function navbar(): string {
63
        $newnav = new \theme_boost\boostnavbar($this->page);
64
        return $this->render_from_template('core/navbar', $newnav);
65
    }
66
 
67
    /**
68
     * Renders the context header for the page.
69
     *
70
     * @param array $headerinfo Heading information.
71
     * @param int $headinglevel What 'h' level to make the heading.
72
     * @return string A rendered context header.
73
     */
74
    public function context_header($headerinfo = null, $headinglevel = 1): string {
75
        global $DB, $USER, $CFG;
76
        require_once($CFG->dirroot . '/user/lib.php');
77
        $context = $this->page->context;
78
        $heading = null;
79
        $imagedata = null;
80
        $userbuttons = null;
81
 
82
        // Make sure to use the heading if it has been set.
83
        if (isset($headerinfo['heading'])) {
84
            $heading = $headerinfo['heading'];
85
        } else {
86
            $heading = $this->page->heading;
87
        }
88
 
89
        // The user context currently has images and buttons. Other contexts may follow.
90
        if ((isset($headerinfo['user']) || $context->contextlevel == CONTEXT_USER) && $this->page->pagetype !== 'my-index') {
91
            if (isset($headerinfo['user'])) {
92
                $user = $headerinfo['user'];
93
            } else {
94
                // Look up the user information if it is not supplied.
95
                $user = $DB->get_record('user', array('id' => $context->instanceid));
96
            }
97
 
98
            // If the user context is set, then use that for capability checks.
99
            if (isset($headerinfo['usercontext'])) {
100
                $context = $headerinfo['usercontext'];
101
            }
102
 
103
            // Only provide user information if the user is the current user, or a user which the current user can view.
104
            // When checking user_can_view_profile(), either:
105
            // If the page context is course, check the course context (from the page object) or;
106
            // If page context is NOT course, then check across all courses.
107
            $course = ($this->page->context->contextlevel == CONTEXT_COURSE) ? $this->page->course : null;
108
 
109
            if (user_can_view_profile($user, $course)) {
110
                // Use the user's full name if the heading isn't set.
111
                if (empty($heading)) {
112
                    $heading = fullname($user);
113
                }
114
 
115
                $imagedata = $this->user_picture($user, array('size' => 100));
116
 
117
                // Check to see if we should be displaying a message button.
118
                if (!empty($CFG->messaging) && has_capability('moodle/site:sendmessage', $context)) {
119
                    $userbuttons = array(
120
                        'messages' => array(
121
                            'buttontype' => 'message',
122
                            'title' => get_string('message', 'message'),
123
                            'url' => new moodle_url('/message/index.php', array('id' => $user->id)),
124
                            'image' => 'message',
125
                            'linkattributes' => \core_message\helper::messageuser_link_params($user->id),
126
                            'page' => $this->page
127
                        )
128
                    );
129
 
130
                    if ($USER->id != $user->id) {
131
                        $iscontact = \core_message\api::is_contact($USER->id, $user->id);
11 efrain 132
                        $isrequested = \core_message\api::get_contact_requests_between_users($USER->id, $user->id);
133
                        $contacturlaction = '';
134
                        $linkattributes = \core_message\helper::togglecontact_link_params(
135
                            $user,
136
                            $iscontact,
137
                            true,
138
                            !empty($isrequested),
139
                        );
140
                        // If the user is not a contact.
141
                        if (!$iscontact) {
142
                            if ($isrequested) {
143
                                // We just need the first request.
144
                                $requests = array_shift($isrequested);
145
                                if ($requests->userid == $USER->id) {
146
                                    // If the user has requested to be a contact.
147
                                    $contacttitle = 'contactrequestsent';
148
                                } else {
149
                                    // If the user has been requested to be a contact.
150
                                    $contacttitle = 'waitingforcontactaccept';
151
                                }
152
                                $linkattributes = array_merge($linkattributes, [
153
                                    'class' => 'disabled',
154
                                    'tabindex' => '-1',
155
                                ]);
156
                            } else {
157
                                // If the user is not a contact and has not requested to be a contact.
158
                                $contacttitle = 'addtoyourcontacts';
159
                                $contacturlaction = 'addcontact';
160
                            }
161
                            $contactimage = 'addcontact';
162
                        } else {
163
                            // If the user is a contact.
164
                            $contacttitle = 'removefromyourcontacts';
165
                            $contacturlaction = 'removecontact';
166
                            $contactimage = 'removecontact';
167
                        }
1 efrain 168
                        $userbuttons['togglecontact'] = array(
169
                                'buttontype' => 'togglecontact',
170
                                'title' => get_string($contacttitle, 'message'),
171
                                'url' => new moodle_url('/message/index.php', array(
172
                                        'user1' => $USER->id,
173
                                        'user2' => $user->id,
174
                                        $contacturlaction => $user->id,
175
                                        'sesskey' => sesskey())
176
                                ),
177
                                'image' => $contactimage,
11 efrain 178
                                'linkattributes' => $linkattributes,
1 efrain 179
                                'page' => $this->page
180
                            );
181
                    }
182
 
183
                    $this->page->requires->string_for_js('changesmadereallygoaway', 'moodle');
184
                }
185
            } else {
186
                $heading = null;
187
            }
188
        }
189
 
190
        $prefix = null;
191
        if ($context->contextlevel == CONTEXT_MODULE) {
192
            if ($this->page->course->format === 'singleactivity') {
193
                $heading = format_string($this->page->course->fullname, true, ['context' => $context]);
194
            } else {
195
                $heading = $this->page->cm->get_formatted_name();
196
                $iconurl = $this->page->cm->get_icon_url();
197
                $iconclass = $iconurl->get_param('filtericon') ? '' : 'nofilter';
198
                $iconattrs = [
199
                    'class' => "icon activityicon $iconclass",
200
                    'aria-hidden' => 'true'
201
                ];
202
                $imagedata = html_writer::img($iconurl->out(false), '', $iconattrs);
203
                $purposeclass = plugin_supports('mod', $this->page->activityname, FEATURE_MOD_PURPOSE);
204
                $purposeclass .= ' activityiconcontainer icon-size-6';
205
                $purposeclass .= ' modicon_' . $this->page->activityname;
206
                $isbranded = component_callback('mod_' . $this->page->activityname, 'is_branded', [], false);
207
                $imagedata = html_writer::tag('div', $imagedata, ['class' => $purposeclass . ($isbranded ? ' isbranded' : '')]);
208
                if (!empty($USER->editing)) {
209
                    $prefix = get_string('modulename', $this->page->activityname);
210
                }
211
            }
212
        }
213
 
214
        // Return the heading wrapped in an sr-only element so it is only visible to screen-readers.
215
        if (!empty($this->page->layout_options['nocontextheader'])) {
216
            return html_writer::div($heading, 'sr-only');
217
        }
218
 
219
        $contextheader = new \context_header($heading, $headinglevel, $imagedata, $userbuttons, $prefix);
220
        return $this->render($contextheader);
221
    }
222
 
223
    /**
224
     * See if this is the first view of the current cm in the session if it has fake blocks.
225
     *
226
     * (We track up to 100 cms so as not to overflow the session.)
227
     * This is done for drawer regions containing fake blocks so we can show blocks automatically.
228
     *
229
     * @return boolean true if the page has fakeblocks and this is the first visit.
230
     */
231
    public function firstview_fakeblocks(): bool {
232
        global $SESSION;
233
 
234
        $firstview = false;
235
        if ($this->page->cm) {
236
            if (!$this->page->blocks->region_has_fakeblocks('side-pre')) {
237
                return false;
238
            }
239
            if (!property_exists($SESSION, 'firstview_fakeblocks')) {
240
                $SESSION->firstview_fakeblocks = [];
241
            }
242
            if (array_key_exists($this->page->cm->id, $SESSION->firstview_fakeblocks)) {
243
                $firstview = false;
244
            } else {
245
                $SESSION->firstview_fakeblocks[$this->page->cm->id] = true;
246
                $firstview = true;
247
                if (count($SESSION->firstview_fakeblocks) > 100) {
248
                    array_shift($SESSION->firstview_fakeblocks);
249
                }
250
            }
251
        }
252
        return $firstview;
253
    }
254
}