How to remove programmatically all participation of a student in a Moodle 2.5 group?

1k views Asked by At

I need to remove an user programmatically from everything he is related with a group, like notes and everything. I removed the user from the table mdl_user_enrollments but he's still enrolled and when he logs in all data of the group is still being displayed. I want to remove everything this user has related with the group, so i can enroll he again and he'll start all over.

1

There are 1 answers

0
André Morales On

Don't know if this is exactly what you are looking for but the procedure bellow removes users data from course. Not from all installed moodle modules that will vary for each Moodle configuration) but you can add a few querys for your extra modules as well.

    CREATE DEFINER = `your_user`@`%` PROCEDURE `RemoveUserDataFromCourse`(IN `c_id` int,IN `u_id` int)
BEGIN
    # ENROLLMENT
    DELETE FROM mdl_user_enrolments WHERE userid = u_id AND enrolid IN (SELECT id FROM mdl_enrol WHERE courseid = c_id);

  # LOG, EVENT, POSTS
    DELETE FROM mdl_log WHERE course = c_id AND userid = u_id;
    DELETE FROM mdl_event WHERE courseid = c_id AND userid = u_id;
    DELETE FROM mdl_post WHERE courseid = c_id AND userid = u_id;

    # Course completion
    DELETE FROM mdl_course_modules_completion WHERE coursemoduleid IN (SELECT id FROM mdl_course_modules WHERE course= c_id and userid=u_id);
    DELETE FROM mdl_course_completions WHERE course = c_id AND userid = u_id;
    DELETE FROM mdl_course_completion_crit_compl WHERE course = c_id AND userid = u_id;

    # Grades from all modules
    DELETE FROM mdl_grade_grades WHERE userid = u_id and itemid IN (SELECT id FROM mdl_grade_items WHERE courseid = c_id);

    # MODULES

    # ASSIGNMENTS
    DELETE FROM mdl_assign_grades WHERE userid = u_id AND assignment IN (SELECT id FROM mdl_assign WHERE course = c_id);    

    # QUIZ
    DELETE FROM mdl_quiz_attempts WHERE userid = u_id AND quiz IN (SELECT id FROM mdl_quiz WHERE course = c_id);
    DELETE FROM mdl_quiz_grades WHERE userid = u_id AND quiz IN (SELECT id FROM mdl_quiz WHERE course = c_id);

    # SCORM
    DELETE FROM mdl_scorm_scoes_track WHERE userid = u_id AND scormid IN (SELECT id FROM mdl_scorm WHERE course = c_id);

    # CHECK LIST
    DELETE FROM mdl_checklist_check WHERE userid = u_id AND item IN ( SELECT mci.id FROM mdl_checklist mc INNER JOIN mdl_checklist_item mci ON mci.checklist = mc.id WHERE course = c_id );
    DELETE FROM mdl_checklist_comment WHERE userid = u_id AND itemid IN ( SELECT mci.id FROM mdl_checklist mc INNER JOIN mdl_checklist_item mci ON mci.checklist = mc.id WHERE course = c_id );

    # CHOICE
    DELETE FROM mdl_choice_answers WHERE userid = u_id AND choiceid IN (SELECT id FROM mdl_choice WHERE course = c_id);
END;

This procedure is based on the operations made by course/reset.php it receives two parameters: the course id and the user id. Add/remove the modules related querys depending on you Moodle configuration.