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 .= '<tr class="' . $rawClass . '">' . 
"\n";
-                               $output .= $signesOutput;
-                               $output .= '<td' . $colSpan . '>';
-                               $output .= $this->mParser->recursiveTagParse( 
$text );
-                               $output .= '</td>';
-                               $output .= '</tr>' . "\n";
+                               $lineOutput = '';
+                               $lineOutput = '<tr class="' . $rawClass . '">' 
. "\n";
+                               $lineOutput .= $signesOutput;
+                               $lineOutput .= '<td' . $colSpan . '>';
+                               $lineOutput .= 
$this->mParser->recursiveTagParse( $text );
+                               $lineOutput .= '</td>';
+                               $lineOutput .= '</tr>' . "\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; ) {
+                                       $j = rand( 0, $proposalCount );
+                                       if( strpos( $order, ''.$j ) == false ) {
+                                               $order .= ' '.$j;
+                                               $i++;
+                                       }
+                               }
+                       } else {
+                               for( $i = 0; $i <= $proposalCount; $i++ ) {
+                                       $order .= ' '.$i;
+                               }
+                       }
+               }
+               $tempOrder = explode( ' ', $order );
+               for ( $i = 0; $i <= $proposalCount; ++$i ) {
+                       $output .= $lines[ $tempOrder[ $i+1 ] ];
+               }
+               $orderName = $this->mQuestionId . '|order';
+               $orderValue = $order;
+               $attribs['hidden'] = 'hidden';
+               $attribs['checked'] = 'checked';
+               $orderHtml = Xml::input( $orderName, $orderSize = null, 
$orderValue, $attribs );
+               $output = $orderHtml . $output;
                return $output;
        }
 
diff --git a/Quiz.class.php b/Quiz.class.php
index d139c24..45dcdec 100644
--- a/Quiz.class.php
+++ b/Quiz.class.php
@@ -35,6 +35,8 @@
                $this->mIgnoringCoef = false;
                $this->mDisplaySimple = ( array_key_exists( 'display', $argv ) 
&&
                        $argv['display'] == 'simple' );
+               $this->shuffleAnswers = ( array_key_exists( 'shuffleanswers', 
$argv ) &&
+                       $argv['shuffleanswers'] == 'true' );
 
                if ( $this->mBeingCorrected ) {
                        $lAddedPoints = str_replace( ',', '.',
@@ -271,6 +273,7 @@
                        $this->mBeingCorrected,
                        $this->mCaseSensitive,
                        $this->mQuestionId,
+                       $this->shuffleAnswers,
                        $this->mParser
                );
                Hooks::run( 'QuizQuestionCreated', [ $this, &$question ] );
diff --git a/tests/phpunit/QuestionTest.php b/tests/phpunit/QuestionTest.php
index fc6583d..95e488e 100644
--- a/tests/phpunit/QuestionTest.php
+++ b/tests/phpunit/QuestionTest.php
@@ -35,7 +35,9 @@
        }
 
        private function getQuestion( $beingCorrected, $caseSensitive, 
$questionId ) {
-               return new Question( $beingCorrected, $caseSensitive, 
$questionId, $this->parser );
+               // Randomly generate shuffle parameter value
+               $shuffle = rand( 0, 100 ) % 2 == 0 ? 0 : 1;
+               return new Question( $beingCorrected, $caseSensitive, 
$questionId, $shuffle, $this->parser );
        }
 
        private function getRequest() {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7bdeade1fb5359e0db2051a48a2e51831f673ba7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Quiz
Gerrit-Branch: master
Gerrit-Owner: Harjotsingh <[email protected]>

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

Reply via email to