weekly release 5.0dev
[moodle.git] / repository / wikimedia / lib.php
blobc854b0e33c7935d65251d29375c1a4ed48f22130
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 * This plugin is used to access wikimedia files
21 * @since Moodle 2.0
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');
29 /**
30 * repository_wikimedia class
31 * This is a class used to browse images from wikimedia
33 * @since Moodle 2.0
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. */
42 protected $keyword;
44 /**
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
50 * @return int
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) {
56 $pref = $param;
57 set_user_preference('repository_wikimedia_maxwidth', $pref);
59 return $pref;
62 /**
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
68 * @return int
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) {
74 $pref = $param;
75 set_user_preference('repository_wikimedia_maxheight', $pref);
77 return $pref;
80 public function get_listing($path = '', $page = '') {
81 $client = new wikimedia;
82 $list = array();
83 $list['page'] = (int)$page;
84 if ($list['page'] < 1) {
85 $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
97 } else {
98 $list['pages'] = 0; // no paging
100 return $list;
102 // login
103 public function check_login() {
104 global $SESSION;
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 = '';
130 $maxwidth = array(
131 'label' => get_string('maxwidth', 'repository_wikimedia').': ',
132 'type' => 'text',
133 'name' => 'wikimedia_maxwidth',
134 'value' => get_user_preferences('repository_wikimedia_maxwidth', WIKIMEDIA_IMAGE_SIDE_LENGTH),
136 $maxheight = array(
137 'label' => get_string('maxheight', 'repository_wikimedia').': ',
138 'type' => 'text',
139 'name' => 'wikimedia_maxheight',
140 'value' => get_user_preferences('repository_wikimedia_maxheight', WIKIMEDIA_IMAGE_SIDE_LENGTH),
142 if ($this->options['ajax']) {
143 $form = array();
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)
150 return $form;
151 } else {
152 echo <<<EOD
153 <table>
154 <tr>
155 <td>{$keyword->label}</td><td><input name="{$keyword->name}" type="text" /></td>
156 </tr>
157 </table>
158 <input type="submit" />
159 EOD;
162 //search
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() {
166 return false;
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
175 // called.
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) {
190 return $url;
194 * Is this repository accessing private data?
196 * @return bool
198 public function contains_private_data() {
199 return false;