Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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