Moodle
  1. Moodle
  2. MDL-14290

Paths sometimes get corrupted in the mdl_context table

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.9
    • Fix Version/s: 1.9.2
    • Component/s: Roles / Access
    • Labels:
      None
    • Environment:
    • Database:
      MySQL
    • Affected Branches:
      MOODLE_19_STABLE
    • Fixed Branches:
      MOODLE_19_STABLE
    • Rank:
      31062

      Description

      Instance History:

      Upgraded from 1.5 -> 1.6 -> 1.8 ->1.9 -> 1.9 Stable

      1/3 of courses on instance have the following error when an instructor clicks a Quiz:

      This is an unknown context () in print_context_name!

      If I login as a student, I can still enter quizzes, and make attempts. If I attempt to login as instructor, it fails.

      I can provide a backup of the course, and access to my copy of the instance, and mysql if it helps to debug.

        Activity

        Hide
        Anthony Borrow added a comment -

        OK - it seems to be in quizzes with no questions. When there is a redirect to edit, somewhere there is a call to print_context_name that is passing a null value. I added a var_dump($context) at the beginning of the function. Now to figure out where it is being called from. Peace - Anthony

        Show
        Anthony Borrow added a comment - OK - it seems to be in quizzes with no questions. When there is a redirect to edit, somewhere there is a call to print_context_name that is passing a null value. I added a var_dump($context) at the beginning of the function. Now to figure out where it is being called from. Peace - Anthony
        Hide
        Anthony Borrow added a comment -

        Somehow it seems that the path got munged in Brent's database in mdl_context. It added an extra context which did not exist. In Brent's case it was listed as 1/9/19/1497/6124 where 1 was the frontpage (1, contextlevel 10), 19 was the category (6, contextlevel 40), 1497 was the course (45, contextlevel 50), and 6124 was the course module (5858, contextlevel 70). Brent reports that he had previously had the courses nested in another category (which was probably context id 9). When that course category was deleted somehow the context paths were not updated. To fix Brent's problems, I executed:

        UPDATE mdl_context
        SET path = replace(path,'/9/','/')
        WHERE path LIKE '%/9/%';

        Others experiencing this problem will need to identify the context in the path that does not exist and remove it from the path. I would not recommend attempting this without a good backup and understanding the relationship between the context table and the other Moodle tables - this is not for the faint of heart. We should probably work toward figuring out what was causing this. I vaguely recall an issue with nested categories. Also, thanks to Brett for sharing a test server with production data that allowed me to dig into this a little deeper.

        Peace - Anthony

        Show
        Anthony Borrow added a comment - Somehow it seems that the path got munged in Brent's database in mdl_context. It added an extra context which did not exist. In Brent's case it was listed as 1/9/19/1497/6124 where 1 was the frontpage (1, contextlevel 10), 19 was the category (6, contextlevel 40), 1497 was the course (45, contextlevel 50), and 6124 was the course module (5858, contextlevel 70). Brent reports that he had previously had the courses nested in another category (which was probably context id 9). When that course category was deleted somehow the context paths were not updated. To fix Brent's problems, I executed: UPDATE mdl_context SET path = replace(path,'/9/','/') WHERE path LIKE '%/9/%'; Others experiencing this problem will need to identify the context in the path that does not exist and remove it from the path. I would not recommend attempting this without a good backup and understanding the relationship between the context table and the other Moodle tables - this is not for the faint of heart. We should probably work toward figuring out what was causing this. I vaguely recall an issue with nested categories. Also, thanks to Brett for sharing a test server with production data that allowed me to dig into this a little deeper. Peace - Anthony
        Hide
        Anthony Borrow added a comment -
        Show
        Anthony Borrow added a comment - This issue is discussed at: http://moodle.org/mod/forum/discuss.php?d=93656
        Hide
        Tim Hunt added a comment -

        So the actual bug is that the path field sometimes gets corrupted in the mdl_context table. However, unless we can reproduce the cause, there is not a lot that can be done.

        The cause seems to be something to do with editing course categories, perhaps deleting one - when the courses are then automatically moved to some other category.

        Show
        Tim Hunt added a comment - So the actual bug is that the path field sometimes gets corrupted in the mdl_context table. However, unless we can reproduce the cause, there is not a lot that can be done. The cause seems to be something to do with editing course categories, perhaps deleting one - when the courses are then automatically moved to some other category.
        Hide
        Anthony Borrow added a comment -

        Eloy - I was thinking, but did not have a chance to implement, about possibly creating a script that would essentially loop through the contexts, explode the paths, and then verify that each path segment (i.e. context) actually exists in the table. If not, remove that segment from the path. I think that re-creating the issue might be difficult as it may have been fixed with some of the changes in upgrades and handling of multi-depth course categories. Having a script that would fix the corrupted data in cases where it does occur might be helpful. Does that make sense? Peace - Anthony

        Show
        Anthony Borrow added a comment - Eloy - I was thinking, but did not have a chance to implement, about possibly creating a script that would essentially loop through the contexts, explode the paths, and then verify that each path segment (i.e. context) actually exists in the table. If not, remove that segment from the path. I think that re-creating the issue might be difficult as it may have been fixed with some of the changes in upgrades and handling of multi-depth course categories. Having a script that would fix the corrupted data in cases where it does occur might be helpful. Does that make sense? Peace - Anthony
        Hide
        Petr Škoda added a comment -

        We already have such function - see build_context_path()

        Show
        Petr Škoda added a comment - We already have such function - see build_context_path()
        Hide
        Anthony Borrow added a comment -

        Petr - I saw Tim Hunt had mentioned that in a post but had not followed up to see what it did. Thanks for staying a step ahead of me

        Show
        Anthony Borrow added a comment - Petr - I saw Tim Hunt had mentioned that in a post but had not followed up to see what it did. Thanks for staying a step ahead of me
        Hide
        Sharon Goodson added a comment -

        I don't know if this directly is related, and the issue is resolved for us, but it's so unusual I thought I would mention it. The information about the behavior might be useful to someone. I posted a description of what happened:
        http://moodle.org/mod/forum/discuss.php?d=111714#p490355

        Show
        Sharon Goodson added a comment - I don't know if this directly is related, and the issue is resolved for us, but it's so unusual I thought I would mention it. The information about the behavior might be useful to someone. I posted a description of what happened: http://moodle.org/mod/forum/discuss.php?d=111714#p490355
        Hide
        Sharon Goodson added a comment -

        Actually, now we're in a really bad way. We're about to have to shut down. Teachers and Admin can't access what they should and students are accessing admin items. Our roles keep getting bounced around, sometimes we can access, sometimes not. I have 31 entries in mdl_context that have: id 0, path NULL, depth 0. The first one is context level 10, the remaining are context level 30. The instanceids are 0-30 respectively.

        I did discover twice yesterday that the table was corrupted, and repaired it twice. Where these entries created then maybe? Can I delete them? Please Advise!! Thanks!

        Show
        Sharon Goodson added a comment - Actually, now we're in a really bad way. We're about to have to shut down. Teachers and Admin can't access what they should and students are accessing admin items. Our roles keep getting bounced around, sometimes we can access, sometimes not. I have 31 entries in mdl_context that have: id 0, path NULL, depth 0. The first one is context level 10, the remaining are context level 30. The instanceids are 0-30 respectively. I did discover twice yesterday that the table was corrupted, and repaired it twice. Where these entries created then maybe? Can I delete them? Please Advise!! Thanks!
        Hide
        Sharon Goodson added a comment -

        sorry to post again, but it seems, no, I can't delete them. When I try to edit, I get "MySQL returned an empty result set (i.e. zero rows)." When I try to delete, I get nothing, it returns and the entry remains. I exported the table to my computer and the 31 'id 0/NULL' entries are not there (presumably because they are empty?)

        Oddly enough, I can only see the entries when I sort by 'contextlevel,' (not by any other column), and then OTHER entries I just saw are not there.

        For example: When I do NOT sort , my first entry is -
        id 1, contextlevel 10, instanceid 0, path /1, depth 1

        but when I sort by context level it is not there, instead I see -
        id 0, contextlevel 10, instanceid 0, path NULL, depth 0.

        Show
        Sharon Goodson added a comment - sorry to post again, but it seems, no, I can't delete them. When I try to edit, I get "MySQL returned an empty result set (i.e. zero rows)." When I try to delete, I get nothing, it returns and the entry remains. I exported the table to my computer and the 31 'id 0/NULL' entries are not there (presumably because they are empty?) Oddly enough, I can only see the entries when I sort by 'contextlevel,' (not by any other column), and then OTHER entries I just saw are not there. For example: When I do NOT sort , my first entry is - id 1, contextlevel 10, instanceid 0, path /1, depth 1 but when I sort by context level it is not there, instead I see - id 0, contextlevel 10, instanceid 0, path NULL, depth 0.
        Hide
        Eloy Lafuente (stronk7) added a comment -

        Hi Sharon,

        it's really strange to have one record with id = 0 in any Moodle table. In fact, it's near impossible to have it, because they are auto-introduced fields always starting with 1.

        In any case, if you've all the contexts in the table... it's safe to execute the build_context_path(true) function in order to get all the depths and paths for those contexts updated/fixed.

        But something tells me that something is wrong in that table. Only 30 records looks like a small number (only very-small sites can have such reduced number of contexts - it's at least the sum of number of categories + number of courses + number of activities + number of users).

        Cannot you check some backup of your database (before repair) and see if the records in that table used to be also only 30?

        Ciao

        Show
        Eloy Lafuente (stronk7) added a comment - Hi Sharon, it's really strange to have one record with id = 0 in any Moodle table. In fact, it's near impossible to have it, because they are auto-introduced fields always starting with 1. In any case, if you've all the contexts in the table... it's safe to execute the build_context_path(true) function in order to get all the depths and paths for those contexts updated/fixed. But something tells me that something is wrong in that table. Only 30 records looks like a small number (only very-small sites can have such reduced number of contexts - it's at least the sum of number of categories + number of courses + number of activities + number of users). Cannot you check some backup of your database (before repair) and see if the records in that table used to be also only 30? Ciao
        Hide
        Sharon Goodson added a comment -

        Hi Eloy! Thanks for the response. I didn't mean to indicate I had only 31 entries, I have more like 3600 entries, but 31 of them were displaying 0/NULL. I managed to eliminate that by optimizing, etc. the table, but I'm still having major issues, including security because students are getting free run of the site. We're in a semi-permanent maintenance mode.

        Admin are suddenly treated as non-admin, and after preforming a function, usually roles-related, but not always, we are redirected to My Moodle. If we wait long enough, I can log back in as admin, but eventually, we're bounced back to non-admin. Likewise, students have been directed to the front page instead of My Moodle.

        In a forum post I noted what I thought was a resolved issue, http://moodle.org/mod/forum/discuss.php?d=111714#p490355 (noted above) , but it has resurfaced in an even worse way. When clicking on 'assign roles' and/or 'grades from a course page, we are redirected to some other roles assign page, often 'assign system roles,' and sometimes various blocks or other course. Depending on what we're doing, sometimes we get 'Invalid course ID.'

        A very odd thing is that half the time the link will be correct, then when you go back to check again it's back to the incorrect link (which tends to remain the same incorrect link).

        In defining roles, sometimes we can make changes and they take, sometimes it comes back with a 'error/admin/sectionerror' page, sometimes a 'you don't have permission.' After that, we are always bounced back to a My Moodle page.

        I want to post this to the forum because we are in desperate need of help, but I haven't a clue how to explain this phenomenon. We were supposed to be moving to a Moodle Partner this month, but we've been unable to reach them. We were hoping they might know what to do. Our current host is no help, they suggested it was a Moodle problem and we needed to go to MySQL and look at our database (duh!).

        Any help would be most appreciated!

        Show
        Sharon Goodson added a comment - Hi Eloy! Thanks for the response. I didn't mean to indicate I had only 31 entries, I have more like 3600 entries, but 31 of them were displaying 0/NULL. I managed to eliminate that by optimizing, etc. the table, but I'm still having major issues, including security because students are getting free run of the site. We're in a semi-permanent maintenance mode. Admin are suddenly treated as non-admin, and after preforming a function, usually roles-related, but not always, we are redirected to My Moodle. If we wait long enough, I can log back in as admin, but eventually, we're bounced back to non-admin. Likewise, students have been directed to the front page instead of My Moodle. In a forum post I noted what I thought was a resolved issue, http://moodle.org/mod/forum/discuss.php?d=111714#p490355 (noted above) , but it has resurfaced in an even worse way. When clicking on 'assign roles' and/or 'grades from a course page, we are redirected to some other roles assign page, often 'assign system roles,' and sometimes various blocks or other course. Depending on what we're doing, sometimes we get 'Invalid course ID.' A very odd thing is that half the time the link will be correct, then when you go back to check again it's back to the incorrect link (which tends to remain the same incorrect link). In defining roles, sometimes we can make changes and they take, sometimes it comes back with a 'error/admin/sectionerror' page, sometimes a 'you don't have permission.' After that, we are always bounced back to a My Moodle page. I want to post this to the forum because we are in desperate need of help, but I haven't a clue how to explain this phenomenon. We were supposed to be moving to a Moodle Partner this month, but we've been unable to reach them. We were hoping they might know what to do. Our current host is no help, they suggested it was a Moodle problem and we needed to go to MySQL and look at our database (duh!). Any help would be most appreciated!
        Hide
        Petr Škoda added a comment -

        I think that the problem Brent reported might have been already fixed - there were some problems with contexts of questions - should be fixed now.

        Problem reported by Sharon seems completely different - I do not think this is a PHP problem, I suppose it is a server issue - unfortunately we can not confirm that, the only way is to take the database data + files and install it on another computer.

        Petr

        Show
        Petr Škoda added a comment - I think that the problem Brent reported might have been already fixed - there were some problems with contexts of questions - should be fixed now. Problem reported by Sharon seems completely different - I do not think this is a PHP problem, I suppose it is a server issue - unfortunately we can not confirm that, the only way is to take the database data + files and install it on another computer. Petr
        Hide
        Tim Hunt added a comment -

        Something is definitely badly wrong, but it is probably your database. Perhaps some of the other tables related to mdl_context are corrupted too. I would run the most thorough check you can of everything in your database. If you are seeing weird things with rows depending on the sort order, perhaps it is an index that is corrupted, not the table? I am guessing a bit here - I am not a MySQL expert.

        Or of things are this badly wrong, perhaps it is worth running a thorough check of the file system and the hard disc. I think you need to start eliminating possibilities.

        Show
        Tim Hunt added a comment - Something is definitely badly wrong, but it is probably your database. Perhaps some of the other tables related to mdl_context are corrupted too. I would run the most thorough check you can of everything in your database. If you are seeing weird things with rows depending on the sort order, perhaps it is an index that is corrupted, not the table? I am guessing a bit here - I am not a MySQL expert. Or of things are this badly wrong, perhaps it is worth running a thorough check of the file system and the hard disc. I think you need to start eliminating possibilities.
        Hide
        Sharon Goodson added a comment -

        Yes, I believe it is a database problem too. Sorry for posting somewhere the post probably didn't belong (but where WOULD this belong lol...if I don't laugh, I'll cry). I also suspect (at least the possibility of) a server-related issue. Hard drive failure? memory leak? Don't know - after three calls, the host denies any recent changes or problems. We want to do exactly as Petr says, but we've had no luck getting a hold of the MP we were planning to move to since this started. I hesitate to use my personal XP as a server to test - I don't test it.

        Most unfortunately, I'm not a MySQL expert either, not even an amateur. I did check the other tables. None report being corrupted (at least when accessing them). And I did look more closely at tables related to context and roles (though I did not do the 'sort' thing on all the columns).

        All of the roles tables (except mdl_roles) give the warning "UNIQUE and INDEX keys should not both be set for column..." But I don't have a clue how to address that, and there have been a number of tables from day one giving that warning. It seemed to be a none issue (except maybe performance?)

        I discovered late last night there was a slight pattern in the chaos. When clicking on users from 'browse users,' and going to 'roles,' One Admin had two teachers and two students assigned to him as user role. I removed these (then needed up back in My Moodle with no privileges, went through that silly processes I described in the forum). When I logged back in with admin rights, the removal had taken. Then I saw several admin had themselves assigned as admin in their user role. Then going through every teacher and student, when clicking roles from user profile, I was taken to either Assign System Roles OR Assign Roles in Math X. Always Math X, never another class. And there were random users already assigned, it changed depending on the user I was checking. Going to Math X, there were no roles assigned. On a rare occasion, the proper assign roles page appeared.

        Again, the database values SEEM correct, except the ID=0/NULL in mdl_context, which would continue to sneak in, and I continued to repair/optimize. The tables are so complex (to someone like me) and integrated though, who knows what I'm missing.

        It appears this started in some fashion with the HTML image source in an assignment description that pointed to a PDF file instead of an image (see forum link above).

        NOW - what strikes me as most odd (besides the whole thing) at this point is:
        a) The user who made the PDF mistake is the same user who created the Math X course above (after I corrected the PDF mistake)
        b) The user above is the ONLY person who had other users assigned to him in his profile user roles
        c) While redirects do go to other pages, they overwhelming go to the Math X pages...

        Thanks for the assistance. Anthony has also been kind enough to offer to take a look at things. I'll report back. If I need to post somewhere else, please let me know.

        Show
        Sharon Goodson added a comment - Yes, I believe it is a database problem too. Sorry for posting somewhere the post probably didn't belong (but where WOULD this belong lol ...if I don't laugh, I'll cry). I also suspect (at least the possibility of) a server-related issue. Hard drive failure? memory leak? Don't know - after three calls, the host denies any recent changes or problems. We want to do exactly as Petr says, but we've had no luck getting a hold of the MP we were planning to move to since this started. I hesitate to use my personal XP as a server to test - I don't test it. Most unfortunately, I'm not a MySQL expert either, not even an amateur. I did check the other tables. None report being corrupted (at least when accessing them). And I did look more closely at tables related to context and roles (though I did not do the 'sort' thing on all the columns). All of the roles tables (except mdl_roles) give the warning "UNIQUE and INDEX keys should not both be set for column..." But I don't have a clue how to address that, and there have been a number of tables from day one giving that warning. It seemed to be a none issue (except maybe performance?) I discovered late last night there was a slight pattern in the chaos. When clicking on users from 'browse users,' and going to 'roles,' One Admin had two teachers and two students assigned to him as user role. I removed these (then needed up back in My Moodle with no privileges, went through that silly processes I described in the forum). When I logged back in with admin rights, the removal had taken. Then I saw several admin had themselves assigned as admin in their user role. Then going through every teacher and student, when clicking roles from user profile, I was taken to either Assign System Roles OR Assign Roles in Math X. Always Math X, never another class. And there were random users already assigned, it changed depending on the user I was checking. Going to Math X, there were no roles assigned. On a rare occasion, the proper assign roles page appeared. Again, the database values SEEM correct, except the ID=0/NULL in mdl_context, which would continue to sneak in, and I continued to repair/optimize. The tables are so complex (to someone like me) and integrated though, who knows what I'm missing. It appears this started in some fashion with the HTML image source in an assignment description that pointed to a PDF file instead of an image (see forum link above). NOW - what strikes me as most odd (besides the whole thing) at this point is: a) The user who made the PDF mistake is the same user who created the Math X course above (after I corrected the PDF mistake) b) The user above is the ONLY person who had other users assigned to him in his profile user roles c) While redirects do go to other pages, they overwhelming go to the Math X pages... Thanks for the assistance. Anthony has also been kind enough to offer to take a look at things. I'll report back. If I need to post somewhere else, please let me know.
        Hide
        Brent Lee added a comment -

        Petr is correct. Our problem was solved right after the bug was submitted. Since then anytime I have seen the bug, I have run Anthony's statement listed higher up in this bug.

        Have a great Holiday!

        --Brent

        Show
        Brent Lee added a comment - Petr is correct. Our problem was solved right after the bug was submitted. Since then anytime I have seen the bug, I have run Anthony's statement listed higher up in this bug. Have a great Holiday! --Brent
        Hide
        Tim Hunt added a comment -

        Sharon, a better place to get help is probably from the forums at http://moodle.org/course/view.php?id=5. Perhaps the General Problems forum is the most appropriate.

        Show
        Tim Hunt added a comment - Sharon, a better place to get help is probably from the forums at http://moodle.org/course/view.php?id=5 . Perhaps the General Problems forum is the most appropriate.
        Hide
        Petr Škoda added a comment -

        closing, because the original problem was fixed long ago, please use forum for trying to find the cause of the second problem mentioned here

        thanks for the report

        Show
        Petr Škoda added a comment - closing, because the original problem was fixed long ago, please use forum for trying to find the cause of the second problem mentioned here thanks for the report
        Hide
        Tim Hunt added a comment -

        Agree. Closing.

        Show
        Tim Hunt added a comment - Agree. Closing.

          People

          • Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: