Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

MDL-32036: Repositories wikimedia and youtube lack paging

repositories wikimedia and youtube:
- save last search in
- process parameter 'page'
- remove buttons 'Refresh', 'Search' and 'Logout' since they don't have meaning
filepicker:
- allow unknown number of pages (['pages'] = -1), show one 'Next' page after the current (both JS and non-JS modes)
repository wikimedia:
- add non-AJAX search form to make it work with JS disabled
  • Loading branch information...
commit 9616f3f71da9a885564b2bc6904f9afa0e1ad47b 1 parent 476626d
@marinaglancy marinaglancy authored
View
2  lib/outputrequirementslib.php
@@ -408,7 +408,7 @@ protected function find_module($component) {
array('saving', 'repository'), array('search', 'repository'), array('searching', 'repository'), array('size', 'repository'),
array('submit', 'repository'), array('sync', 'repository'), array('title', 'repository'), array('upload', 'repository'),
array('uploading', 'repository'), array('xhtmlerror', 'repository'),
- array('cancel'), array('chooselicense', 'repository'), array('author', 'repository'),
+ array('cancel'), array('chooselicense', 'repository'), array('author', 'repository'),array('next', 'moodle'),
array('ok', 'moodle'), array('error', 'moodle'), array('info', 'moodle'), array('norepositoriesavailable', 'repository'), array('norepositoriesexternalavailable', 'repository'),
array('nofilesattached', 'repository'), array('filepicker', 'repository'),
array('nofilesavailable', 'repository'), array('overwrite', 'repository'),
View
18 repository/filepicker.js
@@ -1370,15 +1370,21 @@ M.core_filepicker.init = function(Y, options) {
var r = this.active_repo;
var str = '';
var action = '';
- if(r.pages > 1) {
+ var lastpage = r.pages;
+ var lastpagetext = r.pages;
+ if (r.pages == -1) {
+ lastpage = r.page + 1;
+ lastpagetext = M.str.moodle.next;
+ }
+ if (lastpage > 1) {
str += '<div class="fp-paging" id="paging-'+html_id+'-'+client_id+'">';
str += this.get_page_button(1)+'1</a> ';
var span = 5;
var ex = (span-1)/2;
- if (r.page+ex>=r.pages) {
- var max = r.pages;
+ if (r.page+ex>=lastpage) {
+ var max = lastpage;
} else {
if (r.page<span) {
var max = span;
@@ -1405,11 +1411,11 @@ M.core_filepicker.init = function(Y, options) {
}
// won't display upper boundary
- if (max==r.pages) {
- str += this.get_page_button(r.pages)+r.pages+'</a>';
+ if (max==lastpage) {
+ str += this.get_page_button(lastpage)+lastpagetext+'</a>';
} else {
str += this.get_page_button(max)+max+'</a>';
- str += ' ... '+this.get_page_button(r.pages)+r.pages+'</a>';
+ str += ' ... '+this.get_page_button(lastpage)+lastpagetext+'</a>';
}
str += '</div>';
}
View
20 repository/filepicker.php
@@ -206,9 +206,23 @@
}
}
if (!empty($list['page'])) {
- // TODO: need a better solution
- $pagingurl = new moodle_url("$CFG->httpswwwroot/repository/filepicker.php?action=list&itemid=$itemid&ctx_id=$contextid&repo_id=$repo_id&course=$courseid");
- echo $OUTPUT->paging_bar($list['total'], $list['page'] - 1, $list['perpage'], $pagingurl);
+ // TODO MDL-28482: need a better solution
+ // paging_bar is not a good option because it starts page numbering from 0 and
+ // repositories number pages starting from 1.
+ $pagingurl = new moodle_url("$CFG->httpswwwroot/repository/filepicker.php?action=list&itemid=$itemid&ctx_id=$contextid&repo_id=$repo_id&course=$courseid&sesskey=". sesskey());
+ if (!isset($list['perpage']) && !isset($list['total'])) {
+ $list['perpage'] = 10; // instead of setting perpage&total we use number of pages, the result is the same
+ }
+ if (empty($list['total'])) {
+ if ($list['pages'] == -1) {
+ $total = ($list['page'] + 2) * $list['perpage'];
+ } else {
+ $total = $list['pages'] * $list['perpage'];
+ }
+ } else {
+ $total = $list['total'];
+ }
+ echo $OUTPUT->paging_bar($total, $list['page'], $list['perpage'], $pagingurl);
}
echo '<table>';
foreach ($list['list'] as $item) {
View
48 repository/wikimedia/lib.php
@@ -31,17 +31,41 @@
class repository_wikimedia extends repository {
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
+ global $SESSION;
parent::__construct($repositoryid, $context, $options);
$this->keyword = optional_param('wikimedia_keyword', '', PARAM_RAW);
if (empty($this->keyword)) {
$this->keyword = optional_param('s', '', PARAM_RAW);
}
+ $sess_keyword = 'wikimedia_'.$this->id.'_keyword';
+ if (empty($this->keyword) && optional_param('page', '', PARAM_RAW)) {
+ // This is the request of another page for the last search, retrieve the cached keyword
+ if (isset($SESSION->{$sess_keyword})) {
+ $this->keyword = $SESSION->{$sess_keyword};
+ }
+ } else if (!empty($this->keyword)) {
+ // save the search keyword in the session so we can retrieve it later
+ $SESSION->{$sess_keyword} = $this->keyword;
+ }
}
public function get_listing($path = '', $page = '') {
$client = new wikimedia;
$list = array();
- $list['list'] = $client->search_images($this->keyword);
+ $list['page'] = (int)$page;
+ if ($list['page'] < 1) {
+ $list['page'] = 1;
+ }
+ $list['list'] = $client->search_images($this->keyword, $list['page'] - 1);
$list['nologin'] = true;
+ $list['norefresh'] = true;
+ $list['nosearch'] = true;
+ if (!empty($list['list'])) {
+ $list['pages'] = -1; // means we don't know exactly how many pages there are but we can always jump to the next page
+ } else if ($list['page'] > 1) {
+ $list['pages'] = $list['page']; // no images available on this page, this is the last page
+ } else {
+ $list['pages'] = 0; // no paging
+ }
return $list;
}
// login
@@ -57,10 +81,24 @@ public function print_login() {
$keyword->type = 'text';
$keyword->name = 'wikimedia_keyword';
$keyword->value = '';
-
- $form = array();
- $form['login'] = array($keyword);
- return $form;
+ if ($this->options['ajax']) {
+ $form = array();
+ $form['login'] = array($keyword);
+ $form['nologin'] = true;
+ $form['norefresh'] = true;
+ $form['nosearch'] = true;
+ $form['allowcaching'] = true; // indicates that login form can be cached in filepicker.js
+ return $form;
+ } else {
+ echo <<<EOD
+<table>
+<tr>
+<td>{$keyword->label}</td><td><input name="{$keyword->name}" type="text" /></td>
+</tr>
+</table>
+<input type="submit" />
+EOD;
+ }
}
//search
// if this plugin support global search, if this function return
View
3  repository/wikimedia/wikimedia.php
@@ -141,13 +141,14 @@ public function get_thumb_url($image_url, $orig_width, $orig_height, $thumb_widt
* @param string $keyword
* @return array
*/
- public function search_images($keyword) {
+ public function search_images($keyword, $page = 0) {
$files_array = array();
$this->_param['action'] = 'query';
$this->_param['generator'] = 'search';
$this->_param['gsrsearch'] = $keyword;
$this->_param['gsrnamespace'] = WIKIMEDIA_FILE_NS;
$this->_param['gsrlimit'] = WIKIMEDIA_THUMBS_PER_PAGE;
+ $this->_param['gsroffset'] = $page * WIKIMEDIA_THUMBS_PER_PAGE;
$this->_param['prop'] = 'imageinfo';
$this->_param['iiprop'] = 'url|dimensions|mime';
$this->_param['iiurlwidth'] = WIKIMEDIA_IMAGE_SIDE_LENGTH;
View
39 repository/youtube/lib.php
@@ -27,6 +27,8 @@
*/
class repository_youtube extends repository {
+ /** @var int maximum number of thumbs per page */
+ const YOUTUBE_THUMBS_PER_PAGE = 27;
/**
* Youtube plugin constructor
@@ -35,9 +37,6 @@ class repository_youtube extends repository {
* @param array $options
*/
public function __construct($repositoryid, $context = SYSCONTEXTID, $options = array()) {
- $this->start =1;
- $this->max = 27;
- $this->sort = optional_param('youtube_sort', 'relevance', PARAM_TEXT);
parent::__construct($repositoryid, $context, $options);
}
@@ -50,11 +49,40 @@ public function check_login() {
* @param string $search_text
* @return array
*/
- public function search($search_text) {
+ public function search($search_text, $page) {
+ global $SESSION;
+ $sort = optional_param('youtube_sort', '', PARAM_TEXT);
+ $sess_keyword = 'youtube_'.$this->id.'_keyword';
+ $sess_sort = 'youtube_'.$this->id.'_sort';
+
+ // This is the request of another page for the last search, retrieve the cached keyword and sort
+ if ($page && !$search_text && isset($SESSION->{$sess_keyword})) {
+ $search_text = $SESSION->{$sess_keyword};
+ }
+ if ($page && !$sort && isset($SESSION->{$sess_sort})) {
+ $sort = $SESSION->{$sess_sort};
+ }
+ if (!$sort) {
+ $sort = 'relevance'; // default
+ }
+
+ // Save this search in session
+ $SESSION->{$sess_keyword} = $search_text;
+ $SESSION->{$sess_sort} = $sort;
+
$this->keyword = $search_text;
$ret = array();
$ret['nologin'] = true;
- $ret['list'] = $this->_get_collection($search_text, $this->start, $this->max, $this->sort);
+ $ret['page'] = (int)$page;
+ if ($ret['page'] < 1) {
+ $ret['page'] = 1;
+ }
+ $start = ($ret['page'] - 1) * self::YOUTUBE_THUMBS_PER_PAGE + 1;
+ $max = self::YOUTUBE_THUMBS_PER_PAGE;
+ $ret['list'] = $this->_get_collection($search_text, $start, $max, $sort);
+ $ret['norefresh'] = true;
+ $ret['nosearch'] = true;
+ $ret['pages'] = -1;
return $ret;
}
@@ -142,6 +170,7 @@ public function print_login($ajax = true) {
$ret['login'] = array($search, $sort);
$ret['login_btn_label'] = get_string('search');
$ret['login_btn_action'] = 'search';
+ $ret['allowcaching'] = true; // indicates that login form can be cached in filepicker.js
return $ret;
}
Please sign in to comment.
Something went wrong with that request. Please try again.