Artificial context object for testing?

Artificial context object for testing?

by Brad Smith -
Number of replies: 2

I started this post by describing a particular problem I'm having, but realized that what would be more valuable to me is a more general troubleshooting tool.

I frequently find myself asking "what is this function/method returning?", and I can't just create a quick CLI test script because it takes a context object as an argument. 

I can't be the first person who's had this problem, so I wonder: does there exist a way to just say "create a context object for me as if I was logged in as user X and look at module Y"?

If there's no such thing, do people have any other advice for investigating those long chains where X calls Y, which calls Z, which calls... and somewhere along the way something unexpected is happening? 

Thanks!

Average of ratings: -
In reply to Brad Smith

Re: Artificial context object for testing?

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

To get a context object for a course-module id 123:

context_module::instance(123)

They don't depend on the user looking at it.

For general debugging, you could try to install php debugging tools, or the easiest way otherwise is just to call print_object to print relevant information as it goes through.

--sam




In reply to sam marshall

Re: Artificial context object for testing?

by Brad Smith -

Thanks for the response! If context is user-independent, then maybe I need to come back to my original question. 

Basically, I am using an old-style assignment (Moodle 2.2) with a custom type. I'll be upgrading soon and have re-implemented this as a standalone module, but I fear I'll have the same or a similar problem, which is...

The assignment mod sends an email to report submission of the assignment to people with the 'grade assignment' permission. In some cases these should go to people assigned at the course level, but in others they should go to mentors, which are assign on a per-user basis. 

To assign a mentor in this way, I create a 'mentor' role with the 'grade assignment' permission, and then use the 'assign roles relative to this user' link on a student's profile page to grant the mentor role to their mentor's account. 

Unfortunately, when the user submits an assignment, the course-level graders get an email, but the user-level ones don't. 

Hopefully context_module::instance() will help me troubleshoot, but perhaps I have a more fundamental misunderstanding here?