Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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  declare(strict_types=1);
  18  
  19  namespace core_reportbuilder\task;
  20  
  21  use core_user;
  22  use core\task\adhoc_task;
  23  use core_reportbuilder\local\helpers\schedule as helper;
  24  use core_reportbuilder\local\models\schedule;
  25  
  26  /**
  27   * Ad-hoc task for sending a single report schedule
  28   *
  29   * @package     core_reportbuilder
  30   * @copyright   2021 Paul Holden <paulh@moodle.com>
  31   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class send_schedule extends adhoc_task {
  34  
  35      use \core\task\logging_trait;
  36  
  37      /**
  38       * Execute the task
  39       */
  40      public function execute(): void {
  41          global $CFG, $USER, $DB;
  42  
  43          [
  44              'reportid' => $reportid,
  45              'scheduleid' => $scheduleid,
  46          ] = (array) $this->get_custom_data();
  47  
  48          // Custom reports are disabled.
  49          if (empty($CFG->enablecustomreports)) {
  50              return;
  51          }
  52  
  53          $schedule = schedule::get_record(['id' => $scheduleid, 'reportid' => $reportid]);
  54          if ($schedule === false) {
  55              $this->log('Invalid schedule', 0);
  56              return;
  57          }
  58  
  59          $this->log_start('Sending schedule: ' . $schedule->get_formatted_name());
  60  
  61          $scheduleattachment = null;
  62          $originaluser = $USER;
  63  
  64          // Switch to schedule creator, and retrieve list of recipient users.
  65          cron_setup_user(core_user::get_user($schedule->get('usercreated')));
  66  
  67          $users = helper::get_schedule_report_users($schedule);
  68          if (count($users) > 0) {
  69  
  70              $scheduleuserviewas = $schedule->get('userviewas');
  71              $schedulereportempty = $schedule->get('reportempty');
  72  
  73              // Handle schedule configuration as to who the report should be viewed as.
  74              if ($scheduleuserviewas === schedule::REPORT_VIEWAS_CREATOR) {
  75                  $scheduleattachment = helper::get_schedule_report_file($schedule);
  76              } else if ($scheduleuserviewas !== schedule::REPORT_VIEWAS_RECIPIENT) {
  77                  cron_setup_user(core_user::get_user($scheduleuserviewas));
  78                  $scheduleattachment = helper::get_schedule_report_file($schedule);
  79              }
  80  
  81              // Apply special handling if report is empty (default is to send it anyway).
  82              if ($schedulereportempty === schedule::REPORT_EMPTY_DONT_SEND &&
  83                      $scheduleattachment !== null && helper::get_schedule_report_count($schedule) === 0) {
  84  
  85                  $this->log('Empty report, skipping');
  86              } else {
  87  
  88                  // Now iterate over recipient users, send the report to each.
  89                  foreach ($users as $user) {
  90                      $this->log('Sending to: ' . fullname($user, true));
  91  
  92                      // If we already created the attachment, send that. Otherwise generate per recipient.
  93                      if ($scheduleattachment !== null) {
  94                          helper::send_schedule_message($schedule, $user, $scheduleattachment);
  95                      } else {
  96                          cron_setup_user($user);
  97  
  98                          if ($schedulereportempty === schedule::REPORT_EMPTY_DONT_SEND &&
  99                              helper::get_schedule_report_count($schedule) === 0) {
 100  
 101                              $this->log('Empty report, skipping', 2);
 102                              continue;
 103                          }
 104  
 105                          $recipientattachment = helper::get_schedule_report_file($schedule);
 106                          helper::send_schedule_message($schedule, $user, $recipientattachment);
 107                          $recipientattachment->delete();
 108                      }
 109                  }
 110              }
 111          }
 112  
 113          // Finish, clean up (set persistent property manually to avoid updating it's user/time modified data).
 114          $DB->set_field($schedule::TABLE, 'timelastsent', time(), ['id' => $schedule->get('id')]);
 115  
 116          if ($scheduleattachment !== null) {
 117              $scheduleattachment->delete();
 118          }
 119  
 120          $this->log_finish('Sending schedule complete');
 121  
 122          // Restore cron user to original state.
 123          cron_setup_user($originaluser);
 124      }
 125  }