Incorrect variable name used for parameter.
[moodle-linuxchix.git] / enrol / database / enrol.php
blob65f42edfe8f3a84ea38314af00512aa073f2ba6d
1 <?php // $Id$
3 require_once("$CFG->dirroot/enrol/enrol.class.php");
5 class enrolment_plugin extends enrolment_base {
7 var $log;
9 /// Leave get_teacher_courses() function unchanged for the time being
12 /// Leave cron() function unchanged
16 /// Overide the base get_student_courses() function
17 function get_student_courses(&$user) {
18 global $CFG;
20 parent::get_student_courses($user); /// Start with the list of known enrolments
21 /// If the database fails we can at least use this
23 // This is a hack to workaround what seems to be a bug in ADOdb with accessing
24 // two databases of the same kind ... it seems to get confused when trying to access
25 // the first database again, after having accessed the second.
26 // The following hack will make the database explicit which keeps it happy
28 if (strpos($CFG->prefix, $CFG->dbname) === false) {
29 $oldprefix = $CFG->prefix;
30 $CFG->prefix = "$CFG->dbname.$CFG->prefix";
33 // Try to connect to the external database
35 $enroldb = &ADONewConnection($CFG->enrol_dbtype);
37 if ($enroldb->PConnect($CFG->enrol_dbhost,$CFG->enrol_dbuser,$CFG->enrol_dbpass,$CFG->enrol_dbname)) {
39 $courselist = array(); /// Initialise new array
40 $newstudent = array();
42 /// Get the authoritative list of enrolments from the database
44 $useridnumber = $user->{$CFG->enrol_localuserfield};
47 if ($rs = $enroldb->Execute("SELECT $CFG->enrol_remotecoursefield
48 FROM $CFG->enrol_dbtable
49 WHERE $CFG->enrol_remoteuserfield = '$useridnumber' ")) {
51 if ($rs->RecordCount() > 0) {
52 while (!$rs->EOF) {
53 $courselist[] = $rs->fields[0];
54 $rs->MoveNext();
57 foreach ($courselist as $coursefield) {
58 if ($course = get_record('course', $CFG->enrol_localcoursefield, $coursefield)) {
59 $newstudent[$course->id] = true; /// Add it to new list
60 if (isset($user->student[$course->id])) { /// We have it already
61 unset($user->student[$course->id]); /// Remove from old list
62 } else {
63 enrol_student($user->id, $course->id); /// Enrol the student
69 if (!empty($user->student)) { /// We have some courses left that we need to unenrol from
70 foreach ($user->student as $courseid => $value) {
71 unenrol_student($user->id, $courseid); /// Unenrol the student
72 unset($user->student[$course->id]); /// Remove from old list
76 $user->student = $newstudent; /// Overwrite the array with the new one
79 $enroldb->Close();
82 if (!empty($oldprefix)) {
83 $CFG->prefix = $oldprefix; // Restore it just in case
88 /// Override the base print_entry() function
89 function print_entry($course) {
90 global $CFG;
92 if (! empty($CFG->enrol_allowinternal) ) {
93 parent::print_entry($course);
94 } else {
95 print_header();
96 notice(get_string("enrolmentnointernal"), $CFG->wwwroot);
101 /// Override the base check_entry() function
102 function check_entry($form, $course) {
103 global $CFG;
105 if (! empty($CFG->enrol_allowinternal) ) {
106 parent::check_entry($form, $course);
111 /// Overide the get_access_icons() function
112 function get_access_icons($course) {
116 /// Overide the base config_form() function
117 function config_form($frm) {
118 global $CFG;
120 $vars = array('enrol_dbhost', 'enrol_dbuser', 'enrol_dbpass',
121 'enrol_dbname', 'enrol_dbtable',
122 'enrol_localcoursefield', 'enrol_localuserfield',
123 'enrol_remotecoursefield', 'enrol_remoteuserfield',
124 'enrol_allowinternal');
125 foreach ($vars as $var) {
126 if (!isset($frm->$var)) {
127 $frm->$var = '';
130 include("$CFG->dirroot/enrol/database/config.html");
133 /// Override the base process_config() function
134 function process_config($config) {
136 if (!isset($config->enrol_dbtype)) {
137 $config->enrol_dbtype = 'mysql';
139 set_config('enrol_dbtype', $config->enrol_dbtype);
141 if (!isset($config->enrol_dbhost)) {
142 $config->enrol_dbhost = '';
144 set_config('enrol_dbhost', $config->enrol_dbhost);
146 if (!isset($config->enrol_dbuser)) {
147 $config->enrol_dbuser = '';
149 set_config('enrol_dbuser', $config->enrol_dbuser);
151 if (!isset($config->enrol_dbpass)) {
152 $config->enrol_dbpass = '';
154 set_config('enrol_dbpass', $config->enrol_dbpass);
156 if (!isset($config->enrol_dbname)) {
157 $config->enrol_dbname = '';
159 set_config('enrol_dbname', $config->enrol_dbname);
161 if (!isset($config->enrol_dbtable)) {
162 $config->enrol_dbtable = '';
164 set_config('enrol_dbtable', $config->enrol_dbtable);
166 if (!isset($config->enrol_localcoursefield)) {
167 $config->enrol_localcoursefield = '';
169 set_config('enrol_localcoursefield', $config->enrol_localcoursefield);
171 if (!isset($config->enrol_localuserfield)) {
172 $config->enrol_localuserfield = '';
174 set_config('enrol_localuserfield', $config->enrol_localuserfield);
176 if (!isset($config->enrol_remotecoursefield)) {
177 $config->enrol_remotecoursefield = '';
179 set_config('enrol_remotecoursefield', $config->enrol_remotecoursefield);
181 if (!isset($config->enrol_remoteuserfield)) {
182 $config->enrol_remoteuserfield = '';
184 set_config('enrol_remoteuserfield', $config->enrol_remoteuserfield);
186 if (!isset($config->enrol_allowinternal)) {
187 $config->enrol_allowinternal = '';
189 set_config('enrol_allowinternal', $config->enrol_allowinternal);
191 return true;
196 } // end of class