Conditional Activity - instant revocation

Conditional Activity - instant revocation

by Jason C -
Number of replies: 5

Hi all, I hope this is in the right section.

I've setup a lesson that needs to be viewed which then allows a quiz through a conditional activity to be accessed immediately.

If one of the answers is incorrect on the quiz, I want the quiz to immediately end and become hidden again until the lesson is retaken.

From reading,  I don't think this is possible without modification and having a bit of a play, I have narrowed down the relevant tables and completion code.

I've got the completion code to change back to not viewed for the course->module->user when a certain "failed" content page is shown in the quiz but I can't find how to apply the change. The completion snippets below:

require_once("{$CFG->libdir}/classes/task/completion_regular_task.php");

$ci = new \completion_info($course);
$ci->update_state($cm,COMPLETION_NOT_VIEWED,3);

Interestingly, if I refresh the logs by hitting the button under siteadmin->Reports->Logs--"Get these logs", my completion code from above is applied and the user is locked out of quiz again. Something in that button code is instantly applying the completion revocation.

Would someone be able to tell me which function the log update button uses or how to apply completion changes immediately?

Thankyou

Jason


Average of ratings: -
In reply to Jason C

Re: Conditional Activity - instant revocation

by Jason C -

Is someone be able to tell me which function the log update button uses?


Thanks


In reply to Jason C

Re: Conditional Activity - instant revocation

by Jason C -

Is someone be able to tell me which function the log update button uses?


Thanks


In reply to Jason C

Re: Conditional Activity - instant revocation

by Mihir J -

Hi Jason


COuld you please tell us, where have you put up this code now? in which file?

require_once("{$CFG->libdir}/classes/task/completion_regular_task.php");

$ci = new \completion_info($course);
$ci->update_state($cm,COMPLETION_NOT_VIEWED,3);

thanks!


In reply to Mihir J

Re: Conditional Activity - instant revocation

by Jason C -

I've just put it {root dir}/mod/lesson/classes/event/content_page_viewed.php to fire on a particualr page view.

I can see that it is firing and calling update_state from the completionlib.php  but no matter what functions I try (rebuild_cache etc) it rehides the linked course module and updates the database table only when I press the refresh log button.

In reply to Mihir J

Re: Conditional Activity - instant revocation

by Jason C -

My issue (which took me way too long to discover) was I shouldn't have put my code in the aforementioned event file.

I've hardcoded some variables which isn't ideal but at least shows what I've done. I'm not a programmer and I'm sure I've broken every best practice out there but hopefully it helps someone as I could find no posts on how to instant revoke a module.


I moved it to mod/lesson/view.php with an "If" condition (particular course id and page id) at around line173 (under if ($pageid == LESSON_UNSEENBRANCHPAGE)) code block.


#############################################################
if ($lesson->course == 2 && $lesson->id ==2 && $pageid == 8)


$DB->delete_records('lesson_branch', array('lessonid'=>1,'userid'=>$USER->id));  ## this clears page progress from the database for lesson 1
$courseA= $DB->get_record('course', array('id' => 2), '*', MUST_EXIST); # course id 2
$cmA = get_coursemodule_from_id('lesson', 2, 0, false, MUST_EXIST); # module id 2 (which is lesson 1) from mdl_course_modules
$ci = new \completion_info($courseA);
$ci->update_state($cmA,COMPLETION_INCOMPLETE,$USER->id); # this resets completion on lesson 1  for current user
};

Course id/module id and lesson id are all a little confusing as a lesson is also a module but pretty much what it's saying is:

If the current courseid = 2, lessonid = 2 and lesson pageid = 8 (my "failure" page displayed on a wrong answer)

then

clear table entries (mdl_lesson_branch) related to that lesson/user - this makes them have to start from the beginning

and mark lesson 1 as unviewed for that user


As lesson 2 has a restriction placed on it (lesson 1 must be viewed and marked complete) , this stops lesson 2 assessment from being taken until they view lesson 1 again.


Edit: I renamed some of the variables $cmA $courseA etc so they were unique and I didn't affect other code on the page.