| 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 stdClass;
  | 
        
        
            | 
            | 
           20 | 
           use tool_admin_presets\form\continue_form;
  | 
        
        
            | 
            | 
           21 | 
              | 
        
        
            | 
            | 
           22 | 
           /**
  | 
        
        
            | 
            | 
           23 | 
            * This class extends base class and handles rollback function.
  | 
        
        
            | 
            | 
           24 | 
            *
  | 
        
        
            | 
            | 
           25 | 
            * @package          tool_admin_presets
  | 
        
        
            | 
            | 
           26 | 
            * @copyright        2021 Pimenko <support@pimenko.com><pimenko.com>
  | 
        
        
            | 
            | 
           27 | 
            * @author           Jordan Kesraoui | Sylvain Revenu | Pimenko based on David Monllaó <david.monllao@urv.cat> code
  | 
        
        
            | 
            | 
           28 | 
            * @license          http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           29 | 
            */
  | 
        
        
            | 
            | 
           30 | 
           class rollback extends base {
  | 
        
        
            | 
            | 
           31 | 
              | 
        
        
            | 
            | 
           32 | 
               /**
  | 
        
        
            | 
            | 
           33 | 
                * Displays the different previous applications of the preset
  | 
        
        
            | 
            | 
           34 | 
                */
  | 
        
        
            | 
            | 
           35 | 
               public function show(): void {
  | 
        
        
            | 
            | 
           36 | 
                   global $DB, $OUTPUT;
  | 
        
        
            | 
            | 
           37 | 
              | 
        
        
            | 
            | 
           38 | 
                   // Preset data.
  | 
        
        
            | 
            | 
           39 | 
                   $preset = $DB->get_record('adminpresets', ['id' => $this->id]);
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
                   // Applications data.
  | 
        
        
            | 
            | 
           42 | 
                   $context = new stdClass();
  | 
        
        
            | 
            | 
           43 | 
                   $applications = $DB->get_records('adminpresets_app', ['adminpresetid' => $this->id], 'time DESC');
  | 
        
        
            | 
            | 
           44 | 
                   $context->noapplications = !empty($applications);
  | 
        
        
            | 
            | 
           45 | 
                   $context->applications = [];
  | 
        
        
            | 
            | 
           46 | 
                   foreach ($applications as $application) {
  | 
        
        
            | 
            | 
           47 | 
                       $format = get_string('strftimedatetime', 'langconfig');
  | 
        
        
            | 
            | 
           48 | 
                       $user = $DB->get_record('user', ['id' => $application->userid]);
  | 
        
        
            | 
            | 
           49 | 
                       $rollbacklink = new \moodle_url(
  | 
        
        
            | 
            | 
           50 | 
                           '/admin/tool/admin_presets/index.php',
  | 
        
        
            | 
            | 
           51 | 
                           ['action' => 'rollback', 'mode' => 'execute', 'id' => $application->id, 'sesskey' => sesskey()]
  | 
        
        
            | 
            | 
           52 | 
                       );
  | 
        
        
            | 
            | 
           53 | 
              | 
        
        
            | 
            | 
           54 | 
                       $context->applications[] = [
  | 
        
        
            | 
            | 
           55 | 
                           'timeapplied' => \core_date::strftime($format, (int)$application->time),
  | 
        
        
            | 
            | 
           56 | 
                           'user' => fullname($user),
  | 
        
        
            | 
            | 
           57 | 
                           'action' => $rollbacklink->out(false),
  | 
        
        
            | 
            | 
           58 | 
                       ];
  | 
        
        
            | 
            | 
           59 | 
                   }
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
            | 
            | 
           61 | 
                   $this->outputs .= '<br/>' . $OUTPUT->heading(get_string('presetname', 'tool_admin_presets') . ': ' . $preset->name, 3);
  | 
        
        
            | 
            | 
           62 | 
                   $this->outputs = $OUTPUT->render_from_template('tool_admin_presets/preset_applications_list', $context);
  | 
        
        
            | 
            | 
           63 | 
              | 
        
        
            | 
            | 
           64 | 
                   $url = new \moodle_url('/admin/tool/admin_presets/index.php');
  | 
        
        
            | 
            | 
           65 | 
                   $this->moodleform = new continue_form($url);
  | 
        
        
            | 
            | 
           66 | 
               }
  | 
        
        
            | 
            | 
           67 | 
              | 
        
        
            | 
            | 
           68 | 
               /**
  | 
        
        
            | 
            | 
           69 | 
                * Executes the application rollback
  | 
        
        
            | 
            | 
           70 | 
                *
  | 
        
        
            | 
            | 
           71 | 
                * Each setting value is checked against the config_log->value
  | 
        
        
            | 
            | 
           72 | 
                */
  | 
        
        
            | 
            | 
           73 | 
               public function execute(): void {
  | 
        
        
            | 
            | 
           74 | 
                   global $OUTPUT;
  | 
        
        
            | 
            | 
           75 | 
              | 
        
        
            | 
            | 
           76 | 
                   require_sesskey();
  | 
        
        
            | 
            | 
           77 | 
              | 
        
        
            | 
            | 
           78 | 
                   list($presetapp, $rollback, $failures) = $this->manager->revert_preset($this->id);
  | 
        
        
            | 
            | 
           79 | 
              | 
        
        
            | 
            | 
           80 | 
                   if (!is_null($presetapp)) {
  | 
        
        
            | 
            | 
           81 | 
                       // Change $this->id to point to the preset.
  | 
        
        
            | 
            | 
           82 | 
                       $this->id = $presetapp->adminpresetid;
  | 
        
        
            | 
            | 
           83 | 
                   }
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
                   $appliedchanges = new stdClass();
  | 
        
        
            | 
            | 
           86 | 
                   $appliedchanges->show = !empty($rollback);
  | 
        
        
            | 
            | 
           87 | 
                   $appliedchanges->caption = get_string('rollbackresults', 'tool_admin_presets');
  | 
        
        
            | 
            | 
           88 | 
                   $appliedchanges->settings = $rollback;
  | 
        
        
            | 
            | 
           89 | 
              | 
        
        
            | 
            | 
           90 | 
                   $skippedchanges = new stdClass();
  | 
        
        
            | 
            | 
           91 | 
                   $skippedchanges->show = !empty($failures);
  | 
        
        
            | 
            | 
           92 | 
                   $skippedchanges->caption = get_string('rollbackfailures', 'tool_admin_presets');
  | 
        
        
            | 
            | 
           93 | 
                   $skippedchanges->settings = $failures;
  | 
        
        
            | 
            | 
           94 | 
              | 
        
        
            | 
            | 
           95 | 
                   $data = new stdClass();
  | 
        
        
            | 
            | 
           96 | 
                   $data->appliedchanges = $appliedchanges;
  | 
        
        
            | 
            | 
           97 | 
                   $data->skippedchanges = $skippedchanges;
  | 
        
        
            | 
            | 
           98 | 
                   $data->beforeapplying = true;
  | 
        
        
            | 
            | 
           99 | 
                   $this->outputs = $OUTPUT->render_from_template('tool_admin_presets/settings_rollback', $data);
  | 
        
        
            | 
            | 
           100 | 
              | 
        
        
            | 
            | 
           101 | 
                   $url = new \moodle_url('/admin/tool/admin_presets/index.php');
  | 
        
        
            | 
            | 
           102 | 
                   $this->moodleform = new continue_form($url);
  | 
        
        
            | 
            | 
           103 | 
               }
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
               protected function get_title(): string {
  | 
        
        
            | 
            | 
           106 | 
                   global $DB;
  | 
        
        
            | 
            | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
                   $title = '';
  | 
        
        
            | 
            | 
           109 | 
                   if ($preset = $DB->get_record('adminpresets', ['id' => $this->id])) {
  | 
        
        
            | 
            | 
           110 | 
                       $title = get_string($this->action . $this->mode, 'tool_admin_presets', $preset->name);
  | 
        
        
            | 
            | 
           111 | 
                   }
  | 
        
        
            | 
            | 
           112 | 
              | 
        
        
            | 
            | 
           113 | 
                   return $title;
  | 
        
        
            | 
            | 
           114 | 
               }
  | 
        
        
            | 
            | 
           115 | 
              | 
        
        
            | 
            | 
           116 | 
               protected function get_explanatory_description(): ?string {
  | 
        
        
            | 
            | 
           117 | 
                   $text = null;
  | 
        
        
            | 
            | 
           118 | 
                   if ($this->mode == 'show') {
  | 
        
        
            | 
            | 
           119 | 
                       $text = get_string('rollbackdescription', 'tool_admin_presets');
  | 
        
        
            | 
            | 
           120 | 
                   }
  | 
        
        
            | 
            | 
           121 | 
              | 
        
        
            | 
            | 
           122 | 
                   return $text;
  | 
        
        
            | 
            | 
           123 | 
               }
  | 
        
        
            | 
            | 
           124 | 
           }
  |