[MediaWiki-commits] [Gerrit] mediawiki...Quiz[master]: Add ability for proposal positions to be shuffled on load

2017-08-29 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/374035 )

Change subject: Add ability for proposal positions to be shuffled on load
..


Add ability for proposal positions to be shuffled on load

Currently the quiz extension only shuffles the order of questions using Js.
This change adds shuffling feature for proposal for each questions by using
shuffle function of Php.It is achieved by using shuffleanswer parameter for
quiz.Tests are also added for the same.

Bug: T170799
Change-Id: I7bdeade1fb5359e0db2051a48a2e51831f673ba7
---
M Question.php
M Quiz.class.php
M tests/phpunit/QuestionTest.php
3 files changed, 142 insertions(+), 12 deletions(-)

Approvals:
  jenkins-bot: Verified
  Mvolz: Looks good to me, approved



diff --git a/Question.php b/Question.php
index 93c1e25..d9c1a2e 100644
--- a/Question.php
+++ b/Question.php
@@ -4,17 +4,19 @@
/**
 * Constructor
 *
-* @param bool $beingCorrected
-* @param bool $caseSensitive
-* @param int $questionId the Identifier of the question used to 
generate input names.
-* @param Parser &$parser the wikitext parser.
+* @param Boolean $beingCorrected : Identifier for quiz being corrected.
+* @param Boolean $caseSensitive : Identifier for case sensitive inputs.
+* @param Integer $questionId : the Identifier of the question used to 
generate input names.
+* @param Integer $shufAns : Identifier if answers are supposed to be 
shuffled.
+* @param Parser &$parser : Parser the wikitext parser.
 */
-   public function __construct( $beingCorrected, $caseSensitive, 
$questionId, &$parser ) {
+   public function __construct( $beingCorrected, $caseSensitive, 
$questionId, $shufAns, &$parser ) {
global $wgRequest;
$this->mRequest = &$wgRequest;
$this->mQuestionId = $questionId;
$this->mBeingCorrected = $beingCorrected;
$this->mCaseSensitive = $caseSensitive;
+   $this->shuffleAnswers = $shufAns;
$this->mParser = $parser;
$this->mState = ( $beingCorrected ) ? 'NA' : '';
$this->mType = 'multipleChoice';
@@ -125,6 +127,44 @@
}
 
/**
+* Check order obtained from request
+*
+* @param string $order : The order obtained from request
+* @param Integer $proposalIndex : Contains the index of last Proposal
+*
+* @return int
+*/
+   function checkRequestOrder( $order, $proposalIndex ) {
+   $tempOrder = explode( ' ', $order );
+
+   // Check the number of order matches number of proposals
+   if ( count( $tempOrder ) !== $proposalIndex + 1 ) {
+   return 1;
+   }
+
+   // Check if each value is numeric and is within 0 to 
proposalIndex rannge
+   foreach ( $tempOrder as $orderVal ) {
+   if ( !is_numeric( $orderVal ) ) {
+   return 1;
+   }
+   if ( $orderVal < 0 || $orderVal > $proposalIndex ) {
+   return 1;
+   }
+   }
+
+   // Check for repeated values
+   $orderChecker = array_fill( 0, $proposalIndex + 1, 0 );
+   foreach ( $tempOrder as $orderVal ) {
+   if ( $orderChecker[ $orderVal ] !== 1 ) {
+   $orderChecker[ $orderVal ] = 1;
+   } else {
+   return 1;
+   }
+   }
+   return 0;
+   }
+
+   /**
 * Transmit a single choice object to the basic type parser.
 *
 * @param string $input A question object in quiz syntax.
@@ -162,12 +202,15 @@
// Parameters used in some special cases.
$expectOn = 0;
$attemptChecker = 0;
+   $lines = [];
+   $proposalIndex = -1;
$checkedCount = 0;
foreach ( $raws as $proposalId => $raw ) {
$text = null;
$colSpan = '';
$signesOutput = '';
if ( preg_match( $this->mProposalPattern, $raw, 
$matches ) ) {
+   $proposalIndex++;
$rawClass = 'proposal';
// Insulate the proposal signes.
$text = array_pop( $matches );
@@ -275,18 +318,73 @@
$colSpan = ' colspan="13"';
}
if ( $text ) {
-   $output .= '' . 
"\n";
-   $output .= $signesOutput;
-  

[MediaWiki-commits] [Gerrit] mediawiki...Quiz[master]: Add ability for proposal positions to be shuffled on load

2017-08-26 Thread Harjotsingh (Code Review)
Harjotsingh has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/374035 )

Change subject: Add ability for proposal positions to be shuffled on load
..

Add ability for proposal positions to be shuffled on load

Currently the quiz extension only shuffles the order of questions using Js.
This change adds shuffling feature for proposal for each questions by using
shuffle function of Php.It is achieved by using shuffleanswer parameter for
quiz.

Bug: T170799
Change-Id: I7bdeade1fb5359e0db2051a48a2e51831f673ba7
---
M Question.php
M Quiz.class.php
M tests/phpunit/QuestionTest.php
3 files changed, 60 insertions(+), 8 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Quiz 
refs/changes/35/374035/1

diff --git a/Question.php b/Question.php
index 29fcd2f..a26a3ef 100644
--- a/Question.php
+++ b/Question.php
@@ -9,12 +9,13 @@
 * @param $questionId Integer: the Identifier of the question used to 
generate input names.
 * @param $parser Parser the wikitext parser.
 */
-   public function __construct( $beingCorrected, $caseSensitive, 
$questionId, &$parser ) {
+   public function __construct( $beingCorrected, $caseSensitive, 
$questionId, $shuffle, &$parser ) {
global $wgRequest;
$this->mRequest = &$wgRequest;
$this->mQuestionId = $questionId;
$this->mBeingCorrected = $beingCorrected;
$this->mCaseSensitive = $caseSensitive;
+   $this->shuffleAnswers = $shuffle;
$this->mParser = $parser;
$this->mState = ( $beingCorrected ) ? 'NA' : '';
$this->mType = 'multipleChoice';
@@ -161,12 +162,15 @@
// Parameters used in some special cases.
$expectOn = 0;
$attemptChecker = 0;
+   $lines = [];
+   $proposalCount = -1;
$checkedCount = 0;
foreach ( $raws as $proposalId => $raw ) {
$text = null;
$colSpan = '';
$signesOutput = '';
if ( preg_match( $this->mProposalPattern, $raw, 
$matches ) ) {
+   $proposalCount++;
$rawClass = 'proposal';
// Insulate the proposal signes.
$text = array_pop( $matches );
@@ -274,18 +278,61 @@
$colSpan = ' colspan="13"';
}
if ( $text ) {
-   $output .= '' . 
"\n";
-   $output .= $signesOutput;
-   $output .= '';
-   $output .= $this->mParser->recursiveTagParse( 
$text );
-   $output .= '';
-   $output .= '' . "\n";
+   $lineOutput = '';
+   $lineOutput = '' 
. "\n";
+   $lineOutput .= $signesOutput;
+   $lineOutput .= '';
+   $lineOutput .= 
$this->mParser->recursiveTagParse( $text );
+   $lineOutput .= '';
+   $lineOutput .= '' . "\n";
+   if ( $rawClass === 'correction selected' || 
$rawClass === 'correction unselected' ) {
+   if ( $proposalCount === -1 ) {
+   // Add to output directly
+   $output .= $lineOutput;
+   } else {
+   // Add feedback to previous 
proposal
+   $lines[ $proposalCount ] .= 
$lineOutput;
+   }
+   } else {
+   // Add lineOutput for proposal
+   $lines[ $proposalCount ] = $lineOutput;
+   }
}
}
// A single choice object with no correct proposal is a syntax 
error.
if ( isset( $typeId ) && $typeId == 'sn' && $expectOn == 0 ) {
$this->setState( 'error' );
}
+   //Finding order
+   $order = '';
+   if ( $this->mBeingCorrected ) {
+   $order = $this->mRequest->getVal( $this->mQuestionId . 
'|order' );
+   } else {
+   if ( $this->shuffleAnswers ) {
+   $order = '';
+   for( $i = 0; $i <= $proposalCount; ) {
+   

[MediaWiki-commits] [Gerrit] mediawiki...Quiz[master]: Add ability for proposal positions to be shuffled on load

2017-08-16 Thread Harjotsingh (Code Review)
Harjotsingh has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/372130 )

Change subject: Add ability for proposal positions to be shuffled on load
..

Add ability for proposal positions to be shuffled on load

Currently the quiz extension only shuffles the order of questions using Js.
This change adds shuffling feature for proposal for each questions by using
shuffle function of Php.

Bug: T170799
Change-Id: I471984b7a9eb1faa0f5e4e4dae7c519312e495ea
---
M Question.php
1 file changed, 28 insertions(+), 6 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Quiz 
refs/changes/30/372130/1

diff --git a/Question.php b/Question.php
index f2d544c..85e13a9 100644
--- a/Question.php
+++ b/Question.php
@@ -161,11 +161,14 @@
// Parameters used in some special cases.
$expectOn = 0;
$checkedCount = 0;
+   $lines = [];
+   $proposalCount = -1;
foreach ( $raws as $proposalId => $raw ) {
$text = null;
$colSpan = '';
$signesOutput = '';
if ( preg_match( $this->mProposalPattern, $raw, 
$matches ) ) {
+   $proposalCount++;
$rawClass = 'proposal';
// Insulate the proposal signes.
$text = array_pop( $matches );
@@ -269,14 +272,33 @@
$colSpan = ' colspan="13"';
}
if ( $text ) {
-   $output .= '' . 
"\n";
-   $output .= $signesOutput;
-   $output .= '';
-   $output .= $this->mParser->recursiveTagParse( 
$text );
-   $output .= '';
-   $output .= '' . "\n";
+   $lineOutput = '';
+   $lineOutput = '' 
. "\n";
+   $lineOutput .= $signesOutput;
+   $lineOutput .= '';
+   $lineOutput .= 
$this->mParser->recursiveTagParse( $text );
+   $lineOutput .= '';
+   $lineOutput .= '' . "\n";
+   if ( $rawClass === 'correction' ) {
+   if ( $proposalCount === -1 ) {
+   // Add to output directly
+   $output .= $lineOutput;
+   } else {
+   // Add feedback to previous 
proposal
+   $lines[ $proposalCount ] .= 
$lineOutput;
+
+   }
+   } else {
+   // Add lineOutput for proposal
+   $lines[ $proposalCount ] = $lineOutput;
+   }
}
}
+   // Shuffling the proposals
+   shuffle( $lines );
+   for( $i = 0; $i <= $proposalCount; ++$i ) {
+$output .= $lines[ $i ];
+}
// A single choice object with no correct proposal is a syntax 
error.
if ( isset( $typeId ) && $typeId == 'sn' && $expectOn == 0 ) {
$this->setState( 'error' );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I471984b7a9eb1faa0f5e4e4dae7c519312e495ea
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Quiz
Gerrit-Branch: master
Gerrit-Owner: Harjotsingh 

___
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits