The Musing
I had a faculty member contact me and let me know that when they clicked the ‘Grades’ heading in Moodle (5.0.4+), they got an exception: Attempt to assign property “sortorder” on null’. Big red exception, with no helpful message. The best they could tell me was that they were adjusting weights and adding grade items. Hmm…doesn’t sound terribly “dangerous”, does it?
I looked at the logs quite a bit, and noticed they had deleted some categories, too. Strangely, the name of the category was ” – also odd.
According to this forum post on Moodle.org, a grade item was in a grade category that didn’t exist. That’s got to be hard to do, right?
The Fix
I decided to see if the forum post was correct. Digging around in the Moodle DB, I decided to match up all of the grade items with their corresponding categories to see if there were any that was in a nonexistent category.
I used this SQL to see if anything was missing (courseid was 12456):
SELECT item.id,left(itemname,10) as name,itemtype,fullname AS catname
FROM mdl_grade_items AS item
LEFT JOIN mdl_grade_categories AS cat
ON categoryid=cat.id
WHERE itemtype!='course'
AND itemtype!='category'
AND item.courseid=12456;
The output was something like:
+--------+------------+----------+------------------------------+
| id | name | itemtype | catname |
+--------+------------+----------+------------------------------+
| 159125 | Syllabus Q | mod | Journal / Forum / Quizes |
| 159993 | Chapter 4 | mod | Journal / Forum / Quizes |
| 160213 | Icebreaker | manual | Journal / Forum / Quizes |
| 160214 | Anatomy of | manual | Journal / Forum / Quizes |
| 160374 | Group Time | mod | Group Timeline Project |
| 160387 | Playwright | mod | Playwright Presentation |
| 160422 | Kendrick C | mod | Journal / Forum / Quizes |
| 160658 | Week 4 Qui | mod | Journal / Forum / Quizes |
| 160679 | Julius Cae | mod | Study Guide 1, Julius Caesar |
| 160680 | Group Time | mod | Group Timeline Project |
| 160770 | Julius Cae | manual | Journal / Forum / Quizes |
| 161005 | Week 5 Qui | mod | Journal / Forum / Quizes |
| 161006 | Julius Cae | mod | Study Guide 1, Julius Caesar |
| 161167 | Week 6 Qui | mod | Journal / Forum / Quizes |
| 161319 | Week 7 Sli | mod | Journal / Forum / Quizes |
| 161517 | Julius Cae | mod | Study Guide 1, Julius Caesar |
| 161518 | Playwright | mod | Playwright Presentation |
+--------+------------+----------+------------------------------+
Notice in the ‘catname’ column, every item seems to have a valid category name. Hmm…doesn’t seem like we have the same problem as the poster from the Moodle forums.
Next, I started looking at the categories themselves. Was something screwy about them? Remember, from the logs, I did notice that the instructor had deleted a category just before emailing me.
I tried to match up all the grade items that were categories with their detail from the mdl_grade_categories table using this SQL:
SELECT items.id,iteminstance,fullname
FROM mdl_grade_items AS items
LEFT JOIN mdl_grade_categories AS cats
ON items.iteminstance=cats.id
WHERE items.courseid=12456
AND itemtype='category';
This gave me a more interesting result:
+--------+--------------+------------------------------+
| id | iteminstance | fullname |
+--------+--------------+------------------------------+
| 160211 | 25608 | Journal / Forum / Quizes |
| 160375 | 25626 | Group Timeline Project |
| 160767 | 25745 | Study Guide 1, Julius Caesar |
| 160768 | 25746 | Playwright Presentation |
| 161884 | 25609 | NULL |
| 161885 | 25866 | Two Group Plays |
+--------+--------------+------------------------------+
Aha! Look at that suspect row with id 161884. NULL for the fullname? Looking at the mdl_grade_categories table, I found this:
SELECT * FROM mdl_grade_categories WHERE id=25609;
Empty set (0.000 sec)
Now we have it. There is a grade item (id 161884) that is a category, and the corresponding entry in the grade_categories table (id 25609) doesn’t exist! I bet that will cause issues.
I searched back through all of the other grade categories for course 12456, to make sure the category 25609 isn’t referenced in the path or parent fields:
SELECT id,parent,path
FROM mdl_grade_categories
WHERE courseid=12456;
Result:
+-------+--------+---------------+
| id | parent | path |
+-------+--------+---------------+
| 25081 | NULL | /25081/ |
| 25608 | 25081 | /25081/25608/ |
| 25626 | 25081 | /25081/25626/ |
| 25745 | 25081 | /25081/25745/ |
| 25746 | 25081 | /25081/25746/ |
| 25866 | 25081 | /25081/25866/ |
+-------+--------+---------------+
Nope. No 25609 anywhere. I did something similar with the grade_items table:
SELECT id,categoryid,itemtype,iteminstance
FROM mdl_grade_items
WHERE courseid=12456;
Result:
+--------+------------+----------+--------------+
| id | categoryid | itemtype | iteminstance |
+--------+------------+----------+--------------+
| 156813 | NULL | course | 25081 |
| 159125 | 25608 | mod | 17226 |
| 159993 | 25608 | mod | 17375 |
| 160211 | NULL | category | 25608 |
| 160213 | 25608 | manual | NULL |
| 160214 | 25608 | manual | NULL |
| 160374 | 25626 | mod | 73528 |
| 160375 | NULL | category | 25626 |
| 160387 | 25746 | mod | 73537 |
| 160422 | 25608 | mod | 17396 |
| 160658 | 25608 | mod | 17410 |
| 160679 | 25745 | mod | 73634 |
| 160680 | 25626 | mod | 73635 |
| 160767 | NULL | category | 25745 |
| 160768 | NULL | category | 25746 |
| 160770 | 25608 | manual | NULL |
| 161005 | 25608 | mod | 17457 |
| 161006 | 25745 | mod | 73773 |
| 161167 | 25608 | mod | 17468 |
| 161319 | 25608 | mod | 17480 |
| 161517 | 25745 | mod | 74042 |
| 161518 | 25746 | mod | 74043 |
| 161884 | NULL | category | 25609 |
| 161885 | NULL | category | 25866 |
+--------+------------+----------+--------------+
I couldn’t find any occurrences of 25609 other than the row with id 161884. So – this is on a test site, mind you – I decided to delete the entry from mdl_grade_items!
DELETE FROM mdl_grade_items
WHERE id=161884;
I refreshed the grades pages and voilĂ ! The nasty red error was gone. I had to recalculate the grades – wonder if that was somehow a contributing factor – but then everything showed up as it should.
I replicated my steps on the live site, held my breath, deleted the row, and had the same result.
I’m not sure why the category wasn’t deleted cleanly (or wasn’t created cleanly?), but it seems to be working now, with no ill effects.