Cicalese has submitted this change and it was merged.

Change subject: Refactored code and allow variable # of stars
......................................................................


Refactored code and allow variable # of stars

Change-Id: Ie4bae092dc142b6df041ef119574f7632d6f36ef
---
D SemanticRating.class.php
M SemanticRating.i18n.magic.php
M SemanticRating.php
A SemanticRatingFormInput.php
A SemanticRatingHtmlRenderer.php
M i18n/en.json
M i18n/qqq.json
M scripts/SemanticRating.js
8 files changed, 258 insertions(+), 181 deletions(-)

Approvals:
  Cicalese: Verified; Looks good to me, approved
  Siebrand: Looks good to me, but someone else must approve



diff --git a/SemanticRating.class.php b/SemanticRating.class.php
deleted file mode 100644
index 430a4ca..0000000
--- a/SemanticRating.class.php
+++ /dev/null
@@ -1,110 +0,0 @@
-<?php
-
-/*
- * Copyright (c) 2013 The MITRE Corporation
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
-
-class SemanticRating {
-
-       public function renderRating($input, $imagepath) {
-
-               $output = Html::openElement('span');
-
-               if ($input < 0) {
-                       $input = 0;
-               } else if ($input > 5) {
-                       $input = 5;
-               }
-
-               $i = 1;
-               while ($i <= $input) {
-                       $output .=
-                               Html::element('img',
-                                       array('src' => $imagepath . 
'yellowstar.png'));
-                       $i++;
-               }
-               if ($input - $i + 1 != 0) {
-                       $output .=
-                               Html::element('img',
-                                       array('src' => $imagepath . 
'halfstar.png'));
-                       $i++;
-               }
-               while ($i < 6) {
-                       $output .=
-                               Html::element('img',
-                                       array('src' => $imagepath . 
'greystar.png'));
-                       $i++;
-               }
-
-               $output .=
-                       Html::closeElement('span');
-
-               return $output;
-       }
-
-       public function editRating($cur_value, $input_name, $imagepath) {
-       
-               if (!is_numeric($cur_value) || $cur_value < 0 || $cur_value > 
5) {
-                       $cur_value = 0;
-               }
-       
-               $output =
-                       Html::openElement('table', array('style' => 
'display:inline;')) .
-                       Html::openElement('td');
-       
-               global $sfgFieldNum;
-               $input_id = "input_$sfgFieldNum";
-               $output .= Html::element('input', array(
-                       'type' => 'hidden',
-                       'id' => $input_id,
-                       'name' => $input_name,
-                       'value' => $cur_value
-                       ));
-       
-               $i = 1;
-       
-               $src =  $imagepath . 'yellowstar.png';
-               while ($i < $cur_value + 1) {
-                       $output .= Html::element('img', array(
-                               'src' => $src,
-                               'id' => $input_id . '_s_' . $i,
-                               'onclick' => 'setrating(' . $i . ",'" . 
$input_id . "'" . ');'
-                               ));
-                       $i++;
-               }
-                       
-               $src =  $imagepath . 'greystar.png';
-               while ($i < 6) {
-                       $output .= Html::element('img', array(
-                               'src' => $src,
-                               'id' => $input_id . '_s_' . $i,
-                               'onclick' => 'setrating(' . $i . ",'" . 
$input_id . "'" . ');'
-                               ));
-                       $i++;
-               }
-                       
-               $output .=
-                       Html::closeElement('td') .
-                       Html::closeElement('table');
-       
-               return $output;
-       }
-}
diff --git a/SemanticRating.i18n.magic.php b/SemanticRating.i18n.magic.php
index 0403810..bab828e 100644
--- a/SemanticRating.i18n.magic.php
+++ b/SemanticRating.i18n.magic.php
@@ -1,7 +1,7 @@
 <?php
 
 /*
- * Copyright (c) 2013 The MITRE Corporation
+ * Copyright (c) 2014 The MITRE Corporation
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
diff --git a/SemanticRating.php b/SemanticRating.php
index 39cc1a0..21af262 100644
--- a/SemanticRating.php
+++ b/SemanticRating.php
@@ -1,6 +1,6 @@
 <?php
 /*
- * Copyright (c) 2013 The MITRE Corporation
+ * Copyright (c) 2014 The MITRE Corporation
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -25,7 +25,7 @@
        die('<b>Error:</b> This file is part of a MediaWiki extension and 
cannot be run standalone.');
 }
 
-if (version_compare($wgVersion, '1.21', 'lt')) {
+if (version_compare($GLOBALS['wgVersion'], '1.21', 'lt')) {
        die('<b>Error:</b> This version of SemanticRating is only compatible 
with MediaWiki 1.21 or above.');
 }
 
@@ -37,9 +37,9 @@
        die('<b>Error:</b> This version of SemanticRating is only compatible 
with Semantic Forms 2.5.2 or above.');
 }
 
-$wgExtensionCredits['semantic'][] = array (
+$GLOBALS['wgExtensionCredits']['semantic'][] = array (
        'name' => 'SemanticRating',
-       'version' => '1.3',
+       'version' => '2.0',
        'author' => array(
                '[https://www.mediawiki.org/wiki/User:Cindy.cicalese Cindy 
Cicalese]'
        ),
@@ -52,56 +52,39 @@
 // for the original idea that inspired this extension and to Kelly Hatfield
 // for an early implementation of this extension.
 
-$wgAutoloadClasses['SemanticRating'] =
-       __DIR__ . '/SemanticRating.class.php';
+$GLOBALS['wgAutoloadClasses']['SemanticRatingHtmlRenderer'] =
+       __DIR__ . '/SemanticRatingHtmlRenderer.php';
 
-$wgMessagesDirs['SemanticRating'] = __DIR__ . '/i18n';
-$wgExtensionMessagesFiles['SemanticRating'] =
+$GLOBALS['wgAutoloadClasses']['SemanticRatingFormInput'] =
+       __DIR__ . '/SemanticRatingFormInput.php';
+
+$GLOBALS['wgMessagesDirs']['SemanticRating'] = __DIR__ . '/i18n';
+$GLOBALS['wgExtensionMessagesFiles']['SemanticRating'] =
        __DIR__ . '/SemanticRating.i18n.php';
 
-$wgExtensionMessagesFiles['SemanticRatingMagic'] =
+$GLOBALS['wgExtensionMessagesFiles']['SemanticRatingMagic'] =
        __DIR__ . '/SemanticRating.i18n.magic.php';
 
-$wgResourceModules['ext.SemanticRating'] = array(
+$GLOBALS['wgResourceModules']['ext.SemanticRating'] = array(
        'localBasePath' => __DIR__,
        'remoteExtPath' => 'SemanticRating',
        'scripts' => 'scripts/SemanticRating.js'
 );
 
-$wgHooks['ParserFirstCallInit'][] = 'efSemanticRatingParserFunction_Setup';
-
-function efSemanticRatingParserFunction_Setup (&$parser) {
-       global $SemanticRating_Parse;
-       if (!isset($SemanticRating_Parse)) {
-               $SemanticRating_Parse = true;
+$GLOBALS['wgHooks']['ParserFirstCallInit'][] = function (\Parser &$parser) {
+       if (!array_key_exists('SemanticRating_DefaultMax', $GLOBALS)) {
+               $GLOBALS['SemanticRating_DefaultMax'] = 5;
        }
-       $parser->setFunctionHook('rating', 'renderRating');
-       global $sfgFormPrinter;
-       $sfgFormPrinter->setInputTypeHook('rating', 'editRating', array());
+       $parser->setFunctionHook('rating', function($parser) {
+               $imagepath = $GLOBALS['wgServer'] . $GLOBALS['wgScriptPath'] .
+                       "/extensions/SemanticRating/images/";
+               $semanticRatingHtmlRenderer =
+                       new SemanticRatingHtmlRenderer($imagepath);
+               return $semanticRatingHtmlRenderer->render($parser, 
func_get_args());
+       });
+       $imagepath = $GLOBALS['wgServer'] . $GLOBALS['wgScriptPath'] .
+               "/extensions/SemanticRating/images/";
+       SemanticRatingFormInput::setImagePath($imagepath);
+       
$GLOBALS['sfgFormPrinter']->registerInputType('SemanticRatingFormInput');
        return true;
-}
-
-$SemanticRating_ImagePath = $wgServer . $wgScriptPath .
-       "/extensions/SemanticRating/images/";
-
-function renderRating($parser, $input) {
-       global $SemanticRating_ImagePath;
-       $instance = new SemanticRating;
-       $output = $instance->renderRating($input, $SemanticRating_ImagePath);
-       global $SemanticRating_Parse;
-       if ($SemanticRating_Parse) {
-               $output = array($parser->insertStripItem($output, 
$parser->mStripState),
-                       'noparse' => false, 'isHTML' => true);
-       }
-       return $output;
-}
-
-function editRating($cur_value, $input_name, $is_mandatory, $is_disabled,
-       $field_args) {
-       global $wgOut, $SemanticRating_ImagePath;
-       $wgOut->addModules('ext.SemanticRating');
-       $instance = new SemanticRating;
-       $output = $instance->editRating($cur_value, $input_name,
-               $SemanticRating_ImagePath);
-       return array($output, null);
-}
+};
diff --git a/SemanticRatingFormInput.php b/SemanticRatingFormInput.php
new file mode 100644
index 0000000..7035a30
--- /dev/null
+++ b/SemanticRatingFormInput.php
@@ -0,0 +1,112 @@
+<?php
+/*
+ * Copyright (c) 2014 The MITRE Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+class SemanticRatingFormInput extends SFFormInput {
+
+       private static $imagepath = null;
+
+       public static function setImagePath($value) {
+               self::$imagepath = $value;
+       }
+
+       public function __construct($input_number, $cur_value, $input_name,
+               $disabled, $other_args) {
+               parent::__construct($input_number, $cur_value, $input_name, 
$disabled,
+                       $other_args);
+               if (array_key_exists('max', $this->mOtherArgs)) {
+                       $this->mMax = $this->mOtherArgs['max'];
+               } else {
+                       $this->mMax = $GLOBALS['SemanticRating_DefaultMax'];
+               }
+       }
+
+       public static function getName() {
+               return 'rating';
+       }
+
+       public function getHtmlText() {
+
+               if (!is_numeric($this->mCurrentValue) || $this->mCurrentValue < 
0 ||
+                       $this->mCurrentValue > $this->mMax) {
+                       $this->mCurrentValue = 0;
+               }
+
+               $output =
+                       Html::openElement('table', array('style' => 
'display:inline;')) .
+                       Html::openElement('td');
+
+               $input_id = "input_" . $GLOBALS['sfgFieldNum'];
+               $output .= Html::element('input', array(
+                       'type' => 'hidden',
+                       'id' => $input_id,
+                       'name' => $this->mInputName,
+                       'value' => $this->mCurrentValue
+                       ));
+
+               $i = 1;
+
+               $src =  self::$imagepath . 'yellowstar.png';
+               while ($i < $this->mCurrentValue + 1) {
+                       $output .= Html::element('img', array(
+                               'src' => $src,
+                               'id' => $input_id . '_s_' . $i,
+                               'onclick' => 'semanticRating.setrating(' . $i . 
",'" . $input_id . "'," .
+                                       $this->mMax . ');'
+                               ));
+                       $i++;
+               }
+
+               $src =  self::$imagepath . 'greystar.png';
+               while ($i <= $this->mMax) {
+                       $output .= Html::element('img', array(
+                               'src' => $src,
+                               'id' => $input_id . '_s_' . $i,
+                               'onclick' => 'semanticRating.setrating(' . $i . 
",'" . $input_id . "'," .
+                                       $this->mMax . ');'
+                               ));
+                       $i++;
+               }
+
+               $output .=
+                       Html::closeElement('td') .
+                       Html::closeElement('table');
+
+               return $output;
+       }
+
+       public static function getParameters() {
+               $params = parent::getParameters();
+               $params[] = array(
+                       'name' => 'max',
+                       'type' => 'int',
+                       'description' => wfMessage('semanticrating-max')->text()
+               );
+               return $params;
+       }
+
+       public function getResourceModuleNames() {
+               return array(
+                       'ext.SemanticRating'
+               );
+       }
+}
diff --git a/SemanticRatingHtmlRenderer.php b/SemanticRatingHtmlRenderer.php
new file mode 100644
index 0000000..a679104
--- /dev/null
+++ b/SemanticRatingHtmlRenderer.php
@@ -0,0 +1,80 @@
+<?php
+
+/*
+ * Copyright (c) 2014 The MITRE Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+class SemanticRatingHtmlRenderer {
+
+       var $imagepath = null;
+
+       public function __construct($imagepath) {
+               $this->imagepath = $imagepath;
+       }
+
+       public function render($parser, $params) {
+
+               if (count($params) > 1) {
+                       $rating = $params[1];
+               } else {
+                       $rating = 0;
+               }
+               if (count($params) > 2) {
+                       $max = $params[2];
+               } else {
+                       $max = $GLOBALS['SemanticRating_DefaultMax'];
+               }
+
+               $output = Html::openElement('span');
+
+               if ($rating < 0) {
+                       $rating = 0;
+               } else if ($rating > $max) {
+                       $rating = $max;
+               }
+
+               $i = 1;
+               while ($i <= $rating) {
+                       $output .=
+                               Html::element('img',
+                                       array('src' => $this->imagepath . 
'yellowstar.png'));
+                       $i++;
+               }
+               if ($rating - $i + 1 != 0) {
+                       $output .=
+                               Html::element('img',
+                                       array('src' => $this->imagepath . 
'halfstar.png'));
+                       $i++;
+               }
+               while ($i <= $max) {
+                       $output .=
+                               Html::element('img',
+                                       array('src' => $this->imagepath . 
'greystar.png'));
+                       $i++;
+               }
+
+               $output .=
+                       Html::closeElement('span');
+
+               return array($parser->insertStripItem($output, 
$parser->mStripState),
+                       'noparse' => false, 'isHTML' => true);
+       }
+}
diff --git a/i18n/en.json b/i18n/en.json
index 5be26db..2396e75 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -1,6 +1,9 @@
 {
-    "@metadata": {
-        "authors": []
-    },
-    "semanticrating-desc": "Allows users to rate items between 1 and 5 with 
stars"
+       "@metadata": {
+               "authors": [
+                       "Cicalese"
+               ]
+       },
+       "semanticrating-desc": "Allows users to rate items with stars",
+       "semanticrating-max": "The maximum number of stars in a rating"
 }
diff --git a/i18n/qqq.json b/i18n/qqq.json
index aed5f14..606d468 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -1,8 +1,10 @@
 {
        "@metadata": {
                "authors": [
-                       "Kghbln"
+                       "Kghbln",
+                       "Cicalese"
                ]
        },
-       "semanticrating-desc": "{{desc|name=Semantic 
Rating|url=http://www.mediawiki.org/wiki/Extension:Semantic_Rating}}";
+       "semanticrating-desc": "{{desc|name=Semantic 
Rating|url=http://www.mediawiki.org/wiki/Extension:Semantic_Rating}}";,
+       "semanticrating-max": "The description of the form field parameter 
representing the maximum number of stars"
 }
diff --git a/scripts/SemanticRating.js b/scripts/SemanticRating.js
index e052a4b..0d0c204 100644
--- a/scripts/SemanticRating.js
+++ b/scripts/SemanticRating.js
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013 The MITRE Corporation
+ * Copyright (c) 2014 The MITRE Corporation
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
@@ -20,23 +20,30 @@
  * DEALINGS IN THE SOFTWARE.
  */
 
-(function($) {
-       window.setrating = function(rating, input_id) {
-               $("#" + input_id).attr('value', rating);
-               var i = 1;
-               while (i <= rating) {
-                       var star = $("#" + input_id + "_s_" + i);
-                       var src = star.attr('src');
-                       src = src.replace("grey", "yellow");
-                       star.attr('src', src);
-                       i++;
+var semanticRating = (function($) {
+
+       'use strict';
+
+       return {
+               setrating: function(rating, input_id, max) {
+                       $("#" + input_id).attr('value', rating);
+                       var i = 1;
+                       while (i <= rating) {
+                               var star = $("#" + input_id + "_s_" + i);
+                               var src = star.attr('src');
+                               src = src.replace("grey", "yellow");
+                               star.attr('src', src);
+                               i++;
+                       }
+                       while (i <= max) {
+                               var star = $("#" + input_id + "_s_" + i);
+                               var src = star.attr('src');
+                               src = src.replace("yellow", "grey");
+                               star.attr('src', src);
+                               i++;
+                       }
                }
-               while (i < 6) {
-                       var star = $("#" + input_id + "_s_" + i);
-                       var src = star.attr('src');
-                       src = src.replace("yellow", "grey");
-                       star.attr('src', src);
-                       i++;
-               }
-       }
+       };
 }(jQuery));
+
+window.semanticRating = semanticRating;

-- 
To view, visit https://gerrit.wikimedia.org/r/145488
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie4bae092dc142b6df041ef119574f7632d6f36ef
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/SemanticRating
Gerrit-Branch: master
Gerrit-Owner: Cicalese <[email protected]>
Gerrit-Reviewer: Adi.iiita <[email protected]>
Gerrit-Reviewer: Cicalese <[email protected]>
Gerrit-Reviewer: Mwjames <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to