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