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] [Versions 402 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  /**
  18   * This plugin is used to access merlot files
  19   *
  20   * @since Moodle 2.0
  21   * @package    repository_merlot
  22   * @copyright  2010 Dongsheng Cai {@link http://dongsheng.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  require_once($CFG->dirroot . '/repository/lib.php');
  26  
  27  /**
  28   * repository_merlot is used to search merlot.org in moodle
  29   *
  30   * @since Moodle 2.0
  31   * @package    repository_merlot
  32   * @copyright  2009 Dongsheng Cai {@link http://dongsheng.org}
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class repository_merlot extends repository {
  36  
  37      /** @var string merlot keyword. */
  38      protected $keyword;
  39  
  40      /** @var string merlot author. */
  41      protected $author;
  42  
  43      /** @var string merlot license key. */
  44      protected $licensekey;
  45  
  46      /** @var string API URL. */
  47      protected $api;
  48  
  49      public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
  50          parent::__construct($repositoryid, $context, $options);
  51          $this->keyword = optional_param('merlot_keyword', '', PARAM_RAW);
  52          $this->author = optional_param('merlot_author', '', PARAM_RAW);
  53          $this->licensekey = trim(get_config('merlot', 'licensekey'));
  54      }
  55  
  56      /**
  57       * Display login screen or not
  58       *
  59       * @return boolean
  60       */
  61      public function check_login() {
  62          return !empty($this->keyword);
  63      }
  64  
  65      /**
  66       * Doesn't support global search
  67       *
  68       * @return boolean
  69       */
  70      public function global_search() {
  71          return false;
  72      }
  73  
  74      /**
  75       * Look for a link in merlot.org
  76       * @param string $search_text
  77       * @return array
  78       */
  79      public function search($search_text, $page = 0) {
  80          $ret  = array();
  81          $ret['nologin'] = true;
  82          $ret['list'] = $this->_get_collection($this->keyword, $this->author);
  83          return $ret;
  84      }
  85  
  86      /**
  87       * Get a list of links
  88       * @return array
  89       */
  90      public function get_listing($path = '', $page = '') {
  91          $ret  = array();
  92          $ret['nologin'] = true;
  93          $ret['list'] = $this->_get_collection($this->keyword);
  94          return $ret;
  95      }
  96  
  97      private function _get_collection($keyword) {
  98          global $OUTPUT;
  99          $list = array();
 100          $this->api = 'https://www.merlot.org/merlot/materials.rest?keywords=' . urlencode($keyword) . '&licenseKey='.$this->licensekey;
 101          $c = new curl(array('cache'=>true, 'module_cache'=>'repository'));
 102          $content = $c->get($this->api);
 103          $xml = simplexml_load_string($content);
 104          foreach ($xml->results->material as $entry) {
 105              $list[] = array(
 106                  'title' => (string)$entry->title,
 107                  'thumbnail' => $OUTPUT->image_url(file_extension_icon($entry->title))->out(false),
 108                  'date' => userdate((int)$entry->creationDate),
 109                  'size' => '',
 110                  'source' => (string)$entry->URL,
 111              );
 112          }
 113          return $list;
 114      }
 115  
 116      /**
 117       * Define a search form
 118       *
 119       * @return array
 120       */
 121      public function print_login(){
 122          $ret = array();
 123          $search = new stdClass();
 124          $search->type = 'text';
 125          $search->id   = 'merlog_search';
 126          $search->name = 'merlot_keyword';
 127          $search->label = get_string('search').': ';
 128          $author = new stdClass();
 129          $author->type = 'text';
 130          $author->id   = 'merlog_author';
 131          $author->name = 'merlot_author';
 132          $author->label = get_string('author', 'search').': ';
 133  
 134          $ret['login'] = array($search, $author);
 135          $ret['login_btn_label'] = get_string('search');
 136          $ret['login_btn_action'] = 'search';
 137          return $ret;
 138      }
 139  
 140      /**
 141       * Names of the plugin settings
 142       *
 143       * @return array
 144       */
 145      public static function get_type_option_names() {
 146          return array('licensekey', 'pluginname');
 147      }
 148  
 149      /**
 150       * Add Plugin settings input to Moodle form
 151       *
 152       * @param object $mform
 153       */
 154      public static function type_config_form($mform, $classname = 'repository') {
 155          parent::type_config_form($mform);
 156          $licensekey = get_config('merlot', 'licensekey');
 157          if (empty($licensekey)) {
 158              $licensekey = '';
 159          }
 160          $strrequired = get_string('required');
 161          $mform->addElement('text', 'licensekey', get_string('licensekey', 'repository_merlot'), array('value'=>$licensekey,'size' => '40'));
 162          $mform->setType('licensekey', PARAM_RAW_TRIMMED);
 163          $mform->addRule('licensekey', $strrequired, 'required', null, 'client');
 164      }
 165  
 166      /**
 167       * Support external link only
 168       *
 169       * @return int
 170       */
 171      public function supported_returntypes() {
 172          return FILE_EXTERNAL;
 173      }
 174      public function supported_filetypes() {
 175          return array('link');
 176      }
 177  
 178      /**
 179       * Is this repository accessing private data?
 180       *
 181       * @return bool
 182       */
 183      public function contains_private_data() {
 184          return false;
 185      }
 186  }