Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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