Skip to content

Commit

Permalink
Multichoice answer numbering
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Michel Vedrine committed Jun 4, 2018
1 parent bb773a5 commit fa7fe01
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 48 deletions.
1 change: 1 addition & 0 deletions backup/moodle1/lib.php
Expand Up @@ -111,6 +111,7 @@ public function process_question(array $data, array $raw) {
$this->xmlwriter->full_tag('incorrectfeedback', '');
$this->xmlwriter->full_tag('incorrectfeedbackformat', FORMAT_HTML);
$this->xmlwriter->full_tag('shownumcorrect', 0);
$this->xmlwriter->full_tag('answernumbering', 'abc');
$this->xmlwriter->end_tag('formulas');
}
}
3 changes: 2 additions & 1 deletion backup/moodle2/backup_qtype_formulas_plugin.class.php
Expand Up @@ -52,7 +52,8 @@ protected function define_question_plugin_structure() {
'varsrandom', 'varsglobal',
'correctfeedback', 'correctfeedbackformat',
'partiallycorrectfeedback', 'partiallycorrectfeedbackformat',
'incorrectfeedback', 'incorrectfeedbackformat', 'shownumcorrect'));
'incorrectfeedback', 'incorrectfeedbackformat', 'shownumcorrect',
'answernumbering'));

$formulasanswers = new backup_nested_element('formulas_answers');
$formulasanswer = new backup_nested_element('formulas_answer', array('id'), array(
Expand Down
3 changes: 3 additions & 0 deletions backup/moodle2/restore_qtype_formulas_plugin.class.php
Expand Up @@ -82,6 +82,9 @@ public function process_formulas($data) {
if (!isset($data->shownumcorrect)) {
$data->shownumcorrect = 0;
}
if (!isset($data->answernumbering)) {
$data->answernumbering = 'abc';
}
// Adjust some columns.
$data->questionid = $newquestionid;
// Insert record.
Expand Down
85 changes: 43 additions & 42 deletions db/install.xml
Expand Up @@ -4,56 +4,57 @@
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="qtype_formulas_options" COMMENT="Options for formulas question type" NEXT="qtype_formulas_answers">
<TABLE NAME="qtype_formulas_options" COMMENT="Options for formulas question type">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="questionid"/>
<FIELD NAME="questionid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="varsrandom"/>
<FIELD NAME="varsrandom" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="questionid" NEXT="varsglobal"/>
<FIELD NAME="varsglobal" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="varsrandom" NEXT="correctfeedback"/>
<FIELD NAME="correctfeedback" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Feedback shown for any correct response." PREVIOUS="varsglobal" NEXT="correctfeedbackformat"/>
<FIELD NAME="correctfeedbackformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="correctfeedback" NEXT="partiallycorrectfeedback"/>
<FIELD NAME="partiallycorrectfeedback" TYPE="text" 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" DEFAULT="0" SEQUENCE="false" PREVIOUS="partiallycorrectfeedback" NEXT="incorrectfeedback"/>
<FIELD NAME="incorrectfeedback" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Feedback shown for any incorrect response." PREVIOUS="partiallycorrectfeedbackformat" NEXT="incorrectfeedbackformat"/>
<FIELD NAME="incorrectfeedbackformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="incorrectfeedback" NEXT="shownumcorrect"/>
<FIELD NAME="shownumcorrect" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="If true, then when the user gets a question partially correct, tell them how many parts they got correct alongside the feedback." PREVIOUS="incorrectfeedbackformat"/>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="questionid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="varsrandom" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="varsglobal" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="correctfeedback" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Feedback shown for any correct response."/>
<FIELD NAME="correctfeedbackformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="partiallycorrectfeedback" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Feedback shown for any partially correct response."/>
<FIELD NAME="partiallycorrectfeedbackformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="incorrectfeedback" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="Feedback shown for any incorrect response."/>
<FIELD NAME="incorrectfeedbackformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="shownumcorrect" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="If true, then when the user gets a question partially correct, tell them how many parts they got correct alongside the feedback."/>
<FIELD NAME="answernumbering" TYPE="char" LENGTH="10" NOTNULL="true" DEFAULT="abc" SEQUENCE="false" COMMENT="Indicates how and whether the choices should be numbered."/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="questionid"/>
<KEY NAME="questionid" TYPE="foreign" FIELDS="questionid" REFTABLE="question" REFFIELDS="id" PREVIOUS="primary"/>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="questionid" TYPE="foreign" FIELDS="questionid" REFTABLE="question" REFFIELDS="id"/>
</KEYS>
</TABLE>
<TABLE NAME="qtype_formulas_answers" COMMENT="Answers for formulas question type" PREVIOUS="qtype_formulas_options">
<TABLE NAME="qtype_formulas_answers" COMMENT="Answers for formulas question type">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true" NEXT="questionid"/>
<FIELD NAME="questionid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="id" NEXT="partindex"/>
<FIELD NAME="partindex" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="index of part in question" PREVIOUS="questionid" NEXT="placeholder"/>
<FIELD NAME="placeholder" TYPE="char" LENGTH="40" NOTNULL="false" SEQUENCE="false" PREVIOUS="partindex" NEXT="answermark"/>
<FIELD NAME="answermark" TYPE="float" NOTNULL="true" SEQUENCE="false" PREVIOUS="placeholder" NEXT="answertype"/>
<FIELD NAME="answertype" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="answermark" NEXT="numbox"/>
<FIELD NAME="numbox" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" PREVIOUS="answertype" NEXT="vars1"/>
<FIELD NAME="vars1" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="numbox" NEXT="answer"/>
<FIELD NAME="answer" TYPE="text" NOTNULL="true" SEQUENCE="false" PREVIOUS="vars1" NEXT="vars2"/>
<FIELD NAME="vars2" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="answer" NEXT="correctness"/>
<FIELD NAME="correctness" TYPE="text" NOTNULL="true" SEQUENCE="false" PREVIOUS="vars2" NEXT="unitpenalty"/>
<FIELD NAME="unitpenalty" TYPE="float" NOTNULL="false" SEQUENCE="false" PREVIOUS="correctness" NEXT="postunit"/>
<FIELD NAME="postunit" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="unitpenalty" NEXT="ruleid"/>
<FIELD NAME="ruleid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" PREVIOUS="postunit" NEXT="otherrule"/>
<FIELD NAME="otherrule" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="ruleid" NEXT="subqtext"/>
<FIELD NAME="subqtext" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="otherrule" NEXT="subqtextformat"/>
<FIELD NAME="subqtextformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="subqtext" NEXT="feedback"/>
<FIELD NAME="feedback" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="subqtextformat" NEXT="feedbackformat"/>
<FIELD NAME="feedbackformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="feedback" NEXT="partcorrectfb"/>
<FIELD NAME="partcorrectfb" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="feedbackformat" NEXT="partcorrectfbformat"/>
<FIELD NAME="partcorrectfbformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="partcorrectfb" NEXT="partpartiallycorrectfb"/>
<FIELD NAME="partpartiallycorrectfb" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="partcorrectfbformat" NEXT="partpartiallycorrectfbformat"/>
<FIELD NAME="partpartiallycorrectfbformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="partpartiallycorrectfb" NEXT="partincorrectfb"/>
<FIELD NAME="partincorrectfb" TYPE="text" NOTNULL="false" SEQUENCE="false" PREVIOUS="partpartiallycorrectfbformat" NEXT="partincorrectfbformat"/>
<FIELD NAME="partincorrectfbformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="partincorrectfb"/>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="questionid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="partindex" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="index of part in question"/>
<FIELD NAME="placeholder" TYPE="char" LENGTH="40" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="answermark" TYPE="float" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="answertype" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="numbox" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="vars1" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="answer" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="vars2" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="correctness" TYPE="text" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="unitpenalty" TYPE="float" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="postunit" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="ruleid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="otherrule" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="subqtext" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="subqtextformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="feedback" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="feedbackformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="partcorrectfb" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="partcorrectfbformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="partpartiallycorrectfb" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="partpartiallycorrectfbformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="partincorrectfb" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="partincorrectfbformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id" NEXT="questionid"/>
<KEY NAME="questionid" TYPE="foreign" FIELDS="questionid" REFTABLE="question" REFFIELDS="id" PREVIOUS="primary"/>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="questionid" TYPE="foreign" FIELDS="questionid" REFTABLE="question" REFFIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
Expand Down
15 changes: 15 additions & 0 deletions db/upgrade.php
Expand Up @@ -344,5 +344,20 @@ function xmldb_qtype_formulas_upgrade($oldversion=0) {
$DB->set_field('qtype_formulas_answers', 'feedback', '');
upgrade_plugin_savepoint(true, 2018042801, 'qtype', 'formulas');
}

if ($oldversion < 2018060400) {

// Define field answernumbering to be added to qtype_formulas_options.
$table = new xmldb_table('qtype_formulas_options');
$field = new xmldb_field('answernumbering', XMLDB_TYPE_CHAR, '10', null, XMLDB_NOTNULL, null, 'abc', 'shownumcorrect');

// Conditionally launch add field answernumbering.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Formulas savepoint reached.
upgrade_plugin_savepoint(true, 2018060400, 'qtype', 'formulas');
}
return true;
}
4 changes: 4 additions & 0 deletions edit_formulas_form.php
Expand Up @@ -68,6 +68,10 @@ protected function definition_inner($mform) {

$mform->insertElementBefore($mform->createElement('header', 'mainq', get_string('mainq', 'qtype_formulas'),
''), 'questiontext');
$numberingoptions = question_bank::get_qtype('multichoice')->get_numbering_styles();
$mform->addElement('select', 'answernumbering',
get_string('answernumbering', 'qtype_multichoice'), $numberingoptions);
$mform->setDefault('answernumbering', 'abc');
// Part's answers.
$this->add_per_answer_fields($mform, get_string('answerno', 'qtype_formulas', '{no}'),
question_bank::fraction_options(), 1, 2);
Expand Down
2 changes: 1 addition & 1 deletion questiontype.php
Expand Up @@ -75,7 +75,7 @@ public function questionid_column_name() {
* @return mixed array as above, or null to tell the base class to do nothing.
*/
public function extra_question_fields() {
return array('qtype_formulas_options', 'varsrandom', 'varsglobal');
return array('qtype_formulas_options', 'varsrandom', 'varsglobal', 'answernumbering');
}

/**
Expand Down
34 changes: 33 additions & 1 deletion renderer.php
Expand Up @@ -165,6 +165,37 @@ public function get_part_image_and_class($qa, $options, $part) {
return $sub;
}

/**
* @param int $num The number, starting at 0.
* @param string $style The style to render the number in. One of the
* options returned by {@link qtype_multichoice:;get_numbering_styles()}.
* @return string the number $num in the requested style.
*/
protected function number_in_style($num, $style) {
switch($style) {
case 'abc':
$number = chr(ord('a') + $num);
break;
case 'ABCD':
$number = chr(ord('A') + $num);
break;
case '123':
$number = $num + 1;
break;
case 'iii':
$number = question_utils::int_to_roman($num + 1);
break;
case 'IIII':
$number = strtoupper(question_utils::int_to_roman($num + 1));
break;
case 'none':
return '';
default:
return 'ERR';
}
return $number . '. ';
}

// Return the part's text with variables replaced by their values.
public function get_part_formulation(question_attempt $qa, question_display_options $options, $i, $vars, $sub) {
$question = $qa->get_question();
Expand Down Expand Up @@ -274,6 +305,7 @@ public function get_part_formulation(question_attempt $qa, question_display_opti
}
$output = $this->all_choices_wrapper_start();
foreach ($stexts->value as $x => $mctxt) {
$mctxt = html_writer::span($this->number_in_style($x, $question->answernumbering), 'answernumber') . $mctxt;
$inputattributes['id'] = $inputname.'_'.$x;
$inputattributes['value'] = $x;
$isselected = ($currentanswer != '' && $x == $currentanswer);
Expand All @@ -289,7 +321,7 @@ public function get_part_formulation(question_attempt $qa, question_display_opti
$output .= $this->choice_wrapper_start($class);
$output .= html_writer::empty_tag('input', $inputattributes);
$output .= html_writer::tag('label', $mctxt,
array('for' => $inputattributes['id']));
array('for' => $inputattributes['id'], 'class' => 'm-l-1'));
$output .= $this->choice_wrapper_end();
}
$output .= $this->all_choices_wrapper_end();
Expand Down
2 changes: 1 addition & 1 deletion styles.css
Expand Up @@ -102,7 +102,7 @@
.que.formulas .multichoice_answer div.r0,
.que.formulas .multichoice_answer div.r1 {
padding: 0.3em 0 0.3em 25px;
text-indent: -10px;
text-indent: -25px;
}


Expand Down
4 changes: 2 additions & 2 deletions version.php
Expand Up @@ -25,14 +25,14 @@
defined('MOODLE_INTERNAL') || die();

$plugin->component = 'qtype_formulas';
$plugin->version = 2018052101;
$plugin->version = 2018060400;

$plugin->cron = 0;
$plugin->requires = 2015111600;
$plugin->dependencies = array(
'qbehaviour_adaptive' => 2015111600,
'qbehaviour_adaptivemultipart' => 2014092500,
);
$plugin->release = '4.62 for Moodle 3.0 to 3.5';
$plugin->release = '4.7 for Moodle 3.0 to 3.5';

$plugin->maturity = MATURITY_STABLE;

0 comments on commit fa7fe01

Please sign in to comment.