Divadsn has uploaded a new change for review. https://gerrit.wikimedia.org/r/325097
Change subject: Improve code readability in Quiz extension by reformatting code ...................................................................... Improve code readability in Quiz extension by reformatting code This extension had a lot of long code lines which has been wrapped. Only code lines where HTML tags were present hasn't been changed. Bug: T150103 Change-Id: Iacebb4d97a00307b64aacdaefc9b86c4f28dd5d5 --- M Question.php M Quiz.class.php M modules/ext.quiz.js 3 files changed, 276 insertions(+), 232 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Quiz refs/changes/97/325097/1 diff --git a/Question.php b/Question.php old mode 100644 new mode 100755 index dc88150..d545e7c --- a/Question.php +++ b/Question.php @@ -19,10 +19,10 @@ $this->mState = ( $beingCorrected ) ? 'NA' : ''; $this->mType = 'multipleChoice'; $this->mCoef = 1; - $this->mProposalPattern = '`^([+-]) ?(.*)`'; - $this->mCorrectionPattern = '`^\|\|(.*)`'; - $this->mCategoryPattern = '`^\|(\n|[^\|].*\n)`'; - $this->mTextFieldPattern = '`\{ ([^\}]*?)(_([\d]*) ?| )\}`'; + $this->mProposalPattern = '`^([+-]) ?(.*)`'; + $this->mCorrectionPattern = '`^\|\|(.*)`'; + $this->mCategoryPattern = '`^\|(\n|[^\|].*\n)`'; + $this->mTextFieldPattern = '`\{ ([^\}]*?)(_([\d]*) ?| )\}`'; } /** @@ -32,19 +32,18 @@ * @param $pState String: */ function setState( $pState ) { - if ( - $pState == 'error' || - ( $pState == 'wrong' && $this->mState != 'error' ) || + if ( $pState == 'error' || ( $pState == 'wrong' && $this->mState != 'error' ) || ( $pState == 'right' && ( $this->mState == 'NA' || $this->mState == 'na_right' ) ) || ( $pState == 'na_wrong' && ( $this->mState == 'NA' || $this->mState == 'na_right' ) ) || ( $pState == 'na_right' && ( $this->mState == 'NA' ) ) || ( $pState == 'new_NA' && ( $this->mState == 'NA' || $this->mState == 'right' ) ) - ) - { + ) { $this->mState = $pState; } - # Special cases - if( ( $pState == 'na_wrong' && $this->mState == 'right' ) || ( $pState == 'right' && $this->mState == 'na_wrong' ) ) { + + // Special cases + if ( ( $pState == 'na_wrong' && $this->mState == 'right' ) || + ( $pState == 'right' && $this->mState == 'na_wrong' ) ) { $this->mState = 'wrong'; } return; @@ -73,15 +72,19 @@ */ function parseHeader( $input ) { $parametersPattern = '`\n\|([^\|].*)\s*$`'; - $input = preg_replace_callback( $parametersPattern, array( $this, 'parseParameters' ), $input ); + $input = preg_replace_callback( + $parametersPattern, + array( $this, 'parseParameters' ), + $input + ); $splitHeaderPattern = '`\n\|\|`'; $unparsedHeader = preg_split( $splitHeaderPattern, $input ); $output = $this->mParser->recursiveTagParse( trim( $unparsedHeader[0] ) . "\n" ); - if( array_key_exists( 1, $unparsedHeader ) ) { + if ( array_key_exists( 1, $unparsedHeader ) ) { $output .= '<table class="correction"><tr>'; - $output .= '<td>→</td><td>' . - $this->mParser->recursiveTagParse( trim( $unparsedHeader[1] ) ) . - '</td>'; + $output .= '<td>→</td><td>'; + $output .= $this->mParser->recursiveTagParse( trim( $unparsedHeader[1] ) ); + $output .= '</td>'; $output .= '</tr></table>'; } return $output; @@ -95,9 +98,9 @@ */ function parseParameters( $matches ) { $typePattern = '`t[yi]p[eo]?="(.*?)"`'; - if( preg_match( $typePattern, $matches[1], $type ) ) { - # List of all object type code and the correspondant question type. - switch( $type[1] ) { + if ( preg_match( $typePattern, $matches[1], $type ) ) { + // List of all object type code and the correspondant question type. + switch ( $type[1] ) { case '{}': $this->mType = 'textField'; break; @@ -110,7 +113,8 @@ } } $coefPattern = '`[ck]oef="(.*?)"`'; - if( preg_match( $coefPattern, $matches[1], $coef ) && is_numeric( $coef[1] ) && $coef[1] > 0 ) { + if ( preg_match( $coefPattern, $matches[1], $coef ) && + is_numeric( $coef[1] ) && $coef[1] > 0 ) { $this->mCoef = $coef[1]; } return; @@ -147,27 +151,28 @@ * @return string A question object in HTML. */ function basicTypeParseObject( $input, $inputType ) { - $output = preg_match( $this->mCategoryPattern, $input, $matches ) ? $this->parseCategories( $matches[1] ) : ''; + $output = preg_match( $this->mCategoryPattern, $input, $matches ) ? + $this->parseCategories( $matches[1] ) : ''; $raws = preg_split( '`\n`s', $input, -1, PREG_SPLIT_NO_EMPTY ); - # Parameters used in some special cases. - $expectOn = 0; - $checkedCount = 0; - foreach( $raws as $proposalId => $raw ) { - $text = null; - $colSpan = ''; - $signesOutput = ''; - if( preg_match( $this->mProposalPattern, $raw, $matches ) ) { + // Parameters used in some special cases. + $expectOn = 0; + $checkedCount = 0; + foreach ( $raws as $proposalId => $raw ) { + $text = null; + $colSpan = ''; + $signesOutput = ''; + if ( preg_match( $this->mProposalPattern, $raw, $matches ) ) { $rawClass = 'proposal'; - # Insulate the proposal signes. + // Insulate the proposal signes. $text = array_pop( $matches ); array_shift( $matches ); - # Determine a type ID, according to the questionType and the number of signes. + // Determine a type ID, according to the questionType and the number of signes. $typeId = substr( $this->mType, 0, 1 ); $typeId .= array_key_exists( 1, $matches ) ? 'c' : 'n'; - foreach( $matches as $signId => $sign ) { + foreach ( $matches as $signId => $sign ) { $title = $disabled = $inputStyle = ''; - # Determine the input's name and value. - switch( $typeId ) { + // Determine the input's name and value. + switch ( $typeId ) { case 'mn': $name = "q$this->mQuestionId" . "p$proposalId"; $value = "p$proposalId"; @@ -185,21 +190,22 @@ $value = "s$signId"; break; } - # Determine if the input had to be checked. - $checked = ( $this->mBeingCorrected && $this->mRequest->getVal( $name ) == $value ) ? 'checked="checked"' : null; - # Determine the color of the cell and modify the state of the question. - switch( $sign ) { + // Determine if the input had to be checked. + $checked = ( $this->mBeingCorrected && + $this->mRequest->getVal( $name ) == $value ) ? 'checked="checked"' : null; + // Determine the color of the cell and modify the state of the question. + switch ( $sign ) { case '+': $expectOn++; - # A single choice object with many correct proposal is a syntax error. - if( $this->mType == 'singleChoice' && $expectOn > 1 ) { + // A single choice object with many correct proposal is a syntax error. + if ( $this->mType == 'singleChoice' && $expectOn > 1 ) { $this->setState( 'error' ); $inputStyle = 'style="outline: ' . Quiz::getColor( 'error' ) . ' solid 3px; *border: 3px solid ' . Quiz::getColor( 'error' ) . ';"'; $title = 'title="' . wfMessage( 'quiz_colorError' )->escaped() . '"'; $disabled = 'disabled="disabled"'; } - if( $this->mBeingCorrected ) { - if( $checked ) { + if ( $this->mBeingCorrected ) { + if ( $checked ) { $checkedCount++; $this->setState( 'right' ); $inputStyle = 'style="outline: ' . Quiz::getColor( 'right' ) . ' solid 3px; *border: 3px solid ' . Quiz::getColor( 'right' ) . ';"'; @@ -212,8 +218,8 @@ } break; case '-': - if( $this->mBeingCorrected ) { - if( $checked ) { + if ( $this->mBeingCorrected ) { + if ( $checked ) { $checkedCount++; $this->setState( 'wrong' ); $inputStyle = 'style="outline: ' . Quiz::getColor( 'wrong' ) . ' solid 3px; *border: 3px solid ' . Quiz::getColor( 'wrong' ) . ';"'; @@ -232,26 +238,26 @@ } $signesOutput .= "<td class=\"sign\"><input class=\"check\" $inputStyle type=\"$inputType\" $title name=\"$name\" value=\"$value\" $checked $disabled/></td>"; } - if( $typeId == 'sc' ) { - # A single choice object with no correct proposal is a syntax error. - if( $expectOn == 0 ) { + if ( $typeId == 'sc' ) { + // A single choice object with no correct proposal is a syntax error. + if ( $expectOn == 0 ) { $this->setState( 'error' ); } $expectOn = 0; } - # If the proposal text is empty, the question has a syntax error. - if( trim( $text ) == '' ) { + // If the proposal text is empty, the question has a syntax error. + if ( trim( $text ) == '' ) { $text = '???'; $this->setState( 'error' ); } - } elseif( preg_match( $this->mCorrectionPattern, $raw, $matches)) { + } elseif ( preg_match( $this->mCorrectionPattern, $raw, $matches)) { $rawClass = 'correction'; $text = array_pop( $matches ); $signesOutput = '<td>→</td>'; - # Hacks to avoid counting the number of signes. + // Hacks to avoid counting the number of signes. $colSpan = ' colspan="13"'; } - if( $text ) { + if ( $text ) { $output .= "<tr class=\"$rawClass\">\n"; $output .= $signesOutput; $output .= "<td$colSpan>"; @@ -260,8 +266,8 @@ $output .= "</tr>\n"; } } - # A single choice object with no correct proposal is a syntax error. - if( isset( $typeId ) && $typeId == 'sn' && $expectOn == 0 ) { + // A single choice object with no correct proposal is a syntax error. + if ( isset( $typeId ) && $typeId == 'sn' && $expectOn == 0 ) { $this->setState( 'error' ); } return $output; @@ -276,21 +282,21 @@ */ function parseCategories( $input ) { $categories = explode( '|', $input ); - # Less than two categories is a syntax error. - if( !array_key_exists( 1, $categories ) ) { + // Less than two categories is a syntax error. + if ( !array_key_exists( 1, $categories ) ) { $categories[1] = '???'; $this->setState( 'error' ); } $output = "<tr class=\"categories\">\n"; $this->mProposalPattern = '`^'; - foreach( $categories as $key => $category ) { - # If a category name is empty, the question has a syntax error. - if( trim( $category ) == '' ) { + foreach ( $categories as $key => $category ) { + // If a category name is empty, the question has a syntax error. + if ( trim( $category ) == '' ) { $category = '???'; $this->setState( 'error' ); } $output .= '<th>' . $this->mParser->recursiveTagParse( $category ) . '</th>'; - if( $key == 0 ) { + if ( $key == 0 ) { $this->mProposalPattern .= '([+-]) ?'; } else { $this->mProposalPattern .= '([+-])? ?'; @@ -314,14 +320,18 @@ global $wqInputId; $wqInputId = $this->mQuestionId * 100; $output = ''; - foreach( $raws as $raw ) { - if( preg_match( $this->mCorrectionPattern, $raw, $matches ) ) { + foreach ( $raws as $raw ) { + if ( preg_match( $this->mCorrectionPattern, $raw, $matches ) ) { $rawClass = 'correction'; $text = '<td>→ ' . $this->mParser->recursiveTagParse( $matches[1] ) . '</td>'; - } elseif( trim( $raw ) != '' ) { + } elseif ( trim( $raw ) != '' ) { $rawClass = 'proposal'; $text = $this->mParser->recursiveTagParse( $raw ); - $text = preg_replace_callback( $this->mTextFieldPattern, array( $this, 'parseTextField' ), $text ); + $text = preg_replace_callback( + $this->mTextFieldPattern, + array( $this, 'parseTextField' ), + $text + ); $text = "<td class=\"input\">$text</td>"; } $output.= "<tr class=\"$rawClass\">\n$text</tr>\n"; @@ -337,49 +347,48 @@ global $wqInputId; $wqInputId++; $title = $state = $size = $maxlength = $class = $style = $value = $disabled = $a_inputBeg = $a_inputEnd = $big = ''; - # determine size and maxlength of the input. - if( array_key_exists( 3, $input ) ) { + // determine size and maxlength of the input. + if ( array_key_exists( 3, $input ) ) { $size = $input[3]; - if( $size < 3 ) { + if ( $size < 3 ) { $size = 'size="1"'; - } elseif( $size < 12 ) { + } elseif ( $size < 12 ) { $size = 'size="' . ( $size - 2 ) . '"'; } else { $size = 'size="' . ( $size - 1 ) . '"'; } $maxlength = 'maxlength="' . $input[3] . '"'; } - # Syntax error if there is no input text. - if( empty( $input[1] ) ) { + // Syntax error if there is no input text. + if ( empty( $input[1] ) ) { $value = 'value="???"'; $state = 'error'; } else { - if( $this->mBeingCorrected ) { + if ( $this->mBeingCorrected ) { $value = trim( $this->mRequest->getVal( $wqInputId ) ); $a_inputBeg = '<a class="input" href="#nogo"><span class="correction">'; $state = 'NA'; $title = 'title="' . wfMessage( 'quiz_colorNA' )->escaped() . '"'; } $class = 'class="numbers"'; - foreach( preg_split( '` *\| *`', trim( $input[1] ), -1, PREG_SPLIT_NO_EMPTY ) as $possibility ) { - if( $state == '' || $state == 'NA' || $state == 'wrong' ) { - if( preg_match( '`^(-?\d+\.?\d*)(-(-?\d+\.?\d*)| (\d+\.?\d*)(%))?$`', str_replace( ',', '.', $possibility ), $matches ) ) { - if( array_key_exists( 5, $matches ) ) { + foreach ( preg_split( '` *\| *`', trim( $input[1] ), -1, PREG_SPLIT_NO_EMPTY ) as $possibility ) { + if ( $state == '' || $state == 'NA' || $state == 'wrong' ) { + if ( preg_match( '`^(-?\d+\.?\d*)(-(-?\d+\.?\d*)| (\d+\.?\d*)(%))?$`', str_replace( ',', '.', $possibility ), $matches ) ) { + if ( array_key_exists( 5, $matches ) ) { $strlen = $size = $maxlength = ''; - } elseif( array_key_exists( 3, $matches ) ) { + } elseif ( array_key_exists( 3, $matches ) ) { $strlen = strlen( $matches[1] ) > strlen( $matches[3] ) ? strlen( $matches[1] ) : strlen( $matches[3] ); } else { $strlen = strlen( $matches[1] ); } - if( $this->mBeingCorrected && !empty( $value ) ) { + if ( $this->mBeingCorrected && !empty( $value ) ) { $value = str_replace( ',', '.', $value ); - if( - is_numeric( $value ) && - ( - ( array_key_exists( 5, $matches ) && $value >= ( $matches[1] - ( $matches[1] * $matches[4] ) / 100 ) && $value <= ( $matches[1] + ( $matches[1] * $matches[4] ) / 100 ) ) || - ( array_key_exists( 3, $matches ) && $value >= $matches[1] && $value <= $matches[3] ) || - $value == $possibility - ) + if ( is_numeric( $value ) && ( + ( array_key_exists( 5, $matches ) + && $value >= ( $matches[1] - ( $matches[1] * $matches[4] ) / 100 ) + && $value <= ( $matches[1] + ( $matches[1] * $matches[4] ) / 100 ) + ) || ( array_key_exists( 3, $matches ) && $value >= $matches[1] && $value <= $matches[3] + ) || $value == $possibility ) ) { $state = 'right'; $title = 'title="' . wfMessage( 'quiz_colorRight' )->escaped() . '"'; @@ -391,43 +400,42 @@ } else { $strlen = preg_match( '` \(i\)$`', $possibility ) ? mb_strlen( $possibility ) - 4 : mb_strlen( $possibility ); $class = 'class="words"'; - if( $this->mBeingCorrected && !empty( $value ) ) { - if( - $value == $possibility || + if ( $this->mBeingCorrected && !empty( $value ) ) { + if ( $value == $possibility || ( preg_match( '`^' . preg_quote( $value, '`' ) . ' \(i\)$`i', $possibility ) ) || ( !$this->mCaseSensitive && preg_match( '`^' . preg_quote( $value, '`' ) . '$`i', $possibility ) ) ) { $state = 'right'; $title = 'title="' . wfMessage( 'quiz_colorRight' )->escaped() . '"'; - } else { + } else { $state = 'wrong'; $title = 'title="' . wfMessage( 'quiz_colorWrong' )->escaped() . '"'; } } } - if( array_key_exists( 3, $input ) && $strlen > $input[3] ) { - # The textfield is too short for the answer + if ( array_key_exists( 3, $input ) && $strlen > $input[3] ) { + // The textfield is too short for the answer $state = 'error'; $value = "<_{$possibility}_ >"; } } - if( $this->mBeingCorrected ) { + if ( $this->mBeingCorrected ) { $a_inputBeg .= "$possibility<br />"; } } $value = empty( $value ) ? '' : 'value="' . str_replace( '"', '"', $value ) . '"'; - if( $this->mBeingCorrected ) { + if ( $this->mBeingCorrected ) { $a_inputBeg.= '</span>'; $a_inputEnd = '</a>'; $big = '<em>▼</em>'; } } - if( $state == 'error' || $this->mBeingCorrected ) { + if ( $state == 'error' || $this->mBeingCorrected ) { global $wgContLang; $border = $wgContLang->isRTL() ? 'border-right' : 'border-left'; $style = "style=\"$border:3px solid " . Quiz::getColor( $state ) . '; "'; $this->setState( empty( $value ) ? 'new_NA' : $state ); - if( $state == 'error' ) { + if ( $state == 'error' ) { $size = ''; $maxlength = ''; $disabled = 'disabled="disabled"'; diff --git a/Quiz.class.php b/Quiz.class.php old mode 100644 new mode 100755 index 84a1650..81934f2 --- a/Quiz.class.php +++ b/Quiz.class.php @@ -3,13 +3,13 @@ * Processes quiz markup */ class Quiz { - # Quiz colors + // Quiz colors static $mColors = array( - 'right' => '#1FF72D', - 'wrong' => '#F74245', - 'correction' => '#F9F9F9', - 'NA' => '#2834FF', - 'error' => '#D700D7' + 'right' => '#1FF72D', + 'wrong' => '#F74245', + 'correction' => '#F9F9F9', + 'NA' => '#2834FF', + 'error' => '#D700D7' ); static $sQuizId = 0; @@ -25,54 +25,65 @@ global $wgRequest; $this->mParser = $parser; $this->mRequest = &$wgRequest; - # Allot a unique identifier to the quiz. + // Allot a unique identifier to the quiz. $this->mQuizId = self::$sQuizId; self::$sQuizId++; - # Reset the unique identifier of the questions. + //Reset the unique identifier of the questions. $this->mQuestionId = 0; - # Reset the counter of div "shuffle" or "noshuffle" inside the quiz. + // Reset the counter of div "shuffle" or "noshuffle" inside the quiz. $this->mShuffleDiv = 0; - # Determine if this quiz is being corrected or not, according to the quizId - $this->mBeingCorrected = ( $wgRequest->getVal( 'quizId' ) == "$this->mQuizId" )? true : false; - # Initialize various parameters used for the score calculation + // Determine if this quiz is being corrected or not, according to the quizId + $this->mBeingCorrected = ( + $wgRequest->getVal( 'quizId' ) == "$this->mQuizId" + ) ? true : false; + // Initialize various parameters used for the score calculation $this->mState = 'NA'; $this->mTotal = $this->mScore = 0; $this->mAddedPoints = 1; $this->mCutoffPoints = 0; $this->mIgnoringCoef = false; - $this->mDisplaySimple = ( array_key_exists( 'display', $argv ) && $argv['display'] == 'simple' ) ? true : false; - if( $this->mBeingCorrected ) { - $lAddedPoints = str_replace( ',', '.', $this->mRequest->getVal( 'addedPoints' ) ); - if( is_numeric( $lAddedPoints ) ) { + $this->mDisplaySimple = ( array_key_exists( 'display', $argv ) && + $argv['display'] == 'simple' ) ? true : false; + + if ( $this->mBeingCorrected ) { + $lAddedPoints = str_replace( ',', '.', + $this->mRequest->getVal( 'addedPoints' ) + ); + if ( is_numeric( $lAddedPoints ) ) { $this->mAddedPoints = $lAddedPoints; } - $lCutoffPoints = str_replace( ',', '.', $this->mRequest->getVal( 'cutoffPoints' ) ); - if( is_numeric( $lCutoffPoints ) ) { + + $lCutoffPoints = str_replace( ',', '.', + $this->mRequest->getVal( 'cutoffPoints' ) + ); + if ( is_numeric( $lCutoffPoints ) ) { $this->mCutoffPoints = $lCutoffPoints; } - if( $this->mRequest->getVal( 'ignoringCoef' ) == 'on' ) { + if ( $this->mRequest->getVal( 'ignoringCoef' ) == 'on' ) { $this->mIgnoringCoef = true; } } - if ( - array_key_exists( 'points', $argv ) && + + if ( array_key_exists( 'points', $argv ) && ( !$this->mBeingCorrected || $this->mDisplaySimple ) && preg_match( '`([\d\.]*)/?([\d\.]*)(!)?`', str_replace( ',', '.', $argv['points'] ), $matches ) - ) - { - if( is_numeric( $matches[1] ) ) { + ) { + if ( is_numeric( $matches[1] ) ) { $this->mAddedPoints = $matches[1]; } - if( is_numeric( $matches[2] ) ) { + if ( is_numeric( $matches[2] ) ) { $this->mCutoffPoints = $matches[2]; } - if( array_key_exists( 3, $matches ) ) { + if ( array_key_exists( 3, $matches ) ) { $this->mIgnoringCoef = true; } } - $this->mShuffle = ( array_key_exists( 'shuffle', $argv ) && $argv['shuffle'] == 'none' ) ? false : true; - $this->mCaseSensitive = ( array_key_exists( 'case', $argv ) && $argv['case'] == '(i)' ) ? false : true; - # Patterns used in several places + $this->mShuffle = ( array_key_exists( 'shuffle', $argv ) && + $argv['shuffle'] == 'none' ) ? false : true; + $this->mCaseSensitive = ( array_key_exists( 'case', $argv ) && + $argv['case'] == '(i)' ) ? false : true; + + // Patterns used in several places $this->mIncludePattern = '`^\{\{:?(.*)\}\}[ \t]*`m'; } @@ -93,10 +104,9 @@ * @throws Exception */ public static function getColor( $colorId ) { - if( array_key_exists( $colorId, self::$mColors ) ) { + if ( array_key_exists( $colorId, self::$mColors ) ) { return self::$mColors[$colorId]; } - throw new Exception( 'Invalid color ID: ' . $colorId ); } @@ -107,78 +117,92 @@ * @return string */ function parseQuiz( $input ) { - # Ouput the style and the script to the header once for all. - if( $this->mQuizId == 0 ) { + // Ouput the style and the script to the header once for all. + if ( $this->mQuizId == 0 ) { global $wgOut; - $wgOut->addModules( 'ext.quiz' ); } - # Process the input + // Process the input $input = $this->parseQuestions( $this->parseIncludes( $input ) ); - # Generates the output. + // Generates the output. + // TODO: Maybe the use of a TemplateParser would be better, possible task? $classHide = ( $this->mBeingCorrected ) ? '' : ' class="hideCorrection"'; - $output = '<div class="quiz">'; - $output .= "<form id=\"quiz$this->mQuizId\" $classHide method=\"post\" action=\"#quiz$this->mQuizId\">\n"; - # Determine the content of the settings table. - $settings = array_fill( 0, 4, '' ); - if( !$this->mDisplaySimple ) { - $settings[0] .= '<td>' . wfMessage( 'quiz_addedPoints', $this->mAddedPoints )->escaped() . '</td>' . - "<td><input class=\"numerical\" type=\"text\" name=\"addedPoints\" value=\"$this->mAddedPoints\"/>  </td>"; - $settings[1] .= '<td>' . wfMessage( 'quiz_cutoffPoints', $this->mCutoffPoints )->escaped() . '</td>' . - "<td><input class=\"numerical\" type=\"text\" name=\"cutoffPoints\" value=\"$this->mCutoffPoints\"/></td>"; - $bChecked = ( $this->mIgnoringCoef ) ? ' checked="checked"' : ''; - $settings[2] .= '<td>' . wfMessage( 'quiz_ignoreCoef' )->escaped() . '</td>' . - "<td><input type=\"checkbox\" name=\"ignoringCoef\"$bChecked/></td>"; - if( $this->mShuffle && !$this->mBeingCorrected ) { - $settings[3] .= '<td><input class="shuffle" name="shuffleButton" type="button" value="' . wfMessage( 'quiz_shuffle' )->escaped() . '" style="display: none;"/></td>' . - '<td></td>'; - } else { - $settings[3] .= '<td></td><td></td>'; - } - } - if( $this->mBeingCorrected ) { - $settings[0] .= '<td class="margin" style="background: ' . self::getColor( 'right' ) . '"></td>' . - '<td style="background: transparent;">' . wfMessage( 'quiz_colorRight' )->escaped() . '</td>'; - $settings[1] .= '<td class="margin" style="background: ' . self::getColor( 'wrong' ) . '"></td>' . - '<td style="background: transparent;">' . wfMessage( 'quiz_colorWrong' )->escaped() . '</td>'; - $settings[2] .= '<td class="margin" style="background: ' . self::getColor( 'NA' ) . '"></td>' . - '<td style="background: transparent;">' . wfMessage( 'quiz_colorNA' )->escaped() . '</td>'; - } - if( $this->mState == 'error' ) { - $errorKey = $this->mBeingCorrected ? 3 : 0; - $settings[$errorKey] .= '<td class="margin" style=\"background: ' . self::getColor( 'error' ) . '"></td>' . - '<td>' . wfMessage( 'quiz_colorError' )->escaped() . '</td>'; - } - # Build the settings table. - $settingsTable = ''; - foreach( $settings as $settingsTr ) { - if( !empty( $settingsTr ) ) { - $settingsTable .= "<tr>\n$settingsTr</tr>\n"; - } - } - if( !empty( $settingsTable ) ) { - $output .= "<table class=\"settings\">\n$settingsTable</table>\n"; - } - $output .= "<input type=\"hidden\" name=\"quizId\" value=\"$this->mQuizId\" />"; + $output = '<div class="quiz">' . "\n"; + $output .= '<form id="quiz' . $this->mQuizId . '" ' . $classHide . ' method="post" action="#quiz' . $this->mQuizId . '">' . "\n"; - $output .= '<div class="quizQuestions">'; - $output .= $input; - $output .= '</div>'; + // Determine the content of the settings table. + $settings = array_fill( 0, 4, '' ); + if ( !$this->mDisplaySimple ) { + $settings[0] .= "<td>" . wfMessage( 'quiz_addedPoints', $this->mAddedPoints )->escaped() . "</td>\n"; + $settings[0] .= '<td><input class="numerical" type="text" name="addedPoints" value="' . $this->mAddedPoints .'"/>  </td>' . "\n"; + + $settings[1] .= "<td>" . wfMessage( 'quiz_cutoffPoints', $this->mCutoffPoints )->escaped() . "</td>'\n"; + $settings[1] .= '<td><input class="numerical" type="text" name="cutoffPoints" value="' . $this->mCutoffPoints . '"/></td>' . "\n"; + + $checked = ( $this->mIgnoringCoef ) ? ' checked="checked"' : ''; + $settings[2] .= "<td>" . wfMessage( 'quiz_ignoreCoef' )->escaped() . "</td>\n"; + $settings[2] .= '<td><input type="checkbox" name="ignoringCoef"' . $checked . '/></td>' . "\n"; + + if ( $this->mShuffle && !$this->mBeingCorrected ) { + $settings[3] .= '<td><input class="shuffle" name="shuffleButton" type="button" value="' . wfMessage( 'quiz_shuffle' )->escaped() . '" style="display: none;"/></td>' . "\n"; + } else { + $settings[3] .= "<td></td>\n"; + } + $settings[3] .= "<td></td>\n"; + } + if ( $this->mBeingCorrected ) { + $settings[0] .= '<td class="margin" style="background: ' . self::getColor( 'right' ) . '"></td>' . "\n"; + $settings[0] .= '<td style="background: transparent;">' . wfMessage( 'quiz_colorRight' )->escaped() . '</td>' . "\n"; + + $settings[1] .= '<td class="margin" style="background: ' . self::getColor( 'wrong' ) . '"></td>' . "\n"; + $settings[1] .= '<td style="background: transparent;">' . wfMessage( 'quiz_colorWrong' )->escaped() . '</td>' . "\n"; + + $settings[2] .= '<td class="margin" style="background: ' . self::getColor( 'NA' ) . '"></td>' . "\n"; + $settings[2] .= '<td style="background: transparent;">' . wfMessage( 'quiz_colorNA' )->escaped() . '</td>' . "\n"; + } + if ( $this->mState === 'error' ) { + $errorKey = $this->mBeingCorrected ? 3 : 0; + $settings[$errorKey] .= '<td class="margin" style=\"background: ' . self::getColor( 'error' ) . '"></td>' . "\n"; + $settings[$errorKey] .= "<td>" . wfMessage( 'quiz_colorError' )->escaped() . "</td>\n"; + } + + // Build the settings table. + $settingsTable = ''; + foreach ( $settings as $settingsTr ) { + if ( !empty( $settingsTr ) ) { + $settingsTable .= "<tr>\n"; + $settingsTable .= $settingsTr . "\n"; + $settingsTable .= "</tr>\n"; + } + } + + if ( !empty( $settingsTable ) ) { + $output .= '<table class="settings">' . "\n"; + $output .= $settingsTable . "\n"; + $output .= "</table>\n"; + } + $output .= '<input type="hidden" name="quizId" value="' . $this->mQuizId . '" />' . "\n"; + + $output .= '<div class="quizQuestions">' . "\n"; + $output .= $input . "\n"; + $output .= "</div>\n"; $output .= '<p><input type="submit" value="' . wfMessage( 'quiz_correction' )->escaped() . '"/>'; - if( $this->mBeingCorrected ) { + if ( $this->mBeingCorrected ) { $output .= '<input class="reset" type="submit" value="' . wfMessage( 'quiz_reset' )->escaped() . '" style="display: none;" />'; } - $output .= '</p>'; + $output .= "</p>\n"; + $output .= '<span class="correction">'; $output .= wfMessage( 'quiz_score' )->rawParams( - "<span class=\"score\">$this->mScore</span>", - "<span class=\"total\">$this->mTotal</span>" + '<span class="score">' . $this->mScore . '</span>', + '<span class="total">' . $this->mTotal . '</span>' )->escaped(); - $output .= '</span>'; + $output .= "</span>\n"; + $output .= "</form>\n"; $output .= "</div>\n"; return $output; @@ -209,12 +233,10 @@ $title = Title::makeTitleSafe( NS_MAIN, $matches[1] ); $text = $this->mParser->fetchTemplate( $title ); $output = ''; - if( preg_match( '`<quiz[^>]*>(.*?)</quiz>`sU', $text, $unparsedQuiz ) ) { - # Remove inclusions from included quiz. + if ( preg_match( '`<quiz[^>]*>(.*?)</quiz>`sU', $text, $unparsedQuiz ) ) { + // Remove inclusions from included quiz. $output = preg_replace( - $this->mIncludePattern, - '', - StringUtils::escapeRegexReplacement( $unparsedQuiz[1] ) + $this->mIncludePattern, '', StringUtils::escapeRegexReplacement( $unparsedQuiz[1] ) ); $output .= "\n"; } @@ -237,21 +259,23 @@ ); $output = ''; $questionPattern = '`(.*?[^|\}])\}[ \t]*(\n(.*)|$)`s'; - foreach( $unparsedQuestions as $unparsedQuestion ) { - # If this "unparsedQuestion" is not a full question, - # we put the text into a buffer to add it at the beginning of the next question. - if( !empty( $buffer ) ) { - $unparsedQuestion = "$buffer\n\n{" . $unparsedQuestion; + foreach ( $unparsedQuestions as $unparsedQuestion ) { + // If this "unparsedQuestion" is not a full question, + // we put the text into a buffer to add it at the beginning of the next question. + if ( !empty( $buffer ) ) { + $unparsedQuestion = $buffer . "\n\n{" . $unparsedQuestion; } - if( preg_match( $questionPattern, $unparsedQuestion, $matches ) ) { + + if ( preg_match( $questionPattern, $unparsedQuestion, $matches ) ) { $buffer = ''; $output .= $this->parseQuestion( $matches ); } else { $buffer = $unparsedQuestion; } } - # Close unclosed "shuffle" or "noshuffle" tags. - while( $this->mShuffleDiv > 0 ) { + + // Close unclosed "shuffle" or "noshuffle" tags. + while ( $this->mShuffleDiv > 0 ) { $output .= '</div>'; $this->mShuffleDiv--; } @@ -275,19 +299,19 @@ ); Hooks::run( 'QuizQuestionCreated', array( $this, &$question ) ); $buffer = $question->parseHeader( $matches[1] ); - if( !array_key_exists( 3, $matches ) || trim( $matches[3] ) == '' ) { - switch( $matches[1] ) { + if ( !array_key_exists( 3, $matches ) || trim( $matches[3] ) == '' ) { + switch ( $matches[1] ) { case 'X': $this->mShuffleDiv++; - return "<div class=\"shuffle\">\n"; + return '<div class="shuffle">' . "\n"; break; case '!X': $this->mShuffleDiv++; - return "<div class=\"noshuffle\">\n"; + return '<div class="noshuffle">' . "\n"; break; case '/X': - # Prevent closing of other tags. - if( $this->mShuffleDiv == 0 ) { + // Prevent closing of other tags. + if ( $this->mShuffleDiv == 0 ) { return ''; } else { $this->mShuffleDiv--; @@ -295,27 +319,32 @@ } break; default: - return "<div class=\"quizText\">$buffer<br /></div>"; + return '<div class="quizText">' . $buffer . "<br /></div>\n"; break; } } - $output = "<div class=\"question\">\n"; - $output .= "<div class=\"header\">\n"; - $output .= '<span class="questionId">' . ++$this->mQuestionId . ". </span>$buffer"; + $output = '<div class="question">' . "\n"; + $output .= '<div class="header">' . "\n"; + $output .= '<span class="questionId">' . ++$this->mQuestionId . ". </span>" . $buffer; $output .= "</div>\n"; - # Store the parsed object into a buffer to determine some parameters before outputing it. - $buffer = call_user_func( array( $question, "{$question->mType}ParseObject" ), $matches[3] ); + + // Store the parsed object into a buffer to determine some parameters before outputing it. + $buffer = call_user_func( array( + $question, + "{$question->mType}ParseObject" + ), $matches[3]); $output .= '<table class="object" '; $lState = $question->getState(); - # Determine the side border color, title, score and the total of the question. - if( $lState != '' ) { + // Determine the side border color, title, score and the total of the question. + if ( $lState != '' ) { global $wgContLang; $border = $wgContLang->isRTL() ? 'border-right' : 'border-left'; $output .= "style=\"$border:3px solid " . self::getColor( $lState ) . '"'; - if( $this->mIgnoringCoef ) { + if ( $this->mIgnoringCoef ) { $question->mCoef = 1; } - switch( $lState ) { + + switch ( $lState ) { case 'right': $this->mTotal += $this->mAddedPoints * $question->mCoef; $this->mScore += $this->mAddedPoints * $question->mCoef; diff --git a/modules/ext.quiz.js b/modules/ext.quiz.js old mode 100644 new mode 100755 index 046aee9..dc05cc9 --- a/modules/ext.quiz.js +++ b/modules/ext.quiz.js @@ -41,25 +41,30 @@ var div = area.childNodes; var quizText; - for( var i = 0, questions = []; i < div.length; ++i ) { - if( div[i].className ) { - if( questions.length === 0 && div[i].className === 'quizText' ) { + for ( var i = 0, questions = []; i < div.length; ++i ) { + if ( div[i].className ) { + if ( questions.length === 0 && div[i].className === 'quizText' ) { quizText = div[i]; } else { questions.push( div[i] ); - if( div[i].className === 'shuffle' || div[i].className === 'noshuffle' ) { + if ( div[i].className === 'shuffle' || + div[i].className === 'noshuffle' + ) { shuffle( div[i] ); } } } } - if( area.className !== 'noshuffle' ) { - for( var l, x, m = questions.length; m; l = parseInt( Math.random() * m ), x = questions[--m], questions[m] = questions[l], questions[l] = x ); + if ( area.className !== 'noshuffle' ) { + for ( + var l, x, m = questions.length; m; l = parseInt( Math.random() * m ), + x = questions[--m], questions[m] = questions[l], questions[l] = x + ); } - if( quizText ) { + if ( quizText ) { questions.unshift( quizText ); } - for( var j = 0, areaHTML = ''; j < questions.length; ++j ) { + for ( var j = 0, areaHTML = ''; j < questions.length; ++j ) { areaHTML += '<div class="' + questions[j].className + '">' + questions[j].innerHTML + '</div>'; } area.innerHTML = areaHTML; @@ -68,26 +73,26 @@ // Prepare the quiz for "javascriptable" browsers function prepareQuiz() { var bodyContentDiv = document.getElementById( 'bodyContent' ).getElementsByTagName( 'div' ); - for( var i = 0; i < bodyContentDiv.length; ++i ) { - if( bodyContentDiv[i].className === 'quiz' ) { + for ( var i = 0; i < bodyContentDiv.length; ++i ) { + if ( bodyContentDiv[i].className === 'quiz' ) { var input = bodyContentDiv[i].getElementsByTagName( 'input' ); - for( var j = 0; j < input.length; ++j ) { + for ( var j = 0; j < input.length; ++j ) { // Add the possibility of unchecking radio buttons - if( input[j].type === 'radio' ) { + if ( input[j].type === 'radio' ) { input[j].ondblclick = function() { this.checked = false; }; } // Displays the shuffle buttons. - else if( input[j].className === 'shuffle' ) { + else if ( input[j].className === 'shuffle' ) { input[j].style.display = 'inline'; /* jshint loopfunc:true */ input[j].onclick = function() { shuffle( this.form.getElementsByTagName( 'div' )[0] ); var sh_input = this.form.getElementsByTagName( 'input' ); - for( var k = 0; k < sh_input.length; ++k ) { + for ( var k = 0; k < sh_input.length; ++k ) { // Add the possibility of unchecking radio buttons - if( input[k].type === 'radio' ) { + if ( input[k].type === 'radio' ) { input[k].ondblclick = function() { this.checked = false; }; @@ -97,7 +102,7 @@ /* jshint loopfunc:false */ } // Display the reset button - else if( input[j].className === 'reset' ) { + else if ( input[j].className === 'reset' ) { input[j].style.display = 'inline'; input[j].onclick = function() { this.form.quizId.value = ''; @@ -105,8 +110,10 @@ }; } // Correct the bug of ie6 on textfields - else if( input[j].className === 'numbers' || input[j].className === 'words' ) { - if( typeof document.body.style.maxHeight === 'undefined' ) { + else if ( input[j].className === 'numbers' || + input[j].className === 'words' + ) { + if ( typeof document.body.style.maxHeight === 'undefined' ) { input[j].parentNode.onclick = function() { this.parentNode.firstChild.style.display = 'inline'; this.parentNode.firstChild.style.position = 'absolute'; @@ -117,20 +124,20 @@ }; } input[j].onkeydown = function() { - if( this.form.shuffleButton ) { + if ( this.form.shuffleButton ) { this.form.shuffleButton.disabled = true; } }; } - if( input[j].className === 'check' ) { + if ( input[j].className === 'check' ) { input[j].onclick = function() { - if( this.form.shuffleButton ) { + if ( this.form.shuffleButton ) { this.form.shuffleButton.disabled = true; } }; } // Disable the submit button if the page is in preview mode - if( input[j].type === 'submit' && document.editform ) { + if ( input[j].type === 'submit' && document.editform ) { input[j].disabled = true; } } -- To view, visit https://gerrit.wikimedia.org/r/325097 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iacebb4d97a00307b64aacdaefc9b86c4f28dd5d5 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Quiz Gerrit-Branch: master Gerrit-Owner: Divadsn <divad.nnamtd...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits