[ Index ]

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

title

Body

[close]

/admin/ -> uploaduser.php (source)

   1  <?php  // $Id: uploaduser.php,v 1.68.2.13 2008/09/26 09:10:20 scyrma Exp $
   2  
   3  /// Bulk user registration script from a comma separated file
   4  /// Returns list of users with their user ids
   5  
   6  require ('../config.php');
   7  require_once($CFG->libdir.'/adminlib.php');
   8  require_once($CFG->libdir.'/csvlib.class.php');
   9  require_once($CFG->dirroot.'/user/profile/lib.php');
  10  require_once ('uploaduser_form.php');
  11  
  12  $iid         = optional_param('iid', '', PARAM_INT);
  13  $previewrows = optional_param('previewrows', 10, PARAM_INT);
  14  $readcount   = optional_param('readcount', 0, PARAM_INT);
  15  
  16  define('UU_ADDNEW', 0);
  17  define('UU_ADDINC', 1);
  18  define('UU_ADD_UPDATE', 2);
  19  define('UU_UPDATE', 3);
  20  
  21  @set_time_limit(3600); // 1 hour should be enough
  22  @raise_memory_limit('256M');
  23  if (function_exists('apache_child_terminate')) {
  24      // if we are running from Apache, give httpd a hint that
  25      // it can recycle the process after it's done. Apache's
  26      // memory management is truly awful but we can help it.
  27      @apache_child_terminate();
  28  }
  29  
  30  admin_externalpage_setup('uploadusers');
  31  require_capability('moodle/site:uploadusers', get_context_instance(CONTEXT_SYSTEM));
  32  
  33  $textlib = textlib_get_instance();
  34  $systemcontext = get_context_instance(CONTEXT_SYSTEM);
  35  
  36  $struserrenamed             = get_string('userrenamed', 'admin');
  37  $strusernotrenamedexists    = get_string('usernotrenamedexists', 'error');
  38  $strusernotrenamedmissing   = get_string('usernotrenamedmissing', 'error');
  39  $strusernotrenamedoff       = get_string('usernotrenamedoff', 'error');
  40  $strusernotrenamedadmin     = get_string('usernotrenamedadmin', 'error');
  41  
  42  $struserupdated             = get_string('useraccountupdated', 'admin');
  43  $strusernotupdated          = get_string('usernotupdatederror', 'error');
  44  $strusernotupdatednotexists = get_string('usernotupdatednotexists', 'error');
  45  $strusernotupdatedadmin     = get_string('usernotupdatedadmin', 'error');
  46  
  47  $struseradded               = get_string('newuser');
  48  $strusernotadded            = get_string('usernotaddedregistered', 'error');
  49  $strusernotaddederror       = get_string('usernotaddederror', 'error');
  50  
  51  $struserdeleted             = get_string('userdeleted', 'admin');
  52  $strusernotdeletederror     = get_string('usernotdeletederror', 'error');
  53  $strusernotdeletedmissing   = get_string('usernotdeletedmissing', 'error');
  54  $strusernotdeletedoff       = get_string('usernotdeletedoff', 'error');
  55  $strusernotdeletedadmin     = get_string('usernotdeletedadmin', 'error');
  56  
  57  $strcannotassignrole        = get_string('cannotassignrole', 'error');
  58  $strduplicateusername       = get_string('duplicateusername', 'error');
  59  
  60  $struserauthunsupported     = get_string('userauthunsupported', 'error');
  61  $stremailduplicate          = get_string('useremailduplicate', 'error');;
  62  
  63  $errorstr                   = get_string('error');
  64  
  65  $returnurl = $CFG->wwwroot.'/'.$CFG->admin.'/uploaduser.php';
  66  $bulknurl  = $CFG->wwwroot.'/'.$CFG->admin.'/user/user_bulk.php';
  67  
  68  // array of all valid fields for validation
  69  $STD_FIELDS = array('id', 'firstname', 'lastname', 'username', 'email', 
  70          'city', 'country', 'lang', 'auth', 'timezone', 'mailformat', 
  71          'maildisplay', 'maildigest', 'htmleditor', 'ajax', 'autosubscribe', 
  72          'mnethostid', 'institution', 'department', 'idnumber', 'skype', 
  73          'msn', 'aim', 'yahoo', 'icq', 'phone1', 'phone2', 'address', 
  74          'url', 'description', 'oldusername', 'emailstop', 'deleted',  
  75          'password');
  76  
  77  $PRF_FIELDS = array();
  78  
  79  if ($prof_fields = get_records('user_info_field')) {
  80      foreach ($prof_fields as $prof_field) {
  81          $PRF_FIELDS[] = 'profile_field_'.$prof_field->shortname;
  82      }
  83      unset($prof_fields);
  84  }
  85  
  86  if (empty($iid)) {
  87      $mform = new admin_uploaduser_form1();
  88  
  89      if ($formdata = $mform->get_data()) {
  90          $iid = csv_import_reader::get_new_iid('uploaduser');
  91          $cir = new csv_import_reader($iid, 'uploaduser');
  92  
  93          $content = $mform->get_file_content('userfile');
  94  
  95          $readcount = $cir->load_csv_content($content, $formdata->encoding, $formdata->delimiter_name, 'validate_user_upload_columns');
  96          unset($content);
  97  
  98          if ($readcount === false) {
  99              error($cir->get_error(), $returnurl);
 100          } else if ($readcount == 0) {
 101              print_error('csvemptyfile', 'error', $returnurl);
 102          }
 103          // continue to form2
 104  
 105      } else {
 106          admin_externalpage_print_header();
 107          print_heading_with_help(get_string('uploadusers'), 'uploadusers2');
 108          $mform->display();
 109          admin_externalpage_print_footer();
 110          die;
 111      }
 112  } else {
 113      $cir = new csv_import_reader($iid, 'uploaduser');
 114  }
 115  
 116  if (!$columns = $cir->get_columns()) {
 117      error('Error reading temporary file', $returnurl);
 118  }
 119  $mform = new admin_uploaduser_form2(null, $columns);
 120  // get initial date from form1
 121  $mform->set_data(array('iid'=>$iid, 'previewrows'=>$previewrows, 'readcount'=>$readcount));
 122  
 123  // If a file has been uploaded, then process it
 124  if ($formdata = $mform->is_cancelled()) {
 125      $cir->cleanup(true);
 126      redirect($returnurl);
 127  
 128  } else if ($formdata = $mform->get_data(false)) { // no magic quotes here!!!
 129      // Print the header
 130      admin_externalpage_print_header();
 131      print_heading(get_string('uploadusersresult', 'admin'));
 132  
 133      $optype = $formdata->uutype;
 134  
 135      $createpasswords   = (!empty($formdata->uupasswordnew) and $optype != UU_UPDATE);
 136      $updatepasswords   = (!empty($formdata->uupasswordold)  and $optype != UU_ADDNEW and $optype != UU_ADDINC);
 137      $allowrenames      = (!empty($formdata->uuallowrenames) and $optype != UU_ADDNEW and $optype != UU_ADDINC);
 138      $allowdeletes      = (!empty($formdata->uuallowdeletes) and $optype != UU_ADDNEW and $optype != UU_ADDINC);
 139      $updatetype        = isset($formdata->uuupdatetype) ? $formdata->uuupdatetype : 0;
 140      $bulk              = $formdata->uubulk;
 141      $noemailduplicates = $formdata->uunoemailduplicates;
 142  
 143      // verification moved to two places: after upload and into form2
 144      $usersnew     = 0;
 145      $usersupdated = 0;
 146      $userserrors  = 0;
 147      $deletes      = 0;
 148      $deleteerrors = 0;
 149      $renames      = 0;
 150      $renameerrors = 0;
 151      $usersskipped = 0;
 152  
 153      $forcechangepassword = 0;
 154  
 155      // caches
 156      $ccache    = array(); // course cache - do not fetch all courses here, we  will not probably use them all anyway!
 157      $rolecache = array(); // roles lookup cache
 158  
 159      $allowedauths   = uu_allowed_auths();
 160      $allowedauths   = array_keys($allowedauths);
 161      $availableauths = get_list_of_plugins('auth');
 162  
 163      $allowedroles = uu_allowed_roles(true);
 164      foreach ($allowedroles as $rid=>$rname) {
 165          $rolecache[$rid] = new object();
 166          $rolecache[$rid]->id = $rid;
 167          $rolecache[$rid]->name = $rname;
 168          if (!is_numeric($rname)) { // only non-numeric shornames are supported!!!
 169              $rolecache[$rname] = new object();
 170              $rolecache[$rname]->id = $rid;
 171              $rolecache[$rname]->name = $rname;
 172          }
 173      }
 174      unset($allowedroles);
 175  
 176      // clear bilk selection
 177      if ($bulk) {
 178          $SESSION->bulk_users = array();
 179      }
 180  
 181      // init csv import helper
 182      $cir->init();
 183      $linenum = 1; //column header is first line
 184  
 185      // init upload progress tracker
 186      $upt = new uu_progress_tracker();
 187      $upt->init(); // start table
 188  
 189      while ($line = $cir->next()) {
 190          $upt->flush();
 191          $linenum++;
 192  
 193          $upt->track('line', $linenum);
 194  
 195          $user = new object();
 196          // by default, use the local mnet id (this may be changed in the file)
 197          $user->mnethostid = $CFG->mnet_localhost_id;
 198          // add fields to user object
 199          foreach ($line as $key => $value) {
 200              if ($value !== '') {
 201                  $key = $columns[$key];
 202                  // password is special field
 203                  if ($key == 'password') {
 204                      if ($value !== '') {
 205                          $user->password = hash_internal_user_password($value);
 206                          if (!empty($CFG->passwordpolicy) and !check_password_policy($value, $errmsg)) {
 207                              $forcechangepassword++;
 208                          }
 209                      }
 210                  } else {
 211                      $user->$key = $value;
 212                      if (in_array($key, $upt->columns)) {
 213                          $upt->track($key, $value);
 214                      }
 215                  }
 216              }
 217          }
 218  
 219          // get username, first/last name now - we need them in templates!!
 220          if ($optype == UU_UPDATE) {
 221              // when updating only username is required
 222              if (!isset($user->username)) {
 223                  $upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
 224                  $upt->track('username', $errorstr, 'error');
 225                  $userserrors++;
 226                  continue;
 227              }
 228  
 229          } else {
 230              $error = false;
 231              // when all other ops need firstname and lastname
 232              if (!isset($user->firstname) or $user->firstname === '') {
 233                  $upt->track('status', get_string('missingfield', 'error', 'firstname'), 'error');
 234                  $upt->track('firstname', $errorstr, 'error');
 235                  $error = true;
 236              }
 237              if (!isset($user->lastname) or $user->lastname === '') {
 238                  $upt->track('status', get_string('missingfield', 'error', 'lastname'), 'error');
 239                  $upt->track('lastname', $errorstr, 'error');
 240                  $error = true;
 241              }
 242              if ($error) {
 243                  $userserrors++;
 244                  continue;
 245              }
 246              // we require username too - we might use template for it though
 247              if (!isset($user->username)) {
 248                  if (!isset($formdata->username) or $formdata->username === '') {
 249                      $upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
 250                      $upt->track('username', $errorstr, 'error');
 251                      $userserrors++;
 252                      continue;
 253                  } else {
 254                      $user->username = process_template($formdata->username, $user);
 255                      $upt->track('username', $user->username);
 256                  }
 257              }
 258          }
 259  
 260          // normalize username
 261          $user->username = $textlib->strtolower($user->username);
 262          if (empty($CFG->extendedusernamechars)) {
 263              $user->username = eregi_replace('[^(-\.[:alnum:])]', '', $user->username);
 264          }
 265          if (empty($user->username)) {
 266              $upt->track('status', get_string('missingfield', 'error', 'username'), 'error');
 267              $upt->track('username', $errorstr, 'error');
 268              $userserrors++;
 269              continue;
 270          }
 271  
 272          if ($existinguser = get_record('user', 'username', addslashes($user->username), 'mnethostid', $user->mnethostid)) {
 273              $upt->track('id', $existinguser->id, 'normal', false);
 274          }
 275  
 276          // find out in username incrementing required
 277          if ($existinguser and $optype == UU_ADDINC) {
 278              $oldusername = $user->username;
 279              $user->username = increment_username($user->username, $user->mnethostid);
 280              $upt->track('username', '', 'normal', false); // clear previous
 281              $upt->track('username', $oldusername.'-->'.$user->username, 'info');
 282              $existinguser = false;
 283          }
 284  
 285          // add default values for remaining fields
 286          foreach ($STD_FIELDS as $field) {
 287              if (isset($user->$field)) {
 288                  continue;
 289              }
 290              // all validation moved to form2
 291              if (isset($formdata->$field)) {
 292                  // process templates
 293                  $user->$field = process_template($formdata->$field, $user);
 294              }
 295          }
 296          foreach ($PRF_FIELDS as $field) {
 297              if (isset($user->$field)) {
 298                  continue;
 299              }
 300              if (isset($formdata->$field)) {
 301                  // process templates
 302                  $user->$field = process_template($formdata->$field, $user);
 303              }
 304          }
 305  
 306          // delete user
 307          if (!empty($user->deleted)) {
 308              if (!$allowdeletes) {
 309                  $usersskipped++;
 310                  $upt->track('status', $strusernotdeletedoff, 'warning');
 311                  continue;
 312              }
 313              if ($existinguser) {
 314                  if (has_capability('moodle/site:doanything', $systemcontext, $existinguser->id)) {
 315                      $upt->track('status', $strusernotdeletedadmin, 'error');
 316                      $deleteerrors++;
 317                      continue;
 318                  }
 319                  if (delete_user($existinguser)) {
 320                      $upt->track('status', $struserdeleted);
 321                      $deletes++;
 322                  } else {
 323                      $upt->track('status', $strusernotdeletederror, 'error');
 324                      $deleteerrors++;
 325                  }
 326              } else {
 327                  $upt->track('status', $strusernotdeletedmissing, 'error');
 328                  $deleteerrors++;
 329              }
 330              continue;
 331          }
 332          // we do not need the deleted flag anymore
 333          unset($user->deleted);
 334  
 335          // renaming requested?
 336          if (!empty($user->oldusername) ) {
 337              $oldusername = $textlib->strtolower($user->oldusername);
 338              if (!$allowrenames) {
 339                  $usersskipped++;
 340                  $upt->track('status', $strusernotrenamedoff, 'warning');
 341                  continue;
 342              }
 343  
 344              if ($existinguser) {
 345                  $upt->track('status', $strusernotrenamedexists, 'error');
 346                  $renameerrors++;
 347                  continue;
 348              }
 349  
 350              if ($olduser = get_record('user', 'username', addslashes($oldusername), 'mnethostid', addslashes($user->mnethostid))) {
 351                  $upt->track('id', $olduser->id, 'normal', false);
 352                  if (has_capability('moodle/site:doanything', $systemcontext, $olduser->id)) {
 353                      $upt->track('status', $strusernotrenamedadmin, 'error');
 354                      $renameerrors++;
 355                      continue;
 356                  }
 357                  if (set_field('user', 'username', addslashes($user->username), 'id', $olduser->id)) {
 358                      $upt->track('username', '', 'normal', false); // clear previous
 359                      $upt->track('username', $oldusername.'-->'.$user->username, 'info');
 360                      $upt->track('status', $struserrenamed);
 361                      $renames++;
 362                  } else {
 363                      $upt->track('status', $strusernotrenamedexists, 'error');
 364                      $renameerrors++;
 365                      continue;
 366                  }
 367              } else {
 368                  $upt->track('status', $strusernotrenamedmissing, 'error');
 369                  $renameerrors++;
 370                  continue;
 371              }
 372              $existinguser = $olduser;
 373              $existinguser->username = $user->username;
 374          }
 375  
 376          // can we process with update or insert?
 377          $skip = false;
 378          switch ($optype) {
 379              case UU_ADDNEW:
 380                  if ($existinguser) {
 381                      $usersskipped++;
 382                      $upt->track('status', $strusernotadded, 'warning');
 383                      $skip = true;;
 384                  }
 385                  break;
 386  
 387              case UU_ADDINC:
 388                  if ($existinguser) {
 389                      //this should not happen!
 390                      $upt->track('status', $strusernotaddederror, 'error');
 391                      $userserrors++;
 392                      continue;
 393                  }
 394                  break;
 395  
 396              case UU_ADD_UPDATE:
 397                  break;
 398  
 399              case UU_UPDATE:
 400                  if (!$existinguser) {
 401                      $usersskipped++;
 402                      $upt->track('status', $strusernotupdatednotexists, 'warning');
 403                      $skip = true;
 404                  }
 405                  break;
 406          }
 407  
 408          if ($skip) {
 409              continue;
 410          }
 411  
 412          if ($existinguser) {
 413              $user->id = $existinguser->id;
 414  
 415              if (has_capability('moodle/site:doanything', $systemcontext, $user->id)) {
 416                  $upt->track('status', $strusernotupdatedadmin, 'error');
 417                  $userserrors++;
 418                  continue;
 419              }
 420  
 421              if (!$updatetype) {
 422                  // no updates of existing data at all
 423              } else {
 424                  $existinguser->timemodified = time();
 425                  //load existing profile data
 426                  profile_load_data($existinguser);
 427  
 428                  $allowed = array();
 429                  if ($updatetype == 1) {
 430                      $allowed = $columns;
 431                  } else if ($updatetype == 2 or $updatetype == 3) {
 432                      $allowed = array_merge($STD_FIELDS, $PRF_FIELDS);
 433                  }
 434                  foreach ($allowed as $column) {
 435                      if ($column == 'username') {
 436                          continue;
 437                      }
 438                      if ($column == 'password') {
 439                          if (!$updatepasswords or $updatetype == 3) {
 440                              continue;
 441                          } else if (!empty($user->password)) {
 442                              $upt->track('password', get_string('updated'));
 443                              if ($forcechangepassword) {
 444                                  set_user_preference('auth_forcepasswordchange', 1, $existinguser->id);
 445                              }
 446                          }
 447                      }
 448                      if ((array_key_exists($column, $existinguser) and array_key_exists($column, $user)) or in_array($column, $PRF_FIELDS)) {
 449                          if ($updatetype == 3 and $existinguser->$column !== '') {
 450                              //missing == non-empty only
 451                              continue;
 452                          }
 453                          if ($existinguser->$column !== $user->$column) {
 454                              if ($column == 'email') {
 455                                  if (record_exists('user', 'email', addslashes($user->email))) {
 456                                      if ($noemailduplicates) {
 457                                          $upt->track('email', $stremailduplicate, 'error');
 458                                          $upt->track('status', $strusernotupdated, 'error');
 459                                          $userserrors++;
 460                                          continue 2;
 461                                      } else {
 462                                          $upt->track('email', $stremailduplicate, 'warning');
 463                                      }
 464                                  }
 465                              }
 466                              if ($column != 'password' and in_array($column, $upt->columns)) {
 467                                  $upt->track($column, '', 'normal', false); // clear previous
 468                                  $upt->track($column, $existinguser->$column.'-->'.$user->$column, 'info');
 469                              }
 470                              $existinguser->$column = $user->$column;
 471                          }
 472                      }
 473                  }
 474  
 475                  // do not update record if new auth plguin does not exist!
 476                  if (!in_array($existinguser->auth, $availableauths)) {
 477                      $upt->track('auth', get_string('userautherror', 'error', $existinguser->auth), 'error');
 478                      $upt->track('status', $strusernotupdated, 'error');
 479                      $userserrors++;
 480                      continue;
 481                  } else if (!in_array($existinguser->auth, $allowedauths)) {
 482                      $upt->track('auth', $struserauthunsupported, 'warning');
 483                  }
 484  
 485                  if (update_record('user', addslashes_recursive($existinguser))) {
 486                      $upt->track('status', $struserupdated);
 487                      $usersupdated++;
 488                  } else {
 489                      $upt->track('status', $strusernotupdated, 'error');
 490                      $userserrors++;
 491                      continue;
 492                  }
 493                  // save custom profile fields data from csv file
 494                  profile_save_data(addslashes_recursive($existinguser));
 495              }
 496  
 497              if ($bulk == 2 or $bulk == 3) {
 498                  if (!in_array($user->id, $SESSION->bulk_users)) {
 499                      $SESSION->bulk_users[] = $user->id;
 500                  }
 501              }
 502  
 503          } else {
 504              // save the user to the database
 505              $user->confirmed = 1;
 506              $user->timemodified = time();
 507  
 508              if (!$createpasswords and empty($user->password)) {
 509                  $upt->track('password', get_string('missingfield', 'error', 'password'), 'error');
 510                  $upt->track('status', $strusernotaddederror, 'error');
 511                  $userserrors++;
 512                  continue;
 513              }
 514  
 515              // do not insert record if new auth plguin does not exist!
 516              if (isset($user->auth)) {
 517                  if (!in_array($user->auth, $availableauths)) {
 518                      $upt->track('auth', get_string('userautherror', 'error', $user->auth), 'error');
 519                      $upt->track('status', $strusernotaddederror, 'error');
 520                      $userserrors++;
 521                      continue;
 522                  } else if (!in_array($user->auth, $allowedauths)) {
 523                      $upt->track('auth', $struserauthunsupported, 'warning');
 524                  }
 525              }
 526  
 527              if (record_exists('user', 'email', addslashes($user->email))) {
 528                  if ($noemailduplicates) {
 529                      $upt->track('email', $stremailduplicate, 'error');
 530                      $upt->track('status', $strusernotaddederror, 'error');
 531                      $userserrors++;
 532                      continue;
 533                  } else {
 534                      $upt->track('email', $stremailduplicate, 'warning');
 535                  }
 536              }
 537  
 538              if ($user->id = insert_record('user', addslashes_recursive($user))) {
 539                  $info = ': ' . $user->username .' (ID = ' . $user->id . ')';
 540                  $upt->track('status', $struseradded);
 541                  $upt->track('id', $user->id, 'normal', false);
 542                  $usersnew++;
 543                  if ($createpasswords and empty($user->password)) {
 544                      // passwords will be created and sent out on cron
 545                      set_user_preference('create_password', 1, $user->id);
 546                      set_user_preference('auth_forcepasswordchange', 1, $user->id);
 547                      $upt->track('password', get_string('new'));
 548                  }
 549                  if ($forcechangepassword) {
 550                      set_user_preference('auth_forcepasswordchange', 1, $user->id);
 551                  }
 552              } else {
 553                  // Record not added -- possibly some other error
 554                  $upt->track('status', $strusernotaddederror, 'error');
 555                  $userserrors++;
 556                  continue;
 557              }
 558              // save custom profile fields data
 559              profile_save_data($user);
 560  
 561              // make sure user context exists
 562              get_context_instance(CONTEXT_USER, $user->id);
 563  
 564              if ($bulk == 1 or $bulk == 3) {
 565                  if (!in_array($user->id, $SESSION->bulk_users)) {
 566                      $SESSION->bulk_users[] = $user->id;
 567                  }
 568              }
 569          }
 570  
 571          // find course enrolments, groups and roles/types
 572          foreach ($columns as $column) {
 573              if (!preg_match('/^course\d+$/', $column)) {
 574                  continue;
 575              }
 576              $i = substr($column, 6);
 577  
 578              $shortname = $user->{'course'.$i};
 579              if (!array_key_exists($shortname, $ccache)) {
 580                  if (!$course = get_record('course', 'shortname', addslashes($shortname), '', '', '', '', 'id, shortname, defaultrole')) {
 581                      $upt->track('enrolments', get_string('unknowncourse', 'error', $shortname), 'error');
 582                      continue;
 583                  }
 584                  $ccache[$shortname] = $course;
 585                  $ccache[$shortname]->groups = null;
 586              }
 587              $courseid      = $ccache[$shortname]->id;
 588              $coursecontext = get_context_instance(CONTEXT_COURSE, $courseid);
 589  
 590              // find role
 591              $rid = false;
 592              if (!empty($user->{'role'.$i})) {
 593                  $addrole = $user->{'role'.$i};
 594                  if (array_key_exists($addrole, $rolecache)) {
 595                      $rid = $rolecache[$addrole]->id;
 596                  } else {
 597                      $upt->track('enrolments', get_string('unknownrole', 'error', $addrole), 'error');
 598                      continue;
 599                  }
 600  
 601              } else if (!empty($user->{'type'.$i})) {
 602                  // if no role, then find "old" enrolment type
 603                  $addtype = $user->{'type'.$i};
 604                  if ($addtype < 1 or $addtype > 3) {
 605                      $upt->track('enrolments', $strerror.': typeN = 1|2|3', 'error');
 606                      continue;
 607                  } else if ($addtype == 1 and empty($formdata->uulegacy1)) {
 608                      if (empty($ccache[$shortname]->defaultrole)) {
 609                          $rid = $CFG->defaultcourseroleid;
 610                      } else {
 611                          $rid = $ccache[$shortname]->defaultrole;
 612                      }
 613                  } else {
 614                      $rid = $formdata->{'uulegacy'.$addtype};
 615                  }
 616  
 617              } else {
 618                  // no role specified, use the default
 619                  if (empty($ccache[$shortname]->defaultrole)) {
 620                      $rid = $CFG->defaultcourseroleid;
 621                  } else {
 622                      $rid = $ccache[$shortname]->defaultrole;
 623                  }
 624              }
 625              if ($rid) {
 626                  $a = new object();
 627                  $a->course = $shortname;
 628                  $a->role   = $rolecache[$rid]->name;
 629                  if (role_assign($rid, $user->id, 0, $coursecontext->id)) {
 630                      $upt->track('enrolments', get_string('enrolledincourserole', '', $a));
 631                  } else {
 632                      $upt->track('enrolments', get_string('enrolledincoursenotrole', '', $a), 'error');
 633                  }
 634              }
 635  
 636              // find group to add to
 637              if (!empty($user->{'group'.$i})) {
 638                  // make sure user is enrolled into course before adding into groups
 639                  if (!has_capability('moodle/course:view', $coursecontext, $user->id, false)) {
 640                      $upt->track('enrolments', get_string('addedtogroupnotenrolled', '', $gname), 'error');
 641                      continue;
 642                  }
 643                  //build group cache
 644                  if (is_null($ccache[$shortname]->groups)) {
 645                      $ccache[$shortname]->groups = array();
 646                      if ($groups = get_groups($courseid)) {
 647                          foreach ($groups as $gid=>$group) {
 648                              $ccache[$shortname]->groups[$gid] = new object();
 649                              $ccache[$shortname]->groups[$gid]->id   = $gid;
 650                              $ccache[$shortname]->groups[$gid]->name = $group->name;
 651                              if (!is_numeric($group->name)) { // only non-numeric names are supported!!!
 652                              $ccache[$shortname]->groups[$group->name] = new object();
 653                              $ccache[$shortname]->groups[$group->name]->id   = $gid;
 654                              $ccache[$shortname]->groups[$group->name]->name = $group->name;
 655                              }
 656                          }
 657                      }
 658                  }
 659                  // group exists?
 660                  $addgroup = $user->{'group'.$i};
 661                  if (!array_key_exists($addgroup, $ccache[$shortname]->groups)) {
 662                      // if group doesn't exist,  create it
 663                      $newgroupdata = new object();
 664                      $newgroupdata->name = $addgroup;
 665                      $newgroupdata->courseid = $ccache[$shortname]->id;
 666                      if ($ccache[$shortname]->groups[$addgroup]->id = groups_create_group($newgroupdata)){
 667                          $ccache[$shortname]->groups[$addgroup]->name = $newgroupdata->name;
 668                      } else {
 669                          $upt->track('enrolments', get_string('unknowngroup', 'error', $addgroup), 'error');
 670                          continue;
 671                      }
 672                  }
 673                  $gid   = $ccache[$shortname]->groups[$addgroup]->id;
 674                  $gname = $ccache[$shortname]->groups[$addgroup]->name;
 675  
 676                  if (groups_add_member($gid, $user->id)) {
 677                      $upt->track('enrolments', get_string('addedtogroup', '', $gname));
 678                  } else {
 679                      $upt->track('enrolments', get_string('addedtogroupnot', '', $gname), 'error');
 680                      continue;
 681                  }
 682              }
 683          }
 684      }
 685      $upt->flush();
 686      $upt->close(); // close table
 687  
 688      $cir->close();
 689      $cir->cleanup(true);
 690  
 691      print_box_start('boxwidthnarrow boxaligncenter generalbox', 'uploadresults');
 692      echo '<p>';
 693      if ($optype != UU_UPDATE) {
 694          echo get_string('userscreated', 'admin').': '.$usersnew.'<br />';
 695      }
 696      if ($optype == UU_UPDATE or $optype == UU_ADD_UPDATE) {
 697          echo get_string('usersupdated', 'admin').': '.$usersupdated.'<br />';
 698      }
 699      if ($allowdeletes) {
 700          echo get_string('usersdeleted', 'admin').': '.$deletes.'<br />';
 701          echo get_string('deleteerrors', 'admin').': '.$deleteerrors.'<br />';
 702      }
 703      if ($allowrenames) {
 704          echo get_string('usersrenamed', 'admin').': '.$renames.'<br />';
 705          echo get_string('renameerrors', 'admin').': '.$renameerrors.'<br />';
 706      }
 707      if ($usersskipped) {
 708          echo get_string('usersskipped', 'admin').': '.$usersskipped.'<br />';
 709      }
 710      echo get_string('usersweakpassword', 'admin').': '.$forcechangepassword.'<br />';
 711      echo get_string('errors', 'admin').': '.$userserrors.'</p>';
 712      print_box_end();
 713  
 714      if ($bulk) {
 715          print_continue($bulknurl);
 716      } else {
 717          print_continue($returnurl);
 718      }
 719      admin_externalpage_print_footer();
 720      die;
 721  }
 722  
 723  // Print the header
 724  admin_externalpage_print_header();
 725  
 726  /// Print the form
 727  print_heading_with_help(get_string('uploaduserspreview', 'admin'), 'uploadusers2');
 728  
 729  $ci = 0;
 730  $ri = 0;
 731  
 732  echo '<table id="uupreview" class="generaltable boxaligncenter" summary="'.get_string('uploaduserspreview', 'admin').'">';
 733  echo '<tr class="heading r'.$ri++.'">';
 734  foreach ($columns as $col) {
 735      echo '<th class="header c'.$ci++.'" scope="col">'.s($col).'</th>';
 736  }
 737  echo '</tr>';
 738  
 739  $cir->init();
 740  while ($fields = $cir->next()) {
 741      if ($ri > $previewrows) {
 742          echo '<tr class="r'.$ri++.'">';
 743          foreach ($fields as $field) {
 744              echo '<td class="cell c'.$ci++.'">...</td>';;
 745          }
 746          break;
 747      }
 748      $ci = 0;
 749      echo '<tr class="r'.$ri++.'">';
 750      foreach ($fields as $field) {
 751          echo '<td class="cell c'.$ci++.'">'.s($field).'</td>';;
 752      }
 753      echo '</tr>';
 754  }
 755  $cir->close();
 756  
 757  echo '</table>';
 758  echo '<div class="centerpara">'.get_string('uupreprocessedcount', 'admin', $readcount).'</div>';
 759  $mform->display();
 760  admin_externalpage_print_footer();
 761  die;
 762  
 763  /////////////////////////////////////
 764  /// Utility functions and classes ///
 765  /////////////////////////////////////
 766  
 767  class uu_progress_tracker {
 768      var $_row;
 769      var $columns = array('status', 'line', 'id', 'username', 'firstname', 'lastname', 'email', 'password', 'auth', 'enrolments', 'deleted');
 770  
 771      function uu_progress_tracker() {
 772      }
 773  
 774      function init() {
 775          $ci = 0;
 776          echo '<table id="uuresults" class="generaltable boxaligncenter" summary="'.get_string('uploadusersresult', 'admin').'">';
 777          echo '<tr class="heading r0">';
 778          echo '<th class="header c'.$ci++.'" scope="col">'.get_string('status').'</th>';
 779          echo '<th class="header c'.$ci++.'" scope="col">'.get_string('uucsvline', 'admin').'</th>';
 780          echo '<th class="header c'.$ci++.'" scope="col">ID</th>';
 781          echo '<th class="header c'.$ci++.'" scope="col">'.get_string('username').'</th>';
 782          echo '<th class="header c'.$ci++.'" scope="col">'.get_string('firstname').'</th>';
 783          echo '<th class="header c'.$ci++.'" scope="col">'.get_string('lastname').'</th>';
 784          echo '<th class="header c'.$ci++.'" scope="col">'.get_string('email').'</th>';
 785          echo '<th class="header c'.$ci++.'" scope="col">'.get_string('password').'</th>';
 786          echo '<th class="header c'.$ci++.'" scope="col">'.get_string('authentication').'</th>';
 787          echo '<th class="header c'.$ci++.'" scope="col">'.get_string('enrolments').'</th>';
 788          echo '<th class="header c'.$ci++.'" scope="col">'.get_string('delete').'</th>';
 789          echo '</tr>';
 790          $this->_row = null;
 791      }
 792  
 793      function flush() {
 794          if (empty($this->_row) or empty($this->_row['line']['normal'])) {
 795              $this->_row = array();
 796              foreach ($this->columns as $col) {
 797                  $this->_row[$col] = array('normal'=>'', 'info'=>'', 'warning'=>'', 'error'=>'');
 798              }
 799              return;
 800          }
 801          $ci = 0;
 802          $ri = 1;
 803          echo '<tr class="r'.$ri++.'">';
 804          foreach ($this->_row as $field) {
 805              foreach ($field as $type=>$content) {
 806                  if ($field[$type] !== '') {
 807                      $field[$type] = '<span class="uu'.$type.'">'.$field[$type].'</span>';
 808                  } else {
 809                      unset($field[$type]);
 810                  }
 811              }
 812              echo '<td class="cell c'.$ci++.'">';
 813              if (!empty($field)) {
 814                  echo implode('<br />', $field);
 815              } else {
 816                  echo '&nbsp;';
 817              }
 818              echo '</td>';
 819          }
 820          echo '</tr>';
 821          foreach ($this->columns as $col) {
 822              $this->_row[$col] = array('normal'=>'', 'info'=>'', 'warning'=>'', 'error'=>'');
 823          }
 824      }
 825  
 826      function track($col, $msg, $level='normal', $merge=true) {
 827          if (empty($this->_row)) {
 828              $this->flush(); //init arrays
 829          }
 830          if (!in_array($col, $this->columns)) {
 831              debugging('Incorrect column:'.$col);
 832              return;
 833          }
 834          if ($merge) {
 835              if ($this->_row[$col][$level] != '') {
 836                  $this->_row[$col][$level] .='<br />';
 837              }
 838              $this->_row[$col][$level] .= s($msg);
 839          } else {
 840              $this->_row[$col][$level] = s($msg);
 841          }
 842      }
 843  
 844      function close() {
 845          echo '</table>';
 846      }
 847  }
 848  
 849  /**
 850   * Validation callback function - verified the column line of csv file.
 851   * Converts column names to lowercase too.
 852   */
 853  function validate_user_upload_columns(&$columns) {
 854      global $STD_FIELDS, $PRF_FIELDS;
 855  
 856      if (count($columns) < 2) {
 857          return get_string('csvfewcolumns', 'error');
 858      }
 859  
 860      // test columns
 861      $processed = array();
 862      foreach ($columns as $key=>$unused) {
 863          $columns[$key] = strtolower($columns[$key]); // no unicode expected here, ignore case
 864          $field = $columns[$key];
 865          if (!in_array($field, $STD_FIELDS) && !in_array($field, $PRF_FIELDS) &&// if not a standard field and not an enrolment field, then we have an error
 866              !preg_match('/^course\d+$/', $field) && !preg_match('/^group\d+$/', $field) &&
 867              !preg_match('/^type\d+$/', $field) && !preg_match('/^role\d+$/', $field)) {
 868              return get_string('invalidfieldname', 'error', $field);
 869          }
 870          if (in_array($field, $processed)) {
 871              return get_string('csvcolumnduplicates', 'error');
 872          }
 873          $processed[] = $field;
 874      }
 875      return true;
 876  }
 877  
 878  /**
 879   * Increments username - increments trailing number or adds it if not present.
 880   * Varifies that the new username does not exist yet
 881   * @param string $username
 882   * @return incremented username which does not exist yet
 883   */
 884  function increment_username($username, $mnethostid) {
 885      if (!preg_match_all('/(.*?)([0-9]+)$/', $username, $matches)) {
 886          $username = $username.'2';
 887      } else {
 888          $username = $matches[1][0].($matches[2][0]+1);
 889      }
 890  
 891      if (record_exists('user', 'username', addslashes($username), 'mnethostid', addslashes($mnethostid))) {
 892          return increment_username($username, $mnethostid);
 893      } else {
 894          return $username;
 895      }
 896  }
 897  
 898  /**
 899   * Check if default field contains templates and apply them.
 900   * @param string template - potential tempalte string
 901   * @param object user object- we need username, firstname and lastname
 902   * @return string field value
 903   */
 904  function process_template($template, $user) {
 905      if (strpos($template, '%') === false) {
 906          return $template;
 907      }
 908  
 909      // very very ugly hack!
 910      global $template_globals;
 911      $template_globals = new object();
 912      $template_globals->username  = isset($user->username)  ? $user->username  : '';
 913      $template_globals->firstname = isset($user->firstname) ? $user->firstname : '';
 914      $template_globals->lastname  = isset($user->lastname)  ? $user->lastname  : '';
 915  
 916      $result = preg_replace_callback('/(?<!%)%([+-~])?(\d)*([flu])/', 'process_template_callback', $template);
 917  
 918      $template_globals = null;
 919  
 920      if (is_null($result)) {
 921          return $template; //error during regex processing??
 922      } else {
 923          return $result;
 924      }
 925  }
 926  
 927  /**
 928   * Internal callback function.
 929   */
 930  function process_template_callback($block) {
 931      global $template_globals;
 932      $textlib = textlib_get_instance();
 933      $repl = $block[0];
 934  
 935      switch ($block[3]) {
 936          case 'u': $repl = $template_globals->username; break;
 937          case 'f': $repl = $template_globals->firstname; break;
 938          case 'l': $repl = $template_globals->lastname; break;
 939      }
 940      switch ($block[1]) {
 941          case '+': $repl = $textlib->strtoupper($repl); break;
 942          case '-': $repl = $textlib->strtolower($repl); break;
 943          case '~': $repl = $textlib->strtotitle($repl); break;
 944      }
 945      if (!empty($block[2])) {
 946          $repl = $textlib->substr($repl, 0 , $block[2]);
 947      }
 948  
 949      return $repl;
 950  }
 951  
 952  /**
 953   * Returns list of auth plugins that are enabled and known to work.
 954   */
 955  function uu_allowed_auths() {
 956      global $CFG;
 957  
 958      // only following plugins are guaranteed to work properly
 959      // TODO: add support for more plguins in 2.0
 960      $whitelist = array('manual', 'nologin', 'none', 'email');
 961      $plugins = get_enabled_auth_plugins();
 962      $choices = array();
 963      foreach ($plugins as $plugin) {
 964          $choices[$plugin] = get_string('auth_'.$plugin.'title', 'auth');
 965      }
 966  
 967      return $choices;
 968  }
 969  
 970  /**
 971   * Returns list of non administrator roles
 972   */
 973  function uu_allowed_roles($shortname=false) {
 974      global $CFG;
 975  
 976      $roles = get_all_roles();
 977      $choices = array();
 978      foreach($roles as $role) {
 979          if ($shortname) {
 980              $choices[$role->id] = $role->shortname;
 981          } else {
 982              $choices[$role->id] = format_string($role->name);
 983          }
 984      }
 985      // get rid of all admin roles
 986      if ($adminroles = get_roles_with_capability('moodle/site:doanything', CAP_ALLOW)) {
 987          foreach($adminroles as $adminrole) {
 988              unset($choices[$adminrole->id]);
 989          }
 990      }
 991  
 992      return $choices;
 993  }
 994  ?>


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