Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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 run ad hoc task(s)
  19   *
  20   * This script runs a group or a single ad hoc task from the web UI.
  21   *
  22   * @package    tool_task
  23   * @copyright  Catalyst IT
  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_once(__DIR__ . '/../../../config.php');
  30  require_once($CFG->libdir.'/adminlib.php');
  31  
  32  admin_externalpage_setup('adhoctasks');
  33  
  34  $runurl = '/admin/tool/task/run_adhoctasks.php';
  35  $tasksurl = '/admin/tool/task/adhoctasks.php';
  36  
  37  // Allow execution of single task. This requires login and has different rules.
  38  $classname = optional_param('classname', null, PARAM_RAW);
  39  $failedonly = optional_param('failedonly', false, PARAM_BOOL);
  40  $taskid = optional_param('id', null, PARAM_INT);
  41  $confirmed = optional_param('confirm', 0, PARAM_INT);
  42  
  43  if (!\core\task\manager::is_runnable()) {
  44      $redirecturl = new \moodle_url('/admin/settings.php', ['section' => 'systempaths']);
  45      throw new moodle_exception('cannotfindthepathtothecli', 'tool_task', $redirecturl->out());
  46  }
  47  
  48  $params = ['classname' => $classname, 'failedonly' => $failedonly, 'id' => $taskid];
  49  
  50  // Check input parameter id against all existing tasks.
  51  if ($taskid) {
  52      $record = $DB->get_record('task_adhoc', ['id' => $taskid]);
  53      if (!$record) {
  54          throw new \moodle_exception('invalidtaskid');
  55      }
  56      $classname = $record->classname;
  57      $heading = "Run $classname task Id $taskid";
  58      $tasks = [core\task\manager::adhoc_task_from_record($record)];
  59  } else {
  60      if (!$classname) {
  61          throw new \moodle_exception('noclassname', 'tool_task');
  62      }
  63      $heading = "Run " . s($classname) . " " . ($failedonly ? "failed" : "all")." tasks";
  64      $now = time();
  65      $tasks = array_filter(
  66          core\task\manager::get_adhoc_tasks($classname, $failedonly, true),
  67          function ($t) use ($now) {
  68              return $t->get_fail_delay() || $t->get_next_run_time() <= $now;
  69          }
  70      );
  71  }
  72  
  73  // Start output.
  74  $context = context_system::instance();
  75  $PAGE->set_context($context);
  76  $PAGE->set_heading($SITE->fullname);
  77  $PAGE->set_title($classname);
  78  
  79  echo $OUTPUT->header();
  80  echo $OUTPUT->heading($heading);
  81  
  82  if (!$tasks) {
  83      echo $OUTPUT->single_button($tasksurl,
  84              get_string('notasks', 'tool_task'),
  85              'get');
  86      echo $OUTPUT->footer();
  87      exit;
  88  }
  89  
  90  $renderer = $PAGE->get_renderer('tool_task');
  91  if (!get_config('core', 'cron_enabled')) {
  92      echo $renderer->cron_disabled();
  93  }
  94  echo $renderer->adhoc_tasks_simple_table($tasks);
  95  
  96  // The initial request just shows the confirmation page; we don't do anything further unless
  97  // they confirm.
  98  if (!$confirmed) {
  99      echo $OUTPUT->confirm(get_string('runadhoc_confirm', 'tool_task'),
 100              new single_button(new moodle_url($runurl, array_merge($params, ['confirm' => 1])),
 101              get_string('runadhoc', 'tool_task')),
 102              new single_button(new moodle_url($tasksurl, $params),
 103              get_string('cancel'), false));
 104      echo $OUTPUT->footer();
 105      exit;
 106  }
 107  
 108  // Action requires session key.
 109  require_sesskey();
 110  
 111  \core\session\manager::write_close();
 112  
 113  // Prepare to handle output via mtrace.
 114  require ('lib.php');
 115  $CFG->mtrace_wrapper = 'tool_task_mtrace_wrapper';
 116  
 117  // Run the specified tasks.
 118  if ($taskid) {
 119      $repeat = $DB->get_record('task_adhoc', ['id' => $taskid]);
 120  
 121      echo html_writer::start_tag('pre');
 122      \core\task\manager::run_adhoc_from_cli($taskid);
 123      echo html_writer::end_tag('pre');
 124  } else {
 125      $repeat = core\task\manager::get_adhoc_tasks($classname, $failedonly, true);
 126  
 127      // Run failed first (if any). We have to run them separately anyway,
 128      // because faildelay is observed if failed flag is not true.
 129      echo html_writer::tag('p', get_string('runningfailedtasks', 'tool_task'), ['class' => 'lead']);
 130      echo html_writer::start_tag('pre');
 131      \core\task\manager::run_all_adhoc_from_cli(true, $classname);
 132      echo html_writer::end_tag('pre');
 133  
 134      if (!$failedonly) {
 135          echo html_writer::tag('p', get_string('runningalltasks', 'tool_task'), ['class' => 'lead']);
 136          echo html_writer::start_tag('pre');
 137          \core\task\manager::run_all_adhoc_from_cli(false, $classname);
 138          echo html_writer::end_tag('pre');
 139      }
 140  }
 141  
 142  if ($repeat) {
 143      echo html_writer::div(
 144          $OUTPUT->single_button(
 145              new moodle_url($runurl, array_merge($params, ['confirm' => 1])),
 146              get_string('runagain', 'tool_task')
 147          )
 148      );
 149  }
 150  
 151  echo html_writer::div(
 152      html_writer::link(
 153          new moodle_url($tasksurl, $taskid ? ['classname' => $classname] : []),
 154          get_string('backtoadhoctasks', 'tool_task')
 155      )
 156  );
 157  
 158  echo $OUTPUT->footer();