MDL-49740 Lock: Fixed task lock release bugs
[moodle.git] / repository / merlot / lib.php
blob01af4404903f7a44901dbaaa674f0e33925780e7
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.
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/>.
17 /**
18 * This plugin is used to access merlot files
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
25 require_once($CFG->dirroot . '/repository/lib.php');
27 /**
28 * repository_merlot is used to search merlot.org in moodle
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
35 class repository_merlot extends repository {
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'));
44 /**
45 * Display login screen or not
47 * @return boolean
49 public function check_login() {
50 return !empty($this->keyword);
53 /**
54 * Doesn't support global search
56 * @return boolean
58 public function global_search() {
59 return false;
62 /**
63 * Look for a link in merlot.org
64 * @param string $search_text
65 * @return array
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;
74 /**
75 * Get a list of links
76 * @return array
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;
85 private function _get_collection($keyword) {
86 global $OUTPUT;
87 $list = array();
88 $this->api = 'http://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->pix_url(file_extension_icon($entry->title, 90))->out(false),
96 'date'=>userdate((int)$entry->creationDate),
97 'size'=>'',
98 'source'=>(string)$entry->URL
101 return $list;
105 * Define a search form
107 * @return array
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').': ';
122 $ret['login'] = array($search, $author);
123 $ret['login_btn_label'] = get_string('search');
124 $ret['login_btn_action'] = 'search';
125 return $ret;
129 * Names of the plugin settings
131 * @return array
133 public static function get_type_option_names() {
134 return array('licensekey', 'pluginname');
138 * Add Plugin settings input to Moodle form
140 * @param object $mform
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 = '';
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');
155 * Support external link only
157 * @return int
159 public function supported_returntypes() {
160 return FILE_EXTERNAL;
162 public function supported_filetypes() {
163 return array('link');
167 * Is this repository accessing private data?
169 * @return bool
171 public function contains_private_data() {
172 return false;