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 tiny_premium\local;
18
 
19
use tiny_premium\manager;
20
 
21
/**
22
 * Admin setting for managing Tiny Premium plugins.
23
 *
24
 * @package    tiny_premium
25
 * @copyright  2024 David Woloszyn <david.woloszyn@moodle.com>
26
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 */
28
class admin_setting_tiny_premium_plugins extends \admin_setting {
29
 
30
    /**
31
     * Calls parent::__construct with specific arguments.
32
     */
33
    public function __construct() {
34
        $this->nosave = true;
35
        parent::__construct(
36
            name: 'tiny_premium/premiumplugins',
37
            visiblename: new \lang_string('premiumplugins', 'tiny_premium'),
38
            description: new \lang_string('premiumplugins_desc', 'tiny_premium'),
39
            defaultsetting: '',
40
        );
41
    }
42
 
43
    /**
44
     * Always returns true.
45
     *
46
     * @return bool
47
     */
48
    public function get_setting(): bool {
49
        return true;
50
    }
51
 
52
    /**
53
     * Always returns '' and doesn't write anything.
54
     *
55
     * @param mixed $data
56
     * @return string Always returns ''
57
     */
58
    public function write_setting($data): string {
59
        return '';
60
    }
61
 
62
    /**
63
     * Builds the HTML to display the Tiny Premium plugins table.
64
     *
65
     * @param mixed $data Unused
66
     * @param string $query
67
     * @return string highlight
68
     */
69
    public function output_html($data, $query=''): string {
70
        global $OUTPUT;
71
 
72
        $return = '';
73
 
74
        // Warn users about an empty API key when displaying enabled plugins.
75
        if (empty(get_config('tiny_premium', 'apikey')) && !empty(manager::get_enabled_plugins())) {
76
            $return .= \core\notification::warning(get_string('emptyapikeywarning', 'tiny_premium'));
77
        }
78
 
79
        $return .= $OUTPUT->box_start('generalbox');
80
        $return .= $OUTPUT->heading(get_string('premiumplugins', 'tiny_premium'), 3);
81
        $return .= \html_writer::tag('p', get_string('premiumplugins_desc', 'tiny_premium'));
82
        $return .= $this->define_manage_tiny_premium_plugins_table();
83
        $return .= $OUTPUT->box_end();
84
 
85
        return highlight($query, $return);
86
    }
87
 
88
    /**
89
     * Defines table for managing Tiny Premium plugins.
90
     *
91
     * @return string HTML for table
92
     */
93
    public function define_manage_tiny_premium_plugins_table(): string {
94
        global $OUTPUT;
95
        $sesskey = sesskey();
96
 
97
        // Set up table.
98
        $table = new \html_table();
99
        $table->id = 'managetinypremiumpluginstable';
100
        $table->attributes['class'] = 'admintable generaltable';
101
        $table->head  = [
102
            get_string('name'),
103
            get_string('enable'),
104
        ];
105
        $table->colclasses = [
106
            'leftalign',
107
            'centeralign',
108
        ];
109
        $table->data  = [];
110
 
111
        // Keep enabled plugins on top.
112
        $plugins = manager::get_plugins();
113
        $enabledplugins = manager::get_enabled_plugins();
114
        $disabledplugins = array_diff($plugins, $enabledplugins);
115
        $plugins = array_merge($enabledplugins, $disabledplugins);
116
 
117
        foreach ($plugins as $plugin) {
118
 
119
            $pluginname = get_string('premiumplugin:' . $plugin, 'tiny_premium');
120
 
121
            // Determine plugin actions.
122
            if (manager::is_plugin_enabled($plugin)) {
123
                $action = 'disable';
124
                $icon = $OUTPUT->pix_icon('t/hide', get_string('disableplugin', 'core_admin', $pluginname));
125
                $class = '';
126
            } else {
127
                $action = 'enable';
128
                $icon = $OUTPUT->pix_icon('t/show', get_string('enableplugin', 'core_admin', $pluginname));
129
                $class = 'dimmed_text';
130
            }
131
 
132
            // Prepare a link to perform the action.
133
            $hideshowurl = new \moodle_url('/lib/editor/tiny/plugins/premium/pluginsettings.php', [
134
                'action' => $action,
135
                'plugin' => $plugin,
136
                'sesskey' => $sesskey,
137
            ]);
138
            $hideshowlink = \html_writer::link($hideshowurl, $icon);
139
 
140
            // Populate table row.
141
            $row = new \html_table_row([
142
                $pluginname,
143
                $hideshowlink,
144
            ]);
145
            $row->attributes['class'] = $class;
146
            $table->data[] = $row;
147
        }
148
 
149
        return \html_writer::table($table);
150
    }
151
}