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

ewallah / moodle-availability_language / 6064224035

03 Sep 2023 11:36AM UTC coverage: 2.128% (-97.9%) from 100.0%
6064224035

push

github

ewallah
is available for all

14 of 14 new or added lines in 1 file covered. (100.0%)

1 of 47 relevant lines covered (2.13%)

0.15 hits per line

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

0.0
/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 2022 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 2022 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

38
    /** @var string ID of language that this condition requires, or '' = any language */
39
    protected $languageid;
40

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

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

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

84
    /**
85
     * Determines whether a particular item is currently available
86
     * according to this availability condition.
87
     *
88
     * @param bool $not Set true if we are inverting the condition
89
     * @param info $info Item we're checking
90
     * @param bool $grabthelot Performance hint: if true, caches information
91
     *   required for all course-modules, to make the front page and similar
92
     *   pages work more quickly (works only for current user)
93
     * @param int $userid User ID to check availability for
94
     * @return bool True if available
95
     */
96
    public function is_available($not, \core_availability\info $info, $grabthelot, $userid) {
97
        global $CFG, $DB, $USER;
×
98
        if ($userid == $USER->id) {
×
99
            if ($grabthelot) {
×
100
                return $this->is_available_for_all($not);
×
101
            }
102
            // Checking the language of the currently logged in user, so do not
103
            // default to the account language, because the session language
104
            // or the language of the current course may be different.
105
            $language = current_language();
×
106
        } else {
107
            if (is_null($userid)) {
×
108
                // Fall back to site language or English.
109
                $language = $CFG->lang ?? 'en';
×
110
            } else {
111
                // Checking access for someone else than the logged in user, so
112
                // use the preferred language of that user account.
113
                // This language is never empty as there is a not-null constraint.
114
                $language = $DB->get_field('user', 'lang', ['id' => $userid]);
×
115
            }
116
        }
117
        $allow = $this->is_forced($info);
×
118
        if ($language == $this->languageid) {
×
119
            $allow = true;
×
120
        }
121
        if ($not) {
×
122
            $allow = !$allow;
×
123
        }
124
        return $allow;
×
125
    }
126

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

149
    /**
150
     * Obtains a representation of the options of this condition as a string,
151
     * for debugging.
152
     *
153
     * @return string Text representation of parameters
154
     */
155
    protected function get_debug_string() {
156
        return $this->languageid ?? 'any';
×
157
    }
158

159
    /**
160
     * Forced language.
161
     *
162
     * @param bool $full Set true if this is the 'full information' view
163
     * @param bool $not Set true if we are inverting the condition
164
     * @param info $info Item we're checking
165
     * @return bool True if available
166
     */
167
    private function is_forced(\core_availability\info $info) {
168
        $course = $info->get_course();
×
169
        if (isset($course->lang) && $course->lang == $this->languageid) {
×
170
            return true;
×
171
        }
172
        return false;
×
173
    }
174

175
    /**
176
     * Checks whether this condition applies to user lists.
177
     *
178
     * @return bool True if this condition applies to user lists
179
     */
180
    public function is_applied_to_user_lists() {
181
        // Session languages are not 'permanent'.
182
        return false;
×
183
    }
184

185
    /**
186
     * Is available for all.
187
     *
188
     * @param bool $not Set true if we are inverting the condition
189
     * @return bool True if available
190
     */
191
    public function is_available_for_all($not = false) {
192
        if ($this->languageid == '') {
×
193
            $allow = true;
×
194
        } else {
195
            $allow = current_language() == $this->languageid;
×
196
        }
197
        if ($not) {
×
198
            $allow = !$allow;
×
199
        }
200
        return $allow;
×
201
    }
202
}
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