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 tool_admin_presets\local\action;
18
 
19
use moodle_exception;
20
use stdClass;
21
use tool_admin_presets\form\continue_form;
22
use tool_admin_presets\form\load_form;
23
use tool_admin_presets\output\presets_list;
24
 
25
/**
26
 * This class extends base class and handles load function.
27
 *
28
 * @package          tool_admin_presets
29
 * @copyright        2021 Pimenko <support@pimenko.com><pimenko.com>
30
 * @author           Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
31
 * @license          http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class load extends base {
34
 
35
    /**
36
     * Executes the settings load into the system
37
     */
38
    public function execute(): void {
39
        global $OUTPUT;
40
 
41
        $url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'load', 'mode' => 'execute']);
42
        $this->moodleform = new load_form($url);
43
 
44
        if ($this->moodleform->is_cancelled()) {
45
            redirect(new \moodle_url('/admin/tool/admin_presets/index.php?action=base'));
46
        }
47
 
48
        if ($this->moodleform->is_submitted() && $this->moodleform->is_validated() && ($this->moodleform->get_data())) {
49
            // Apply preset settings and set plugins visibility.
50
            [$applied, $skipped] = $this->manager->apply_preset($this->id);
51
 
52
            if (empty($applied)) {
53
                $message = [
54
                    'message' => get_string('nothingloaded', 'tool_admin_presets'),
55
                    'closebutton' => true,
56
                    'announce' => true,
57
                ];
58
            } else {
59
                $message = [
60
                    'message' => get_string('settingsappliednotification', 'tool_admin_presets'),
61
                    'closebutton' => true,
62
                    'announce' => true,
63
                ];
64
            }
65
            $application = new stdClass();
66
            $applieddata = new stdClass();
67
            $applieddata->show = !empty($applied);
68
            $applieddata->message = $message;
69
            $applieddata->heading = get_string('settingsapplied', 'tool_admin_presets');
70
            $applieddata->caption = get_string('settingsapplied', 'tool_admin_presets');
71
            $applieddata->settings = $applied;
72
            $application->appliedchanges = $applieddata;
73
 
74
            $skippeddata = new stdClass();
75
            $skippeddata->show = !empty($skipped);
76
            $skippeddata->heading = get_string('settingsnotapplied', 'tool_admin_presets');
77
            $skippeddata->caption = get_string('settingsnotapplicable', 'tool_admin_presets');
78
            $skippeddata->settings = $skipped;
79
            $application->skippedchanges = $skippeddata;
80
 
81
            $this->outputs = $OUTPUT->render_from_template('tool_admin_presets/settings_application', $application);
82
            $url = new \moodle_url('/admin/tool/admin_presets/index.php');
83
            $this->moodleform = new continue_form($url);
84
        }
85
    }
86
 
87
    /**
88
     * Displays the select preset settings to select what to import.
89
     * Loads the preset data and displays a settings tree.
90
     *
91
     * It checks the Moodle version and it only allows users to import
92
     * the preset available settings.
93
     */
94
    public function show(): void {
95
        $this->display_preset(true);
96
    }
97
 
98
    /**
99
     * Displays a preset information (name, description, settings different from the current configuration...).
100
     */
101
    public function preview(): void {
102
        $this->display_preset(false, false);
103
    }
104
 
105
    /**
106
     * Method to prepare the information to preview/load the preset.
107
     *
108
     * @param bool $displayform Whether the form should be displayed in the page or not.
109
     * @param bool $raiseexception Whether the exception should be raised or not when the preset doesn't exist. When it's set
110
     * to false, a message is displayed, instead of raising the exception.
111
     */
112
    protected function display_preset(bool $displayform = true, bool $raiseexception = true) {
113
        global $DB, $OUTPUT;
114
 
115
        $data = new stdClass();
116
        $data->id = $this->id;
117
 
118
        // Preset data.
119
        if (!$preset = $DB->get_record('adminpresets', ['id' => $data->id])) {
120
            if ($raiseexception) {
121
                throw new moodle_exception('errornopreset', 'core_adminpresets');
122
            } else {
123
                $this->outputs = get_string('errornopreset', 'core_adminpresets');
124
                return;
125
            }
126
        }
127
 
128
        // Print preset basic data.
129
        $list = new presets_list([$preset]);
130
        $this->outputs = $OUTPUT->render($list);
131
 
132
        // Simulate preset application to display settings and plugins that will change.
133
        [$applied] = $this->manager->apply_preset($this->id, true);
134
 
135
        // Order the applied array by the visiblename column.
136
        if (!empty($applied)) {
137
            $visiblenamecolumn = array_column($applied, 'visiblename');
138
            array_multisort($visiblenamecolumn, SORT_ASC, $applied);
139
        }
140
 
141
        $application = new stdClass();
142
        $applieddata = new stdClass();
143
        $applieddata->show = !empty($applied);
144
        $applieddata->heading = get_string('settingstobeapplied', 'tool_admin_presets');
145
        $applieddata->caption = get_string('settingsapplied', 'tool_admin_presets');
146
        $applieddata->settings = $applied;
147
        $applieddata->beforeapplying = true;
148
        $application->appliedchanges = $applieddata;
149
        if ($displayform) {
150
            if (empty($applied)) {
151
                // Display a warning when no settings will be applied.
152
                $applieddata->message = get_string('nosettingswillbeapplied', 'tool_admin_presets');
153
 
154
                // Only display the Continue button.
155
                $url = new \moodle_url('/admin/tool/admin_presets/index.php');
156
                $this->moodleform = new continue_form($url);
157
            } else {
158
                // Display the form to apply the preset.
159
                $url = new \moodle_url('/admin/tool/admin_presets/index.php', ['action' => 'load', 'mode' => 'execute']);
160
                $this->moodleform = new load_form($url);
161
                $this->moodleform->set_data($data);
162
            }
163
        }
164
 
165
        $this->outputs .= $OUTPUT->render_from_template('tool_admin_presets/settings_application', $application);
166
    }
167
 
168
    protected function get_explanatory_description(): ?string {
169
        $text = null;
170
        if ($this->mode == 'show') {
171
            $text = get_string('loaddescription', 'tool_admin_presets');
172
        }
173
 
174
        return $text;
175
    }
176
}