Skip to content

Commit

Permalink
generator
Browse files Browse the repository at this point in the history
  • Loading branch information
rdebleu committed Sep 14, 2024
1 parent 725c40e commit d5955e9
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tests/behat/generated.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
@enrol @ewallah @enrol_coursecompleted
Feature: Enroll users into a course based on the completion of another course
In order to manage course dependencies
As an administrator
I want to ensure users are automatically enrolled into a new course after completing a required course

Background:
Given the following "courses" exist:
| fullname | shortname | idnumber | enablecompletion |
| Required Course | reqcourse | req001 | 1 |
| Target Course | target | tar001 | 1 |

And the following "users" exist:
| username | firstname | lastname |
| student1 | Student | One |
| student2 | Student | Two |
| teacher1 | Teacher | One |

And the following "course enrolments" exist:
| user | course | role |
| student1 | reqcourse | student |
| student2 | reqcourse | student |
| teacher1 | target | teacher |

And I log in as "admin"
And the following "enrol_coursecompleted > courseenrolment" exist:
| course | required |
| target | reqcourse |

And the following "enrol_coursecompleted > coursecompletion" exist:
| course | user |
| reqcourse | student1 |
| target | student1 |

Scenario: Admins can see enrolment after course completion
Given I log in as "admin"
When I am on the "target" "enrolment methods" page
Then I should see "After completing course: reqcourse"

@javascript
Scenario: Teachers can see enrolment after course completion
Given I log in as "teacher1"
And I am on "target" course homepage
When I navigate to course participants
Then I should see "Student1"

@javascript
Scenario: Verify automatic enrollment after course completion
Given I am on the "My courses" page logged in as "student1"
Then I should see "Target Course"

Scenario: Verify no enrollment without course completion
Given I am on the "My courses" page logged in as "student2"
Then I should not see "Target Course"
55 changes: 55 additions & 0 deletions tests/generator/behat_enrol_coursecompleted_generator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?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/>.

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

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

/**
* Functions supported.
*
* @return array
*/
protected function get_creatable_entities(): array {
return [
'coursecompletions' => [
'singular' => 'coursecompletion',
'datagenerator' => 'coursecompletion',
'required' => ['course', 'user'],
],
'courseenrolments' => [
'singular' => 'courseenrolment',
'datagenerator' => 'courseenrolment',
'required' => ['course', 'required'],
],
];
}
}
119 changes: 119 additions & 0 deletions tests/generator/lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?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/>.

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

/**
* Enrol coursecompleted generator.
*
* @package enrol_coursecompleted
* @copyright eWallah.net
* @author Renaat Debleu <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class enrol_coursecompleted_generator extends component_generator_base {
/**
* Create course completion
* @param array $data containing course and user
*/
public function create_coursecompletion(array $data) {
// Should we not complete a course, but better fire a coursecompleted event.
global $DB;
if (!isset($data['course'])) {
throw new coding_exception('Must specify course when creating course completion.');
}

if (!isset($data['user'])) {
throw new coding_exception('Must specify a user name when creating a course completion.');
}

if (clean_param($data['course'], PARAM_TEXT) !== $data['course']) {
throw new coding_exception('Course name must be PARAM_TEXT.');
}

if (clean_param($data['user'], PARAM_TEXT) !== $data['user']) {
throw new coding_exception('User name must be PARAM_TEXT.');
}

if (!$courseid = $DB->get_field('course', 'id', ['shortname' => $data['course']])) {
throw new Exception("A course with shortname '{$course}' does not exist");
}
if (!$userid = $DB->get_field('user', 'id', ['username' => $data['user']])) {
throw new Exception("A user with username '{$username}' does not exist");
}
$compevent = \core\event\course_completed::create(
[
'objectid' => $courseid,
'relateduserid' => $userid,
'context' => \context_course::instance($courseid),
'courseid' => $courseid,
'other' => ['relateduserid' => $userid],
]
);
$compevent->trigger();
$task = new \core\task\completion_regular_task();
$task->execute();
sleep(1);
$task->execute();
mark_user_dirty($userid);
rebuild_course_cache($courseid, true);
}

/**
* Create course enrolment on completion
* @param array $data of course and required
*/
public function create_courseenrolment(array $data) {
global $CFG, $DB;

if (!isset($data['course'])) {
throw new coding_exception('Must specify course when creating a course completion enrolment.');
}

if (!isset($data['required'])) {
throw new coding_exception('Must specify a required course when creating a course completion enrolment.');
}
if (clean_param($data['course'], PARAM_TEXT) !== $data['course']) {
throw new coding_exception('Course name must be PARAM_TEXT.');
}

if (clean_param($data['required'], PARAM_TEXT) !== $data['required']) {
throw new coding_exception('required course must be PARAM_TEXT.');
}

if (!$courseid = $DB->get_field('course', 'id', ['shortname' => $data['course']])) {
throw new Exception("A course with shortname '{$course}' does not exist");
}
if (!$requiredid = $DB->get_field('course', 'id', ['shortname' => $data['required']])) {
throw new Exception("A required course with shortname '{$required}' does not exist");
}
$CFG->enablecompletion = true;
$enabled = enrol_get_plugins(true);
$enabled['coursecompleted'] = true;
$enabled['guest'] = false;
set_config('enrol_plugins_enabled', implode(',', array_keys($enabled)));

$plugin = enrol_get_plugin('coursecompleted');
$plugin->add_instance(get_course($courseid), ['customint1' => $requiredid, 'customint2' => 1]);
}
}

0 comments on commit d5955e9

Please sign in to comment.