-1

i need your help to enroll user in particular course on the basis of quiz complete what i need to do is i want to show to button when student complete the particular quiz on the course page where student already enrol i have sucessfully show that button next when user click on that button user see a popup msg where he get 'do you want to proceed to next course' on submit i want user automatic enrol on that course

what i have done right now plz check in course view.php file i have added this code

global $USER;

$quiz_id = 1; // Replace 35 with the ID of the quiz you want to check
$user_id = $USER->id; // Replace $USER with the Moodle global user object if you're not inside a function that has $USER as a parameter

$attempts = quiz_get_user_attempts($quiz_id, $user_id);

if (!empty($attempts)) {
    $completed = false;
    foreach ($attempts as $attempt) {
        if ($attempt->state == quiz_attempt::FINISHED) {
            // Quiz has been completed by the user
            $completed = true;
            break;
        }
    }
    if ($completed) {
        echo '<button id="enroll-button">Click me</button>';
    } else {
        echo "Quiz has not been completed by the user.";
    }
} else {
    // No attempt exists for the given user and quiz
    echo "No attempt exists for the given user and quiz.";
}
?>

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<script type="text/javascript"> 
 $(document).ready(function() { 
    // Code to display the enroll button goes here
    $('#enroll-button').click(function() {
        var confirmPopup = confirm("Do you want to enroll in all courses?");
        if (confirmPopup == true) {
            // Code to enroll the user in all courses goes here
            var userId = <?php echo $USER->id; ?>;
            var roleId = 5; // Add the ID of the role you want to enroll the user in
            var courseIds = ['3']; // Add the ID of the course you want to enroll them in
            // Enroll the user in each course
            $.each(courseIds, function(index, courseId) {
                // Call the PHP script that enrolls the user in the course
                $.ajax({
                    type: "POST",
                    url: "/dacold/course/enroll_user.php",
                    data: {
                        userId: userId,
                        roleId: roleId,
                        instanceid: 0,
                        enrolperiod: 0,
                        courseId: courseId
                    },
                    success: function(data) {
                        console.log("User enrolled in course ID: " + courseId);
                    },
                    error: function(jqXHR, textStatus, errorThrown) {
                        console.log("Error enrolling user in course ID: " + courseId);
                    }
                });
            });
        } else {
            // Code to handle cancel button click goes here
            alert("You have chosen not to enroll in all courses.");
        }
    });
});
</script>

what i need to write in enroll_user.php file to automatic enrol the student in particular course plz plz help me

2 Answers2

0

Use the below code in your enrol_user.php file to enroll the users. This only works with manual enrolment method, Use any other enrolment if needed then replace the manual in the enrol_get_plugin function

global $DB, $CFG;
require_once($CFG->dirroot.'/lib/enrollib.php');

// Manual enrolment plugin.
$enrolplugin = enrol_get_plugin('manual');

// Enrollment instance added to the course.
$instance = $DB->get_record('enrol', ['courseid' => $courseid, 'enrol' => 'manual']);

// Enrol the user with a role in the course.
$result = $enrolplugin->enrol_user($instance, $userid, $roleid);

Hope it helps,

FYI, In Moodle don't use the default Ajax method. Use the moodle AJAX api with External functions. Please read the Moodle documentations.

https://moodledev.io/docs/guides/javascript/ajax

https://moodledev.io/docs/4.1/apis/subsystems/external/writing-a-service

Thanks,

Prasanna T
  • 169
  • 6
0

i add this code

<?php
require_once('../config.php');

// Retrieve the data sent via AJAX
$user_id = filter_input(INPUT_POST, 'userId', FILTER_SANITIZE_NUMBER_INT);
$course_ids = filter_input(INPUT_POST, 'courseIds', FILTER_SANITIZE_NUMBER_INT, FILTER_REQUIRE_ARRAY);

// Define the role ID that you want to enroll the user as (e.g. student)
$role_id = 5;
 
// Loop through each course and enroll the user
foreach ($course_ids as $course_id) {

    // Call the Moodle function to enroll the user in the course
    $enrol = enrol_get_plugin('self');
    $enrol_instance = $DB->get_record('enrol', array('enrol' => 'self', 'courseid' => $course_id), '*', MUST_EXIST);
    $enrol_instance_id = $enrol_instance->id;
    
    $enrol_user = new stdClass();
    $enrol_user->roleid = $role_id;
    $enrol_user->userid = $user_id;
    $enrol_user->enrolid = $enrol_instance_id;
    $enrol_user->timestart = time();
    $enrol_user->timeend = 0;
    
    if (!$enrol->enrol_user($enrol_instance, $user_id, $role_id)) {
        echo "Error enrolling user with ID $user_id in course with ID $course_id\n";
    } else {
        echo "Enrolled user with ID $user_id in course with ID $course_id\n";
    }
}
?>

now user will enroll in all courses when he click on popup ok button