Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  // This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>.
  16  
  17  namespace tool_langimport\task;
  18  
  19  /**
  20   * Ad hoc task to install one or more language packs.
  21   *
  22   * @package     tool_langimport
  23   * @category    task
  24   * @copyright   2021 David Mudrák <david@moodle.com>
  25   * @license     https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  class install_langpacks extends \core\task\adhoc_task {
  28  
  29      /**
  30       * Execute the ad hoc task.
  31       */
  32      public function execute(): void {
  33  
  34          $data = $this->get_custom_data();
  35  
  36          if (empty($data->langs)) {
  37              mtrace('No language packs to install');
  38          }
  39  
  40          get_string_manager()->reset_caches();
  41  
  42          $controller = new \tool_langimport\controller();
  43  
  44          \core_php_time_limit::raise();
  45  
  46          try {
  47              $controller->install_languagepacks($data->langs);
  48              $this->notify_user_success($controller);
  49  
  50          } catch (\Throwable $e) {
  51              $this->notify_user_error($e->getMessage());
  52  
  53          } finally {
  54              get_string_manager()->reset_caches();
  55          }
  56      }
  57  
  58      /**
  59       * Notify user that the task finished successfully.
  60       *
  61       * @param \tool_langimport\controller $controller
  62       */
  63      protected function notify_user_success(\tool_langimport\controller $controller): void {
  64  
  65          $message = new \core\message\message();
  66  
  67          $message->component = 'moodle';
  68          $message->name = 'notices';
  69          $message->userfrom = \core_user::get_noreply_user();
  70          $message->userto = $this->get_userid();
  71          $message->notification = 1;
  72          $message->contexturl = (new \moodle_url('/admin/tool/langimport/index.php'))->out(false);
  73          $message->contexturlname = get_string('pluginname', 'tool_langimport');
  74  
  75          $message->subject = get_string('installfinished', 'tool_langimport');
  76          $message->fullmessage = '* ' . implode(PHP_EOL . '* ', $controller->info);
  77          $message->fullmessageformat = FORMAT_MARKDOWN;
  78          $message->fullmessagehtml = markdown_to_html($message->fullmessage);
  79          $message->smallmessage = get_string('installfinished', 'tool_langimport');
  80  
  81          message_send($message);
  82      }
  83  
  84      /**
  85       * Notify user that the task failed.
  86       *
  87       * @param string $error The error text
  88       */
  89      protected function notify_user_error(string $error): void {
  90  
  91          $message = new \core\message\message();
  92  
  93          $message->component = 'moodle';
  94          $message->name = 'notices';
  95          $message->userfrom = \core_user::get_noreply_user();
  96          $message->userto = $this->get_userid();
  97          $message->notification = 1;
  98          $message->contexturl = (new \moodle_url('/admin/tool/langimport/index.php'))->out(false);
  99          $message->contexturlname = get_string('pluginname', 'tool_langimport');
 100  
 101          $message->subject = get_string('installfailed', 'tool_langimport');
 102          $message->fullmessage = $error;
 103          $message->fullmessageformat = FORMAT_PLAIN;
 104          $message->fullmessagehtml = text_to_html($message->fullmessage);
 105          $message->smallmessage = get_string('installfailed', 'tool_langimport');
 106  
 107          message_send($message);
 108      }
 109  }