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.

Differences Between: [Versions 400 and 403] [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  namespace mod_bigbluebuttonbn\external;
  18  
  19  use core_external\external_api;
  20  use core_external\external_function_parameters;
  21  use core_external\external_multiple_structure;
  22  use core_external\external_single_structure;
  23  use core_external\external_value;
  24  use core_external\external_warnings;
  25  use mod_bigbluebuttonbn\instance;
  26  use mod_bigbluebuttonbn\local\bigbluebutton\recordings\recording_data;
  27  use mod_bigbluebuttonbn\recording;
  28  
  29  /**
  30   * External service to fetch a list of recordings from the BBB service.
  31   *
  32   * @package   mod_bigbluebuttonbn
  33   * @category  external
  34   * @copyright 2018 onwards, Blindside Networks Inc
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class get_recordings_to_import extends external_api {
  38      /**
  39       * Returns description of method parameters
  40       *
  41       * @return external_function_parameters
  42       */
  43      public static function execute_parameters(): external_function_parameters {
  44          return new external_function_parameters([
  45              'destinationinstanceid' => new external_value(
  46                  PARAM_INT,
  47                  'Id of the other BBB we target for importing recordings into.
  48                  The idea here is to remove already imported recordings.',
  49                  VALUE_REQUIRED
  50              ),
  51              'sourcebigbluebuttonbnid' => new external_value(PARAM_INT,
  52                  'bigbluebuttonbn instance id',
  53                  VALUE_DEFAULT,
  54                  0),
  55              'sourcecourseid' => new external_value(PARAM_INT,
  56                  'source courseid to filter by',
  57                  VALUE_DEFAULT,
  58                  0),
  59              'tools' => new external_value(PARAM_RAW, 'a set of enabled tools', VALUE_DEFAULT,
  60                  'protect,unprotect,publish,unpublish,delete'),
  61              'groupid' => new external_value(PARAM_INT, 'Group ID', VALUE_DEFAULT, null),
  62          ]);
  63      }
  64  
  65      /**
  66       * Get a list of recordings
  67       *
  68       * @param int $destinationinstanceid the bigbluebuttonbn instance id where recordings have been already imported.
  69       * @param int|null $sourcebigbluebuttonbnid the bigbluebuttonbn instance id to which the recordings are referred.
  70       * @param int|null $sourcecourseid the source courseid to filter by
  71       * @param string|null $tools
  72       * @param int|null $groupid
  73       * @return array of warnings and status result
  74       * @throws \invalid_parameter_exception
  75       * @throws \restricted_context_exception
  76       */
  77      public static function execute(
  78          int $destinationinstanceid,
  79          ?int $sourcebigbluebuttonbnid = 0,
  80          ?int $sourcecourseid = 0,
  81          ?string $tools = 'protect,unprotect,publish,unpublish,delete',
  82          ?int $groupid = null
  83      ): array {
  84          global $USER, $DB;
  85  
  86          $returnval = [
  87              'status' => false,
  88              'warnings' => [],
  89          ];
  90  
  91          // Validate the sourcebigbluebuttonbnid ID.
  92          [
  93              'destinationinstanceid' => $destinationinstanceid,
  94              'sourcebigbluebuttonbnid' => $sourcebigbluebuttonbnid,
  95              'sourcecourseid' => $sourcecourseid,
  96              'tools' => $tools,
  97              'groupid' => $groupid
  98          ] = self::validate_parameters(self::execute_parameters(), [
  99              'destinationinstanceid' => $destinationinstanceid,
 100              'sourcebigbluebuttonbnid' => $sourcebigbluebuttonbnid,
 101              'sourcecourseid' => $sourcecourseid,
 102              'tools' => $tools,
 103              'groupid' => $groupid
 104          ]);
 105  
 106          $tools = explode(',', $tools ?? 'protect,unprotect,publish,unpublish,delete');
 107  
 108          // Fetch the session, features, and profile.
 109          $sourceinstance = null;
 110          $sourcecourse = null;
 111          if ($sourcecourseid) {
 112              $sourcecourse = $DB->get_record('course', ['id' => $sourcecourseid], '*', MUST_EXIST);
 113          }
 114  
 115          if (!empty($sourcebigbluebuttonbnid)) {
 116              $sourceinstance = instance::get_from_instanceid($sourcebigbluebuttonbnid);
 117              if (!$sourceinstance) {
 118                  throw new \invalid_parameter_exception('Source Bigbluebutton Id is invalid');
 119              }
 120              $sourcecourse = $sourceinstance->get_course();
 121              // Validate that the user has access to this activity.
 122              self::validate_context($sourceinstance->get_context());
 123          }
 124          $destinstance = instance::get_from_instanceid($destinationinstanceid);
 125          // Validate that the user has access to this activity.
 126          self::validate_context($destinstance->get_context());
 127          if (!$destinstance->user_has_group_access($USER, $groupid)) {
 128              throw new \invalid_parameter_exception('Invalid group for this user ' . $groupid);
 129          }
 130          if ($groupid) {
 131              $destinstance->set_group_id($groupid);
 132          }
 133          // Exclude itself from the list if in import mode.
 134          $excludedids = [$destinstance->get_instance_id()];
 135          if ($sourceinstance) {
 136              $recordings = $sourceinstance->get_recordings($excludedids);
 137          } else {
 138              // There is a course id or a 0, so we fetch all recording including deleted recordings from this course.
 139              $recordings = recording::get_recordings_for_course(
 140                  $sourcecourseid,
 141                  $excludedids,
 142                  true,
 143                  false,
 144                  ($sourcecourseid == 0 || $sourcebigbluebuttonbnid == 0),
 145                  ($sourcecourseid == 0 || $sourcebigbluebuttonbnid == 0)
 146              );
 147          }
 148  
 149          if ($destinationinstanceid) {
 150              // Remove recording already imported in this specific activity.
 151              $destinationinstance = instance::get_from_instanceid($destinationinstanceid);
 152              $importedrecordings = recording::get_recordings_for_instance(
 153                  $destinationinstance,
 154                  true,
 155                  true
 156              );
 157              // Unset from $recordings if recording is already imported.
 158              // Recording $recordings are indexed by $id (moodle table column id).
 159              foreach ($recordings as $index => $recording) {
 160                  foreach ($importedrecordings as $irecord) {
 161                      if ($irecord->get('recordingid') == $recording->get('recordingid')) {
 162                          unset($recordings[$index]);
 163                      }
 164                  }
 165              }
 166          }
 167          $tabledata = recording_data::get_recording_table($recordings, $tools, $sourceinstance, $sourcecourseid);
 168          $returnval['tabledata'] = $tabledata;
 169          $returnval['status'] = true;
 170  
 171          return $returnval;
 172      }
 173  
 174      /**
 175       * Describe the return structure of the external service.
 176       *
 177       * @return external_single_structure
 178       * @since Moodle 3.0
 179       */
 180      public static function execute_returns(): external_single_structure {
 181          return new external_single_structure([
 182              'status' => new external_value(PARAM_BOOL, 'Whether the fetch was successful'),
 183              'tabledata' => new external_single_structure([
 184                  'activity' => new external_value(PARAM_ALPHANUMEXT),
 185                  'ping_interval' => new external_value(PARAM_INT),
 186                  'locale' => new external_value(PARAM_TEXT),
 187                  'profile_features' => new external_multiple_structure(new external_value(PARAM_TEXT)),
 188                  'columns' => new external_multiple_structure(new external_single_structure([
 189                      'key' => new external_value(PARAM_ALPHA),
 190                      'label' => new external_value(PARAM_TEXT),
 191                      'width' => new external_value(PARAM_ALPHANUMEXT),
 192                      // See https://datatables.net/reference/option/columns.type .
 193                      'type' => new external_value(PARAM_ALPHANUMEXT, 'Column type', VALUE_OPTIONAL),
 194                      'sortable' => new external_value(PARAM_BOOL, 'Whether this column is sortable', VALUE_OPTIONAL, false),
 195                      'allowHTML' => new external_value(PARAM_BOOL, 'Whether this column contains HTML', VALUE_OPTIONAL, false),
 196                  ])),
 197                  'data' => new external_value(PARAM_RAW), // For now it will be json encoded.
 198              ], '', VALUE_OPTIONAL),
 199              'warnings' => new external_warnings()
 200          ]);
 201      }
 202  }