Skip to content

Commit

Permalink
MDL-27868 Hints (and some other things) lost when re-editing calculat…
Browse files Browse the repository at this point in the history
…ed* questions.
  • Loading branch information
timhunt committed Jun 17, 2011
1 parent d037c6b commit 5cf69d7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 190 deletions.
86 changes: 28 additions & 58 deletions question/type/calculated/edit_calculated_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,68 +163,38 @@ protected function definition_inner($mform) {
}

public function data_preprocessing($question) {
$default_values = array();
if (isset($question->options)) {
$answers = $question->options->answers;
if (count($answers)) {
$key = 0;
foreach ($answers as $answer) {
$draftid = file_get_submitted_draft_itemid('feedback['.$key.']');
$default_values['answer['.$key.']'] = $answer->answer;
$default_values['fraction['.$key.']'] = $answer->fraction;
$default_values['tolerance['.$key.']'] = $answer->tolerance;
$default_values['tolerancetype['.$key.']'] = $answer->tolerancetype;
$default_values['correctanswerlength['.$key.']'] = $answer->correctanswerlength;
$default_values['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
$default_values['feedback['.$key.']'] = array();
$default_values['feedback['.$key.']']['text'] = file_prepare_draft_area(
$draftid, // draftid
$this->context->id, // context
'question', // component
'answerfeedback', // filarea
!empty($answer->id)?(int)$answer->id:null, // itemid
$this->fileoptions, // options
$answer->feedback // text
);
$default_values['feedback['.$key.']']['format'] = $answer->feedbackformat;
$default_values['feedback['.$key.']']['itemid'] = $draftid;
$key++;
}
}
$default_values['synchronize'] = $question->options->synchronize;
}
if (isset($question->options->single)) {
$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
}
$default_values['submitbutton'] = get_string('nextpage', 'qtype_calculated');
$default_values['makecopy'] = get_string('makecopynextpage', 'qtype_calculated');
$default_values['returnurl'] = '0';

$qu = new stdClass();
$el = new stdClass();
/* no need to call elementExists() here */
if ($this->_form->elementExists('category')) {
$el = $this->_form->getElement('category');
} else {
$el = $this->_form->getElement('categorymoveto');
}
if ($value = $el->getSelected()) {
$qu->category = $value[0];
} else {
// on load $question->category is set by question.php
$qu->category = $question->category;
}
$html2 = $this->qtypeobj->print_dataset_definitions_category($qu);
$this->_form->_elements[$this->_form->_elementIndex['listcategory']]->_text = $html2;
$question = (object)((array)$question + $default_values);

$question = parent::data_preprocessing($question);
$question = $this->data_preprocessing_answers($question);
$question = $this->data_preprocessing_hints($question);
$question = $this->data_preprocessing_units($question);
$question = $this->data_preprocessing_unit_options($question);

if (isset($question->options->synchronize)) {
$question->synchronize = $question->options->synchronize;
}

return $question;
}

protected function data_preprocessing_answers($question) {
$question = parent::data_preprocessing_answers($question);
if (empty($question->options->answers)) {
return $question;
}

$key = 0;
foreach ($question->options->answers as $answer) {
// See comment in the parent method about this hack.
unset($this->_form->_defaultValues["tolerancetype[$key]"]);
unset($this->_form->_defaultValues["correctanswerlength[$key]"]);
unset($this->_form->_defaultValues["correctanswerformat[$key]"]);

$question->tolerancetype[$key] = $answer->tolerancetype;
$question->correctanswerlength[$key] = $answer->correctanswerlength;
$question->correctanswerformat[$key] = $answer->correctanswerformat;
$key++;
}

return $question;
}

Expand Down
126 changes: 32 additions & 94 deletions question/type/calculatedmulti/edit_calculatedmulti_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,104 +183,42 @@ protected function definition_inner($mform) {
}

public function data_preprocessing($question) {
$default_values['multichoice']= $this->editasmultichoice;
$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, true, true);

if (isset($question->options)) {
$answers = $question->options->answers;
if (count($answers)) {
$key = 0;
foreach ($answers as $answer) {
$draftid = file_get_submitted_draft_itemid('feedback['.$key.']');
$default_values['answer['.$key.']'] = $answer->answer;
$default_values['fraction['.$key.']'] = $answer->fraction;
$default_values['tolerance['.$key.']'] = $answer->tolerance;
$default_values['tolerancetype['.$key.']'] = $answer->tolerancetype;
$default_values['correctanswerlength['.$key.']'] = $answer->correctanswerlength;
$default_values['correctanswerformat['.$key.']'] = $answer->correctanswerformat;
$default_values['feedback['.$key.']'] = array();
// prepare draftarea
$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['synchronize'] = $question->options->synchronize;

if (isset($question->options->units)) {
$units = array_values($question->options->units);
// make sure the default unit is at index 0
usort($units, create_function('$a, $b',
'if (1.0 === (float)$a->multiplier) { return -1; } else '.
'if (1.0 === (float)$b->multiplier) { return 1; } else { return 0; }'));
if (count($units)) {
$key = 0;
foreach ($units as $unit) {
$default_values['unit['.$key.']'] = $unit->unit;
$default_values['multiplier['.$key.']'] = $unit->multiplier;
$key++;
}
}
}
}
if (isset($question->options->single)) {
$default_values['single'] = $question->options->single;
$default_values['answernumbering'] = $question->options->answernumbering;
$default_values['shuffleanswers'] = $question->options->shuffleanswers;
}
$default_values['submitbutton'] = get_string('nextpage', 'qtype_calculated');
$default_values['makecopy'] = get_string('makecopynextpage', 'qtype_calculated');

// prepare draft files
foreach (array('correctfeedback', 'partiallycorrectfeedback',
'incorrectfeedback') as $feedbackname) {
if (!isset($question->options->$feedbackname)) {
continue;
}
$text = $question->options->$feedbackname;
$draftid = file_get_submitted_draft_itemid($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_calculatedmulti', // 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;
$question->synchronize = $question->options->synchronize;
$question->single = $question->options->single;
$question->answernumbering = $question->options->answernumbering;
$question->shuffleanswers = $question->options->shuffleanswers;
}
/**
* set the wild cards category display given that on loading the category element is
* unselected when processing this function but have a valid value when processing the
* update category button. The value can be obtain by
* $qu->category = $this->_form->_elements[$this->_form->
* _elementIndex['category']]->_values[0];
* but is coded using existing functions
*/
$qu = new stdClass();
$el = new stdClass();
// no need to call elementExists() here.
if ($this->_form->elementExists('category')) {
$el = $this->_form->getElement('category');
} else {
$el = $this->_form->getElement('categorymoveto');

return $question;
}

protected function data_preprocessing_answers($question) {
$question = parent::data_preprocessing_answers($question);
if (empty($question->options->answers)) {
return $question;
}
if ($value = $el->getSelected()) {
$qu->category = $value[0];
} else {
// on load $question->category is set by question.php
$qu->category = $question->category;

$key = 0;
foreach ($question->options->answers as $answer) {
// See comment in the parent method about this hack.
unset($this->_form->_defaultValues["tolerance[$key]"]);
unset($this->_form->_defaultValues["tolerancetype[$key]"]);
unset($this->_form->_defaultValues["correctanswerlength[$key]"]);
unset($this->_form->_defaultValues["correctanswerformat[$key]"]);

$question->tolerance[$key] = $answer->tolerance;
$question->tolerancetype[$key] = $answer->tolerancetype;
$question->correctanswerlength[$key] = $answer->correctanswerlength;
$question->correctanswerformat[$key] = $answer->correctanswerformat;
$key++;
}
$html2 = $this->qtypeobj->print_dataset_definitions_category($qu);
$this->_form->_elements[$this->_form->_elementIndex['listcategory']]->_text = $html2;
$question = (object)((array)$question + $default_values);

return $question;
}

Expand Down
44 changes: 6 additions & 38 deletions question/type/calculatedsimple/edit_calculatedsimple_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,47 +607,15 @@ protected function definition_inner($mform) {
}

public function data_preprocessing($question) {
$answer = $this->answer;
$default_values = array();
if (count($answer)) {
$key = 0;
foreach ($answer as $answer) {
$default_values['answer['.$key.']'] = $answer->answer;
$default_values['fraction['.$key.']'] = $answer->fraction;
$default_values['tolerance['.$key.']'] = $answer->tolerance;
$default_values['tolerancetype['.$key.']'] = $answer->tolerancetype;
$default_values['correctanswerlength['.$key.']'] = $answer->correctanswerlength;
$default_values['correctanswerformat['.$key.']'] = $answer->correctanswerformat;

// prepare draft files
$draftid = file_get_submitted_draft_itemid('feedback['.$key.']');
$default_values['feedback['.$key.']']['text'] = file_prepare_draft_area(
$draftid, // draftid
$this->context->id, // context
'question', // component
'answerfeedback', // filarea
!empty($answer->id)?(int)$answer->id:null, // itemid
$this->fileoptions, // options
!empty($answer->feedback)?$answer->feedback:'' // text
);
$default_values['feedback['.$key.']']['format'] = !empty($answer->feedbackformat) ?
$answer->feedbackformat : editors_get_preferred_format();
$default_values['feedback['.$key.']']['itemid'] = $draftid;

$key++;
}
}
$default_values['synchronize'] = 0;

$formdata = array();
$fromform = new stdClass();
//this should be done before the elements are created and stored as $this->formdata;
//fill out all data sets and also the fields for the next item to add.
$question = (object)((array)$question + $default_values+$this->formdata);

$question = parent::data_preprocessing($question);
$question = $this->data_preprocessing_answers($question);
$question = $this->data_preprocessing_hints($question);
$question = $this->data_preprocessing_units($question);
$question = $this->data_preprocessing_unit_options($question);

// This is a bit ugly, but it loads all the dataset values.
$question = (object)((array)$question + $this->formdata);

return $question;
}

Expand Down
3 changes: 3 additions & 0 deletions question/type/numerical/edit_numerical_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ protected function data_preprocessing_answers($question) {

$key = 0;
foreach ($question->options->answers as $answer) {
// See comment in the parent method about this hack.
unset($this->_form->_defaultValues["tolerance[$key]"]);

$question->tolerance[$key] = $answer->tolerance;
$key++;
}
Expand Down

0 comments on commit 5cf69d7

Please sign in to comment.