Skip to content

Commit

Permalink
MDL-20636 Mostly working conversion of the multichoice question type.
Browse files Browse the repository at this point in the history
Currently, you get an error on saving the editing form.
  • Loading branch information
timhunt committed Jan 13, 2011
1 parent 9481434 commit c9c989a
Show file tree
Hide file tree
Showing 14 changed files with 1,363 additions and 489 deletions.
48 changes: 24 additions & 24 deletions question/todo/diffstat.txt
Expand Up @@ -335,30 +335,30 @@ DONE question/type/essay/version.php | 2 -
question/type/match/simpletest/testwalkthrough.php | 474 ++++
question/type/match/version.php | 4 +-

question/type/missingtype/display.html | 24 -
question/type/missingtype/edit_missingtype_form.php | 102 +-
question/type/missingtype/lang/en_utf8/qtype_missingtype.php | 10 +
question/type/missingtype/question.php | 86 +
question/type/missingtype/questiontype.php | 125 +-
question/type/missingtype/renderer.php | 36 +
question/type/missingtype/simpletest/testmissingtype.php | 125 +

question/type/multianswer/db/upgrade.php | 1 -
question/type/multianswer/edit_multianswer_form.php | 11 +-
question/type/multianswer/questiontype.php | 61 +-
question/type/multianswer/version.php | 2 -
question/type/multichoice/db/install.xml | 3 +-
question/type/multichoice/db/upgrade.php | 16 +-
question/type/multichoice/display.html | 38 -
question/type/multichoice/edit_multichoice_form.php | 88 +-
question/type/multichoice/lang/en_utf8/qtype_multichoice.php | 42 +
question/type/multichoice/question.php | 367 +++
question/type/multichoice/questiontype.php | 399 +--
question/type/multichoice/renderer.php | 313 +++
question/type/multichoice/simpletest/testquestion.php | 236 ++
question/type/multichoice/simpletest/testquestiontype.php | 90 +
question/type/multichoice/simpletest/testwalkthrough.php | 91 +
question/type/multichoice/version.php | 6 +-
DONE question/type/missingtype/display.html | 24 -
DONE question/type/missingtype/edit_missingtype_form.php | 102 +-
DONE question/type/missingtype/lang/en_utf8/qtype_missingtype.php | 10 +
DONE question/type/missingtype/question.php | 86 +
DONE question/type/missingtype/questiontype.php | 125 +-
DONE question/type/missingtype/renderer.php | 36 +
DONE question/type/missingtype/simpletest/testmissingtype.php | 125 +

DONE question/type/multianswer/db/upgrade.php | 1 -
DONE question/type/multianswer/edit_multianswer_form.php | 11 +-
DONE question/type/multianswer/questiontype.php | 61 +-
DONE question/type/multianswer/version.php | 2 -
DONE question/type/multichoice/db/install.xml | 3 +-
DONE question/type/multichoice/db/upgrade.php | 16 +-
DONE question/type/multichoice/display.html | 38 -
DONE question/type/multichoice/edit_multichoice_form.php | 88 +-
DONE question/type/multichoice/lang/en_utf8/qtype_multichoice.php | 42 +
DONE question/type/multichoice/question.php | 367 +++
DONE question/type/multichoice/questiontype.php | 399 +--
DONE question/type/multichoice/renderer.php | 313 +++
DONE question/type/multichoice/simpletest/testquestion.php | 236 ++
DONE question/type/multichoice/simpletest/testquestiontype.php | 90 +
DONE question/type/multichoice/simpletest/testwalkthrough.php | 91 +
DONE question/type/multichoice/version.php | 6 +-

question/type/numerical/db/upgrade.php | 2 -
question/type/numerical/edit_numerical_form.php | 48 +-
Expand Down
30 changes: 30 additions & 0 deletions question/type/edit_question_form.php
Expand Up @@ -425,6 +425,7 @@ public function set_data($question) {
}
}
}

// subclass adds data_preprocessing code here
$question = $this->data_preprocessing($question);

Expand Down Expand Up @@ -478,6 +479,35 @@ protected function data_preprocessing_answers($question) {
return $question;
}

protected function data_preprocessing_combined_feedback($question, $withshownumcorrect = false) {
if (empty($question->options)) {
return $question;
}

foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
$draftid = file_get_submitted_draft_itemid($feedbackname);
$question->$feedbackname = array();
$question->$feedbackname['text'] = file_prepare_draft_area(
$draftid, // draftid
$this->context->id, // context
'qtype_multichoice', // component
$feedbackname, // filarea
!empty($question->id) ? (int) $question->id : null, // itemid
$this->fileoptions, // options
$question->options->$feedbackname // text
);
$feedbackformat = $feedbackname . 'format';
$question->$feedbackname['format'] = $question->options->$feedbackformat;
$question->$feedbackname['itemid'] = $draftid;
}

if ($withshownumcorrect) {
$question->shownumcorrect = $question->options->shownumcorrect;
}

return $question;
}

protected function data_preprocessing_hints($question, $withclearwrong = false, $withshownumpartscorrect = false) {
if (empty($question->hints)) {
return $question;
Expand Down
3 changes: 2 additions & 1 deletion question/type/multichoice/db/install.xml
Expand Up @@ -18,7 +18,8 @@
<FIELD NAME="partiallycorrectfeedbackformat" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" PREVIOUS="partiallycorrectfeedback" NEXT="incorrectfeedback"/>
<FIELD NAME="incorrectfeedback" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" COMMENT="Feedback shown for any incorrect response." PREVIOUS="partiallycorrectfeedbackformat" NEXT="incorrectfeedbackformat"/>
<FIELD NAME="incorrectfeedbackformat" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" PREVIOUS="incorrectfeedback" NEXT="answernumbering"/>
<FIELD NAME="answernumbering" TYPE="char" LENGTH="10" NOTNULL="true" DEFAULT="abc" SEQUENCE="false" COMMENT="Indicates how and whether the choices should be numbered." PREVIOUS="incorrectfeedbackformat"/>
<FIELD NAME="answernumbering" TYPE="char" LENGTH="10" NOTNULL="true" DEFAULT="abc" SEQUENCE="false" COMMENT="Indicates how and whether the choices should be numbered." PREVIOUS="incorrectfeedbackformat" NEXT="shownumcorrect"/>
<FIELD NAME="shownumcorrect" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" COMMENT="If true, then when the user gets a multiple-response question partially correct, tell them how many choices they got correct alongside the feedback." PREVIOUS="answernumbering"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="question"/>
Expand Down
20 changes: 18 additions & 2 deletions question/type/multichoice/db/upgrade.php
Expand Up @@ -109,7 +109,23 @@ function xmldb_qtype_multichoice_upgrade($oldversion) {
upgrade_plugin_savepoint(true, 2009021801, 'qtype', 'multichoice');
}

return true;
}
// Add new shownumcorrect field. If this is true, then when the user gets a
// multiple-response question partially correct, tell them how many choices
// they got correct alongside the feedback.
if ($oldversion < 2011011200) {

// 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');

// Launch add field shownumcorrect
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// multichoice savepoint reached
upgrade_plugin_savepoint(true, 2011011200, 'qtype', 'multichoice');
}

return true;
}
125 changes: 42 additions & 83 deletions question/type/multichoice/edit_multichoice_form.php
Expand Up @@ -18,25 +18,27 @@
defined('MOODLE_INTERNAL') || die();

/**
* Defines the editing form for the multichoice question type.
* Defines the editing form for the multiple choice question type.
*
* @package qtype
* @subpackage multichoice
* @copyright &copy; 2007 Jamie Pratt
* @author Jamie Pratt me@jamiep.org
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
* @package questionbank
* @subpackage questiontypes
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

/**
* multiple choice editing form definition.
* Multiple choice editing form definition.
*
* @copyright &copy; 2007 Jamie Pratt
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class question_edit_multichoice_form extends question_edit_form {
/**
* Add question-type specific form fields.
*
* @param object $mform the form being built.
*/
function definition_inner(&$mform) {
protected function definition_inner($mform) {
global $QTYPES;

$menu = array(get_string('answersingleno', 'qtype_multichoice'), get_string('answersingleyes', 'qtype_multichoice'));
Expand All @@ -47,112 +49,65 @@ function definition_inner(&$mform) {
$mform->addHelpButton('shuffleanswers', 'shuffleanswers', 'qtype_multichoice');
$mform->setDefault('shuffleanswers', 1);

$numberingoptions = $QTYPES[$this->qtype()]->get_numbering_styles();
$menu = array();
foreach ($numberingoptions as $numberingoption) {
$menu[$numberingoption] = get_string('answernumbering' . $numberingoption, 'qtype_multichoice');
}
$mform->addElement('select', 'answernumbering', get_string('answernumbering', 'qtype_multichoice'), $menu);
$mform->addElement('select', 'answernumbering', get_string('answernumbering', 'qtype_multichoice'),
qtype_multichoice::get_numbering_styles());
$mform->setDefault('answernumbering', 'abc');

/* $mform->addElement('static', 'answersinstruct', get_string('choices', 'qtype_multichoice'), get_string('fillouttwochoices', 'qtype_multichoice'));
$mform->closeHeaderBefore('answersinstruct');
*/
$creategrades = get_grade_options();
$this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'),
$creategrades->gradeoptionsfull, max(5, QUESTION_NUMANS_START));

$mform->addElement('header', 'overallfeedbackhdr', get_string('overallfeedback', 'qtype_multichoice'));

foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
$mform->addElement('editor', $feedbackname, get_string($feedbackname, 'qtype_multichoice'),
array('rows' => 10), $this->editoroptions);
$mform->setType($feedbackname, PARAM_RAW);
}
$this->add_combined_feedback_fields(true);
$mform->disabledIf('shownumcorrect', 'single', 'eq', 1);

$this->add_interactive_settings(true, true);
}

function data_preprocessing($question) {
if (isset($question->options)){
$answers = $question->options->answers;
if (count($answers)) {
$key = 0;
foreach ($answers as $answer){
$default_values['answer['.$key.']'] = $answer->answer;
$default_values['fraction['.$key.']'] = $answer->fraction;

// prepare question text
$draftid = file_get_submitted_draft_itemid('feedback['.$key.']');
$default_values['feedback['.$key.']'] = array();
$default_values['feedback['.$key.']']['text'] = file_prepare_draft_area($draftid, $this->context->id, 'question', 'answerfeedback', empty($answer->id)?null:(int)$answer->id, $this->fileoptions, $answer->feedback);
$default_values['feedback['.$key.']']['format'] = $answer->feedbackformat;
$default_values['feedback['.$key.']']['itemid'] = $draftid;
$key++;
}
}
$default_values['single'] = $question->options->single;
$default_values['answernumbering'] = $question->options->answernumbering;
$default_values['shuffleanswers'] = $question->options->shuffleanswers;

// prepare feedback editor to display files in draft area
foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
$draftid = file_get_submitted_draft_itemid($feedbackname);
$text = $question->options->$feedbackname;
$feedbackformat = $feedbackname . 'format';
$format = $question->options->$feedbackformat;
$default_values[$feedbackname] = array();
$default_values[$feedbackname]['text'] = file_prepare_draft_area(
$draftid, // draftid
$this->context->id, // context
'qtype_multichoice', // component
$feedbackname, // filarea
!empty($question->id)?(int)$question->id:null, // itemid
$this->fileoptions, // options
$text // text
);
$default_values[$feedbackname]['format'] = $format;
$default_values[$feedbackname]['itemid'] = $draftid;
}
// prepare files code block ends

$question = (object)((array)$question + $default_values);
$question = parent::data_preprocessing($question);
$question = $this->data_preprocessing_answers($question, true);
$question = $this->data_preprocessing_combined_feedback($question, true);
$question = $this->data_preprocessing_hints($question);

if (!empty($question->options)) {
$question->single = $question->options->single;
$question->shuffleanswers = $question->options->shuffleanswers;
$question->answernumbering = $question->options->answernumbering;
}
return $question;
}

function qtype() {
return 'multichoice';
return $question;
}

function validation($data, $files) {
public function validation($data, $files) {
$errors = parent::validation($data, $files);
$answers = $data['answer'];
$answercount = 0;

$totalfraction = 0;
$maxfraction = -1;

foreach ($answers as $key => $answer){
foreach ($answers as $key => $answer) {
//check no of choices
$trimmedanswer = trim($answer);
if (!empty($trimmedanswer)){
$answercount++;
if (empty($trimmedanswer)) {
continue;
}

$answercount++;

//check grades
if ($answer != '') {
if ($data['fraction'][$key] > 0) {
$totalfraction += $data['fraction'][$key];
}
if ($data['fraction'][$key] > $maxfraction) {
$maxfraction = $data['fraction'][$key];
}
if ($data['fraction'][$key] > 0) {
$totalfraction += $data['fraction'][$key];
}
if ($data['fraction'][$key] > $maxfraction) {
$maxfraction = $data['fraction'][$key];
}
}

if ($answercount==0){
if ($answercount == 0) {
$errors['answer[0]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
$errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);
} elseif ($answercount==1){
} elseif ($answercount == 1) {
$errors['answer[1]'] = get_string('notenoughanswers', 'qtype_multichoice', 2);

}
Expand All @@ -172,4 +127,8 @@ function validation($data, $files) {
}
return $errors;
}

public function qtype() {
return 'multichoice';
}
}
16 changes: 12 additions & 4 deletions question/type/multichoice/lang/en/qtype_multichoice.php
Expand Up @@ -27,13 +27,18 @@
$string['addmorechoiceblanks'] = 'Blanks for {no} more choices';
$string['answerhowmany'] = 'One or multiple answers?';
$string['answernumbering'] = 'Number the choices?';
$string['answernumbering123'] = '1., 2., 3., ...';
$string['answernumberingabc'] = 'a., b., c., ...';
$string['answernumberingABCD'] = 'A., B., C., ...';
$string['answernumberingiii'] = 'i., ii., iii., ...';
$string['answernumberingIIII'] = 'I., II., III., ...';
$string['answernumberingnone'] = 'No numbering';
$string['answernumbering123'] = '1., 2., 3., ...';
$string['answersingleno'] = 'Multiple answers allowed';
$string['answersingleyes'] = 'One answer only';
$string['choiceno'] = 'Choice {$a}';
$string['choices'] = 'Available choices';
$string['clozeaid'] = 'Enter missing word';
$string['correctansweris'] = 'The correct answer is: {$a}.';
$string['correctfeedback'] = 'For any correct response';
$string['editingmultichoice'] = 'Editing a Multiple choice question';
$string['errfractionsaddwrong'] = 'The positive grades you have chosen do not add up to 100%<br />Instead, they add up to {$a}%';
Expand All @@ -42,19 +47,22 @@
$string['fillouttwochoices'] = 'You must fill out at least two choices. Choices left blank will not be used.';
$string['fractionsaddwrong'] = 'The positive grades you have chosen do not add up to 100%<br />Instead, they add up to {$a}%<br />Do you want to go back and fix this question?';
$string['fractionsnomax'] = 'One of the choices should be 100%, so that it is<br />possible to get a full grade for this question.<br />Do you want to go back and fix this question?';
$string['choiceno'] = 'Choice {$a}';
$string['choices'] = 'Available choices';
$string['incorrectfeedback'] = 'For any incorrect response';
$string['multichoice'] = 'Multiple choice';
$string['multichoice_help'] = 'In response to a question (that may include a image) the respondent chooses from multiple answers. There are two types of multiple choice questions - one answer and multiple answer.';
$string['multichoice_link'] = 'question/type/multichoice';
$string['multichoicesummary'] = 'Allows the selection of a single or multiple responses from a pre-defined list.';
$string['notenoughanswers'] = 'This type of question requires at least {$a} choices';
$string['overallcorrectfeedback'] = 'Feedback for any correct response';
$string['overallfeedback'] = 'Overall Feedback';
$string['overallfeedback'] = 'Overall feedback';
$string['overallincorrectfeedback'] = 'Feedback for any incorrect response';
$string['overallpartiallycorrectfeedback'] = 'Feedback for any partially correct response';
$string['partiallycorrectfeedback'] = 'For any partially correct response';
$string['pleaseselectananswer'] = 'Please select an answer.';
$string['pleaseselectatleastoneanswer'] = 'Please select at least one answer.';
$string['selectmulti'] = 'Select one or more:';
$string['selectone'] = 'Select one:';
$string['shuffleanswers'] = 'Shuffle the choices?';
$string['shuffleanswers_help'] = 'If enabled, the order of the answers is randomly shuffled for each attempt, provided that "Shuffle within questions" in the quiz settings is also enabled.';
$string['singleanswer'] = 'Choose one answer.';
$string['toomanyselected'] = 'You have selected too many options.';

0 comments on commit c9c989a

Please sign in to comment.