Course categories added to the database don't show in UI

Course categories added to the database don't show in UI

by David Parker -
Number of replies: 10

Hello,

I wrote a custom PHP script to create course categories from a CSV file by directly inserting records into the mdl_course_categories table.  The script seems to work fine, but the categories added by the script do not appear under "Site administration" > "Courses" > "Add/edit courses".  We're using Moodle 2.5.

A little more detail: I created a top-level category called "UC" which all of the new categories are created under (some are nested more deeply than others, but all have "UC" as the top-most category).  Here's what shows up in the database:

mysql> SELECT * FROM mdl_course_categories WHERE path LIKE '/17%';
+-----+-------------------+---------------+-------------+-------------------+--------+-----------+-------------+---------+------------+--------------+-------+-----------------+-------+
| id  | name              | idnumber      | description | descriptionformat | parent | sortorder | coursecount | visible | visibleold | timemodified | depth | path            | theme |
+-----+-------------------+---------------+-------------+-------------------+--------+-----------+-------------+---------+------------+--------------+-------+-----------------+-------+
|  17 | UC                | UC            |             |                 1 |      0 |    150000 |           0 |       1 |          1 |   1386350352 |     1 | /17             | NULL  |
| 671 | Fall 2013         | 201380        |             |                 1 |     17 |    160000 |           0 |       1 |          1 |   1386350352 |     2 | /17/671         | NULL  |
| 672 | Arts and Sciences | 201380-AS     |             |                 1 |    671 |    161000 |           0 |       1 |          1 |   1386350352 |     3 | /17/671/672     | NULL  |
| 673 | ANT               | 201380-AS-ANT |             |                 1 |    672 |    161100 |           0 |       1 |          1 |   1386350352 |     4 | /17/671/672/673 | NULL  |
| 674 | ARB               | 201380-AS-ARB |             |                 1 |    672 |    161200 |           0 |       1 |          1 |   1386350352 |     4 | /17/671/672/674 | NULL  |
| 675 | BIO               | 201380-AS-BIO |             |                 1 |    672 |    161300 |           0 |       1 |          1 |   1386350352 |     4 | /17/671/672/675 | NULL  |
| 676 | CHE               | 201380-AS-CHE |             |                 1 |    672 |    161400 |           0 |       1 |          1 |   1386350352 |     4 | /17/671/672/676 | NULL  |
| 677 | COM               | 201380-AS-COM |             |                 1 |    672 |    161500 |           0 |       1 |          1 |   1386350352 |     4 | /17/671/672/677 | NULL  |
| 678 | CSC               | 201380-AS-CSC |             |                 1 |    672 |    161600 |           0 |       1 |          1 |   1386350352 |     4 | /17/671/672/678 | NULL  |
| 679 | ENG               | 201380-AS-ENG |             |                 1 |    672 |    161700 |           0 |       1 |          1 |   1386350352 |     4 | /17/671/672/679 | NULL  |
| 680 | ENV               | 201380-AS-ENV |             |                 1 |    672 |    161800 |           0 |       1 |          1 |   1386350352 |     4 | /17/671/672/680 | NULL  |
+-----+-------------------+---------------+-------------+-------------------+--------+-----------+-------------+---------+------------+--------------+-------+-----------------+-------+
11 rows in set (0.00 sec)

Even after logging out and clearing my browser cache, I still only see the top-level "UC" category and none of the new sub-categories.  Is there something I'm missing? Any help or general pointers would be greatly appreciated.

Thanks!

 

Average of ratings: -
In reply to David Parker

Re: Course categories added to the database don't show in UI

by scott braithwaite -

Hi David I am having a very similar problem I am entering the new categories into the database through 

 return $DB->insert_record('course_categories', $record); and this enters into the database correctly but does not show up whatso ever in the user interface.

 

Have you found a solution to this problem at all, if so could you help me out here as my lead developer is off work at present and have a major deadline to hit and now trying to do it without the main moodle structure expert in the office

In reply to scott braithwaite

Re: Course categories added to the database don't show in UI

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

fix_course_sortorder() ?

Average of ratings: Useful (2)
In reply to Davo Smith

Re: Course categories added to the database don't show in UI

by scott braithwaite -

will have a look into this now davo as have not used this before but will it be relevant for course_categories or would it likely be coursecat or something similar _sortorder()

In reply to David Parker

Re: Course categories added to the database don't show in UI

by scott braithwaite -

David Davo has solved this for both of us if you are having the same problem as me just place it straight after your insert query or if you are manually inserting on one of your first pages to load up possible front-page

In reply to scott braithwaite

Re: Course categories added to the database don't show in UI

by David Parker -

Thanks for the feedback.  Actually, I just found a solution two days ago, but I didn't have the chance to update the forum thread yet.  I discovered that simply adding one course to one of the new categories caused them all to show up.  I have no idea why, but it worked, and I was able to reproduce this behavior over and over again.  Based on the solution Davo provided, my best guess is that the course creation process calls the fix_course_sortorder() function.  Good to know!  Thanks again!

In reply to David Parker

Re: Course categories added to the database don't show in UI

by scott braithwaite -

What happens though david if you have created the new category and you are using custom menus which read the database to create courses as we are completely overhauling how moodle looks with using simple icon viewed layout, and custom forms which enter the data.  Or even from the point of using the existing moodle setup what if you are just getting the categories ready but are not putting the courses in yet then until you add a course inside this you can not get it to display and also how do you add the course without using the database or coding if you can't see the category at the backend.  

 

Just wondering on these points as I can see potential problems with this method but if my mind could be put at ease then it would be something which I would definately be interested in considering this method.

In reply to scott braithwaite

Re: Course categories added to the database don't show in UI

by David Parker -

We're using the stock UI, so we didn't need to worry about showing these categories in a custom view.  But I should have mentioned that the new subcategories did show up under the parent if you clicked on it in the list, but they did not show in the list on the main screen for adding/editing courses and categories.  I didn't realize this was true when I wrote my original post.  So, I was able to click down through the new subcategories and manually create a course in one of them, which instantly caused all of them to show in the list.

We're using a custom script to create the categories, and another to load courses into Moodle.  I'm going to update our course loader to call fix_course_sortorder() after each course is created, and see if that does the trick when this is entirely automated.

In reply to David Parker

Re: Course categories added to the database don't show in UI

by Chris Dreyer -

Hi

Can anybody direct me to a the right place on the forums with sample code / script and sample upload file what David has done? I am about to do create categories for a national project which mount to about 700 subjects and preparing an upload file will surely speed up creating them manually.

Any advice will help.

Thanks

Chris