|
| 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 | + * Tab. |
| 19 | + * |
| 20 | + * @package videotimetab_interaction |
| 21 | + * @copyright 2026 bdecent gmbh <https://bdecent.de> |
| 22 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 23 | + */ |
| 24 | + |
| 25 | +namespace videotimetab_interaction; |
| 26 | + |
| 27 | +use context_module; |
| 28 | +use mod_videotime\output\renderer; |
| 29 | +use mod_videotime\videotime_instance; |
| 30 | +use videotimeplugin_repository\video; |
| 31 | +use stdClass; |
| 32 | + |
| 33 | +defined('MOODLE_INTERNAL') || die(); |
| 34 | + |
| 35 | +require_once("$CFG->dirroot/mod/videotime/lib.php"); |
| 36 | + |
| 37 | +/** |
| 38 | + * Tab. |
| 39 | + * |
| 40 | + * @package videotimetab_interaction |
| 41 | + */ |
| 42 | +class tab extends \mod_videotime\local\tabs\tab { |
| 43 | + /** |
| 44 | + * Get tab panel content |
| 45 | + * |
| 46 | + * @return string |
| 47 | + */ |
| 48 | + public function get_tab_content(): string { |
| 49 | + global $DB, $OUTPUT, $PAGE, $USER; |
| 50 | + |
| 51 | + $instance = $this->get_instance(); |
| 52 | + $data = [ |
| 53 | + 'contextid' => $instance->get_context()->id, |
| 54 | + 'id' => $instance->id, |
| 55 | + 'title' => $instance->name, |
| 56 | + ]; |
| 57 | + |
| 58 | + return $OUTPUT->render_from_template( |
| 59 | + 'videotimetab_interaction/tab', |
| 60 | + $data |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Defines the additional form fields. |
| 66 | + * |
| 67 | + * @param moodle_form $mform form to modify |
| 68 | + */ |
| 69 | + public static function add_form_fields($mform) { |
| 70 | + $mform->addElement( |
| 71 | + 'advcheckbox', |
| 72 | + 'enable_interaction', |
| 73 | + get_string('pluginname', 'videotimetab_interaction'), |
| 74 | + get_string('showtab', 'videotime') |
| 75 | + ); |
| 76 | + $mform->setDefault('enable_interaction', get_config('videotimetab_interaction', 'default')); |
| 77 | + $mform->disabledIf('enable_interaction', 'enabletabs'); |
| 78 | + |
| 79 | + $options = [ |
| 80 | + 0 => get_string('none'), |
| 81 | + 1 => get_string('all'), |
| 82 | + ]; |
| 83 | + $mform->addElement( |
| 84 | + 'select', |
| 85 | + 'interactiontype', |
| 86 | + '', |
| 87 | + $options |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Whether tab is enabled and visible |
| 93 | + * |
| 94 | + * @return bool |
| 95 | + */ |
| 96 | + public function is_visible(): bool { |
| 97 | + global $DB; |
| 98 | + |
| 99 | + $record = $this->get_instance()->to_record(); |
| 100 | + return $this->is_enabled() && $DB->record_exists('videotimetab_interaction', [ |
| 101 | + 'videotime' => $record->id, |
| 102 | + ]); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Saves a settings in database |
| 107 | + * |
| 108 | + * @param stdClass $data Form data with values to save |
| 109 | + */ |
| 110 | + public static function save_settings(stdClass $data) { |
| 111 | + global $DB; |
| 112 | + |
| 113 | + if (empty($data->enable_interaction)) { |
| 114 | + $DB->delete_records('videotimetab_interaction', [ |
| 115 | + 'videotime' => $data->id, |
| 116 | + ]); |
| 117 | + } else { |
| 118 | + if (!$record = $DB->get_record('videotimetab_interaction', ['videotime' => $data->id])) { |
| 119 | + $DB->insert_record('videotimetab_interaction', [ |
| 120 | + 'videotime' => $data->id, |
| 121 | + ]); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Delete settings in database |
| 128 | + * |
| 129 | + * @param int $id |
| 130 | + */ |
| 131 | + public static function delete_settings(int $id) { |
| 132 | + global $DB; |
| 133 | + |
| 134 | + $DB->delete_records('videotimetab_interaction', [ |
| 135 | + 'videotime' => $id, |
| 136 | + ]); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Prepares the form before data are set |
| 141 | + * |
| 142 | + * @param array $defaultvalues |
| 143 | + * @param int $instance |
| 144 | + */ |
| 145 | + public static function data_preprocessing(array &$defaultvalues, int $instance) { |
| 146 | + global $COURSE, $DB; |
| 147 | + |
| 148 | + if (empty($instance)) { |
| 149 | + $defaultvalues['enable_interaction'] = get_config('videotimetab_interaction', 'default'); |
| 150 | + } else if ($record = $DB->get_record('videotimetab_interaction', ['videotime' => $instance])) { |
| 151 | + $defaultvalues['enable_interaction'] = 1; |
| 152 | + } else { |
| 153 | + $defaultvalues['enable_interaction'] = 0; |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * List of missing dependencies needed for plugin to be enabled |
| 159 | + */ |
| 160 | + public static function added_dependencies() { |
| 161 | + global $OUTPUT; |
| 162 | + if (videotime_has_pro()) { |
| 163 | + return ''; |
| 164 | + } |
| 165 | + return $OUTPUT->render_from_template('videotimetab_interaction/upgrade', []); |
| 166 | + } |
| 167 | +} |
0 commit comments