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.
   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 repository_googledocs\local\browser;
  18  
  19  use repository_googledocs\googledocs_content;
  20  use repository_googledocs\helper;
  21  
  22  /**
  23   * Utility class for browsing content from or within a specified google drive.
  24   *
  25   * This class is responsible for generating the content that would be displayed for a specified drive such as
  26   * 'my drive' or any existing shared drive. It also supports generating data for paths which are located
  27   * within a given drive.
  28   *
  29   * @package    repository_googledocs
  30   * @copyright  2021 Mihail Geshoski <mihail@moodle.com>
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class googledocs_drive_content extends googledocs_content {
  34  
  35      /**
  36       * Returns all relevant contents based on the given path or search query.
  37       *
  38       * The method fetches all existing content (files and folders) located in a specific folder under a given drive
  39       * through an API call.
  40       *
  41       * @param string $query The search query
  42       * @return array The array containing the contents
  43       */
  44      protected function get_contents(string $query): array {
  45  
  46          $id = str_replace("'", "\'", $query);
  47          // Define the parameters required by the API call.
  48          // Query all files and folders which have not been trashed and located directly in the folder whose ID is $id.
  49          $q = "'{$id}' in parents AND trashed = false";
  50          // The file fields that should be returned in the response.
  51          $fields = 'files(id,name,mimeType,webContentLink,webViewLink,fileExtension,modifiedTime,size,iconLink)';
  52  
  53          $params = [
  54              'q' => $q,
  55              'fields' => $fields,
  56              'spaces' => 'drive',
  57          ];
  58  
  59          // Check whether there are any shared drives.
  60          $response = helper::request($this->service, 'shared_drives_list', []);
  61          if (!empty($response->drives)) {
  62              // To be able to include content from shared drives, we need to enable 'supportsAllDrives' and
  63              // 'includeItemsFromAllDrives'. The Google Drive API requires explicit request for inclusion of content from
  64              // shared drives and also a confirmation that the application is designed to handle files on shared drives.
  65              $params['supportsAllDrives'] = 'true';
  66              $params['includeItemsFromAllDrives'] = 'true';
  67          }
  68  
  69          // Request the content through the API call.
  70          $response = helper::request($this->service, 'list', $params);
  71  
  72          return $response->files ?? [];
  73      }
  74  }