|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 // This file is part of Moodle - http://moodle.org/ 00003 // 00004 // Moodle is free software: you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation, either version 3 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // Moodle is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00016 00028 defined('MOODLE_INTERNAL') || die(); 00029 00035 class enrol_database_plugin extends enrol_plugin { 00042 public function instance_deleteable($instance) { 00043 if (!enrol_is_enabled('database')) { 00044 return true; 00045 } 00046 if (!$this->get_config('dbtype') or !$this->get_config('dbhost') or !$this->get_config('remoteenroltable') or !$this->get_config('remotecoursefield') or !$this->get_config('remoteuserfield')) { 00047 return true; 00048 } 00049 00050 //TODO: connect to external system and make sure no users are to be enrolled in this course 00051 return false; 00052 } 00053 00061 public function sync_user_enrolments($user) { 00062 global $CFG, $DB; 00063 00064 // we do not create courses here intentionally because it requires full sync and is slow 00065 if (!$this->get_config('dbtype') or !$this->get_config('dbhost') or !$this->get_config('remoteenroltable') or !$this->get_config('remotecoursefield') or !$this->get_config('remoteuserfield')) { 00066 return; 00067 } 00068 00069 $table = $this->get_config('remoteenroltable'); 00070 $coursefield = strtolower($this->get_config('remotecoursefield')); 00071 $userfield = strtolower($this->get_config('remoteuserfield')); 00072 $rolefield = strtolower($this->get_config('remoterolefield')); 00073 00074 $localrolefield = $this->get_config('localrolefield'); 00075 $localuserfield = $this->get_config('localuserfield'); 00076 $localcoursefield = $this->get_config('localcoursefield'); 00077 00078 $unenrolaction = $this->get_config('unenrolaction'); 00079 $defaultrole = $this->get_config('defaultrole'); 00080 00081 $ignorehidden = $this->get_config('ignorehiddencourses'); 00082 00083 if (!is_object($user) or !property_exists($user, 'id')) { 00084 throw new coding_exception('Invalid $user parameter in sync_user_enrolments()'); 00085 } 00086 00087 if (!property_exists($user, $localuserfield)) { 00088 debugging('Invalid $user parameter in sync_user_enrolments(), missing '.$localuserfield); 00089 $user = $DB->get_record('user', array('id'=>$user->id)); 00090 } 00091 00092 // create roles mapping 00093 $allroles = get_all_roles(); 00094 if (!isset($allroles[$defaultrole])) { 00095 $defaultrole = 0; 00096 } 00097 $roles = array(); 00098 foreach ($allroles as $role) { 00099 $roles[$role->$localrolefield] = $role->id; 00100 } 00101 00102 $enrols = array(); 00103 $instances = array(); 00104 00105 if (!$extdb = $this->db_init()) { 00106 // can not connect to database, sorry 00107 return; 00108 } 00109 00110 // read remote enrols and create instances 00111 $sql = $this->db_get_sql($table, array($userfield=>$user->$localuserfield), array(), false); 00112 00113 if ($rs = $extdb->Execute($sql)) { 00114 if (!$rs->EOF) { 00115 while ($fields = $rs->FetchRow()) { 00116 $fields = array_change_key_case($fields, CASE_LOWER); 00117 $fields = $this->db_decode($fields); 00118 00119 if (empty($fields[$coursefield])) { 00120 // missing course info 00121 continue; 00122 } 00123 if (!$course = $DB->get_record('course', array($localcoursefield=>$fields[$coursefield]), 'id,visible')) { 00124 continue; 00125 } 00126 if (!$course->visible and $ignorehidden) { 00127 continue; 00128 } 00129 00130 if (empty($fields[$rolefield]) or !isset($roles[$fields[$rolefield]])) { 00131 if (!$defaultrole) { 00132 // role is mandatory 00133 continue; 00134 } 00135 $roleid = $defaultrole; 00136 } else { 00137 $roleid = $roles[$fields[$rolefield]]; 00138 } 00139 00140 if (empty($enrols[$course->id])) { 00141 $enrols[$course->id] = array(); 00142 } 00143 $enrols[$course->id][] = $roleid; 00144 00145 if ($instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'database'), '*', IGNORE_MULTIPLE)) { 00146 $instances[$course->id] = $instance; 00147 continue; 00148 } 00149 00150 $enrolid = $this->add_instance($course); 00151 $instances[$course->id] = $DB->get_record('enrol', array('id'=>$enrolid)); 00152 } 00153 } 00154 $rs->Close(); 00155 $extdb->Close(); 00156 } else { 00157 // bad luck, something is wrong with the db connection 00158 $extdb->Close(); 00159 return; 00160 } 00161 00162 // enrol user into courses and sync roles 00163 foreach ($enrols as $courseid => $roles) { 00164 if (!isset($instances[$courseid])) { 00165 // ignored 00166 continue; 00167 } 00168 $instance = $instances[$courseid]; 00169 00170 if ($e = $DB->get_record('user_enrolments', array('userid'=>$user->id, 'enrolid'=>$instance->id))) { 00171 // reenable enrolment when previously disable enrolment refreshed 00172 if ($e->status == ENROL_USER_SUSPENDED) { 00173 $this->update_user_enrol($instance, $user->id, ENROL_USER_ACTIVE); 00174 } 00175 } else { 00176 $roleid = reset($roles); 00177 $this->enrol_user($instance, $user->id, $roleid, 0, 0, ENROL_USER_ACTIVE); 00178 } 00179 00180 if (!$context = get_context_instance(CONTEXT_COURSE, $instance->courseid)) { 00181 //weird 00182 continue; 00183 } 00184 $current = $DB->get_records('role_assignments', array('contextid'=>$context->id, 'userid'=>$user->id, 'component'=>'enrol_database', 'itemid'=>$instance->id), '', 'id, roleid'); 00185 00186 $existing = array(); 00187 foreach ($current as $r) { 00188 if (in_array($r->roleid, $roles)) { 00189 $existing[$r->roleid] = $r->roleid; 00190 } else { 00191 role_unassign($r->roleid, $user->id, $context->id, 'enrol_database', $instance->id); 00192 } 00193 } 00194 foreach ($roles as $rid) { 00195 if (!isset($existing[$rid])) { 00196 role_assign($rid, $user->id, $context->id, 'enrol_database', $instance->id); 00197 } 00198 } 00199 } 00200 00201 // unenrol as necessary 00202 $sql = "SELECT e.*, c.visible AS cvisible, ue.status AS ustatus 00203 FROM {enrol} e 00204 JOIN {user_enrolments} ue ON ue.enrolid = e.id 00205 JOIN {course} c ON c.id = e.courseid 00206 WHERE ue.userid = :userid AND e.enrol = 'database'"; 00207 $rs = $DB->get_recordset_sql($sql, array('userid'=>$user->id)); 00208 foreach ($rs as $instance) { 00209 if (!$instance->cvisible and $ignorehidden) { 00210 continue; 00211 } 00212 00213 if (!$context = get_context_instance(CONTEXT_COURSE, $instance->courseid)) { 00214 //weird 00215 continue; 00216 } 00217 00218 if (!empty($enrols[$instance->courseid])) { 00219 // we want this user enrolled 00220 continue; 00221 } 00222 00223 // deal with enrolments removed from external table 00224 if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) { 00225 // unenrol 00226 $this->unenrol_user($instance, $user->id); 00227 00228 } else if ($unenrolaction == ENROL_EXT_REMOVED_KEEP) { 00229 // keep - only adding enrolments 00230 00231 } else if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND or $unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) { 00232 // disable 00233 if ($instance->ustatus != ENROL_USER_SUSPENDED) { 00234 $this->update_user_enrol($instance, $user->id, ENROL_USER_SUSPENDED); 00235 } 00236 if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) { 00237 role_unassign_all(array('contextid'=>$context->id, 'userid'=>$user->id, 'component'=>'enrol_database', 'itemid'=>$instance->id)); 00238 } 00239 } 00240 } 00241 $rs->close(); 00242 } 00243 00250 public function sync_enrolments($verbose = false) { 00251 global $CFG, $DB; 00252 00253 // we do not create courses here intentionally because it requires full sync and is slow 00254 if (!$this->get_config('dbtype') or !$this->get_config('dbhost') or !$this->get_config('remoteenroltable') or !$this->get_config('remotecoursefield') or !$this->get_config('remoteuserfield')) { 00255 if ($verbose) { 00256 mtrace('User enrolment synchronisation skipped.'); 00257 } 00258 return 0; 00259 } 00260 00261 if ($verbose) { 00262 mtrace('Starting user enrolment synchronisation...'); 00263 } 00264 00265 if (!$extdb = $this->db_init()) { 00266 mtrace('Error while communicating with external enrolment database'); 00267 return 1; 00268 } 00269 00270 // we may need a lot of memory here 00271 @set_time_limit(0); 00272 raise_memory_limit(MEMORY_HUGE); 00273 00274 // second step is to sync instances and users 00275 $table = $this->get_config('remoteenroltable'); 00276 $coursefield = strtolower($this->get_config('remotecoursefield')); 00277 $userfield = strtolower($this->get_config('remoteuserfield')); 00278 $rolefield = strtolower($this->get_config('remoterolefield')); 00279 00280 $localrolefield = $this->get_config('localrolefield'); 00281 $localuserfield = $this->get_config('localuserfield'); 00282 $localcoursefield = $this->get_config('localcoursefield'); 00283 00284 $unenrolaction = $this->get_config('unenrolaction'); 00285 $defaultrole = $this->get_config('defaultrole'); 00286 00287 // create roles mapping 00288 $allroles = get_all_roles(); 00289 if (!isset($allroles[$defaultrole])) { 00290 $defaultrole = 0; 00291 } 00292 $roles = array(); 00293 foreach ($allroles as $role) { 00294 $roles[$role->$localrolefield] = $role->id; 00295 } 00296 00297 // get a list of courses to be synced that are in external table 00298 $externalcourses = array(); 00299 $sql = $this->db_get_sql($table, array(), array($coursefield), true); 00300 if ($rs = $extdb->Execute($sql)) { 00301 if (!$rs->EOF) { 00302 while ($mapping = $rs->FetchRow()) { 00303 $mapping = reset($mapping); 00304 $mapping = $this->db_decode($mapping); 00305 if (empty($mapping)) { 00306 // invalid mapping 00307 continue; 00308 } 00309 $externalcourses[$mapping] = true; 00310 } 00311 } 00312 $rs->Close(); 00313 } else { 00314 mtrace('Error reading data from the external enrolment table'); 00315 $extdb->Close(); 00316 return 2; 00317 } 00318 $preventfullunenrol = empty($externalcourses); 00319 if ($preventfullunenrol and $unenrolaction == ENROL_EXT_REMOVED_UNENROL) { 00320 if ($verbose) { 00321 mtrace(' Preventing unenrolment of all current users, because it might result in major data loss, there has to be at least one record in external enrol table, sorry.'); 00322 } 00323 } 00324 00325 // first find all existing courses with enrol instance 00326 $existing = array(); 00327 $sql = "SELECT c.id, c.visible, c.$localcoursefield AS mapping, e.id AS enrolid, c.shortname 00328 FROM {course} c 00329 JOIN {enrol} e ON (e.courseid = c.id AND e.enrol = 'database')"; 00330 $rs = $DB->get_recordset_sql($sql); // watch out for idnumber duplicates 00331 foreach ($rs as $course) { 00332 if (empty($course->mapping)) { 00333 continue; 00334 } 00335 $existing[$course->mapping] = $course; 00336 } 00337 $rs->close(); 00338 00339 // add necessary enrol instances that are not present yet 00340 $params = array(); 00341 $localnotempty = ""; 00342 if ($localcoursefield !== 'id') { 00343 $localnotempty = "AND c.$localcoursefield <> :lcfe"; 00344 $params['lcfe'] = $DB->sql_empty(); 00345 } 00346 $sql = "SELECT c.id, c.visible, c.$localcoursefield AS mapping, c.shortname 00347 FROM {course} c 00348 LEFT JOIN {enrol} e ON (e.courseid = c.id AND e.enrol = 'database') 00349 WHERE e.id IS NULL $localnotempty"; 00350 $rs = $DB->get_recordset_sql($sql, $params); 00351 foreach ($rs as $course) { 00352 if (empty($course->mapping)) { 00353 continue; 00354 } 00355 if (!isset($externalcourses[$course->mapping])) { 00356 // course not synced 00357 continue; 00358 } 00359 if (isset($existing[$course->mapping])) { 00360 // some duplicate, sorry 00361 continue; 00362 } 00363 $course->enrolid = $this->add_instance($course); 00364 $existing[$course->mapping] = $course; 00365 } 00366 $rs->close(); 00367 00368 // free memory 00369 unset($externalcourses); 00370 00371 // sync enrolments 00372 $ignorehidden = $this->get_config('ignorehiddencourses'); 00373 $sqlfields = array($userfield); 00374 if ($rolefield) { 00375 $sqlfields[] = $rolefield; 00376 } 00377 foreach ($existing as $course) { 00378 if ($ignorehidden and !$course->visible) { 00379 continue; 00380 } 00381 if (!$instance = $DB->get_record('enrol', array('id'=>$course->enrolid))) { 00382 continue; //weird 00383 } 00384 $context = get_context_instance(CONTEXT_COURSE, $course->id); 00385 00386 // get current list of enrolled users with their roles 00387 $current_roles = array(); 00388 $current_status = array(); 00389 $user_mapping = array(); 00390 $sql = "SELECT u.$localuserfield AS mapping, u.id, ue.status, ue.userid, ra.roleid 00391 FROM {user} u 00392 JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid = :enrolid) 00393 JOIN {role_assignments} ra ON (ra.userid = u.id AND ra.itemid = ue.enrolid AND ra.component = 'enrol_database') 00394 WHERE u.deleted = 0"; 00395 $params = array('enrolid'=>$instance->id); 00396 if ($localuserfield === 'username') { 00397 $sql .= " AND u.mnethostid = :mnethostid"; 00398 $params['mnethostid'] = $CFG->mnet_localhost_id; 00399 } 00400 $rs = $DB->get_recordset_sql($sql, $params); 00401 foreach ($rs as $ue) { 00402 $current_roles[$ue->userid][$ue->roleid] = $ue->roleid; 00403 $current_status[$ue->userid] = $ue->status; 00404 $user_mapping[$ue->mapping] = $ue->userid; 00405 } 00406 $rs->close(); 00407 00408 // get list of users that need to be enrolled and their roles 00409 $requested_roles = array(); 00410 $sql = $this->db_get_sql($table, array($coursefield=>$course->mapping), $sqlfields); 00411 if ($rs = $extdb->Execute($sql)) { 00412 if (!$rs->EOF) { 00413 if ($localuserfield === 'username') { 00414 $usersearch = array('mnethostid'=>$CFG->mnet_localhost_id, 'deleted' =>0); 00415 } 00416 while ($fields = $rs->FetchRow()) { 00417 $fields = array_change_key_case($fields, CASE_LOWER); 00418 if (empty($fields[$userfield])) { 00419 //user identification is mandatory! 00420 } 00421 $mapping = $fields[$userfield]; 00422 if (!isset($user_mapping[$mapping])) { 00423 $usersearch[$localuserfield] = $mapping; 00424 if (!$user = $DB->get_record('user', $usersearch, 'id', IGNORE_MULTIPLE)) { 00425 // user does not exist or was deleted 00426 continue; 00427 } 00428 $user_mapping[$mapping] = $user->id; 00429 $userid = $user->id; 00430 } else { 00431 $userid = $user_mapping[$mapping]; 00432 } 00433 if (empty($fields[$rolefield]) or !isset($roles[$fields[$rolefield]])) { 00434 if (!$defaultrole) { 00435 // role is mandatory 00436 continue; 00437 } 00438 $roleid = $defaultrole; 00439 } else { 00440 $roleid = $roles[$fields[$rolefield]]; 00441 } 00442 00443 $requested_roles[$userid][$roleid] = $roleid; 00444 } 00445 } 00446 $rs->Close(); 00447 } else { 00448 mtrace('Error while communicating with external enrolment database'); 00449 $extdb->Close(); 00450 return; 00451 } 00452 unset($user_mapping); 00453 00454 // enrol all users and sync roles 00455 foreach ($requested_roles as $userid=>$userroles) { 00456 foreach ($userroles as $roleid) { 00457 if (empty($current_roles[$userid])) { 00458 $this->enrol_user($instance, $userid, $roleid, 0, 0, ENROL_USER_ACTIVE); 00459 $current_roles[$userid][$roleid] = $roleid; 00460 $current_status[$userid] = ENROL_USER_ACTIVE; 00461 if ($verbose) { 00462 mtrace(" enrolling: $userid ==> $course->shortname as ".$allroles[$roleid]->shortname); 00463 } 00464 } 00465 } 00466 00467 // assign extra roles 00468 foreach ($userroles as $roleid) { 00469 if (empty($current_roles[$userid][$roleid])) { 00470 role_assign($roleid, $userid, $context->id, 'enrol_database', $instance->id); 00471 $current_roles[$userid][$roleid] = $roleid; 00472 if ($verbose) { 00473 mtrace(" assigning roles: $userid ==> $course->shortname as ".$allroles[$roleid]->shortname); 00474 } 00475 } 00476 } 00477 00478 // unassign removed roles 00479 foreach($current_roles[$userid] as $cr) { 00480 if (empty($userroles[$cr])) { 00481 role_unassign($cr, $userid, $context->id, 'enrol_database', $instance->id); 00482 unset($current_roles[$userid][$cr]); 00483 if ($verbose) { 00484 mtrace(" unsassigning roles: $userid ==> $course->shortname"); 00485 } 00486 } 00487 } 00488 00489 // reenable enrolment when previously disable enrolment refreshed 00490 if ($current_status[$userid] == ENROL_USER_SUSPENDED) { 00491 $this->update_user_enrol($instance, $userid, ENROL_USER_ACTIVE); 00492 if ($verbose) { 00493 mtrace(" unsuspending: $userid ==> $course->shortname"); 00494 } 00495 } 00496 } 00497 00498 // deal with enrolments removed from external table 00499 if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) { 00500 if (!$preventfullunenrol) { 00501 // unenrol 00502 foreach ($current_status as $userid=>$status) { 00503 if (isset($requested_roles[$userid])) { 00504 continue; 00505 } 00506 $this->unenrol_user($instance, $userid); 00507 if ($verbose) { 00508 mtrace(" unenrolling: $userid ==> $course->shortname"); 00509 } 00510 } 00511 } 00512 00513 } else if ($unenrolaction == ENROL_EXT_REMOVED_KEEP) { 00514 // keep - only adding enrolments 00515 00516 } else if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND or $unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) { 00517 // disable 00518 foreach ($current_status as $userid=>$status) { 00519 if (isset($requested_roles[$userid])) { 00520 continue; 00521 } 00522 if ($status != ENROL_USER_SUSPENDED) { 00523 $this->update_user_enrol($instance, $userid, ENROL_USER_SUSPENDED); 00524 if ($verbose) { 00525 mtrace(" suspending: $userid ==> $course->shortname"); 00526 } 00527 } 00528 if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) { 00529 role_unassign_all(array('contextid'=>$context->id, 'userid'=>$userid, 'component'=>'enrol_database', 'itemid'=>$instance->id)); 00530 if ($verbose) { 00531 mtrace(" unsassigning all roles: $userid ==> $course->shortname"); 00532 } 00533 } 00534 } 00535 } 00536 } 00537 00538 // close db connection 00539 $extdb->Close(); 00540 00541 if ($verbose) { 00542 mtrace('...user enrolment synchronisation finished.'); 00543 } 00544 00545 return 0; 00546 } 00547 00557 public function sync_courses($verbose = false) { 00558 global $CFG, $DB; 00559 00560 // make sure we sync either enrolments or courses 00561 if (!$this->get_config('dbtype') or !$this->get_config('dbhost') or !$this->get_config('newcoursetable') or !$this->get_config('newcoursefullname') or !$this->get_config('newcourseshortname')) { 00562 if ($verbose) { 00563 mtrace('Course synchronisation skipped.'); 00564 } 00565 return 0; 00566 } 00567 00568 if ($verbose) { 00569 mtrace('Starting course synchronisation...'); 00570 } 00571 00572 // we may need a lot of memory here 00573 @set_time_limit(0); 00574 raise_memory_limit(MEMORY_HUGE); 00575 00576 if (!$extdb = $this->db_init()) { 00577 mtrace('Error while communicating with external enrolment database'); 00578 return 1; 00579 } 00580 00581 // first create new courses 00582 $table = $this->get_config('newcoursetable'); 00583 $fullname = strtolower($this->get_config('newcoursefullname')); 00584 $shortname = strtolower($this->get_config('newcourseshortname')); 00585 $idnumber = strtolower($this->get_config('newcourseidnumber')); 00586 $category = strtolower($this->get_config('newcoursecategory')); 00587 00588 $sqlfields = array($fullname, $shortname); 00589 if ($category) { 00590 $sqlfields[] = $category; 00591 } 00592 if ($idnumber) { 00593 $sqlfields[] = $idnumber; 00594 } 00595 $sql = $this->db_get_sql($table, array(), $sqlfields); 00596 $createcourses = array(); 00597 if ($rs = $extdb->Execute($sql)) { 00598 if (!$rs->EOF) { 00599 while ($fields = $rs->FetchRow()) { 00600 $fields = array_change_key_case($fields, CASE_LOWER); 00601 $fields = $this->db_decode($fields); 00602 if (empty($fields[$shortname]) or empty($fields[$fullname])) { 00603 if ($verbose) { 00604 mtrace(' error: invalid external course record, shortname and fullname are mandatory: ' . json_encode($fields)); // hopefully every geek can read JS, right? 00605 } 00606 continue; 00607 } 00608 if ($DB->record_exists('course', array('shortname'=>$fields[$shortname]))) { 00609 // already exists 00610 continue; 00611 } 00612 // allow empty idnumber but not duplicates 00613 if ($idnumber and $fields[$idnumber] !== '' and $fields[$idnumber] !== null and $DB->record_exists('course', array('idnumber'=>$fields[$idnumber]))) { 00614 if ($verbose) { 00615 mtrace(' error: duplicate idnumber, can not create course: '.$fields[$shortname].' ['.$fields[$idnumber].']'); 00616 } 00617 continue; 00618 } 00619 if ($category and !$DB->record_exists('course_categories', array('id'=>$fields[$category]))) { 00620 if ($verbose) { 00621 mtrace(' error: invalid category id, can not create course: '.$fields[$shortname]); 00622 } 00623 continue; 00624 } 00625 $course = new stdClass(); 00626 $course->fullname = $fields[$fullname]; 00627 $course->shortname = $fields[$shortname]; 00628 $course->idnumber = $idnumber ? $fields[$idnumber] : NULL; 00629 $course->category = $category ? $fields[$category] : NULL; 00630 $createcourses[] = $course; 00631 } 00632 } 00633 $rs->Close(); 00634 } else { 00635 mtrace('Error reading data from the external course table'); 00636 $extdb->Close(); 00637 return 4; 00638 } 00639 if ($createcourses) { 00640 require_once("$CFG->dirroot/course/lib.php"); 00641 00642 $templatecourse = $this->get_config('templatecourse'); 00643 $defaultcategory = $this->get_config('defaultcategory'); 00644 00645 $template = false; 00646 if ($templatecourse) { 00647 if ($template = $DB->get_record('course', array('shortname'=>$templatecourse))) { 00648 unset($template->id); 00649 unset($template->fullname); 00650 unset($template->shortname); 00651 unset($template->idnumber); 00652 } else { 00653 if ($verbose) { 00654 mtrace(" can not find template for new course!"); 00655 } 00656 } 00657 } 00658 if (!$template) { 00659 $courseconfig = get_config('moodlecourse'); 00660 $template = new stdClass(); 00661 $template->summary = ''; 00662 $template->summaryformat = FORMAT_HTML; 00663 $template->format = $courseconfig->format; 00664 $template->numsections = $courseconfig->numsections; 00665 $template->hiddensections = $courseconfig->hiddensections; 00666 $template->newsitems = $courseconfig->newsitems; 00667 $template->showgrades = $courseconfig->showgrades; 00668 $template->showreports = $courseconfig->showreports; 00669 $template->maxbytes = $courseconfig->maxbytes; 00670 $template->groupmode = $courseconfig->groupmode; 00671 $template->groupmodeforce = $courseconfig->groupmodeforce; 00672 $template->visible = $courseconfig->visible; 00673 $template->lang = $courseconfig->lang; 00674 $template->groupmodeforce = $courseconfig->groupmodeforce; 00675 } 00676 if (!$DB->record_exists('course_categories', array('id'=>$defaultcategory))) { 00677 if ($verbose) { 00678 mtrace(" default course category does not exist!"); 00679 } 00680 $categories = $DB->get_records('course_categories', array(), 'sortorder', 'id', 0, 1); 00681 $first = reset($categories); 00682 $defaultcategory = $first->id; 00683 } 00684 00685 foreach ($createcourses as $fields) { 00686 $newcourse = clone($template); 00687 $newcourse->fullname = $fields->fullname; 00688 $newcourse->shortname = $fields->shortname; 00689 $newcourse->idnumber = $fields->idnumber; 00690 $newcourse->category = $fields->category ? $fields->category : $defaultcategory; 00691 00692 $c = create_course($newcourse); 00693 if ($verbose) { 00694 mtrace(" creating course: $c->id, $c->fullname, $c->shortname, $c->idnumber, $c->category"); 00695 } 00696 } 00697 00698 unset($createcourses); 00699 unset($template); 00700 } 00701 00702 // close db connection 00703 $extdb->Close(); 00704 00705 if ($verbose) { 00706 mtrace('...course synchronisation finished.'); 00707 } 00708 00709 return 0; 00710 } 00711 00712 protected function db_get_sql($table, array $conditions, array $fields, $distinct = false, $sort = "") { 00713 $fields = $fields ? implode(',', $fields) : "*"; 00714 $where = array(); 00715 if ($conditions) { 00716 foreach ($conditions as $key=>$value) { 00717 $value = $this->db_encode($this->db_addslashes($value)); 00718 00719 $where[] = "$key = '$value'"; 00720 } 00721 } 00722 $where = $where ? "WHERE ".implode(" AND ", $where) : ""; 00723 $sort = $sort ? "ORDER BY $sort" : ""; 00724 $distinct = $distinct ? "DISTINCT" : ""; 00725 $sql = "SELECT $distinct $fields 00726 FROM $table 00727 $where 00728 $sort"; 00729 00730 return $sql; 00731 } 00732 00738 protected function db_init() { 00739 global $CFG; 00740 00741 require_once($CFG->libdir.'/adodb/adodb.inc.php'); 00742 00743 // Connect to the external database (forcing new connection) 00744 $extdb = ADONewConnection($this->get_config('dbtype')); 00745 if ($this->get_config('debugdb')) { 00746 $extdb->debug = true; 00747 ob_start(); //start output buffer to allow later use of the page headers 00748 } 00749 00750 $result = $extdb->Connect($this->get_config('dbhost'), $this->get_config('dbuser'), $this->get_config('dbpass'), $this->get_config('dbname'), true); 00751 if (!$result) { 00752 return null; 00753 } 00754 00755 $extdb->SetFetchMode(ADODB_FETCH_ASSOC); 00756 if ($this->get_config('dbsetupsql')) { 00757 $extdb->Execute($this->get_config('dbsetupsql')); 00758 } 00759 return $extdb; 00760 } 00761 00762 protected function db_addslashes($text) { 00763 // using custom made function for now 00764 if ($this->get_config('dbsybasequoting')) { 00765 $text = str_replace('\\', '\\\\', $text); 00766 $text = str_replace(array('\'', '"', "\0"), array('\\\'', '\\"', '\\0'), $text); 00767 } else { 00768 $text = str_replace("'", "''", $text); 00769 } 00770 return $text; 00771 } 00772 00773 protected function db_encode($text) { 00774 $dbenc = $this->get_config('dbencoding'); 00775 if (empty($dbenc) or $dbenc == 'utf-8') { 00776 return $text; 00777 } 00778 if (is_array($text)) { 00779 foreach($text as $k=>$value) { 00780 $text[$k] = $this->db_encode($value); 00781 } 00782 return $text; 00783 } else { 00784 return textlib_get_instance()->convert($text, 'utf-8', $dbenc); 00785 } 00786 } 00787 00788 protected function db_decode($text) { 00789 $dbenc = $this->get_config('dbencoding'); 00790 if (empty($dbenc) or $dbenc == 'utf-8') { 00791 return $text; 00792 } 00793 if (is_array($text)) { 00794 foreach($text as $k=>$value) { 00795 $text[$k] = $this->db_decode($value); 00796 } 00797 return $text; 00798 } else { 00799 return textlib_get_instance()->convert($text, $dbenc, 'utf-8'); 00800 } 00801 } 00802 } 00803