From e8e0d84584dcd3c73770f06b20908f566d77a444 Mon Sep 17 00:00:00 2001 From: Aaron Barnes Date: Thu, 22 Jul 2010 03:11:27 +0000 Subject: [PATCH] MDL-22781 upgrade enrol/flatfile plugin to use enrolments Adding/removing files missed in last patch --- enrol/flatfile/config.html | 55 -------- enrol/flatfile/{enrol.php => lib.php} | 183 +++++++++++++------------- enrol/flatfile/settings.php | 52 ++++++++ enrol/flatfile/version.php | 28 ++++ 4 files changed, 171 insertions(+), 147 deletions(-) delete mode 100644 enrol/flatfile/config.html rename enrol/flatfile/{enrol.php => lib.php} (68%) create mode 100644 enrol/flatfile/settings.php create mode 100644 enrol/flatfile/version.php diff --git a/enrol/flatfile/config.html b/enrol/flatfile/config.html deleted file mode 100644 index e91df2f1aa2bc..0000000000000 --- a/enrol/flatfile/config.html +++ /dev/null @@ -1,55 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -enrol_flatfilemapping as $id => $record) { - list($name, $mapping) = $record; ?> - - - - - - -
enrol_flatfilelocation: - - - -
enrol_mailstudents: - enrol_mailstudents) echo "checked=\"checked\"" ?> /> - - -
enrol_mailteachers: - enrol_mailteachers) echo "checked=\"checked\"" ?> /> - - -
enrol_mailadmins: - enrol_mailadmins) echo "checked=\"checked\"" ?> /> - - -
enrol_flatfilemapping:
diff --git a/enrol/flatfile/enrol.php b/enrol/flatfile/lib.php similarity index 68% rename from enrol/flatfile/enrol.php rename to enrol/flatfile/lib.php index 229162484b0ec..2a283b5bb0358 100644 --- a/enrol/flatfile/enrol.php +++ b/enrol/flatfile/lib.php @@ -1,83 +1,39 @@ enrol_flatfilelocation: where is the file we are looking for? -// $CFG->enrol_emailstudents: send email to students when they are enrolled in a course -// $CFG->enrol_emailteachers: send email to teachers when they are enrolled in a course -// $CFG->enrol_emailadmins: email the log from the cron job to the admin +// 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 . -require_once($CFG->libdir.'/eventslib.php'); - - -class enrolment_plugin_flatfile { - - var $log; - -/// Override the base config_form() function -function config_form($frm) { - global $CFG, $DB; - - $vars = array('enrol_flatfilelocation', 'enrol_mailstudents', 'enrol_mailteachers', 'enrol_mailadmins'); - foreach ($vars as $var) { - if (!isset($frm->$var)) { - $frm->$var = ''; - } - } - - $roles = $DB->get_records('role', null, '', 'id, name, shortname'); - $ffconfig = get_config('enrol_flatfile'); - - $frm->enrol_flatfilemapping = array(); - foreach($roles as $id => $record) { - - $frm->enrol_flatfilemapping[$id] = array( - $record->name, - isset($ffconfig->{"map_{$record->shortname}"}) ? $ffconfig->{"map_{$record->shortname}"} : $record->shortname - ); - } - - include ("$CFG->dirroot/enrol/flatfile/config.html"); -} - - -/// Override the base process_config() function -function process_config($config) { - global $DB; - - if (!isset($config->enrol_flatfilelocation)) { - $config->enrol_flatfilelocation = ''; - } - set_config('enrol_flatfilelocation', $config->enrol_flatfilelocation); - - if (!isset($config->enrol_mailstudents)) { - $config->enrol_mailstudents = ''; - } - set_config('enrol_mailstudents', $config->enrol_mailstudents); - - if (!isset($config->enrol_mailteachers)) { - $config->enrol_mailteachers = ''; - } - set_config('enrol_mailteachers', $config->enrol_mailteachers); - - if (!isset($config->enrol_mailadmins)) { - $config->enrol_mailadmins = ''; - } - set_config('enrol_mailadmins', $config->enrol_mailadmins); +/** + * Flatfile enrolment plugin. + * + * This plugin lets the user specify a "flatfile" (CSV) containing enrolment information. + * On a regular cron cycle, the specified file is parsed and then deleted. + * @package enrol_flatfile + * @copyright 2010 Eugene Venter + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ - foreach($DB->get_records('role', null, '', 'id, shortname') as $id => $role) { - if (isset($config->{"enrol_flatfilemapping_{$id}"})) { - set_config('map_'.$role->shortname, $config->{"enrol_flatfilemapping_{$id}"}, 'enrol_flatfile'); - } else { - set_config('map_'.$role->shortname, $role->shortname, 'enrol_flatfile'); - } - } +/** + * Flatfile enrolment plugin implementation. + * @author Eugene Venter - based on code by Petr Skoda, Martin Dougiamas, Martin Langhoff and others + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ - return true; +require_once($CFG->libdir.'/eventslib.php'); -} -/// Override the get_access_icons() function -function get_access_icons($course) { -} +class enrol_flatfile_plugin extends enrol_plugin { /** * Override the base cron() function to read in a file @@ -92,13 +48,18 @@ function get_access_icons($course) { * starttime = start time (in seconds since epoch) - optional * endtime = end time (in seconds since epoch) - optional */ - function cron() { + public function cron() { global $CFG, $DB; - if (empty($CFG->enrol_flatfilelocation)) { + $filelocation = $this->get_config('location'); + $mailstudents = $this->get_config('mailstudents'); + $mailteachers = $this->get_config('mailteachers'); + $mailadmins = $this->get_config('mailadmins'); + + if (empty($filelocation)) { $filename = "$CFG->dataroot/1/enrolments.txt"; // Default location } else { - $filename = $CFG->enrol_flatfilelocation; + $filename = $filelocation; } if ( file_exists($filename) ) { @@ -186,11 +147,25 @@ function cron() { // Create/resurrect a context object $context = get_context_instance(CONTEXT_COURSE, $course->id); + if ($fields[0] == 'add') { - // TODO: real enrol, and maybe manual - role_assign($roleid, $user->id, $context->id, 'enrol_flatfile'); + $instance = $DB->get_record('enrol', + array('courseid' => $course->id, 'enrol' => 'flatfile')); + if (empty($instance)) { + // Only add an enrol instance to the course if non-existent + $enrolid = $this->add_instance($course); + $instance = $DB->get_record('enrol', array('id' => $enrolid)); + } + + // Enrol the user with this plugin instance + $this->enrol_user($instance, $user->id, $roleid, $fields[4], $fields[5]); } else { - role_unassign($roleid, $user->id, $context->id); + $instances = $DB->get_records('enrol', + array('enrol' => 'flatfile', 'courseid' => $course->id)); + foreach ($instances as $instance) { + // Unenrol the user from all flatfile enrolment instances + $this->unenrol_user($instance, $user->id); + } } @@ -199,7 +174,7 @@ function cron() { if ($fields[1] == "student") { // TODO: replace this with check for $CFG->couremanager, 'moodle/course:update' is definitely wrong - if ($teachers = get_users_by_capability($context, 'moodle/course:update', 'u.*', 'ra.sortorder ASC')) { + if ($teachers = get_users_by_capability($context, 'moodle/course:update', 'u.*')) { foreach ($teachers as $u) { $teacher = $u; } @@ -213,15 +188,18 @@ function cron() { } - if (!empty($CFG->enrol_mailstudents)) { + if (!empty($mailstudents)) { + // Send mail to students $a->coursename = "$course->fullname"; $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id"; $eventdata = new object(); $eventdata->modulename = 'moodle'; + $eventdata->component = 'course'; + $eventdata->name = 'flatfile_enrolment'; $eventdata->userfrom = $teacher; $eventdata->userto = $user; - $eventdata->subject = get_string("enrolmentnew", '', $course->shortname); + $eventdata->subject = get_string("enrolmentnew", 'enrol', $course->shortname); $eventdata->fullmessage = get_string('welcometocoursetext', '', $a); $eventdata->fullmessageformat = FORMAT_PLAIN; $eventdata->fullmessagehtml = ''; @@ -229,18 +207,21 @@ function cron() { message_send($eventdata); } - if (!empty($CFG->enrol_mailteachers) && $teachers) { + if (!empty($mailteachers) && $teachers) { + // Send mail to teachers foreach($teachers as $teacher) { $a->course = "$course->fullname"; $a->user = fullname($user); $eventdata = new object(); $eventdata->modulename = 'moodle'; + $eventdata->component = 'course'; + $eventdata->name = 'flatfile_enrolment'; $eventdata->userfrom = $user; $eventdata->userto = $teacher; - $eventdata->subject = get_string("enrolmentnew", '', $course->shortname); - $eventdata->fullmessage = get_string('enrolmentnewuser', '', $a); + $eventdata->subject = get_string("enrolmentnew", 'enrol', $course->shortname); + $eventdata->fullmessage = get_string('enrolmentnewuser', 'enrol', $a); $eventdata->fullmessageformat = FORMAT_PLAIN; $eventdata->fullmessagehtml = ''; $eventdata->smallmessage = ''; @@ -263,6 +244,8 @@ function cron() { if(! @unlink($filename)) { $eventdata = new object(); $eventdata->modulename = 'moodle'; + $eventdata->component = 'course'; + $eventdata->name = 'flatfile_enrolment'; $eventdata->userfrom = get_admin(); $eventdata->userto = get_admin(); $eventdata->subject = get_string("filelockedmailsubject", "enrol_flatfile"); @@ -274,9 +257,13 @@ function cron() { $this->log .= "Error unlinking file $filename\n"; } - if (!empty($CFG->enrol_mailadmins)) { + if (!empty($mailadmins)) { + + // Send mail to admin $eventdata = new object(); $eventdata->modulename = 'moodle'; + $eventdata->component = 'course'; + $eventdata->name = 'flatfile_enrolment'; $eventdata->userfrom = get_admin(); $eventdata->userto = get_admin(); $eventdata->subject = "Flatfile Enrolment Log"; @@ -301,14 +288,28 @@ function cron() { function get_roles() { global $DB; + // Get all roles + $roles = $DB->get_records('role', null, '', 'id, name, shortname'); + + $config = get_config('enrol_flatfile'); + + // Set some usable mapping configs for later + foreach($roles as $id => $role) { + if (isset($config->{"map_{$id}"})) { + set_config('map_'.$role->shortname, $config->{"map_{$id}"}, 'enrol_flatfile'); + } else { + set_config('map_'.$role->shortname, $role->shortname, 'enrol_flatfile'); + } + } + // Get the updated config + $config = get_config('enrol_flatfile'); // Get a list of all the roles in the database, indexed by their short names. $roles = $DB->get_records('role', null, '', 'shortname, id'); - array_walk($roles, create_function('&$value', '$value = $value->id;')); // Get any name mappings. These will be of the form 'map_shortname' => 'flatfilename'. - $ffconfig = get_config('enrol_flatfile'); + array_walk($roles, create_function('&$value', '$value = $value->id;')); $rolemap = array(); - foreach($ffconfig as $name => $value) { + foreach($config as $name => $value) { if (strpos($name, 'map_') === 0 && isset($roles[$key = substr($name, 4)])) { $rolemap[$value] = $key; } @@ -318,5 +319,3 @@ function get_roles() { } } // end of class - - diff --git a/enrol/flatfile/settings.php b/enrol/flatfile/settings.php new file mode 100644 index 0000000000000..f869b137edf38 --- /dev/null +++ b/enrol/flatfile/settings.php @@ -0,0 +1,52 @@ +. + +/** + * Flatfile enrolments plugin settings and presets. + * + * @package enrol_flatfile + * @copyright 2010 Eugene Venter + * @author Eugene Venter - based on code by Petr Skoda and others + * @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_flatfile_settings', '', get_string('pluginname_desc', 'enrol_flatfile'))); + + $settings->add(new admin_setting_configtext('enrol_flatfile/location', get_string('location', 'enrol_flatfile'), '', '')); + + $settings->add(new admin_setting_configcheckbox('enrol_flatfile/mailstudents', get_string('mailstudents', 'enrol_flatfile'), '', 0)); + + $settings->add(new admin_setting_configcheckbox('enrol_flatfile/mailteachers', get_string('mailteachers', 'enrol_flatfile'), '', 0)); + + $settings->add(new admin_setting_configcheckbox('enrol_flatfile/mailadmins', get_string('mailadmin', 'enrol_flatfile'), '', 0)); + + //--- mapping ------------------------------------------------------------------------------------------- + if (!during_initial_install()) { + $settings->add(new admin_setting_heading('enrol_flatfile_mapping', get_string('mapping', 'enrol_flatfile'), '')); + + $roles = $DB->get_records('role', null, '', 'id, name, shortname'); + + foreach ($roles as $id => $record) { + $settings->add(new admin_setting_configtext('enrol_flatfile/map_'.$id, format_string($record->name), '', format_string($record->shortname))); + } + } +} diff --git a/enrol/flatfile/version.php b/enrol/flatfile/version.php new file mode 100644 index 0000000000000..05822281e8b9d --- /dev/null +++ b/enrol/flatfile/version.php @@ -0,0 +1,28 @@ +. + +/** + * Flat file enrolment plugin version specification. + * + * @package enrol_flatfile + * @copyright 2010 Eugene Venter + * @author Eugene Venter + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +$plugin->version = 2010070800; +$plugin->cron = 60;