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 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * This plugin is used to access wikimedia files
  20   *
  21   * @since Moodle 2.0
  22   * @package    repository_wikimedia
  23   * @copyright  2010 Dongsheng Cai {@link http://dongsheng.org}
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  require_once($CFG->dirroot . '/repository/lib.php');
  27  require_once (__DIR__ . '/wikimedia.php');
  28  
  29  /**
  30   * repository_wikimedia class
  31   * This is a class used to browse images from wikimedia
  32   *
  33   * @since Moodle 2.0
  34   * @package    repository_wikimedia
  35   * @copyright  2009 Dongsheng Cai {@link http://dongsheng.org}
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  
  39  class repository_wikimedia extends repository {
  40  
  41      /** @var string keyword search. */
  42      protected $keyword;
  43  
  44      /**
  45       * Returns maximum width for images
  46       *
  47       * Takes the maximum width for images eithre from search form or from
  48       * user preferences, updates user preferences if needed
  49       *
  50       * @return int
  51       */
  52      public function get_maxwidth() {
  53          $param = optional_param('wikimedia_maxwidth', 0, PARAM_INT);
  54          $pref = get_user_preferences('repository_wikimedia_maxwidth', WIKIMEDIA_IMAGE_SIDE_LENGTH);
  55          if ($param > 0 && $param != $pref) {
  56              $pref = $param;
  57              set_user_preference('repository_wikimedia_maxwidth', $pref);
  58          }
  59          return $pref;
  60      }
  61  
  62      /**
  63       * Returns maximum height for images
  64       *
  65       * Takes the maximum height for images eithre from search form or from
  66       * user preferences, updates user preferences if needed
  67       *
  68       * @return int
  69       */
  70      public function get_maxheight() {
  71          $param = optional_param('wikimedia_maxheight', 0, PARAM_INT);
  72          $pref = get_user_preferences('repository_wikimedia_maxheight', WIKIMEDIA_IMAGE_SIDE_LENGTH);
  73          if ($param > 0 && $param != $pref) {
  74              $pref = $param;
  75              set_user_preference('repository_wikimedia_maxheight', $pref);
  76          }
  77          return $pref;
  78      }
  79  
  80      public function get_listing($path = '', $page = '') {
  81          $client = new wikimedia;
  82          $list = array();
  83          $list['page'] = (int)$page;
  84          if ($list['page'] < 1) {
  85              $list['page'] = 1;
  86          }
  87          $list['list'] = $client->search_images($this->keyword, $list['page'] - 1,
  88                  array('iiurlwidth' => $this->get_maxwidth(),
  89                      'iiurlheight' => $this->get_maxheight()));
  90          $list['nologin'] = true;
  91          $list['norefresh'] = true;
  92          $list['nosearch'] = true;
  93          if (!empty($list['list'])) {
  94              $list['pages'] = -1; // means we don't know exactly how many pages there are but we can always jump to the next page
  95          } else if ($list['page'] > 1) {
  96              $list['pages'] = $list['page']; // no images available on this page, this is the last page
  97          } else {
  98              $list['pages'] = 0; // no paging
  99          }
 100          return $list;
 101      }
 102     // login
 103      public function check_login() {
 104          global $SESSION;
 105          $this->keyword = optional_param('wikimedia_keyword', '', PARAM_RAW);
 106          if (empty($this->keyword)) {
 107              $this->keyword = optional_param('s', '', PARAM_RAW);
 108          }
 109          $sess_keyword = 'wikimedia_'.$this->id.'_keyword';
 110          if (empty($this->keyword) && optional_param('page', '', PARAM_RAW)) {
 111              // This is the request of another page for the last search, retrieve the cached keyword.
 112              if (isset($SESSION->{$sess_keyword})) {
 113                  $this->keyword = $SESSION->{$sess_keyword};
 114              }
 115          } else if (!empty($this->keyword)) {
 116              // Save the search keyword in the session so we can retrieve it later.
 117              $SESSION->{$sess_keyword} = $this->keyword;
 118          }
 119          return !empty($this->keyword);
 120      }
 121      // if check_login returns false,
 122      // this function will be called to print a login form.
 123      public function print_login() {
 124          $keyword = new stdClass();
 125          $keyword->label = get_string('keyword', 'repository_wikimedia').': ';
 126          $keyword->id    = 'input_text_keyword';
 127          $keyword->type  = 'text';
 128          $keyword->name  = 'wikimedia_keyword';
 129          $keyword->value = '';
 130          $maxwidth = array(
 131              'label' => get_string('maxwidth', 'repository_wikimedia').': ',
 132              'type' => 'text',
 133              'name' => 'wikimedia_maxwidth',
 134              'value' => get_user_preferences('repository_wikimedia_maxwidth', WIKIMEDIA_IMAGE_SIDE_LENGTH),
 135          );
 136          $maxheight = array(
 137              'label' => get_string('maxheight', 'repository_wikimedia').': ',
 138              'type' => 'text',
 139              'name' => 'wikimedia_maxheight',
 140              'value' => get_user_preferences('repository_wikimedia_maxheight', WIKIMEDIA_IMAGE_SIDE_LENGTH),
 141          );
 142          if ($this->options['ajax']) {
 143              $form = array();
 144              $form['login'] = array($keyword, (object)$maxwidth, (object)$maxheight);
 145              $form['nologin'] = true;
 146              $form['norefresh'] = true;
 147              $form['nosearch'] = true;
 148              $form['allowcaching'] = false; // indicates that login form can NOT
 149              // be cached in filepicker.js (maxwidth and maxheight are dynamic)
 150              return $form;
 151          } else {
 152              echo <<<EOD
 153  <table>
 154  <tr>
 155  <td>{$keyword->label}</td><td><input name="{$keyword->name}" type="text" /></td>
 156  </tr>
 157  </table>
 158  <input type="submit" />
 159  EOD;
 160          }
 161      }
 162      //search
 163      // if this plugin support global search, if this function return
 164      // true, search function will be called when global searching working
 165      public function global_search() {
 166          return false;
 167      }
 168      public function search($search_text, $page = 0) {
 169          $client = new wikimedia;
 170          $search_result = array();
 171          $search_result['list'] = $client->search_images($search_text);
 172          return $search_result;
 173      }
 174      // when logout button on file picker is clicked, this function will be
 175      // called.
 176      public function logout() {
 177          return $this->print_login();
 178      }
 179      public function supported_returntypes() {
 180          return (FILE_INTERNAL | FILE_EXTERNAL);
 181      }
 182  
 183      /**
 184       * Return the source information
 185       *
 186       * @param stdClass $url
 187       * @return string|null
 188       */
 189      public function get_file_source_info($url) {
 190          return $url;
 191      }
 192  
 193      /**
 194       * Is this repository accessing private data?
 195       *
 196       * @return bool
 197       */
 198      public function contains_private_data() {
 199          return false;
 200      }
 201  }