MDL-31845 add "Local category field" option in database enrol plugin

This patch uses code from the original patch by Michael Aherne, most of the credit goes to him.
This commit is contained in:
Petr Skoda 2012-03-10 14:01:03 +01:00
parent 23778a4dfa
commit e042c58df1
4 changed files with 10 additions and 5 deletions

View file

@ -44,12 +44,13 @@ $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['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'] = 'Ignore hidden courses';
$string['ignorehiddencourses_desc'] = 'If enabled users will not be enrolled on courses that are set to be unavailable to students.'; $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['localcoursefield'] = 'Local course field';
$string['localrolefield'] = 'Local role field'; $string['localrolefield'] = 'Local role field';
$string['localuserfield'] = 'Local user field'; $string['localuserfield'] = 'Local user field';
$string['newcoursetable'] = 'Remote new courses table'; $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['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 id field'; $string['newcoursecategory'] = 'New course category field';
$string['newcoursefullname'] = 'New course full name field'; $string['newcoursefullname'] = 'New course full name field';
$string['newcourseidnumber'] = 'New course ID number field'; $string['newcourseidnumber'] = 'New course ID number field';
$string['newcourseshortname'] = 'New course short name field'; $string['newcourseshortname'] = 'New course short name field';

View file

@ -622,6 +622,8 @@ class enrol_database_plugin extends enrol_plugin {
$idnumber = strtolower($this->get_config('newcourseidnumber')); $idnumber = strtolower($this->get_config('newcourseidnumber'));
$category = strtolower($this->get_config('newcoursecategory')); $category = strtolower($this->get_config('newcoursecategory'));
$localcategoryfield = $this->get_config('localcategoryfield', 'id');
$sqlfields = array($fullname, $shortname); $sqlfields = array($fullname, $shortname);
if ($category) { if ($category) {
$sqlfields[] = $category; $sqlfields[] = $category;
@ -653,9 +655,9 @@ class enrol_database_plugin extends enrol_plugin {
} }
continue; continue;
} }
if ($category and !$DB->record_exists('course_categories', array('id'=>$fields[$category]))) { if ($category and !$coursecategory = $DB->get_record('course_categories', array($localcategoryfield=>$fields[$category]), 'id')) {
if ($verbose) { if ($verbose) {
mtrace(' error: invalid category id, can not create course: '.$fields[$shortname]); mtrace(' error: invalid category '.$localcategoryfield.', can not create course: '.$fields[$shortname]);
} }
continue; continue;
} }
@ -663,7 +665,7 @@ class enrol_database_plugin extends enrol_plugin {
$course->fullname = $fields[$fullname]; $course->fullname = $fields[$fullname];
$course->shortname = $fields[$shortname]; $course->shortname = $fields[$shortname];
$course->idnumber = $idnumber ? $fields[$idnumber] : NULL; $course->idnumber = $idnumber ? $fields[$idnumber] : NULL;
$course->category = $category ? $fields[$category] : NULL; $course->category = $category ? $coursecategory->id : NULL;
$createcourses[] = $course; $createcourses[] = $course;
} }
} }

View file

@ -66,6 +66,8 @@ if ($ADMIN->fulltree) {
$options = array('id'=>'id', 'shortname'=>'shortname', 'fullname'=>'fullname'); $options = array('id'=>'id', 'shortname'=>'shortname', 'fullname'=>'fullname');
$settings->add(new admin_setting_configselect('enrol_database/localrolefield', get_string('localrolefield', 'enrol_database'), '', 'shortname', $options)); $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_heading('enrol_database_remoteheader', get_string('settingsheaderremote', 'enrol_database'), ''));

View file

@ -25,7 +25,7 @@
defined('MOODLE_INTERNAL') || die(); defined('MOODLE_INTERNAL') || die();
$plugin->version = 2012022700; // The current plugin version (Date: YYYYMMDDXX) $plugin->version = 2012031000; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2012022300; // Requires this Moodle version $plugin->requires = 2012022300; // Requires this Moodle version
$plugin->component = 'enrol_database'; // Full name of the plugin (used for diagnostics) $plugin->component = 'enrol_database'; // Full name of the plugin (used for diagnostics)
//TODO: should we add cron sync? //TODO: should we add cron sync?