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
/**
18
 * Condition main class.
19
 *
20
 * @package   availability_coursecompleted
21
 * @copyright 2015 iplusacademy (www.iplusacademy.org)
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_coursecompleted;
27
 
28
use completion_info;
29
use core_availability\info;
30
use coding_exception;
31
use stdClass;
32
 
33
 
34
/**
35
 * Condition main class.
36
 *
37
 * @package   availability_coursecompleted
38
 * @copyright 2015 iplusacademy (www.iplusacademy.org)
39
 * @author    Renaat Debleu <info@eWallah.net>
40
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
42
class condition extends \core_availability\condition {
43
    /** @var string coursecompleted 0 => No, 1 => Yes */
44
    protected $coursecompleted;
45
 
46
    /**
47
     * Constructor.
48
     *
49
     * @param stdClass $structure Data structure from JSON decode
50
     * @throws coding_exception If invalid data.
51
     */
52
    public function __construct($structure) {
53
        if (!property_exists($structure, 'id')) {
54
            $this->coursecompleted = '';
55
        } else if (is_string($structure->id)) {
56
            $this->coursecompleted = $structure->id;
57
        } else {
58
            new coding_exception('Invalid value for course completed condition');
59
        }
60
    }
61
 
62
    /**
63
     * Saves tree data back to a structure object.
64
     *
65
     * @return stdClass Structure object (ready to be made into JSON format)
66
     */
67
    public function save() {
68
        return (object)['type' => 'coursecompleted', 'id' => $this->coursecompleted];
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 $coursecompleted default empty string
78
     * @return stdClass Object representing condition
79
     */
80
    public static function get_json($coursecompleted = '') {
81
        return (object)['type' => 'coursecompleted', 'id' => $coursecompleted];
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, info $info, $grabthelot, $userid) {
97
        $completioninfo = new \completion_info($info->get_course());
98
        $allow = $completioninfo->is_course_complete($userid);
99
        unset($completioninfo);
100
        if (!$this->coursecompleted) {
101
            $allow = !$allow;
102
        }
103
        if ($not) {
104
            $allow = !$allow;
105
        }
106
        return $allow;
107
    }
108
 
109
    /**
110
     * Obtains a string describing this restriction (whether or not
111
     * it actually applies). Used to obtain information that is displayed to
112
     * students if the activity is not available to them, and for staff to see
113
     * what conditions are.
114
     *
115
     * @param bool $full Set true if this is the 'full information' view
116
     * @param bool $not Set true if we are inverting the condition
117
     * @param info $info Item we're checking
118
     * @return string Information string (for admin) about all restrictions on
119
     *   this item
120
     */
121
    public function get_description($full, $not, info $info) {
122
        $allow = $this->coursecompleted;
123
        if ($not) {
124
            $allow = !$allow;
125
        }
126
        return get_string($allow ? 'getdescription' : 'getdescriptionnot', 'availability_coursecompleted');
127
    }
128
 
129
    /**
130
     * Obtains a representation of the options of this condition as a string,
131
     * for debugging.
132
     *
133
     * @return string Text representation of parameters
134
     */
135
    protected function get_debug_string() {
136
        return get_string($this->coursecompleted ? 'true' : 'false', 'mod_quiz');
137
    }
138
 
139
    /**
140
     * Checks whether this condition applies to user lists.
141
     *
142
     * @return bool True if this condition applies to user lists
143
     */
144
    public function is_applied_to_user_lists() {
145
        // Course completions are assumed to be 'permanent', so they affect the
146
        // display of user lists for activities.
147
        return true;
148
    }
149
 
150
    /**
151
     * Tests against a user list. Users who cannot access the activity due to
152
     * availability restrictions will be removed from the list.
153
     *
154
     * @param array $users Array of userid => object
155
     * @param bool $not If tree's parent indicates it's being checked negatively
156
     * @param info $info Info about current context
157
     * @param capability_checker $checker Capability checker
158
     * @return array Filtered version of input array
159
     */
160
    public function filter_user_list(
161
        array $users,
162
        $not,
163
        \core_availability\info $info,
164
        \core_availability\capability_checker $checker
165
    ) {
166
 
167
        global $DB;
168
 
169
        // If the array is empty already, just return it.
170
        if (!$users) {
171
            return $users;
172
        }
173
 
174
        $course = $info->get_course();
175
        $cond = $this->coursecompleted ? 'NOT' : '';
176
        $sql = "SELECT DISTINCT userid
177
                  FROM {course_completions}
178
                  WHERE timecompleted IS $cond NULL AND course = ?";
179
        $compusers = $DB->get_records_sql($sql, [$course->id]);
180
 
181
        // List users who have access to the completion report.
182
        $adusers = $checker->get_users_by_capability('report/completion:view');
183
        // Filter the user list.
184
        $result = [];
185
        foreach ($users as $id => $user) {
186
            // Always include users with access to completion report.
187
            if (array_key_exists($id, $adusers)) {
188
                $result[$id] = $user;
189
            } else {
190
                // Other users are included or not based on course completion.
191
                $allow = array_key_exists($id, $compusers);
192
                if ($not) {
193
                    $allow = !$allow;
194
                }
195
                if ($allow) {
196
                    $result[$id] = $user;
197
                }
198
            }
199
        }
200
        return $result;
201
    }
202
}