Proyectos de Subversion Moodle

Rev

Rev 11 | | 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)),
1441 ariadna 124
                            'image' => 't/message',
1 efrain 125
                            'linkattributes' => \core_message\helper::messageuser_link_params($user->id),
126
                            'page' => $this->page
127
                        )
128
                    );
129
 
130
                    if ($USER->id != $user->id) {
1441 ariadna 131
                        $cancreatecontact = \core_message\api::can_create_contact($USER->id, $user->id);
1 efrain 132
                        $iscontact = \core_message\api::is_contact($USER->id, $user->id);
11 efrain 133
                        $isrequested = \core_message\api::get_contact_requests_between_users($USER->id, $user->id);
134
                        $contacturlaction = '';
135
                        $linkattributes = \core_message\helper::togglecontact_link_params(
136
                            $user,
137
                            $iscontact,
138
                            true,
139
                            !empty($isrequested),
140
                        );
141
                        // If the user is not a contact.
142
                        if (!$iscontact) {
143
                            if ($isrequested) {
1441 ariadna 144
                                // Set it to true if a request has been sent.
145
                                $cancreatecontact = true;
146
 
11 efrain 147
                                // We just need the first request.
148
                                $requests = array_shift($isrequested);
149
                                if ($requests->userid == $USER->id) {
150
                                    // If the user has requested to be a contact.
151
                                    $contacttitle = 'contactrequestsent';
152
                                } else {
153
                                    // If the user has been requested to be a contact.
154
                                    $contacttitle = 'waitingforcontactaccept';
155
                                }
156
                                $linkattributes = array_merge($linkattributes, [
157
                                    'class' => 'disabled',
158
                                    'tabindex' => '-1',
159
                                ]);
160
                            } else {
161
                                // If the user is not a contact and has not requested to be a contact.
162
                                $contacttitle = 'addtoyourcontacts';
163
                                $contacturlaction = 'addcontact';
164
                            }
1441 ariadna 165
                            $contactimage = 't/addcontact';
11 efrain 166
                        } else {
167
                            // If the user is a contact.
168
                            $contacttitle = 'removefromyourcontacts';
169
                            $contacturlaction = 'removecontact';
1441 ariadna 170
                            $contactimage = 't/removecontact';
11 efrain 171
                        }
1441 ariadna 172
                        if ($cancreatecontact) {
173
                            $userbuttons['togglecontact'] = array(
1 efrain 174
                                'buttontype' => 'togglecontact',
175
                                'title' => get_string($contacttitle, 'message'),
176
                                'url' => new moodle_url('/message/index.php', array(
177
                                        'user1' => $USER->id,
178
                                        'user2' => $user->id,
179
                                        $contacturlaction => $user->id,
180
                                        'sesskey' => sesskey())
181
                                ),
182
                                'image' => $contactimage,
11 efrain 183
                                'linkattributes' => $linkattributes,
1 efrain 184
                                'page' => $this->page
185
                            );
1441 ariadna 186
                        }
1 efrain 187
                    }
188
 
189
                    $this->page->requires->string_for_js('changesmadereallygoaway', 'moodle');
190
                }
191
            } else {
192
                $heading = null;
193
            }
194
        }
195
 
196
        $prefix = null;
197
        if ($context->contextlevel == CONTEXT_MODULE) {
198
            if ($this->page->course->format === 'singleactivity') {
199
                $heading = format_string($this->page->course->fullname, true, ['context' => $context]);
200
            } else {
201
                $heading = $this->page->cm->get_formatted_name();
202
                $iconurl = $this->page->cm->get_icon_url();
203
                $iconclass = $iconurl->get_param('filtericon') ? '' : 'nofilter';
204
                $iconattrs = [
205
                    'class' => "icon activityicon $iconclass",
206
                    'aria-hidden' => 'true'
207
                ];
208
                $imagedata = html_writer::img($iconurl->out(false), '', $iconattrs);
209
                $purposeclass = plugin_supports('mod', $this->page->activityname, FEATURE_MOD_PURPOSE);
210
                $purposeclass .= ' activityiconcontainer icon-size-6';
211
                $purposeclass .= ' modicon_' . $this->page->activityname;
212
                $isbranded = component_callback('mod_' . $this->page->activityname, 'is_branded', [], false);
213
                $imagedata = html_writer::tag('div', $imagedata, ['class' => $purposeclass . ($isbranded ? ' isbranded' : '')]);
214
                if (!empty($USER->editing)) {
215
                    $prefix = get_string('modulename', $this->page->activityname);
216
                }
217
            }
218
        }
219
 
220
        $contextheader = new \context_header($heading, $headinglevel, $imagedata, $userbuttons, $prefix);
221
        return $this->render($contextheader);
222
    }
223
 
224
    /**
225
     * See if this is the first view of the current cm in the session if it has fake blocks.
226
     *
227
     * (We track up to 100 cms so as not to overflow the session.)
228
     * This is done for drawer regions containing fake blocks so we can show blocks automatically.
229
     *
230
     * @return boolean true if the page has fakeblocks and this is the first visit.
231
     */
232
    public function firstview_fakeblocks(): bool {
233
        global $SESSION;
234
 
235
        $firstview = false;
236
        if ($this->page->cm) {
237
            if (!$this->page->blocks->region_has_fakeblocks('side-pre')) {
238
                return false;
239
            }
240
            if (!property_exists($SESSION, 'firstview_fakeblocks')) {
241
                $SESSION->firstview_fakeblocks = [];
242
            }
243
            if (array_key_exists($this->page->cm->id, $SESSION->firstview_fakeblocks)) {
244
                $firstview = false;
245
            } else {
246
                $SESSION->firstview_fakeblocks[$this->page->cm->id] = true;
247
                $firstview = true;
248
                if (count($SESSION->firstview_fakeblocks) > 100) {
249
                    array_shift($SESSION->firstview_fakeblocks);
250
                }
251
            }
252
        }
253
        return $firstview;
254
    }
255
}