]> git.mjollnir.org Git - moodle.git/commitdiff
Flatfile enrolment plugin upgraded to new use roles. Credit: Alastair Pharo, new...
authormjollnir_ <mjollnir_>
Thu, 21 Sep 2006 00:04:43 +0000 (00:04 +0000)
committermjollnir_ <mjollnir_>
Thu, 21 Sep 2006 00:04:43 +0000 (00:04 +0000)
enrol/flatfile/config.html
enrol/flatfile/enrol.php

index cac3ba271c6234f3f97768f30cf7954295df5158..e6313da352f53ea08ec0c757e830492775da90f4 100644 (file)
     </td>
 </tr>
 
+<tr valign="top">
+    <td align="left" colspan="2">enrol_flatfilemapping:</td>
+</tr>
+
+<?php foreach ($frm->enrol_flatfilemapping as $id => $record):
+        list($name, $mapping) = $record; ?>
+<tr valign="top">
+    <td align="right"><?php echo htmlspecialchars($name, ENT_COMPAT, 'UTF-8'); ?></td>
+    <td><input type="text" size="20" name="enrol_flatfilemapping_<?php echo $id; ?>" value="<?php echo htmlspecialchars($mapping, ENT_COMPAT, 'UTF-8'); ?>" /></td>
+</tr>
+<?php endforeach; ?>
+
 </table>
index e4c0f261464cf5b44f0de4428f7153a951359d71..2295c6b4bbede97013ff4b57cc9f5c6631e5adee 100644 (file)
@@ -21,6 +21,19 @@ function config_form($frm) {
             $frm->$var = '';
         } 
     }
+
+    $roles = get_records('role', '', '', '', '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");    
 }
 
@@ -48,6 +61,14 @@ function process_config($config) {
     }
     set_config('enrol_mailadmins', $config->enrol_mailadmins);
 
+    foreach(get_records('role', '', '', '', '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');
+        }
+    }
+
     return true;
 
 }
@@ -84,7 +105,9 @@ function get_access_icons($course) {
             $this->log .= "Flatfile enrol cron found file: $filename\n\n";
 
             if (($fh = fopen($filename, "r")) != false) {
-            
+
+                list($roles, $rolemap) = $this->get_roles();
+                
                 $line = 0;
                 while (!feof($fh)) {
                 
@@ -129,12 +152,11 @@ function get_access_icons($course) {
 
 
                 /// check correct formatting of role field
-                    if ($fields[1] != "student" and $fields[1] != "teacher" and $fields[1] != "teacheredit") {
+                    if (!isset($rolemap[$fields[1]]) && !isset($roles[$fields[1]])) {
                         $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;
@@ -153,6 +175,22 @@ function get_access_icons($course) {
 
 
                     unset($elog);
+
+                    // Either field[1] is a name that appears in the mapping,
+                    // or it's an actual short name. It has to be one or the
+                    // other, or we don't get to this point.
+                    $roleid = isset($rolemap[$fields[1]]) ? $roles[$rolemap[$fields[1]]] : $roles[$fields[1]];
+
+                    // Create/resurrect a context object
+                    $context = get_context_instance(CONTEXT_COURSE, $course->id);
+                    
+                    if ($fields[0] == 'add') {
+                        role_assign($roleid, $user->id, null, $context->id, $fields[4], $fields[5], 0, 'flatfile');
+                    } else {
+                        role_unassign($roleid, $user->id, null, $context->id);
+                    }
+                    
+                    /*
                     switch ($fields[1]) {
                         case "student":
                             if ($fields[0] == "add") {
@@ -192,20 +230,31 @@ function get_access_icons($course) {
 
                         default: // should never get here as checks made above for correct values of $fields[1]
 
-                    } // end of switch
+                    } // end of switch*/
 
 
 
                     if ( empty($elog) and ($fields[0] == "add") ) {
    
                         if ($fields[1] == "student") {
-                            if (! $teacher = get_teacher($course->id)) {
+
+                            if ($teachers = get_users_by_capability($context, 'moodle/course:update', 'u.*,ra.hidden', 'sortorder ASC')) {
+                                foreach ($teachers as $u) {
+                                    if (!$u->hidden || has_capability('moodle/role:viewhiddenassigns', $context)) {
+                                        $teacher = $u;
+                                        break;
+                                    }
+                                }
+                            }
+
+                            if (!isset($teacher)) {
                                 $teacher = get_admin();
                             }
                         } else {
                             $teacher = get_admin();
                         }
 
+                        
                         if (!empty($CFG->enrol_mailstudents)) {
                             $a->coursename = "$course->fullname";
                             $a->profileurl = "$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id";
@@ -213,11 +262,17 @@ function get_access_icons($course) {
                                           get_string('welcometocoursetext', '', $a));
                         }
 
-                        if (!empty($CFG->enrol_mailteachers)) {
-                            $a->course = "$course->fullname";
-                            $a->user = fullname($user);
-                            email_to_user($teacher, $user, get_string("enrolmentnew", '', $course->shortname), 
-                                          get_string('enrolmentnewuser', '', $a));
+                        if (!empty($CFG->enrol_mailteachers) && $teachers) {
+
+                            foreach($teachers as $teacher) {
+                            
+                                if (!$u->hidden || has_capability('moodle/role:viewhiddenassigns', $context)) {
+                                    $a->course = "$course->fullname";
+                                    $a->user = fullname($user);
+                                    email_to_user($teacher, $user, get_string("enrolmentnew", '', $course->shortname), 
+                                                  get_string('enrolmentnewuser', '', $a));
+                                }
+                            }
                         }
                     }
 
@@ -245,6 +300,30 @@ function get_access_icons($course) {
 
     } // end of function
 
+    /**
+     * Returns a pair of arrays.  The first is the set of roleids, indexed by
+     * their shortnames.  The second is the set of shortnames that have
+     * mappings, indexed by those mappings.
+     *
+     * @return array ($roles, $rolemap)
+     */
+    function get_roles() {
+        // Get a list of all the roles in the database, indexed by their short names.
+        $roles = get_records('role', '', '', '', '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');
+        $rolemap = array();
+        foreach($ffconfig as $name => $value) {
+            if (strpos($name, 'map_') === 0 && isset($roles[$key = substr($name, 4)])) {
+                $rolemap[$value] = $key;
+            }
+        }
+
+        return array($roles, $rolemap);
+    }
+
 } // end of class
 
 ?>