Skip to content

Commit

Permalink
MOODLE_404_STABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
rdebleu committed May 3, 2024
1 parent 4f77d86 commit a2b208f
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 192 deletions.
35 changes: 1 addition & 34 deletions classes/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function enrol_user(
$status = null,
$recovergrades = null
) {
global $CFG, $DB;
global $DB;
// We need to keep the role of the user.
if (isset($instance->roleid)) {
$roleid = $instance->roleid;
Expand All @@ -234,39 +234,6 @@ public function enrol_user(
$context = \context_course::instance($instance->courseid, MUST_EXIST);
parent::enrol_user($instance, $userid, $roleid, $timestart, $timeend, $status, $recovergrades);
role_assign($roleid, $userid, $context->id, 'enrol_coursecompleted', $instance->id);

// Send welcome message if needed.
if ($instance->customint2 > 0) {
// There is a course welcome message to be sent.
$adhock = new \enrol_coursecompleted\task\send_welcome();
$adhock->set_custom_data(
[
'userid' => $userid,
'enrolid' => $instance->id,
'courseid' => $instance->courseid,
'completedid' => $instance->customint1,
]
);
$adhock->set_component('enrol_coursecompleted');
\core\task\manager::queue_adhoc_task($adhock);
}

// Keep the user in a group when needed.
if ($instance->customint3 > 0) {
require_once($CFG->dirroot . '/group/lib.php');
$groups = array_values(groups_get_user_groups($instance->customint1, $userid));
foreach ($groups as $group) {
$subs = array_values($group);
foreach ($subs as $sub) {
$groupnamea = groups_get_group_name($sub);
$groupnameb = groups_get_group_by_name($instance->courseid, $groupnamea);
if ($groupnameb) {
groups_add_member($groupnameb, $userid);
}
}
}
}
mark_user_dirty($userid);
} else {
debugging('Role does not exist', DEBUG_DEVELOPER);
}
Expand Down
84 changes: 0 additions & 84 deletions classes/task/send_welcome.php

This file was deleted.

84 changes: 84 additions & 0 deletions classes/user_enrolment_callbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace enrol_coursecompleted;

use context_course;
use core_user;
use moodle_url;
use stdClass;

/**
* Enrol coursecompleted plugin
*
* @package enrol_coursecompleted
* @copyright 2017 eWallah (www.eWallah.net)
* @author Renaat Debleu <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class user_enrolment_callbacks {

/**
* Callback for the user_enrolment hook.
*
* @param \core_enrol\hook\after_user_enrolled $hook
*/
public static function send_course_welcome_message(\core_enrol\hook\after_user_enrolled $hook): void {
global $CFG, $DB;
$instance = $hook->get_enrolinstance();
// Send welcome message.
if ($instance->enrol == 'coursecompleted') {
if ($instance->customint2 > 0) {
$plugin = enrol_get_plugin($instance->enrol);
if ($complcourse = $DB->get_field('course', 'fullname', ['id' => $instance->customint1])) {
$context2 = context_course::instance($instance->customint1);
$a = new stdClass();
$a->completed = format_string($complcourse, true, ['context' => $context2]);
$custom = $instance->customtext1;
if ($custom == '') {
$message = get_string('welcometocourse', 'enrol_coursecompleted', $a);
} else {
$key = ['{$a->completed}'];
$value = [$a->completed];
$message = str_replace($key, $value, $custom);
}
$plugin->send_course_welcome_message_to_user(
instance: $instance,
userid: $hook->get_userid(),
sendoption: ENROL_SEND_EMAIL_FROM_NOREPLY,
message: $message,
);
}
}

// Keep the user in a group when needed.
if ($instance->customint3 > 0) {
require_once($CFG->dirroot . '/group/lib.php');
$groups = array_values(groups_get_user_groups($instance->customint1, $hook->get_userid()));
foreach ($groups as $group) {
$subs = array_values($group);
foreach ($subs as $sub) {
$groupnamea = groups_get_group_name($sub);
$groupnameb = groups_get_group_by_name($instance->courseid, $groupnamea);
if ($groupnameb) {
groups_add_member($groupnameb, $hook->get_userid());
}
}
}
}
}
}
}
33 changes: 33 additions & 0 deletions db/hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* * Hook callbacks for enrol_coursecompleted.
*
* @package enrol_coursecompleted
* @copyright 2024 eWallah (www.eWallah.net)
* @author Renaat Debleu <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

$callbacks = [
[
'hook' => core_enrol\hook\after_user_enrolled::class,
'callback' => 'enrol_coursecompleted\user_enrolment_callbacks::send_course_welcome_message',
],
];
1 change: 0 additions & 1 deletion tests/backup_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ public function test_backup_restore(): void {

$ccompletion = new \completion_completion(['course' => $this->course1->id, 'userid' => $this->student->id]);
$ccompletion->mark_complete(time());
$this->runAdhocTasks();
$bc = new \backup_controller(
\backup::TYPE_1COURSE,
$this->course2->id,
Expand Down
1 change: 0 additions & 1 deletion tests/behat/duration_enrolcoursecompleted.feature
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ Feature: Duration Enrolment on course completion
When I follow "Click to mark user complete"
And I wait "1" seconds
And I run the scheduled task "core\task\completion_regular_task"
And I run all adhoc tasks
And I am on "Course 2" course homepage
And I navigate to course participants
Then I should see "2 participants found"
Expand Down
19 changes: 6 additions & 13 deletions tests/behat/enrol_coursecompleted.feature
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ Feature: Enrolment on course completion
And I click on "Course completion" "link" in the "region-main" "region"
And I follow "Click to mark user complete"
And I log out
And I log in as "admin"
And I wait "1" seconds
And I run the scheduled task "core\task\completion_regular_task"
And I run all adhoc tasks
And I log out
When I am on the "C1" "Course" page logged in as "user1"
Then I should not see "You will be enrolled in this course when"
And I should see "Page A"
Expand All @@ -81,7 +79,6 @@ Feature: Enrolment on course completion
And I follow "Click to mark user complete"
And I wait "1" seconds
And I run the scheduled task "core\task\completion_regular_task"
And I run all adhoc tasks
And I am on "Course 2" course homepage
Then I navigate to course participants
# The user enrolment only starts in 2030
Expand All @@ -101,7 +98,6 @@ Feature: Enrolment on course completion
And I follow "Click to mark user complete"
And I wait "1" seconds
And I run the scheduled task "core\task\completion_regular_task"
And I run all adhoc tasks
And I am on "Course 2" course homepage
When I navigate to course participants
# The user enrolment only starts in 2030
Expand All @@ -123,10 +119,8 @@ Feature: Enrolment on course completion
And I click on "Course completion" "link" in the "region-main" "region"
And I follow "Click to mark user complete"
And I log out
And I log in as "admin"
And I wait "1" seconds
And I run the scheduled task "core\task\completion_regular_task"
And I run all adhoc tasks
And I log out
And I am on the "C2" "Course" page logged in as "teacher1"
And I navigate to course participants
And I should see "Username 1" in the "participants" "table"
Expand Down Expand Up @@ -162,9 +156,9 @@ Feature: Enrolment on course completion
And I click on "Course completion" "link" in the "region-main" "region"
And I follow "Click to mark user complete"
And I log out
And I log in as "admin"
And I wait "1" seconds
And I run the scheduled task "core\task\completion_regular_task"
And I run all adhoc tasks
And I log in as "admin"
And I am on "Course 2" course homepage
And I navigate to course participants
And I click on "Select all" "checkbox"
Expand All @@ -184,10 +178,9 @@ Feature: Enrolment on course completion
And I click on "Course completion" "link" in the "region-main" "region"
And I follow "Click to mark user complete"
And I log out
And I log in as "admin"
And I wait "1" seconds
And I run the scheduled task "core\task\completion_regular_task"
And I run all adhoc tasks
And I am on "Course 2" course homepage
And I am on the "C2" "Course" page logged in as "admin"
And I navigate to course participants
When I click on "Select 'Username 1'" "checkbox"
And I set the field "With selected users..." to "Edit selected enrolments on course completion"
Expand Down
1 change: 0 additions & 1 deletion tests/behat/enrol_groups.feature
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ Feature: Groups kept during enrolment on course completion
And I follow "Click to mark user complete"
And I wait "1" seconds
And I run the scheduled task "core\task\completion_regular_task"
And I run all adhoc tasks

And I am on "Course 2" course homepage
And I navigate to course participants
Expand Down
4 changes: 2 additions & 2 deletions tests/enrol_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ protected function setUp(): void {
/**
* Test if user is enrolled after completing a course.
* @covers \enrol_coursecompleted\observer
* @covers \enrol_coursecompleted\user_enrolment_callbacks
*/
public function test_event_enrolled(): void {
global $PAGE;
Expand Down Expand Up @@ -135,6 +136,7 @@ public function test_event_enrolled(): void {
/**
* Test if user is enrolled after completing a course.
* @covers \enrol_coursecompleted_plugin
* @covers \enrol_coursecompleted\user_enrolment_callbacks
*/
public function test_enrolled_after_completion(): void {
global $PAGE;
Expand All @@ -146,7 +148,6 @@ public function test_enrolled_after_completion(): void {
'100',
\core_completion\progress::get_course_progress_percentage($this->course1, $this->student->id)
);
$this->runAdhocTasks();
$manager = new \course_enrolment_manager($PAGE, $this->course2);
$this->assertCount(1, $manager->get_user_enrolments($this->student->id));
}
Expand All @@ -163,7 +164,6 @@ public function test_user_edit(): void {
'100',
\core_completion\progress::get_course_progress_percentage($this->course1, $this->student->id)
);
$this->runAdhocTasks();
$this->setAdminUser();
$context = \context_course::instance($this->course1->id);
$this->assertTrue(has_capability('report/completion:view', $context));
Expand Down
Loading

0 comments on commit a2b208f

Please sign in to comment.