~ubuntu-branches/ubuntu/hoary/moodle/hoary

« back to all changes in this revision

Viewing changes to enrol/database/enrol.php

  • Committer: Bazaar Package Importer
  • Author(s): Isaac Clerencia
  • Date: 2004-12-29 00:49:52 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041229004952-gliyqzpj2w3e7clx
Tags: 1.4.3-1
* Urgency high as upstream release fixes several security bugs
* New upstream release
* Write database creation errors and warn the user about it, 
closes: #285842, #285842

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php  // $Id: enrol.php,v 1.3.2.3 2004/09/25 15:31:37 moodler Exp $
 
2
 
 
3
require_once("$CFG->dirroot/enrol/enrol.class.php");
 
4
 
 
5
class enrolment_plugin extends enrolment_base {
 
6
 
 
7
    var $log;    
 
8
 
 
9
/// Leave get_teacher_courses() function unchanged for the time being
 
10
 
 
11
 
 
12
/// Leave cron() function unchanged
 
13
 
 
14
 
 
15
 
 
16
/// Overide the base get_student_courses() function
 
17
function get_student_courses(&$user) {
 
18
    global $CFG;
 
19
 
 
20
    parent::get_student_courses($user);  /// Start with the list of known enrolments
 
21
                                         /// If the database fails we can at least use this
 
22
 
 
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
 
27
 
 
28
    if (strpos($CFG->prefix, $CFG->dbname) === false) {
 
29
        $oldprefix = $CFG->prefix;
 
30
        $CFG->prefix = "$CFG->dbname.$CFG->prefix";
 
31
    }
 
32
 
 
33
    // Try to connect to the external database
 
34
 
 
35
    $enroldb = &ADONewConnection($CFG->enrol_dbtype);
 
36
 
 
37
    if ($enroldb->PConnect($CFG->enrol_dbhost,$CFG->enrol_dbuser,$CFG->enrol_dbpass,$CFG->enrol_dbname)) {
 
38
 
 
39
        $courselist = array();      /// Initialise new array
 
40
        $newstudent = array();
 
41
 
 
42
        /// Get the authoritative list of enrolments from the database
 
43
 
 
44
        $useridnumber = $user->{$CFG->enrol_localuserfield};   
 
45
 
 
46
 
 
47
        if ($rs = $enroldb->Execute("SELECT $CFG->enrol_remotecoursefield 
 
48
                                       FROM $CFG->enrol_dbtable 
 
49
                                      WHERE $CFG->enrol_remoteuserfield = '$useridnumber' ")) {
 
50
 
 
51
            if ($rs->RecordCount() > 0) {
 
52
                while (!$rs->EOF) {
 
53
                    $courselist[] = $rs->fields[0];
 
54
                    $rs->MoveNext();
 
55
                }
 
56
 
 
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
 
64
                        }
 
65
                    }
 
66
                }
 
67
            }
 
68
 
 
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
 
73
                }
 
74
            }
 
75
 
 
76
            $user->student = $newstudent;    /// Overwrite the array with the new one
 
77
        }
 
78
        
 
79
        $enroldb->Close();
 
80
    }
 
81
 
 
82
    if (!empty($oldprefix)) {
 
83
        $CFG->prefix = $oldprefix;           // Restore it just in case
 
84
    }
 
85
}
 
86
 
 
87
 
 
88
/// Override the base print_entry() function
 
89
function print_entry($course) {
 
90
    global $CFG;
 
91
 
 
92
    if (! empty($CFG->enrol_allowinternal) ) {
 
93
        parent::print_entry($course);
 
94
    } else {
 
95
        print_header();
 
96
        notice(get_string("enrolmentnointernal"), $CFG->wwwroot);
 
97
    }
 
98
}
 
99
 
 
100
 
 
101
/// Override the base check_entry() function
 
102
function check_entry($form, $course) {
 
103
    global $CFG;
 
104
 
 
105
    if (! empty($CFG->enrol_allowinternal) ) {
 
106
        parent::check_entry($form, $course);
 
107
    }
 
108
}
 
109
 
 
110
 
 
111
/// Overide the get_access_icons() function
 
112
function get_access_icons($course) {
 
113
}
 
114
 
 
115
 
 
116
/// Overide the base config_form() function
 
117
function config_form($frm) {
 
118
    global $CFG;
 
119
    include("$CFG->dirroot/enrol/database/config.html");
 
120
}
 
121
 
 
122
/// Override the base process_config() function
 
123
function process_config($config) {
 
124
 
 
125
    if (!isset($config->enrol_dbtype)) {
 
126
        $config->enrol_dbtype = 'mysql';
 
127
    }
 
128
    set_config('enrol_dbtype', $config->enrol_dbtype);
 
129
 
 
130
    if (!isset($config->enrol_dbhost)) {
 
131
        $config->enrol_dbhost = '';
 
132
    }
 
133
    set_config('enrol_dbhost', $config->enrol_dbhost);
 
134
 
 
135
    if (!isset($config->enrol_dbuser)) {
 
136
        $config->enrol_dbuser = '';
 
137
    }
 
138
    set_config('enrol_dbuser', $config->enrol_dbuser);
 
139
 
 
140
    if (!isset($config->enrol_dbpass)) {
 
141
        $config->enrol_dbpass = '';
 
142
    }
 
143
    set_config('enrol_dbpass', $config->enrol_dbpass);
 
144
 
 
145
    if (!isset($config->enrol_dbname)) {
 
146
        $config->enrol_dbname = '';
 
147
    }
 
148
    set_config('enrol_dbname', $config->enrol_dbname);
 
149
 
 
150
    if (!isset($config->enrol_dbtable)) {
 
151
        $config->enrol_dbtable = '';
 
152
    }
 
153
    set_config('enrol_dbtable', $config->enrol_dbtable);
 
154
 
 
155
    if (!isset($config->enrol_localcoursefield)) {
 
156
        $config->enrol_localcoursefield = '';
 
157
    }
 
158
    set_config('enrol_localcoursefield', $config->enrol_localcoursefield);
 
159
 
 
160
    if (!isset($config->enrol_localuserfield)) {
 
161
        $config->enrol_localuserfield = '';
 
162
    }
 
163
    set_config('enrol_localuserfield', $config->enrol_localuserfield);
 
164
 
 
165
    if (!isset($config->enrol_remotecoursefield)) {
 
166
        $config->enrol_remotecoursefield = '';
 
167
    }
 
168
    set_config('enrol_remotecoursefield', $config->enrol_remotecoursefield);
 
169
 
 
170
    if (!isset($config->enrol_remoteuserfield)) {
 
171
        $config->enrol_remoteuserfield = '';
 
172
    }
 
173
    set_config('enrol_remoteuserfield', $config->enrol_remoteuserfield);
 
174
 
 
175
    if (!isset($config->enrol_allowinternal)) {
 
176
        $config->enrol_allowinternal = '';
 
177
    }
 
178
    set_config('enrol_allowinternal', $config->enrol_allowinternal);
 
179
    
 
180
    return true;
 
181
 
 
182
}
 
183
 
 
184
 
 
185
} // end of class
 
186
 
 
187
?>