Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 and 403]

   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   * Web cron single task
  19   *
  20   * This script runs a single scheduled task from the web UI.
  21   *
  22   * @package tool_task
  23   * @copyright 2016 The Open University
  24   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  define('NO_OUTPUT_BUFFERING', true);
  28  
  29  require('../../../config.php');
  30  
  31  // Allow execution of single task. This requires login and has different rules.
  32  $taskname = required_param('task', PARAM_RAW_TRIMMED);
  33  
  34  // Basic security checks.
  35  require_admin();
  36  $context = context_system::instance();
  37  
  38  // Check input parameter against all existing tasks (this ensures it isn't possible to
  39  // create some kind of security problem by specifying a class that isn't a task or whatever).
  40  $task = \core\task\manager::get_scheduled_task($taskname);
  41  if (!$task) {
  42      throw new moodle_exception('cannotfindinfo', 'error', new moodle_url('/admin/tool/task/scheduledtasks.php'), $taskname);
  43  }
  44  
  45  if (!\core\task\manager::is_runnable()) {
  46      $redirecturl = new \moodle_url('/admin/settings.php', ['section' => 'systempaths']);
  47      throw new moodle_exception('cannotfindthepathtothecli', 'tool_task', $redirecturl->out());
  48  }
  49  
  50  if (!get_config('tool_task', 'enablerunnow') || !$task->can_run()) {
  51      throw new moodle_exception('nopermissions', 'error', new moodle_url('/admin/tool/task/scheduledtasks.php'),
  52          get_string('runnow', 'tool_task'), $task->get_name());
  53  }
  54  
  55  // Start output.
  56  $PAGE->set_url(new moodle_url('/admin/tool/task/schedule_task.php', ['task' => $taskname]));
  57  $PAGE->set_context($context);
  58  $PAGE->set_heading($SITE->fullname);
  59  $PAGE->set_title($task->get_name());
  60  
  61  navigation_node::override_active_url(new moodle_url('/admin/tool/task/scheduledtasks.php'));
  62  $PAGE->navbar->add(s($task->get_name()));
  63  
  64  echo $OUTPUT->header();
  65  echo $OUTPUT->heading($task->get_name());
  66  
  67  // The initial request just shows the confirmation page; we don't do anything further unless
  68  // they confirm.
  69  if (!optional_param('confirm', 0, PARAM_INT)) {
  70      echo $OUTPUT->confirm(get_string('runnow_confirm', 'tool_task', $task->get_name()),
  71              new single_button(new moodle_url('/admin/tool/task/schedule_task.php',
  72                      ['task' => $taskname, 'confirm' => 1, 'sesskey' => sesskey()]),
  73              get_string('runnow', 'tool_task')),
  74              new single_button(new moodle_url('/admin/tool/task/scheduledtasks.php',
  75                      ['lastchanged' => get_class($task)]),
  76              get_string('cancel'), false));
  77      echo $OUTPUT->footer();
  78      exit;
  79  }
  80  
  81  // Action requires session key.
  82  require_sesskey();
  83  
  84  \core\session\manager::write_close();
  85  
  86  // Prepare to handle output via mtrace.
  87  echo html_writer::start_tag('pre');
  88  require ('lib.php');
  89  $CFG->mtrace_wrapper = 'tool_task_mtrace_wrapper';
  90  
  91  // Run the specified task (this will output an error if it doesn't exist).
  92  \core\task\manager::run_from_cli($task);
  93  
  94  echo html_writer::end_tag('pre');
  95  
  96  $output = $PAGE->get_renderer('tool_task');
  97  
  98  // Re-run the specified task (this will output an error if it doesn't exist).
  99  echo $OUTPUT->single_button(new moodle_url('/admin/tool/task/schedule_task.php',
 100          array('task' => $taskname, 'confirm' => 1, 'sesskey' => sesskey())),
 101          get_string('runagain', 'tool_task'));
 102  echo $output->link_back(get_class($task));
 103  
 104  echo $OUTPUT->footer();