Commit 12039f2e authored by Tim Hunt's avatar Tim Hunt
Browse files

MDL-20636 Fix codechecker issues in qtype_multichoice.

parent 2b7c9bd2
......@@ -55,14 +55,16 @@ class backup_qtype_multichoice_plugin extends backup_qtype_plugin {
// Now create the qtype own structures
$multichoice = new backup_nested_element('multichoice', array('id'), array(
'layout', 'answers', 'single', 'shuffleanswers',
'correctfeedback', 'correctfeedbackformat', 'partiallycorrectfeedback', 'partiallycorrectfeedbackformat',
'correctfeedback', 'correctfeedbackformat',
'partiallycorrectfeedback', 'partiallycorrectfeedbackformat',
'incorrectfeedback', 'incorrectfeedbackformat', 'answernumbering', 'shownumcorrect'));
// Now the own qtype tree
$pluginwrapper->add_child($multichoice);
// set source to populate the data
$multichoice->set_source_table('question_multichoice', array('question' => backup::VAR_PARENTID));
$multichoice->set_source_table('question_multichoice',
array('question' => backup::VAR_PARENTID));
// don't need to annotate ids nor files
......
......@@ -47,10 +47,10 @@ class restore_qtype_multichoice_plugin extends restore_qtype_plugin {
// Add own qtype stuff
$elename = 'multichoice';
$elepath = $this->get_pathfor('/multichoice'); // we used get_recommended_name() so this works
// we used get_recommended_name() so this works
$elepath = $this->get_pathfor('/multichoice');
$paths[] = new restore_path_element($elename, $elepath);
return $paths; // And we return the interesting paths
}
......@@ -66,9 +66,10 @@ class restore_qtype_multichoice_plugin extends restore_qtype_plugin {
// Detect if the question is created or mapped
$oldquestionid = $this->get_old_parentid('question');
$newquestionid = $this->get_new_parentid('question');
$questioncreated = $this->get_mappingid('question_created', $oldquestionid) ? true : false;
$questioncreated = (bool) $this->get_mappingid('question_created', $oldquestionid);
// If the question has been created by restore, we need to create its question_multichoice too
// If the question has been created by restore, we need to create its
// question_multichoice too
if ($questioncreated) {
// Adjust some columns
$data->question = $newquestionid;
......@@ -82,8 +83,6 @@ class restore_qtype_multichoice_plugin extends restore_qtype_plugin {
$newitemid = $DB->insert_record('question_multichoice', $data);
// Create mapping (needed for decoding links)
$this->set_mapping('question_multichoice', $oldid, $newitemid);
} else {
// Nothing to remap if the question already existed
}
}
......@@ -135,7 +134,8 @@ class restore_qtype_multichoice_plugin extends restore_qtype_plugin {
$contents = array();
$fields = array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback');
$contents[] = new restore_decode_content('question_multichoice', $fields, 'question_multichoice');
$contents[] = new restore_decode_content('question_multichoice',
$fields, 'question_multichoice');
return $contents;
}
......
......@@ -69,27 +69,30 @@ function xmldb_qtype_multichoice_upgrade($oldversion) {
if ($oldversion < 2009021801) {
/// Define field correctfeedbackformat to be added to question_multichoice
// Define field correctfeedbackformat to be added to question_multichoice
$table = new xmldb_table('question_multichoice');
$field = new xmldb_field('correctfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'correctfeedback');
$field = new xmldb_field('correctfeedbackformat', XMLDB_TYPE_INTEGER, '2', null,
XMLDB_NOTNULL, null, '0', 'correctfeedback');
/// Conditionally launch add field correctfeedbackformat
// Conditionally launch add field correctfeedbackformat
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
/// Define field partiallycorrectfeedbackformat to be added to question_multichoice
$field = new xmldb_field('partiallycorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'partiallycorrectfeedback');
// Define field partiallycorrectfeedbackformat to be added to question_multichoice
$field = new xmldb_field('partiallycorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null,
XMLDB_NOTNULL, null, '0', 'partiallycorrectfeedback');
/// Conditionally launch add field partiallycorrectfeedbackformat
// Conditionally launch add field partiallycorrectfeedbackformat
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
/// Define field incorrectfeedbackformat to be added to question_multichoice
$field = new xmldb_field('incorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'incorrectfeedback');
// Define field incorrectfeedbackformat to be added to question_multichoice
$field = new xmldb_field('incorrectfeedbackformat', XMLDB_TYPE_INTEGER, '2', null,
XMLDB_NOTNULL, null, '0', 'incorrectfeedback');
/// Conditionally launch add field incorrectfeedbackformat
// Conditionally launch add field incorrectfeedbackformat
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
......@@ -103,12 +106,16 @@ function xmldb_qtype_multichoice_upgrade($oldversion) {
FROM {question_multichoice} qm
JOIN {question} q ON qm.question = q.id');
foreach ($rs as $record) {
if ($CFG->texteditors !== 'textarea' && $record->oldquestiontextformat == FORMAT_MOODLE) {
$record->correctfeedback = text_to_html($record->correctfeedback, false, false, true);
if ($CFG->texteditors !== 'textarea' &&
$record->oldquestiontextformat == FORMAT_MOODLE) {
$record->correctfeedback = text_to_html(
$record->correctfeedback, false, false, true);
$record->correctfeedbackformat = FORMAT_HTML;
$record->partiallycorrectfeedback = text_to_html($record->partiallycorrectfeedback, false, false, true);
$record->partiallycorrectfeedback = text_to_html(
$record->partiallycorrectfeedback, false, false, true);
$record->partiallycorrectfeedbackformat = FORMAT_HTML;
$record->incorrectfeedback = text_to_html($record->incorrectfeedback, false, false, true);
$record->incorrectfeedback = text_to_html(
$record->incorrectfeedback, false, false, true);
$record->incorrectfeedbackformat = FORMAT_HTML;
} else {
$record->correctfeedbackformat = $record->oldquestiontextformat;
......@@ -119,7 +126,7 @@ function xmldb_qtype_multichoice_upgrade($oldversion) {
}
$rs->close();
/// multichoice savepoint reached
// multichoice savepoint reached
upgrade_plugin_savepoint(true, 2009021801, 'qtype', 'multichoice');
}
......@@ -130,7 +137,8 @@ function xmldb_qtype_multichoice_upgrade($oldversion) {
// Define field shownumcorrect to be added to question_multichoice
$table = new xmldb_table('question_multichoice');
$field = new xmldb_field('shownumcorrect', XMLDB_TYPE_INTEGER, '2', null, XMLDB_NOTNULL, null, '0', 'answernumbering');
$field = new xmldb_field('shownumcorrect', XMLDB_TYPE_INTEGER, '2', null,
XMLDB_NOTNULL, null, '0', 'answernumbering');
// Launch add field shownumcorrect
if (!$dbman->field_exists($table, $field)) {
......
......@@ -41,15 +41,21 @@ class qtype_multichoice_edit_form extends question_edit_form {
* @param object $mform the form being built.
*/
protected function definition_inner($mform) {
$menu = array(get_string('answersingleno', 'qtype_multichoice'), get_string('answersingleyes', 'qtype_multichoice'));
$mform->addElement('select', 'single', get_string('answerhowmany', 'qtype_multichoice'), $menu);
$menu = array(
get_string('answersingleno', 'qtype_multichoice'),
get_string('answersingleyes', 'qtype_multichoice'),
);
$mform->addElement('select', 'single',
get_string('answerhowmany', 'qtype_multichoice'), $menu);
$mform->setDefault('single', 1);
$mform->addElement('advcheckbox', 'shuffleanswers', get_string('shuffleanswers', 'qtype_multichoice'), null, null, array(0,1));
$mform->addElement('advcheckbox', 'shuffleanswers',
get_string('shuffleanswers', 'qtype_multichoice'), null, null, array(0, 1));
$mform->addHelpButton('shuffleanswers', 'shuffleanswers', 'qtype_multichoice');
$mform->setDefault('shuffleanswers', 1);
$mform->addElement('select', 'answernumbering', get_string('answernumbering', 'qtype_multichoice'),
$mform->addElement('select', 'answernumbering',
get_string('answernumbering', 'qtype_multichoice'),
qtype_multichoice::get_numbering_styles());
$mform->setDefault('answernumbering', 'abc');
......@@ -107,7 +113,7 @@ class qtype_multichoice_edit_form extends question_edit_form {
if ($answercount == 0) {
$errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
$errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
} elseif ($answercount == 1) {
} else if ($answercount == 1) {
$errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
}
......@@ -115,14 +121,14 @@ class qtype_multichoice_edit_form extends question_edit_form {
/// Perform sanity checks on fractional grades
if ($data['single']) {
if ($maxfraction != 1) {
$maxfraction = $maxfraction * 100;
$errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice', $maxfraction);
$errors['fraction[0]'] = get_string('errfractionsnomax', 'qtype_multichoice',
$maxfraction * 100);
}
} else {
$totalfraction = round($totalfraction,2);
$totalfraction = round($totalfraction, 2);
if ($totalfraction != 1) {
$totalfraction = $totalfraction * 100;
$errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice', $totalfraction);
$errors['fraction[0]'] = get_string('errfractionsaddwrong', 'qtype_multichoice',
$totalfraction * 100);
}
}
return $errors;
......
......@@ -117,7 +117,8 @@ abstract class qtype_multichoice_base 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);
}
}
}
......@@ -348,7 +349,8 @@ class qtype_multichoice_multi_question extends qtype_multichoice_base {
}
/**
* @param array $response responses, as returned by {@link question_attempt_step::get_qt_data()}.
* @param array $response responses, as returned by
* {@link question_attempt_step::get_qt_data()}.
* @return int the number of choices that were selected. in this response.
*/
public function get_num_selected_choices(array $response) {
......@@ -397,7 +399,8 @@ class qtype_multichoice_multi_question extends qtype_multichoice_base {
* more choices than the number of right choices. This avoids giving the game away.
* @param question_hint_with_parts $hint a hint.
*/
protected function disable_hint_settings_when_too_many_selected(question_hint_with_parts $hint) {
protected function disable_hint_settings_when_too_many_selected(
question_hint_with_parts $hint) {
$hint->clearwrong = false;
}
......
......@@ -37,7 +37,8 @@ defined('MOODLE_INTERNAL') || die();
class qtype_multichoice extends question_type {
public function get_question_options($question) {
global $DB, $OUTPUT;
$question->options = $DB->get_record('question_multichoice', array('question' => $question->id), '*', MUST_EXIST);
$question->options = $DB->get_record('question_multichoice',
array('question' => $question->id), '*', MUST_EXIST);
parent::get_question_options($question);
}
......@@ -108,7 +109,7 @@ class qtype_multichoice extends question_type {
// Delete any left over old answer records.
$fs = get_file_storage();
foreach($oldanswers as $oldanswer) {
foreach ($oldanswers as $oldanswer) {
$fs->delete_area_files($context->id, 'question', 'answerfeedback', $oldanswer->id);
$DB->delete_records('question_answers', array('id' => $oldanswer->id));
}
......@@ -138,13 +139,15 @@ class qtype_multichoice extends question_type {
/// Perform sanity checks on fractional grades
if ($options->single) {
if ($maxfraction != 1) {
$result->noticeyesno = get_string('fractionsnomax', 'qtype_multichoice', $maxfraction * 100);
$result->noticeyesno = get_string('fractionsnomax', 'qtype_multichoice',
$maxfraction * 100);
return $result;
}
} else {
$totalfraction = round($totalfraction, 2);
if ($totalfraction != 1) {
$result->noticeyesno = get_string('fractionsaddwrong', 'qtype_multichoice', $totalfraction * 100);
$result->noticeyesno = get_string('fractionsaddwrong', 'qtype_multichoice',
$totalfraction * 100);
return $result;
}
}
......@@ -225,7 +228,8 @@ class qtype_multichoice extends question_type {
public static function get_numbering_styles() {
$styles = array();
foreach (array('abc', 'ABCD', '123', 'iii', 'IIII', 'none') as $numberingoption) {
$styles[$numberingoption] = get_string('answernumbering' . $numberingoption, 'qtype_multichoice');
$styles[$numberingoption] =
get_string('answernumbering' . $numberingoption, 'qtype_multichoice');
}
return $styles;
}
......@@ -249,8 +253,11 @@ class qtype_multichoice extends question_type {
parent::delete_files($questionid, $contextid);
$this->delete_files_in_answers($questionid, $contextid, true);
$fs->delete_area_files($contextid, 'qtype_multichoice', 'correctfeedback', $questionid);
$fs->delete_area_files($contextid, 'qtype_multichoice', 'partiallycorrectfeedback', $questionid);
$fs->delete_area_files($contextid, 'qtype_multichoice', 'incorrectfeedback', $questionid);
$fs->delete_area_files($contextid,
'qtype_multichoice', 'correctfeedback', $questionid);
$fs->delete_area_files($contextid,
'qtype_multichoice', 'partiallycorrectfeedback', $questionid);
$fs->delete_area_files($contextid,
'qtype_multichoice', 'incorrectfeedback', $questionid);
}
}
......@@ -94,9 +94,11 @@ abstract class qtype_multichoice_renderer_base extends qtype_with_combined_feedb
));
}
$radiobuttons[] = $hidden . html_writer::empty_tag('input', $inputattributes) .
html_writer::tag('label', $this->number_in_style($value, $question->answernumbering) .
html_writer::tag('label',
$this->number_in_style($value, $question->answernumbering) .
$question->format_text($ans->answer, $ans->answerformat,
$qa, 'question', 'answer', $ansid), array('for' => $inputattributes['id']));
$qa, 'question', 'answer', $ansid),
array('for' => $inputattributes['id']));
// $options->suppresschoicefeedback is a hack specific to the
// oumultiresponse question type. It would be good to refactor to
......
......@@ -189,7 +189,8 @@ class qtype_multichoice_multi_question_test extends UnitTestCase {
$this->assertEqual(array(0.5, question_state::$gradedpartial),
$question->grade_response(array('choice0' => '1')));
$this->assertEqual(array(0, question_state::$gradedwrong),
$question->grade_response(array('choice0' => '1', 'choice1' => '1', 'choice2' => '1')));
$question->grade_response(
array('choice0' => '1', 'choice1' => '1', 'choice2' => '1')));
$this->assertEqual(array(0, question_state::$gradedwrong),
$question->grade_response(array('choice1' => '1')));
}
......
......@@ -37,7 +37,7 @@ require_once($CFG->dirroot . '/question/type/multichoice/questiontype.php');
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_multichoice_test extends UnitTestCase {
var $qtype;
protected $qtype;
public function setUp() {
$this->qtype = new qtype_multichoice();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment