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.
   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   * A schedule task for scheduled allocation cron.
  19   *
  20   * @package   workshopallocation_scheduled
  21   * @copyright 2019 Simey Lameze <simey@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace workshopallocation_scheduled\task;
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * The main schedule task for scheduled allocation cron.
  30   *
  31   * @package   workshopallocation_scheduled
  32   * @copyright 2019 Simey Lameze <simey@moodle.com>
  33   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class cron_task extends \core\task\scheduled_task {
  36      /**
  37       * Get a descriptive name for this task (shown to admins).
  38       *
  39       * @return string
  40       */
  41      public function get_name() {
  42          return get_string('crontask', 'workshopallocation_scheduled');
  43      }
  44  
  45      /**
  46       * Run scheduled allocation cron.
  47       */
  48      public function execute() {
  49          global $CFG, $DB;
  50  
  51          $sql = "SELECT w.*
  52                    FROM {workshopallocation_scheduled} a
  53                    JOIN {workshop} w ON a.workshopid = w.id
  54                   WHERE a.enabled = 1
  55                     AND w.phase = 20
  56                     AND w.submissionend > 0
  57                     AND w.submissionend < ?
  58                     AND (a.timeallocated IS NULL OR a.timeallocated < w.submissionend)";
  59          $workshops = $DB->get_records_sql($sql, array(time()));
  60  
  61          if (empty($workshops)) {
  62              mtrace('... no workshops awaiting scheduled allocation. ', '');
  63              return;
  64          }
  65  
  66          mtrace('... executing scheduled allocation in ' . count($workshops) . ' workshop(s) ... ', '');
  67  
  68          require_once($CFG->dirroot . '/mod/workshop/locallib.php');
  69  
  70          foreach ($workshops as $workshop) {
  71              $cm = get_coursemodule_from_instance('workshop', $workshop->id, $workshop->course, false, MUST_EXIST);
  72              $course = $DB->get_record('course', ['id' => $cm->course], '*', MUST_EXIST);
  73              $workshop = new \workshop($workshop, $cm, $course);
  74              $allocator = $workshop->allocator_instance('scheduled');
  75              $allocator->execute();
  76          }
  77      }
  78  }