Skip to content

Commit

Permalink
MDL-41778 enrol/ldap add settings to allow updating of course fields …
Browse files Browse the repository at this point in the history
…during sync
  • Loading branch information
nitzo committed Sep 20, 2013
1 parent 7f3836d commit d8a55f5
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
12 changes: 12 additions & 0 deletions enrol/ldap/lang/en/enrol_ldap.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,40 @@
$string['autocreate'] = '<p>Courses can be created automatically if there are enrolments to a course that doesn\'t yet exist in Moodle</p><p>If you are using automatic course creation, it is recommended that you remove the following capabilities: moodle/course:changeidnumber, moodle/course:changeshortname, moodle/course:changefullname and moodle/course:changesummary, from the relevant roles to prevent modifications of the four course fields specified above (ID number, shortname, fullname and summary).</p>';
$string['autocreate_key'] = 'Auto create';
$string['autocreation_settings'] = 'Automatic course creation settings';
$string['autoupdate_settings'] = 'Automatic course update settings';
$string['autoupdate_settings_desc'] = '<p>Select fields to update when synchronization script is running (enrol/ldap/cli/sync.php).</p><p>When at least one field is selected an update will occur.</p>';
$string['bind_dn'] = 'If you want to use a bind user to search users, specify it here. Someting like \'cn=ldapuser,ou=public,o=org\'';
$string['bind_dn_key'] = 'Bind user distinguished name';
$string['bind_pw'] = 'Password for the bind user';
$string['bind_pw_key'] = 'Password';
$string['bind_settings'] = 'Bind settings';
$string['cannotcreatecourse'] = 'Cannot create course: missing required data from the LDAP record!';
$string['cannotupdatecourse'] = "Cannot update course: missing required data from the LDAP record! Course idnumber: '{\$a->idnumber}'";
$string['cannotupdatecourse_duplicateshortname'] = "Cannot update course: Duplicate short name. Skipping course with idnumber '{\$a->idnumber}'...";
$string['courseupdated'] = "Course with idnumber '{\$a->idnumber}' was successfully updated.";
$string['courseupdateskipped'] = "Course with idnumber '{\$a->idnumber}' does not require updating. Skipping...";
$string['category'] = 'The category for auto-created courses';
$string['category_key'] = 'Category';
$string['contexts'] = 'LDAP contexts';
$string['couldnotfinduser'] = "Could not find user '{\$a}', skipping\n";
$string['coursenotexistskip'] = "Course '{\$a}' does not exist and autocreation disabled, skipping\n";
$string['course_fullname'] = 'Optional: LDAP attribute to get the full name from';
$string['course_fullname_key'] = 'Full name';
$string['course_fullname_updateonsync'] = 'Update full name during synchronization script';
$string['course_fullname_updateonsync_key'] = 'Update full name';
$string['course_idnumber'] = 'LDAP attribute to get the course ID number from. Usually \'cn\' or \'uid\'.';
$string['course_idnumber_key'] = 'ID number';
$string['course_search_sub'] = 'Search group memberships from subcontexts';
$string['course_search_sub_key'] = 'Search subcontexts';
$string['course_settings'] = 'Course enrolment settings';
$string['course_shortname'] = 'Optional: LDAP attribute to get the shortname from';
$string['course_shortname_key'] = 'Short name';
$string['course_shortname_updateonsync'] = 'Update short name during synchronization script';
$string['course_shortname_updateonsync_key'] = 'Update short name';
$string['course_summary'] = 'Optional: LDAP attribute to get the summary from';
$string['course_summary_key'] = 'Summary';
$string['course_summary_updateonsync'] = 'Update summary during synchronization script';
$string['course_summary_updateonsync_key'] = 'Update summary';
$string['createcourseextid'] = 'CREATE User enrolled to a nonexistant course \'{$a->courseextid}\'';
$string['createnotcourseextid'] = 'User enrolled to a nonexistant course \'{$a->courseextid}\'';
$string['creatingcourse'] = 'Creating course \'{$a}\'...';
Expand Down
74 changes: 74 additions & 0 deletions enrol/ldap/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ public function sync_enrolments(progress_trace $trace, $onecourse = null) {
$trace->output(get_string('createnotcourseextid', 'enrol_ldap', array('courseextid'=>$idnumber)));
continue; // Next; skip this one!
}
} else { // Check if course needs update & update as needed.
$this->update_course($course_obj, $course, $trace);
}

// Enrol & unenrol
Expand Down Expand Up @@ -1000,6 +1002,78 @@ function create_course($course_ext, progress_trace $trace) {
return $newcourse->id;
}

/**
* Will update a moodle course with new values from LDAP
* A field will be updated only if it is marked to be updated
* on sync in plugin settings
*
* @param object $course
* @param array $externalcourse
* @param progress_trace $trace
* @return bool
*/
protected function update_course($course, $externalcourse, progress_trace $trace) {
global $CFG, $DB;

$coursefields = array ('shortname', 'fullname', 'summary');
static $shouldupdate;

// Initialize $shouldupdate variable. Set to true if one or more fields are marked for update.
if (!isset($shouldupdate)) {
$shouldupdate = false;
foreach ($coursefields as $field) {
$shouldupdate = $shouldupdate || $this->get_config('course_'.$field.'_updateonsync');
}
}

// If we should not update return immediately.
if (!$shouldupdate) {
return false;
}

require_once("$CFG->dirroot/course/lib.php");
$courseupdated = false;
$updatedcourse = new stdClass();
$updatedcourse->id = $course->id;

// Update course fields if necessary.
foreach ($coursefields as $field) {
// If field is marked to be updated on sync && field data was changed update it.
if ($this->get_config('course_'.$field.'_updateonsync')
&& isset($externalcourse[$this->get_config('course_'.$field)][0])
&& $course->{$field} != $externalcourse[$this->get_config('course_'.$field)][0]) {
$updatedcourse->{$field} = $externalcourse[$this->get_config('course_'.$field)][0];
$courseupdated = true;
}
}

if (!$courseupdated) {
$trace->output(get_string('courseupdateskipped', 'enrol_ldap', $course));
return false;
}

// Do not allow empty fullname or shortname.
if ((isset($updatedcourse->fullname) && empty($updatedcourse->fullname))
|| (isset($updatedcourse->shortname) && empty($updatedcourse->shortname))) {
// We are in trouble!
$trace->output(get_string('cannotupdatecourse', 'enrol_ldap', $course));
return false;
}

// Check if the shortname already exists if it does - skip course updating.
if (isset($updatedcourse->shortname)
&& $DB->record_exists('course', array('shortname' => $updatedcourse->shortname))) {
$trace->output(get_string('cannotupdatecourse_duplicateshortname', 'enrol_ldap', $course));
return false;
}

// Finally - update course in DB.
update_course($updatedcourse);
$trace->output(get_string('courseupdated', 'enrol_ldap', $course));

return true;
}

/**
* Automatic enrol sync executed during restore.
* Useful for automatic sync by course->idnumber or course category.
Expand Down
12 changes: 11 additions & 1 deletion enrol/ldap/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,13 @@
//--- course mapping settings ---
$settings->add(new admin_setting_heading('enrol_ldap_course_settings', get_string('course_settings', 'enrol_ldap'), ''));
$settings->add(new admin_setting_configtext_trim_lower('enrol_ldap/objectclass', get_string('objectclass_key', 'enrol_ldap'), get_string('objectclass', 'enrol_ldap'), ''));
$coursefields = array ('idnumber', 'shortname', 'fullname', 'summary');
$settings->add(new admin_setting_configtext_trim_lower('enrol_ldap/course_idnumber', get_string('course_idnumber_key', 'enrol_ldap'), get_string('course_idnumber', 'enrol_ldap'), '', true, true));

$coursefields = array ('shortname', 'fullname', 'summary');
foreach ($coursefields as $field) {
$settings->add(new admin_setting_configtext_trim_lower('enrol_ldap/course_'.$field, get_string('course_'.$field.'_key', 'enrol_ldap'), get_string('course_'.$field, 'enrol_ldap'), '', true, true));
}

$settings->add(new admin_setting_configcheckbox('enrol_ldap/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'),
Expand All @@ -97,6 +100,13 @@
}
$settings->add(new admin_setting_configtext_trim_lower('enrol_ldap/template', get_string('template_key', 'enrol_ldap'), get_string('template', 'enrol_ldap'), ''));

//--- course update settings ---
$settings->add(new admin_setting_heading('enrol_ldap_autoupdate_settings', get_string('autoupdate_settings', 'enrol_ldap'), get_string('autoupdate_settings_desc', 'enrol_ldap')));
$options = $yesno;
foreach ($coursefields as $field) {
$settings->add(new admin_setting_configselect('enrol_ldap/course_'.$field.'_updateonsync', get_string('course_'.$field.'_updateonsync_key', 'enrol_ldap'), get_string('course_'.$field.'_updateonsync', 'enrol_ldap'), 0, $options));
}

//--- nested groups settings ---
$settings->add(new admin_setting_heading('enrol_ldap_nested_groups_settings', get_string('nested_groups_settings', 'enrol_ldap'), ''));
$options = $yesno;
Expand Down

0 comments on commit d8a55f5

Please sign in to comment.