[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/enrol/database/ -> enrol.php (source)

   1  <?php  // $Id: enrol.php,v 1.42.2.5 2008/08/27 06:26:49 jerome Exp $
   2  
   3  require_once($CFG->dirroot.'/enrol/enrol.class.php');
   4  
   5  class enrolment_plugin_database {
   6  
   7      var $log;
   8  
   9  /*
  10   * For the given user, let's go out and look in an external database
  11   * for an authoritative list of enrolments, and then adjust the
  12   * local Moodle assignments to match.
  13   */
  14  function setup_enrolments(&$user) {
  15      global $CFG;
  16  
  17      // NOTE: if $this->enrol_connect() succeeds you MUST remember to call
  18      // $this->enrol_disconnect() as it is doing some nasty vodoo with $CFG->prefix
  19      $enroldb = $this->enrol_connect();
  20      if (!$enroldb) {
  21          error_log('[ENROL_DB] Could not make a connection');
  22          return;
  23      }
  24  
  25      // If we are expecting to get role information from our remote db, then
  26      // we execute the below code for every role type.  Otherwise we just
  27      // execute it once with null (hence the dummy array).
  28      $roles = !empty($CFG->enrol_db_remoterolefield) && !empty($CFG->enrol_db_localrolefield)
  29          ? get_records('role')
  30          : array(null);
  31  
  32      //error_log('[ENROL_DB] found ' . count($roles) . ' roles:');
  33  
  34      foreach($roles as $role) {
  35  
  36          //error_log('[ENROL_DB] setting up enrolments for '.$role->shortname);
  37  
  38          /// Get the authoritative list of enrolments from the external database table
  39          /// We're using the ADOdb functions natively here and not our datalib functions
  40          /// because we didn't want to mess with the $db global
  41  
  42          $useridfield = $enroldb->quote($user->{$CFG->enrol_localuserfield});
  43  
  44          list($have_role, $remote_role_name, $remote_role_value) = $this->role_fields($enroldb, $role);
  45  
  46          /// Check if a particular role has been forced by the plugin site-wide
  47          /// (if we aren't doing a role-based select)
  48          if (!$have_role && $CFG->enrol_db_defaultcourseroleid) {
  49              $role = get_record('role', 'id', $CFG->enrol_db_defaultcourseroleid);
  50          }
  51  
  52          /// Whether to fetch the default role on a per-course basis (below) or not.
  53          $use_default_role = !$role;
  54  
  55          /*
  56          if ($have_role) {
  57              error_log('[ENROL_DB] Doing role-specific select from db for role: '.$role->shortname);
  58          } elseif ($use_default_role) {
  59              error_log('[ENROL_DB] Using course default for roles - assuming that database lists defaults');
  60          } else {
  61              error_log('[ENROL_DB] Using config default for roles: '.$role->shortname);
  62          }*/
  63  
  64          if ($rs = $enroldb->Execute("SELECT {$CFG->enrol_remotecoursefield} as enrolremotecoursefield
  65                                         FROM {$CFG->enrol_dbtable}
  66                                        WHERE {$CFG->enrol_remoteuserfield} = " . $useridfield .
  67                                          (isset($remote_role_name, $remote_role_value) ? ' AND '.$remote_role_name.' = '.$remote_role_value : ''))) {
  68  
  69              // We'll use this to see what to add and remove
  70              $existing = $role
  71                  ? get_records_sql("
  72                      SELECT * FROM {$CFG->prefix}role_assignments
  73                      WHERE userid = {$user->id}
  74                       AND roleid = {$role->id}")
  75                  : get_records('role_assignments', 'userid', $user->id);
  76  
  77              if (!$existing) {
  78                  $existing = array();
  79              }
  80  
  81  
  82              if (!$rs->EOF) {   // We found some courses
  83  
  84                  //$count = 0;
  85                  $courselist = array();
  86                  while ($fields_obj = rs_fetch_next_record($rs)) {         // Make a nice little array of courses to process
  87                      $fields_obj = (object)array_change_key_case((array)$fields_obj , CASE_LOWER);
  88                      $courselist[] = $fields_obj->enrolremotecoursefield;
  89                      //$count++;
  90                  }
  91                  rs_close($rs);
  92  
  93                  //error_log('[ENROL_DB] Found '.count($existing).' existing roles and '.$count.' in external database');
  94  
  95                  foreach ($courselist as $coursefield) {   /// Check the list of courses against existing
  96                      $course = get_record('course', $CFG->enrol_localcoursefield, $coursefield);
  97                      if (!is_object($course)) {
  98                          if (empty($CFG->enrol_db_autocreate)) { // autocreation not allowed
  99                              if (debugging('',DEBUG_ALL)) {
 100                                  error_log( "Course $coursefield does not exist, skipping") ;
 101                              }
 102                              continue; // next foreach course
 103                          }
 104                          // ok, now then let's create it!
 105                          // prepare any course properties we actually have
 106                          $course = new StdClass;
 107                          $course->{$CFG->enrol_localcoursefield} = $coursefield;
 108                          $course->fullname  = $coursefield;
 109                          $course->shortname = $coursefield;
 110                          if (!($newcourseid = $this->create_course($course, true)
 111                              and $course = get_record( 'course', 'id', $newcourseid))) {
 112                              error_log( "Creating course $coursefield failed");
 113                              continue; // nothing left to do...
 114                          }
 115                      }
 116  
 117                      // if the course is hidden and we don't want to enrol in hidden courses
 118                      // then just skip it
 119                      if (!$course->visible and $CFG->enrol_db_ignorehiddencourse) {
 120                          continue;
 121                      }
 122  
 123                      /// If there's no role specified, we get the default course role (usually student)
 124                      if ($use_default_role) {
 125                          $role = get_default_course_role($course);
 126                      }
 127  
 128                      $context = get_context_instance(CONTEXT_COURSE, $course->id);
 129  
 130                      // Couldn't get a role or context, skip.
 131                      if (!$role || !$context) {
 132                          continue;
 133                      }
 134  
 135                      // Search the role assignments to see if this user
 136                      // already has this role in this context.  If it is, we
 137                      // skip to the next course.
 138                      foreach($existing as $key => $role_assignment) {
 139                          if ($role_assignment->roleid == $role->id
 140                              && $role_assignment->contextid == $context->id) {
 141                              unset($existing[$key]);
 142                              //error_log('[ENROL_DB] User is already enroled in course '.$course->idnumber);
 143                              continue 2;
 144                          }
 145                      }
 146  
 147                      //error_log('[ENROL_DB] Enrolling user in course '.$course->idnumber);
 148                      role_assign($role->id, $user->id, 0, $context->id, 0, 0, 0, 'database');
 149                  }
 150              } // We've processed all external courses found
 151  
 152              /// We have some courses left that we might need to unenrol from
 153              /// Note: we only process enrolments that we (ie 'database' plugin) made
 154              /// Do not unenrol anybody if the disableunenrol option is 'yes'
 155              if (!$CFG->enrol_db_disableunenrol) {
 156                  foreach ($existing as $role_assignment) {
 157                      if ($role_assignment->enrol == 'database') {
 158                          //error_log('[ENROL_DB] Removing user from context '.$role_assignment->contextid);
 159                          role_unassign($role_assignment->roleid, $user->id, '', $role_assignment->contextid);
 160                      } 
 161                  }
 162              }
 163          } else {
 164              error_log('[ENROL_DB] Couldn\'t get rows from external db: '.$enroldb->ErrorMsg());
 165          }
 166      }
 167      $this->enrol_disconnect($enroldb);
 168  }
 169  
 170  /**
 171   * sync enrolments with database, create courses if required.
 172   *
 173   * @param object The role to sync for. If no role is specified, defaults are
 174   * used.
 175   */
 176  function sync_enrolments($role = null) {
 177      global $CFG;
 178      global $db;
 179      error_reporting(E_ALL);
 180  
 181      // Connect to the external database
 182      $enroldb = $this->enrol_connect();
 183      if (!$enroldb) {
 184          notify("enrol/database cannot connect to server");
 185          return false;
 186      }
 187  
 188      if (isset($role)) {
 189          echo '=== Syncing enrolments for role: '.$role->shortname." ===\n";
 190      } else {
 191          echo "=== Syncing enrolments for default role ===\n";
 192      }
 193  
 194      // first, pack the sortorder...
 195      fix_course_sortorder();
 196  
 197      list($have_role, $remote_role_name, $remote_role_value) = $this->role_fields($enroldb, $role);
 198  
 199      if (!$have_role) {
 200          if (!empty($CFG->enrol_db_defaultcourseroleid)
 201           and $role = get_record('role', 'id', $CFG->enrol_db_defaultcourseroleid)) {
 202              echo "=== Using enrol_db_defaultcourseroleid: {$role->id} ({$role->shortname}) ===\n";
 203          } elseif (isset($role)) {
 204              echo "!!! WARNING: Role specified by caller, but no (or invalid) role configuration !!!\n";
 205          }
 206      }
 207  
 208      // get enrolments per-course
 209      $sql =  "SELECT DISTINCT {$CFG->enrol_remotecoursefield} " .
 210          " FROM {$CFG->enrol_dbtable} " .
 211          " WHERE {$CFG->enrol_remoteuserfield} IS NOT NULL" .
 212          (isset($remote_role_name, $remote_role_value) ? ' AND '.$remote_role_name.' = '.$remote_role_value : '');
 213  
 214      $rs = $enroldb->Execute($sql);
 215      if (!$rs) {
 216          trigger_error($enroldb->ErrorMsg() .' STATEMENT: '. $sql);
 217          return false;
 218      }
 219      if ( $rs->EOF ) { // no courses! outta here...
 220          return true;
 221      }
 222  
 223      begin_sql();
 224      $extcourses = array();
 225      while ($extcourse_obj = rs_fetch_next_record($rs)) { // there are more course records
 226          $extcourse_obj = (object)array_change_key_case((array)$extcourse_obj , CASE_LOWER);
 227          $extcourse = $extcourse_obj->{strtolower($CFG->enrol_remotecoursefield)};
 228          array_push($extcourses, $extcourse);
 229  
 230          // does the course exist in moodle already?
 231          $course = false;
 232          $course = get_record( 'course',
 233                                $CFG->enrol_localcoursefield,
 234                                $extcourse );
 235  
 236          if (!is_object($course)) {
 237              if (empty($CFG->enrol_db_autocreate)) { // autocreation not allowed
 238                  if (debugging('', DEBUG_ALL)) {
 239                      error_log( "Course $extcourse does not exist, skipping");
 240                  }
 241                  continue; // next foreach course
 242              }
 243              // ok, now then let's create it!
 244              // prepare any course properties we actually have
 245              $course = new StdClass;
 246              $course->{$CFG->enrol_localcoursefield} = $extcourse;
 247              $course->fullname  = $extcourse;
 248              $course->shortname = $extcourse;
 249              if (!($newcourseid = $this->create_course($course, true)
 250               and $course = get_record( 'course', 'id', $newcourseid))) {
 251                  error_log( "Creating course $extcourse failed");
 252                  continue; // nothing left to do...
 253              }
 254  
 255          }
 256  
 257          $context = get_context_instance(CONTEXT_COURSE, $course->id);
 258  
 259          // If we don't have a proper role setup, then we default to the default
 260          // role for the current course.
 261          if (!$have_role) {
 262              $role = get_default_course_role($course);
 263          }
 264  
 265          // get a list of the student ids the are enrolled
 266          // in the external db -- hopefully it'll fit in memory...
 267          $extenrolments = array();
 268          $sql = "SELECT {$CFG->enrol_remoteuserfield} " .
 269              " FROM {$CFG->enrol_dbtable} " .
 270              " WHERE {$CFG->enrol_remotecoursefield} = " . $enroldb->quote($extcourse) .
 271                  ($have_role ? ' AND '.$remote_role_name.' = '.$remote_role_value : '');
 272  
 273          $crs = $enroldb->Execute($sql);
 274          if (!$crs) {
 275              trigger_error($enroldb->ErrorMsg() .' STATEMENT: '. $sql);
 276              return false;
 277          }
 278          if ( $crs->EOF ) { // shouldn't happen, but cover all bases
 279              continue;
 280          }
 281  
 282          // slurp results into an array
 283          while ($crs_obj = rs_fetch_next_record($crs)) {
 284              $crs_obj = (object)array_change_key_case((array)$crs_obj , CASE_LOWER);
 285              array_push($extenrolments, $crs_obj->{strtolower($CFG->enrol_remoteuserfield)});
 286          }
 287          rs_close($crs); // release the handle
 288  
 289          //
 290          // prune enrolments to users that are no longer in ext auth
 291          // hopefully they'll fit in the max buffer size for the RDBMS
 292          //
 293          // TODO: This doesn't work perfectly.  If we are operating without
 294          // roles in the external DB, then this doesn't handle changes of role
 295          // within a course (because the user is still enrolled in the course,
 296          // so NOT IN misses the course).
 297          //
 298          // When the user logs in though, their role list will be updated
 299          // correctly.
 300          //
 301          if (!$CFG->enrol_db_disableunenrol) {
 302              $to_prune = get_records_sql("
 303               SELECT ra.*
 304               FROM {$CFG->prefix}role_assignments ra
 305                JOIN {$CFG->prefix}user u ON ra.userid = u.id
 306               WHERE ra.enrol = 'database'
 307                AND ra.contextid = {$context->id}
 308                AND ra.roleid = ". $role->id . ($extenrolments
 309                  ? " AND u.{$CFG->enrol_localuserfield} NOT IN (".join(", ", array_map(array(&$db, 'quote'), $extenrolments)).")"
 310                  : ''));
 311  
 312              if ($to_prune) {
 313                  foreach ($to_prune as $role_assignment) {
 314                      if (role_unassign($role->id, $role_assignment->userid, 0, $role_assignment->contextid)){
 315                          error_log( "Unassigned {$role->shortname} assignment #{$role_assignment->id} for course {$course->id} (" . format_string($course->shortname) . "); user {$role_assignment->userid}");
 316                      } else {
 317                          error_log( "Failed to unassign {$role->shortname} assignment #{$role_assignment->id} for course {$course->id} (" . format_string($course->shortname) . "); user {$role_assignment->userid}");
 318                      }
 319                  }
 320              }
 321          }
 322  
 323          //
 324          // insert current enrolments
 325          // bad we can't do INSERT IGNORE with postgres...
 326          //
 327          foreach ($extenrolments as $member) {
 328              // Get the user id and whether is enrolled in one fell swoop
 329              $sql = "
 330                  SELECT u.id AS userid, ra.id AS enrolmentid
 331                  FROM {$CFG->prefix}user u
 332                   LEFT OUTER JOIN {$CFG->prefix}role_assignments ra ON u.id = ra.userid
 333                    AND ra.roleid = {$role->id}
 334                    AND ra.contextid = {$context->id}
 335                   WHERE u.{$CFG->enrol_localuserfield} = ".$db->quote($member) .
 336                   " AND (u.deleted IS NULL OR u.deleted=0) ";
 337  
 338              $ers = $db->Execute($sql);
 339              if (!$ers) {
 340                  trigger_error($db->ErrorMsg() .' STATEMENT: '. $sql);
 341                  return false;
 342              }
 343              if ( $ers->EOF ) { // if this returns empty, it means we don't have the student record.
 344                                                // should not happen -- but skip it anyway
 345                  trigger_error('weird! no user record entry?');
 346                  continue;
 347              }
 348              $user_obj = rs_fetch_record($ers);
 349              $userid      = $user_obj->userid;
 350              $enrolmentid = $user_obj->enrolmentid;
 351              rs_close($ers); // release the handle
 352  
 353              if ($enrolmentid) { // already enrolled - skip
 354                  continue;
 355              }
 356  
 357              if (role_assign($role->id, $userid, 0, $context->id, 0, 0, 0, 'database')){
 358                  error_log( "Assigned role {$role->shortname} to user {$userid} in course {$course->id} (" . format_string($course->shortname) . ")");
 359              } else {
 360                  error_log( "Failed to assign role {$role->shortname} to user {$userid} in course {$course->id} (" . format_string($course->shortname) . ")");
 361              }
 362  
 363          } // end foreach member
 364      } // end while course records
 365      rs_close($rs); //Close the main course recordset
 366  
 367      //
 368      // prune enrolments to courses that are no longer in ext auth
 369      //
 370      // TODO: This doesn't work perfectly.  If we are operating without
 371      // roles in the external DB, then this doesn't handle changes of role
 372      // within a course (because the user is still enrolled in the course,
 373      // so NOT IN misses the course).
 374      //
 375      // When the user logs in though, their role list will be updated
 376      // correctly.
 377      //
 378      if (!$CFG->enrol_db_disableunenrol) {
 379          $sql = "
 380              SELECT ra.roleid, ra.userid, ra.contextid
 381              FROM {$CFG->prefix}role_assignments ra
 382                  JOIN {$CFG->prefix}context cn ON cn.id = ra.contextid
 383                  JOIN {$CFG->prefix}course c ON c.id = cn.instanceid
 384              WHERE ra.enrol = 'database'
 385                AND cn.contextlevel = ".CONTEXT_COURSE." " .
 386                  ($have_role ? ' AND ra.roleid = '.$role->id : '') .
 387                  ($extcourses
 388                      ? " AND c.{$CFG->enrol_localcoursefield} NOT IN (" . join(",", array_map(array(&$db, 'quote'), $extcourses)) . ")"
 389                      : '');
 390  
 391          $ers = $db->Execute($sql);
 392          if (!$ers) {
 393              trigger_error($db->ErrorMsg() .' STATEMENT: '. $sql);
 394              return false;
 395          }
 396          if ( !$ers->EOF ) {
 397              while ($user_obj = rs_fetch_next_record($ers)) {
 398                  $user_obj = (object)array_change_key_case((array)$user_obj , CASE_LOWER);
 399                  $roleid     = $user_obj->roleid;
 400                  $user       = $user_obj->userid;
 401                  $contextid  = $user_obj->contextid;
 402                  if (role_unassign($roleid, $user, 0, $contextid)){
 403                      error_log( "Unassigned role {$roleid} from user $user in context $contextid");
 404                  } else {
 405                      error_log( "Failed unassign role {$roleid} from user $user in context $contextid");
 406                  }
 407              }
 408              rs_close($ers); // release the handle
 409          }
 410      }
 411  
 412      commit_sql();
 413  
 414      // we are done now, a bit of housekeeping
 415      fix_course_sortorder();
 416  
 417      $this->enrol_disconnect($enroldb);
 418      return true;
 419  }
 420  
 421  /// Overide the get_access_icons() function
 422  function get_access_icons($course) {
 423  }
 424  
 425  
 426  /// Overide the base config_form() function
 427  function config_form($frm) {
 428      global $CFG;
 429  
 430      $vars = array('enrol_dbhost', 'enrol_dbuser', 'enrol_dbpass',
 431                    'enrol_dbname', 'enrol_dbtable',
 432                    'enrol_localcoursefield', 'enrol_localuserfield',
 433                    'enrol_remotecoursefield', 'enrol_remoteuserfield',
 434                    'enrol_db_autocreate', 'enrol_db_category', 'enrol_db_template',
 435                    'enrol_db_localrolefield', 'enrol_db_remoterolefield',
 436                    'enrol_remotecoursefield', 'enrol_remoteuserfield',
 437                    'enrol_db_ignorehiddencourse', 'enrol_db_defaultcourseroleid',
 438                    'enrol_db_disableunenrol');
 439  
 440      foreach ($vars as $var) {
 441          if (!isset($frm->$var)) {
 442              $frm->$var = '';
 443          }
 444      }
 445      include("$CFG->dirroot/enrol/database/config.html");
 446  }
 447  
 448  /// Override the base process_config() function
 449  function process_config($config) {
 450  
 451      if (!isset($config->enrol_dbtype)) {
 452          $config->enrol_dbtype = 'mysql';
 453      }
 454      set_config('enrol_dbtype', $config->enrol_dbtype);
 455  
 456      if (!isset($config->enrol_dbhost)) {
 457          $config->enrol_dbhost = '';
 458      }
 459      set_config('enrol_dbhost', $config->enrol_dbhost);
 460  
 461      if (!isset($config->enrol_dbuser)) {
 462          $config->enrol_dbuser = '';
 463      }
 464      set_config('enrol_dbuser', $config->enrol_dbuser);
 465  
 466      if (!isset($config->enrol_dbpass)) {
 467          $config->enrol_dbpass = '';
 468      }
 469      set_config('enrol_dbpass', $config->enrol_dbpass);
 470  
 471      if (!isset($config->enrol_dbname)) {
 472          $config->enrol_dbname = '';
 473      }
 474      set_config('enrol_dbname', $config->enrol_dbname);
 475  
 476      if (!isset($config->enrol_dbtable)) {
 477          $config->enrol_dbtable = '';
 478      }
 479      set_config('enrol_dbtable', $config->enrol_dbtable);
 480  
 481      if (!isset($config->enrol_localcoursefield)) {
 482          $config->enrol_localcoursefield = '';
 483      }
 484      set_config('enrol_localcoursefield', $config->enrol_localcoursefield);
 485  
 486      if (!isset($config->enrol_localuserfield)) {
 487          $config->enrol_localuserfield = '';
 488      }
 489      set_config('enrol_localuserfield', $config->enrol_localuserfield);
 490  
 491      if (!isset($config->enrol_remotecoursefield)) {
 492          $config->enrol_remotecoursefield = '';
 493      }
 494      set_config('enrol_remotecoursefield', $config->enrol_remotecoursefield);
 495  
 496      if (!isset($config->enrol_remoteuserfield)) {
 497          $config->enrol_remoteuserfield = '';
 498      }
 499      set_config('enrol_remoteuserfield', $config->enrol_remoteuserfield);
 500  
 501      if (!isset($config->enrol_db_autocreate)) {
 502          $config->enrol_db_autocreate = '';
 503      }
 504      set_config('enrol_db_autocreate', $config->enrol_db_autocreate);
 505  
 506      if (!isset($config->enrol_db_category)) {
 507          $config->enrol_db_category = '';
 508      }
 509      set_config('enrol_db_category', $config->enrol_db_category);
 510  
 511      if (!isset($config->enrol_db_template)) {
 512          $config->enrol_db_template = '';
 513      }
 514      set_config('enrol_db_template', $config->enrol_db_template);
 515  
 516      if (!isset($config->enrol_db_defaultcourseroleid)) {
 517          $config->enrol_db_defaultcourseroleid = '';
 518      }
 519      set_config('enrol_db_defaultcourseroleid', $config->enrol_db_defaultcourseroleid);
 520  
 521      if (!isset($config->enrol_db_localrolefield)) {
 522          $config->enrol_db_localrolefield = '';
 523      }
 524      set_config('enrol_db_localrolefield', $config->enrol_db_localrolefield);
 525  
 526      if (!isset($config->enrol_db_remoterolefield)) {
 527          $config->enrol_db_remoterolefield = '';
 528      }
 529      set_config('enrol_db_remoterolefield', $config->enrol_db_remoterolefield);
 530  
 531      if (!isset($config->enrol_db_ignorehiddencourse)) {
 532          $config->enrol_db_ignorehiddencourse = '';
 533      }
 534      set_config('enrol_db_ignorehiddencourse', $config->enrol_db_ignorehiddencourse );
 535  
 536      if (!isset($config->enrol_db_disableunenrol)) {
 537          $config->enrol_db_disableunenrol = '';
 538      }
 539      set_config('enrol_db_disableunenrol', $config->enrol_db_disableunenrol );
 540  
 541      return true;
 542  }
 543  
 544  // will create the moodle course from the template
 545  // course_ext is an array as obtained from ldap -- flattened somewhat
 546  // NOTE: if you pass true for $skip_fix_course_sortorder
 547  // you will want to call fix_course_sortorder() after your are done
 548  // with course creation
 549  function create_course ($course,$skip_fix_course_sortorder=0){
 550      global $CFG;
 551  
 552      // define a template
 553      if(!empty($CFG->enrol_db_template)){
 554          $template = get_record("course", 'shortname', $CFG->enrol_db_template);
 555          $template = (array)$template;
 556      } else {
 557          $site = get_site();
 558          $template = array(
 559                            'startdate'      => time() + 3600 * 24,
 560                            'summary'        => get_string("defaultcoursesummary"),
 561                            'format'         => "weeks",
 562                            'password'       => "",
 563                            'guest'          => 0,
 564                            'numsections'    => 10,
 565                            'idnumber'       => '',
 566                            'cost'           => '',
 567                            'newsitems'      => 5,
 568                            'showgrades'     => 1,
 569                            'groupmode'      => 0,
 570                            'groupmodeforce' => 0,
 571                            'student'  => $site->student,
 572                            'students' => $site->students,
 573                            'teacher'  => $site->teacher,
 574                            'teachers' => $site->teachers,
 575                            );
 576      }
 577      // overlay template
 578      foreach (array_keys($template) AS $key) {
 579          if (empty($course->$key)) {
 580              $course->$key = $template[$key];
 581          }
 582      }
 583  
 584      $course->category = 1;     // the misc 'catch-all' category
 585      if (!empty($CFG->enrol_db_category)){ //category = 0 or undef will break moodle
 586          $course->category = $CFG->enrol_db_category;
 587      }
 588  
 589      // define the sortorder
 590      $sort = get_field_sql('SELECT COALESCE(MAX(sortorder)+1, 100) AS max ' .
 591                            ' FROM ' . $CFG->prefix . 'course ' .
 592                            ' WHERE category=' . $course->category);
 593      $course->sortorder = $sort;
 594  
 595      // override with local data
 596      $course->startdate   = time() + 3600 * 24;
 597      $course->timecreated = time();
 598      $course->visible     = 1;
 599  
 600      // clear out id just in case
 601      unset($course->id);
 602  
 603      // truncate a few key fields
 604      $course->idnumber  = substr($course->idnumber, 0, 100);
 605      $course->shortname = substr($course->shortname, 0, 100);
 606  
 607      // store it and log
 608      if ($newcourseid = insert_record("course", addslashes_object($course))) {  // Set up new course
 609          $section = NULL;
 610          $section->course = $newcourseid;   // Create a default section.
 611          $section->section = 0;
 612          $section->id = insert_record("course_sections", $section);
 613          $page = page_create_object(PAGE_COURSE_VIEW, $newcourseid);
 614          blocks_repopulate_page($page); // Return value no
 615  
 616  
 617          if(!$skip_fix_course_sortorder){
 618              fix_course_sortorder();
 619          }
 620          add_to_log($newcourseid, "course", "new", "view.php?id=$newcourseid", "enrol/database auto-creation");
 621      } else {
 622          trigger_error("Could not create new course $extcourse from  from database");
 623          notify("Serious Error! Could not create the new course!");
 624          return false;
 625      }
 626  
 627      return $newcourseid;
 628  }
 629  
 630  /// DB Connect
 631  /// NOTE: You MUST remember to disconnect
 632  /// when you stop using it -- as this call will
 633  /// sometimes modify $CFG->prefix for the whole of Moodle!
 634  function enrol_connect() {
 635      global $CFG;
 636  
 637      // Try to connect to the external database (forcing new connection)
 638      $enroldb = &ADONewConnection($CFG->enrol_dbtype);
 639      if ($enroldb->Connect($CFG->enrol_dbhost, $CFG->enrol_dbuser, $CFG->enrol_dbpass, $CFG->enrol_dbname, true)) {
 640          $enroldb->SetFetchMode(ADODB_FETCH_ASSOC); ///Set Assoc mode always after DB connection
 641          return $enroldb;
 642      } else {
 643          trigger_error("Error connecting to enrolment DB backend with: "
 644                        . "$CFG->enrol_dbhost,$CFG->enrol_dbuser,$CFG->enrol_dbpass,$CFG->enrol_dbname");
 645          return false;
 646      }
 647  }
 648  
 649  /// DB Disconnect
 650  function enrol_disconnect($enroldb) {
 651      global $CFG;
 652  
 653      $enroldb->Close();
 654  }
 655  
 656  /**
 657   * This function returns the name and value of the role field to query the db
 658   * for, or null if there isn't one.
 659   *
 660   * @param object The ADOdb connection
 661   * @param object The role
 662   * @return array (boolean, string, db quoted string)
 663   */
 664  function role_fields($enroldb, $role) {
 665      global $CFG;
 666  
 667      if ($have_role = !empty($role)
 668       && !empty($CFG->enrol_db_remoterolefield)
 669       && !empty($CFG->enrol_db_localrolefield)
 670       && !empty($role->{$CFG->enrol_db_localrolefield})) {
 671          $remote_role_name = $CFG->enrol_db_remoterolefield;
 672          $remote_role_value = $enroldb->quote($role->{$CFG->enrol_db_localrolefield});
 673      } else {
 674          $remote_role_name = $remote_role_value = null;
 675      }
 676  
 677      return array($have_role, $remote_role_name, $remote_role_value);
 678  }
 679  
 680  } // end of class
 681  
 682  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7