MDL-37541 Wikimedia allows to resize found images
authorMarina Glancy <marina@moodle.com>
Tue, 22 Jan 2013 01:39:58 +0000 (12:39 +1100)
committerMarina Glancy <marina@moodle.com>
Tue, 22 Jan 2013 02:48:59 +0000 (13:48 +1100)
repository/wikimedia/lang/en/repository_wikimedia.php
repository/wikimedia/lib.php
repository/wikimedia/wikimedia.php

index d0c152f..3114683 100644 (file)
@@ -23,7 +23,9 @@
  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  */
 
-$string['keyword'] = 'Full text';
+$string['keyword'] = 'Search for';
 $string['pluginname'] = 'Wikimedia';
 $string['wikimedia:view'] = 'View wikimedia repository';
 $string['configplugin'] = 'Wikimedia repository type configuration';
+$string['maxwidth'] = 'Max image width (px)';
+$string['maxheight'] = 'Max image height (px)';
index cc4bf2b..09a00fb 100644 (file)
@@ -55,6 +55,43 @@ class repository_wikimedia extends repository {
             $SESSION->{$sess_keyword} = $this->keyword;
         }
     }
+
+    /**
+     * Returns maximum width for images
+     *
+     * Takes the maximum width for images eithre from search form or from
+     * user preferences, updates user preferences if needed
+     *
+     * @return int
+     */
+    public function get_maxwidth() {
+        $param = optional_param('wikimedia_maxwidth', 0, PARAM_INT);
+        $pref = get_user_preferences('repository_wikimedia_maxwidth', WIKIMEDIA_IMAGE_SIDE_LENGTH);
+        if ($param > 0 && $param != $pref) {
+            $pref = $param;
+            set_user_preference('repository_wikimedia_maxwidth', $pref);
+        }
+        return $pref;
+    }
+
+    /**
+     * Returns maximum height for images
+     *
+     * Takes the maximum height for images eithre from search form or from
+     * user preferences, updates user preferences if needed
+     *
+     * @return int
+     */
+    public function get_maxheight() {
+        $param = optional_param('wikimedia_maxheight', 0, PARAM_INT);
+        $pref = get_user_preferences('repository_wikimedia_maxheight', WIKIMEDIA_IMAGE_SIDE_LENGTH);
+        if ($param > 0 && $param != $pref) {
+            $pref = $param;
+            set_user_preference('repository_wikimedia_maxheight', $pref);
+        }
+        return $pref;
+    }
+
     public function get_listing($path = '', $page = '') {
         $client = new wikimedia;
         $list = array();
@@ -62,7 +99,9 @@ class repository_wikimedia extends repository {
         if ($list['page'] < 1) {
             $list['page'] = 1;
         }
-        $list['list'] = $client->search_images($this->keyword, $list['page'] - 1);
+        $list['list'] = $client->search_images($this->keyword, $list['page'] - 1,
+                array('iiurlwidth' => $this->get_maxwidth(),
+                    'iiurlheight' => $this->get_maxheight()));
         $list['nologin'] = true;
         $list['norefresh'] = true;
         $list['nosearch'] = true;
@@ -88,13 +127,26 @@ class repository_wikimedia extends repository {
         $keyword->type  = 'text';
         $keyword->name  = 'wikimedia_keyword';
         $keyword->value = '';
+        $maxwidth = array(
+            'label' => get_string('maxwidth', 'repository_wikimedia').': ',
+            'type' => 'text',
+            'name' => 'wikimedia_maxwidth',
+            'value' => get_user_preferences('repository_wikimedia_maxwidth', WIKIMEDIA_IMAGE_SIDE_LENGTH),
+        );
+        $maxheight = array(
+            'label' => get_string('maxheight', 'repository_wikimedia').': ',
+            'type' => 'text',
+            'name' => 'wikimedia_maxheight',
+            'value' => get_user_preferences('repository_wikimedia_maxheight', WIKIMEDIA_IMAGE_SIDE_LENGTH),
+        );
         if ($this->options['ajax']) {
             $form = array();
-            $form['login'] = array($keyword);
+            $form['login'] = array($keyword, (object)$maxwidth, (object)$maxheight);
             $form['nologin'] = true;
             $form['norefresh'] = true;
             $form['nosearch'] = true;
-            $form['allowcaching'] = true; // indicates that login form can be cached in filepicker.js
+            $form['allowcaching'] = false; // indicates that login form can NOT
+            // be cached in filepicker.js (maxwidth and maxheight are dynamic)
             return $form;
         } else {
             echo <<<EOD
index b70bfae..36950ca 100644 (file)
@@ -26,6 +26,7 @@
 define('WIKIMEDIA_THUMBS_PER_PAGE', 24);
 define('WIKIMEDIA_FILE_NS', 6);
 define('WIKIMEDIA_IMAGE_SIDE_LENGTH', 1024);
+define('WIKIMEDIA_THUMB_SIZE', 120);
 
 class wikimedia {
     private $_conn  = null;
@@ -135,13 +136,16 @@ class wikimedia {
             return $thumb_url;
         }
     }
+
     /**
      * Search for images and return photos array.
      *
      * @param string $keyword
+     * @param int $page
+     * @param array $params additional query params
      * @return array
      */
-    public function search_images($keyword, $page = 0) {
+    public function search_images($keyword, $page = 0, $params = array()) {
         global $OUTPUT;
         $files_array = array();
         $this->_param['action'] = 'query';
@@ -152,8 +156,9 @@ class wikimedia {
         $this->_param['gsroffset'] = $page * WIKIMEDIA_THUMBS_PER_PAGE;
         $this->_param['prop']   = 'imageinfo';
         $this->_param['iiprop'] = 'url|dimensions|mime|timestamp|size|user';
-        $this->_param['iiurlwidth'] = WIKIMEDIA_IMAGE_SIDE_LENGTH;
-        $this->_param['iiurlheight'] = WIKIMEDIA_IMAGE_SIDE_LENGTH;
+        $this->_param += $params;
+        $this->_param += array('iiurlwidth' => WIKIMEDIA_IMAGE_SIDE_LENGTH,
+            'iiurlheight' => WIKIMEDIA_IMAGE_SIDE_LENGTH);
         //didn't work with POST
         $content = $this->_conn->get($this->api, $this->_param);
         $result = unserialize($content);
@@ -174,6 +179,12 @@ class wikimedia {
                             'image_width' => $page['imageinfo'][0]['thumbwidth'],
                             'image_height' => $page['imageinfo'][0]['thumbheight']
                         );
+                        if ($attrs['image_width'] <= WIKIMEDIA_THUMB_SIZE && $attrs['image_height'] <= WIKIMEDIA_THUMB_SIZE) {
+                            $attrs['realthumbnail'] = $attrs['source'];
+                        }
+                        if ($attrs['image_width'] <= 24 && $attrs['image_height'] <= 24) {
+                            $attrs['realicon'] = $attrs['source'];
+                        }
                     } else {
                         $attrs = array(
                             //upload full size image
@@ -184,20 +195,19 @@ class wikimedia {
                         );
                     }
                     $attrs += array(
-                        'thumbnail' => $this->get_thumb_url($page['imageinfo'][0]['url'], $page['imageinfo'][0]['width'], $page['imageinfo'][0]['height'], 120),
-                        'icon' => $this->get_thumb_url($page['imageinfo'][0]['url'], $page['imageinfo'][0]['width'], $page['imageinfo'][0]['height'], 24),
+                        'realthumbnail' => $this->get_thumb_url($page['imageinfo'][0]['url'], $page['imageinfo'][0]['width'], $page['imageinfo'][0]['height'], WIKIMEDIA_THUMB_SIZE),
+                        'realicon' => $this->get_thumb_url($page['imageinfo'][0]['url'], $page['imageinfo'][0]['width'], $page['imageinfo'][0]['height'], 24),
                         'author' => $page['imageinfo'][0]['user'],
                         'datemodified' => strtotime($page['imageinfo'][0]['timestamp']),
                         );
                 } else {  // other file types
-                    $attrs = array(
-                        'thumbnail' => $OUTPUT->pix_url(file_extension_icon(substr($title, 5), 120))->out(false),
-                        'source' => $page['imageinfo'][0]['url']);
+                    $attrs = array('source' => $page['imageinfo'][0]['url']);
                 }
                 $files_array[] = array(
                     'title'=>substr($title, 5),         //chop off 'File:'
-                    'thumbnail_width'=>120,
-                    'thumbnail_height'=>120,
+                    'thumbnail' => $OUTPUT->pix_url(file_extension_icon(substr($title, 5), WIKIMEDIA_THUMB_SIZE))->out(false),
+                    'thumbnail_width' => WIKIMEDIA_THUMB_SIZE,
+                    'thumbnail_height' => WIKIMEDIA_THUMB_SIZE,
                     'license' => 'cc-sa',
                     // the accessible url of the file
                     'url'=>$page['imageinfo'][0]['descriptionurl']