From 5f1a95f9adf9f4f46ea7c7cd9b7e52e14f01add1 Mon Sep 17 00:00:00 2001 From: Jean-Michel Vedrine Date: Sat, 27 Sep 2014 22:09:10 +0200 Subject: CONTRIB-5302 CONTRIB-5303 and codechecker fixes --- displayformula.php | 8 ++--- edit_algebra_form.php | 86 +++++++++++++++++++++++------------------------ lang/en/qtype_algebra.php | 3 +- question.php | 56 +++++++++++++++--------------- questiontype.php | 68 ++++++++++++++++++------------------- renderer.php | 23 +++++++------ version.php | 4 +-- 7 files changed, 125 insertions(+), 123 deletions(-) diff --git a/displayformula.php b/displayformula.php index dd49486..788304b 100644 --- a/displayformula.php +++ b/displayformula.php @@ -32,15 +32,15 @@ global $PAGE, $CFG; $p = new qtype_algebra_parser; try { - $query=urldecode($_SERVER['QUERY_STRING']); - $m=array(); + $query = urldecode($_SERVER['QUERY_STRING']); + $m = array(); if (!preg_match('/vars=([^&]*)&expr=(.*)$/A', $query, $m)) { throw new Exception('Invalid query string received from http server!'); } - $vars=explode(',', $m[1]); + $vars = explode(',', $m[1]); if (empty($m[2])) { - $texexp=''; + $texexp = ''; } else { $exp = $p->parse($m[2], $vars); if ($CFG->qtype_algebra_texdelimiters == 'old') { diff --git a/edit_algebra_form.php b/edit_algebra_form.php index 9b0be1a..11b0f69 100644 --- a/edit_algebra_form.php +++ b/edit_algebra_form.php @@ -59,12 +59,12 @@ class qtype_algebra_edit_form extends question_edit_form { // First create an array with all the allowed values. We will then use this array // with the array_combine function to create a single array where the keys are the // same as the array values. - $chk_array=array( '1', '2', '3', '5', '7', + $chkarray = array( '1', '2', '3', '5', '7', '10', '20', '30', '50', '70', '100', '200', '300', '500', '700', '1000'); // Add the select element using the array_combine method discussed above. $mform->addElement('select', 'nchecks', get_string('nchecks', 'qtype_algebra'), - array_combine($chk_array, $chk_array)); + array_combine($chkarray, $chkarray)); $mform->addHelpButton('nchecks', 'nchecks', 'qtype_algebra'); // Set the default number of checks to perform. $mform->setDefault('nchecks', '10'); @@ -76,33 +76,33 @@ class qtype_algebra_edit_form extends question_edit_form { $mform->setDefault('tolerance', '0.001'); // Add an entry for the answer box prefix. - $mform->addElement('text', 'answerprefix', get_string('answerprefix', 'qtype_algebra'), array('size'=>55)); + $mform->addElement('text', 'answerprefix', get_string('answerprefix', 'qtype_algebra'), array('size' => 55)); $mform->addHelpButton('answerprefix', 'answerprefix', 'qtype_algebra'); $mform->setType('answerprefix', PARAM_RAW); // Add an entry for a disallowed expression. - $mform->addElement('text', 'disallow', get_string('disallow', 'qtype_algebra'), array('size'=>55)); + $mform->addElement('text', 'disallow', get_string('disallow', 'qtype_algebra'), array('size' => 55)); $mform->addHelpButton('disallow', 'disallow', 'qtype_algebra'); $mform->setType('disallow', PARAM_RAW); // Create an array which will store the function checkboxes. - $func_group=array(); + $funcgroup = array(); // Create an array to add spacers between the boxes. - $spacers=array('
'); + $spacers = array('
'); // Add the initial all functions box to the list of check boxes. - $func_group[] =& $mform->createElement('checkbox', 'all', '', get_string('allfunctions', 'qtype_algebra')); + $funcgroup[] =& $mform->createElement('checkbox', 'all', '', get_string('allfunctions', 'qtype_algebra')); // Create a checkbox element for each function understood by the parser. - for ($i=0; $icreateElement('checkbox', $func, '', $func); + for ($i = 0; $i < count(qtype_algebra_parser::$functions); $i++) { + $func = qtype_algebra_parser::$functions[$i]; + $funcgroup[] =& $mform->createElement('checkbox', $func, '', $func); if (($i % 6) == 5) { - $spacers[]='
'; + $spacers[] = '
'; } else { - $spacers[]=str_repeat(' ', 8-strlen($func)); + $spacers[] = str_repeat(' ', 8 - strlen($func)); } } // Create and add the group of function controls to the form. - $mform->addGroup($func_group, 'allowedfuncs', get_string('allowedfuncs', 'qtype_algebra'), $spacers, true); + $mform->addGroup($funcgroup, 'allowedfuncs', get_string('allowedfuncs', 'qtype_algebra'), $spacers, true); $mform->addHelpButton('allowedfuncs', 'allowedfuncs', 'qtype_algebra'); $mform->disabledif ('allowedfuncs', 'allowedfuncs[all]', 'checked'); $mform->setDefault('allowedfuncs[all]', 'checked'); @@ -161,9 +161,9 @@ class qtype_algebra_edit_form extends question_edit_form { */ protected function variable_group($mform) { $grouparray = array(); - $grouparray[] = $mform->createElement('text', 'variable', get_string('variablename', 'qtype_algebra'), array('size'=>10)); - $grouparray[] = $mform->createElement('text', 'varmin', get_string('varmin', 'qtype_algebra'), array('size'=>10)); - $grouparray[] = $mform->createElement('text', 'varmax', get_string('varmax', 'qtype_algebra'), array('size'=>20)); + $grouparray[] = $mform->createElement('text', 'variable', get_string('variablename', 'qtype_algebra'), array('size' => 10)); + $grouparray[] = $mform->createElement('text', 'varmin', get_string('varmin', 'qtype_algebra'), array('size' => 10)); + $grouparray[] = $mform->createElement('text', 'varmax', get_string('varmax', 'qtype_algebra'), array('size' => 20)); return $grouparray; } @@ -210,11 +210,11 @@ class qtype_algebra_edit_form extends question_edit_form { $key = 0; foreach ($vars as $var) { // For every variable set the default values. - $default_values['variable['.$key.']'] = $var->name; + $defaultvalues['variable['.$key.']'] = $var->name; // Only set the min and max defaults if this variable has a range. - if ($var->min!='') { - $default_values['varmin['.$key.']'] = $var->min; - $default_values['varmax['.$key.']'] = $var->max; + if ($var->min != '') { + $defaultvalues['varmin['.$key.']'] = $var->min; + $defaultvalues['varmax['.$key.']'] = $var->max; } $key++; } @@ -222,27 +222,27 @@ class qtype_algebra_edit_form extends question_edit_form { // Add the default values for the allowed functions controls. // First check to see if there are any allowed functions defined. - if (count($question->options->allowedfuncs)>0) { + if (count($question->options->allowedfuncs) > 0) { // Clear the 'all functions' flag since functions are restricted. - $default_values['allowedfuncs[all]']=0; + $defaultvalues['allowedfuncs[all]'] = 0; // Loop over all the functions which the parser understands. foreach (qtype_algebra_parser::$functions as $func) { // For each function see if the function is in the allowed function // list and if so set the check box otherwise remove the check box. if (in_array($func, $question->options->allowedfuncs)) { - $default_values['allowedfuncs['.$func.']']=1; + $defaultvalues['allowedfuncs['.$func.']'] = 1; } else { - $default_values['allowedfuncs['.$func.']']=0; + $defaultvalues['allowedfuncs['.$func.']'] = 0; } } } else { // There are no allowed functions defined so all functions are allowed. - $default_values['allowedfuncs[all]']=1; + $defaultvalues['allowedfuncs[all]'] = 1; } // Add the default values to the question object in a form which the parent // set data method will be able to use to find the default values. - $question = (object)((array)$question + $default_values); + $question = (object)((array)$question + $defaultvalues); // Finally call the parent set data method to handle everything else. parent::set_data($question); @@ -262,13 +262,13 @@ class qtype_algebra_edit_form extends question_edit_form { $errors = parent::validation($data, $files); // Regular expression string to match a number. - $renumber='/([+-]*(([0-9]+\.[0-9]*)|([0-9]+)|(\.[0-9]+))|'. + $renumber = '/([+-]*(([0-9]+\.[0-9]*)|([0-9]+)|(\.[0-9]+))|'. '(([0-9]+\.[0-9]*)|([0-9]+)|(\.[0-9]+))E([-+]?\d+))/A'; // Perform sanity checks on the variables. $vars = $data['variable']; // Create an array of defined variables. - $varlist=array(); + $varlist = array(); foreach ($vars as $key => $var) { $trimvar = trim($var); $trimmin = trim($data['varmin'][$key]); @@ -286,12 +286,12 @@ class qtype_algebra_edit_form extends question_edit_form { $errors['variables['.$key.']'] = get_string('duplicatevar', 'qtype_algebra', $trimvar); } else { // Add the variable to the list of defined variables. - $varlist[]=$trimvar; + $varlist[] = $trimvar; } // If the comparison algorithm selected is evaluate then ensure that each variable // has a valid minimum and maximum defined. For the other types of comparison we can // ignore the range. - if ($data['compareby']=='eval') { + if ($data['compareby'] == 'eval') { // Check that a minimum has been defined. if ($trimmin == '') { $errors['variables['.$key.']'] = get_string('novarmin', 'qtype_algebra'); @@ -312,7 +312,7 @@ class qtype_algebra_edit_form extends question_edit_form { } // End check for eval type. } // End loop over variables. // Check that at least one variable is defined. - if (count($varlist)==0) { + if (count($varlist) == 0) { $errors['variables[0]'] = get_string('notenoughvars', 'qtype_algebra'); } @@ -323,30 +323,30 @@ class qtype_algebra_edit_form extends question_edit_form { $answercount = 0; $maxgrade = false; // Create an empty array to store the used variables. - $ansvars=array(); + $ansvars = array(); // Create an empty array to store the used functions. - $ansfuncs=array(); + $ansfuncs = array(); // Loop over all the answers in the form. foreach ($answers as $key => $answer) { // Try to parse the answer string using the parser. If this fails it will // throw an exception which we catch to generate the associated error string // for the expression. try { - $expr=$p->parse($answer); + $expr = $p->parse($answer); // Add any new variables to the list we are keeping. First we get the list // of variables in this answer. Then we get the array of variables which are // in this answer that are not in any previous answer (using array_diff). // Finally we merge this difference array with the list of all variables so far. - $tmpvars=$expr->get_variables(); - $ansvars=array_merge($ansvars, array_diff($tmpvars, $ansvars)); + $tmpvars = $expr->get_variables(); + $ansvars = array_merge($ansvars, array_diff($tmpvars, $ansvars)); // Check that all the variables in this answer have been declared. // Do this by looking for a non-empty array to be returned from the array_diff // between the list of all declared variables and the variables in this answer. - if ($d=array_diff($tmpvars, $varlist)) { + if ($d = array_diff($tmpvars, $varlist)) { $errors['answeroptions['.$key.']'] = get_string('undefinedvar', 'qtype_algebra', "'".implode("', '", $d)."'"); } // Do the same for functions which we did for variables. - $ansfuncs=array_merge($ansfuncs, array_diff($expr->get_functions(), $ansfuncs)); + $ansfuncs = array_merge($ansfuncs, array_diff($expr->get_functions(), $ansfuncs)); // Check that this is not an empty answer. if (!is_a($expr, "qtype_algebra_parser_nullterm")) { // Increase the number of answers. @@ -357,14 +357,14 @@ class qtype_algebra_edit_form extends question_edit_form { } } } catch (Exception $e) { - $errors['answeroptions['.$key.']']=$e->getMessage(); + $errors['answeroptions['.$key.']'] = $e->getMessage(); // Return here because subsequent errors may be wrong due to not counting the answer // which just failed to parse. return $errors; } } // Check that we have at least one answer. - if ($answercount==0) { + if ($answercount == 0) { $errors['answeroptions[0]'] = get_string('notenoughanswers', 'qtype_algebra'); } // Check that at least one question has the maximum possible grade. @@ -374,7 +374,7 @@ class qtype_algebra_edit_form extends question_edit_form { // Check for variables which are defined but never used. // Do this by looking for a non-empty array to be returned from array_diff. - if ($d=array_diff($varlist, $ansvars)) { + if ($d = array_diff($varlist, $ansvars)) { // Loop over all the variables in the form. foreach ($vars as $key => $var) { $trimvar = trim($var); @@ -386,8 +386,8 @@ class qtype_algebra_edit_form extends question_edit_form { } // Check that the tolerance is greater than or equal to zero. - if ($data['tolerance']<0) { - $errors['tolerance']=get_string('toleranceltzero', 'qtype_algebra'); + if ($data['tolerance'] < 0) { + $errors['tolerance'] = get_string('toleranceltzero', 'qtype_algebra'); } return $errors; diff --git a/lang/en/qtype_algebra.php b/lang/en/qtype_algebra.php index c6a5290..fefb0b2 100644 --- a/lang/en/qtype_algebra.php +++ b/lang/en/qtype_algebra.php @@ -28,7 +28,7 @@ $string['badequivtype'] = 'Invalid type: can only compare parser terms with othe $string['badfuncargs'] = 'Invalid arguments for the function \'{$a}\''; $string['decimal'] = '.'; $string['illegalplusminus'] = 'Found a + or - in an invalid location'; -$string['mismatchedbracket'] = 'Mismatched brackets: Open and close bracket pair not of same type \'$a\''; +$string['mismatchedbracket'] = 'Mismatched brackets: Open and close bracket pair not of same type \'{$a}\''; $string['mismatchedcloseb'] = 'Mismatched brackets: Close bracket without an open bracket found'; $string['mismatchedopenb'] = 'Mismatched brackets: Open bracket without a close bracket found'; $string['missingonearg'] = 'Syntax Error: Operator \'{$a}\' missing its argument'; @@ -95,6 +95,7 @@ receiving any grade for the question even if the response would match a given answer for the question.'; $string['disallowans'] = 'Disallowed Answer'; $string['disallowanswer'] = 'Disallowed Answer'; +$string['displayresponse'] = 'Display response'; $string['duplicatevar'] = 'Duplicated variable name: \'{$a}\' is already defined.'; $string['editingalgebra'] = 'Editing an Algebra question'; $string['evalchecks'] = 'Evaluation Checks'; diff --git a/question.php b/question.php index 10978a3..7d585bb 100644 --- a/question.php +++ b/question.php @@ -94,15 +94,15 @@ class qtype_algebra_question extends question_graded_by_strategy } // Create an array of variable names for the parser from the question if defined. - $varnames=array(); + $varnames = array(); if (isset($this->variables)) { foreach ($this->variables as $var) { - $varnames[]=$var->name; + $varnames[] = $var->name; } } // We now assume that we have a string to parse. Create a parser instance to // to this and return the parser expression at the top of the parse tree. - $p=new qtype_algebra_parser; + $p = new qtype_algebra_parser; // Perform the actual parsing inside a try-catch block so that any exceptions // can be caught and converted into errors. try { @@ -129,15 +129,15 @@ class qtype_algebra_question extends question_graded_by_strategy global $CFG; // Create an array of variable names for the parser from the question if defined. - $varnames=array(); + $varnames = array(); if (isset($this->variables)) { foreach ($this->variables as $var) { - $varnames[]=$var->name; + $varnames[] = $var->name; } } // We now assume that we have a string to parse. Create a parser instance to // to this and return the parser expression at the top of the parse tree. - $p=new qtype_algebra_parser; + $p = new qtype_algebra_parser; // Perform the actual parsing inside a try-catch block so that any exceptions // can be caught and converted into errors. try { @@ -147,7 +147,7 @@ class qtype_algebra_question extends question_graded_by_strategy } else { return '\['.$exp->tex().'\]'; } - + } catch (Exception $e) { return ''; } @@ -160,9 +160,9 @@ class qtype_algebra_question extends question_graded_by_strategy return false; } // Parse the previous response. - $expr=$this->parse_expression($prevresponse['answer']); + $expr = $this->parse_expression($prevresponse['answer']); // Parse the new response. - $testexpr=$this->parse_expression($newresponse['answer']); + $testexpr = $this->parse_expression($newresponse['answer']); // The type of comparison done depends on the comparision algorithm selected by // the question. Use the defined algorithm to select which comparison function // to call... @@ -187,7 +187,7 @@ class qtype_algebra_question extends question_graded_by_strategy if ($answer->answer == '*') { return true; } - $expr=$this->parse_expression($response['answer']); + $expr = $this->parse_expression($response['answer']); // Check that there is a response and if not return false. We do this here // because even an empty response should match a widlcard answer. if (is_a($expr, 'qtype_algebra_parser_nullterm')) { @@ -195,7 +195,7 @@ class qtype_algebra_question extends question_graded_by_strategy } // Now parse the answer. - $ansexpr=$this->parse_expression($answer->answer); + $ansexpr = $this->parse_expression($answer->answer); // The type of comparison done depends on the comparision algorithm selected by // the question. Use the defined algorithm to select which comparison function // to call... @@ -223,20 +223,20 @@ class qtype_algebra_question extends question_graded_by_strategy */ public function test_response_by_sage($response, $answer) { global $CFG; - $request=array( + $request = array( 'host' => $CFG->qtype_algebra_host, 'port' => $CFG->qtype_algebra_port, 'uri' => $CFG->qtype_algebra_uri, ); // Sets the name of the method to call to full_symbolic_compare. - $request['method']='full_symbolic_compare'; + $request['method'] = 'full_symbolic_compare'; // Get a list of all the variables to declare. - $vars=$response->get_variables(); - $vars=array_merge($vars, array_diff($vars, $answer->get_variables())); + $vars = $response->get_variables(); + $vars = array_merge($vars, array_diff($vars, $answer->get_variables())); // Sets the arguments to the sage string of the response and the list of variables. - $request['args']=array($answer->sage(), $response->sage(), $vars); + $request['args'] = array($answer->sage(), $response->sage(), $vars); // Calls the XML-RPC method on the server and returns the response. - return xu_rpc_http_concise($request)==0; + return xu_rpc_http_concise($request) == 0; } /** @@ -251,34 +251,34 @@ class qtype_algebra_question extends question_graded_by_strategy */ public function test_response_by_evaluation($response, $answer) { // Flag used to denote mismatch in response and answer. - $same=true; + $same = true; // Run the evaluation loop 10 times with different random variables... - for ($i=0; $i<$this->nchecks; $i++) { + for ($i = 0; $i < $this->nchecks; $i++) { // Create an array to store the values of all the variables. - $values=array(); + $values = array(); // Loop over all the variables in the question. foreach ($this->variables as $var) { // Set the value of the variable to a random number between the min and max. - $values[$var->name]=$var->min+lcg_value()*abs($var->max-$var->min); + $values[$var->name] = $var->min + lcg_value() * abs($var->max - $var->min); } - $resp_value=$response->evaluate($values); - $ans_value=$answer->evaluate($values); + $respvalue = $response->evaluate($values); + $ansvalue = $answer->evaluate($values); // Return false if only one of the reponse or answer gives NaN. - if (is_nan($resp_value) xor is_nan($ans_value)) { + if (is_nan($respvalue) xor is_nan($ansvalue)) { return false; } // Return false if only one of the reponse or answer is infinite. - if (is_infinite($resp_value) xor is_infinite($ans_value)) { + if (is_infinite($respvalue) xor is_infinite($ansvalue)) { return false; } // Use the fractional difference method if the answer has a value // which is clearly distinguishable from zero. - if (abs($ans_value)>1e-300) { + if (abs($ansvalue) > 1e-300) { // Get the difference between the response and answer evaluations. - $diff=abs(($resp_value-$ans_value)/$ans_value); + $diff = abs(($respvalue - $ansvalue) / $ansvalue); } else { // Otherwise use an arbitrary minimum value. - $diff=abs(($resp_value-$ans_value)/1e-300); + $diff = abs(($respvalue - $ansvalue) / 1e-300); } // Check to see if the difference is greater than tolerance. if ($diff > $this->tolerance) { diff --git a/questiontype.php b/questiontype.php index f755ce8..d8672b9 100644 --- a/questiontype.php +++ b/questiontype.php @@ -240,25 +240,25 @@ class qtype_algebra extends question_type { // then add the answers and variables to the database. try { // First write out all the variables associated with the question. - $variables=$this->save_question_variables($question); + $variables = $this->save_question_variables($question); // Loop over all the answers in the question form and parse them to generate // a parser string. This ensures a constant formatting is stored in the database. foreach ($question->answer as &$answer) { - $expr=$this->parse_expression($answer); + $expr = $this->parse_expression($answer); // TODO detect invalid answer and issue a warning. - $answer=$expr->sage(); + $answer = $expr->sage(); } // Now we need to write out all the answers to the question to the database. - $answers=$this->save_question_answers($question); + $answers = $this->save_question_answers($question); } catch (Exception $e) { // Error when adding answers or variables to the database so create a result class // and put the error string in the error member funtion and then return the class // This keeps us compatible with the existing save_question_options methods. - $result=new stdClass; - $result->error=$e->getMessage(); + $result = new stdClass; + $result->error = $e->getMessage(); return $result; } @@ -266,12 +266,12 @@ class qtype_algebra extends question_type { // in the parent class' save_question_options method called at the end of this method // Look for the 'all' option. If we find it then set the string to an empty value. if (array_key_exists('all', $question->allowedfuncs)) { - $question->allowedfuncs=''; + $question->allowedfuncs = ''; } else { // Not all functions are allowed so set allowed functions to those which are. // Create a comma separated string of the function names which are stored in the // keys of the array. - $question->allowedfuncs=implode(',', array_keys($question->allowedfuncs)); + $question->allowedfuncs = implode(',', array_keys($question->allowedfuncs)); } parent::save_question_options($question); @@ -299,7 +299,7 @@ class qtype_algebra extends question_type { return false; } // Check that we have answers and if not then bail since this question type requires answers. - if (count($question->options->answers)==0) { + if (count($question->options->answers) == 0) { notify('Failed to load question answers from the table question_answers for questionid ' . $question->id); return false; @@ -308,19 +308,19 @@ class qtype_algebra extends question_type { $question->options->variables = $DB->get_records('qtype_algebra_variables', array('question' => $question->id)); // Check that we have variables and if not then bail since this question type requires variables. - if (count($question->options->variables)==0) { + if (count($question->options->variables) == 0) { notify('Failed to load question variables from the table qtype_algebra_variables '. "for questionid $question->id"); return false; } // Check to see if there are any allowed functions. - if ($question->options->allowedfuncs!='') { + if ($question->options->allowedfuncs != '') { // Extract the allowed functions as an array. - $question->options->allowedfuncs=explode(',', $question->options->allowedfuncs); + $question->options->allowedfuncs = explode(',', $question->options->allowedfuncs); } else { // Otherwise just create an empty array. - $question->options->allowedfuncs=array(); + $question->options->allowedfuncs = array(); } // Everything worked so return true. @@ -339,7 +339,7 @@ class qtype_algebra extends question_type { * @param $extra extra information (not required for importing this question in this format) * @return text string containing the question data in XML format */ - public function import_from_xml($data, $question, qformat_xml $format, $extra=null) { + public function import_from_xml($data, $question, qformat_xml $format, $extra = null) { if (!array_key_exists('@', $data)) { return false; } @@ -350,47 +350,47 @@ class qtype_algebra extends question_type { // Import the common question headers. $qo = $format->import_headers($data); // Set the question type. - $qo->qtype='algebra'; + $qo->qtype = 'algebra'; $qo->compareby = $format->getpath($data, array('#', 'compareby', 0, '#'), 'eval'); $qo->tolerance = $format->getpath($data, array('#', 'tolerance', 0, '#'), '0'); $qo->nchecks = $format->getpath($data, array('#', 'nchecks', 0, '#'), '10'); $qo->disallow = $format->getpath($data, array('#', 'disallow', 0, '#', 'text', 0, '#'), '', true); $allowedfuncs = $format->getpath($data, array('#', 'allowedfuncs', 0, '#'), ''); - if ($allowedfuncs=='') { - $qo->allowedfuncs=array('all' => 1); + if ($allowedfuncs == '') { + $qo->allowedfuncs = array('all' => 1); } else { // Need to separate the allowed functions into an array of strings and then // flip the values of this array into the keys because this is what the // save options method requires. - $qo->allowedfuncs=array_flip(explode(',', $allowedfuncs)); + $qo->allowedfuncs = array_flip(explode(',', $allowedfuncs)); } $qo->answerprefix = $format->getpath($data, array('#', 'answerprefix', 0, '#', 'text', 0, '#'), '', true); // Import all the answers. $answers = $data['#']['answer']; - $a_count = 0; + $acount = 0; // Loop over each answer block found in the XML. foreach ($answers as $answer) { // Use the common answer import function in the format class to load the data. $ans = $format->import_answer($answer); - $qo->answer[$a_count] = $ans->answer['text']; - $qo->fraction[$a_count] = $ans->fraction; - $qo->feedback[$a_count] = $ans->feedback; - ++$a_count; + $qo->answer[$acount] = $ans->answer['text']; + $qo->fraction[$acount] = $ans->fraction; + $qo->feedback[$acount] = $ans->feedback; + ++$acount; } // Import all the variables. $vars = $data['#']['variable']; - $v_count = 0; + $vcount = 0; // Loop over each answer block found in the XML. foreach ($vars as $var) { - $qo->variable[$v_count] = $format->getpath($var, array('@', 'name'), 0); - $qo->varmin[$v_count] = $format->getpath($var, + $qo->variable[$vcount] = $format->getpath($var, array('@', 'name'), 0); + $qo->varmin[$vcount] = $format->getpath($var, array('#', 'min', 0, '#'), '0', false, get_string('novarmin', 'qtype_algebra')); - $qo->varmax[$v_count] = $format->getpath($var, + $qo->varmax[$vcount] = $format->getpath($var, array('#', 'max', 0, '#'), '0', false, get_string('novarmax', 'qtype_algebra')); - ++$v_count; + ++$vcount; } $format->import_hints($qo, $data); @@ -412,10 +412,10 @@ class qtype_algebra extends question_type { * @param $extra extra information (not required for exporting this question in this format) * @return text string containing the question data in XML format */ - public function export_to_xml($question, qformat_xml $format, $extra=null) { - $expout=''; + public function export_to_xml($question, qformat_xml $format, $extra = null) { + $expout = ''; // Create a text string of the allowed functions from the array. - $allowedfuncs=implode(',', $question->options->allowedfuncs); + $allowedfuncs = implode(',', $question->options->allowedfuncs); // Write out all the extra fields belonging to the algebra question type. $expout .= " {$question->options->compareby}\n"; $expout .= " {$question->options->tolerance}\n"; @@ -473,15 +473,15 @@ class qtype_algebra extends question_type { // Check whether we have a state object or a simple string. If a state // then replace it with the response string. if (isset($expr->responses[''])) { - $expr=$expr->responses['']; + $expr = $expr->responses['']; } // Create an empty array of variable names for the parser (no variable checking here as it is done in the form validation // TODO see in case of import. - $varnames=array(); + $varnames = array(); // We now assume that we have a string to parse. Create a parser instance to // to this and return the parser expression at the top of the parse tree. - $p=new qtype_algebra_parser; + $p = new qtype_algebra_parser; // Perform the actual parsing inside a try-catch block so that any exceptions. // can be caught and converted into errors. try { diff --git a/renderer.php b/renderer.php index ada0fb5..d2d00f0 100644 --- a/renderer.php +++ b/renderer.php @@ -71,22 +71,22 @@ class qtype_algebra_renderer extends qtype_renderer { $iframename = $nameprefix.'_if'; // Name of the javascript function which causes the entered formula to be rendered. - $df_name = $nameprefix.'_display'; + $dfname = $nameprefix.'_display'; // Create an array of variable names to use when displaying the function entered. - $varnames=array(); + $varnames = array(); if ($question and isset($question->variables)) { $variables = $question->variables; foreach ($question->variables as $var) { - $varnames[]=$var->name; + $varnames[] = $var->name; } } - $varnames=implode(',', $varnames); + $varnames = implode(',', $varnames); // Javascript function which the button uses to display the rendering // This function sents the source of the iframe to the 'displayformula.php' script giving // it an argument of the formula entered by the student. $displayfunction = - 'function '.$df_name."() {\n". + 'function '.$dfname."() {\n". ' var text="vars='.$varnames.'&expr="+escape(document.getElementsByName("'.$inputname.'")[0].value);'."\n". " if(text.length != 0) {\n". ' document.getElementsByName("'.$iframename.'")[0].src="'. @@ -100,12 +100,12 @@ class qtype_algebra_renderer extends qtype_renderer { $input = html_writer::empty_tag('input', $inputattributes) . $feedbackimg; $result = html_writer::tag('div', $questiontext, array('class' => 'qtext')); - $result .= html_writer::tag('script', $displayfunction, array('type'=>'text/javascript')); + $result .= html_writer::tag('script', $displayfunction, array('type' => 'text/javascript')); $result .= html_writer::start_tag('div', array('class' => 'ablock')); $result .= html_writer::start_tag('div', array('class' => 'prompt', 'style' => 'vertical-align: top')); if (isset($question->answerprefix) and !empty($question->answerprefix)) { - $opts=new StdClass; - $opts->para=false; + $opts = new StdClass; + $opts->para = false; $result .= html_writer::tag('div', format_text($question->answerprefix, FORMAT_MOODLE, $opts) . $input, array('class' => 'answer')); } else { @@ -120,11 +120,12 @@ class qtype_algebra_renderer extends qtype_renderer { array('class' => 'validationerror')); } $result .= html_writer::start_tag('div', array('class' => 'dispresponse')); - $result .= html_writer::empty_tag('input', array('type'=>'button', 'value'=>'Display Response', 'onclick'=>$df_name.'()')); + $result .= html_writer::empty_tag('input', array('type' => 'button', + 'value' => get_string('displayresponse', 'qtype_algebra'), 'onclick' => $dfname.'()')); $result .= html_writer::start_tag('iframe', - array('name'=>$iframename, 'width'=>'60%', 'height'=>60, 'align'=>'middle', 'src'=>'')); + array('name' => $iframename, 'width' => '60%', 'height' => 60, 'align' => 'middle', 'src' => '')); $result .= html_writer::end_tag('iframe'); - $result .= html_writer::tag('script', $df_name.'();', array('type'=>'text/javascript')); + $result .= html_writer::tag('script', $dfname.'();', array('type' => 'text/javascript')); $result .= html_writer::end_tag('div'); return $result; diff --git a/version.php b/version.php index 41b93ad..e5ccc83 100644 --- a/version.php +++ b/version.php @@ -23,8 +23,8 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'qtype_algebra'; -$plugin->version = 2014061900; +$plugin->version = 2014092700; $plugin->requires = 2013050100; -$plugin->release = '1.31 for Moodle 2.5, 2.6 and 2.7'; +$plugin->release = '1.32 for Moodle 2.5, 2.6 and 2.7'; $plugin->maturity = MATURITY_STABLE; -- cgit v1.2.3