question editing: MDL-18035 Nice refactoring from Oleg Sychev that saves 50+ lines of code between the various editing forms.
This commit is contained in:
@@ -19,6 +19,28 @@ class question_edit_calculated_form extends question_edit_form {
|
||||
* @var question_calculated_qtype
|
||||
*/
|
||||
var $qtypeobj;
|
||||
|
||||
function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
|
||||
$repeated = parent::get_per_answer_fields(&$mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
|
||||
$mform->setType('answer', PARAM_NOTAGS);
|
||||
|
||||
$addrepeated = array();
|
||||
$addrepeated[] =& $mform->createElement('text', 'tolerance', get_string('tolerance', 'qtype_calculated'));
|
||||
$repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
|
||||
$repeatedoptions['tolerance']['default'] = 0.01;
|
||||
$addrepeated[] =& $mform->createElement('select', 'tolerancetype', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
|
||||
|
||||
$addrepeated[] =& $mform->createElement('select', 'correctanswerlength', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
|
||||
$repeatedoptions['correctanswerlength']['default'] = 2;
|
||||
|
||||
$answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
|
||||
$addrepeated[] =& $mform->createElement('select', 'correctanswerformat', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
|
||||
array_splice($repeated, 3, 0, $addrepeated);
|
||||
$repeated[1]->setLabel(get_string('correctanswerformula', 'quiz').'=');
|
||||
|
||||
return $repeated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add question-type specific form fields.
|
||||
*
|
||||
@@ -37,43 +59,9 @@ class question_edit_calculated_form extends question_edit_form {
|
||||
|
||||
$mform->insertElementBefore( $mform->createElement('submit', $addfieldsname, $addstring),'listcategory');
|
||||
|
||||
$repeated = array();
|
||||
$repeated[] =& $mform->createElement('header', 'answerhdr', get_string('answerhdr', 'qtype_calculated', '{no}'));
|
||||
|
||||
$repeated[] =& $mform->createElement('text', 'answer', get_string('correctanswerformula', 'quiz').'=', array('size' => 50));
|
||||
$repeatedoptions['answer']['type'] = PARAM_NOTAGS;
|
||||
|
||||
$creategrades = get_grade_options();
|
||||
$gradeoptions = $creategrades->gradeoptions;
|
||||
$repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
|
||||
$repeatedoptions['fraction']['default'] = 0;
|
||||
|
||||
$repeated[] =& $mform->createElement('text', 'tolerance', get_string('tolerance', 'qtype_calculated'));
|
||||
$repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
|
||||
$repeatedoptions['tolerance']['default'] = 0.01;
|
||||
$repeated[] =& $mform->createElement('select', 'tolerancetype', get_string('tolerancetype', 'quiz'), $this->qtypeobj->tolerance_types());
|
||||
|
||||
$repeated[] =& $mform->createElement('select', 'correctanswerlength', get_string('correctanswershows', 'qtype_calculated'), range(0, 9));
|
||||
$repeatedoptions['correctanswerlength']['default'] = 2;
|
||||
|
||||
$answerlengthformats = array('1' => get_string('decimalformat', 'quiz'), '2' => get_string('significantfiguresformat', 'quiz'));
|
||||
$repeated[] =& $mform->createElement('select', 'correctanswerformat', get_string('correctanswershowsformat', 'qtype_calculated'), $answerlengthformats);
|
||||
|
||||
$repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
|
||||
array('course' => $this->coursefilesid));
|
||||
$repeatedoptions['feedback']['type'] = PARAM_RAW;
|
||||
|
||||
if (isset($this->question->options)){
|
||||
$count = count($this->question->options->answers);
|
||||
} else {
|
||||
$count = 0;
|
||||
}
|
||||
if ($this->question->formoptions->repeatelements){
|
||||
$repeatsatstart = $count + 1;
|
||||
} else {
|
||||
$repeatsatstart = $count;
|
||||
}
|
||||
$this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', 1, get_string('addmoreanswerblanks', 'qtype_calculated'));
|
||||
$this->add_per_answer_fields($mform, get_string('answerhdr', 'qtype_calculated', '{no}'),
|
||||
$creategrades->gradeoptions, 1, 1);
|
||||
|
||||
$repeated = array();
|
||||
$repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}'));
|
||||
|
||||
@@ -131,6 +131,10 @@ class question_calculated_qtype extends default_questiontype {
|
||||
// Get old answers:
|
||||
global $CFG, $DB;
|
||||
|
||||
if (isset($question->answer) && !isset($question->asnwers)) {
|
||||
$question->answers = $question->answer;
|
||||
}
|
||||
|
||||
// Get old versions of the objects
|
||||
if (!$oldanswers = $DB->get_records('question_answers', array('question' => $question->id), 'id ASC')) {
|
||||
$oldanswers = array();
|
||||
@@ -139,8 +143,8 @@ class question_calculated_qtype extends default_questiontype {
|
||||
if (!$oldoptions = $DB->get_records('question_calculated', array('question' => $question->id), 'answer ASC')) {
|
||||
$oldoptions = array();
|
||||
}
|
||||
// Save the units.
|
||||
// Save units
|
||||
|
||||
// Save the units.
|
||||
$virtualqtype = $this->get_virtual_qtype();
|
||||
$result = $virtualqtype->save_numerical_units($question);
|
||||
if (isset($result->error)) {
|
||||
|
||||
@@ -248,6 +248,56 @@ class question_edit_form extends moodleform {
|
||||
// By default, do nothing.
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of form elements to repeat, one for each answer.
|
||||
* @param object $mform the form being built.
|
||||
* @param $label the label to use for each option.
|
||||
* @param $gradeoptions the possible grades for each answer.
|
||||
* @param $repeatedoptions reference to array of repeated options to fill
|
||||
* @param $answersoption reference to return the name of $question->options field holding an array of answers
|
||||
* @return array of form fields.
|
||||
*/
|
||||
function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
|
||||
$repeated = array();
|
||||
$repeated[] =& $mform->createElement('header', 'answerhdr', $label);
|
||||
$repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'), array('size' => 50));
|
||||
$repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
|
||||
$repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
|
||||
array('course' => $this->coursefilesid));
|
||||
$repeatedoptions['answer']['type'] = PARAM_RAW;
|
||||
$repeatedoptions['fraction']['default'] = 0;
|
||||
$answersoption = 'answers';
|
||||
return $repeated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a set of form fields, obtained from get_per_answer_fields, to the form,
|
||||
* one for each existing answer, with some blanks for some new ones.
|
||||
* @param object $mform the form being built.
|
||||
* @param $label the label to use for each option.
|
||||
* @param $gradeoptions the possible grades for each answer.
|
||||
* @param $minoptions the minimum number of answer blanks to display. Default QUESTION_NUMANS_START.
|
||||
* @param $addoptions the number of answer blanks to add. Default QUESTION_NUMANS_ADD.
|
||||
*/
|
||||
function add_per_answer_fields(&$mform, $label, $gradeoptions, $minoptions = QUESTION_NUMANS_START, $addoptions = QUESTION_NUMANS_ADD) {
|
||||
$answersoption = '';
|
||||
$repeatedoptions = array();
|
||||
$repeated = $this->get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
|
||||
|
||||
if (isset($this->question->options)){
|
||||
$countanswers = count($this->question->options->$answersoption);
|
||||
} else {
|
||||
$countanswers = 0;
|
||||
}
|
||||
if ($this->question->formoptions->repeatelements){
|
||||
$repeatsatstart = max($minoptions, $countanswers + $addoptions);
|
||||
} else {
|
||||
$repeatsatstart = $countanswers;
|
||||
}
|
||||
|
||||
$this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', $addoptions, get_string('addmorechoiceblanks', 'qtype_multichoice'));
|
||||
}
|
||||
|
||||
function set_data($question) {
|
||||
global $QTYPES;
|
||||
if (empty($question->image)){
|
||||
|
||||
@@ -13,6 +13,18 @@
|
||||
* match editing form definition.
|
||||
*/
|
||||
class question_edit_match_form extends question_edit_form {
|
||||
|
||||
function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
|
||||
$repeated = array();
|
||||
$repeated[] =& $mform->createElement('header', 'answerhdr', $label);
|
||||
$repeated[] =& $mform->createElement('textarea', 'subquestions', get_string('question', 'quiz'), array('cols'=>40, 'rows'=>3));
|
||||
$repeated[] =& $mform->createElement('text', 'subanswers', get_string('answer', 'quiz'), array('size'=>50));
|
||||
$repeatedoptions['subquestions']['type'] = PARAM_RAW;
|
||||
$repeatedoptions['subanswers']['type'] = PARAM_TEXT;
|
||||
$answersoption = 'subquestions';
|
||||
return $repeated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add question-type specific form fields.
|
||||
*
|
||||
@@ -26,27 +38,7 @@ class question_edit_match_form extends question_edit_form {
|
||||
$mform->addElement('static', 'answersinstruct', get_string('choices', 'quiz'), get_string('filloutthreeqsandtwoas', 'qtype_match'));
|
||||
$mform->closeHeaderBefore('answersinstruct');
|
||||
|
||||
$repeated = array();
|
||||
$repeated[] =& $mform->createElement('header', 'choicehdr', get_string('questionno', 'quiz', '{no}'));
|
||||
$repeated[] =& $mform->createElement('textarea', 'subquestions', get_string('question', 'quiz'), array('cols'=>40, 'rows'=>3));
|
||||
$repeated[] =& $mform->createElement('text', 'subanswers', get_string('answer', 'quiz'), array('size'=>50));
|
||||
|
||||
if (isset($this->question->options)){
|
||||
$countsubquestions = count($this->question->options->subquestions);
|
||||
} else {
|
||||
$countsubquestions = 0;
|
||||
}
|
||||
if ($this->question->formoptions->repeatelements){
|
||||
$repeatsatstart = (QUESTION_NUMANS_START > ($countsubquestions + QUESTION_NUMANS_ADD))?
|
||||
QUESTION_NUMANS_START : ($countsubquestions + QUESTION_NUMANS_ADD);
|
||||
} else {
|
||||
$repeatsatstart = $countsubquestions;
|
||||
}
|
||||
$mform->setType('subanswer', PARAM_TEXT);
|
||||
$mform->setType('subquestion', PARAM_TEXT);
|
||||
|
||||
$this->repeat_elements($repeated, $repeatsatstart, array(), 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmoreqblanks', 'qtype_match'));
|
||||
|
||||
$this->add_per_answer_fields($mform, get_string('questionno', 'quiz', '{no}'), 0);
|
||||
}
|
||||
|
||||
function set_data($question) {
|
||||
|
||||
@@ -20,29 +20,8 @@ class question_edit_missingtype_form extends question_edit_form {
|
||||
*/
|
||||
function definition_inner(&$mform) {
|
||||
$creategrades = get_grade_options();
|
||||
$gradeoptions = $creategrades->gradeoptionsfull;
|
||||
$repeated = array();
|
||||
$repeated[] =& $mform->createElement('header', 'choicehdr', get_string('choiceno', 'qtype_multichoice', '{no}'));
|
||||
$repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'));
|
||||
$repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
|
||||
$repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
|
||||
array('course' => $this->coursefilesid));
|
||||
|
||||
if (isset($this->question->options)){
|
||||
$countanswers = count($this->question->options->answers);
|
||||
} else {
|
||||
$countanswers = 0;
|
||||
}
|
||||
if ($this->question->formoptions->repeatelements){
|
||||
$repeatsatstart = (QUESTION_NUMANS_START > ($countanswers + QUESTION_NUMANS_ADD))?
|
||||
QUESTION_NUMANS_START : ($countanswers + QUESTION_NUMANS_ADD);
|
||||
} else {
|
||||
$repeatsatstart = $countanswers;
|
||||
}
|
||||
$repeatedoptions = array();
|
||||
$repeatedoptions['fraction']['default'] = 0;
|
||||
$mform->setType('answer', PARAM_NOTAGS);
|
||||
$this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmorechoiceblanks', 'qtype_multichoice'));
|
||||
$this->add_per_answer_fields($mform, get_string('choiceno', 'qtype_multichoice', '{no}'),
|
||||
$creategrades->gradeoptionsfull);
|
||||
}
|
||||
|
||||
function set_data($question) {
|
||||
|
||||
@@ -41,42 +41,16 @@ class question_edit_multichoice_form extends question_edit_form {
|
||||
$mform->closeHeaderBefore('answersinstruct');
|
||||
*/
|
||||
$creategrades = get_grade_options();
|
||||
$gradeoptions = $creategrades->gradeoptionsfull;
|
||||
$repeated = array();
|
||||
$repeated[] =& $mform->createElement('header', 'choicehdr', get_string('choiceno', 'qtype_multichoice', '{no}'));
|
||||
$repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'), array('size' => 50));
|
||||
$repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
|
||||
$repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
|
||||
array('course' => $this->coursefilesid));
|
||||
|
||||
if (isset($this->question->options)){
|
||||
$countanswers = count($this->question->options->answers);
|
||||
} else {
|
||||
$countanswers = 0;
|
||||
}
|
||||
if ($this->question->formoptions->repeatelements){
|
||||
$repeatsatstart = max(5, QUESTION_NUMANS_START, $countanswers + QUESTION_NUMANS_ADD);
|
||||
} else {
|
||||
$repeatsatstart = $countanswers;
|
||||
}
|
||||
$repeatedoptions = array();
|
||||
$repeatedoptions['fraction']['default'] = 0;
|
||||
$mform->setType('answer', PARAM_RAW);
|
||||
$this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmorechoiceblanks', 'qtype_multichoice'));
|
||||
$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'));
|
||||
|
||||
$mform->addElement('htmleditor', 'correctfeedback', get_string('correctfeedback', 'qtype_multichoice'),
|
||||
foreach (array('correctfeedback', 'partiallycorrectfeedback', 'incorrectfeedback') as $feedbackname) {
|
||||
$mform->addElement('htmleditor', $feedbackname, get_string($feedbackname, 'qtype_multichoice'),
|
||||
array('course' => $this->coursefilesid));
|
||||
$mform->setType('correctfeedback', PARAM_RAW);
|
||||
|
||||
$mform->addElement('htmleditor', 'partiallycorrectfeedback', get_string('partiallycorrectfeedback', 'qtype_multichoice'),
|
||||
array('course' => $this->coursefilesid));
|
||||
$mform->setType('partiallycorrectfeedback', PARAM_RAW);
|
||||
|
||||
$mform->addElement('htmleditor', 'incorrectfeedback', get_string('incorrectfeedback', 'qtype_multichoice'),
|
||||
array('course' => $this->coursefilesid));
|
||||
$mform->setType('incorrectfeedback', PARAM_RAW);
|
||||
$mform->setType($feedbackname, PARAM_RAW);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,18 @@
|
||||
* numerical editing form definition.
|
||||
*/
|
||||
class question_edit_numerical_form extends question_edit_form {
|
||||
|
||||
function get_per_answer_fields(&$mform, $label, $gradeoptions, &$repeatedoptions, &$answersoption) {
|
||||
$repeated = parent::get_per_answer_fields($mform, $label, $gradeoptions, $repeatedoptions, $answersoption);
|
||||
|
||||
$tolerance =& $mform->createElement('text', 'tolerance', get_string('acceptederror', 'quiz'));
|
||||
$repeatedoptions['tolerance']['type'] = PARAM_NUMBER;
|
||||
array_splice($repeated, 3, 0, array($tolerance));
|
||||
$repeated[1]->setSize(10);
|
||||
|
||||
return $repeated;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add question-type specific form fields.
|
||||
*
|
||||
@@ -22,38 +34,8 @@ class question_edit_numerical_form extends question_edit_form {
|
||||
|
||||
//------------------------------------------------------------------------------------------
|
||||
$creategrades = get_grade_options();
|
||||
$gradeoptions = $creategrades->gradeoptions;
|
||||
$repeated = array();
|
||||
$repeatedoptions = array();
|
||||
$repeated[] =& $mform->createElement('header', 'answerhdr', get_string('answerno', 'qtype_numerical', '{no}'));
|
||||
|
||||
$repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'));
|
||||
$mform->setType('answer', PARAM_RAW);
|
||||
|
||||
$repeated[] =& $mform->createElement('text', 'tolerance', get_string('acceptederror', 'quiz'));
|
||||
$mform->setType('tolerance', PARAM_NUMBER);
|
||||
|
||||
$repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
|
||||
$repeatedoptions['fraction']['default'] = 0;
|
||||
|
||||
$repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
|
||||
array('course' => $this->coursefilesid));
|
||||
$mform->setType('feedback', PARAM_RAW);
|
||||
|
||||
|
||||
if (isset($this->question->options)){
|
||||
$countanswers = count($this->question->options->answers);
|
||||
} else {
|
||||
$countanswers = 0;
|
||||
}
|
||||
if ($this->question->formoptions->repeatelements){
|
||||
$repeatsatstart = (QUESTION_NUMANS_START > ($countanswers + 1))?
|
||||
QUESTION_NUMANS_START : ($countanswers + 1);
|
||||
} else {
|
||||
$repeatsatstart = $countanswers;
|
||||
}
|
||||
$this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', 2, get_string('addmoreanswerblanks', 'qtype_numerical'));
|
||||
|
||||
$this->add_per_answer_fields($mform, get_string('answerno', 'qtype_numerical', '{no}'),
|
||||
$creategrades->gradeoptions);
|
||||
//------------------------------------------------------------------------------------------
|
||||
$repeated = array();
|
||||
$repeated[] =& $mform->createElement('header', 'unithdr', get_string('unithdr', 'qtype_numerical', '{no}'));
|
||||
|
||||
@@ -26,30 +26,8 @@ class question_edit_shortanswer_form extends question_edit_form {
|
||||
$mform->closeHeaderBefore('answersinstruct');
|
||||
|
||||
$creategrades = get_grade_options();
|
||||
$gradeoptions = $creategrades->gradeoptions;
|
||||
$repeated = array();
|
||||
$repeated[] =& $mform->createElement('header', 'answerhdr', get_string('answerno', 'qtype_shortanswer', '{no}'));
|
||||
$repeated[] =& $mform->createElement('text', 'answer', get_string('answer', 'quiz'), array('size' => 54));
|
||||
$repeated[] =& $mform->createElement('select', 'fraction', get_string('grade'), $gradeoptions);
|
||||
$repeated[] =& $mform->createElement('htmleditor', 'feedback', get_string('feedback', 'quiz'),
|
||||
array('course' => $this->coursefilesid));
|
||||
|
||||
if (isset($this->question->options)){
|
||||
$countanswers = count($this->question->options->answers);
|
||||
} else {
|
||||
$countanswers = 0;
|
||||
}
|
||||
if ($this->question->formoptions->repeatelements){
|
||||
$repeatsatstart = (QUESTION_NUMANS_START > ($countanswers + QUESTION_NUMANS_ADD))?
|
||||
QUESTION_NUMANS_START : ($countanswers + QUESTION_NUMANS_ADD);
|
||||
} else {
|
||||
$repeatsatstart = $countanswers;
|
||||
}
|
||||
$repeatedoptions = array();
|
||||
$mform->setType('answer', PARAM_RAW);
|
||||
$repeatedoptions['fraction']['default'] = 0;
|
||||
$this->repeat_elements($repeated, $repeatsatstart, $repeatedoptions, 'noanswers', 'addanswers', QUESTION_NUMANS_ADD, get_string('addmoreanswerblanks', 'qtype_shortanswer'));
|
||||
|
||||
$this->add_per_answer_fields($mform, get_string('answerno', 'qtype_shortanswer', '{no}'),
|
||||
$creategrades->gradeoptions);
|
||||
}
|
||||
|
||||
function set_data($question) {
|
||||
|
||||
Reference in New Issue
Block a user