PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/repository/wikimedia/lib.php

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