]> git.mjollnir.org Git - moodle.git/commitdiff
Enrolment architecture version 0.5 !
authormoodler <moodler>
Fri, 25 Jun 2004 03:28:12 +0000 (03:28 +0000)
committermoodler <moodler>
Fri, 25 Jun 2004 03:28:12 +0000 (03:28 +0000)
The internal method (the default) simply replicates Moodle's
existing behaviour and is working fine.

The new flatfile module also works.

There is no admin config interface yet, I'm still implementing
this using templates.

enrol/README.txt [new file with mode: 0644]
enrol/enrol.class.php [new file with mode: 0644]
enrol/flatfile/enrol.php [new file with mode: 0644]
enrol/flatfile/example.txt [new file with mode: 0644]
enrol/internal/enrol.html [new file with mode: 0644]
enrol/internal/enrol.php [new file with mode: 0644]

diff --git a/enrol/README.txt b/enrol/README.txt
new file mode 100644 (file)
index 0000000..552d8b6
--- /dev/null
@@ -0,0 +1,15 @@
+ENROLMENT MODULES
+-----------------
+
+(Yes, that's the correct English spelling  ;-) )
+
+enrol.class.php contains the base class and explains 
+all the possible functions that an enrolment module 
+can have.
+
+Each plugin is in a subfolder here and extends the
+base class as necessary.
+
+
+Martin Dougiamas and Shane Elliott, Moodle.com
+
diff --git a/enrol/enrol.class.php b/enrol/enrol.class.php
new file mode 100644 (file)
index 0000000..d41fd57
--- /dev/null
@@ -0,0 +1,286 @@
+<?php   /// $Id$
+///////////////////////////////////////////////////////////////////////////
+//                                                                       //
+// NOTICE OF COPYRIGHT                                                   //
+//                                                                       //
+// Moodle - Modular Object-Oriented Dynamic Learning Environment         //
+//          http://moodle.org                                            //
+//                                                                       //
+// Copyright (C) 2004  Martin Dougiamas  http://moodle.com               //
+//                                                                       //
+// This program 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 2 of the License, or     //
+// (at your option) any later version.                                   //
+//                                                                       //
+// This program 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:                          //
+//                                                                       //
+//          http://www.gnu.org/copyleft/gpl.html                         //
+//                                                                       //
+///////////////////////////////////////////////////////////////////////////
+
+
+/**
+* enrolment_base is the base class for enrolment plugins
+*
+* This class provides all the functionality for an enrolment plugin
+* In fact it includes all the code for the default, "internal" method
+* so that other plugins can override these as necessary.
+*/
+
+class enrolment_base {
+
+var $errormsg;
+
+
+
+/**
+* Returns information about the courses a student has access to
+*
+* Set the $user->student course array
+* Set the $user->timeaccess course array
+*
+* @param    user  referenced object, must contain $user->id already set
+*/
+function get_student_courses(&$user) {
+
+    if ($students = get_records("user_students", "userid", $user->id)) {
+        $currenttime = time();
+        foreach ($students as $student) {
+
+        /// Is course visible?
+
+            if (get_field("course", "visible", "id", $student->course)) {
+
+            /// Is student enrolled for a specific time period?
+
+                if ( ( $student->timestart == 0 or ( $currenttime > $student->timestart )) and 
+                     ( $student->timeend   == 0 or ( $currenttime < $student->timeend )) ) {
+                    $user->student[$student->course] = true;
+                    $user->timeaccess[$student->course] = $student->timeaccess;
+                }
+            }
+        }
+    }   
+}
+
+
+
+/**
+* Returns information about the courses a student has access to
+*
+* Set the $user->teacher course array
+* Set the $user->teacheredit course array
+* Set the $user->timeaccess course array
+*
+* @param    user  referenced object, must contain $user->id already set
+*/
+function get_teacher_courses(&$user) {
+
+    if ($teachers = get_records("user_teachers", "userid", $user->id)) {
+        $currenttime = time();
+        foreach ($teachers as $teacher) {
+
+        /// Is teacher only teaching this course for a specific time period?
+
+            if ( ( $teacher->timestart == 0 or ( $currenttime > $teacher->timestart )) and 
+                 ( $teacher->timeend   == 0 or ( $currenttime < $teacher->timeend )) ) {
+
+                $user->teacher[$teacher->course] = true;
+
+                if ($teacher->editall) {
+                    $user->teacheredit[$teacher->course] = true;
+                }   
+
+                $user->timeaccess[$teacher->course] = $teacher->timeaccess;
+            }
+        }   
+    }
+}
+
+
+
+
+/**
+* Prints the entry form/page for this enrolment
+*
+* This is only called from course/enrol.php
+* Most plugins will probably override this to print payment 
+* forms etc, or even just a notice to say that manual enrolment 
+* is disabled
+*
+* @param    course  current course object
+*/
+function print_entry($course) {
+    global $CFG, $USER, $SESSION;
+
+    $strloginto = get_string("loginto", "", $course->shortname);
+    $strcourses = get_string("courses");
+
+
+/// Double check just in case they are actually enrolled already 
+/// This might occur if they were manually enrolled during this session
+
+    if (record_exists("user_students", "userid", $USER->id, "course", $course->id)) {
+        $USER->student[$course->id] = true;
+
+        if ($SESSION->wantsurl) {
+            $destination = $SESSION->wantsurl;
+            unset($SESSION->wantsurl);
+        } else {
+            $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
+        }
+
+        redirect($destination);
+    }
+
+
+/// Automatically enrol into courses without password
+
+    if ($course->password == "") {   // no password, so enrol
+
+        if (isguest()) {
+            add_to_log($course->id, "course", "guest", "view.php?id=$course->id", "$USER->id");
+
+        } else if (empty($_GET['confirm'])) {
+
+            print_header($strloginto, $course->fullname, "<a href=\".\">$strcourses</a> -> $strloginto");
+            echo "<br />";
+            notice_yesno(get_string("enrolmentconfirmation"), "enrol.php?id=$course->id&confirm=1", $CFG->wwwroot);
+            print_footer();
+            exit;
+
+        } else {
+
+            if (! enrol_student($USER->id, $course->id)) {
+                error("An error occurred while trying to enrol you.");
+            }
+            add_to_log($course->id, "course", "enrol", "view.php?id=$course->id", "$USER->id");
+
+            $USER->student[$course->id] = true;
+
+            if ($SESSION->wantsurl) {
+                $destination = $SESSION->wantsurl;
+                unset($SESSION->wantsurl);
+            } else {
+                $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
+            }
+
+            redirect($destination);
+        }
+    }
+
+    $teacher = get_teacher($course->id);
+    if (!isset($password)) {
+        $password = "";
+    }
+
+
+    print_header($strloginto, $course->fullname, "<A HREF=\".\">$strcourses</A> -> $strloginto", "form.password");
+
+    print_course($course);
+
+    include("$CFG->dirroot/enrol/internal/enrol.html");
+
+    print_footer();
+
+}
+
+
+
+/**
+* The other half to print_entry, this checks the form data
+*
+* This function checks that the user has completed the task on the 
+* enrolment entry page and then enrolls them.
+*
+* @param    form    the form data submitted, as an object
+* @param    course  the current course, as an object
+*/
+function check_entry($form, $course) {
+    global $CFG, $USER, $SESSION;
+
+    if ($form->password == $course->password) {
+
+        if (isguest()) {
+        
+            add_to_log($course->id, "course", "guest", "view.php?id=$course->id", $_SERVER['REMOTE_ADDR']);
+            
+        } else if (!record_exists("user_students", "userid", $USER->id, "course", $course->id)) {
+
+            if (! enrol_student($USER->id, $course->id)) {
+                error("An error occurred while trying to enrol you.");
+            }
+            
+            $subject = get_string("welcometocourse", "", $course->fullname);
+            $a->coursename = $course->fullname;
+            $a->profileurl = "$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id";
+            $message = get_string("welcometocoursetext", "", $a);
+            
+            if (! $teacher = get_teacher($course->id)) {
+                $teacher = get_admin();
+            }
+            
+            email_to_user($USER, $teacher, $subject, $message);
+            add_to_log($course->id, "course", "enrol", "view.php?id=$course->id", "$USER->id");
+        }
+        
+        $USER->student[$course->id] = true;
+        
+        if ($SESSION->wantsurl) {
+            $destination = $SESSION->wantsurl;
+            unset($SESSION->wantsurl);
+        } else {
+            $destination = "$CFG->wwwroot/course/view.php?id=$course->id";
+        }
+        
+        redirect($destination);
+
+    } else {
+        $this->errormsg = get_string("enrolmentkeyhint", "", substr($course->password,0,1));
+    }
+                        
+}
+
+
+/**
+* Prints a form for configuring the current enrolment plugin
+*
+* This function is called from admin/enrol.php, and outputs a 
+* full page with a form for defining the current enrolment plugin.
+*
+* @param    page  an object containing all the data for this page
+*/
+function print_config($page) {
+}
+
+
+/**
+* Processes and stored configuration data for the enrolment plugin
+*
+* Processes and stored configuration data for the enrolment plugin
+*
+* @param    config  all the configuration data as entered by the admin
+*/
+function process_config($config) {
+}
+
+
+/**
+* This function is run by admin/cron.php every time 
+*
+* The cron function can perform regular checks for the current 
+* enrollment plugin.  For example it can check a foreign database,
+* all look for a file to pull data in from
+*
+*/
+function cron() {
+}
+
+
+} /// end of class
+
+?>
diff --git a/enrol/flatfile/enrol.php b/enrol/flatfile/enrol.php
new file mode 100644 (file)
index 0000000..d43440d
--- /dev/null
@@ -0,0 +1,208 @@
+<?php
+require_once("$CFG->dirroot/enrol/enrol.class.php");
+
+// The following flags are set in the configuration
+// $CFG->enrol_flatfilelocation:       where is the file we are looking for?
+// $CFG->enrol_flatfilemailusers:      send email to users when they are enrolled in a course
+// $CFG->enrol_flatfilemailadmin:      email the log from the cron job to the admin
+// $CFG->enrol_flatfileallowinternal:  allow internal enrolment in courses
+
+
+
+
+class enrolment_plugin extends enrolment_base {
+
+    var $log;    
+
+/// Override the base print_entry() function
+function print_entry($course) {
+    global $CFG;
+
+    if (! empty($CFG->enrol_flatfileallowinternal) ) {
+        parent::print_entry($course);
+    } else {
+        print_header();
+        notice(get_string("enrolmentnointernal"), $CFG->wwwroot);
+    }
+}
+
+
+/// Override the base check_entry() function
+function check_entry($form, $course) {
+    global $CFG;
+
+    if (! empty($CFG->enrol_flatfileallowinternal) ) {
+        parent::check_entry($form, $course);
+    }
+}
+
+
+
+/**
+* Override the base cron() function to read in a file
+*
+* Comma separated file assumed to have four fields per line:
+*   operation, role, idnumber(user), idnumber(course)
+* where:
+*   operation        = add | del
+*   role             = student | teacher | teacheredit
+*   idnumber(user)   = idnumber in the user table NB not id
+*   idnumber(course) = idnumber in the course table NB not id
+*/
+    function cron() {
+        global $CFG;
+
+        if (empty($CFG->enrol_flatfilelocation)) {
+            $filename = "$CFG->dataroot/1/enrolments.txt";  // Default location
+        } else {
+            $filename = $CFG->enrol_flatfilelocation;
+        }
+
+        if ( file_exists($filename) ) {
+            
+            $this->log  = userdate(time()) . "\n";
+            $this->log .= "Flatfile enrol cron found file: $filename\n\n";
+
+            if (($fh = fopen($filename, "r")) != false) {
+            
+                $line = 0;
+                while (!feof($fh)) {
+                
+                    $line++;
+                    $fields = explode( ",", str_replace( "\r", "", fgets($fh) ) );
+
+
+                /// If a line is incorrectly formatted ie does not have 4 comma separated fields then ignore it
+                    if ( count($fields) != 4) {
+                        if ( count($fields) > 1 or strlen($fields[0]) > 1) { // no error for blank lines
+                            $this->log .= "$line: Line incorrectly formatted - ignoring\n";
+                        }
+                        continue;
+                    }
+                    
+
+                    $fields[0] = trim(strtolower($fields[0]));
+                    $fields[1] = trim(strtolower($fields[1]));
+                    $fields[2] = trim($fields[2]);
+                    $fields[3] = trim($fields[3]);
+
+
+                    $this->log .= "$line: $fields[0] $fields[1] $fields[2] $fields[3]: ";
+
+
+
+                /// check correct formatting of operation field
+                    if ($fields[0] != "add" and $fields[0] != "del") {
+                        $this->log .= "Unknown operation in field 1 - ignoring line\n";
+                        continue;
+                    }
+
+
+                /// check correct formatting of role field
+                    if ($fields[1] != "student" and $fields[1] != "teacher" and $fields[1] != "teacheredit") {
+                        $this->log .= "Unknown role in field2 - ignoring line\n";
+                        continue;
+                    }
+
+
+                    if (! $user = get_record("user", "idnumber", $fields[2]) ) {
+                        $this->log .= "Unknown user idnumber in field 3 - ignoring line\n";
+                        continue;
+                    }
+
+
+                    if (! $course = get_record("course", "idnumber", $fields[3]) ) {
+                        $this->log .= "Unknown course idnumber in field 4 - ignoring line\n";
+                        continue;
+                    }
+
+
+                    unset($elog);
+                    switch ($fields[1]) {
+                        case "student":
+                            if ($fields[0] == "add") {
+                                if (! enrol_student($user->id, $course->id)) {
+                                    $elog = "Error enrolling in course\n";
+                                }
+                            } else {
+                                if (! unenrol_student($user->id, $course->id)) {
+                                    $elog = "Error unenrolling from course\n";
+                                }
+                            }
+                            break;
+
+                        case "teacher":
+                            if ($fields[0] == "add") {
+                                if (! add_teacher($user->id, $course->id, 0)) {
+                                    $elog = "Error adding teacher to course\n";
+                                }
+                            } else {
+                                if (! remove_teacher($user->id, $course->id)) {
+                                    $elog = "Error removing teacher from course\n";
+                                }
+                            }
+                            break;
+
+                        case "teacheredit":
+                            if ($fields[0] == "add") {
+                                if (! add_teacher($user->id, $course->id, 1)) {
+                                    $elog = "Error adding teacher to course\n";
+                                }
+                            } else {
+                                if (! remove_teacher($user->id, $course->id)) {
+                                    $elog = "Error removing teacher from course\n";
+                                }
+                            }
+                            break;
+
+                        default: // should never get here as checks made above for correct values of $fields[1]
+
+                    } // end of switch
+
+
+
+                    if ( (! empty($CFG->enrol_flatfilemailusers)) and empty($elog) and ($fields[0] == "add") ) {
+                        $subject = get_string("welcometocourse", "", $course->fullname);
+                        $a->coursename = $course->fullname;
+                        $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&course=$course->id";
+                        $message = get_string("welcometocoursetext", "", $a);
+
+                        if ($fields[1] == "student") {
+                            if (! $teacher = get_teacher($course->id)) {
+                                $teacher = get_admin();
+                            }
+                        } else {
+                            $teacher = get_admin();
+                        }
+
+                        email_to_user($user, $teacher, $subject, $message);
+                    }
+
+
+
+                    if (empty($elog)) {
+                        $elog = "OK\n";
+                    }
+                    $this->log .= $elog;
+
+                } // end of while loop
+
+            fclose($fh);
+            } // end of if(file_open)
+
+            if(! @unlink($filename)) {
+                email_to_user(get_admin(), get_admin(), get_string("filelockedmailsubject", "enrol_flatfile"), get_string("filelockedmail", "enrol_flatfile", $filename));
+                $this->log .= "Error unlinking file $filename\n";
+            }
+
+            if (! empty($CFG->enrol_flatfilemailadmin)) {
+                email_to_user(get_admin(), get_admin(), "Flatfile Enrolment Log", $this->log);
+            }
+
+        } // end of if(file_exists)
+
+    } // end of function
+
+} // end of class
+
+?>
diff --git a/enrol/flatfile/example.txt b/enrol/flatfile/example.txt
new file mode 100644 (file)
index 0000000..256a676
--- /dev/null
@@ -0,0 +1,5 @@
+add, student, 5, CF101
+add, teacher, 6, CF101
+add, teacheredit, 7, CF101
+del, student, 8, CF101
+del, student, 17, CF101
diff --git a/enrol/internal/enrol.html b/enrol/internal/enrol.html
new file mode 100644 (file)
index 0000000..3c89b1c
--- /dev/null
@@ -0,0 +1,45 @@
+<table cellpadding="20" align="center">\r
+  <tr valign="top">\r
+    <td>\r
+    <p align="center">\r
+       <?php\r
+          if ($teacher) {\r
+              $teachername = "<a href=\"../user/view.php?id=$teacher->id&course=$site->id\">".\r
+                             "$teacher->firstname $teacher->lastname</a>.";\r
+          } else {\r
+              $teachername = get_string("yourteacher", "", strtolower($course->teacher));\r
+          }\r
+          print_string("enrolmentkeyfrom", "", $teachername);\r
+       ?>\r
+       </p>\r
+    </td>\r
+\r
+  </tr>\r
+  <tr valign=top>\r
+    <th class="generalbox"><center><?php if (! empty($this->errormsg)) {formerr($this->errormsg);} ?></center>\r
+      <form name="form" method="post" action="enrol.php">\r
+        <table>\r
+          <tr>\r
+            <td width="50%" align="right"><p><?php print_string("enrolmentkey") ?>:</p></td>\r
+            <td width="50%">\r
+              <input type="password" name="password" size="20" value="<?php p($password) ?>" >\r
+              <input type="hidden" name="id" value="<?php p($course->id) ?>" >\r
+            </td>\r
+          <tr>\r
+            <td width="50%">&nbsp;</td>\r
+            <td width="50%">\r
+              <table cellpadding="1" cellspacing="0" align="center">\r
+                <tr>\r
+\r
+                    <td><input type="submit" value="<?php print_string("login") ?>">&nbsp;&nbsp;</form></td>\r
+                    <td><form action="<?php p($CFG->wwwroot)?>/index.php" method=post>\r
+                        <input type="submit" value="<?php print_string("cancel") ?>"></form></td>\r
+                </tr>\r
+              </table>\r
+            </td>\r
+        </table>\r
+    </td>\r
+\r
+  </tr>\r
+</table>\r
+\r
diff --git a/enrol/internal/enrol.php b/enrol/internal/enrol.php
new file mode 100644 (file)
index 0000000..038393c
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+require_once("$CFG->dirroot/enrol/enrol.class.php");
+
+class enrolment_plugin extends enrolment_base {
+    /// nothing to do - it's already perfect!
+}
+
+?>