Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

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