PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/repository/wikimedia/lib.php

https://github.com/pauln/moodle
PHP | 198 lines | 120 code | 9 blank | 69 comment | 19 complexity | 60ccda2391aabdefca4b0c9e2494cb9e MD5 | raw file
  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.
  13. //
  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/>.
  16. /**
  17. * This plugin is used to access wikimedia files
  18. *
  19. * @since Moodle 2.0
  20. * @package repository_wikimedia
  21. * @copyright 2010 Dongsheng Cai {@link http://dongsheng.org}
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. require_once($CFG->dirroot . '/repository/lib.php');
  25. require_once(dirname(__FILE__) . '/wikimedia.php');
  26. /**
  27. * repository_wikimedia class
  28. * This is a class used to browse images from wikimedia
  29. *
  30. * @since Moodle 2.0
  31. * @package repository_wikimedia
  32. * @copyright 2009 Dongsheng Cai {@link http://dongsheng.org}
  33. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34. */
  35. class repository_wikimedia extends repository {
  36. /**
  37. * Returns maximum width for images
  38. *
  39. * Takes the maximum width for images eithre from search form or from
  40. * user preferences, updates user preferences if needed
  41. *
  42. * @return int
  43. */
  44. public function get_maxwidth() {
  45. $param = optional_param('wikimedia_maxwidth', 0, PARAM_INT);
  46. $pref = get_user_preferences('repository_wikimedia_maxwidth', WIKIMEDIA_IMAGE_SIDE_LENGTH);
  47. if ($param > 0 && $param != $pref) {
  48. $pref = $param;
  49. set_user_preference('repository_wikimedia_maxwidth', $pref);
  50. }
  51. return $pref;
  52. }
  53. /**
  54. * Returns maximum height for images
  55. *
  56. * Takes the maximum height for images eithre from search form or from
  57. * user preferences, updates user preferences if needed
  58. *
  59. * @return int
  60. */
  61. public function get_maxheight() {
  62. $param = optional_param('wikimedia_maxheight', 0, PARAM_INT);
  63. $pref = get_user_preferences('repository_wikimedia_maxheight', WIKIMEDIA_IMAGE_SIDE_LENGTH);
  64. if ($param > 0 && $param != $pref) {
  65. $pref = $param;
  66. set_user_preference('repository_wikimedia_maxheight', $pref);
  67. }
  68. return $pref;
  69. }
  70. public function get_listing($path = '', $page = '') {
  71. $client = new wikimedia;
  72. $list = array();
  73. $list['page'] = (int)$page;
  74. if ($list['page'] < 1) {
  75. $list['page'] = 1;
  76. }
  77. $list['list'] = $client->search_images($this->keyword, $list['page'] - 1,
  78. array('iiurlwidth' => $this->get_maxwidth(),
  79. 'iiurlheight' => $this->get_maxheight()));
  80. $list['nologin'] = true;
  81. $list['norefresh'] = true;
  82. $list['nosearch'] = true;
  83. if (!empty($list['list'])) {
  84. $list['pages'] = -1; // means we don't know exactly how many pages there are but we can always jump to the next page
  85. } else if ($list['page'] > 1) {
  86. $list['pages'] = $list['page']; // no images available on this page, this is the last page
  87. } else {
  88. $list['pages'] = 0; // no paging
  89. }
  90. return $list;
  91. }
  92. // login
  93. public function check_login() {
  94. global $SESSION;
  95. $this->keyword = optional_param('wikimedia_keyword', '', PARAM_RAW);
  96. if (empty($this->keyword)) {
  97. $this->keyword = optional_param('s', '', PARAM_RAW);
  98. }
  99. $sess_keyword = 'wikimedia_'.$this->id.'_keyword';
  100. if (empty($this->keyword) && optional_param('page', '', PARAM_RAW)) {
  101. // This is the request of another page for the last search, retrieve the cached keyword.
  102. if (isset($SESSION->{$sess_keyword})) {
  103. $this->keyword = $SESSION->{$sess_keyword};
  104. }
  105. } else if (!empty($this->keyword)) {
  106. // Save the search keyword in the session so we can retrieve it later.
  107. $SESSION->{$sess_keyword} = $this->keyword;
  108. }
  109. return !empty($this->keyword);
  110. }
  111. // if check_login returns false,
  112. // this function will be called to print a login form.
  113. public function print_login() {
  114. $keyword = new stdClass();
  115. $keyword->label = get_string('keyword', 'repository_wikimedia').': ';
  116. $keyword->id = 'input_text_keyword';
  117. $keyword->type = 'text';
  118. $keyword->name = 'wikimedia_keyword';
  119. $keyword->value = '';
  120. $maxwidth = array(
  121. 'label' => get_string('maxwidth', 'repository_wikimedia').': ',
  122. 'type' => 'text',
  123. 'name' => 'wikimedia_maxwidth',
  124. 'value' => get_user_preferences('repository_wikimedia_maxwidth', WIKIMEDIA_IMAGE_SIDE_LENGTH),
  125. );
  126. $maxheight = array(
  127. 'label' => get_string('maxheight', 'repository_wikimedia').': ',
  128. 'type' => 'text',
  129. 'name' => 'wikimedia_maxheight',
  130. 'value' => get_user_preferences('repository_wikimedia_maxheight', WIKIMEDIA_IMAGE_SIDE_LENGTH),
  131. );
  132. if ($this->options['ajax']) {
  133. $form = array();
  134. $form['login'] = array($keyword, (object)$maxwidth, (object)$maxheight);
  135. $form['nologin'] = true;
  136. $form['norefresh'] = true;
  137. $form['nosearch'] = true;
  138. $form['allowcaching'] = false; // indicates that login form can NOT
  139. // be cached in filepicker.js (maxwidth and maxheight are dynamic)
  140. return $form;
  141. } else {
  142. echo <<<EOD
  143. <table>
  144. <tr>
  145. <td>{$keyword->label}</td><td><input name="{$keyword->name}" type="text" /></td>
  146. </tr>
  147. </table>
  148. <input type="submit" />
  149. EOD;
  150. }
  151. }
  152. //search
  153. // if this plugin support global search, if this function return
  154. // true, search function will be called when global searching working
  155. public function global_search() {
  156. return false;
  157. }
  158. public function search($search_text, $page = 0) {
  159. $client = new wikimedia;
  160. $search_result = array();
  161. $search_result['list'] = $client->search_images($search_text);
  162. return $search_result;
  163. }
  164. // when logout button on file picker is clicked, this function will be
  165. // called.
  166. public function logout() {
  167. return $this->print_login();
  168. }
  169. public function supported_returntypes() {
  170. return (FILE_INTERNAL | FILE_EXTERNAL);
  171. }
  172. /**
  173. * Return the source information
  174. *
  175. * @param stdClass $url
  176. * @return string|null
  177. */
  178. public function get_file_source_info($url) {
  179. return $url;
  180. }
  181. /**
  182. * Is this repository accessing private data?
  183. *
  184. * @return bool
  185. */
  186. public function contains_private_data() {
  187. return false;
  188. }
  189. }