Merge branch 'install_22_STABLE' of git://github.com/amosbot/moodle into MOODLE_22_STABLE
[moodle.git] / repository / merlot / lib.php
blob981a684b72183eafc61e1b7aaeeb07a9da8b93fd
1 <?php
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.
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/>.
18 /**
19 * repository_merlot is used to search merlot.org in moodle
21 * @since 2.0
22 * @package repository
23 * @subpackage merlot
24 * @copyright 2009 Dongsheng Cai
25 * @author Dongsheng Cai <dongsheng@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 class repository_merlot extends repository {
30 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
31 parent::__construct($repositoryid, $context, $options);
32 $this->keyword = optional_param('merlot_keyword', '', PARAM_RAW);
33 $this->author = optional_param('merlot_author', '', PARAM_RAW);
34 $this->licensekey = trim(get_config('merlot', 'licensekey'));
37 /**
38 * Display login screen or not
40 * @return boolean
42 public function check_login() {
43 return !empty($this->keyword);
46 /**
47 * Doesn't support global search
49 * @return boolean
51 public function global_search() {
52 return false;
55 /**
56 * Look for a link in merlot.org
57 * @param string $search_text
58 * @return array
60 public function search($search_text) {
61 $ret = array();
62 $ret['nologin'] = true;
63 $ret['list'] = $this->_get_collection($this->keyword, $this->author);
64 return $ret;
67 /**
68 * Get a list of links
69 * @return array
71 public function get_listing() {
72 $ret = array();
73 $ret['nologin'] = true;
74 $ret['list'] = $this->_get_collection($this->keyword);
75 return $ret;
78 private function _get_collection($keyword) {
79 global $OUTPUT;
80 $list = array();
81 $this->api = 'http://www.merlot.org/merlot/materials.rest?keywords=' . urlencode($keyword) . '&licenseKey='.$this->licensekey;
82 $c = new curl(array('cache'=>true, 'module_cache'=>'repository'));
83 $content = $c->get($this->api);
84 $xml = simplexml_load_string($content);
85 foreach ($xml->results->material as $entry) {
86 $list[] = array(
87 'title'=>(string)$entry->title,
88 'thumbnail'=>$OUTPUT->pix_url('f/unknown-32')->out(false),
89 'date'=>userdate((int)$entry->creationDate),
90 'size'=>'',
91 'source'=>(string)$entry->URL
94 return $list;
97 /**
98 * Define a search form
100 * @return array
102 public function print_login(){
103 $ret = array();
104 $search = new stdClass();
105 $search->type = 'text';
106 $search->id = 'merlog_search';
107 $search->name = 'merlot_keyword';
108 $search->label = get_string('search').': ';
109 $author = new stdClass();
110 $author->type = 'text';
111 $author->id = 'merlog_author';
112 $author->name = 'merlot_author';
113 $author->label = get_string('author', 'search').': ';
115 $ret['login'] = array($search, $author);
116 $ret['login_btn_label'] = get_string('search');
117 $ret['login_btn_action'] = 'search';
118 return $ret;
122 * Names of the plugin settings
124 * @return array
126 public static function get_type_option_names() {
127 return array('licensekey', 'pluginname');
131 * Add Plugin settings input to Moodle form
133 * @param object $mform
135 public function type_config_form($mform) {
136 parent::type_config_form($mform);
137 $licensekey = get_config('merlot', 'licensekey');
138 if (empty($licensekey)) {
139 $licensekey = '';
141 $strrequired = get_string('required');
142 $mform->addElement('text', 'licensekey', get_string('licensekey', 'repository_merlot'), array('value'=>$licensekey,'size' => '40'));
143 $mform->addRule('licensekey', $strrequired, 'required', null, 'client');
147 * Support external link only
149 * @return int
151 public function supported_returntypes() {
152 return FILE_EXTERNAL;
154 public function supported_filetypes() {
155 return array('link');