mform: question saving and re-editing problems

mform: question saving and re-editing problems

by Süreç Özcan -
Number of replies: 13
Hello,

this is a sum of the forum entry discussion here up from "by Süreç Özcan - Monday, 21 July 2008, 04:19 PM". I have attached the current version of my soregexp-question type.

I have been working on creating a modified version of the regexp-question type adapting it to my needs.

Adding a new question I receive following error message:
**********************************
Notice: Undefined property: stdClass::$noneditablequestiontext in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/questiontype.php on line 104

Notice: Undefined property: stdClass::$useeditor_questiontext in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/questiontype.php on line 105

Notice: Undefined property: stdClass::$useeditor_genfeedb in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/questiontype.php on line 106
Column 'useeditor_questiontext' cannot be null

INSERT INTO mdl19b_question_soregexp ( QUESTION, ANSWERS, USEHINT, SOURCECODE, NONEDITABLEQUESTIONTEXT, USEEDITOR_QUESTIONTEXT, USEEDITOR_GENFEEDB ) VALUES ( 64, '68', 0, 5, null, null, null )
  • line 1532 of lib/dmllib.php: call to debugging()
  • line 107 of question/type/soregexp/questiontype.php: call to insert_record()
  • line 316 of question/type/questiontype.php: call to question_soregexp_qtype->save_question_options()
  • line 183 of question/question.php: call to default_questiontype->save_question()
Could not insert quiz soregexp options!
Stack trace:
  • line 5670 of lib/weblib.php: call to debugging()
  • line 319 of question/type/questiontype.php: call to error()
  • line 183 of question/question.php: call to default_questiontype->save_question()
**********************************

In the editing-quiz-view I can see my created question (which returned the above mentioned error-message) - but I don't see it in the database.
Re-editing the question I receive following messages:

**********************************
Error: Missing question options!

Notice: Trying to get property of non-object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line 66

Notice: Trying to get property of non-object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line 97

Notice: Trying to get property of non- object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line 171

Notice: Trying to get property of non- object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line 182

Notice: Trying to get property of non- object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line 183

Notice: Trying to get property of non- object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line 184

Notice: Trying to get property of non- object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line 185

Notice: Trying to get property of non- object in /web/moodle.mi.fu-berlin.de/htdocs/moodle19beta/question/type/soregexp/edit_soregexp_form.php on line
186
**********************************

I can see the complete form, the 'questiontext' gets displayed, but the 'answer', '$sourcecode'-selection and '$noneditablequestiontext ' not. Does anybody know what I forget doing in order to get my new database fields saved for re-editing? And before that it doesn't save my question properly.... - I don't understand why?

Average of ratings: -
In reply to Süreç Özcan

Re: mform: question saving and re-editing problems

by Süreç Özcan -
The above mentioned issue happens on Moodle 1.9.

I gave it a second try (see attachment) for my other question-type sosourcecode where I only need to add one field into the database. It is a copy of the description-question with needed modifications. (I wanted to see if it might be a bug in 1.9 and prepared the question for Moodle 1.8.2)
Here at least it has no question saving problems - but my major problem still exist:
I save one of my new sosourcecode-question with checking the 'useeditor' check-box and I see a HTML-editor instead of a text-editor - which is correct. But when re-editing the question I see the text-editor instead of the already selected HTML-editor. Trying to check again the check-box returns me to the quiz-editing-form instead of redisplaying my question-editing-form.

I would be very thankful if anybody has any idea why this doesn't work properly.
Süreç
In reply to Süreç Özcan

Re: mform: question saving and re-editing problems

by Süreç Özcan -
oh, wow - I have found a bug in line 39 where I had written
get_record('question_sosourcecode', 'question', $question->id)
but it should be
get_record('question_sosourcecode', 'questionid', $question->id)
So: I think a copy and paste bug.

(on Moodle 1.8.2)
Now it almost seems to be working.
Trying to check again the check-box returns me to the quiz-editing-form instead of redisplaying my question-editing-form. If I delete the question-name before clicking at the check-box though it seems to be working properly.
Why should this happen? Of course I don't want to need the question-name before using the check-box. Anybody any ideas?

(on Moodle 1.9)
I have tried out sosourcecode-question type also in Moodle 1.9. There I can see it also in the database correct and so on. I have the same problem as in Moodle 1.8 and additionally when re-editing the question doesn't display the HTML-editor and the check-box isn't checked although I had saved it like this initially.
In reply to Süreç Özcan

Re: mform: question saving and re-editing problems

by Süreç Özcan -
Alright, now in both Moodle-versions I have only left the strange behavior mentioned above:
When re-editing the question trying to check again the check-box returns me to the quiz-editing-form instead of redisplaying my question-editing-form. If I delete the question-name before clicking at the check-box though it seems to be working properly. Does anybody has an idea why this happens?

(See the attachment for debugged version!)
What I did to solve the other issue for Moodle 1.9: I had forgotten to set $default_values for 'useeditor'-field in set_data-function which I had done in 1.8.

I don't understand why I found this next bug only after a restart of my computer - but in both Moodle versions the switch didn't work when re-editing the question. I needed to change:
---------------
$useeditor = optional_param('useeditor', false);
if ($useeditor) {
$this->_textarea_or_htmleditor = 'htmleditor'; //options for htmleditor
$this->_editor_options = array('rows' => 8, 'course' => $COURSE->id);
} else {
$this->_textarea_or_htmleditor = 'textarea'; // options for textarea
$this->_editor_options = array('rows' => 3, 'cols' => 57, 'course' => $COURSE->id);
}

---------------
to
---------------
global $question;
//needed for opening editing page and using db-value, later use the reloaded value
$loaded_initialy = optional_param('reloaded_initialy',1,PARAM_INT);
print_object('reloaded initially?: ' . $loaded_initialy);//empty($loaded_initialy));

if (isset($question->id) and $loaded_initialy){//!$loaded_initialy){ //do this only if re-editing question and once
$questionid = $question->id;
print_object('questionid: '. $questionid);
$useeditor_db = get_record_select('question_sosourcecode', "questionid=$questionid",
'useeditor');
print_object('useeditor_db: ' . $useeditor_db->useeditor);
if ($useeditor_db->useeditor == 0) {
print_object("useeditor = 0");

$useeditor = optional_param('useeditor', $useeditor_db->useeditor);
$this->_textarea_or_htmleditor = 'textarea'; // options for textarea
$this->_editor_options = array('rows' => 3, 'cols' => 57, 'course' => $COURSE->id);

$loaded_initialy = 0;
print_object('reloaded_initialy_useeditor0: '. $loaded_initialy);
}
if ($useeditor_db->useeditor == 1){
print_object("useeditor = 1");

$useeditor = optional_param('useeditor', $useeditor_db->useeditor);
$this->_textarea_or_htmleditor = 'htmleditor'; //options for htmleditor
$this->_editor_options = array('rows' => 8, 'course' => $COURSE->id);

$loaded_initialy = 0;
print_object('reloaded_initialy_useeditor: '. $loaded_initialy);
}
}
else{ // do this when creating a new question and after having reloaded once
$useeditor = optional_param('useeditor', false);
if ($useeditor) {
$this->_textarea_or_htmleditor = 'htmleditor'; //options for htmleditor
$this->_editor_options = array('rows' => 8, 'course' => $COURSE->id);
} else {
$this->_textarea_or_htmleditor = 'textarea'; // options for textarea
$this->_editor_options = array('rows' => 3, 'cols' => 57, 'course' => $COURSE->id);
}
$loaded_initialy = 0;
}
$mform->addElement('hidden', 'reloaded_initialy', $loaded_initialy);

---------------
The idea is following: Do the first if-branch only when re-editing a question and it is not reloaded. This way it takes the db-entry-value once and displays the proper editor. (without this part it used always the text-editor which was set initially). After this initial usage do the first else-branch, otherwise it keeps on using the value from the database which won't get changed until the question is saved again.





In reply to Süreç Özcan

Re: mform: question saving and re-editing problems

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Alright, now in both Moodle-versions I have only left the strange behavior mentioned above:

When re-editing the question trying to check again the check-box returns me to the quiz-editing-form instead of redisplaying my question-editing-form.

I think that must be becuase the reload javascript submits the form ...

If I delete the question-name before clicking at the check-box though it seems to be working properly. Does anybody has an idea why this happens?

When the form is submitted, if there are any validation errors, the form is redisplayed. Question name is compulsory, so if it is not set, the form is redisplayed.

... so, when the form is submitted by the onclick JS on the check-boxes, you need to ensure that formslib does not think it is a real submission. You already have a reloaded form element that seems to be for this purpose. I think you just need to do

$mform->registerNoSubmitButton('reloaded');

to get formslib to play along.
In reply to Tim Hunt

Re: mform: question saving and re-editing problems

by Süreç Özcan -
wow - great - yeah, this makes sense! I put your suggested line above the hidden-element. It seems to work now - partially.

Now the submit-button doesn't work. The question doesn't get closed.
This is strange because I just tell 'reloaded' being not a submit button. 'save as new question' though seems to work - closes the editing-view and lists it in the question-list.
In reply to Süreç Özcan

Re: mform: question saving and re-editing problems

by Süreç Özcan -
The problem of not saving the question occurs only after the editor has been changed once. If I edit other stuff of the question without changing the already used editor, saving works fine.
What could that mean?
In reply to Süreç Özcan

Re: mform: question saving and re-editing problems

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Ah, it is becuase changing the editor changes the reloaded parameter to 1, after which it stays set to 1. One way to solve this is:

In set_data, just before the line

parent::set_data($question);

Add the line

$question->reloaded = 0;
In reply to Tim Hunt

Re: mform: question saving and re-editing problems

by Süreç Özcan -
doing as you suggest still keeps the same problem - I did:

print_object('question-reloaded1?: '.$question->reloaded);
$question->reloaded = 0;
print_object('question-reloaded0?: '.$question->reloaded);
parent::set_data($question);


Is it normal that the first print_object doesn't return any value - I expected it to return 1!?
The second print_object returns 0 as expected.
By the way how can I see the content of $question using print_object?
In reply to Süreç Özcan

Re: mform: question saving and re-editing problems

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
This has got me completely puzzled. I have been trying to understand the formslib code to see why this does not work, and I can't make head or tail of it.

To display the whole of $question, you have to do print_object($question).
In reply to Tim Hunt

Re: mform: question saving and re-editing problems

by Süreç Özcan -
hmmmm - really strange...
Thank you very much for taking your time!
I hope somebody else can help me out with this!?
In reply to Süreç Özcan

Re: mform: question saving and re-editing problems

by Süreç Özcan -
Aha, a hack in questiontype.php in display_question_editing_page-function in the javascript with "document.forms['$formid'].reloaded.value = 0;" setting before reload-function handles this.

With some reason the set_data-function ignores when I set $question->reloaded=0; on the server-side. The hack mentioned above is client-side and this seems to work.
In reply to Süreç Özcan

Re: mform-field: question saving and re-editing problems

by Süreç Özcan -
I have a drop-down-menu. Selecting a question from the list displays the questiontext of the selected question in 'noneditablequestiontext'. The questiontext is a source code so that I use format_text-function on the questiontext in order to get it displayed properly in a static-field.

The problem: Saving the question I can't see the questiontext in the database so that as a result there is nothing to display when re-editing the question initially.
Could this have to do anything because I format the questiontext?
Could this have to do something with stripslashes_safe- and s-methods?

I have tried out doing the following in definition_inner-function, but this didn't work:
set_field('question_soregexp', 'noneditablequestiontext', $sc_to_display->questiontext, 'question', $question->id);

I have attached the current soregexp-version with install.xml instead of several sql-files.
In reply to Süreç Özcan

Re: mform-field: question saving and re-editing problems

by Süreç Özcan -
Alright, the point is that I don't really need an extra database field entry for this, because I can use 'sourcecode'-field to get what I need.
In order to get it working I needed to take care of following:
While creating a new question, initially the source code of the first field of the drop-down-menu needs to be saved.
Then later, when I re-edit the question, the initial value needs to be set to the already selected/saved source code in order to show the correct source code. Otherwise it would display the first selection of the drop-down-menu again.