Unenrol student (with PHP)

Unenrol student (with PHP)

by Domenico Garofoli -
Number of replies: 6

Hi,

I have this problem: when I unenrol student from specific course, after login, I see again the subscriptions. The unenrol haven't no effects.

I try with manual unenrolment user from moodle backend and after with this script:

```
$instances = $DB->get_records('enrol', array('courseid' => $course_id));

foreach ($instances as $instance) {
  $plugin = enrol_get_plugin($instance->enrol);
  $plugin->unenrol_user($instance, $user_id);
}
```

And delete this rows on database:

```

delete from mdl_course_completions where userid=966;
delete from mdl_course_completion_crit_compl where userid=966;
delete from mdl_course_modules_completion where userid=966;

```

Anyone know a solution ? Thank you.


Average of ratings: -
In reply to Domenico Garofoli

Re: Unenrol student (with PHP)

by Domenico Garofoli -

Let me explain it better: I need to unenroll a student, deleting completely his progress on the course (lessons and scorms completions), leaving him able to enroll himself again but starting the course from the beginning, with a clean state.

When I unenroll the student from the admin panel or via API, if the student tries to enroll himself again, he still finds the old progress and lessons completed on the course.

I tried cleaning up those tables: mdl_course_completions, mdl_course_completion_crit_compl, mdl_course_modules_completion and mdl_logstore_standard_log.

Unfortunately the student keeps seeing the old progress when re-enroling.

Any ideas on how to clean completely his enrolment?

Thanks

In reply to Domenico Garofoli

Re: Unenrol student (with PHP)

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

It would help to know what version of Moodle you are using. If you are using Moodle 3.3+ or higher with GDPR capabilities, this would appear to be a case where a GDPR Data request of "Delete all of my personal data" might be the way to go. NOTE: Just be aware, this will delete him from every course.

In reply to AL Rachels

Re: Unenrol student (with PHP)

by Domenico Garofoli -

I'm using 3.3.3 (Build: 20171113).

In reply to Domenico Garofoli

Re: Unenrol student (with PHP)

by AL Rachels -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

The behavior you describe is just the way Moodle works. When you delete the student, you are actually just setting a flag in their account and not actually deleting any of their work.

Basically, I think you have four options:

1. IF that student is the ONLY student in a course, you could reset the course.

2. IF you have the latest Moodle 3.3 and have the GDPR plugin installed, you can delete all data for the student. BUT, this process will delete all their data from ALL courses, not just one.

3. Give the student a new account for the course in question and still use the old account for other courses.

4. Manually go through and delete all their work from each activity, if you can.

Sorry, but all of these have drawbacks to them, and I cannot think of any other options

Average of ratings: Useful (1)
In reply to Domenico Garofoli

Re: Unenrol student (with PHP)

by Darko Miletić -

You would have to write some custom code.

I suggest to take a look at what delete_user API does and write your own version of that adding whatever is missing.

You should hook that code to listen for \core\event\user_enrolment_deleted event and act accordingly.

 

In reply to Darko Miletić

Re: Unenrol student (with PHP)

by Rachel Martin -

We have an old plugin which does an OK job for our needs, but it is not perfect as I have noticed some gaps here and there.  Most of our courses are on-demand and don't use the forums/assignments and such.  Below is the array of tables we clear.

$todelete = array (

array( 'table' => 'post', 'where' => "userid = $userid AND courseid = $courseid" ),

array( 'table' => 'course_completions', 'where' => "userid = $userid AND course = $courseid" ),

array( 'table' => 'course_completion_crit_compl', 'where' => "userid = $userid AND course = $courseid" ),

array( 'table' => 'event','where' => "userid = $userid AND courseid = $courseid" ),

array( 'table' => 'scorm_scoes_track','where' => "userid = $userid AND scormidIN (SELECT id FROM {scorm}WHERE course = $courseid)" ),

array( 'table' => 'groups_members', 'where' => "userid = $userid AND groupidIN (SELECT id FROM {groups} WHERE courseid = $courseid)" ),

array( 'table' => 'lesson_attempts','where' => "userid = $userid AND lessonid IN (SELECT id FROM {lesson} WHERE course = $courseid)" ),

array( 'table' => 'lesson_branch','where' => "userid = $userid AND lessonid IN (SELECT id FROM {lesson} WHERE course = $courseid)" ),

array( 'table' => 'lesson_grades','where' => "userid = $userid AND lessonid IN (SELECT id FROM {lesson} WHERE course = $courseid)" ),

array( 'table' => 'lesson_overrides', 'where' => "userid = $userid AND lessonid IN (SELECT id FROM {lesson} WHERE course = $courseid)" ),

array( 'table' => 'lesson_timer', 'where' => "userid = $userid AND lessonid IN (SELECT id FROM {lesson} WHERE course = $courseid)" ),

array( 'table' => 'lti_submission', 'where' => "userid = $userid AND ltiidIN (SELECT id FROM {lti}WHERE course = $courseid)" ),

array( 'table' => 'grade_grades', 'where' => "userid = $userid AND itemid IN (SELECT id FROM {grade_items}WHERE courseid = $courseid)" ),

array( 'table' => 'assign_grades','where' => "userid = $userid AND assignment IN (SELECT id FROM {assign} WHERE course = $courseid)" ),

array( 'table' => 'assignment_submissions','where' => "userid = $userid AND assignment IN (SELECT id FROM {assignment} WHERE course = $courseid)" ),

array( 'table' => 'quiz_attempts','where' => "userid = $userid AND quiz IN (SELECT id FROM {quiz} WHERE course = $courseid)" ),

array( 'table' => 'quiz_grades','where' => "userid = $userid AND quiz IN (SELECT id FROM {quiz} WHERE course = $courseid)" ),

array( 'table' => 'course_modules_completion','where' => "userid = $userid AND coursemoduleid IN (SELECT id FROM {course_modules} WHERE course = $courseid)" ),

array( 'table' => 'certificate_issues', 'where' => "userid = $userid AND certificateidIN (SELECT id FROM {certificate}WHERE course = $courseid)" ),

array( 'table' => 'user_enrolments','where' => "userid = $userid AND enrolidIN (SELECT id FROM {enrol}WHERE courseid = $courseid)" ),

array( 'table' => 'feedback_value', 'where' => "completed IN (SELECT {feedback_completed}.id FROM {feedback_completed}, {feedback}

WHERE feedback={feedback}.id AND userid = $userid AND course = $courseid)" ),

array( 'table' => 'feedback_completedtmp', 'where' => "userid = $userid AND feedbackIN (SELECT id FROM {feedback} WHERE course = $courseid)" ),

array( 'table' => 'feedback_completed', 'where' => "userid = $userid AND feedbackIN (SELECT id FROM {feedback} WHERE course = $courseid)" ),

array( 'table' => 'repository_instances', 'where' => "userid = $userid

AND contextid IN (SELECT id FROM {context}WHERE instanceid = $courseid AND contextlevel = ".CONTEXT_COURSE.')' ),

array( 'table' => 'role_assignments', 'where' => "userid = $userid

AND contextid IN (SELECT id FROM {context}WHERE instanceid = $courseid AND contextlevel = ".CONTEXT_COURSE.')' )

);