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
namespace mod_bigbluebuttonbn\completion;
18
 
19
use cm_info;
20
use core_completion\activity_custom_completion;
21
use mod_bigbluebuttonbn\extension;
22
use mod_bigbluebuttonbn\instance;
23
use mod_bigbluebuttonbn\logger;
24
use moodle_exception;
25
use stdClass;
26
 
27
/**
28
 * Class custom_completion
29
 *
30
 * @package   mod_bigbluebuttonbn
31
 * @copyright 2010 onwards, Blindside Networks Inc
32
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 * @author    Laurent David (laurent@call-learning.fr)
34
 */
35
class custom_completion extends activity_custom_completion {
36
 
37
    /**
38
     * Filters for logs
39
     */
40
    const FILTERS = [
41
        'completionattendance' => [logger::EVENT_SUMMARY],
42
        'completionengagementchats' => [logger::EVENT_SUMMARY],
43
        'completionengagementtalks' => [logger::EVENT_SUMMARY],
44
        'completionengagementraisehand' => [logger::EVENT_SUMMARY],
45
        'completionengagementpollvotes' => [logger::EVENT_SUMMARY],
46
        'completionengagementemojis' => [logger::EVENT_SUMMARY],
47
    ];
48
 
49
    /**
50
     * @var array $completionaddons array of extension class for the completion
51
     */
52
    private $completionaddons;
53
 
54
    /**
55
     * activity_custom_completion constructor.
56
     *
57
     * @param cm_info $cm
58
     * @param int $userid
59
     * @param array|null $completionstate The current state of the core completion criteria
60
     */
61
    public function __construct(cm_info $cm, int $userid, ?array $completionstate = null) {
62
        parent::__construct($cm, $userid, $completionstate);
63
        $completionaddonsclasses = extension::custom_completion_addons_instances($cm, $userid, $completionstate);
64
        $this->completionaddons = array_map(function($targetclassname) use ($cm, $userid, $completionstate) {
65
            return new $targetclassname($cm, $userid, $completionstate);
66
        }, $completionaddonsclasses);
67
    }
68
 
69
    /**
70
     * Get current state
71
     *
72
     * @param string $rule
73
     * @return int
74
     */
75
    public function get_state(string $rule): int {
76
        // Get instance details.
77
        $instance = instance::get_from_cmid($this->cm->id);
78
 
79
        if (empty($instance)) {
80
            throw new moodle_exception("Can't find bigbluebuttonbn instance {$this->cm->instance}");
81
        }
82
 
83
        // Default return value.
84
        $returnedvalue = COMPLETION_INCOMPLETE;
85
        $filters = self::FILTERS[$rule] ?? [logger::EVENT_SUMMARY];
86
        $logs = logger::get_user_completion_logs($instance, $this->userid, $filters);
87
 
88
        if (method_exists($this, "get_{$rule}_value")) {
89
            $completionvalue = $this->aggregate_values($logs, self::class . "::get_{$rule}_value");
90
            if ($completionvalue) {
91
                // So in this case we check the value set in the module setting. If we go over the threshold, then
92
                // this is complete.
93
                $rulevalue = $instance->get_instance_var($rule);
94
                if (!is_null($rulevalue)) {
95
                    if ($rulevalue <= $completionvalue) {
96
                        $returnedvalue = COMPLETION_COMPLETE;
97
                    }
98
                } else {
99
                    // If there is at least a hit, we consider it as complete.
100
                    $returnedvalue = $completionvalue ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE;
101
                }
102
            }
103
        }
104
        // Check for any completion for this rule in addons / extensions.
105
        foreach ($this->completionaddons as $customcompletion) {
106
            if (in_array($rule, $customcompletion->get_defined_custom_rules())) {
107
                $returnedvalue = $returnedvalue || $customcompletion->get_state($rule);
108
            }
109
        }
110
        return $returnedvalue;
111
    }
112
 
113
    /**
114
     * Compute current state from logs.
115
     *
116
     * @param array $logs
117
     * @param callable $logvaluegetter
118
     * @return int the sum of all values for this particular event (it can be a duration or a number of hits)
119
     */
120
    protected function aggregate_values(array $logs, callable $logvaluegetter): int {
121
        if (empty($logs)) {
122
            // As completion by engagement with $rulename hand was required, the activity hasn't been completed.
123
            return 0;
124
        }
125
 
126
        $value = 0;
127
        foreach ($logs as $log) {
128
            $value += $logvaluegetter($log);
129
        }
130
 
131
        return $value;
132
    }
133
 
134
    /**
135
     * Fetch the list of custom completion rules that this module defines.
136
     *
137
     * @return array
138
     */
139
    public static function get_defined_custom_rules(): array {
140
        $rules = [
141
            'completionattendance',
142
            'completionengagementchats',
143
            'completionengagementtalks',
144
            'completionengagementraisehand',
145
            'completionengagementpollvotes',
146
            'completionengagementemojis',
147
        ];
148
        $completionaddonsclasses = extension::custom_completion_addons_classes();
149
        foreach ($completionaddonsclasses as $customcompletion) {
150
            $rules = array_merge($rules, $customcompletion::get_defined_custom_rules());
151
        }
152
        return $rules;
153
    }
154
 
155
    /**
156
     * Returns an associative array of the descriptions of custom completion rules.
157
     *
158
     * @return array
159
     */
160
    public function get_custom_rule_descriptions(): array {
161
        $completionengagementchats = $this->cm->customdata['customcompletionrules']['completionengagementchats'] ?? 1;
162
        $completionengagementtalks = $this->cm->customdata['customcompletionrules']['completionengagementtalks'] ?? 1;
163
        $completionengagementraisehand = $this->cm->customdata['customcompletionrules']['completionengagementraisehand'] ?? 1;
164
        $completionengagementpollvotes = $this->cm->customdata['customcompletionrules']['completionengagementpollvotes'] ?? 1;
165
        $completionengagementemojis = $this->cm->customdata['customcompletionrules']['completionengagementemojis'] ?? 1;
166
        $completionattendance = $this->cm->customdata['customcompletionrules']['completionattendance'] ?? 1;
167
        $descriptions = [
168
            'completionengagementchats' => get_string('completionengagementchats_desc', 'mod_bigbluebuttonbn',
169
                $completionengagementchats),
170
            'completionengagementtalks' => get_string('completionengagementtalks_desc', 'mod_bigbluebuttonbn',
171
                $completionengagementtalks),
172
            'completionengagementraisehand' => get_string('completionengagementraisehand_desc', 'mod_bigbluebuttonbn',
173
                $completionengagementraisehand),
174
            'completionengagementpollvotes' => get_string('completionengagementpollvotes_desc', 'mod_bigbluebuttonbn',
175
                $completionengagementpollvotes),
176
            'completionengagementemojis' => get_string('completionengagementemojis_desc', 'mod_bigbluebuttonbn',
177
                $completionengagementemojis),
178
            'completionattendance' => get_string('completionattendance_desc', 'mod_bigbluebuttonbn',
179
                $completionattendance),
180
        ];
181
        // Check for any completion for this rule in addons / extensions.
182
        foreach ($this->completionaddons as $customcompletion) {
183
            $descriptions = array_merge($descriptions, $customcompletion->get_custom_rule_descriptions());
184
        }
185
        return $descriptions;
186
    }
187
 
188
    /**
189
     * Returns an array of all completion rules, in the order they should be displayed to users.
190
     *
191
     * @return array
192
     */
193
    public function get_sort_order(): array {
194
        $rules = self::get_defined_custom_rules();
195
        array_unshift($rules, 'completionview');
196
        return $rules;
197
    }
198
 
199
    /**
200
     * Get current states of completion in a human-friendly version
201
     *
202
     * @return string[]
203
     */
204
    public function get_printable_states(): array {
205
        $result = [];
206
        foreach ($this->get_available_custom_rules() as $rule) {
207
            $result[] = $this->get_printable_state($rule);
208
        }
209
        return $result;
210
    }
211
 
212
    /**
213
     * Get current states of completion for a rule in a human-friendly version
214
     *
215
     * @param string $rule
216
     * @return string
217
     */
218
    private function get_printable_state(string $rule): string {
219
        // Get instance details.
220
        $instance = instance::get_from_cmid($this->cm->id);
221
 
222
        if (empty($instance)) {
223
            throw new moodle_exception("Can't find bigbluebuttonbn instance {$this->cm->instance}");
224
        }
225
        $summary = "";
226
        $filters = self::FILTERS[$rule] ?? [logger::EVENT_SUMMARY];
227
        $logs = logger::get_user_completion_logs($instance, $this->userid, $filters);
228
 
229
        if (method_exists($this, "get_{$rule}_value")) {
230
            $summary = get_string(
231
                $rule . '_event_desc',
232
                'mod_bigbluebuttonbn',
233
                $this->aggregate_values($logs, self::class . "::get_{$rule}_value")
234
            );
235
        }
236
        return $summary;
237
    }
238
 
239
    /**
240
     * Get current state in a friendly version
241
     *
242
     * @param string $rule
243
     * @return string
244
     */
245
    public function get_last_log_timestamp(string $rule): string {
246
        // Get instance details.
247
        $instance = instance::get_from_cmid($this->cm->id);
248
 
249
        if (empty($instance)) {
250
            throw new moodle_exception("Can't find bigbluebuttonbn instance {$this->cm->instance}");
251
        }
252
        $filters = self::FILTERS[$rule] ?? [logger::EVENT_SUMMARY];
253
        return logger::get_user_completion_logs_max_timestamp($instance, $this->userid, $filters);
254
    }
255
 
256
    /**
257
     * Get attendance summary value
258
     *
259
     * @param stdClass $log
260
     * @return int
261
     */
262
    protected static function get_completionattendance_value(stdClass $log): int {
263
        $summary = json_decode($log->meta);
264
        return empty($summary->data->duration) ? 0 : (int)($summary->data->duration / 60);
265
    }
266
 
267
    /**
268
     * Get general completion engagement value
269
     *
270
     * @param stdClass $log
271
     * @return int
272
     */
273
    protected static function get_completionengagementchats_value(stdClass $log): int {
274
        return self::get_completionengagement_value($log, 'chats');
275
    }
276
 
277
    /**
278
     * Get general completion engagement value
279
     *
280
     * @param stdClass $log
281
     * @return int
282
     */
283
    protected static function get_completionengagementtalks_value(stdClass $log): int {
284
        return self::get_completionengagement_value($log, 'talks');
285
    }
286
 
287
    /**
288
     * Get general completion engagement value
289
     *
290
     * @param stdClass $log
291
     * @return int
292
     */
293
    protected static function get_completionengagementraisehand_value(stdClass $log): int {
294
        return self::get_completionengagement_value($log, 'raisehand');
295
    }
296
 
297
    /**
298
     * Get general completion engagement value
299
     *
300
     * @param stdClass $log
301
     * @return int
302
     */
303
    protected static function get_completionengagementpollvotes_value(stdClass $log): int {
304
        return self::get_completionengagement_value($log, 'poll_votes');
305
    }
306
 
307
    /**
308
     * Get general completion engagement value
309
     *
310
     * @param stdClass $log
311
     * @return int
312
     */
313
    protected static function get_completionengagementemojis_value(stdClass $log): int {
314
        return self::get_completionengagement_value($log, 'emojis');
315
    }
316
 
317
    /**
318
     * Get general completion engagement value
319
     *
320
     * @param stdClass $log
321
     * @param string $type
322
     * @return int
323
     */
324
    protected static function get_completionengagement_value(stdClass $log, string $type): int {
325
        $summary = json_decode($log->meta);
326
        return intval($summary->data->engagement->$type ?? 0);
327
    }
328
}