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 tool_usertours;
18
 
19
/**
20
 * Cache manager.
21
 *
22
 * @package    tool_usertours
23
 * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
class cache {
27
    /**
28
     * @var CACHENAME_TOUR      The name of the cache used for storing tours.
29
     */
30
    const CACHENAME_TOUR = 'tourdata';
31
 
32
    /**
33
     * @var CACHEKEY_TOUR       The name of the key used for storing tours.
34
     */
35
    const CACHEKEY_TOUR = 'tours';
36
 
37
    /**
38
     * @var CACHENAME_STEP      The name of the cache used for storing steps.
39
     */
40
    const CACHENAME_STEP = 'stepdata';
41
 
42
    /**
43
     * Fetch all enabled tours.
44
     */
45
    public static function get_enabled_tourdata() {
46
        global $DB;
47
 
48
        $cache = \cache::make('tool_usertours', self::CACHENAME_TOUR);
49
 
50
        $data = $cache->get(self::CACHEKEY_TOUR);
51
        if ($data === false) {
52
            $sql = <<<EOF
53
                SELECT t.*
54
                  FROM {tool_usertours_tours} t
55
                 WHERE t.enabled = 1
56
                   AND t.id IN (SELECT s.tourid FROM {tool_usertours_steps} s GROUP BY s.tourid)
57
              ORDER BY t.sortorder ASC
58
EOF;
59
 
60
            $data = $DB->get_records_sql($sql);
61
            $cache->set('tours', $data);
62
        }
63
 
64
        return $data;
65
    }
66
 
67
    /**
68
     * Fetch all enabled tours matching the specified target.
69
     *
70
     * @param   moodle_url  $targetmatch    The URL to match.
71
     */
72
    public static function get_matching_tourdata(\moodle_url $targetmatch) {
73
        $tours = self::get_enabled_tourdata();
74
 
75
        // Attempt to determine whether this is the front page.
76
        // This is a special case because the frontpage uses a shortened page path making it difficult to detect exactly.
77
        $isfrontpage = $targetmatch->compare(new \moodle_url('/'), URL_MATCH_BASE);
78
        $isdashboard = $targetmatch->compare(new \moodle_url('/my/'), URL_MATCH_BASE);
79
        $ismycourses = $targetmatch->compare(new \moodle_url('/my/courses.php'), URL_MATCH_BASE);
80
 
81
        $possiblematches = [];
82
        if ($isfrontpage) {
83
            $possiblematches = ['FRONTPAGE', 'FRONTPAGE_MY', 'FRONTPAGE_MYCOURSES', 'FRONTPAGE_MY_MYCOURSES'];
84
        } else if ($isdashboard) {
85
            $possiblematches = ['MY', 'FRONTPAGE_MY', 'MY_MYCOURSES', 'FRONTPAGE_MY_MYCOURSES'];
86
        } else if ($ismycourses) {
87
            $possiblematches = ['MYCOURSES', 'FRONTPAGE_MYCOURSES', 'MY_MYCOURSES', 'FRONTPAGE_MY_MYCOURSES'];
88
        }
89
 
90
        $target = $targetmatch->out_as_local_url();
91
        return array_filter($tours, function ($tour) use ($possiblematches, $target) {
92
            if (in_array($tour->pathmatch, $possiblematches)) {
93
                return true;
94
            }
95
            $pattern = preg_quote($tour->pathmatch, '@');
96
            if (strpos($pattern, '%') !== false) {
97
                // The URL match format is something like: /my/%.
98
                // We need to find all the URLs which match the first part of the pattern.
99
                $pattern = str_replace('%', '.*', $pattern);
100
            } else {
101
                // The URL match format is something like: /my/courses.php.
102
                // We need to find all the URLs which match with whole pattern.
103
                $pattern .= '$';
104
            }
105
            return !!preg_match("@{$pattern}@", $target);
106
        });
107
    }
108
 
109
    /**
110
     * Notify of changes to any tour to clear the tour cache.
111
     */
112
    public static function notify_tour_change() {
113
        $cache = \cache::make('tool_usertours', self::CACHENAME_TOUR);
114
        $cache->delete(self::CACHEKEY_TOUR);
115
    }
116
 
117
    /**
118
     * Fetch the tour data for the specified tour.
119
     *
120
     * @param   int         $tourid         The ID of the tour to fetch steps for
121
     */
122
    public static function get_stepdata($tourid) {
123
        global $DB;
124
 
125
        $cache = \cache::make('tool_usertours', self::CACHENAME_STEP);
126
 
127
        $data = $cache->get($tourid);
128
        if ($data === false) {
129
            $sql = <<<EOF
130
                SELECT s.*
131
                  FROM {tool_usertours_steps} s
132
                 WHERE s.tourid = :tourid
133
              ORDER BY s.sortorder ASC
134
EOF;
135
 
136
            $data = $DB->get_records_sql($sql, ['tourid' => $tourid]);
137
            $cache->set($tourid, $data);
138
        }
139
 
140
        return $data;
141
    }
142
    /**
143
     * Notify of changes to any step to clear the step cache for that tour.
144
     *
145
     * @param   int         $tourid         The ID of the tour to clear the step cache for
146
     */
147
    public static function notify_step_change($tourid) {
148
        $cache = \cache::make('tool_usertours', self::CACHENAME_STEP);
149
        $cache->delete($tourid);
150
    }
151
}