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