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

ewallah / moodle-availability_language / 14447943481

14 Apr 2025 02:15PM UTC coverage: 100.0%. Remained the same
14447943481

push

github

rdebleu
MOODLE_500

36 of 36 relevant lines covered (100.0%)

7.64 hits per line

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

100.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 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')) {
20✔
49
            $this->languageid = '';
3✔
50
        } else if (is_string($structure->id)) {
20✔
51
            $this->languageid = $structure->id;
20✔
52
        } else {
53
            throw new \coding_exception('Invalid ->id for language condition');
3✔
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'];
3✔
64
        if ($this->languageid) {
3✔
65
            $result->id = $this->languageid;
3✔
66
        }
67
        return $result;
3✔
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];
6✔
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();
11✔
100
        $allow = false;
11✔
101
        if (isset($course->lang) && $course->lang === $this->languageid) {
11✔
102
            $allow = true;
3✔
103
        } else {
104
            if ($userid === $USER->id) {
11✔
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();
5✔
109
            } else {
110
                if (is_null($userid)) {
8✔
111
                    // Fall back to site language or English.
112
                    $language = $CFG->lang;
2✔
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) {
11✔
121
                $allow = true;
11✔
122
            }
123
        }
124
        if ($not) {
11✔
125
            return !($allow);
5✔
126
        }
127
        return $allow;
11✔
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 != '') {
9✔
143
            $installedlangs = get_string_manager()->get_list_of_translations();
9✔
144
            if (array_key_exists($this->languageid, $installedlangs)) {
9✔
145
                $snot = $not ? 'not' : '';
9✔
146
                return get_string('getdescription' . $snot, 'availability_language', $installedlangs[$this->languageid]);
9✔
147
            }
148
        }
149
        return '';
9✔
150
    }
151

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