I am making a simple local plugin that randomly displays a modal with a question and answer pair (provided by student) to confirm that the student is present while their chosen course is opened. If this is not answered correctly within the allotted time the student account is to be suspended. Everything is working except the suspend account part.
Since the 'sesskey' and 'user' that calls the function that should change user->suspended from 0 to 1 is from the student who is logged in, I am getting Debug info of "Error code: nopermissions" and the notification "Sorry, but you do not currently have permissions to do that (Update user profiles)".
This is the section of code where I encounter the error:
if (confirm_sesskey()) { require_capability('moodle/user:update', $sitecontext); if ($user = $DB->get_record('user', array('id'=>$id, 'deleted'=>0))) { if (!is_siteadmin($user) and $user->suspended != 1) { $user->suspended = 1; } // Force logout. \core\session\manager::kill_user_sessions($user->id); user_update_user($user, false); } } //redirect($returnurl); }
How would I go about changing the permissions for this student during the instance this script is called so that the account can be suspended, then return the students permissions to their prior default after this action?
Thank you in advance for any help on this. I'm new to Moodle and this is the last piece of this plugin to get it working.