Non-core contributed modules

Rendered match questions don't restore when you try to restore a backup file.

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.6.2
  • Fix Version/s: 1.9
  • Labels:
    None
  • Environment:
    ALL.
  • Database:
    Any
  • Affected Branches:
    MOODLE_16_STABLE
  • Fixed Branches:
    MOODLE_19_STABLE

Description

The rendered match question type isn't restored. I have renderedmatch installed on moodle 1.6.2 but they wouldn't restore.

here is a fix.
open question/restorelib.php

go to function restore_questions

after this ..
//If the question exists, only record its id
if ($question_exists) { $newid = $question_exists->id; $creatingnewquestion = false; //Else, create a new question } else { //The structure is equal to the db, so insert the question $newid = insert_record ("question",$question); $creatingnewquestion = true; }

ADD THIS...

/new stuff/

if($question->qtype=='renderedmatch'||$question->qtype=="13")
{
$x=0;
$test=1;
$question_renderedmatch_sub->question=$newid;
$question_renderedmatch->question=$newid;

while($test!=2)
{
if(!empty($que_info'#'['RENDEREDS']['0']'#'['RENDERED'][$x]))

{ $question_renderedmatch_sub->questiontext= backup_todb($que_info['#']['RENDEREDS']['0']['#']['RENDERED'][$x]['#']['QUESTIONTEXT']['0']['#']); $question_renderedmatch_sub->answertext= backup_todb($que_info['#']['RENDEREDS']['0']['#']['RENDERED'][$x]['#']['ANSWERTEXT']['0']['#']); $subid=insert_record ("question_renderedmatch_sub",$question_renderedmatch_sub); $subquest[$x]=$subid; }

else

{ $test=2; }

$x=$x+1;

}
$question_renderedmatch->subquestions=implode(",",$subquest);
insert_record("question_renderedmatch",$question_renderedmatch);

}
/new stuff ends/

not pretty but it works...

Issue Links

Activity

Hide
Eloy Lafuente (stronk7) added a comment -

Sure Tim is more able here....

Show
Eloy Lafuente (stronk7) added a comment - Sure Tim is more able here....
Hide
Tim Hunt added a comment -

Tom, thanks for fixing this. I'm afraid your code does not match the moodle coding guidelines, so I have rewritten it:

/new stuff/
// For renderedmatch questions, restore the information about subquestions.
if ($question->qtype == 'renderedmatch') {
$subquest = array();
foreach ($que_info'#'['RENDEREDS']['0']'#'['RENDERED'] as $i => $sub_que_info) { $subquestion = new stdClass; $subquestion->question = $newid; $subquestion->questiontext = backup_todb($sub_que_info['#']['QUESTIONTEXT']['0']['#']); $subquestion->answertext = backup_todb($sub_que_info['#']['ANSWERTEXT']['0']['#']); $subid = insert_record('question_renderedmatch_sub', $subquestion); $subquest[$i] = $subid; }

$renderedmatch = new stdClass;
$renderedmatch->question = $newid;
$renderedmatch->subquestions = implode(',', $subquest);
insert_record('question_renderedmatch', $renderedmatch);
}
/new stuff ends/

However, I don't have much experience with this question type, so it is not very easy for me to test. Would you be able to test this new code for me?

Alternatively, could you attatch a backup file containing several renderedmatch questions to this bug report, so I can test it myself.

Thanks for your help.

Show
Tim Hunt added a comment - Tom, thanks for fixing this. I'm afraid your code does not match the moodle coding guidelines, so I have rewritten it: /new stuff/ // For renderedmatch questions, restore the information about subquestions. if ($question->qtype == 'renderedmatch') { $subquest = array(); foreach ($que_info'#'['RENDEREDS']['0']'#'['RENDERED'] as $i => $sub_que_info) { $subquestion = new stdClass; $subquestion->question = $newid; $subquestion->questiontext = backup_todb($sub_que_info['#']['QUESTIONTEXT']['0']['#']); $subquestion->answertext = backup_todb($sub_que_info['#']['ANSWERTEXT']['0']['#']); $subid = insert_record('question_renderedmatch_sub', $subquestion); $subquest[$i] = $subid; } $renderedmatch = new stdClass; $renderedmatch->question = $newid; $renderedmatch->subquestions = implode(',', $subquest); insert_record('question_renderedmatch', $renderedmatch); } /new stuff ends/ However, I don't have much experience with this question type, so it is not very easy for me to test. Would you be able to test this new code for me? Alternatively, could you attatch a backup file containing several renderedmatch questions to this bug report, so I can test it myself. Thanks for your help.
Hide
Tom Trueluk added a comment -

Here is a backup from moodle 1.5.4+ since I just realized that moodle 1.6 also won't backup rendered matching in courses. I'll take a look at that and submit code for that later today. Your code works fine, except we need to change $i to $j (or something besides $i) since we are already in a for loop " for($i = 0; $i < sizeof($questions); $i++) { "
using $i causes an infinite loop.

However, there is an issue. When you actually go into the quiz and try to edit the question, the subquestions and subanswers don't show up. But, in the preview they are both there, not sure whats going on because they are all in the database and work when you take the quiz, just not editable????

Tom.

Show
Tom Trueluk added a comment - Here is a backup from moodle 1.5.4+ since I just realized that moodle 1.6 also won't backup rendered matching in courses. I'll take a look at that and submit code for that later today. Your code works fine, except we need to change $i to $j (or something besides $i) since we are already in a for loop " for($i = 0; $i < sizeof($questions); $i++) { " using $i causes an infinite loop. However, there is an issue. When you actually go into the quiz and try to edit the question, the subquestions and subanswers don't show up. But, in the preview they are both there, not sure whats going on because they are all in the database and work when you take the quiz, just not editable???? Tom.
Hide
Tom Trueluk added a comment -

Ok, I have coded the question/backuplib.php to backup rendered match question types. Once again, the code might not be up to moodle standards, but it appears to be working. But, still on restore you can't edit the subquestions and subanswers, but theyr are there when you preview and are present in the database.

here is the code i added to backuplib.php in the backup_question function:

fwrite ($bf,full_tag("STAMP",6,false,$question->stamp));
fwrite ($bf,full_tag("VERSION",6,false,$question->version));
fwrite ($bf,full_tag("HIDDEN",6,false,$question->hidden));
// Backup question type specific data
/code above is already there/
/* this is my code */

if($question->qtype=="renderedmatch")
{
$subs=get_records("question_renderedmatch_sub","question",$question->id);
fwrite ($bf,start_tag("RENDEREDS",6,true));

foreach($subs as $sub)

{ fwrite ($bf,start_tag("RENDERED",6,true)); fwrite ($bf,full_tag("ID",6,false,$sub->id)); fwrite ($bf,full_tag("QUESTIONTEXT",6,false,$sub->questiontext)); fwrite ($bf,full_tag("ANSWERTEXT",6,false,$sub->answertext)); fwrite ($bf,end_tag("RENDERED",5,true)); }

fwrite ($bf,end_tag("RENDEREDS",5,true));

}

/*this is the end of my code, the code below is already in file */
$status = $QTYPES[$question->qtype]>backup($bf,$preferences,$question>id);
//End question

Show
Tom Trueluk added a comment - Ok, I have coded the question/backuplib.php to backup rendered match question types. Once again, the code might not be up to moodle standards, but it appears to be working. But, still on restore you can't edit the subquestions and subanswers, but theyr are there when you preview and are present in the database. here is the code i added to backuplib.php in the backup_question function: fwrite ($bf,full_tag("STAMP",6,false,$question->stamp)); fwrite ($bf,full_tag("VERSION",6,false,$question->version)); fwrite ($bf,full_tag("HIDDEN",6,false,$question->hidden)); // Backup question type specific data /code above is already there/ /* this is my code */ if($question->qtype=="renderedmatch") { $subs=get_records("question_renderedmatch_sub","question",$question->id); fwrite ($bf,start_tag("RENDEREDS",6,true)); foreach($subs as $sub) { fwrite ($bf,start_tag("RENDERED",6,true)); fwrite ($bf,full_tag("ID",6,false,$sub->id)); fwrite ($bf,full_tag("QUESTIONTEXT",6,false,$sub->questiontext)); fwrite ($bf,full_tag("ANSWERTEXT",6,false,$sub->answertext)); fwrite ($bf,end_tag("RENDERED",5,true)); } fwrite ($bf,end_tag("RENDEREDS",5,true)); } /*this is the end of my code, the code below is already in file */ $status = $QTYPES[$question->qtype]>backup($bf,$preferences,$question>id); //End question
Hide
Jean-Michel Vedrine added a comment -

Tim,
renderedmatch is now in contrib/plugins/question/type/renderedmatch
I have added corrects (I think !) backup & restore functions to the questiontype.php file.
So I think you can close this bug

Show
Jean-Michel Vedrine added a comment - Tim, renderedmatch is now in contrib/plugins/question/type/renderedmatch I have added corrects (I think !) backup & restore functions to the questiontype.php file. So I think you can close this bug
Hide
Tim Hunt added a comment -

Just assigning it to you, so you get the credit for fixing it.

Show
Tim Hunt added a comment - Just assigning it to you, so you get the credit for fixing it.

People

Vote (2)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: