Details
Description
In the actual question/type/questiontype.php save_question function
the code is
if (!empty($question->id) && !empty($form->categorymoveto)) { // Question already exists
list($movetocategory, $movetocontextid) = explode(',', $form->categorymoveto);
if ($movetocategory != $question->category){
question_require_capability_on($question, 'move');
$question->category = $movetocategory;
//don't need to test add permission of category we are moving question to.
//Only categories that we have permission to add
//a question to will get through the form cleaning code for the select box.
}
// keep existing unique stamp code
$question->stamp = get_field('question', 'stamp', 'id', $question->id);
$question->modifiedby = $USER->id;
$question->timemodified = time();
if (!update_record('question', $question)) {
error('Could not update question!');
}
} else { // Question is a new one
if (isset($form->categorymoveto)){
// Doing save as new question, and we have move rights.
list($question->category, $notused) = explode(',', $form->categorymoveto);
//don't need to test add permission of category we are moving question to.
//Only categories that we have permission to add
//a question to will get through the form cleaning code for the select box.
} else {
// Really a new question.
list($question->category, $notused) = explode(',', $form->category);
}
// Set the unique code
$question->stamp = make_unique_id_code();
$question->createdby = $USER->id;
$question->modifiedby = $USER->id;
$question->timecreated = time();
$question->timemodified = time();
if (!$question->id = insert_record('question', $question)) {
print_object($question);
error('Could not insert new question!');
}
}
If there is a call to save_question with an existing question i.e. !empty($question->id without the $form->categorymoveto parameter, this question will be considered as a new one.
This is the case when saving multianswers subquestions when these subquestions already exist i.e. when modifying a multianswer.
The multianswer case can be solved by adding a false categorymoveto parameter before calling the save_question.
However I think that the code should be modified so that it takes in account the case when the save_question is not coming from a question_edit_form with a valid $form->categorymoveto.
The situation we have to be careful of is when the user had done save as new question.
In this case $question->id will be set, but we are still creating a new question. We need to find a reliable way of identifying this case.
I think this is a tricky bug. There have been related problems before like http://tracker.moodle.org/browse/MDL-14676. Can you prepare a patch, and then we can review it together. It would be nice to get this sorted out once and for all, but it is clearly very easy to cause regressions in this area. Thanks.