Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 402] [Versions 311 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      /**
  42       * Returns maximum width for images
  43       *
  44       * Takes the maximum width for images eithre from search form or from
  45       * user preferences, updates user preferences if needed
  46       *
  47       * @return int
  48       */
  49      public function get_maxwidth() {
  50          $param = optional_param('wikimedia_maxwidth', 0, PARAM_INT);
  51          $pref = get_user_preferences('repository_wikimedia_maxwidth', WIKIMEDIA_IMAGE_SIDE_LENGTH);
  52          if ($param > 0 && $param != $pref) {
  53              $pref = $param;
  54              set_user_preference('repository_wikimedia_maxwidth', $pref);
  55          }
  56          return $pref;
  57      }
  58  
  59      /**
  60       * Returns maximum height for images
  61       *
  62       * Takes the maximum height for images eithre from search form or from
  63       * user preferences, updates user preferences if needed
  64       *
  65       * @return int
  66       */
  67      public function get_maxheight() {
  68          $param = optional_param('wikimedia_maxheight', 0, PARAM_INT);
  69          $pref = get_user_preferences('repository_wikimedia_maxheight', WIKIMEDIA_IMAGE_SIDE_LENGTH);
  70          if ($param > 0 && $param != $pref) {
  71              $pref = $param;
  72              set_user_preference('repository_wikimedia_maxheight', $pref);
  73          }
  74          return $pref;
  75      }
  76  
  77      public function get_listing($path = '', $page = '') {
  78          $client = new wikimedia;
  79          $list = array();
  80          $list['page'] = (int)$page;
  81          if ($list['page'] < 1) {
  82              $list['page'] = 1;
  83          }
  84          $list['list'] = $client->search_images($this->keyword, $list['page'] - 1,
  85                  array('iiurlwidth' => $this->get_maxwidth(),
  86                      'iiurlheight' => $this->get_maxheight()));
  87          $list['nologin'] = true;
  88          $list['norefresh'] = true;
  89          $list['nosearch'] = true;
  90          if (!empty($list['list'])) {
  91              $list['pages'] = -1; // means we don't know exactly how many pages there are but we can always jump to the next page
  92          } else if ($list['page'] > 1) {
  93              $list['pages'] = $list['page']; // no images available on this page, this is the last page
  94          } else {
  95              $list['pages'] = 0; // no paging
  96          }
  97          return $list;
  98      }
  99     // login
 100      public function check_login() {
 101          global $SESSION;
 102          $this->keyword = optional_param('wikimedia_keyword', '', PARAM_RAW);
 103          if (empty($this->keyword)) {
 104              $this->keyword = optional_param('s', '', PARAM_RAW);
 105          }
 106          $sess_keyword = 'wikimedia_'.$this->id.'_keyword';
 107          if (empty($this->keyword) && optional_param('page', '', PARAM_RAW)) {
 108              // This is the request of another page for the last search, retrieve the cached keyword.
 109              if (isset($SESSION->{$sess_keyword})) {
 110                  $this->keyword = $SESSION->{$sess_keyword};
 111              }
 112          } else if (!empty($this->keyword)) {
 113              // Save the search keyword in the session so we can retrieve it later.
 114              $SESSION->{$sess_keyword} = $this->keyword;
 115          }
 116          return !empty($this->keyword);
 117      }
 118      // if check_login returns false,
 119      // this function will be called to print a login form.
 120      public function print_login() {
 121          $keyword = new stdClass();
 122          $keyword->label = get_string('keyword', 'repository_wikimedia').': ';
 123          $keyword->id    = 'input_text_keyword';
 124          $keyword->type  = 'text';
 125          $keyword->name  = 'wikimedia_keyword';
 126          $keyword->value = '';
 127          $maxwidth = array(
 128              'label' => get_string('maxwidth', 'repository_wikimedia').': ',
 129              'type' => 'text',
 130              'name' => 'wikimedia_maxwidth',
 131              'value' => get_user_preferences('repository_wikimedia_maxwidth', WIKIMEDIA_IMAGE_SIDE_LENGTH),
 132          );
 133          $maxheight = array(
 134              'label' => get_string('maxheight', 'repository_wikimedia').': ',
 135              'type' => 'text',
 136              'name' => 'wikimedia_maxheight',
 137              'value' => get_user_preferences('repository_wikimedia_maxheight', WIKIMEDIA_IMAGE_SIDE_LENGTH),
 138          );
 139          if ($this->options['ajax']) {
 140              $form = array();
 141              $form['login'] = array($keyword, (object)$maxwidth, (object)$maxheight);
 142              $form['nologin'] = true;
 143              $form['norefresh'] = true;
 144              $form['nosearch'] = true;
 145              $form['allowcaching'] = false; // indicates that login form can NOT
 146              // be cached in filepicker.js (maxwidth and maxheight are dynamic)
 147              return $form;
 148          } else {
 149              echo <<<EOD
 150  <table>
 151  <tr>
 152  <td>{$keyword->label}</td><td><input name="{$keyword->name}" type="text" /></td>
 153  </tr>
 154  </table>
 155  <input type="submit" />
 156  EOD;
 157          }
 158      }
 159      //search
 160      // if this plugin support global search, if this function return
 161      // true, search function will be called when global searching working
 162      public function global_search() {
 163          return false;
 164      }
 165      public function search($search_text, $page = 0) {
 166          $client = new wikimedia;
 167          $search_result = array();
 168          $search_result['list'] = $client->search_images($search_text);
 169          return $search_result;
 170      }
 171      // when logout button on file picker is clicked, this function will be
 172      // called.
 173      public function logout() {
 174          return $this->print_login();
 175      }
 176      public function supported_returntypes() {
 177          return (FILE_INTERNAL | FILE_EXTERNAL);
 178      }
 179  
 180      /**
 181       * Return the source information
 182       *
 183       * @param stdClass $url
 184       * @return string|null
 185       */
 186      public function get_file_source_info($url) {
 187          return $url;
 188      }
 189  
 190      /**
 191       * Is this repository accessing private data?
 192       *
 193       * @return bool
 194       */
 195      public function contains_private_data() {
 196          return false;
 197      }
 198  }