MDL-20636 fix some more coding style issues in the question code.

This commit is contained in:
Tim Hunt
2011-05-20 18:50:41 +01:00
parent 449bfe90c6
commit e0736817f0
24 changed files with 169 additions and 374 deletions
+1
View File
@@ -548,6 +548,7 @@ function local_qeupgradehelper_format_var($name, $var) {
$out = preg_replace('/\n (?! )/', "\n ", $out);
$out = preg_replace('/\n (?! )/', "\n ", $out);
$out = preg_replace('/\n(?! )/', "\n ", $out);
$out = preg_replace('/\bNULL\b/', 'null', $out);
return " $name = $out;\n";
}
+1 -1
View File
@@ -427,7 +427,7 @@ class question_category_object {
}
// Update the category record.
$cat = NULL;
$cat = null;
$cat->id = $updateid;
$cat->name = $newname;
$cat->info = $newinfo;
+1 -1
View File
@@ -1653,7 +1653,7 @@ function question_edit_setup($edittab, $baseurl, $requirecmid = false, $requirec
$contextlist = join($contextlistarr, ' ,');
if (!empty($pagevars['cat'])){
$catparts = explode(',', $pagevars['cat']);
if (!$catparts[0] || (FALSE !== array_search($catparts[1], $contextlistarr)) ||
if (!$catparts[0] || (false !== array_search($catparts[1], $contextlistarr)) ||
!$DB->count_records_select("question_categories", "id = ? AND contextid = ?", array($catparts[0], $catparts[1]))) {
print_error('invalidcategory', 'question');
}
+4 -4
View File
@@ -36,9 +36,9 @@ defined('MOODLE_INTERNAL') || die();
class qformat_default {
public $displayerrors = true;
public $category = NULL;
public $category = null;
public $questions = array();
public $course = NULL;
public $course = null;
public $filename = '';
public $realfilename = '';
public $matchgrades = 'error';
@@ -602,7 +602,7 @@ class qformat_default {
$formatnotimplemented = get_string('formatnotimplemented', 'question');
echo "<p>$formatnotimplemented</p>";
return NULL;
return null;
}
/**
@@ -834,7 +834,7 @@ class qformat_default {
// if not overidden, then this is an error.
$formatnotimplemented = get_string('formatnotimplemented', 'question');
echo "<p>$formatnotimplemented</p>";
return NULL;
return null;
}
/**
@@ -287,11 +287,11 @@ class qtype_calculated_edit_form extends qtype_numerical_edit_form {
return $errors;
}
function is_valid_answer($answer, $data) {
protected function is_valid_answer($answer, $data) {
return !qtype_calculated_find_formula_errors($answer);
}
function valid_answer_message($answer) {
protected function valid_answer_message($answer) {
if (!$answer) {
return get_string('mustenteraformulaorstar', 'qtype_numerical');
} else {
+15 -14
View File
@@ -81,7 +81,7 @@ class qtype_calculated_question extends qtype_numerical_question
* is to work with {@link qtype_calculated_question_helper}.
*
* As well as this method, the class that implements this interface must have
* fields
* fields
* public $datasetloader; // of type qtype_calculated_dataset_loader
* public $vs; // of type qtype_calculated_variable_substituter
*
@@ -322,7 +322,7 @@ class qtype_calculated_variable_substituter {
/**
* Return an array of the variables and their values.
* @return array name => value.
* @return array name => value.
*/
public function get_values() {
return $this->values;
@@ -404,13 +404,13 @@ class qtype_calculated_variable_substituter {
while (preg_match('~\\{[[:alpha:]][^>} <{"\']*\\}~', $formula, $regs)) {
$formula = str_replace($regs[0], '1', $formula);
}
// Strip away empty space and lowercase it
$formula = strtolower(str_replace(' ', '', $formula));
$safeoperatorchar = '-+/*%>:^\~<?=&|!'; /* */
$operatorornumber = "[$safeoperatorchar.0-9eE]";
while (preg_match("~(^|[$safeoperatorchar,(])([a-z0-9_]*)" .
"\\(($operatorornumber+(,$operatorornumber+((,$operatorornumber+)+)?)?)?\\)~",
$formula, $regs)) {
@@ -421,14 +421,14 @@ class qtype_calculated_variable_substituter {
return get_string('illegalformulasyntax', 'qtype_calculated', $regs[0]);
}
break;
// Zero argument functions
case 'pi':
if ($regs[3]) {
return get_string('functiontakesnoargs', 'qtype_calculated', $regs[2]);
}
break;
// Single argument functions (the most common case)
case 'abs': case 'acos': case 'acosh': case 'asin': case 'asinh':
case 'atan': case 'atanh': case 'bindec': case 'ceil': case 'cos':
@@ -441,32 +441,33 @@ class qtype_calculated_variable_substituter {
return get_string('functiontakesonearg', 'qtype_calculated', $regs[2]);
}
break;
// Functions that take one or two arguments
case 'log': case 'round':
if (!empty($regs[5]) || empty($regs[3])) {
return get_string('functiontakesoneortwoargs', 'qtype_calculated', $regs[2]);
return get_string('functiontakesoneortwoargs', 'qtype_calculated',
$regs[2]);
}
break;
// Functions that must have two arguments
case 'atan2': case 'fmod': case 'pow':
if (!empty($regs[5]) || empty($regs[4])) {
return get_string('functiontakestwoargs', 'qtype_calculated', $regs[2]);
}
break;
// Functions that take two or more arguments
case 'min': case 'max':
if (empty($regs[4])) {
return get_string('functiontakesatleasttwo', 'qtype_calculated', $regs[2]);
}
break;
default:
return get_string('unsupportedformulafunction', 'qtype_calculated', $regs[2]);
}
// Exchange the function call with '1' and then chack for
// another function call...
if ($regs[1]) {
@@ -477,7 +478,7 @@ class qtype_calculated_variable_substituter {
$formula = preg_replace("~^$regs[2]\\([^)]*\\)~", '1', $formula);
}
}
if (preg_match("~[^$safeoperatorchar.0-9eE]+~", $formula, $regs)) {
return get_string('illegalformulasyntax', 'qtype_calculated', $regs[0]);
} else {
@@ -54,7 +54,8 @@ class qtype_calculated_test_helper extends question_test_helper {
$q->answers = array(
13 => new qtype_numerical_answer(13, '{a} + {b}', 1.0, 'Very good.', FORMAT_HTML, 0),
14 => new qtype_numerical_answer(14, '{a} - {b}', 0.0, 'Add. not subtract!.', FORMAT_HTML, 0),
14 => new qtype_numerical_answer(14, '{a} - {b}', 0.0, 'Add. not subtract!.',
FORMAT_HTML, 0),
17 => new qtype_numerical_answer(17, '*', 0.0, 'Completely wrong.', FORMAT_HTML, 0),
);
foreach ($q->answers as $answer) {
@@ -54,7 +54,8 @@ class qtype_calculatedmulti_test_helper extends question_test_helper {
$q->generalfeedback = 'Generalfeedback: {={a} + {b}} is the right answer.';
$q->answers = array(
13 => new qtype_numerical_answer(13, '{a} + {b}', 1.0, 'Very good.', FORMAT_HTML, 0),
14 => new qtype_numerical_answer(14, '{a} - {b}', 0.0, 'Add. not subtract!.', FORMAT_HTML, 0),
14 => new qtype_numerical_answer(14, '{a} - {b}', 0.0, 'Add. not subtract!.',
FORMAT_HTML, 0),
17 => new qtype_numerical_answer(17, '*', 0.0, 'Completely wrong.', FORMAT_HTML, 0),
);
$q->qtype = question_bank::get_qtype('calculated');
@@ -71,28 +72,3 @@ class qtype_calculatedmulti_test_helper extends question_test_helper {
return $q;
}
}
/**
* Test implementation of {@link qtype_calculated_dataset_loader}. Gets the values
* from an array passed to the constructor, rather than querying the database.
*
* @copyright 2011 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_calculated_test_dataset_loader extends qtype_calculated_dataset_loader{
protected $valuesets;
public function __construct($questionid, array $valuesets) {
parent::__construct($questionid);
$this->valuesets = $valuesets;
}
public function get_number_of_items() {
return count($this->valuesets);
}
public function load_values($itemnumber) {
return $this->valuesets[$itemnumber - 1];
}
}
@@ -54,7 +54,8 @@ class qtype_calculatedsimple_test_helper extends question_test_helper {
$q->answers = array(
13 => new qtype_numerical_answer(13, '{a} + {b}', 1.0, 'Very good.', FORMAT_HTML, 0),
14 => new qtype_numerical_answer(14, '{a} - {b}', 0.0, 'Add. not subtract!.', FORMAT_HTML, 0),
14 => new qtype_numerical_answer(14, '{a} - {b}', 0.0, 'Add. not subtract!.',
FORMAT_HTML, 0),
17 => new qtype_numerical_answer(17, '*', 0.0, 'Completely wrong.', FORMAT_HTML, 0),
);
foreach ($q->answers as $answer) {
@@ -103,9 +103,9 @@ class qtype_description_attempt_upgrader_test extends question_attempt_upgrader_
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '1',
);
$qsession = (object) array(
@@ -248,9 +248,9 @@ class qtype_description_attempt_upgrader_test extends question_attempt_upgrader_
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '0',
);
$qsession = (object) array(
@@ -396,9 +396,9 @@ class qtype_description_attempt_upgrader_test extends question_attempt_upgrader_
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '0',
);
$qsession = (object) array(
@@ -337,9 +337,9 @@ class qtype_essay_attempt_upgrader_test extends question_attempt_upgrader_test_b
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '0',
'options' => (object) array(
'answers' => array(
@@ -516,9 +516,9 @@ class qtype_essay_attempt_upgrader_test extends question_attempt_upgrader_test_b
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '0',
'options' => (object) array(
'answers' => array(
@@ -103,9 +103,9 @@ class qtype_match_attempt_upgrader_test extends question_attempt_upgrader_test_b
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '3',
'options' => (object) array(
'id' => '27',
@@ -352,9 +352,9 @@ class qtype_match_attempt_upgrader_test extends question_attempt_upgrader_test_b
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '1',
'options' => (object) array(
'id' => '35',
@@ -563,9 +563,9 @@ class qtype_match_attempt_upgrader_test extends question_attempt_upgrader_test_b
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '1',
'options' => (object) array(
'id' => '279',
@@ -162,17 +162,17 @@ class qtype_multichoice_attempt_upgrader_test extends question_attempt_upgrader_
1 => (object) array(
'hint' => 'First hint',
'questionid' => '2',
'shownumcorrect' => NULL,
'clearwrong' => NULL,
'options' => NULL,
'shownumcorrect' => null,
'clearwrong' => null,
'options' => null,
'id' => 1,
),
2 => (object) array(
'hint' => 'Second hint',
'questionid' => '2',
'shownumcorrect' => NULL,
'clearwrong' => NULL,
'options' => NULL,
'shownumcorrect' => null,
'clearwrong' => null,
'options' => null,
'id' => 2,
),
),
@@ -338,9 +338,9 @@ class qtype_multichoice_attempt_upgrader_test extends question_attempt_upgrader_
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '1',
'options' => (object) array(
'id' => '44',
@@ -505,9 +505,9 @@ class qtype_multichoice_attempt_upgrader_test extends question_attempt_upgrader_
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '1',
'options' => (object) array(
'id' => '152',
@@ -701,9 +701,9 @@ class qtype_multichoice_attempt_upgrader_test extends question_attempt_upgrader_
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '5',
'options' => (object) array(
'id' => '943',
@@ -937,9 +937,9 @@ class qtype_multichoice_attempt_upgrader_test extends question_attempt_upgrader_
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '1',
'options' => (object) array(
'id' => '44',
@@ -1071,9 +1071,9 @@ public function test_multichoice_deferredfeedback_qsession140() {
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '1',
'options' => (object) array(
'id' => '71',
@@ -1520,7 +1520,7 @@ public function test_multichoice_deferredfeedback_qsession140() {
'timemodified' => '1223623680',
'createdby' => '26409',
'modifiedby' => '351133',
'unlimited' => NULL,
'unlimited' => null,
'maxmark' => '1',
'options' => (object) array(
'id' => '23896',
@@ -1748,9 +1748,9 @@ public function test_multichoice_deferredfeedback_qsession140() {
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '1',
'options' => (object) array(
'id' => '117',
@@ -2187,9 +2187,9 @@ public function test_multichoice_deferredfeedback_qsession140() {
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '5',
'options' => (object) array(
'id' => '1439',
@@ -2408,9 +2408,9 @@ public function test_multichoice_deferredfeedback_qsession140() {
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '0',
'options' => (object) array(
'id' => '180',
@@ -2618,9 +2618,9 @@ public function test_multichoice_deferredfeedback_qsession140() {
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '0',
'options' => (object) array(
'id' => '175',
@@ -78,10 +78,6 @@ class restore_qtype_numerical_plugin extends restore_qtype_plugin {
$data->answer = $this->get_mappingid('question_answer', $data->answer);
// Insert record
$newitemid = $DB->insert_record('question_numerical', $data);
// Create mapping (not needed, no files nor childs nor states here)
//$this->set_mapping('question_numerical', $oldid, $newitemid);
} else {
// Nothing to remap if the question already existed
}
}
}
@@ -135,9 +135,9 @@ class qtype_numerical_attempt_upgrader_test extends question_attempt_upgrader_te
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '1',
'options' => (object) array(
'answers' => array(
+27 -16
View File
@@ -36,25 +36,32 @@ function xmldb_qtype_numerical_upgrade($oldversion) {
$dbman = $DB->get_manager();
//===== 1.9.0 upgrade line ======//
//===== 1.9.0 upgrade line ======//
if ($oldversion < 2009100100 ) { //New version in version.php
/// Define table question_numerical_options to be created
// Define table question_numerical_options to be created
$table = new xmldb_table('question_numerical_options');
/// Adding fields to table question_numerical_options
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('question', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('instructions', XMLDB_TYPE_TEXT, 'small', null, null, null, null);
$table->add_field('showunits', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('unitsleft', XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, '0');
$table->add_field('unitgradingtype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0');
$table->add_field('unitpenalty', XMLDB_TYPE_NUMBER, '12, 7', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0.1');
// Adding fields to table question_numerical_options
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('question', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED,
XMLDB_NOTNULL, null, '0');
$table->add_field('instructions', XMLDB_TYPE_TEXT, 'small', null,
null, null, null);
$table->add_field('showunits', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED,
XMLDB_NOTNULL, null, '0');
$table->add_field('unitsleft', XMLDB_TYPE_INTEGER, '4', null,
XMLDB_NOTNULL, null, '0');
$table->add_field('unitgradingtype', XMLDB_TYPE_INTEGER, '4', XMLDB_UNSIGNED,
XMLDB_NOTNULL, null, '0');
$table->add_field('unitpenalty', XMLDB_TYPE_NUMBER, '12, 7', XMLDB_UNSIGNED,
XMLDB_NOTNULL, null, '0.1');
/// Adding keys to table question_numerical_options
// Adding keys to table question_numerical_options
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
$table->add_key('question', XMLDB_KEY_FOREIGN, array('question'), 'question', array('id'));
/// Conditionally launch create table for question_calculated_options
// Conditionally launch create table for question_calculated_options
if (!$dbman->table_exists($table)) {
// $dbman->create_table doesnt return a result, we just have to trust it
$dbman->create_table($table);
@@ -66,7 +73,8 @@ function xmldb_qtype_numerical_upgrade($oldversion) {
// Define field instructionsformat to be added to question_numerical_options
$table = new xmldb_table('question_numerical_options');
$field = new xmldb_field('instructionsformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'instructions');
$field = new xmldb_field('instructionsformat', XMLDB_TYPE_INTEGER, '2', null,
XMLDB_NOTNULL, null, '0', 'instructions');
// Conditionally launch add field instructionsformat
if (!$dbman->field_exists($table, $field)) {
@@ -81,7 +89,8 @@ function xmldb_qtype_numerical_upgrade($oldversion) {
FROM {question_numerical_options} qno
JOIN {question} q ON qno.question = q.id');
foreach ($rs as $record) {
if ($CFG->texteditors !== 'textarea' && $record->oldquestiontextformat == FORMAT_MOODLE) {
if ($CFG->texteditors !== 'textarea' &&
$record->oldquestiontextformat == FORMAT_MOODLE) {
$record->instructions = text_to_html($record->questiontext, false, false, true);
$record->instructionsformat = FORMAT_HTML;
} else {
@@ -159,7 +168,8 @@ function xmldb_qtype_numerical_upgrade($oldversion) {
}
$oldfiles = $fs->get_area_files($numericaloptions->contextid,
'qtype_numerical', 'instruction', $numericaloptions->questionid, 'id', false);
'qtype_numerical', 'instruction', $numericaloptions->questionid,
'id', false);
foreach ($oldfiles as $oldfile) {
$filerecord = new stdClass();
$filerecord->component = 'question';
@@ -265,6 +275,7 @@ function qtype_numerical_convert_to_html($text, $oldformat) {
return text_to_html($text, null, $options['para'], $options['newlines']);
default:
throw new coding_exception('Unexpected text format when upgrading numerical questions.');
throw new coding_exception(
'Unexpected text format when upgrading numerical questions.');
}
}
@@ -243,7 +243,7 @@ class qtype_numerical_edit_form extends question_edit_form {
* @param array $errors the errors array to add to.
* @return array the updated errors array.
*/
function validate_answers($data, $errors) {
protected function validate_answers($data, $errors) {
// Check the answers.
$answercount = 0;
$maxgrade = false;
@@ -282,14 +282,14 @@ class qtype_numerical_edit_form extends question_edit_form {
* @param array $data the submitted data.
* @return bool whether this is a valid answer.
*/
function is_valid_answer($answer, $data) {
protected function is_valid_answer($answer, $data) {
return $answer == '*' || is_numeric($answer);
}
/**
* @return string erre describing what an answer should be.
*/
function valid_answer_message($answer) {
protected function valid_answer_message($answer) {
return get_string('answermustbenumberorstar', 'qtype_numerical');
}
@@ -299,7 +299,7 @@ class qtype_numerical_edit_form extends question_edit_form {
* @param array $errors the errors array to add to.
* @return array the updated errors array.
*/
function validate_numerical_options($data, $errors) {
protected function validate_numerical_options($data, $errors) {
if ($data['unitrole'] != qtype_numerical::UNITNONE && trim($data['unit'][0]) == '') {
$errors['unit[0]'] = get_string('unitonerequired', 'qtype_numerical');
}
+3 -6
View File
@@ -49,10 +49,6 @@ class qtype_numerical_question extends question_graded_automatically {
/** @var qtype_numerical_answer_processor */
public $ap;
public function __construct() {
parent::__construct();
}
public function get_expected_data() {
$expected = array('answer' => PARAM_RAW_TRIMMED);
if ($this->unitdisplay == qtype_numerical::UNITSELECT) {
@@ -244,7 +240,7 @@ class qtype_numerical_question extends question_graded_automatically {
$this->apply_unit_penalty($ans->fraction, $unit)));
}
function check_file_access($question, $state, $options, $contextid, $component,
public function check_file_access($question, $state, $options, $contextid, $component,
$filearea, $args) {
if ($component == 'question' && $filearea == 'answerfeedback') {
$currentanswer = $qa->get_last_qt_var('answer');
@@ -256,7 +252,8 @@ class qtype_numerical_question extends question_graded_automatically {
return $this->check_hint_file_access($qa, $options, $args);
} else {
return parent::check_file_access($qa, $options, $component, $filearea, $args, $forcedownload);
return parent::check_file_access($qa, $options, $component, $filearea,
$args, $forcedownload);
}
}
}
+11 -207
View File
@@ -27,8 +27,6 @@
defined('MOODLE_INTERNAL') || die();
//require_once($CFG->dirroot . '/question/type/numerical/question.php');
/**
* The numerical question type class.
@@ -130,7 +128,7 @@ class qtype_numerical extends question_type {
if ($defaultunit = $this->get_default_numerical_unit($question)) {
$question->options->showunits = self::UNITINPUT;
} else {
$question->options->showunits = self::UNITNONE ;
$question->options->showunits = self::UNITNONE;
}
$question->options->unitsleft = 0;
@@ -248,12 +246,13 @@ class qtype_numerical extends question_type {
* as old question.
*
*/
function save_unit_options($question) {
public function save_unit_options($question) {
global $DB;
$result = new stdClass();
$update = true ;
$options = $DB->get_record('question_numerical_options', array('question' => $question->id));
$update = true;
$options = $DB->get_record('question_numerical_options',
array('question' => $question->id));
if (!$options) {
$options = new stdClass();
$options->question = $question->id;
@@ -264,7 +263,7 @@ class qtype_numerical extends question_type {
$options->unitpenalty = $question->unitpenalty;
} else {
// Either an old question or a close question type.
$options->unitpenalty = 1 ;
$options->unitpenalty = 1;
}
$options->unitgradingtype = 0;
@@ -370,7 +369,7 @@ class qtype_numerical extends question_type {
return new qtype_numerical_answer_processor($cleanedunits, $unitsleft);
}
function delete_question($questionid, $contextid) {
public function delete_question($questionid, $contextid) {
global $DB;
$DB->delete_records('question_numerical', array('question' => $questionid));
$DB->delete_records('question_numerical_options', array('question' => $questionid));
@@ -379,202 +378,6 @@ class qtype_numerical extends question_type {
parent::delete_question($questionid, $contextid);
}
/**
* This function has been reinserted in numerical/questiontype.php to simplify
* the separate rendering of number and unit
*/
function print_question_formulation_and_controls(&$question, &$state, $cmoptions, $options) {
global $CFG, $OUTPUT;
$context = $this->get_context_by_category_id($question->category);
$readonly = empty($options->readonly) ? '' : 'readonly="readonly"';
$formatoptions = new stdClass();
$formatoptions->noclean = true;
$formatoptions->para = false;
$nameprefix = $question->name_prefix;
/// Print question text and media
$questiontext = format_text($question->questiontext,
$question->questiontextformat, $formatoptions, $cmoptions->course);
/// Print input controls
// as the entry is controlled the question type here is numerical
// In all cases there is a text input for the number
// If $question->options->showunits == NUMERICALQUESTIONUNITTEXTDISPLAY
// there is an additional text input for the unit
// If $question->options->showunits == NUMERICALQUESTIONUNITMULTICHOICEDISPLAY"
// radio elements display the defined unit
// The code allows the input number elememt to be displayed
// before i.e. at left or after at rigth of the unit variants.
$nameanswer = "name=\"".$question->name_prefix."answer\"";
$nameunit = "name=\"".$question->name_prefix."unit\"";
// put old answer data in $state->responses['answer'] and $state->responses['unit']
if (isset($state->responses['']) && $state->responses[''] != '' && !isset($state->responses['answer'])) {
$this->split_old_answer($state->responses[''], $question->options->units, $state->responses['answer'] ,$state->responses['unit'] );
}
// prepare the values of the input elements to be dispalyed answer i.e. number and unit
if (isset($state->responses['answer']) && $state->responses['answer']!='') {
$valueanswer = ' value="'.s($state->responses['answer']).'" ';
} else {
$valueanswer = ' value="" ';
}
if (isset($state->responses['unit']) && $state->responses['unit']!='') {
$valueunit = ' value="'.s($state->responses['unit']).'" ';
} else {
$valueunit = ' value="" ';
if ($question->options->showunits == NUMERICALQUESTIONUNITTEXTDISPLAY ) {
$valueunit = ' value="'.s($question->options->units[0]->unit).'" ';
}
}
$feedback = '';
$class = '';
$classunit = '' ;
$classunitvalue = '' ;
$feedbackimg = '';
$feedbackimgunit = '' ;
$answerasterisk = false ;
$response = '' ;
$valid_numerical_unit = false ;
$valid_numerical_unit_index = -1 ;
$unit_in_numerical_answer = false ;
$rawgrade = 0 ;
if ($options->feedback) {
$class = question_get_feedback_class(0);
$classunit = question_get_feedback_class(0);
$feedbackimg = question_get_feedback_image(0);
$feedbackimgunit = question_get_feedback_image(0);
$classunitvalue = 0 ;
$valid_numerical_unit_index = -1 ;
// if there is unit in answer and unitgradingtype = 0
// the grade is 0
//this is OK for the first answer with a good response
// having to test for * so response as long as not empty
// $response = $this->extract_numerical_response($state->responses['answer']);
// test for a greater than 0 grade
foreach($question->options->answers as $answer) {
if ($this->test_response($question, $state, $answer)) {
// Answer was correct or partially correct.
if ( $answer->answer === '*') {
$answerasterisk = true ;
}
// in all cases
$class = question_get_feedback_class($answer->fraction);
$feedbackimg = question_get_feedback_image($answer->fraction);
if ($question->options->unitgradingtype == 0 || ($question->options->unitgradingtype == 0 && $answer->answer === '*')) {
// if * then unit has the $answer->fraction value
// if $question->options->unitgradingtype == 0 everything has been checked
// if $question->options->showunits == NUMERICALQUESTIONUNITTEXTINPUTDISPLAY
// then number - unit combination has been used to test response
// so the unit should have same color
$classunit = question_get_feedback_class($answer->fraction);
$feedbackimgunit = question_get_feedback_image($answer->fraction);
$rawgrade = $answer->fraction ;
}else {
/* so we need to apply unit grading i.e. to check if the number-unit combination
* was the rigth one
* on NUMERICALQUESTIONUNITTEXTINPUTDISPLAY we need only to ckeck if applyunit will test OK
* with the $state->responses['unit'] value which cannot be empty
* if $state->responses['unit']
* if apply-unit is true with a specific unit as long as the unit as been written either in the
* we need the numerical response and test it with the available units
* if the unit used is good then it should be set OK
* however the unit could have been put in the number element in this case
* the unit penalty should be apllied.
* testing apply_unit with no units will get us a false response if there is any text in it
* testing apply_unit with a given unit will get a good value if the number is good with this unit
* apply unit will return the numerical if
* we need to know which conditions let to a good numerical value that were done in the
*/
$valid_numerical_unit = false ;
$rawgrade = $answer->fraction ;
$valid_numerical_unit_index = -1 ;
$invalid_unit_in_numerical_answer = false ;
if ( $answerasterisk ) {
$classunit = question_get_feedback_class($answer->fraction);
$feedbackimgunit = question_get_feedback_image($answer->fraction);
$valid_numerical_unit = true ;//everything is true with *
} else {
// if( isset($state->responses['unit']) && $state->responses['unit'] != '' ) {// unit should be written in the unit input or checked in multichoice
// we need to see if something was written in the answer field that was not in the number
// although we cannot actually detect units put before the number which will cause bad numerical.
// use extract response
$response = $this->extract_numerical_response($state->responses['answer']);
if(isset($response->unit ) && $response->unit != '') {
$unit_in_numerical_answer = true ;
}else {
$unit_in_numerical_answer = false ;
}
// the we let the testing to the two cases either
// NUMERICALQUESTIONUNITTEXTINPUTDISPLAY or
// NUMERICALQUESTIONUNITMULTICHOICEDISPLAY
if( !isset($state->responses['unit']) || $state->responses['unit'] == '' ) {
// unit should be written in the unit input or checked in multichoice
$valid_numerical_unit = false ;
$classunit = question_get_feedback_class(0);
$feedbackimgunit = question_get_feedback_image(0);
$empty_unit = true ;
} else {
// echo"<p> some unit answer <pre>";print_r($answer) ;echo"</pre></p>";
// echo"<p> some unit answer <pre>";print_r($answer) ;echo"</pre></p>";
$empty_unit = false ;
$valid_numerical_unit = false ;
foreach ($question->options->units as $key => $unit) {
if ($unit->unit == $state->responses['unit']) {
// $response = $this->apply_unit($state->responses['answer'].$unit->unit, array($question->options->units[$key])) ;
// echo "<p> avant false valid_numerical_unit_index $valid_numerical_unit_index ".$state->responses['answer']."</p>";
$invalid_unit_found = 0 ;
if ($response->number !== false) {
//echo "<p> avanr get valid_numerical_unit_index $valid_numerical_unit_index </p>";
// $this->get_tolerance_interval($answer);
$testresponse = $response->number /$unit->multiplier ;
if($answer->min <= $testresponse && $testresponse <= $answer->max) {
//echo "<p> apres min max valid_numerical_unit_index $valid_numerical_unit_index </p>";
$classunit = question_get_feedback_class($answer->fraction) ; //question_get_feedback_class(1);
$feedbackimgunit = question_get_feedback_image($rawgrade);
$valid_numerical_unit = true ;
$valid_numerical_unit_index = $key ;
break ;
}
}
}
}
}
}
}
if ($answer->feedback) {
$answer->feedback = quiz_rewrite_question_urls($answer->feedback, 'pluginfile.php', $context->id, 'question', 'answerfeedback', array($state->attempt, $state->question), $answer->id);
$feedback = format_text($answer->feedback, $answer->feedbackformat, $formatoptions, $cmoptions->course);
}
break;
}
}
}
$state->options->raw_unitpenalty = 0 ;
$raw_unitpenalty = 0 ;
if( $question->options->showunits == NUMERICALQUESTIONUNITNODISPLAY ||
$question->options->showunits == NUMERICALQUESTIONUNITTEXTDISPLAY ) {
$classunitvalue = 1 ;
}
if(! $answerasterisk && $question->options->unitgradingtype != 0 && (! $valid_numerical_unit || $unit_in_numerical_answer)) {
if($question->options->unitgradingtype == 1) {
$raw_unitpenalty = $question->options->unitpenalty * $rawgrade ;
}else {
$raw_unitpenalty = $question->options->unitpenalty ;
}
$state->options->raw_unitpenalty = $raw_unitpenalty ;
}
/// Removed correct answer, to be displayed later MDL-7496
include("$CFG->dirroot/question/type/numerical/display.html");
}
public function get_random_guess_score($questiondata) {
foreach ($questiondata->options->answers as $aid => $answer) {
if ('*' == trim($answer->answer)) {
@@ -641,13 +444,13 @@ class qtype_numerical extends question_type {
* @return float The rawresponse with the unit taken into
* account as a float.
*/
function apply_unit($rawresponse, $units, $unitsleft) {
public function apply_unit($rawresponse, $units, $unitsleft) {
$ap = $this->make_answer_processor($units, $unitsleft);
list($value, $unit) = $ap->apply_units($rawresponse);
return $value;
}
function move_files($questionid, $oldcontextid, $newcontextid) {
public function move_files($questionid, $oldcontextid, $newcontextid) {
$fs = get_file_storage();
parent::move_files($questionid, $oldcontextid, $newcontextid);
@@ -681,7 +484,8 @@ class qtype_numerical_answer_processor {
protected $regex = null;
public function __construct($units, $unitsbefore = false, $decsep = null, $thousandssep = null) {
public function __construct($units, $unitsbefore = false, $decsep = null,
$thousandssep = null) {
if (is_null($decsep)) {
$decsep = get_string('decsep', 'langconfig');
}
+10 -5
View File
@@ -51,11 +51,16 @@ class qtype_numerical_test_helper extends question_test_helper {
$num->questiontext = 'What is pi to two d.p.?';
$num->generalfeedback = 'Generalfeedback: 3.14 is the right answer.';
$num->answers = array(
13 => new qtype_numerical_answer(13, '3.14', 1.0, 'Very good.', FORMAT_HTML, 0),
14 => new qtype_numerical_answer(14, '3.142', 0.0, 'Too accurate.', FORMAT_HTML, 0.005),
15 => new qtype_numerical_answer(15, '3.1', 0.0, 'Not accurate enough.', FORMAT_HTML, 0.05),
16 => new qtype_numerical_answer(16, '3', 0.0, 'Not accurate enough.', FORMAT_HTML, 0.5),
17 => new qtype_numerical_answer(17, '*', 0.0, 'Completely wrong.', FORMAT_HTML, 0),
13 => new qtype_numerical_answer(13, '3.14', 1.0, 'Very good.',
FORMAT_HTML, 0),
14 => new qtype_numerical_answer(14, '3.142', 0.0, 'Too accurate.',
FORMAT_HTML, 0.005),
15 => new qtype_numerical_answer(15, '3.1', 0.0, 'Not accurate enough.',
FORMAT_HTML, 0.05),
16 => new qtype_numerical_answer(16, '3', 0.0, 'Not accurate enough.',
FORMAT_HTML, 0.5),
17 => new qtype_numerical_answer(17, '*', 0.0, 'Completely wrong.',
FORMAT_HTML, 0),
);
$num->qtype = question_bank::get_qtype('numerical');
$num->unitdisplay = qtype_numerical::UNITOPTIONAL;
@@ -95,7 +95,8 @@ class qtype_numerical_answer_processor_test extends UnitTestCase {
public function test_apply_units() {
$ap = new qtype_numerical_answer_processor(
array('m/s' => 1, 'c' => 3.3356409519815E-9, 'mph' => 2.2369362920544), false, '.', ',');
array('m/s' => 1, 'c' => 3.3356409519815E-9,
'mph' => 2.2369362920544), false, '.', ',');
$this->verify_value_and_unit(3e8, 'm/s', $ap, '3x10^8 m/s');
$this->verify_value_and_unit(3e8, '', $ap, '3x10^8');
@@ -108,7 +109,8 @@ class qtype_numerical_answer_processor_test extends UnitTestCase {
public function test_apply_units_separate_unit() {
$ap = new qtype_numerical_answer_processor(
array('m/s' => 1, 'c' => 3.3356409519815E-9, 'mph' => 2.2369362920544), false, '.', ',');
array('m/s' => 1, 'c' => 3.3356409519815E-9,
'mph' => 2.2369362920544), false, '.', ',');
$this->verify_value_and_unit(3e8, 'm/s', $ap, '3x10^8', 'm/s');
$this->verify_value_and_unit(3e8, '', $ap, '3x10^8', '');
@@ -670,7 +670,7 @@ class qtype_random_attempt_upgrader_test extends question_attempt_upgrader_test_
'timecreated' => '1262959197',
'timemodified' => '0',
'createdby' => '123783',
'modifiedby' => NULL,
'modifiedby' => null,
'unlimited' => '0',
'options' => (object) array(
'answers' => array(
@@ -726,13 +726,13 @@ class qtype_random_attempt_upgrader_test extends question_attempt_upgrader_test_
44315 => (object) array(
'questionid' => '83248',
'hint' => 'See Unit 2, Subsection 2.3.',
'rest' => NULL,
'rest' => null,
'id' => 44315,
),
44316 => (object) array(
'questionid' => '83248',
'hint' => '<p> Find the measurement on the horizontal axis, draw a line vertically up to meet the conversion line and read off the corresponding value on the vertical axis. </p><p>See Unit 2, Subsection 2.3. </p>',
'rest' => NULL,
'rest' => null,
'id' => 44316,
),
),
@@ -105,9 +105,9 @@ class qtype_shortanswer_attempt_upgrader_test extends question_attempt_upgrader_
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '1',
'options' => (object) array(
'answers' => array(
@@ -313,9 +313,9 @@ class qtype_shortanswer_attempt_upgrader_test extends question_attempt_upgrader_
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '1',
'options' => (object) array(
'answers' => array(
@@ -486,9 +486,9 @@ class qtype_shortanswer_attempt_upgrader_test extends question_attempt_upgrader_
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '1',
'options' => (object) array(
'answers' => array(
@@ -103,9 +103,9 @@ class qtype_truefalse_attempt_upgrader_test extends question_attempt_upgrader_te
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '5',
'options' => (object) array(
'id' => '98',
@@ -302,7 +302,7 @@ class qtype_truefalse_attempt_upgrader_test extends question_attempt_upgrader_te
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '1204646598',
'createdby' => NULL,
'createdby' => null,
'modifiedby' => '97230',
'unlimited' => '0',
'maxmark' => '1',
@@ -470,9 +470,9 @@ class qtype_truefalse_attempt_upgrader_test extends question_attempt_upgrader_te
'generalfeedbackformat' => '1',
'timecreated' => '0',
'timemodified' => '0',
'createdby' => NULL,
'modifiedby' => NULL,
'unlimited' => NULL,
'createdby' => null,
'modifiedby' => null,
'unlimited' => null,
'maxmark' => '1',
'options' => (object) array(
'id' => '24',