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
/**
18
 * Supporting infrastructure for the multiblock editing.
19
 *
20
 * @package   block_multiblock
21
 * @copyright 2019 Peter Spicer <peter.spicer@catalyst-eu.net>
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
 
25
namespace block_multiblock;
26
 
27
use block_multiblock\helper;
28
use admin_category;
29
use admin_settingpage;
30
use context_course;
31
use context_coursecat;
32
use context_system;
33
use context_user;
34
use moodle_url;
35
use navigation_node;
36
 
37
/**
38
 * Supporting infrastructure for the multiblock editing.
39
 *
40
 * @package   block_multiblock
41
 * @copyright 2019 Peter Spicer <peter.spicer@catalyst-eu.net>
42
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
43
 */
44
class navigation {
45
 
46
    /**
47
     * While context_block provides getting a given page's URL,
48
     * it is not always 100% consistent or reliable. So, instead,
49
     * we do it ourselves.
50
     *
51
     * @param int $blockid The block's id from mdl_block_instances.
52
     * @return moodle_url The page URL relating to that block.
53
     */
54
    public static function get_page_url($blockid): moodle_url {
55
        global $DB, $CFG;
56
 
57
        $parentcontext = helper::find_nearest_nonblock_ancestor($blockid);
58
        $block = $DB->get_record('block_instances', ['id' => $blockid]);
59
 
60
        // If the block is in a user context, it could well be a dashboard.
61
        if ($parentcontext instanceof context_user) {
62
            if ($block->pagetypepattern == 'my-index') {
63
                return new moodle_url('/my/');
64
            }
65
 
66
            if (strpos($block->pagetypepattern, 'totara-dashboard') !== false) {
67
 
68
                if (preg_match('~^totara-dashboard-(\d+)$~', $block->pagetypepattern, $match)) {
69
                    return new moodle_url('/totara/dashboard/', ['id' => $match[1]]);
70
                }
71
 
72
                return new moodle_url('/totara/dashboard/');
73
            }
74
        }
75
 
76
        // If this is a system context, something really interesting could be happening.
77
        if ($parentcontext instanceof context_system) {
78
            if ($block->pagetypepattern == 'my-index') {
79
                return new moodle_url('/my/indexsys.php');
80
            }
81
            if (strpos($block->pagetypepattern, 'totara-dashboard') !== false) {
82
 
83
                if (preg_match('~^totara-dashboard-(\d+)$~', $block->pagetypepattern, $match)) {
84
                    return new moodle_url('/totara/dashboard/layout.php', ['id' => $match[1]]);
85
                }
86
 
87
                return new moodle_url('/totara/dashboard/layout.php', ['id' => 1]);
88
            }
89
            return static::map_site_context_url($block->pagetypepattern, $parentcontext);
90
        }
91
 
92
        // If this is a course category, we might have the management page.
93
        if ($parentcontext instanceof context_coursecat) {
94
            if (substr($block->pagetypepattern, 0, 17) == 'course-management') {
95
                return new moodle_url('/course/management.php');
96
            }
97
        }
98
 
99
        // If this is a course, we might have to switch between course-view and course-info.
100
        if ($parentcontext instanceof context_course) {
101
            // If this is the site course (home page), we have to get a little fancier.
102
            if ($parentcontext->instanceid == SITEID) {
103
                if (!empty($CFG->defaulthomepage) && ($CFG->defaulthomepage == HOMEPAGE_MY)) {
104
                    return new moodle_url('/', ['redirect' => 0]);
105
                } else {
106
                    return new moodle_url('/');
107
                }
108
            }
109
 
110
            if (substr($block->pagetypepattern, 0, 11) == 'course-info') {
111
                return new moodle_url('/course/info.php', ['id' => $parentcontext->instanceid]);
112
            } else if ($block->pagetypepattern == 'course-edit') {
113
                return new moodle_url('/course/edit.php', ['id' => $parentcontext->instanceid]);
114
            }
115
        }
116
 
117
        return $parentcontext->get_url();
118
    }
119
 
120
    /**
121
     * Maps known page-type patterns to destinations within the site context.
122
     *
123
     * @param string $pagetypepattern The page type pattern the block lists
124
     * @param context $context The parent context
125
     * @return moodle_url The URL to route to
126
     */
127
    public static function map_site_context_url($pagetypepattern, $context): moodle_url {
128
        global $CFG;
129
 
130
        $map = [
131
            'admin-*' => '/admin/search.php',
132
            'my-index' => '/my/indexsys.php',
133
            'site-index' => '/',
134
        ];
135
 
136
        if (isset($map[$pagetypepattern])) {
137
            return new moodle_url($map[$pagetypepattern]);
138
        }
139
 
140
        // Page type admin-setting-x can either be a category or a settings page.
141
        if (preg_match('/^admin-setting-(.*)/i', $pagetypepattern, $match)) {
142
            require_once($CFG->libdir . '/adminlib.php');
143
            navigation_node::require_admin_tree();
144
 
145
            $adminroot = admin_get_root();
146
            $page = $adminroot->locate($match[1], true);
147
 
148
            if ($page instanceof admin_settingpage) {
149
                return new moodle_url('/admin/settings.php', ['section' => $match[1]]);
150
            } else if ($page instanceof admin_category) {
151
                return new moodle_url('/admin/category.php', ['category' => $match[1]]);
152
            } else if ($page instanceof admin_externalpage) {
153
                return new moodle_url($page->url);
154
            }
155
        }
156
 
157
        // Grade editing.
158
        if (strpos($pagetypepattern, 'admin-grade-') === 0) {
159
            // We can convert these from admin-grade-edit-scale-index to grade/edit/scale/index.php.
160
            $parts = explode('-', $pagetypepattern);
161
            array_shift($parts); // Remove the first part, 'admin'.
162
            return new moodle_url('/' . implode('/', $parts) . '.php');
163
        }
164
 
165
        // Otherwise it's based on the page URL:
166
        // PTP: admin-plugins -> admin/plugins.php.
167
        // PTP: admin-tool-customlang-index -> admin/tool/customlang/index.php.
168
        if (strpos($pagetypepattern, '*') === false) {
169
            $parts = explode('-', $pagetypepattern);
170
            return new moodle_url('/' . implode('/', $parts) . '.php');
171
        }
172
 
173
        // Just in case, we can always try the context's URL - that will get us *something*.
174
        return $context->get_url();
175
    }
176
 
177
    /**
178
     * Identifies if the specified URL is a dashboard.
179
     *
180
     * @param moodle_url $url The URL of the page in question.
181
     * @return bool True if the page is a dashboard.
182
     */
183
    public static function is_dashboard(moodle_url $url): bool {
184
        $local = $url->out_as_local_url(false);
185
        return strpos($local, '/my/') === 0 && strpos($local, '/my/indexsys') === false;
186
    }
187
 
188
    /**
189
     * Identifies if the specified URL is somewhere inside the admin panel.
190
     *
191
     * @param moodle_url $url The URL of the page in question.
192
     * @return bool True if the page is an admin page.
193
     */
194
    public static function is_admin_url(moodle_url $url): bool {
195
        global $CFG;
196
 
197
        $local = $url->out_as_local_url(false);
198
        return strpos($local, '/' . $CFG->admin . '/') === 0 || strpos($local, '/my/indexsys') === 0;
199
    }
200
}