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

ewallah / moodle-availability_language / 18308198196

07 Oct 2025 09:25AM UTC coverage: 97.297% (-2.7%) from 100.0%
18308198196

push

github

rdebleu
version

36 of 37 relevant lines covered (97.3%)

9.08 hits per line

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

96.88
/classes/condition.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
 * Condition main class.
19
 *
20
 * @package   availability_language
21
 * @copyright eWallah (www.eWallah.net)
22
 * @author    Renaat Debleu <info@eWallah.net>
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25

26
namespace availability_language;
27

28
/**
29
 * Condition main class.
30
 *
31
 * @package   availability_language
32
 * @copyright eWallah (www.eWallah.net)
33
 * @author    Renaat Debleu <info@eWallah.net>
34
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35
 */
36
class condition extends \core_availability\condition {
37
    /** @var string ID of language that this condition requires, or '' = any language */
38
    protected $languageid;
39

40
    /**
41
     * Constructor.
42
     *
43
     * @param \stdClass $structure Data structure from JSON decode
44
     * @throws \coding_exception If invalid data structure.
45
     */
46
    public function __construct($structure) {
47
        // Get language id.
48
        if (!property_exists($structure, 'id')) {
24✔
49
            $this->languageid = '';
4✔
50
        } else if (is_string($structure->id)) {
24✔
51
            $this->languageid = $structure->id;
24✔
52
        } else {
53
            throw new \coding_exception('Invalid ->id for language condition');
4✔
54
        }
55
    }
56

57
    /**
58
     * Saves data back to a structure object.
59
     *
60
     * @return \stdClass Structure object
61
     */
62
    public function save() {
63
        $result = (object)['type' => 'language'];
4✔
64
        if ($this->languageid) {
4✔
65
            $result->id = $this->languageid;
4✔
66
        }
67
        return $result;
4✔
68
    }
69

70
    /**
71
     * Returns a JSON object which corresponds to a condition of this type.
72
     *
73
     * Intended for unit testing, as normally the JSON values are constructed
74
     * by JavaScript code.
75
     *
76
     * @param string $languageid Not required language
77
     * @return stdClass Object representing condition
78
     */
79
    public static function get_json($languageid = '') {
80
        return (object)['type' => 'language', 'id' => $languageid];
8✔
81
    }
82

83
    /**
84
     * Determines whether a particular item is currently available
85
     * according to this availability condition.
86
     *
87
     * @param bool $not Set true if we are inverting the condition
88
     * @param info $info Item we're checking
89
     * @param bool $grabthelot Performance hint: if true, caches information
90
     *   required for all course-modules, to make the front page and similar
91
     *   pages work more quickly (works only for current user)
92
     * @param int $userid User ID to check availability for
93
     * @return bool True if available
94
     */
95
    public function is_available($not, \core_availability\info $info, $grabthelot, $userid) {
96
        global $CFG, $USER;
97

98
        // If course has forced language.
99
        $course = $info->get_course();
12✔
100
        $allow = false;
12✔
101
        if (isset($course->lang) && $course->lang === $this->languageid) {
12✔
102
            $allow = true;
4✔
103
        } else {
104
            if ($userid === $USER->id) {
12✔
105
                // Checking the language of the currently logged in user, so do not
106
                // default to the account language, because the session language
107
                // or the language of the current course may be different.
108
                $language = current_language();
4✔
109
            } else {
110
                if (is_null($userid)) {
8✔
111
                    // Fall back to site language or English.
112
                    $language = $CFG->lang;
×
113
                } else {
114
                    // Checking access for someone else than the logged in user, so
115
                    // use the preferred language of that user account.
116
                    // This language is never empty as there is a not-null constraint.
117
                    $language = \core_user::get_user($userid)->lang;
8✔
118
                }
119
            }
120
            if ($language === $this->languageid) {
12✔
121
                $allow = true;
12✔
122
            }
123
        }
124
        if ($not) {
12✔
125
            return !($allow);
4✔
126
        }
127
        return $allow;
12✔
128
    }
129

130
    /**
131
     * Obtains a string describing this restriction (whether or not
132
     * it actually applies). Used to obtain information that is displayed to
133
     * students if the activity is not available to them, and for staff to see
134
     * what conditions are.
135
     *
136
     * @param bool $full Set true if this is the 'full information' view
137
     * @param bool $not Set true if we are inverting the condition
138
     * @param info $info Item we're checking
139
     * @return string Information string (for admin) about all restrictions on this item
140
     */
141
    public function get_description($full, $not, \core_availability\info $info) {
142
        if ($this->languageid != '') {
12✔
143
            $smanager = get_string_manager();
12✔
144
            $langs = $smanager->get_list_of_translations();
12✔
145
            if (array_key_exists($this->languageid, $langs)) {
12✔
146
                $snot = $not ? 'getdescriptionnot' : 'getdescription';
12✔
147
                return $smanager->get_string($snot, 'availability_language', $langs[$this->languageid]);
12✔
148
            }
149
        }
150
        return '';
12✔
151
    }
152

153
    /**
154
     * Obtains a representation of the options of this condition as a string,
155
     * for debugging.
156
     *
157
     * @return string Text representation of parameters
158
     */
159
    protected function get_debug_string() {
160
        return $this->languageid ?? 'any';
4✔
161
    }
162
}
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

© 2025 Coveralls, Inc