## Quiz

### This is an unknown context () in print_context_name!

This is an unknown context () in print_context_name!

Everything was moving along happily until I decided to edit a quiz. I receive a message as follows:

This is an unknown context () in print_context_name!

Don't know what to do. I have:

1. Deleted the quiz and quiz results.
2. Created a brand new quiz.

I can create a new quiz but when I try to add questions I get the same message.

This is a clean installation of 1.9. Added a few packages but I have been editing quizes since then without a problem.

The only thing I did prior to this problem was switch to the Standard Logo theme. I've switched back to see if it solves the problem but nope.

I'm kind of in a real pinch. It's Saturday and I have to have all these quizes created by Monday morning. If anyone has any ideas I would truly appreciate it.

Average of ratings: -
Re: This is an unknown context () in print_context_name!

New piece of information. I can create a new quiz in a new course. So, evidently, something has gone bad with the course.

Also, tried the rolesdebug.php script and it collected nothing.

Average of ratings: -
Re: This is an unknown context () in print_context_name!
"Added a few packages"

Which packages? That would be were I would look. Try removing/disabling them one at a time and see if the problem goes away.

Average of ratings: -
Re: This is an unknown context () in print_context_name!

Well I have resolved my problem. I don't think it has anything to do with the packages I have added but I could be wrong.

I created a new course and copied everything from the existing course to the new one. It works fine now. I'll investigate the issue later but right now, I just have a lot of work to do.

Thanks for the comment!

Steve

Average of ratings: -
Re: This is an unknown context () in print_context_name!

Hi Steve and Tim,

I can report that I had the very same problem recently on my local Moodle install, version 1.9. Unfortunately I cannot remember the sequence of actions which led to this, but I did get the error message displayed whenever I tried to access the Questions databank:

This is an unknown context () in print_context_name!

Since this was my local test platform I simply deleted the course and created a new one. But it would have been more serious if this had happened on a production platform. What is baffling is that I have no idea why this message should be triggered by accessing Questions.

Joseph

Average of ratings: -
Re: This is an unknown context () in print_context_name!
This is acutally a minor problem to do with the roes system. What happens is that there should be a row in the mdl_context table for each course, modules etc. and you have one missing which is leading to error messages.

In the past, it was not like this. Entries in mdl_context did not have to exist. Whenever a context was referenced, the mdl_context entry was created if it did not already exist. However, that caused performance problems, so now the code assumes that all entries already exist, and the upgrade code to 1.9 should have created any missing entries.

To check that this is really the cause, try running the following query:

SELECT cm.* FROM mdl_course_modules cm LEFT JOIN mdl_context con ON cm.id = con.instanceid AND con.contextlevel = 70 WHERE con.id IS NULL;

It will list all the modules that are missing the corresponding context. This is relatively easy to fix, all the information you need is in other tables, it just needs to be linked together. In fact there is even a function to do it for you. Make a php script that does:

require_once('config.php');
create_contexts(CONTEXT_MODULE, true, true);

This should be completely safe, however, common sense suggests you should take a full backup before doing this.

Average of ratings: -
Re: This is an unknown context () in print_context_name!
I have tried what is suggested above:
<?
require_once('config.php');
create_contexts(CONTEXT_MODULE, true, true);
?>

the output was sucess several times to the screen, however, I still have the same issue.

I am running the lastest version of Moodle on Linux.

--B

Average of ratings: -
Re: This is an unknown context () in print_context_name!
Weird, so it must be a more subtle inconsistency between the mdl_course, mdl_course_modules and mdl_quiz tables. Problems like this do tend to be localised to one course, and so, as you found, creating a new course a moving all the content over fixes it.

Of course it should never happen in the first place, but unless we can pin down what actually caused the problem, there is very little we can do. Sorry.

Average of ratings: -
Re: This is an unknown context () in print_context_name!
Alrighty,

Some more information on this issue, as a simple backup and restore of the course does not work all the time.

I have a second moodle instance running the same version as my primary instance: 1.9. Build 2008331.

I backup a course and restore it on the other instance, I do not receive the context error when attempting to edit a quiz.

If I restore the same course from that instance on my primary instance, I still get the error. If I restore the course deleting previous course first, still same error, if I make it an entirely new course, same error.

Would it be helpful if I gave you the course, or looked at some specific logs?

This is the third course on my instance to have this problem since 1.9's upgrade.

Tell me what to do, and I am game.

--B

Average of ratings: -
Re: This is an unknown context () in print_context_name!
Hi,

unfortunately, I don't have the answer to your question, but I have the same problem and I remember the chain of events before this happened.

Clean install of Moodle 1.9, no modules/blocks/filters added. I created my first course on it and named it. I didn't add anything to it (apart from 2 labels) but placed it into a category. I then went way for the weekend. A couple of days after I decided to remove the category the course was in and also decided to rename the course. Once all this was done, I decided to add a quiz to the course, changed a few of the settings and then tried to add questions, bam the error appeared!
This was the first thing I did to my Moodle installation, so I don't think I "messed it up".

Hope it helps,

Fred

Average of ratings: -
Re: This is an unknown context () in print_context_name!
In my case, I am out of the box install,this instance has progressed through upgrades since Version 1.5, and is a production instance. It is time to start hacking code by the looks of it.

Tim, What table should I be looking at in the Moodle backup scripts? that would reference this context error? It will narrow down my searching.

Average of ratings: -
Re: This is an unknown context () in print_context_name!
I have been in mdl_quiz and mdl_context comparing a working quiz on another instance with a quiz with the problem noted above.

There seems to be no apparent different. Is there another table that is part of this or perhaps a file. The context table references a location in my data directory, would this be related to this error?

Average of ratings: -
Re: This is an unknown context () in print_context_name!
Hey Tim,

I have 25 courses now with this error and they have students in them with data. So creating a new shell for each course and emptying them doesn't help. I have created a second instance of 1.9 and moved them there, and the error goes with them if I restore the student data.

I am going to create a bug for this issue as I think more of these will arise as people upgrade through from 1.5.

--B

Average of ratings: -
Re: This is an unknown context () in print_context_name!
SELECT cm.* FROM mdl_course_modules cm LEFT JOIN mdl_context con ON cm.id = con.instanceid AND con.contextlevel = 70 WHERE con.id IS NULL;

Should that be 7 instead of 70?

Average of ratings: -
Re: This is an unknown context () in print_context_name!
Brent - 70 is correct. The contexts are defined in /lib/accesslib.php. Peace - Anthony

Average of ratings: -
Re: This is an unknown context () in print_context_name!
Hi all,

It looks like somehow data has been corrupted in your contexts table during your upgrade. I have written a short php script to debug some accesslib functions that we rely on in the question code. Please run this script and the output will give us more details of what has gone wrong. The script is completely safe to run, it doesn't alter anything in your db.

To run :

• download attached script and save it as contexttest.php in the root of your Moodle installation (the same folder as config.php).
• find out the course module id of the quizzes that are triggering errors by :
• opening the quiz and seeing what id appears in the url.
• Eg in http://localhost/workspace/moodle-HEAD/mod/quiz/view.php?id=5 The id is 5.
• Then run the script with param cmid= the course module id.
• The url will be {yourmoodlewwwroot}/contexttest.php?cmid={coursemodule id} Eg.
• In the above example it will be http://localhost/workspace/moodle-HEAD/contexttest.php?cmid=5
Hopefully this script will trigger some errors like you are seeing in your quizzes and the verbose output will help us work out exactly what the problem is.

Jamie

Average of ratings: -
Re: This is an unknown context () in print_context_name!
Running my script on your server will help pinpoint where the problem with paths is, in your db, for a particular quiz.

Average of ratings: -
Re: This is an unknown context () in print_context_name!
Jamie - I'd like to see if we could find a way of reproducing this so that we can track down the real source of the bug and get it fixed. Any help in doing that would be greatly appreciated. Peace - Anthony

Average of ratings: -
Re: This is an unknown context () in print_context_name!
Tim - It appears that there is some problem with paths, would this help resolve that by recalculating it? Peace - Anthony

Average of ratings: -
Re: This is an unknown context () in print_context_name!
Well done for tracking down that this seems to be to do with corrupt paths. If that is the only cause, then yes, as far as I know, executing

require_once('config.php');
create_contexts(CONTEXT_MODULE, true, true);

will fix it. (And if this does not work, you will have to ask someone more knowledgeable, like Martin Langhoff or Petr Skoda.) If editing categories is reproducibly corrupting paths, that is a bug that needs to be fixed - file it in the tracker with exact steps to reproduce.

Average of ratings: -
Re: This is an unknown context () in print_context_name!

hi

i have installed 1.9 moodle on windows xp..i added one course and in that one quizz..it worked well...then i added one more curse , in this i added some ppsx files and wav file....then again i tried to edit the quiz for update i got the same error.....the context one.....
you said to do

require_once('config.php');
create_contexts(CONTEXT_MODULE, true, true);

how to do this on windows?....will it fix the prob?

Average of ratings: -
Re: This is an unknown context () in print_context_name!
Hi Umesh,

Just put a file contextfix.php in the root of your web directory - in the same directory as your config.php with the following contents in it :

<?php
require_once('config.php');
create_contexts(CONTEXT_MODULE, true, true);
?>

And then go to http://{address of root of your moodle site}/contextfix.php with a browser. You won't see anything I think but this will run the script.

Jamie

Average of ratings: -
Re: This is an unknown context () in print_context_name!
Jamie - Do you think it would be helpful to create a fix_mod_contexts.php file in admin and just leave it there by default? We could add a capability check to make sure the person has admin privileges. I'm just wondering if it would be good to have it there. What do you think? Peace - Anthony

Average of ratings: -
Re: This is an unknown context () in print_context_name!

I have the same problem and I think I know how it is produced. My courses began doing this when i was using the Minted IMS package to install courses. Unfortunately there was a problem and I had to delete a bunch of catagories that wrote wrong due to a varchar(3) sql mistake, should have been (40). I deleted the categories which continually pushed courses into the parent category until I all courses were in Miscellaneous. Moving courses in and out of categories seems to create this problem in 1.9 linux. I have not checked but I think I have 1200 affected courses

Average of ratings: -
Re: This is an unknown context () in print_context_name!
Does the suggested fix above - making a script that calls the create_contexts function - fix it for you?

Average of ratings: -
Re: This is an unknown context () in print_context_name!

I think it will, this was a bit over my head and I turned it over to a programmer, he said the actual error was a table that was corrupted or something that prevented communication between question contexts...hmm I'm not real sure but the code he put in fixed this permanently and the site is working good. I will see if he will email me the code, and I will post it and you guys can compare.

Average of ratings: -
Re: This is an unknown context () in print_context_name!
It would be great to see the code. Thanks.

Average of ratings: -
Re: This is an unknown context () in print_context_name!

Below is How our programmer resolved this problem.

Hi! Chad, To answer your question I have narrowed the restore problems down to a couple of issues. First considering all quiz's and questions are in the database and not missing the following code hack seems to afford the restore procedure a better chance of restoring an archived course and it contents. Affects file: ./question/restorelib.php Code Line 649-656 //We have to recode the answer field //It depends of the question type !! //We get the question first if (!$question = get_record("question","id",$state->question)) { echo 'The record for question '.$state->question.' is missing! '; echo 'Check database table _question for id='.$state->question.'
'; continue; // Don't let this stop us from our course restore. I have also recently been working with another issue which happens only when one attempts to restore a quiz which is actually missing questions in the questions table. With the restore library the main issue is quizzes in general which always seem to break the restore if everything is not just peachy perfect. I have experimented with changes to function question_make_default_categories($contexts) where a condition exists that$context->contextlevel is actually null which will throw the restore error "This is an unknown context () in print_context_name!" which is generated from lib/accesslib.php function print_context_name. The trick is here is to always insure the \$context->contextlevel is not null but rather checked and set to a value during the restore process.

Average of ratings: -
Re: This is an unknown context () in print_context_name!

Hi.
I don't know what to do...

- First, I did a php file with

require_once('config.php');
create_contexts(CONTEXT_MODULE, true, true);

All was "success" (http://www.sallep.net/evaelasalle/contextfix.php9

But the problem is there yet...

- Then, I did a sql consult:
SELECT cm. *
FROM mdl_course_modules cm
LEFT JOIN mdl_context con ON cm.id = con.instanceid
AND con.contextlevel =70
WHERE con.id IS NULL
LIMIT 0 , 30;

This is the message:
MySQL ha devuelto un valor vacío (i.e., cero columnas). (La consulta tardó 0.0099 seg)

What can I do?

Thanks.

Average of ratings: -
Re: This is an unknown context () in print_context_name!

I have tried the contextfix.php suggestion too and got the same results. I still can't create or edit questons.

Average of ratings: -
Re: This is an unknown context () in print_context_name!

I have also got the problem with quizzes in our moodle.

"This is an unknown context () in print_context_name!"

I have tried the contextfix with:

require_once('config.php');
create_contexts(CONTEXT_MODULE, true, true);

.... and it has not improved our situation

All new quizzes give the error after creation & some old ones error upon viewing or editing. There are a handful that still work, and they are the quizzes that we first created a couple of years ago.

Is anyone getting closer to solving this one?

Tim

Average of ratings: -
Re: This is an unknown context () in print_context_name!

We also had this error message immediately after creating new course categories and moving existing courses into those categories.

After reading these posts, we moved the courses back to their previous categories and the problem was resolved.

Average of ratings: -
Re: This is an unknown context () in print_context_name!

We have seen this problem arise again with a couple courses, I will ask Remote Learner for the actual code they ran and post it here.

Average of ratings: -
Re: This is an unknown context () in print_context_name!

We also have this issue. Under further invertigation I looked into the mdl_context table and found most(10017 contexts out of 10476) to have a path value of /1/10/28/27/8709/6812/1/11/22/1964/9079/1/10/28/27/8709/6812/1/11/22/1964/9080/1/10/28/27/8709/6812/1/11/22/9245/9076/1/10/28/27/8709/6812/1/11/22/1964/9079/1/10/28/27/8709/6812/1/11/22/1964/9080/1/10/28/27/8709/6812/1/11/22/8388/8746/1/10/28/27/8709/6812

Surely all these contexts should have a different path.

Anyone got any suggestions whether this is correct or how to fix this??

Average of ratings: -
Re: This is an unknown context () in print_context_name!
I just had a problem with this today. I bunch of quizzes on one course page would display the "This is an unknown context () in print_context_name!" error.

Looking at mdl_context and comparing it to mdl_course_modules, I could see that all of the problematic quizzes had entries like this

id=481 contextlevel=70 instanceid=425 path= /1/3/471/481 depth=4

Since "471" was the common, next to last element, I looked for id=471 and found was missing. Putting all of the ids in order, I also saw that the id above had instanceid=417 and that below had instanceid=419, so clearly the data row for id=471 had been deleted.

I then added one more row to the db like this, and now everything works:

id=471 contextlevel=70 instanceid=418 path= /1/3/471 depth=3

You might be able to fix similar problems in an analogous way.

Cheers,
Tom

Average of ratings: -
Re: This is an unknown context () in print_context_name!

Hi

I have checked my courses and all courses below number 237 (1 - 236) do not work with the error :-

This is an unknown context () in print_context_name!

All courses above this number do work normally and go to question bank.

I tested this with : http://transylvania-college.ro/vle/question/edit.php?courseid=236  (and changed the course numbers on the end).

How can I repair the older (1 - 236) courses without removing and restoring them?

Thanks

Average of ratings: -
Re: The solution

El problema se debe a que los registros en la tabla mdl_context estan mal enlazados(en nuestro caso debido a los scripts con los que cargamos los cursos).
Lo hemos solucionado utilizando el script que mencionais,pero BORRANDO antes los registros de mdl_context,porque esta funcion no los actualiza.El script seria:

<?php
require_once('config.php');
cleanup_contexts();
create_contexts(null, true, true);
?>

Average of ratings: -
Re: This is an unknown context () in print_context_name!

I ran into this problem when I was creating courses by modifying the database directly. My mistake was not setting the mdl_context table information carefully.

You need to have an entry with a context level of 50 (for the course). The instanceid is the course id. The path has to include "/1" for the root, followed by the category context id, then the id of the current record.

To get the category context id, search the mdl_context for a record with a context level of 40 (for a category) with the instanceid of the category id.

I was putting the category id itself into the path, rather than its associated context id. This caused the error listed above.

Average of ratings: -
Re: This is an unknown context () in print_context_name!

This worked for me - thank to Tim Hunt's post on http://tracker.moodle.org/browse/MDL-12667

Tim Hunt added a comment - 20/Jun/08 12:06 AM
Right, so that will be the root of the problem. And actually that means it is a roles system problem. It would be good if Tormod and Madeleine are able to determine whether their problem comes from the same underlying cause.

There is a way to sort out all the context paths if they get screwed up. You write a script that says

<?php
require_once('config.php');
require_capability('moodle/site:doanything', get_context_instance(CONTEXT_SYSTEM));
build_context_path(true, true);
echo 'Done';
?>

call it something like fixpaths.php

then go to http://example.com/moodle/fixpaths.php to execute it.

Average of ratings: Useful (1)
תשובה ל: Re: This is an unknown context () in print_context_name!

After doing this

I'm getting errors in the participants page of a course:

Notice: Undefined index: in /var/www/html/moodle/lib/datalib.php on line 1008 Notice: Trying to get property of non-object in /var/www/html/moodle/lib/datalib.php on line 1008 Notice: Undefined index: 136 in /var/www/html/moodle/lib/datalib.php on line 1024 Notice: Trying to get property of non-object in /var/www/html/moodle/lib/datalib.php on line 1024
Incorrect context specified

line 376 of lib/accesslib.php: call to debugging()
line 1025 of lib/datalib.php: call to has_capability()
line 200 of user/index.php: call to get_my_courses()

Notice: Undefined index: 143 in /var/www/html/moodle/lib/datalib.php on line 1024 Notice: Trying to get property of non-object in /var/www/html/moodle/lib/datalib.php on line 1024
Incorrect context specified

line 376 of lib/accesslib.php: call to debugging()
line 1025 of lib/datalib.php: call to has_capability()
line 200 of user/index.php: call to get_my_courses

why? and how can I fix it?

Thank you

Average of ratings: -