';
str += this.get_page_button(1)+'1 ';
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';
+ if (max==lastpage) {
+ str += this.get_page_button(lastpage)+lastpagetext+'';
} else {
str += this.get_page_button(max)+max+'';
- str += ' ... '+this.get_page_button(r.pages)+r.pages+'';
+ str += ' ... '+this.get_page_button(lastpage)+lastpagetext+'';
}
str += '
';
}
diff --git a/repository/filepicker.php b/repository/filepicker.php
index b7b1a65f3f3..68aee10060b 100644
--- a/repository/filepicker.php
+++ b/repository/filepicker.php
@@ -206,9 +206,23 @@ case 'sign':
}
}
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 '
';
foreach ($list['list'] as $item) {
diff --git a/repository/wikimedia/lib.php b/repository/wikimedia/lib.php
index adc3d25d947..8a827afa95b 100644
--- a/repository/wikimedia/lib.php
+++ b/repository/wikimedia/lib.php
@@ -31,17 +31,41 @@ require_once('wikimedia.php');
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 @@ class repository_wikimedia extends repository {
$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 <<
+
+
{$keyword->label}
+
+
+
+EOD;
+ }
}
//search
// if this plugin support global search, if this function return
diff --git a/repository/wikimedia/wikimedia.php b/repository/wikimedia/wikimedia.php
index 549b394dbb9..ce186b8f21e 100644
--- a/repository/wikimedia/wikimedia.php
+++ b/repository/wikimedia/wikimedia.php
@@ -141,13 +141,14 @@ class wikimedia {
* @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;
diff --git a/repository/youtube/lib.php b/repository/youtube/lib.php
index cb0020443cf..19f60b27cbf 100644
--- a/repository/youtube/lib.php
+++ b/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 @@ class repository_youtube extends repository {
* @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 @@ class repository_youtube extends repository {
$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;
}