|
| 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 | + * The mod_externalcontent module custom completion class. |
| 19 | + * |
| 20 | + * @package mod_externalcontent |
| 21 | + * @copyright 2019-2021 LushOnline |
| 22 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 23 | + */ |
| 24 | + |
| 25 | +namespace mod_externalcontent\completion; |
| 26 | + |
| 27 | +defined('MOODLE_INTERNAL') || die(); |
| 28 | + |
| 29 | +if (!class_exists('\core_completion\activity_custom_completion')) { |
| 30 | + // New API does not exist in this site, so do nothing. |
| 31 | + return; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * The mod_externalcontent module custom completion. |
| 36 | + * |
| 37 | + * @package mod_externalcontent |
| 38 | + * @copyright 2019-2021 LushOnline |
| 39 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 40 | + */ |
| 41 | +class custom_completion extends \core_completion\activity_custom_completion { |
| 42 | + /** |
| 43 | + * Fetches the completion state for a given completion rule. |
| 44 | + * |
| 45 | + * @param string $rule The completion rule. |
| 46 | + * @return int The completion state. |
| 47 | + */ |
| 48 | + public function get_state(string $rule): int { |
| 49 | + global $DB; |
| 50 | + |
| 51 | + $this->validate_rule($rule); |
| 52 | + // Get external content details. |
| 53 | + $externalcontent = $DB->get_record('externalcontent', array('id' => $this->cm->instance), '*', MUST_EXIST); |
| 54 | + |
| 55 | + switch ($rule) { |
| 56 | + case 'completionexternally': |
| 57 | + $params = array('userid' => $this->userid, 'externalcontentid' => $externalcontent->id, 'completed' => 1); |
| 58 | + $completed = $DB->record_exists('externalcontent_track', $params); |
| 59 | + |
| 60 | + $status = $completed ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE; |
| 61 | + break; |
| 62 | + |
| 63 | + default : |
| 64 | + $status = COMPLETION_INCOMPLETE; |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + return $status; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Fetch the list of custom completion rules that this module defines. |
| 73 | + * |
| 74 | + * @return array |
| 75 | + */ |
| 76 | + public static function get_defined_custom_rules(): array { |
| 77 | + return ['completionexternally']; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Returns an associative array of the descriptions of custom completion rules. |
| 82 | + * |
| 83 | + * @return array |
| 84 | + */ |
| 85 | + public function get_custom_rule_descriptions(): array { |
| 86 | + return ['completionexternally' => get_string('eventcompletedexternally', 'externalcontent')]; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Returns an array of all completion rules, in the order they should be displayed to users. |
| 91 | + * |
| 92 | + * @return array |
| 93 | + */ |
| 94 | + public function get_sort_order(): array { |
| 95 | + return [ |
| 96 | + 'completionview', |
| 97 | + 'completionexternally', |
| 98 | + ]; |
| 99 | + } |
| 100 | +} |
0 commit comments