3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
19 * This plugin is used to access wikimedia files
22 * @package repository_wikimedia
23 * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once($CFG->dirroot
. '/repository/lib.php');
27 require_once(__DIR__
. '/wikimedia.php');
30 * repository_wikimedia class
31 * This is a class used to browse images from wikimedia
34 * @package repository_wikimedia
35 * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class repository_wikimedia
extends repository
{
41 /** @var string keyword search. */
45 * Returns maximum width for images
47 * Takes the maximum width for images eithre from search form or from
48 * user preferences, updates user preferences if needed
52 public function get_maxwidth() {
53 $param = optional_param('wikimedia_maxwidth', 0, PARAM_INT
);
54 $pref = get_user_preferences('repository_wikimedia_maxwidth', WIKIMEDIA_IMAGE_SIDE_LENGTH
);
55 if ($param > 0 && $param != $pref) {
57 set_user_preference('repository_wikimedia_maxwidth', $pref);
63 * Returns maximum height for images
65 * Takes the maximum height for images eithre from search form or from
66 * user preferences, updates user preferences if needed
70 public function get_maxheight() {
71 $param = optional_param('wikimedia_maxheight', 0, PARAM_INT
);
72 $pref = get_user_preferences('repository_wikimedia_maxheight', WIKIMEDIA_IMAGE_SIDE_LENGTH
);
73 if ($param > 0 && $param != $pref) {
75 set_user_preference('repository_wikimedia_maxheight', $pref);
80 public function get_listing($path = '', $page = '') {
81 $client = new wikimedia
;
83 $list['page'] = (int)$page;
84 if ($list['page'] < 1) {
87 $list['list'] = $client->search_images($this->keyword
, $list['page'] - 1,
88 array('iiurlwidth' => $this->get_maxwidth(),
89 'iiurlheight' => $this->get_maxheight()));
90 $list['nologin'] = true;
91 $list['norefresh'] = true;
92 $list['nosearch'] = true;
93 if (!empty($list['list'])) {
94 $list['pages'] = -1; // means we don't know exactly how many pages there are but we can always jump to the next page
95 } else if ($list['page'] > 1) {
96 $list['pages'] = $list['page']; // no images available on this page, this is the last page
98 $list['pages'] = 0; // no paging
103 public function check_login() {
105 $this->keyword
= optional_param('wikimedia_keyword', '', PARAM_RAW
);
106 if (empty($this->keyword
)) {
107 $this->keyword
= optional_param('s', '', PARAM_RAW
);
109 $sess_keyword = 'wikimedia_'.$this->id
.'_keyword';
110 if (empty($this->keyword
) && optional_param('page', '', PARAM_RAW
)) {
111 // This is the request of another page for the last search, retrieve the cached keyword.
112 if (isset($SESSION->{$sess_keyword})) {
113 $this->keyword
= $SESSION->{$sess_keyword};
115 } else if (!empty($this->keyword
)) {
116 // Save the search keyword in the session so we can retrieve it later.
117 $SESSION->{$sess_keyword} = $this->keyword
;
119 return !empty($this->keyword
);
121 // if check_login returns false,
122 // this function will be called to print a login form.
123 public function print_login() {
124 $keyword = new stdClass();
125 $keyword->label
= get_string('keyword', 'repository_wikimedia').': ';
126 $keyword->id
= 'input_text_keyword';
127 $keyword->type
= 'text';
128 $keyword->name
= 'wikimedia_keyword';
129 $keyword->value
= '';
131 'label' => get_string('maxwidth', 'repository_wikimedia').': ',
133 'name' => 'wikimedia_maxwidth',
134 'value' => get_user_preferences('repository_wikimedia_maxwidth', WIKIMEDIA_IMAGE_SIDE_LENGTH
),
137 'label' => get_string('maxheight', 'repository_wikimedia').': ',
139 'name' => 'wikimedia_maxheight',
140 'value' => get_user_preferences('repository_wikimedia_maxheight', WIKIMEDIA_IMAGE_SIDE_LENGTH
),
142 if ($this->options
['ajax']) {
144 $form['login'] = array($keyword, (object)$maxwidth, (object)$maxheight);
145 $form['nologin'] = true;
146 $form['norefresh'] = true;
147 $form['nosearch'] = true;
148 $form['allowcaching'] = false; // indicates that login form can NOT
149 // be cached in filepicker.js (maxwidth and maxheight are dynamic)
155 <td>{$keyword->label}</td><td><input name="{$keyword->name}" type="text" /></td>
158 <input type="submit" />
163 // if this plugin support global search, if this function return
164 // true, search function will be called when global searching working
165 public function global_search() {
168 public function search($search_text, $page = 0) {
169 $client = new wikimedia
;
170 $search_result = array();
171 $search_result['list'] = $client->search_images($search_text);
172 return $search_result;
174 // when logout button on file picker is clicked, this function will be
176 public function logout() {
177 return $this->print_login();
179 public function supported_returntypes() {
180 return (FILE_INTERNAL | FILE_EXTERNAL
);
184 * Return the source information
186 * @param stdClass $url
187 * @return string|null
189 public function get_file_source_info($url) {
194 * Is this repository accessing private data?
198 public function contains_private_data() {