Skip to content

Commit

Permalink
MDL-27410 qtype_calculated fix basic question editing and preview.
Browse files Browse the repository at this point in the history
At least, after these changes, I was able to create a calculated question through the Moodle UI and then preview it. I am sure more testing and fixing is needed to make sure everything works.
  • Loading branch information
timhunt committed May 18, 2011
1 parent 1da4060 commit 18f9b2d
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 335 deletions.
2 changes: 1 addition & 1 deletion question/type/calculated/datasetdefinitions_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class question_dataset_dependent_definitions_form extends moodleform {
public function __construct($submiturl, $question) {
global $DB;
$this->question = $question;
$this->qtypeobj = $QTYPES[$this->question->qtype];
$this->qtypeobj = question_bank::get_qtype($this->question->qtype);
// Validate the question category.
if (!$category = $DB->get_record('question_categories',
array('id' => $question->category))) {
Expand Down
4 changes: 2 additions & 2 deletions question/type/calculated/datasetitems_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __construct($submiturl, $question, $regenerate) {
global $SESSION, $CFG, $DB;
$this->regenerate = $regenerate;
$this->question = $question;
$this->qtypeobj =& $QTYPES[$this->question->qtype];
$this->qtypeobj = question_bank::get_qtype($this->question->qtype);
// Validate the question category.
if (!$category = $DB->get_record('question_categories',
array('id' => $question->category))) {
Expand Down Expand Up @@ -192,7 +192,7 @@ protected function definition() {

$answerlengthformats = array(
'1' => get_string('decimalformat', 'qtype_numerical'),
'2' => get_string('significantfiguresformat', 'quiz')
'2' => get_string('significantfiguresformat', 'qtype_calculated')
);
$mform->addElement('select', 'correctanswerformat['.$key.']',
get_string('correctanswershowsformat', 'qtype_calculated'),
Expand Down
78 changes: 33 additions & 45 deletions question/type/calculated/edit_calculated_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/question/type/numerical/edit_numerical_form.php');


/**
* Calculated question type editing form definition.
*
* @copyright 2007 Jamie Pratt [email protected]
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_calculated_edit_form extends question_edit_form {
class qtype_calculated_edit_form extends qtype_numerical_edit_form {
/**
* Handle to the question type for this question.
*
Expand All @@ -50,7 +52,6 @@ public function __construct($submiturl, $question, $category, $contexts,
$formeditable = true) {
global $CFG, $DB;
$this->question = $question;
$this->qtypeobj = $QTYPES[$this->question->qtype];
if ('1' == optional_param('reload', '', PARAM_INT)) {
$this->reload = true;
} else {
Expand Down Expand Up @@ -101,7 +102,7 @@ public function get_per_answer_fields($mform, $label, $gradeoptions,

$answerlengthformats = array(
'1' => get_string('decimalformat', 'qtype_numerical'),
'2' => get_string('significantfiguresformat', 'quiz')
'2' => get_string('significantfiguresformat', 'qtype_calculated')
);
$addrepeated[] = $mform->createElement('select', 'correctanswerformat',
get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
Expand All @@ -115,8 +116,8 @@ public function get_per_answer_fields($mform, $label, $gradeoptions,
*
* @param MoodleQuickForm $mform the form being built.
*/
protected function definition_inner(&$mform) {
$this->qtypeobj = $QTYPES[$this->qtype()];
protected function definition_inner($mform) {
$this->qtypeobj = question_bank::get_qtype($this->qtype());
$label = get_string('sharedwildcards', 'qtype_calculated');
$mform->addElement('hidden', 'initialcategory', 1);
$mform->addElement('hidden', 'reload', 1);
Expand Down Expand Up @@ -152,8 +153,8 @@ protected function definition_inner(&$mform) {

$repeated = array();

$QTYPES['numerical']->add_units_options($mform, $this);
$QTYPES['numerical']->add_units_elements($mform, $this);
$this->add_unit_options($mform, $this);
$this->add_unit_fields($mform, $this);

//hidden elements
$mform->addElement('hidden', 'synchronize', '');
Expand All @@ -163,8 +164,6 @@ protected function definition_inner(&$mform) {
}

public function data_preprocessing($question) {
global $QTYPES;

$default_values = array();
if (isset($question->options)) {
$answers = $question->options->answers;
Expand Down Expand Up @@ -194,8 +193,6 @@ public function data_preprocessing($question) {
}
}
$default_values['synchronize'] = $question->options->synchronize;
// set unit data, prepare files in instruction area
$QTYPES['numerical']->set_numerical_unit_data($this, $question, $default_values);
}
if (isset($question->options->single)) {
$default_values['single'] = $question->options->single;
Expand Down Expand Up @@ -225,6 +222,9 @@ public function data_preprocessing($question) {
$this->_form->_elements[$this->_form->_elementIndex['listcategory']]->_text = $html2;
$question = (object)((array)$question + $default_values);

$question = $this->data_preprocessing_units($question);
$question = $this->data_preprocessing_unit_options($question);

return $question;
}

Expand All @@ -234,7 +234,6 @@ public function qtype() {

public function validation($data, $files) {

$errors = parent::validation($data, $files);
// verifying for errors in {=...} in question text;
$qtext = "";
$qtextremaining = $data['questiontext']['text'];
Expand All @@ -255,57 +254,46 @@ public function validation($data, $files) {
}
}
}

$errors = parent::validation($data, $files);

// Check that the answers use datasets.
$answers = $data['answer'];
$answercount = 0;
$maxgrade = false;
$possibledatasets = $this->qtypeobj->find_dataset_names($data['questiontext']['text']);
$mandatorydatasets = array();
foreach ($answers as $key => $answer) {
$mandatorydatasets += $this->qtypeobj->find_dataset_names($answer);
}
if (count($mandatorydatasets)== 0) {
if (empty($mandatorydatasets)) {
foreach ($answers as $key => $answer) {
$errors['answer['.$key.']'] =
get_string('atleastonewildcard', 'qtype_datasetdependent');
}
}
// regular calculated

// Validate the answer format.
foreach ($answers as $key => $answer) {
// check no of choices
// the * for everykind of answer not actually implemented
$trimmedanswer = trim($answer);
if ($trimmedanswer != '' || $answercount == 0) {
$eqerror = qtype_calculated_find_formula_errors($trimmedanswer);
if (false !== $eqerror) {
$errors['answer['.$key.']'] = $eqerror;
}
}
if ($trimmedanswer != '') {
if ('2' == $data['correctanswerformat'][$key]
&& '0' == $data['correctanswerlength'][$key]) {
if (trim($answer)) {
if ($data['correctanswerformat'][$key] == 2 &&
$data['correctanswerlength'][$key] == '0') {
$errors['correctanswerlength['.$key.']'] =
get_string('zerosignificantfiguresnotallowed', 'qtype_calculated');
}
if (!is_numeric($data['tolerance'][$key])) {
$errors['tolerance['.$key.']'] =
get_string('mustbenumeric', 'qtype_calculated');
}
if ($data['fraction'][$key] == 1) {
$maxgrade = true;
}

$answercount++;
}
}

$QTYPES['numerical']->validate_numerical_options($data, $errors);
if ($answercount == 0) {
$errors['answer[0]'] = get_string('atleastoneanswer', 'qtype_calculated');
}
if ($maxgrade == false) {
$errors['fraction[0]'] = get_string('fractionsnomax', 'question');
}

return $errors;
}

function is_valid_answer($answer, $data) {
return !qtype_calculated_find_formula_errors($answer);
}

function valid_answer_message($answer) {
if (!$answer) {
return get_string('mustenteraformulaorstar', 'qtype_numerical');
} else {
return qtype_calculated_find_formula_errors($answer);
}
}
}
5 changes: 5 additions & 0 deletions question/type/calculated/lang/en/qtype_calculated.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
$string['addsets'] = 'Add set(s)';
$string['answerhdr'] = 'Answer';
$string['answerstoleranceparam'] = 'Answers tolerance parameters';
$string['anyvalue'] = 'Any value';
$string['atleastoneanswer'] = 'You need to provide at least one answer.';
$string['atleastonerealdataset']='There should be at least one real dataset in question text';
$string['atleastonewildcard']='There should be at least one wild card in answer formula or question text';
Expand All @@ -43,6 +44,7 @@
$string['calculatedsummary'] = 'Calculated questions are like numerical questions but with the numbers used selected randomly from a set when the quiz is taken.';
$string['choosedatasetproperties'] = 'Choose wildcards dataset properties';
$string['choosedatasetproperties_help'] = 'A dataset is a set of values inserted in place of a wildcard. You can create a private dataset for a specific question, or a shared dataset that can be used for other calculated questions within the category.';
$string['correctanswerformula'] = 'Correct answer formula';
$string['correctanswershows'] = 'Correct answer shows';
$string['correctanswershowsformat'] = 'Format';
$string['correctfeedback'] = 'For any correct response';
Expand Down Expand Up @@ -86,11 +88,14 @@
$string['loguniformbit'] = 'digits, from a loguniform distribution';
$string['makecopynextpage'] = 'Next page (new question)';
$string['mandatoryhdr'] = 'Mandatory wild cards present in answers';
$string['max'] = 'Max';
$string['min'] = 'Min';
$string['minmax'] = 'Range of Values';
$string['missingformula'] = 'Missing formula';
$string['missingname'] = 'Missing question name';
$string['missingquestiontext'] = 'Missing question text';
$string['mustbenumeric'] = 'You must enter a number here.';
$string['mustenteraformulaorstar'] = 'You must enter a formula or \'*\'.';
$string['mustnotbenumeric'] = 'This can\'t be a number.';
$string['newcategory1'] = 'will use a new shared dataset';
$string['newcategory2'] = 'a file from a new set of files that may also be used by other questions in this category';
Expand Down
7 changes: 4 additions & 3 deletions question/type/calculated/question.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function start_attempt(question_attempt_step $step) {

public function apply_attempt_state(question_attempt_step $step) {
$values = array();
foreach ($step->get_qt_datavalues() as $name => $value) {
foreach ($step->get_qt_data() as $name => $value) {
if (substr($name, 0, 5) === '_var_') {
$values[substr($name, 5)] = $value;
}
Expand All @@ -81,7 +81,6 @@ public function apply_attempt_state(question_attempt_step $step) {
protected function calculate_all_expressions() {
$this->questiontext = $this->vs->replace_expressions_in_text($this->questiontext);
$this->generalfeedback = $this->vs->replace_expressions_in_text($this->generalfeedback);
// TODO etc.

foreach ($this->answers as $ans) {
if ($ans->answer && $ans->answer !== '*') {
Expand Down Expand Up @@ -142,7 +141,9 @@ public function get_number_of_items() {
* @return array name => value;
*/
protected function load_values($itemnumber) {
return $DB->get_records_sql('
global $DB;

return $DB->get_records_sql_menu('
SELECT qdd.name, qdi.value
FROM {question_dataset_items} qdi
JOIN {question_dataset_definitions} qdd ON qdd.id = qdi.definition
Expand Down
Loading

0 comments on commit 18f9b2d

Please sign in to comment.