Skip to content

Commit

Permalink
MDL-22781 upgrade enrol/flatfile plugin to use enrolments
Browse files Browse the repository at this point in the history
Adding/removing files missed in last patch
  • Loading branch information
Aaron Barnes committed Jul 22, 2010
1 parent 979833d commit e8e0d84
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 147 deletions.
55 changes: 0 additions & 55 deletions enrol/flatfile/config.html

This file was deleted.

183 changes: 91 additions & 92 deletions enrol/flatfile/enrol.php → enrol/flatfile/lib.php
Original file line number Diff line number Diff line change
@@ -1,83 +1,39 @@
<?php
// The following flags are set in the configuration
// $CFG->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 <http://www.gnu.org/licenses/>.

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
Expand All @@ -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) ) {
Expand Down Expand Up @@ -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);
}
}


Expand All @@ -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;
}
Expand All @@ -213,34 +188,40 @@ 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&amp;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 = '';
$eventdata->smallmessage = '';
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 = '';
Expand All @@ -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");
Expand All @@ -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";
Expand All @@ -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;
}
Expand All @@ -318,5 +319,3 @@ function get_roles() {
}

} // end of class


52 changes: 52 additions & 0 deletions enrol/flatfile/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?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/>.

/**
* 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)));
}
}
}
Loading

0 comments on commit e8e0d84

Please sign in to comment.