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