"cannot find data record in database table context" Quiz results (Review attempt)

"cannot find data record in database table context" Quiz results (Review attempt)

by Tim States -
Number of replies: 7

Received this error when trying to review an attempt on a quiz.  Using Moodle 2.4.4.  Any help in solving the issue is appreciated greatly.  Below is the debugging info:

 

Tim States

Anderson University

Average of ratings: Useful (1)
In reply to Tim States

Re: "cannot find data record in database table context" Quiz results (Review attempt)

by Melissa Benson -

Hey Tim, Did you ever figure this out?

Average of ratings: Useful (1)
In reply to Tim States

Re: "cannot find data record in database table context" Quiz results (Review attempt)

by Ben Kelada -

HI currently researhing this same problem on our moodle instance

Did anyone figure this out?

cheers,

Ben

In reply to Ben Kelada

Re: "cannot find data record in database table context" Quiz results (Review attempt)

by Ben Kelada -

ok i know what is causing this issue for us

here is my debugging for future moodlers:

line 1375 is where a context is searched for, line 872 gives the item which context id will be searched for (question_has_capability_on)

  • line 1375 of /lib/questionlib.php: call to context::instance_by_id()
  • line 872 of /mod/quiz/attemptlib.php: call to question_has_capability_on
Questions have a category, their question categories have a contextid, the error experienced is that the category is missing its context
to find the exact category that is "orphaned" from its context we run the following query:

select * from mdl_question_categories qc 
left join mdl_context mc on qc.contextid = mc.id
where contextid = {id here}; --in my case it was id 1426139

For further debugging we can find all categories with missing contexts:
select * from mdl_question_categories qc 
left join mdl_context mc on qc.contextid = mc.id
where mc.id is null

If you want to find the questions that belong to the orphaned category, you take the id from the "orphaned" category
you can find all questions associated with the category they may produce errors if used within question attempts
select * from mdl_question where category = {ghost category id}; --in my case 139055
The actual quiz on my course shows correctly with the current questions, on the quiz / report / overview i can see and click answered questions
it is only when i click on "review attempt" that the above mentioned error occurs.


From my investigation  it is because a previous attempt was made containing the questions from the orphaned category.
by taking the attempt id from the url of the page that generated the error (in my case: review.php?attempt=1474741 )
Note this only proves the assumption, the information is not necessary to fix the problem.
SELECT
distinct 
    qa.questionid,*
 FROM      mdl_question_usages          quba
LEFT JOIN mdl_question_attempts          qa   ON qa.questionusageid    = quba.id
LEFT JOIN mdl_question_attempt_steps    qas  ON qas.questionattemptid = qa.id
LEFT JOIN mdl_question_attempt_step_data qasd ON qasd.attemptstepid    = qas.id
LEFT JOIN mdl_quiz_attempts quiza on quiza.uniqueid = quba.id
WHERE  1=1
and quiza.id = '1474741' -- attempt# from url
and qa.questionid in (select id from mdl_question where category = 139055) --filter by broken category for brevity

To fix the problem for us:
I changed the context for the "orphaned" question category to a known context, i chose an "uncategorized' context
UPDATE mdl_question_categories
   SET  contextid= 110527 -- I chose this context from the same subjects questionbank
 WHERE id = 139055 --this is the broken question category id

I've tried this in our dev environment and it fixes the error but i'm unsure of the other implications
if anyone who knows could comment i would appreciate
Also the root cause is still not known, i imagine someone deleted the category but left the questions? is this possible?
cheers,
Ben

In reply to Ben Kelada

Re: "cannot find data record in database table context" Quiz results (Review attempt)

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Are you reading the other similar thread: https://moodle.org/mod/forum/discuss.php?d=263041

Your analysis looks right. The only thing that might not work are images, are there is a hint in the other thread about how to fix them too.

Average of ratings: Useful (1)
In reply to Tim Hunt

Re: "cannot find data record in database table context" Quiz results (Review attempt)

by Ben Kelada -

thanks tim, 

no i  hadn't seen that thread in spite of my attempts at searching and scanning!

Average of ratings: Useful (1)
In reply to Tim States

Re: "cannot find data record in database table context" Quiz results (Review attempt)

by Conn Warwicker -
Picture of Core developers Picture of Plugin developers

In case it helps anyone, I found that changing the category on the question wasn't enough on its own, I also had to purge the cache otherwise the questions still had the old category.

Average of ratings: Useful (1)
In reply to Tim States

Re: "cannot find data record in database table context" Quiz results (Review attempt)

by steve miley -

After upgrading to 2.8 from 2.5, we ran into this.  We came up with this query to identify all question categories with a context that is broken and needs fixing -   we just had two.


<code>

SELECT

    mdl_quiz.id,

    mdl_quiz.course,

    mdl_quiz.name,

    mdl_question.id,

    mdl_question.category,

    mdl_question.parent,

    mdl_question.name,

    mdl_question_categories.contextid AS catcontextidbroken,

mdl_question_categories.id AS questinocategoryid,

    mdl_question_categories.name      AS catname,

    mdl_context.id                    AS contextidtoset

FROM

    mdl_quiz_slots

INNER JOIN

    mdl_quiz

ON

    (

        mdl_quiz_slots.quizid = mdl_quiz.id)

INNER JOIN

    mdl_question

ON

    (

        mdl_quiz_slots.questionid = mdl_question.id)

INNER JOIN

    mdl_question_categories

ON

    (

        mdl_question.category = mdl_question_categories.id)

INNER JOIN

    mdl_context

ON

    (

        mdl_quiz.course = mdl_context.instanceid)

        

        left join mdl_context mc on mdl_question_categories.contextid = mc.id


WHERE mc.id is null AND 

    mdl_context.id <>

    mdl_question_categories.contextid

AND mdl_context.contextlevel = 50 ;


</code>


after this, we would have the informatino to do the 

update mdl_question_categories set contextid = XXX where id = YYY;