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 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\external\audiences;
  20  
  21  use core_reportbuilder\local\audiences\base;
  22  use external_api;
  23  use external_function_parameters;
  24  use external_value;
  25  use core_reportbuilder\manager;
  26  use core_reportbuilder\permission;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once("{$CFG->libdir}/externallib.php");
  32  
  33  /**
  34   * External method for deleting a report audience
  35   *
  36   * @package     core_reportbuilder
  37   * @copyright   2021 David Matamoros <davidmc@moodle.com>
  38   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class delete extends external_api {
  41  
  42      /**
  43       * Describes the parameters for get_users_courses.
  44       *
  45       * @return external_function_parameters
  46       */
  47      public static function execute_parameters(): external_function_parameters {
  48          return new external_function_parameters(
  49              [
  50                  'reportid' => new external_value(PARAM_INT, 'Report id'),
  51                  'instanceid' => new external_value(PARAM_INT, 'Audience instance id'),
  52              ]
  53          );
  54      }
  55  
  56      /**
  57       * External function to delete a report audience instance.
  58       *
  59       * @param int $reportid
  60       * @param int $instanceid
  61       * @return bool
  62       */
  63      public static function execute(int $reportid, int $instanceid): bool {
  64          [
  65              'reportid' => $reportid,
  66              'instanceid' => $instanceid,
  67          ] = self::validate_parameters(self::execute_parameters(), [
  68              'reportid' => $reportid,
  69              'instanceid' => $instanceid,
  70          ]);
  71  
  72          $report = manager::get_report_from_id($reportid);
  73  
  74          self::validate_context($report->get_context());
  75          permission::require_can_edit_report($report->get_report_persistent());
  76  
  77          $baseinstance = base::instance($instanceid);
  78          if ($baseinstance && $baseinstance->user_can_edit()) {
  79              $persistent = $baseinstance->get_persistent();
  80              $persistent->delete();
  81              return true;
  82          }
  83  
  84          return false;
  85      }
  86  
  87      /**
  88       * Describes the data returned from the external function.
  89       *
  90       * @return external_value
  91       */
  92      public static function execute_returns(): external_value {
  93          return new external_value(PARAM_BOOL, '', VALUE_REQUIRED);
  94      }
  95  }