• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ewallah / moodle-filter_sectionnames / 9451276460

10 Jun 2024 03:33PM UTC coverage: 97.826% (-2.2%) from 100.0%
9451276460

push

github

rdebleu
infection

3 of 4 new or added lines in 1 file covered. (75.0%)

45 of 46 relevant lines covered (97.83%)

3.74 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.78
/filter.php
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
 * Automatically links section names in a Moodle course and its activities
19
 *
20
 * This filter provides automatic linking to sections when its name (title)
21
 * is found inside every Moodle text
22
 *
23
 * @package    filter_sectionnames
24
 * @copyright  2017 Matt Davidson
25
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27

28
/**
29
 * Section name filtering.
30
 *
31
 * @package    filter_sectionnames
32
 * @copyright  2017 Matt Davidson
33
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34
 */
35
class filter_sectionnames extends moodle_text_filter {
36
    // Trivial-cache - keyed on $cachedcourseid and $cacheduserid.
37
    /** @var array section list. */
38
    public static $sectionslist = null;
39

40
    /** @var int course id. */
41
    public static $cachedcourseid;
42

43
    /** @var int userid. */
44
    public static $cacheduserid;
45

46
    /**
47
     * Given an object containing all the necessary data,
48
     * (defined by the form in mod_form.php) this function
49
     * will update an existing instance with new data.
50
     *
51
     * @param string $text The text that will be filtered.
52
     * @param array $options The standard filter options passed.
53
     * @return string Filtered text.
54
     */
55
    public function filter($text, array $options = []) {
56
        global $CFG, $USER; // Since 2.7 we can finally start using globals in filters.
57
        require_once($CFG->dirroot . '/course/format/lib.php'); // Needed to ensure course_get_format().
4✔
58

59
        $coursectx = $this->context->get_course_context(false);
4✔
60
        if (!$coursectx) {
4✔
61
            return $text;
2✔
62
        }
63
        $courseid = $coursectx->instanceid;
4✔
64

65
        // Initialise/invalidate our trivial cache if dealing with a different course.
66
        if (!isset(self::$cachedcourseid) || self::$cachedcourseid !== (int)$courseid) {
4✔
67
            self::$sectionslist = null;
4✔
68
        }
69
        self::$cachedcourseid = (int)$courseid;
4✔
70
        // And the same for user id.
71
        if (!isset(self::$cacheduserid) || self::$cacheduserid !== (int)$USER->id) {
4✔
72
            self::$sectionslist = null;
2✔
73
        }
74
        self::$cacheduserid = (int)$USER->id;
4✔
75

76
        // It may be cached.
77
        if (is_null(self::$sectionslist)) {
4✔
78
            $modinfo = get_fast_modinfo($courseid);
4✔
79
            self::$sectionslist = []; // We will store all the created filters here.
4✔
80

81
            // Create array of visible sections sorted by the name length (we are only interested in properties name and url).
82
            $sortedsections = [];
4✔
83

84
            $formatinfo = function_exists('course_get_format') ? course_get_format($courseid) : format_base::instance($courseid);
4✔
85

86
            $numsections = $formatinfo->get_last_section_number();
4✔
87
            $section = 1; // Skip the general section 0.
4✔
88
            while ($section <= $numsections) {
4✔
89
                if (!empty($modinfo->get_section_info($section)) && $modinfo->get_section_info($section)->visible) {
4✔
90
                    $sortedsections[] = (object)[
4✔
91
                        'name' => get_section_name($courseid, $section),
4✔
92
                        'url' => course_get_url($courseid, $section),
4✔
93
                        'id' => $section,
4✔
94
                        'namelen' => -strlen(get_section_name($courseid, $section)), // Negative value for reverse sorting.
4✔
95
                    ];
4✔
96
                }
97
                $section++;
4✔
98
            }
99

100
            // Sort activities by the length of the section name in reverse order.
101
            core_collator::asort_objects_by_property($sortedsections, 'namelen', core_collator::SORT_NUMERIC);
4✔
102

103
            foreach ($sortedsections as $section) {
4✔
104
                $title = s(trim(strip_tags($section->name)));
4✔
105
                $currentname = trim($section->name);
4✔
106
                $entname = s($currentname);
4✔
107
                // Avoid empty or unlinkable activity names.
108
                if (!empty($title)) {
4✔
109
                    $hrefopen = html_writer::start_tag('a', ['class' => 'autolink', 'title' => $title, 'href' => $section->url]);
4✔
110
                    self::$sectionslist[$section->id] = new filterobject($currentname, $hrefopen, '</a>', false, true);
4✔
111
                    if ($currentname != $entname) {
4✔
112
                        // If name has some entity (&amp; &quot; &lt; &gt;) add that filter too. MDL-17545.
113
                        self::$sectionslist[$section->id . '-e'] = new filterobject($entname, $hrefopen, '</a>', false, true);
2✔
114
                    }
115
                }
116
            }
117
        }
118

119
        $filterslist = [];
4✔
120
        if (self::$sectionslist) {
4✔
121
            $sectionid = $this->context->instanceid;
4✔
122
            if ($this->context->contextlevel == CONTEXT_MODULE && isset(self::$sectionslist[$sectionid])) {
4✔
123
                // Remove filterobjects for the current module.
NEW
124
                $filterslist = array_diff_key(self::$sectionslist, [$sectionid => 1, $sectionid . '-e' => 1]);
×
125
            } else {
126
                $filterslist = self::$sectionslist;
4✔
127
            }
128
            $filterslist = array_values($filterslist);
4✔
129
        }
130

131
        return $filterslist ? filter_phrases($text, $filterslist) : $text;
4✔
132
    }
133
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc