Dokeos PHP Cross Reference Learning Management Systems

Source: /main/mySpace/user_add.php - 420 lines - 18784 bytes - Summary - Text - Print

   1  <?php // $Id: user_add.php 15105 2008-04-25 08:38:20Z elixir_inter $
   2  /*
   3  ==============================================================================
   4      Dokeos - elearning and course management software
   5  
   6      Copyright (c) 2008 Dokeos SPRL
   7      Copyright (c) 2009 Julio Montoya Armas <gugli100@gmail.com>
   8  
   9      For a full list of contributors, see "credits.txt".
  10      The full license can be read in "license.txt".
  11  
  12      This program is free software; you can redistribute it and/or
  13      modify it under the terms of the GNU General Public License
  14      as published by the Free Software Foundation; either version 2
  15      of the License, or (at your option) any later version.
  16  
  17      See the GNU General Public License for more details.
  18  
  19      Contact: Dokeos, rue du Corbeau, 108, B-1000 Brussels, Belgium, info@dokeos.com
  20  ==============================================================================
  21  */
  22  
  23  // name of the language file that needs to be included
  24  $language_file = array('admin', 'registration');
  25  $cidReset = true;
  26  
  27  // including necessary libraries
  28  require  '../inc/global.inc.php';
  29  $libpath = api_get_path(LIBRARY_PATH);
  30  require_once $libpath.'fileManage.lib.php';
  31  require_once $libpath.'fileUpload.lib.php';
  32  require_once $libpath.'usermanager.lib.php';
  33  require_once $libpath.'formvalidator/FormValidator.class.php';
  34  require_once $libpath.'image.lib.php';
  35  require_once $libpath.'mail.lib.inc.php';
  36  
  37  // user permissions
  38  api_protect_admin_script(true);
  39  api_block_anonymous_users();
  40  
  41  // Database table definitions
  42  $table_admin     = Database :: get_main_table(TABLE_MAIN_ADMIN);
  43  $table_user     = Database :: get_main_table(TABLE_MAIN_USER);
  44  $database         = Database::get_main_database();
  45  
  46  $htmlHeadXtra[] = '
  47  <script type="text/javascript">
  48  <!--
  49  function enable_expiration_date() { //v2.0
  50      document.user_add.radio_expiration_date[0].checked=false;
  51      document.user_add.radio_expiration_date[1].checked=true;
  52  }
  53  
  54  function password_switch_radio_button(form, input){
  55      var NodeList = document.getElementsByTagName("input");
  56      for(var i=0; i< NodeList.length; i++){
  57          if(NodeList.item(i).name=="password[password_auto]" && NodeList.item(i).value=="0"){
  58              NodeList.item(i).checked=true;
  59          }
  60      }
  61  }
  62  
  63  function display_drh_list(){
  64      if(document.getElementById("status_select").value=='.STUDENT.')
  65      {
  66          document.getElementById("drh_list").style.display="block";
  67      }
  68      else
  69      {
  70          document.getElementById("drh_list").style.display="none";
  71          document.getElementById("drh_select").options[0].selected="selected";
  72      }
  73  }
  74  
  75  //-->
  76  </script>';
  77  
  78  if (!empty($_GET['message'])) {
  79      $message = urldecode($_GET['message']);
  80  }
  81  
  82  $id_session='';
  83  if (isset($_GET["id_session"]) && $_GET["id_session"] != "") {
  84       $id_session = Security::remove_XSS($_GET["id_session"]);
  85      //$interbreadcrumb[] = array ("url" => "session.php", "name" => get_lang('Sessions'));
  86      //$interbreadcrumb[] = array ("url" => "course.php?id_session=".$_GET["id_session"]."", "name" => get_lang('Cours'));
  87  }
  88  
  89  $interbreadcrumb[] = array ('url' => '../admin/index.php', 'name' => get_lang('PlatformAdmin'));
  90  
  91  $tool_name = get_lang('AddUsers');
  92  // Create the form
  93  $form = new FormValidator('user_add');
  94  if (api_is_western_name_order()) {
  95      // Firstname
  96      $form->addElement('text','firstname', get_lang('FirstName'));
  97      $form->applyFilter('firstname', 'html_filter');
  98      $form->applyFilter('firstname', 'trim');
  99      $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
 100      // Lastname
 101      $form->addElement('text','lastname', get_lang('LastName'));
 102      $form->applyFilter('lastname', 'html_filter');
 103      $form->applyFilter('lastname', 'trim');
 104      $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
 105  } else {
 106      // Lastname
 107      $form->addElement('text','lastname', get_lang('LastName'));
 108      $form->applyFilter('lastname', 'html_filter');
 109      $form->applyFilter('lastname', 'trim');
 110      $form->addRule('lastname', get_lang('ThisFieldIsRequired'), 'required');
 111      // Firstname
 112      $form->addElement('text','firstname', get_lang('FirstName'));
 113      $form->applyFilter('firstname', 'html_filter');
 114      $form->applyFilter('firstname', 'trim');
 115      $form->addRule('firstname', get_lang('ThisFieldIsRequired'), 'required');
 116  }
 117  // Official code
 118  $form->addElement('text', 'official_code', get_lang('OfficialCode'), array('size' => '40'));
 119  $form->applyFilter('official_code', 'html_filter');
 120  $form->applyFilter('official_code', 'trim');
 121  // Email
 122  $form->addElement('text', 'email', get_lang('Email'), array('size' => '40'));
 123  $form->addRule('email', get_lang('EmailWrong'), 'email');
 124  $form->addRule('email', get_lang('EmailWrong'), 'required');
 125  // Phone
 126  $form->addElement('text', 'phone', get_lang('PhoneNumber'));
 127  // Picture
 128  $form->addElement('file', 'picture', get_lang('AddPicture'));
 129  $allowed_picture_types = array ('jpg', 'jpeg', 'png', 'gif');
 130  $form->addRule('picture', get_lang('OnlyImagesAllowed').' ('.implode(',', $allowed_picture_types).')', 'filetype', $allowed_picture_types);
 131  // Username
 132  $form->addElement('text', 'username', get_lang('LoginName'), array('maxlength' => USERNAME_MAX_LENGTH));
 133  $form->addRule('username', get_lang('ThisFieldIsRequired'), 'required');
 134  $form->addRule('username', sprintf(get_lang('UsernameMaxXCharacters'), (string)USERNAME_MAX_LENGTH), 'maxlength', USERNAME_MAX_LENGTH);
 135  $form->addRule('username', get_lang('OnlyLettersAndNumbersAllowed'), 'username');
 136  $form->addRule('username', get_lang('UserTaken'), 'username_available', $user_data['username']);
 137  // Password
 138  $group = array();
 139  $auth_sources = 0; //make available wider as we need it in case of form reset (see below)
 140  if (count($extAuthSource) > 0) {
 141      $group[] =& HTML_QuickForm::createElement('radio', 'password_auto', null, get_lang('ExternalAuthentication').' ', 2);
 142      $auth_sources = array();
 143      foreach ($extAuthSource as $key => $info) {
 144          $auth_sources[$key] = $key;
 145      }
 146      $group[] =& HTML_QuickForm::createElement('select', 'auth_source', null, $auth_sources);
 147      $group[] =& HTML_QuickForm::createElement('static', '', '', '<br />');
 148  }
 149  $group[] =& HTML_QuickForm::createElement('radio', 'password_auto', get_lang('Password'), get_lang('AutoGeneratePassword').'<br />', 1);
 150  $group[] =& HTML_QuickForm::createElement('radio', 'password_auto', 'id="radio_user_password"', null, 0);
 151  $group[] =& HTML_QuickForm::createElement('password', 'password', null, array('onkeydown' => 'javascript: password_switch_radio_button(document.user_add,"password[password_auto]");'));
 152  $form->addGroup($group, 'password', get_lang('Password'), '');
 153  
 154  
 155  
 156  // Send email
 157  $group = array();
 158  $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('Yes'), 1);
 159  $group[] =& HTML_QuickForm::createElement('radio', 'send_mail',null,get_lang('No'), 0);
 160  $form->addGroup($group, 'mail', get_lang('SendMailToNewUser'), '&nbsp;');
 161  // Expiration Date
 162  $form->addElement('radio', 'radio_expiration_date', get_lang('ExpirationDate'), get_lang('NeverExpires'), 0);
 163  $group = array ();
 164  $group[] = & $form->createElement('radio', 'radio_expiration_date', null, get_lang('On'), 1);
 165  $group[] = & $form->createElement('datepicker', 'expiration_date', null, array('form_name' => $form->getAttribute('name'), 'onchange' => 'javascript: enable_expiration_date();'));
 166  $form->addGroup($group, 'max_member_group', null, '', false);
 167  // Active account or inactive account
 168  $form->addElement('radio', 'active', get_lang('ActiveAccount'), get_lang('Active'), 1);
 169  $form->addElement('radio', 'active', '', get_lang('Inactive'), 0);
 170  
 171  
 172  //session list
 173  if (api_is_session_admin()) {
 174      $where = 'WHERE session_admin_id='.intval(api_get_user_id());
 175      $where .= ' AND ( (session.date_start <= CURDATE() AND session.date_end >= CURDATE()) OR session.date_start="0000-00-00" ) ';
 176      $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
 177      $sql="SELECT id,name,nbr_courses,date_start,date_end FROM $tbl_session $where ORDER BY name";
 178      $result = Database::query($sql,__FILE__,__LINE__);
 179      $a_sessions = Database::store_result($result);
 180      $session_list = array();
 181      $session_list[0] = get_lang('SelectSession');
 182      if (is_array($a_sessions)) {
 183          foreach ($a_sessions as $session) {
 184              $session_list[$session['id']]=$session['name'];
 185          }
 186      }
 187  
 188      //asort($session_list);
 189      //api_asort($session_list, SORT_STRING);
 190      api_natsort($session_list);
 191  
 192      $form->addElement('select', 'session_id', get_lang('Session'), $session_list);
 193  }
 194  
 195  
 196  // EXTRA FIELDS
 197  $extra = UserManager::get_extra_fields(0, 50, 5, 'ASC');
 198  foreach ($extra as $id => $field_details) {
 199      switch ($field_details[2]) {
 200          case USER_FIELD_TYPE_TEXT:
 201              $form->addElement('text', 'extra_'.$field_details[1], $field_details[3], array('size' => 40));
 202              $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
 203              $form->applyFilter('extra_'.$field_details[1], 'trim');
 204              break;
 205          case USER_FIELD_TYPE_TEXTAREA:
 206              $form->add_html_editor('extra_'.$field_details[1], $field_details[3], false, false, array('ToolbarSet' => 'Profile', 'Width' => '100%', 'Height' => '130'));
 207              //$form->addElement('textarea', 'extra_'.$field_details[1], $field_details[3], array('size' => 80));
 208              $form->applyFilter('extra_'.$field_details[1], 'stripslashes');
 209              $form->applyFilter('extra_'.$field_details[1], 'trim');
 210              break;
 211          case USER_FIELD_TYPE_RADIO:
 212              $group = array();
 213              foreach ($field_details[9] as $option_id => $option_details) {
 214                  $options[$option_details[1]] = $option_details[2];
 215                  $group[] =& HTML_QuickForm::createElement('radio', 'extra_'.$field_details[1], $option_details[1], $option_details[2].'<br />', $option_details[1]);
 216              }
 217              $form->addGroup($group, 'extra_'.$field_details[1], $field_details[3], '');
 218              break;
 219          case USER_FIELD_TYPE_SELECT:
 220              $options = array();
 221              foreach ($field_details[9] as $option_id => $option_details) {
 222                  $options[$option_details[1]] = $option_details[2];
 223              }
 224              $form->addElement('select', 'extra_'.$field_details[1], $field_details[3], $options, '');
 225              break;
 226          case USER_FIELD_TYPE_SELECT_MULTIPLE:
 227              $options = array();
 228              foreach ($field_details[9] as $option_id => $option_details) {
 229                  $options[$option_details[1]] = $option_details[2];
 230              }
 231              $form->addElement('select', 'extra_'.$field_details[1], $field_details[3], $options, array('multiple' => 'multiple'));
 232              break;
 233          case USER_FIELD_TYPE_DATE:
 234              $form->addElement('datepickerdate', 'extra_'.$field_details[1], $field_details[3]);
 235              $form->_elements[$form->_elementIndex['extra_'.$field_details[1]]]->setLocalOption('minYear', 1900);
 236              $defaults['extra_'.$field_details[1]] = date('Y-m-d 12:00:00');
 237              $form -> setDefaults($defaults);
 238              $form->applyFilter('theme', 'trim');
 239              break;
 240          case USER_FIELD_TYPE_DATETIME:
 241              $form->addElement('datepicker', 'extra_'.$field_details[1], $field_details[3]);
 242              $form->_elements[$form->_elementIndex['extra_'.$field_details[1]]]->setLocalOption('minYear', 1900);
 243              $defaults['extra_'.$field_details[1]] = date('Y-m-d 12:00:00');
 244              $form -> setDefaults($defaults);
 245              $form->applyFilter('theme', 'trim');
 246              break;
 247      }
 248  }
 249  
 250  
 251  // Set default values
 252  $defaults['admin']['platform_admin'] = 0;
 253  $defaults['mail']['send_mail'] = 0;
 254  $defaults['password']['password_auto'] = 1;
 255  $defaults['active'] = 1;
 256  $defaults['expiration_date'] = array();
 257  $days = api_get_setting('account_valid_duration');
 258  $time = strtotime('+'.$days.' day');
 259  $defaults['expiration_date']['d'] = date('d', $time);
 260  $defaults['expiration_date']['F'] = date('m', $time);
 261  $defaults['expiration_date']['Y'] = date('Y', $time);
 262  $defaults['radio_expiration_date'] = 0;
 263  $defaults['status'] = STUDENT;
 264  $defaults['session_id'] = api_get_session_id();
 265  
 266  
 267  $form->setDefaults($defaults);
 268  // Submit button
 269  $select_level = array ();
 270  $html_results_enabled[] = FormValidator :: createElement ('style_submit_button', 'submit_plus', get_lang('Add').'+', 'class="add"');
 271  $html_results_enabled[] = FormValidator :: createElement ('style_submit_button', 'submit', get_lang('Add'), 'class="add"');
 272  $form->addGroup($html_results_enabled);
 273  // Validate form
 274  if ($form->validate()) {
 275      $check = Security::check_token('post');
 276      if ($check) {
 277          $user = $form->exportValues();
 278          $picture_element = & $form->getElement('picture');
 279          $picture = $picture_element->getValue();
 280          $picture_uri = '';
 281          if (strlen($picture['name']) > 0) {
 282              if (!is_dir(api_get_path(SYS_CODE_PATH).'upload/users/')) {
 283                  if (mkdir(api_get_path(SYS_CODE_PATH).'upload/users/')) {
 284                      $perm = api_get_setting('permissions_for_new_directories');
 285                      $perm = octdec(!empty($perm) ? $perm : '0770');
 286                      chmod(api_get_path(SYS_CODE_PATH).'upload/users/');
 287                  }
 288              }
 289              $picture_uri = uniqid('').'_'.replace_dangerous_char($picture['name']);
 290              $picture_location = api_get_path(SYS_CODE_PATH).'upload/users/'.$picture_uri;
 291              move_uploaded_file($picture['tmp_name'], $picture_location);
 292          }
 293          $lastname = $user['lastname'];
 294          $firstname = $user['firstname'];
 295          $official_code = $user['official_code'];
 296          $email = $user['email'];
 297          $phone = $user['phone'];
 298          $username = $user['username'];
 299          $status = intval($user['status']);
 300          $picture = $_FILES['picture'];
 301          $platform_admin = intval($user['admin']['platform_admin']);
 302          $send_mail = intval($user['mail']['send_mail']);
 303          $hr_dept_id = intval($user['hr_dept_id']);
 304          if (count($extAuthSource) > 0 && $user['password']['password_auto'] == '2') {
 305              $auth_source = $user['password']['auth_source'];
 306              $password = 'PLACEHOLDER';
 307          } else {
 308              $auth_source = PLATFORM_AUTH_SOURCE;
 309              $password = $user['password']['password_auto'] == '1' ? api_generate_password() : $user['password']['password'];
 310          }
 311          if ($user['radio_expiration_date'] == '1') {
 312              $expiration_date = $user['expiration_date'];
 313          } else {
 314              $expiration_date = '0000-00-00 00:00:00';
 315          }
 316          $active = intval($user['active']);
 317          // default status = student
 318          $status = 5;
 319          //create user
 320          $user_id = UserManager::create_user($firstname, $lastname, $status, $email, $username, $password, $official_code, api_get_setting('platformLanguage'), $phone, $picture_uri, $auth_source, $expiration_date, $active, $hr_dept_id);
 321  
 322          //adding to the session
 323          if (api_is_session_admin()) {
 324              $tbl_session_rel_course                = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
 325              $tbl_session_rel_course_rel_user    = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
 326              $tbl_session                        = Database::get_main_table(TABLE_MAIN_SESSION);
 327              $tbl_session_rel_user                = Database::get_main_table(TABLE_MAIN_SESSION_USER);
 328  
 329              $id_session = $user['session_id'];
 330              if ($id_session != 0) {
 331                  $result = Database::query("SELECT course_code FROM $tbl_session_rel_course WHERE id_session='$id_session'",__FILE__,__LINE__);
 332  
 333                  $CourseList=array();
 334                  while ($row = Database::fetch_array($result)) {
 335                      $CourseList[] = $row['course_code'];
 336                  }
 337  
 338                  foreach ($CourseList as $enreg_course) {
 339                      Database::query("INSERT INTO $tbl_session_rel_course_rel_user(id_session,course_code,id_user) VALUES('$id_session','$enreg_course','$user_id')",__FILE__,__LINE__);
 340                      // updating the total
 341                      $sql = "SELECT COUNT(id_user) as nbUsers FROM $tbl_session_rel_course_rel_user WHERE id_session='$id_session' AND course_code='$enreg_course'";
 342                      $rs = Database::query($sql, __FILE__, __LINE__);
 343                      list($nbr_users) = Database::fetch_array($rs);
 344                      Database::query("UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'",__FILE__,__LINE__);
 345                  }
 346  
 347                  Database::query("INSERT INTO $tbl_session_rel_user(id_session, id_user) VALUES('$id_session','$user_id')", __FILE__, __LINE__);
 348  
 349                  $sql = "SELECT COUNT(nbr_users) as nbUsers FROM $tbl_session WHERE id='$id_session' ";
 350                  $rs = Database::query($sql, __FILE__, __LINE__);
 351                  list($nbr_users) = Database::fetch_array($rs);
 352  
 353                  Database::query("UPDATE $tbl_session SET nbr_users= $nbr_users WHERE id='$id_session' ", __FILE__, __LINE__);
 354              }
 355          }
 356  
 357  
 358          $extras = array();
 359          foreach ($user as $key => $value) {
 360              if (substr($key, 0, 6) == 'extra_') {
 361                  //an extra field
 362                  $myres = UserManager::update_extra_field_value($user_id, substr($key, 6), $value);
 363              }
 364          }
 365  
 366          if ($platform_admin) {
 367              $sql = "INSERT INTO $table_admin SET user_id = '".$user_id."'";
 368              Database::query($sql, __FILE__, __LINE__);
 369          }
 370          if (!empty ($email) && $send_mail) {
 371              $emailto = '"'.api_get_person_name($firstname, $lastname, null, PERSON_NAME_EMAIL_ADDRESS).'" <'.$email.'>';
 372              $emailsubject = '['.api_get_setting('siteName').'] '.get_lang('YourReg').' '.api_get_setting('siteName');
 373              $emailheaders = 'From: '.api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'), null, PERSON_NAME_EMAIL_ADDRESS).' <'.api_get_setting('emailAdministrator').">\n";
 374              $emailheaders .= 'Reply-To: '.api_get_setting('emailAdministrator');
 375  
 376              $portal_url = $_configuration['root_web'];
 377              if ($_configuration['multiple_access_urls']==true) {
 378                  $access_url_id = api_get_current_access_url_id();
 379                  if ($access_url_id != -1) {
 380                      $url = api_get_access_url($access_url_id);
 381                      $portal_url = $url['url'];
 382                  }
 383              }
 384              $emailbody=get_lang('Dear')." ".stripslashes(api_get_person_name($firstname, $lastname)).",\n\n".get_lang('YouAreReg')." ". api_get_setting('siteName') ." ".get_lang('WithTheFollowingSettings')."\n\n".get_lang('Username')." : ". $username ."\n". get_lang('Pass')." : ".stripslashes($password)."\n\n" .get_lang('Address') ." ". api_get_setting('siteName') ." ". get_lang('Is') ." : ".$portal_url."\n\n". get_lang('Problem'). "\n\n". get_lang('Formula').",\n\n".api_get_person_name(api_get_setting('administratorName'), api_get_setting('administratorSurname'))."\n". get_lang('Manager'). " ".api_get_setting('siteName')."\nT. ".api_get_setting('administratorTelephone')."\n" .get_lang('Email') ." : ".api_get_setting('emailAdministrator');
 385              @api_send_mail($emailto, $emailsubject, $emailbody, $emailheaders);
 386          }
 387          Security::clear_token();
 388          if (isset($user['submit_plus'])) {
 389              //we want to add more. Prepare report message and redirect to the same page (to clean the form)
 390              $tok = Security::get_token();
 391              header('Location: user_add.php?message='.urlencode(get_lang('UserAdded')).'&sec_token='.$tok);
 392              exit ();
 393          } else {
 394              $tok = Security::get_token();
 395              header('Location: ../admin/user_list.php?action=show_message&message='.urlencode(get_lang('UserAdded')).'&sec_token='.$tok);
 396              exit ();
 397          }
 398      }
 399  } else {
 400      if (isset($_POST['submit'])) {
 401          Security::clear_token();
 402      }
 403      $token = Security::get_token();
 404      $form->addElement('hidden', 'sec_token');
 405      $form->setConstants(array('sec_token' => $token));
 406  }
 407  // Display form
 408  Display::display_header($tool_name);
 409  //api_display_tool_title($tool_name);
 410  if (!empty($message)) {
 411      Display::display_normal_message(stripslashes($message));
 412  }
 413  $form->display();
 414  
 415  /*
 416  ==============================================================================
 417          FOOTER
 418  ==============================================================================
 419  */
 420  Display::display_footer();

title

Description

title

Description

title

Description

title

title

Body