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  /**
  18   * Utility class for searching of content bank files.
  19   *
  20   * @package    repository_contentbank
  21   * @copyright  2020 Mihail Geshoski <mihail@moodle.com>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace repository_contentbank;
  26  
  27  /**
  28   * Represents the content bank search related functionality.
  29   *
  30   * @package    repository_contentbank
  31   * @copyright  2020 Mihail Geshoski <mihail@moodle.com>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class contentbank_search {
  35  
  36      /**
  37       * Generate and return content nodes for all content bank files that match the search criteria
  38       * and can be viewed/accessed by the user.
  39       *
  40       * @param string $search The search string
  41       * @return array[] The array containing all content file nodes that match the search criteria. Each content node is
  42       *                 an array with keys: shorttitle, title, datemodified, datecreated, author, license, isref, source,
  43       *                 icon, thumbnail.
  44       */
  45      public static function get_search_contents(string $search): array {
  46          $contentbank = new \core_contentbank\contentbank();
  47          // Return all content bank content that matches the search criteria and can be viewed/accessed by the user.
  48          $contents = $contentbank->search_contents($search);
  49          return array_reduce($contents, function($list, $content) {
  50              $contentcontext = \context::instance_by_id($content->get_content()->contextid);
  51              $browser = \repository_contentbank\helper::get_contentbank_browser($contentcontext);
  52              // If the user can access the content and content node can be created, add the node into the
  53              // search results list.
  54              if ($browser->can_access_content() &&
  55                      $contentnode = \repository_contentbank\helper::create_contentbank_content_node($content)) {
  56                  $list[] = $contentnode;
  57              }
  58              return $list;
  59          }, []);
  60      }
  61  }