Health Problem: Question categories should belong to a valid context

Health Problem: Question categories should belong to a valid context

by Han Gao -
Number of replies: 23

Hello all,

Recently I have upgraded our moodle server to the newest weekly (1.9.3+) (this was convenient because the server was migrated to a new machine). After doing all the tasks, I went to the health center, and encountered this health problem:

Question Categories should belong to a valid context

All qestion categories are linked to a context id, and, the context they are linked to must exist. the following categories belong to a non-existant category

<some details>

Any of these categories that contain no questions can just be deleted from the database. Other categories will require more thought

-------------------------

When i press on "view solution", I get this:

-------------------------

Solution

You can delete the empty categories by executing the following SQL:

DELETE FROM mdl_question_categories qc WHERE NOT EXISTS (SELECT * FROM mdl_question q WHERE q.category = qc.id) AND NOT EXISTS (SELECT * FROM mdl_context con WHERE  qc.contextid = con.id)

Any remaining categories that contain questions will require more thought. People in the Quiz forum may be able to help.

-------------------------

Needless to say, I am a total newbe to MySQL, and I copied the statement into the MYSql environment. However, when I run it, i get the error that i have an error in my SQL syntax.

My question now is: is this health problem a serious problem? To me it seems it is not that big a deal, but I want to be sure. And how to run the above command, such that the health problem will disappear?

Any toughts much appreciated!

Average of ratings: -
In reply to Han Gao

Re: Health Problem: Question categories should belong to a valid context

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 is not a serious problem. These orphaned categories will not cause any problems for your site. The extra categories and any questions they contain will just take up a very small amount of space in you database.

You are right that the SQL in the solution does not work on MySQL. Drat! MDL-16935
In reply to Tim Hunt

Re: Health Problem: Question categories should belong to a valid context

by Leo Dil -
Hi Tim,

I've installed a clean 1.9.7+ and added a few questions via Quiz.
After adding I get this error!

I've had a site with 1.8 and couldn't upgrade it because of this problem.

How to provent is from happening??

Thanks
In reply to Leo Dil

Re: Health Problem: Question categories should belong to a valid context

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Very odd that is is still happening in Moodle 1.9.7.

In an ideal world, you would be able to tell us an exact sequence of steps:

1. Install Moodle 1.9.7
2. Do this ...
3. Do that ...
4. ...

That ends up with an orphaned question category. That way, we would be able to reproduce the problem and fix it.


I suppose, in the mean time, could you have a look in the question_categories table in the database, and see which contextid values there are that do not correspond to ids in the context table. Does that give you any clues as to what is going on?
In reply to Tim Hunt

Re: Health Problem: Question categories should belong to a valid context

by Leo Dil -
Hallo Tim,

Yes indeed. Here is what I've done.
I've looked at all of the field inside Context and question_categories and found out that:

1: ID 103 doesn't exist in Context but exists in question_categories.
2: ID 285, 371 and 612 is dubble in questions_categories.

I think I found the problem. I've deleted the course and restored it again with the same result, thus there is something wrong with restore I think.

Hope this helps you find the problem. Let me know if u need more info.

Thanks.

Leon
In reply to Leo Dil

Re: Health Problem: Question categories should belong to a valid context

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

I don't have any great ideas for what might be wrong. I guess we should file a tracker.moodle.org bug with the details for now.

The safest way to sort out this situation is to:

1. Create a temporary course.
2. Find that course's context id.
3. change the orphan question_categories (in the database) to point to the temp course context id.
4. Go into the question bank for the temp course, and see what is there, and either move anything that should not have been deleted to the right place, or delete anything that is junk.
In reply to Tim Hunt

Re: Health Problem: Question categories should belong to a valid context

by Leo Dil -
Hi Tim,

I wasn't able to reply sooner.

I've followed the steps that you wrote.

Here wat I did:
  • I've created a temp course.
  • Changed the orphan questions_categories to point to the temp course.
  • When I go to question bank I don't see anything. It is empty?
  • I've add questions and deleted them, but I still don't see the question_categories that I've added via database.
Do you know what could be wrong?

Leon
In reply to Leo Dil

Re: Health Problem: Question categories should belong to a valid context

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
That's weird. I suppose the problem might be with the parent categories. If the category is pointing ot a parent category that does not exist, then it might not show up in the UI. You could try setting the parent column to 0 for one of the categories, and see if that makes it show up.
In reply to Tim Hunt

Re: Health Problem: Question categories should belong to a valid context

by Leo Dil -
The parent id was already 0 and the context id of xxx_question_categories is the same as instance id of xxx_context.

Am I doning something wrong?
In reply to Leo Dil

Re: Health Problem: Question categories should belong to a valid context

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! xxx_question_categories.contextid needs to equal xxx_context.id, where contextlevel = 50 and instanceid = {the id of the temporary course you created}.
In reply to Tim Hunt

Re: Health Problem: Question categories should belong to a valid context

by Leo Dil -
So I've checked it and here is what I found:

xxx_question_categories.contextid needs to equal xxx_context.id, where contextlevel = 80 and instanceid = {is okay}.

So contextlevel is 80 and not 50. It this the problem?
In reply to Leo Dil

Re: Health Problem: Question categories should belong to a valid context

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Well, you need to use the appropriate contextlevel for the type of context. The context levels are defined at the top of cvs:/moodle/lib/accesslib.php.*) 80 is block context, which is almost certainly wrong.
In reply to Tim Hunt

Re: Health Problem: Question categories should belong to a valid context

by Leo Dil -
Should I update Moodle 1.9.7+?
I didn't use 80 but it was automatic. I have no idea how it got there.

In reply to Leo Dil

Re: Health Problem: Question categories should belong to a valid context

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Well, you should update Moodle to 1.9.7, not least because of recent security fixes. However, I don't think it will make much difference to this particular problem.

Can I repeat the basic advice:
  1. Create a new course called something like 'Rescue questions'.
  2. Work out the context id of that course. For example by going to the assign roles page and looking at the URL.
  3. For any question_categories in the database that currently point to a bogus contextid, change the context id to point to the Rescue questions course context. (Perhaps start by doing one at a time.)
In reply to Tim Hunt

Re: Health Problem: Question categories should belong to a valid context

by Leo Dil -
Hallo Tim,

I've followed the steps you mentioned and it is working fine.
I've talked with a college of mine about the problem. he says that the course that had a problem was originally made with 1.9.4.
So I think that this could be the problem. Anyhow 1.9.7+ is working okay.

Thanks for the help. Let me know if you need more info.

Leon
In reply to Leo Dil

Re: Health Problem: Question categories should belong to a valid context

by Toni Soto -

I experienced the same problem described in this post and I solved it as Tim suggested. Now I'm running 1.9.15 and I didn't check the 'health status' previously. I thinks problems come form older version (1.9.3 and/or 1.9.9 or 1.9.13). Anyway I can't imagine how this can happen. What it is supposed teachers would have made wrong to produce this problem..?

Toni

In reply to Tim Hunt

Re: Health Problem: Question categories should belong to a valid context

by Séverin Terrier -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators

Hi Tim,

If i understand well, all questions in these orphaned categories can't be used in Moodle, and are not used, and just take a small amount of space, and generate some bogus relations between tables (with possible problems to upgrade to more recent Moodle versions) ?

I've checked in (one of) my Moodle (1.9.x), and i've discovered 2 categories, one with 5 questions, and the other one with 2, and they have been made in september 2008 ! So, haven't been used from then, and can be deleted, if i've understand...

Just waiting for a confirmation before deleting, and making a test upgrade to Moodle 2.2.

Thanks
Séverin

In reply to Séverin Terrier

Re: Health Problem: Question categories should belong to a valid context

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

Yes, should be safe to delete. If you are paranoid, check quiz_qusetion_instances, and question_states, to ensure nothing it using those questions.

In reply to Tim Hunt

Re: Health Problem: Question categories should belong to a valid context

by Séverin Terrier -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators

Thanks Tim for this quick answer.

I'm not paranoid, but i like when things are in proper state wink

I had never digged in quiz and questions tables (so don't hesitate to correct me if i'm wrong), but taken time to have a look at this, with these requests (463 and 693 are Category Id's shown by health.php) :

SELECT qqi.id, qqi.quiz, qqi.question FROM mdl_quiz_question_instances qqi JOIN mdl_question q ON qqi.question=q.id WHERE q.category IN (463,693);

And found nothing, good.

SELECT qs.id, qs.attempt, qs.question, qs.answer FROM mdl_question_states qs JOIN mdl_question q ON qs.question=q.id WHERE q.category IN (463,693);

And found 18 occurrences.

Just have to delete them... Is the next delete command correct for that ?

DELETE FROM mdl_question_states WHERE question IN (SELECT q.id FROM mdl_question q WHERE q.category IN (463,693));

After that, i can modify  mdl_question_categories.contextid for faulting ones to match an existing course, and then delete properly related questions, and then the question categories, and all will be OK ?

Thanks in advance,
Séverin

In reply to Séverin Terrier

Re: Health Problem: Question categories should belong to a valid context

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

I don't think you should delete question states like that. You should read http://docs.moodle.org/dev/Question_database_structure

In reply to Tim Hunt

Re: Health Problem: Question categories should belong to a valid context

by Séverin Terrier -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators

I'm not really sure to understand exactly what you mean...

After reading this page, you say that, to clean everything, i would have to also clean question_sessions and question_attempts?

I think that, for the moment, i'll keep somewhere trace of id's for elements in question and question_categories before deleting them, and i'll look later at how to clean all necessary tables.

If you have complete queries for that, and/or more explanation, i'm also interested...

Séverin

In reply to Séverin Terrier

Re: Health Problem: Question categories should belong to a valid context

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

"to clean everything, i would have to also clean question_sessions and question_attempts?"

Yes, I think that is what I meant. Probably also quiz_attempts.

In reply to Tim Hunt

Re: Health Problem: Question categories should belong to a valid context

by Séverin Terrier -
Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Testers Picture of Translators

Ok. Thanks for that information.

In fact, as we finally have only backuped used courses, to restore them on a completly new Moodle 2.3 instance, it's not really important now...

But i will know, if i have to do it on another instance.

Séverin