Merge branch 'm20_MDL-22449_wiki_help_buttons' of git://github.com/dongsheng/moodle
[moodle.git] / repository / merlot / lib.php
CommitLineData
9b1ebc5d
DC
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 * repository_merlot is used to search merlot.org in moodle
20 *
10d53fd3 21 * @since 2.0
d078f6d3
PS
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
9b1ebc5d
DC
27 */
28class repository_merlot extends repository {
29
447c7a19 30 public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
9b1ebc5d
DC
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'));
35 }
36
37 /**
38 * Display login screen or not
39 *
40 * @return boolean
41 */
42 public function check_login() {
43 return !empty($this->keyword);
44 }
45
46 /**
47 * Doesn't support global search
48 *
49 * @return boolean
50 */
51 public function global_search() {
52 return false;
53 }
54
55 /**
56 * Look for a link in merlot.org
57 * @param string $search_text
58 * @return array
59 */
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;
65 }
66
67 /**
68 * Get a list of links
69 * @return array
70 */
71 public function get_listing() {
72 $ret = array();
73 $ret['nologin'] = true;
74 $ret['list'] = $this->_get_collection($this->keyword);
75 return $ret;
76 }
77
9344dea9 78 private function _get_collection($keyword) {
9b1ebc5d
DC
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);
4454447d 84 $xml = simplexml_load_string($content);
9b1ebc5d
DC
85 foreach ($xml->results->material as $entry) {
86 $list[] = array(
87 'title'=>(string)$entry->title,
ede72522 88 'thumbnail'=>$OUTPUT->pix_url('f/unknown-32')->out(false),
9b1ebc5d
DC
89 'date'=>userdate((int)$entry->creationDate),
90 'size'=>'',
91 'source'=>(string)$entry->URL
92 );
93 }
94 return $list;
95 }
96
97 /**
98 * Define a search form
99 *
100 * @return array
101 */
102 public function print_login(){
103 $ret = array();
6bdfef5d 104 $search = new stdClass();
9b1ebc5d
DC
105 $search->type = 'text';
106 $search->id = 'merlog_search';
107 $search->name = 'merlot_keyword';
108 $search->label = get_string('search').': ';
6bdfef5d 109 $author = new stdClass();
9b1ebc5d
DC
110 $author->type = 'text';
111 $author->id = 'merlog_author';
112 $author->name = 'merlot_author';
113 $author->label = get_string('author', 'search').': ';
114
115 $ret['login'] = array($search, $author);
116 $ret['login_btn_label'] = get_string('search');
117 $ret['login_btn_action'] = 'search';
118 return $ret;
119 }
120
121 /**
122 * Names of the plugin settings
123 *
124 * @return array
125 */
126 public static function get_type_option_names() {
a5adfa26 127 return array('licensekey', 'pluginname');
9b1ebc5d
DC
128 }
129
130 /**
131 * Add Plugin settings input to Moodle form
132 *
133 * @param object $mform
134 */
aea5595c 135 public function type_config_form($mform) {
a5adfa26 136 parent::type_config_form($mform);
9b1ebc5d
DC
137 $licensekey = get_config('merlot', 'licensekey');
138 if (empty($licensekey)) {
139 $licensekey = '';
140 }
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');
144 }
145
146 /**
147 * Support external link only
148 *
149 * @return int
150 */
151 public function supported_returntypes() {
152 return FILE_EXTERNAL;
153 }
154 public function supported_filetypes() {
155 return array('link');
156 }
157}
158