To be able to create more complex queries, you have to understand the database in the background. This lesson is very important if you don't really know how databases work or if you are not familiar with the Moodle database at all.
As I said earlier, we are going to find specific forums on our Moodle site, where our teachers, developers, course admins are most probably misusing the 'news' forum feature. Before you continue, please read the previous chapter here.
We used the following code previously and we are going to extend this code.
What happens if there is a forum on our anomaly report? We have to find it on the website and change it or delete it or do something else.
Right now we only know the courseID. So we have found the 6th forum in the 6th course called 'News forum'.
As I knew where to find it in real, this is the actual URL to the forum:
What is the number at the very end of the URL? Why is it 78? Where is it in the database exactly? We will see soon. We have to have a look at another table called 'mdl_course_modules'.
As you can see, there is one row where all the previously discovered numbers are in place:
These are the actual resources and activities we can use within a course and 9 means forum, so now we are ready to create a better query with an actual URL.
Please note: on your Moodle site the modules might be different or they have different id numbers, so discover your database first. But the code I am going to provide is going to work no matter what your module allocation is.
Let's have a final look at the tables and their relationship, before we move on.
In this case instance=6 means forum.id=6, but only because module.id=9 which is a forum. If module was 10, then we should replace the mdl_forum table with mdl_glossary. I hope it makes sense.
Click here to see how it works on my dummy Moodle site. Don't forget to log in as a guest. Come back for more tutorials. Enjoy!