Skip to content

Commit

Permalink
MDL-47494 gapselect: ddwtos and gapselect qtypes now very nearly work.
Browse files Browse the repository at this point in the history
Todo rendering questions with files, and ddwtos javascript.
  • Loading branch information
timhunt committed Feb 1, 2011
1 parent 7540fcd commit 721ef2e
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 130 deletions.
11 changes: 7 additions & 4 deletions question/type/gapselect/db/install.xml
Expand Up @@ -9,10 +9,13 @@
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" NEXT="questionid"/>
<FIELD NAME="questionid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="shuffleanswers"/>
<FIELD NAME="shuffleanswers" TYPE="int" LENGTH="4" NOTNULL="true" UNSIGNED="true" DEFAULT="1" SEQUENCE="false" PREVIOUS="questionid" NEXT="correctfeedback"/>
<FIELD NAME="correctfeedback" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" PREVIOUS="shuffleanswers" NEXT="partiallycorrectfeedback"/>
<FIELD NAME="partiallycorrectfeedback" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" PREVIOUS="correctfeedback" NEXT="incorrectfeedback"/>
<FIELD NAME="incorrectfeedback" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" PREVIOUS="partiallycorrectfeedback" NEXT="shownumcorrect"/>
<FIELD NAME="shownumcorrect" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="incorrectfeedback"/>
<FIELD NAME="correctfeedback" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" COMMENT="Feedback shown for any correct response." PREVIOUS="shuffleanswers" NEXT="correctfeedbackformat"/>
<FIELD NAME="correctfeedbackformat" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="false" DEFAULT="0" SEQUENCE="false" PREVIOUS="correctfeedback" NEXT="partiallycorrectfeedback"/>
<FIELD NAME="partiallycorrectfeedback" TYPE="text" LENGTH="small" NOTNULL="true" SEQUENCE="false" COMMENT="Feedback shown for any partially correct response." PREVIOUS="correctfeedbackformat" NEXT="partiallycorrectfeedbackformat"/>
<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="shownumcorrect"/>
<FIELD NAME="shownumcorrect" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="incorrectfeedbackformat"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="questionid"/>
Expand Down
89 changes: 49 additions & 40 deletions question/type/gapselect/edit_form_base.php
Expand Up @@ -8,6 +8,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_gapselect_edit_form_base extends question_edit_form {
const MAX_GROUPS = 8;

/** @var array of HTML tags allowed in choices / drag boxes. */
protected $allowedhtmltags = array(
Expand Down Expand Up @@ -64,7 +65,7 @@ private function get_list_of_printable_allowed_tags($allowedhtmltags) {
* definition_inner adds all specific fields to the form.
* @param object $mform (the form being built).
*/
function definition_inner(&$mform) {
function definition_inner($mform) {
global $CFG;

//add the answer (choice) fields to the form
Expand All @@ -75,19 +76,14 @@ function definition_inner(&$mform) {
}

protected function definition_answer_choice(&$mform) {
$mform->addElement('header', 'choicehdr', get_string('choices', 'qtype_gapselect'));
$mform->addElement('header', 'choicehdr', get_string('choices', 'qtype_gapselect'));

$mform->addElement('checkbox', 'shuffleanswers', get_string('shuffle', 'quiz'));
$mform->addElement('checkbox', 'shuffleanswers', get_string('shuffle', 'qtype_gapselect'));
$mform->setDefault('shuffleanswers', 0);

$textboxgroup = array();

$grouparray = array();
$grouparray[] =& $mform->createElement('text', 'answer', get_string('answer', 'qtype_gapselect'), array('size'=>30, 'class'=>'tweakcss'));
$grouparray[] =& $mform->createElement('static', '', '',' '.get_string('group', 'qtype_gapselect').' ');

$grouparray = $this->choice_group($mform, $grouparray);
$textboxgroup[] = $mform->createElement('group','choices', 'Choice {no}',$grouparray);
$textboxgroup[] = $mform->createElement('group', 'choices',
get_string('choicex', 'qtype_gapselect'), $this->choice_group($mform));

if (isset($this->question->options)) {
$countanswers = count($this->question->options->answers);
Expand All @@ -96,7 +92,7 @@ protected function definition_answer_choice(&$mform) {
}

if ($this->question->formoptions->repeatelements) {
$defaultstartnumbers = QUESTION_NUMANS_START*2;
$defaultstartnumbers = QUESTION_NUMANS_START * 2;
$repeatsatstart = max($defaultstartnumbers, QUESTION_NUMANS_START, $countanswers + QUESTION_NUMANS_ADD);
} else {
$repeatsatstart = $countanswers;
Expand All @@ -107,29 +103,51 @@ protected function definition_answer_choice(&$mform) {
$this->repeat_elements($textboxgroup, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmorechoiceblanks', 'qtype_gapselect'));
}

protected function choice_group($mform) {
$options = array();
for ($i = 1; $i <= self::MAX_GROUPS; $i += 1) {
$options[$i] = $i;
}
$grouparray = array();
$grouparray[] = $mform->createElement('text', 'answer', get_string('answer', 'qtype_gapselect'), array('size'=>30, 'class'=>'tweakcss'));
$grouparray[] = $mform->createElement('static', '', '',' '.get_string('group', 'qtype_gapselect').' ');
$grouparray[] = $mform->createElement('select', 'choicegroup', get_string('group', 'qtype_gapselect'), $options);
return $grouparray;
}

protected function repeated_options() {
$repeatedoptions = array();
$repeatedoptions['choicegroup']['default'] = '1';
return $repeatedoptions;
}

public function set_data($question) {
if (isset($question->options)) {
$options = $question->options;
$default_values = array();
if (count($options->answers)) {
$key = 0;
foreach ($options->answers as $answer) {
$default_values['choices['.$key.'][answer]'] = $answer->answer;
$default_values += $this->default_values_from_feedback_field($answer->feedback, $key);
$key++;
}
public function data_preprocessing($question) {
$question = parent::data_preprocessing($question);
$question = $this->data_preprocessing_combined_feedback($question, true);
$question = $this->data_preprocessing_hints($question, true, true);

$question = $this->data_preprocessing_answers($question, true);
if (!empty($question->options->answers)) {
$key = 0;
foreach ($question->options->answers as $answer) {
$question = $this->data_preprocessing_choice($question, $answer, $key);
$key++;
}
}

$default_values['shuffleanswers'] = $question->options->shuffleanswers;
$default_values['correctfeedback'] = $question->options->correctfeedback;
$default_values['partiallycorrectfeedback'] = $question->options->partiallycorrectfeedback;
$default_values['incorrectfeedback'] = $question->options->incorrectfeedback;
$default_values['shownumcorrect'] = $question->options->shownumcorrect;
$question = (object)((array)$question + $default_values);
if (!empty($question->options)) {
$question->shuffleanswers = $question->options->shuffleanswers;
}
parent::set_data($question);

return $question;
}

protected function data_preprocessing_choice($question, $answer, $key) {
// See comment in data_preprocessing_answers.
unset($this->_form->_defaultValues['choices[$key][choicegroup]']);
$question->choices[$key]['answer'] = $answer->answer;
$question->choices[$key]['choicegroup'] = $answer->feedback;
return $question;
}

public function validation($data, $files) {
Expand All @@ -138,7 +156,7 @@ public function validation($data, $files) {
$choices = $data['choices'];

//check the whether the slots are valid
$errorsinquestiontext = $this->validate_slots($questiontext, $choices);
$errorsinquestiontext = $this->validate_slots($questiontext['text'], $choices);
if ($errorsinquestiontext) {
$errors['questiontext'] = $errorsinquestiontext;
}
Expand Down Expand Up @@ -196,17 +214,8 @@ private function validate_slots($questiontext, $choices) {
}
return false;
}

function qtype() {
return '';
}

protected function default_values_from_feedback_field($feedback, $key) {
$default_values = array();
return $default_values;
}

protected function repeated_options() {
$repeatedoptions = array();
return $repeatedoptions;
}
}
21 changes: 0 additions & 21 deletions question/type/gapselect/edit_gapselect_form.php
Expand Up @@ -35,31 +35,10 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class qtype_gapselect_edit_form extends qtype_gapselect_edit_form_base {

// HTML tags allowed in answers (choices).
protected $allowedhtmltags = array();

function qtype() {
return 'gapselect';
}

protected function default_values_from_feedback_field($feedback, $key) {
$default_values = array();
$default_values['choices['.$key.'][selectgroup]'] = $feedback;
return $default_values;
}

protected function repeated_options() {
$repeatedoptions = array();
$repeatedoptions['selectgroup']['default'] = '1';
return $repeatedoptions;
}
protected function choice_group(&$mform, $grouparray) {
$options = array();
for ($i = 1; $i <= 8; $i += 1) {
$options[$i] = $i;
}
$grouparray[] =& $mform->createElement('select', 'selectgroup', get_string('group', 'qtype_gapselect'), $options);
return $grouparray;
}
}
4 changes: 3 additions & 1 deletion question/type/gapselect/lang/en/qtype_gapselect.php
Expand Up @@ -30,10 +30,12 @@
$string['addmorechoiceblanks'] = 'Blanks for {no} more choices';
$string['answer'] = 'Answer';
$string['choices'] = 'Choices';
$string['choicex'] = 'Choice {no}';
$string['correctansweris'] = 'The correct answer is: {$a}';
$string['editinggapselect'] = 'Editing a select missing words question';
$string['gapselect'] = 'Select missing words';
$string['gapselect_help'] = 'Type in some question text like "The [[1]] jumped over the [[2]]", then enter the possible words to go in gaps 1 and 2 underneath.';
$string['gapselectsummary'] = 'Missing words in some text are filled in using dropdown menus.';
$string['editinggapselect'] = 'Editing a select missing words question';
$string['group'] = 'Group';
$string['pleaseputananswerineachbox'] = 'Please put an answer in each box.';
$string['shuffle'] = 'Shuffle';
File renamed without changes
2 changes: 1 addition & 1 deletion question/type/gapselect/questiontype.php
Expand Up @@ -40,7 +40,7 @@
*/
class qtype_gapselect extends qtype_gapselect_base {
protected function choice_options_to_feedback($choice) {
return $choice['selectgroup'];
return $choice['choicegroup'];
}

protected function make_choice($choicedata) {
Expand Down

0 comments on commit 721ef2e

Please sign in to comment.