HELP WITH The MDL_CONTEXT Table

HELP WITH The MDL_CONTEXT Table

by Bruno José Palmeira de Araujo -
Number of replies: 21
Can someone explain what is the contextleve and instanceid fields?

Where the system create the context level? and the instanceid??

How the users is related to capabilities or rules, based on the context ?

Best regards.
Average of ratings: -
In reply to Bruno José Palmeira de Araujo

Re: HELP WITH The MDL_CONTEXT Table

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Doing a quick search of Moodle Docs I was not able to find an explanation of how this table works. I am beginning to look at how I will pull in users for next year and will need to understand this table. Any insights and/or explanations about how the mdl_context table factors in would be greatly appreciated. Peace.
In reply to Anthony Borrow

Re: HELP WITH The MDL_CONTEXT Table

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
In /lib/accesslib.php I found the following which helps to explain things for the contextlevel field. Any help on the instanceid?

00025: // context definitions
00026: define('CONTEXT_SYSTEM', 10);
00027: define('CONTEXT_PERSONAL', 20);
00028: define('CONTEXT_USER', 30);
00029: define('CONTEXT_COURSECAT', 40);
00030: define('CONTEXT_COURSE', 50);
00031: define('CONTEXT_GROUP', 60);
00032: define('CONTEXT_MODULE', 70);
00033: define('CONTEXT_BLOCK', 80);

Average of ratings: Useful (6)
In reply to Anthony Borrow

Re: HELP WITH The MDL_CONTEXT Table

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
OK - the mdl_context table is starting to make a little more sense to me. If the contextlevel field is 50 (i.e. a course) then instanceid means the course id number. I suspect that if it is a user then it is the mdl_user id number. Course category would be the id number from mdl_course_categories, group corresponds to mdl_groups (I am not positive about this as I am not familiar with the new groupings concept), module to mdl_modules, block to mdl_block. I am not sure about how CONTEXT_SYSTEM and CONTEXT_PERSONAL are used. I suspect that CONTEXT_SYSTEM always has an instance id of 0. I would welcome any insights on CONTEXT_PERSONAL.
Average of ratings: Useful (2)
In reply to Anthony Borrow

Re: HELP WITH The MDL_CONTEXT Table

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
I would also appreciate a brief explanation about mdl_context_rel - it is not clear to me what is being related to what and more importantly why?
In reply to Anthony Borrow

Re: HELP WITH The MDL_CONTEXT Table

by Yu Zhang -
Hi Anthony,

The mdl_context_rel is a relationship cache showing the relationship between contexts. It is basically saying context A is a child (or child-child) or context B. This is used only when resolving capabilities override during log in.

The issue is that for example, we have a user with a student role in "Course 101", and an override for the student role in "Teacher's Forum in Course 101", then we have a pair of contexts. We need to know if this override indeed applies. This pair of contexts in this case is valid, because the forum context is a child context of the course context, but another pair could or could be invalid (say course 102 and teacher's forum in course 101), and that row should be discarded and not contributed to the overall permissions.

Since the db tables does not know what contexts are related, we can either load everything and do a loop through, discarding invalid results, or just cache this information in db, which is significantly faster for larger sites.

Cheers,

Yu
Average of ratings: Useful (1)
In reply to Yu Zhang

Re: HELP WITH The MDL_CONTEXT Table

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Yu - Xie Xie for the explanation of the mdl_context_rel table - that helped immensely as now I know that I can safely disregard it for the purpose of doing my import of data from our SIS. Peace.
In reply to Yu Zhang

Teacher Role and Default View Hidden Courses Capability - Inquiry

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
I suspect Yu may be the best person to answer this but anyone familiar with roles is more than welcome to jump in. I was working on populating my database from our SIS system. I added to mdl_role_assignments several hundred entries for the courses that each of our teachers teach. I entered one for a contextlevel of 50 for each course that a user teaches with a roleid of 3 (i.e. teacher). So far so good. As far as I can tell I have teachers assigned in the teacher role for all of their classes which was what I had intended.

What I was surprised to see missing were some of the hidden courses that I pair the teachers of a common subject together in to use as a repository. What I believe I discovered is that by default teachers are not able to see hidden courses (even if they teach the hidden course). This is a change from the behavior I became accustomed to in 1.6 and I suspect an unintended one.

My concern is that if teachers have the ability to hide a course and do so then they will not (at least using the default teacher role) be able to get back in. I am wondering if the teacher role should by default allow (rather than inherit as it is currently set) the /moodle/course:viewhiddencourses capability. Any thoughts? Should I add this to the tracker? Peace.
In reply to Anthony Borrow

Re: Teacher Role and Default View Hidden Courses Capability - Inquiry

by Yu Zhang -
Hi Anthony,

You are right, this is unintended. Teachers should be able to see hidden courses they teach in ( MDL-7939). And teachers who can not see hidden courses should not be able to hide them in the first place (MDL-8885), Both issues are fixed by now. However you would need to edit your teacher and non-editting teacher role so that they can see hidden courses (by allowing moodle/course:viewhiddencourses).

Cheers,

Yu
In reply to Yu Zhang

Re: Teacher Role and Default View Hidden Courses Capability - Inquiry

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Yu - I am happy to see that these issues have already been addressed. Will moodle/course:viewhiddencourses be allowed by default with a clean install? I am preparing to check and see where the code for default settings is done. Any pointers would be welcome but I see that you have been busy on the forums and do not want to take away from development and bug fixing. I'm excited and ready for 1.8 to be officially released.
In reply to Yu Zhang

Re: Teacher Role and Default View Hidden Courses Capability - Inquiry

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Yu - I figured out how to get the changes integrated. I opted to use the reset to default pushbutton for the various roles (teacher, course creator, and non-editing teaching since those were the ones changed in CVS.

Doing so made me wonder how a system admin would become aware of such changes. During an upgrade would it be possible/desirable to indicate to the admin that a change to legacy roles has taken place, would you like to apply to these changes or something along those lines? I envision it being similar to how changes to the database are handled. I just want to make sure that as code and roles get updated in CVS that the admins are aware of the changes and have an opportunity to apply them. Any thoughts?
In reply to Anthony Borrow

Re: Teacher Role and Default View Hidden Courses Capability - Inquiry

by Yu Zhang -
Hi Anthony,

Currently we cannot support change of default role definitions during upgrades. We only support adding or removing of capabilities from default roles during upgrades. The main reasons are 1) Default roles are not mandatory (i.e. you can have a student role with no legacy:student capability attached and still have your site running) and 2) some sites might have capabilities changed and/or wants the current configuration, and we should not forcefully change this for them. So these changes have to be done manual, for now.

Cheers,

Yu
In reply to Yu Zhang

Re: Teacher Role and Default View Hidden Courses Capability - Inquiry

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Yu - Thanks, that makes sense, I just wonder how we might better communicate to site administrators when a change has been made to the default roles. In other words, how would a site that is up and running know if such a change were made? Perhaps the solution is to keep the site updated to CVS and periodically - if the admin wants - to go then reset the roles to the default settings. That would then set their roles to match what the latest role definitions. Then, if they have any custom changes they can re-apply those. Peace - Anthony
In reply to Anthony Borrow

Re: Teacher Role and Default View Hidden Courses Capability - Inquiry

by Yu Zhang -
Hi Anthony,

Hopefully, we won't have to modify base role definitions for default roles anymore. tongueout I think we are very close to getting everything right.

Cheers,

Yu
In reply to Yu Zhang

Re: Teacher Role and Default View Hidden Courses Capability - Inquiry

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Excellent - thanks for all your work on Roles - they have tremendously increased Moodle's power. Peace.
In reply to Yu Zhang

Re: Teacher Role and Default View Hidden Courses Capability - Inquiry

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Yu - Another roles related question but hopefully a simple one. As part of my setting up Moodle with data from our SIS I create a series of metacourses via a series of sql queries. It appears that Moodle takes care of enrolling the students after I have added the data to mdl_course_meta; however, I suspect that to be thorough it would be best if I manually added the student enrollments to the mdl_role_assignments for the metacourses. Previously, for the metacourses I had to add records to the mdl_user_students table. Would you recommend manually adding the records to mdl_role_assignments or is it safe to allow Moodle to handle it. My only hesitancy in not doing it is I do not know where and when Moodle does so. When I go in to the metacourse and go to assign roles, I see that the students are listed under the student role. What I want to avoid is adding the records to the mdl_role_assignments table if they are not needed. Thanks for any guidance you might be able to provide. Peace - Anthony
In reply to Anthony Borrow

Re: Teacher Role and Default View Hidden Courses Capability - Inquiry

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
OK - I did a test by manually creating a metacourse and it appears that the mdl_role_assignments table does get populated with a record for each student enroled in the metacourse. I have gone ahead and added that as part of my script.
In reply to Anthony Borrow

Re: Teacher Role and Default View Hidden Courses Capability - Inquiry

by Atanas Atanasov -
I see that you understand the way to manage roles. I read all post but i have a problem to make sql query to moodle database to see course/courses that users belong to.
Can you help me.
I want to understand for users with role as teacher which course manage.
And for user with role as student which course/courses he is belong to.
Thanks.
In reply to Atanas Atanasov

Re: Teacher Role and Default View Hidden Courses Capability - Inquiry

by Anthony Borrow -
Picture of Core developers Picture of Plugin developers Picture of Testers
Atanas - I'm just returning from a 2 week trip and am currently catching up on emails and the like. I have this on my list and will respond with a more detailed answer (hopefully tomorrow). Peace - Anthony
In reply to Anthony Borrow

Re: Teacher Role and Default View Hidden Courses Capability - Inquiry

by Dennis Chew -
I discovered the same problem when creating metacourses and manually adding the mdl_course_meta records for parent_course, child_course through SQL.

Doing this alone does not populate the mdl_role_assignments table which means that the enrolments from the child courses are not brought across to the parent "meta" course.

The following lines are necessary to sync the parent / child enrolments. In my case I knew the 'id' of the metacourse ($metacourseid).

// set up mdl_role_assignment
if (! $course = get_record('course', 'id', $metacourseid)) {
error("Meta course ID is incorrect");
}

sync_metacourse($course);


Hope this helps.
In reply to Dennis Chew

Re: Teacher Role and Default View Hidden Courses Capability - Inquiry

by Marco Ramos -
so can any one explain me the mdl_context ?

i have a script that add's the course to moodle after it was created on SIS, the course is created on moodle fine, but there are some options missing, i'm trying to understand the mdl_context table

this is an example of a course that i have created, my course id is 6 so :

id | contextlevel | instanceid | path | depth
43 | 50 | 6 | /1/2/43 | 3

any help would be appreciated thanks..

marco