first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,41 @@
<?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/>.
/**
* Privacy Subsystem implementation for enrol_database.
*
* @package enrol_database
* @copyright 2018 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_database\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for enrol_database implementing null_provider.
*
* @copyright 2018 Carlos Escobedo <carlos@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
@@ -0,0 +1,63 @@
<?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/>.
/**
* Sync enrolments task
* @package enrol_database
* @copyright 2018 Daniel Neis Araujo <danielneis@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_database\task;
defined('MOODLE_INTERNAL') || die();
/**
* Class sync_enrolments
* @package enrol_database
* @copyright 2018 Daniel Neis Araujo <danielneis@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class sync_enrolments extends \core\task\scheduled_task {
/**
* Name for this task.
*
* @return string
*/
public function get_name() {
return get_string('syncenrolmentstask', 'enrol_database');
}
/**
* Run task for synchronising users.
*/
public function execute() {
$trace = new \text_progress_trace();
if (!enrol_is_enabled('database')) {
$trace->output('Plugin not enabled');
return;
}
$enrol = enrol_get_plugin('database');
// Update enrolments -- these handlers should autocreate courses if required.
$enrol->sync_courses($trace);
$enrol->sync_enrolments($trace);
}
}
+45
View File
@@ -0,0 +1,45 @@
<?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/>.
/**
* Capabilities for database enrolment plugin.
*
* @package enrol_database
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$capabilities = array(
/* This is used only when sync suspends users instead of full unenrolment. */
'enrol/database:unenrol' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'manager' => CAP_ALLOW,
)
),
'enrol/database:config' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
'manager' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
)
),
);
+30
View File
@@ -0,0 +1,30 @@
<?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/>.
/**
* Database enrolment plugin installation.
*
* @package enrol_database
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
function xmldb_enrol_database_install() {
global $CFG, $DB;
}
+37
View File
@@ -0,0 +1,37 @@
<?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/>.
/**
* Task definition for enrol_database.
* @package enrol_database
* @copyright 2018 Daniel Neis Araujo <danielneis@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$tasks = array(
array(
'classname' => '\enrol_database\task\sync_enrolments',
'blocking' => 0,
'minute' => 'R',
'hour' => 'R',
'day' => '*',
'month' => '*',
'dayofweek' => '*',
'disabled' => 1
)
);
+39
View File
@@ -0,0 +1,39 @@
<?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/>.
/**
* Database enrolment plugin upgrade.
*
* @package enrol_database
* @copyright 2011 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
function xmldb_enrol_database_upgrade($oldversion) {
// Automatically generated Moodle v4.1.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.2.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.3.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.4.0 release upgrade line.
// Put any upgrade step following this.
return true;
}
+77
View File
@@ -0,0 +1,77 @@
<?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/>.
/**
* Strings for component 'enrol_database', language 'en'.
*
* @package enrol_database
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$string['database:config'] = 'Configure database enrol instances';
$string['database:unenrol'] = 'Unenrol suspended users';
$string['dbencoding'] = 'Database encoding';
$string['dbhost'] = 'Database host';
$string['dbhost_desc'] = 'Type database server IP address or host name. Use a system DSN name if using ODBC. Use a PDO DSN if using PDO.';
$string['dbname'] = 'Database name';
$string['dbname_desc'] = 'Leave empty if using a DSN name in database host.';
$string['dbpass'] = 'Database password';
$string['dbsetupsql'] = 'Database setup command';
$string['dbsetupsql_desc'] = 'SQL command for special database setup, often used to setup communication encoding - example for MySQL and PostgreSQL: <em>SET NAMES \'utf8\'</em>';
$string['dbsybasequoting'] = 'Use sybase quotes';
$string['dbsybasequoting_desc'] = 'Sybase style single quote escaping - needed for Oracle, MS SQL and some other databases. Do not use for MySQL!';
$string['dbtype'] = 'Database driver';
$string['dbtype_desc'] = 'ADOdb database driver name, type of the external database engine.';
$string['dbuser'] = 'Database user';
$string['debugdb'] = 'Debug ADOdb';
$string['debugdb_desc'] = 'Debug ADOdb connection to external database - use when getting empty page during login. Not suitable for production sites!';
$string['defaultcategory'] = 'Default new course category';
$string['defaultcategory_desc'] = 'The default category for auto-created courses. Used when no new category id specified or not found.';
$string['defaultrole'] = 'Default role';
$string['defaultrole_desc'] = 'The role that will be assigned by default if no other role is specified in external table.';
$string['ignorehiddencourses'] = 'Ignore hidden courses';
$string['ignorehiddencourses_desc'] = 'If enabled users will not be enrolled on courses that are set to be unavailable to students.';
$string['localcategoryfield'] = 'Local category field';
$string['localcoursefield'] = 'Local course field';
$string['localrolefield'] = 'Local role field';
$string['localuserfield'] = 'Local user field';
$string['newcoursetable'] = 'Remote new courses table';
$string['newcoursetable_desc'] = 'Specify of the name of the table that contains list of courses that should be created automatically. Empty means no courses are created.';
$string['newcoursecategory'] = 'New course category field';
$string['newcoursefullname'] = 'New course full name field';
$string['newcourseidnumber'] = 'New course ID number field';
$string['newcourseshortname'] = 'New course short name field';
$string['pluginname'] = 'External database';
$string['pluginname_desc'] = 'You can use an external database (of nearly any kind) to control your enrolments. It is assumed your external database contains at least a field containing a course ID, and a field containing a user ID. These are compared against fields that you choose in the local course and user tables.';
$string['remotecoursefield'] = 'Remote course field';
$string['remotecoursefield_desc'] = 'The name of the field in the remote table that we are using to match entries in the course table.';
$string['remoteenroltable'] = 'Remote user enrolment table';
$string['remoteenroltable_desc'] = 'Specify the name of the table that contains list of user enrolments. Empty means no user enrolment sync.';
$string['remoteotheruserfield'] = 'Remote Other User field';
$string['remoteotheruserfield_desc'] = 'The name of the field in the remote table that we are using to flag "Other User" role assignments.';
$string['remoterolefield'] = 'Remote role field';
$string['remoterolefield_desc'] = 'The name of the field in the remote table that we are using to match entries in the roles table.';
$string['remoteuserfield'] = 'Remote user field';
$string['settingsheaderdb'] = 'External database connection';
$string['settingsheaderlocal'] = 'Local field mapping';
$string['settingsheaderremote'] = 'Remote enrolment sync';
$string['settingsheadernewcourses'] = 'Creation of new courses';
$string['syncenrolmentstask'] = 'Synchronise external database enrolments task';
$string['remoteuserfield_desc'] = 'The name of the field in the remote table that we are using to match entries in the user table.';
$string['templatecourse'] = 'New course template';
$string['templatecourse_desc'] = 'Optional: auto-created courses can copy their settings from a template course. Type here the shortname of the template course.';
$string['privacy:metadata'] = 'The External database enrolment plugin does not store any personal data.';
File diff suppressed because it is too large Load Diff
+123
View File
@@ -0,0 +1,123 @@
<?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/>.
/**
* Database enrolment plugin settings and presets.
*
* @package enrol_database
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
if ($ADMIN->fulltree) {
//--- general settings -----------------------------------------------------------------------------------
$settings->add(new admin_setting_heading('enrol_database_settings', '', get_string('pluginname_desc', 'enrol_database')));
$settings->add(new admin_setting_heading('enrol_database_exdbheader', get_string('settingsheaderdb', 'enrol_database'), ''));
$options = array('', "access", "ado_access", "ado", "ado_mssql", "borland_ibase", "csv", "db2", "fbsql", "firebird", "ibase", "informix72", "informix", "mssql", "mssql_n", "mssqlnative", "mysql", "mysqli", "mysqlt", "oci805", "oci8", "oci8po", "odbc", "odbc_mssql", "odbc_oracle", "oracle", "pdo", "postgres64", "postgres7", "postgres", "proxy", "sqlanywhere", "sybase", "vfp");
$options = array_combine($options, $options);
$settings->add(new admin_setting_configselect('enrol_database/dbtype', get_string('dbtype', 'enrol_database'), get_string('dbtype_desc', 'enrol_database'), '', $options));
$settings->add(new admin_setting_configtext('enrol_database/dbhost', get_string('dbhost', 'enrol_database'), get_string('dbhost_desc', 'enrol_database'), 'localhost'));
$settings->add(new admin_setting_configtext('enrol_database/dbuser', get_string('dbuser', 'enrol_database'), '', ''));
$settings->add(new admin_setting_configpasswordunmask('enrol_database/dbpass', get_string('dbpass', 'enrol_database'), '', ''));
$settings->add(new admin_setting_configtext('enrol_database/dbname', get_string('dbname', 'enrol_database'), get_string('dbname_desc', 'enrol_database'), ''));
$settings->add(new admin_setting_configtext('enrol_database/dbencoding', get_string('dbencoding', 'enrol_database'), '', 'utf-8'));
$settings->add(new admin_setting_configtext('enrol_database/dbsetupsql', get_string('dbsetupsql', 'enrol_database'), get_string('dbsetupsql_desc', 'enrol_database'), ''));
$settings->add(new admin_setting_configcheckbox('enrol_database/dbsybasequoting', get_string('dbsybasequoting', 'enrol_database'), get_string('dbsybasequoting_desc', 'enrol_database'), 0));
$settings->add(new admin_setting_configcheckbox('enrol_database/debugdb', get_string('debugdb', 'enrol_database'), get_string('debugdb_desc', 'enrol_database'), 0));
$settings->add(new admin_setting_heading('enrol_database_localheader', get_string('settingsheaderlocal', 'enrol_database'), ''));
$options = array('id'=>'id', 'idnumber'=>'idnumber', 'shortname'=>'shortname');
$settings->add(new admin_setting_configselect('enrol_database/localcoursefield', get_string('localcoursefield', 'enrol_database'), '', 'idnumber', $options));
$options = array('id'=>'id', 'idnumber'=>'idnumber', 'email'=>'email', 'username'=>'username'); // only local users if username selected, no mnet users!
$settings->add(new admin_setting_configselect('enrol_database/localuserfield', get_string('localuserfield', 'enrol_database'), '', 'idnumber', $options));
$options = array('id'=>'id', 'shortname'=>'shortname');
$settings->add(new admin_setting_configselect('enrol_database/localrolefield', get_string('localrolefield', 'enrol_database'), '', 'shortname', $options));
$options = array('id'=>'id', 'idnumber'=>'idnumber');
$settings->add(new admin_setting_configselect('enrol_database/localcategoryfield', get_string('localcategoryfield', 'enrol_database'), '', 'id', $options));
$settings->add(new admin_setting_heading('enrol_database_remoteheader', get_string('settingsheaderremote', 'enrol_database'), ''));
$settings->add(new admin_setting_configtext('enrol_database/remoteenroltable', get_string('remoteenroltable', 'enrol_database'), get_string('remoteenroltable_desc', 'enrol_database'), ''));
$settings->add(new admin_setting_configtext('enrol_database/remotecoursefield', get_string('remotecoursefield', 'enrol_database'), get_string('remotecoursefield_desc', 'enrol_database'), ''));
$settings->add(new admin_setting_configtext('enrol_database/remoteuserfield', get_string('remoteuserfield', 'enrol_database'), get_string('remoteuserfield_desc', 'enrol_database'), ''));
$settings->add(new admin_setting_configtext('enrol_database/remoterolefield', get_string('remoterolefield', 'enrol_database'), get_string('remoterolefield_desc', 'enrol_database'), ''));
$otheruserfieldlabel = get_string('remoteotheruserfield', 'enrol_database');
$otheruserfielddesc = get_string('remoteotheruserfield_desc', 'enrol_database');
$settings->add(new admin_setting_configtext('enrol_database/remoteotheruserfield', $otheruserfieldlabel, $otheruserfielddesc, ''));
if (!during_initial_install()) {
$options = get_default_enrol_roles(context_system::instance());
$student = get_archetype_roles('student');
$student = reset($student);
$settings->add(new admin_setting_configselect('enrol_database/defaultrole',
get_string('defaultrole', 'enrol_database'),
get_string('defaultrole_desc', 'enrol_database'),
$student->id ?? null,
$options));
}
$settings->add(new admin_setting_configcheckbox('enrol_database/ignorehiddencourses', get_string('ignorehiddencourses', 'enrol_database'), get_string('ignorehiddencourses_desc', 'enrol_database'), 0));
$options = array(ENROL_EXT_REMOVED_UNENROL => get_string('extremovedunenrol', 'enrol'),
ENROL_EXT_REMOVED_KEEP => get_string('extremovedkeep', 'enrol'),
ENROL_EXT_REMOVED_SUSPEND => get_string('extremovedsuspend', 'enrol'),
ENROL_EXT_REMOVED_SUSPENDNOROLES => get_string('extremovedsuspendnoroles', 'enrol'));
$settings->add(new admin_setting_configselect('enrol_database/unenrolaction', get_string('extremovedaction', 'enrol'), get_string('extremovedaction_help', 'enrol'), ENROL_EXT_REMOVED_UNENROL, $options));
$settings->add(new admin_setting_heading('enrol_database_newcoursesheader', get_string('settingsheadernewcourses', 'enrol_database'), ''));
$settings->add(new admin_setting_configtext('enrol_database/newcoursetable', get_string('newcoursetable', 'enrol_database'), get_string('newcoursetable_desc', 'enrol_database'), ''));
$settings->add(new admin_setting_configtext('enrol_database/newcoursefullname', get_string('newcoursefullname', 'enrol_database'), '', 'fullname'));
$settings->add(new admin_setting_configtext('enrol_database/newcourseshortname', get_string('newcourseshortname', 'enrol_database'), '', 'shortname'));
$settings->add(new admin_setting_configtext('enrol_database/newcourseidnumber', get_string('newcourseidnumber', 'enrol_database'), '', 'idnumber'));
$settings->add(new admin_setting_configtext('enrol_database/newcoursecategory', get_string('newcoursecategory', 'enrol_database'), '', ''));
$settings->add(new admin_settings_coursecat_select('enrol_database/defaultcategory',
get_string('defaultcategory', 'enrol_database'),
get_string('defaultcategory_desc', 'enrol_database'), 1));
$settings->add(new admin_setting_configtext('enrol_database/templatecourse', get_string('templatecourse', 'enrol_database'), get_string('templatecourse_desc', 'enrol_database'), ''));
}
+101
View File
@@ -0,0 +1,101 @@
<?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/>.
/**
* Database enrolment tests.
*
* @package enrol_database
* @copyright 2017 Jun Pataleta
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_database;
use course_enrolment_manager;
defined('MOODLE_INTERNAL') || die();
/**
* Database enrolment tests.
*
* @package enrol_database
* @copyright 2017 Jun Pataleta
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class lib_test extends \advanced_testcase {
public static function tearDownAfterClass(): void {
global $DB;
// Apply sqlsrv native driver error and logging default
// settings while finishing the AdoDB tests.
if ($DB->get_dbfamily() === 'mssql') {
sqlsrv_configure("WarningsReturnAsErrors", false);
sqlsrv_configure("LogSubsystems", SQLSRV_LOG_SYSTEM_OFF);
sqlsrv_configure("LogSeverity", SQLSRV_LOG_SEVERITY_ERROR);
}
}
/**
* Test for getting user enrolment actions.
*/
public function test_get_user_enrolment_actions(): void {
global $CFG, $PAGE;
$this->resetAfterTest();
// Set page URL to prevent debugging messages.
$PAGE->set_url('/enrol/editinstance.php');
$pluginname = 'database';
// Only enable the database enrol plugin.
$CFG->enrol_plugins_enabled = $pluginname;
$generator = $this->getDataGenerator();
// Get the enrol plugin.
$plugin = enrol_get_plugin($pluginname);
// Create a course.
$course = $generator->create_course();
// Enable this enrol plugin for the course.
$plugin->add_instance($course);
// Create a student.
$student = $generator->create_user();
// Enrol the student to the course.
$generator->enrol_user($student->id, $course->id, 'student', $pluginname);
// Teachers don't have enrol/database:unenrol capability by default. Login as admin for simplicity.
$this->setAdminUser();
require_once($CFG->dirroot . '/enrol/locallib.php');
$manager = new course_enrolment_manager($PAGE, $course);
$userenrolments = $manager->get_user_enrolments($student->id);
$this->assertCount(1, $userenrolments);
$ue = reset($userenrolments);
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Database enrol has 0 enrol actions for active users.
$this->assertCount(0, $actions);
// Enrol actions for a suspended student.
// Suspend the student.
$ue->status = ENROL_USER_SUSPENDED;
$actions = $plugin->get_user_enrolment_actions($manager, $ue);
// Database enrol has enrol actions for suspended students -- unenrol.
$this->assertCount(1, $actions);
}
}
+805
View File
@@ -0,0 +1,805 @@
<?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_database;
/**
* External database enrolment sync tests
*
* This also tests adodb drivers that are matching
* our four supported Moodle database drivers.
*
* @package enrol_database
* @category test
* @copyright 2011 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class sync_test extends \advanced_testcase {
protected static $courses = array();
protected static $users = array();
protected static $roles = array();
/** @var string Original error log */
protected $oldlog;
public static function tearDownAfterClass(): void {
global $DB;
// Apply sqlsrv native driver error and logging default
// settings while finishing the AdoDB tests.
if ($DB->get_dbfamily() === 'mssql') {
sqlsrv_configure("WarningsReturnAsErrors", false);
sqlsrv_configure("LogSubsystems", SQLSRV_LOG_SYSTEM_OFF);
sqlsrv_configure("LogSeverity", SQLSRV_LOG_SEVERITY_ERROR);
}
}
protected function init_enrol_database() {
global $DB, $CFG;
// Discard error logs from AdoDB.
$this->oldlog = ini_get('error_log');
ini_set('error_log', "$CFG->dataroot/testlog.log");
$dbman = $DB->get_manager();
set_config('dbencoding', 'utf-8', 'enrol_database');
set_config('dbhost', $CFG->dbhost, 'enrol_database');
set_config('dbuser', $CFG->dbuser, 'enrol_database');
set_config('dbpass', $CFG->dbpass, 'enrol_database');
set_config('dbname', $CFG->dbname, 'enrol_database');
if (!empty($CFG->dboptions['dbport'])) {
set_config('dbhost', $CFG->dbhost.':'.$CFG->dboptions['dbport'], 'enrol_database');
}
switch ($DB->get_dbfamily()) {
case 'mysql':
set_config('dbtype', 'mysqli', 'enrol_database');
set_config('dbsetupsql', "SET NAMES 'UTF-8'", 'enrol_database');
set_config('dbsybasequoting', '0', 'enrol_database');
if (!empty($CFG->dboptions['dbsocket'])) {
$dbsocket = $CFG->dboptions['dbsocket'];
if ((strpos($dbsocket, '/') === false and strpos($dbsocket, '\\') === false)) {
$dbsocket = ini_get('mysqli.default_socket');
}
set_config('dbtype', 'mysqli://'.rawurlencode($CFG->dbuser).':'.rawurlencode($CFG->dbpass).'@'.rawurlencode($CFG->dbhost).'/'.rawurlencode($CFG->dbname).'?socket='.rawurlencode($dbsocket), 'enrol_database');
}
break;
case 'oracle':
set_config('dbtype', 'oci8po', 'enrol_database');
set_config('dbsybasequoting', '1', 'enrol_database');
break;
case 'postgres':
set_config('dbtype', 'postgres7', 'enrol_database');
$setupsql = "SET NAMES 'UTF-8'";
if (!empty($CFG->dboptions['dbschema'])) {
$setupsql .= "; SET search_path = '".$CFG->dboptions['dbschema']."'";
}
set_config('dbsetupsql', $setupsql, 'enrol_database');
set_config('dbsybasequoting', '0', 'enrol_database');
if (!empty($CFG->dboptions['dbsocket']) and ($CFG->dbhost === 'localhost' or $CFG->dbhost === '127.0.0.1')) {
if (strpos($CFG->dboptions['dbsocket'], '/') !== false) {
$socket = $CFG->dboptions['dbsocket'];
if (!empty($CFG->dboptions['dbport'])) {
$socket .= ':' . $CFG->dboptions['dbport'];
}
set_config('dbhost', $socket, 'enrol_database');
} else {
set_config('dbhost', '', 'enrol_database');
}
}
break;
case 'mssql':
set_config('dbtype', 'mssqlnative', 'enrol_database');
set_config('dbsybasequoting', '1', 'enrol_database');
// The native sqlsrv driver uses a comma as separator between host and port.
$dbhost = $CFG->dbhost;
if (!empty($dboptions['dbport'])) {
$dbhost .= ',' . $dboptions['dbport'];
}
set_config('dbhost', $dbhost, 'enrol_database');
break;
default:
throw new exception('Unknown database driver '.get_class($DB));
}
// NOTE: It is stongly discouraged to create new tables in advanced_testcase classes,
// but there is no other simple way to test ext database enrol sync, so let's
// disable transactions are try to cleanup after the tests.
$table = new \xmldb_table('enrol_database_test_enrols');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('courseid', XMLDB_TYPE_CHAR, '255', null, null, null);
$table->add_field('userid', XMLDB_TYPE_CHAR, '255', null, null, null);
$table->add_field('roleid', XMLDB_TYPE_CHAR, '255', null, null, null);
$table->add_field('otheruser', XMLDB_TYPE_CHAR, '1', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
$dbman->create_table($table);
set_config('remoteenroltable', $CFG->prefix.'enrol_database_test_enrols', 'enrol_database');
set_config('remotecoursefield', 'courseid', 'enrol_database');
set_config('remoteuserfield', 'userid', 'enrol_database');
set_config('remoterolefield', 'roleid', 'enrol_database');
set_config('remoteotheruserfield', 'otheruser', 'enrol_database');
$table = new \xmldb_table('enrol_database_test_courses');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
$table->add_field('fullname', XMLDB_TYPE_CHAR, '255', null, null, null);
$table->add_field('shortname', XMLDB_TYPE_CHAR, '255', null, null, null);
$table->add_field('idnumber', XMLDB_TYPE_CHAR, '255', null, null, null);
$table->add_field('category', XMLDB_TYPE_CHAR, '255', null, null, null);
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
}
$dbman->create_table($table);
set_config('newcoursetable', $CFG->prefix.'enrol_database_test_courses', 'enrol_database');
set_config('newcoursefullname', 'fullname', 'enrol_database');
set_config('newcourseshortname', 'shortname', 'enrol_database');
set_config('newcourseidnumber', 'idnumber', 'enrol_database');
set_config('newcoursecategory', 'category', 'enrol_database');
// Create some test users and courses.
for ($i = 1; $i <= 4; $i++) {
self::$courses[$i] = $this->getDataGenerator()->create_course(array('fullname' => 'Test course '.$i, 'shortname' => 'tc'.$i, 'idnumber' => 'courseid'.$i));
}
for ($i = 1; $i <= 10; $i++) {
self::$users[$i] = $this->getDataGenerator()->create_user(array('username' => 'username'.$i, 'idnumber' => 'userid'.$i, 'email' => 'user'.$i.'@example.com'));
}
foreach (get_all_roles() as $role) {
self::$roles[$role->shortname] = $role;
}
}
protected function cleanup_enrol_database() {
global $DB;
$dbman = $DB->get_manager();
$table = new \xmldb_table('enrol_database_test_enrols');
$dbman->drop_table($table);
$table = new \xmldb_table('enrol_database_test_courses');
$dbman->drop_table($table);
self::$courses = null;
self::$users = null;
self::$roles = null;
ini_set('error_log', $this->oldlog);
}
protected function reset_enrol_database() {
global $DB;
$DB->delete_records('enrol_database_test_enrols', array());
$DB->delete_records('enrol_database_test_courses', array());
$plugin = enrol_get_plugin('database');
$instances = $DB->get_records('enrol', array('enrol' => 'database'));
foreach($instances as $instance) {
$plugin->delete_instance($instance);
}
}
protected function assertIsEnrolled($userindex, $courseindex, $status=null, $rolename = null) {
global $DB;
$dbinstance = $DB->get_record('enrol', array('courseid' => self::$courses[$courseindex]->id, 'enrol' => 'database'), '*', MUST_EXIST);
$conditions = array('enrolid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id);
if ($status !== null) {
$conditions['status'] = $status;
}
$this->assertTrue($DB->record_exists('user_enrolments', $conditions));
$this->assertHasRoleAssignment($userindex, $courseindex, $rolename);
}
protected function assertHasRoleAssignment($userindex, $courseindex, $rolename = null) {
global $DB;
$dbinstance = $DB->get_record('enrol', array('courseid' => self::$courses[$courseindex]->id, 'enrol' => 'database'), '*', MUST_EXIST);
$coursecontext = \context_course::instance(self::$courses[$courseindex]->id);
if ($rolename === false) {
$this->assertFalse($DB->record_exists('role_assignments', array('component' => 'enrol_database', 'itemid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id, 'contextid' => $coursecontext->id)));
} else if ($rolename !== null) {
$this->assertTrue($DB->record_exists('role_assignments', array('component' => 'enrol_database', 'itemid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id, 'contextid' => $coursecontext->id, 'roleid' => self::$roles[$rolename]->id)));
}
}
protected function assertIsNotEnrolled($userindex, $courseindex) {
global $DB;
if (!$dbinstance = $DB->get_record('enrol', array('courseid' => self::$courses[$courseindex]->id, 'enrol' => 'database'))) {
return;
}
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id)));
}
public function test_sync_user_enrolments(): void {
global $DB;
$this->init_enrol_database();
$this->resetAfterTest(false);
$this->preventResetByRollback();
$plugin = enrol_get_plugin('database');
// Test basic enrol sync for one user after login.
$this->reset_enrol_database();
$plugin->set_config('localcoursefield', 'idnumber');
$plugin->set_config('localuserfield', 'idnumber');
$plugin->set_config('localrolefield', 'shortname');
$plugin->set_config('defaultrole', self::$roles['student']->id);
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid2', 'roleid' => 'teacher'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid2', 'courseid' => 'courseid1', 'roleid' => null));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid4', 'courseid' => 'courseid4', 'roleid' => 'editingteacher', 'otheruser' => '1'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'xxxxxxx', 'courseid' => 'courseid1', 'roleid' => 'student')); // Bogus record to be ignored.
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'xxxxxxxxx', 'roleid' => 'student')); // Bogus record to be ignored.
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
// Make sure there are no errors or changes on the next login.
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$plugin->sync_user_enrolments(self::$users[2]);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$plugin->sync_user_enrolments(self::$users[4]);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
// Enrolment removals.
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_KEEP);
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, false);
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsNotEnrolled(1, 1);
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid4', 'courseid' => 'courseid4', 'roleid' => 'editingteacher'));
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
$plugin->sync_user_enrolments(self::$users[4]);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, false);
// Test all other mapping options.
$this->reset_enrol_database();
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$plugin->set_config('localcoursefield', 'id');
$plugin->set_config('localuserfield', 'id');
$plugin->set_config('localrolefield', 'id');
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$this->reset_enrol_database();
$plugin->set_config('localcoursefield', 'shortname');
$plugin->set_config('localuserfield', 'email');
$plugin->set_config('localrolefield', 'id');
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[2]->shortname, 'roleid' => self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id));
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$this->reset_enrol_database();
$plugin->set_config('localcoursefield', 'id');
$plugin->set_config('localuserfield', 'username');
$plugin->set_config('localrolefield', 'id');
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
}
/**
* @depends test_sync_user_enrolments
*/
public function test_sync_users(): void {
global $DB;
$this->resetAfterTest(false);
$this->preventResetByRollback();
$this->reset_enrol_database();
$plugin = enrol_get_plugin('database');
$trace = new \null_progress_trace();
// Test basic enrol sync for one user after login.
$this->reset_enrol_database();
$plugin->set_config('localcoursefield', 'idnumber');
$plugin->set_config('localuserfield', 'idnumber');
$plugin->set_config('localrolefield', 'shortname');
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid2', 'roleid' => 'editingteacher'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid2', 'courseid' => 'courseid1', 'roleid' => 'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid4', 'courseid' => 'courseid4', 'roleid' => 'editingteacher', 'otheruser' => '1'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'xxxxxxx', 'courseid' => 'courseid1', 'roleid' => 'student')); // Bogus record to be ignored.
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'xxxxxxxxx', 'roleid' => 'student')); // Bogus record to be ignored.
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$plugin->sync_enrolments($trace);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$plugin->set_config('defaultrole', self::$roles['teacher']->id);
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid3', 'courseid' => 'courseid3'));
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
// Test different unenrolment options.
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_KEEP);
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, false);
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
$plugin->sync_enrolments($trace);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsNotEnrolled(1, 1);
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'teacher'));
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(6, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'teacher');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'teacher'));
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
// Test all other mapping options.
$this->reset_enrol_database();
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$plugin->set_config('localcoursefield', 'id');
$plugin->set_config('localuserfield', 'id');
$plugin->set_config('localrolefield', 'id');
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$plugin->sync_enrolments($trace);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->reset_enrol_database();
$plugin->set_config('localcoursefield', 'shortname');
$plugin->set_config('localuserfield', 'email');
$plugin->set_config('localrolefield', 'id');
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[2]->shortname, 'roleid' => self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id));
$plugin->sync_enrolments($trace);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->reset_enrol_database();
$plugin->set_config('localcoursefield', 'id');
$plugin->set_config('localuserfield', 'username');
$plugin->set_config('localrolefield', 'id');
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$plugin->sync_enrolments($trace);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
// Test sync of one course only.
$this->reset_enrol_database();
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$plugin->sync_enrolments($trace, self::$courses[3]->id);
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
$this->assertEquals(1, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$plugin->sync_enrolments($trace, self::$courses[1]->id);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$plugin->sync_enrolments($trace, self::$courses[2]->id);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
$DB->delete_records('enrol_database_test_enrols', array());
$plugin->sync_enrolments($trace, self::$courses[1]->id);
$this->assertEquals(1, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(1, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$plugin->sync_enrolments($trace, self::$courses[2]->id);
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
}
/**
* @depends test_sync_users
*/
public function test_sync_courses(): void {
global $DB;
$this->resetAfterTest(true);
$this->preventResetByRollback();
$this->reset_enrol_database();
$plugin = enrol_get_plugin('database');
$trace = new \null_progress_trace();
$plugin->set_config('localcategoryfield', 'id');
$coursecat = $this->getDataGenerator()->create_category(array('name' => 'Test category 1', 'idnumber' => 'tcid1'));
$defcat = $DB->get_record('course_categories', array('id' => $plugin->get_config('defaultcategory')));
$course1 = array('fullname' => 'New course 1', 'shortname' => 'nc1', 'idnumber' => 'ncid1', 'category' => $coursecat->id);
$course2 = array('fullname' => 'New course 2', 'shortname' => 'nc2', 'idnumber' => 'ncid2', 'category' => null);
// Duplicate records are to be ignored.
$course3 = array('fullname' => 'New course 3', 'shortname' => 'xx', 'idnumber' => 'yy2', 'category' => $defcat->id);
$course4 = array('fullname' => 'New course 4', 'shortname' => 'xx', 'idnumber' => 'yy3', 'category' => $defcat->id);
$course5 = array('fullname' => 'New course 5', 'shortname' => 'xx1', 'idnumber' => 'yy', 'category' => $defcat->id);
$course6 = array('fullname' => 'New course 6', 'shortname' => 'xx2', 'idnumber' => 'yy', 'category' => $defcat->id);
$DB->insert_record('enrol_database_test_courses', $course1);
$DB->insert_record('enrol_database_test_courses', $course2);
$DB->insert_record('enrol_database_test_courses', $course3);
$DB->insert_record('enrol_database_test_courses', $course4);
$DB->insert_record('enrol_database_test_courses', $course5);
$DB->insert_record('enrol_database_test_courses', $course6);
$this->assertEquals(1+count(self::$courses), $DB->count_records('course'));
$plugin->sync_courses($trace);
$this->assertEquals(4+1+count(self::$courses), $DB->count_records('course'));
$this->assertTrue($DB->record_exists('course', $course1));
$course2['category'] = $defcat->id;
$this->assertTrue($DB->record_exists('course', $course2));
// People should NOT push duplicates there because the results are UNDEFINED! But anyway skip the duplicates.
$this->assertEquals(1, $DB->count_records('course', array('idnumber' => 'yy')));
$this->assertEquals(1, $DB->count_records('course', array('shortname' => 'xx')));
// Check default number of sections matches with the created course sections.
$recordcourse1 = $DB->get_record('course', $course1);
$courseconfig = get_config('moodlecourse');
$numsections = $DB->count_records('course_sections', array('course' => $recordcourse1->id));
// To compare numsections we have to add topic 0 to default numsections.
$this->assertEquals(($courseconfig->numsections + 1), $numsections);
// Test category mapping via idnumber.
$plugin->set_config('localcategoryfield', 'idnumber');
$course7 = array('fullname' => 'New course 7', 'shortname' => 'nc7', 'idnumber' => 'ncid7', 'category' => 'tcid1');
$DB->insert_record('enrol_database_test_courses', $course7);
$plugin->sync_courses($trace);
$this->assertEquals(1+4+1+count(self::$courses), $DB->count_records('course'));
$this->assertTrue($DB->record_exists('course', $course1));
$this->assertTrue($DB->record_exists('course', $course2));
$course7['category'] = $coursecat->id;
$this->assertTrue($DB->record_exists('course', $course7));
// Test course template.
$template = $this->getDataGenerator()->create_course(array('numsections' => 666, 'shortname' => 'crstempl'));
$plugin->set_config('templatecourse', 'crstempl');
$course8 = array('fullname' => 'New course 8', 'shortname' => 'nc8', 'idnumber' => 'ncid8', 'category' => null);
$DB->insert_record('enrol_database_test_courses', $course8);
$plugin->sync_courses($trace);
$this->assertEquals(2+1+4+1+count(self::$courses), $DB->count_records('course'));
$course8['category'] = $defcat->id;
$record = $DB->get_record('course', $course8);
$this->assertFalse(empty($record));
$this->assertEquals(666, course_get_format($record)->get_last_section_number());
// Test invalid category.
$course9 = array('fullname' => 'New course 9', 'shortname' => 'nc9', 'idnumber' => 'ncid9', 'category' => 'xxxxxxx');
$DB->insert_record('enrol_database_test_courses', $course9);
$plugin->sync_courses($trace);
$this->assertEquals(2+1+4+1+count(self::$courses), $DB->count_records('course'));
$this->assertFalse($DB->record_exists('course', array('idnumber' => 'ncid9')));
// Test when categories not specified.
$plugin->set_config('newcoursecategory', '');
$plugin->sync_courses($trace);
$this->assertEquals(1+2+1+4+1+count(self::$courses), $DB->count_records('course'));
$this->assertTrue($DB->record_exists('course', array('idnumber' => 'ncid9')));
// Final cleanup - remove extra tables, fixtures and caches.
$this->cleanup_enrol_database();
}
}
+11
View File
@@ -0,0 +1,11 @@
This files describes API changes in the enrol_database code.
=== 3.11 ===
* Final deprecation enrol/database/cli/sync.php. Refer below for substitute.
=== 3.9 ===
* Class enrol_database_admin_setting_category has been removed. This class was only used by the database
enrolment plugin settings and it was replaced by admin_settings_coursecat_select.
=== 3.7 ===
* enrol/database/cli/sync.php script has been deprecated in favour of enrol_database\task\sync_enrolments task.
+29
View File
@@ -0,0 +1,29 @@
<?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/>.
/**
* Database enrolment plugin version specification.
*
* @package enrol_database
* @copyright 2010 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024042200; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2024041600; // Requires this Moodle version.
$plugin->component = 'enrol_database'; // Full name of the plugin (used for diagnostics)