| [ Index ] |
|
Code source de Claroline 188 |
1 <?php // $Id: AddCSVusers.php,v 1.75.2.1 2007/09/28 14:49:33 mathieu Exp $ 2 /** 3 * CLAROLINE 4 * 5 * tool for bulk subscribe. 6 * 7 * @version 1.8 $Revision: 1.75.2.1 $ 8 * 9 * @copyright 2001-2007 Universite catholique de Louvain (UCL) 10 * 11 * @license http://www.gnu.org/copyleft/gpl.html (GPL) GENERAL PUBLIC LICENSE 12 * 13 * @package CLUSR 14 * 15 * @author Claro Team <cvs@claroline.net> 16 * @author Guillaume Lederer <guim@claroline.net> 17 * 18 */ 19 20 //used libraries 21 22 23 /** 24 * @todo TODO use is_coursemanager_allowed_to_import_user_list 25 $can_import_user_list = (bool) (claro_is_course_manager() 26 && get_conf('is_coursemanager_allowed_to_import_user_list') ) 27 || claro_is_platform_admin(); 28 */ 29 30 require '../inc/claro_init_global.inc.php'; 31 require_once get_path('incRepositorySys') . '/lib/admin.lib.inc.php'; 32 require_once get_path('incRepositorySys') . '/lib/user.lib.php'; 33 require_once get_path('incRepositorySys') . '/lib/class.lib.php'; 34 require_once get_path('incRepositorySys') . '/lib/course_user.lib.php' ; 35 require_once get_path('incRepositorySys') . '/lib/import_csv.lib.php'; 36 37 include claro_get_conf_repository() . 'user_profile.conf.php'; 38 39 40 /* 41 * See in which context of user we are and check WHO is using the tool,there are 3 possibilities : 42 * - adding CSV users by the admin tool (AddType=adminTool) 43 * - adding CSV users by the admin, but with the class tool (AddType=adminClassTool) 44 * - adding CSV users by the user tool in a course (in this case, available to teacher too) (AddType=userTool) 45 */ 46 47 $can_import_user_list = (bool) (claro_is_course_manager() 48 && get_conf('is_coursemanager_allowed_to_import_user_list') ) 49 || claro_is_platform_admin(); 50 51 52 if ( isset($_REQUEST['AddType']) ) $AddType = $_REQUEST['AddType']; 53 else $AddType = 'userTool'; // default access is the user tool 54 55 switch ($AddType) 56 { 57 case 'adminTool' : 58 case 'adminClassTool' : 59 if ( ! claro_is_user_authenticated() ) claro_disp_auth_form(); 60 if ( ! claro_is_platform_admin() ) claro_die(get_lang('Not allowed')); 61 break; 62 63 case 'userTool' : 64 default : 65 if ( ! claro_is_in_a_course() || ! claro_is_course_allowed() ) claro_disp_auth_form(true); 66 67 if ( ! $can_import_user_list ) claro_die(get_lang('Not allowed')); 68 $AddType = 'userTool' ; 69 break; 70 } 71 72 if ( isset($_REQUEST['class_id']) ) 73 { 74 $_SESSION['admin_user_class_id'] = $_REQUEST['class_id']; 75 } 76 77 /* 78 * DB tables definition 79 */ 80 81 $tbl_mdb_names = claro_sql_get_main_tbl(); 82 $tbl_user = $tbl_mdb_names['user']; 83 $tbl_class = $tbl_mdb_names['user_category']; 84 $tbl_class_user = $tbl_mdb_names['user_rel_profile_category']; 85 86 //declare temporary upload directory 87 88 $uploadTempDir = 'tmp/'; 89 90 //deal with session variables to know in which step we are really and avoid doing changes twice 91 92 if (isset($_REQUEST['cmd']) && (($_REQUEST['cmd'] == 'exImpSec' || $_REQUEST['cmd'] == 'exImp') && $_SESSION['claro_CSV_done']) || empty($_REQUEST['cmd'])) // this is to avoid a redo because of a page reload in browser 93 { 94 $cmd = ''; 95 $display = 'default'; 96 $_SESSION['claro_CSV_done'] = FALSE; 97 } 98 99 //Set format, fields separator and enclosion used for CSV files 100 101 $defaultFormat = 'firstname;lastname;email;phone;username;password;officialCode'; 102 103 if ( empty($_SESSION['claro_usedFormat']) ) 104 { 105 $_SESSION['claro_usedFormat'] = $defaultFormat; 106 } 107 108 if (isset($_REQUEST['loadDefault']) && ($_REQUEST['loadDefault'] =='yes')) 109 { 110 $usedFormat = $defaultFormat; 111 $_SESSION['claro_usedFormat'] = $defaultFormat; 112 $_SESSION['CSV_fieldSeparator'] = ';'; 113 $_SESSION['CSV_enclosedBy'] = ''; 114 $dialogBox = get_lang('Format changed'); 115 } 116 117 elseif (isset($_REQUEST['usedFormat'])) 118 { 119 //check if posted new format is OK 120 121 $field_correct = claro_CSV_format_ok($_REQUEST['usedFormat'], $_REQUEST['fieldSeparator'], $_REQUEST['enclosedBy']); 122 123 if (!$field_correct) 124 { 125 $dialogBox = get_lang('ERROR: The format you gave is not compatible with Claroline'); 126 } 127 else 128 { 129 $dialogBox = get_lang('Format changed'); 130 $_SESSION['claro_usedFormat'] = $_REQUEST['usedFormat']; 131 $_SESSION['CSV_fieldSeparator'] = $_REQUEST['fieldSeparator']; 132 $_SESSION['CSV_enclosedBy'] = $_REQUEST['enclosedBy']; 133 } 134 } 135 136 if (!isset($_SESSION['CSV_fieldSeparator'])) $_SESSION['CSV_fieldSeparator'] = ";"; 137 if (!isset($_SESSION['CSV_enclosedBy'])) $_SESSION['CSV_enclosedBy'] = "\""; 138 139 $usedFormat = $_SESSION['claro_usedFormat']; 140 141 /** 142 * Execute command section 143 */ 144 145 $cmd = isset($_REQUEST['cmd']) ? $_REQUEST['cmd'] : null ; 146 147 switch ($cmd) 148 { 149 150 //STEP ONE : FILE UPLOADED, CHECK FOR POTENTIAL ERRORS 151 152 case 'exImp' : 153 154 //see if format is defined in session or in file 155 156 if ($_REQUEST['firstLineFormat']=='YES') 157 { 158 $useFirstLine = true; 159 } 160 else 161 { 162 $fieldSeparator = $_REQUEST['fieldSeparator']; 163 $enclosedBy = $_REQUEST['enclosedBy']; 164 if ($_REQUEST['enclosedBy']=='dbquote') 165 { 166 $enclosedBy = '"'; 167 } 168 $useFirstLine = false; 169 } 170 171 //check if a file was actually posted and that the mimetype is good 172 173 $mimetypes = array(); //array used with supported mimetype for CSV files 174 $mimetypes[] = 'text/comma-separated-values'; 175 $mimetypes[] = 'text/csv'; 176 $mimetypes[] = 'text/plain'; 177 $mimetypes[] = 'application/csv'; 178 $mimetypes[] = 'application/excel'; 179 $mimetypes[] = 'application/vnd.ms-excel'; 180 $mimetypes[] = 'application/vnd.msexcel'; 181 $mimetypes[] = 'text/anytext'; 182 183 if ( $_FILES['CSVfile']['size'] == 0 ) 184 { 185 $display = 'default'; 186 $dialogBox = get_lang('You must select a file'); 187 } 188 elseif (!in_array($_FILES['CSVfile']['type'],$mimetypes) && (strpos($_FILES['CSVfile']['type'],'text')===FALSE) ) 189 { 190 $display = 'default'; 191 $dialogBox = get_lang('You must select a text file'); 192 } 193 else 194 { 195 //check file content to see potentiel problems to add the users in this campus (errors are saved in session) 196 197 claro_check_campus_CSV_File($uploadTempDir, $useFirstLine, $usedFormat, $_REQUEST['fieldSeparator'], $_REQUEST['enclosedBy']); 198 $display = 'stepone'; 199 200 } 201 202 break; 203 204 //STEP TWO : ADD CONFIRMED, USERS ARE ADDED 205 206 case 'exImpSec' : 207 208 //build 2D array with users who will be add, avoiding those with error(s). 209 210 $usersToAdd = array(); 211 212 for ($i=0, $size=sizeof($_SESSION['claro_csv_userlist']); $i<$size; $i++) 213 { 214 // user must be added only if we encountered exactly no error 215 216 if ( ! ( isset($_SESSION['claro_mail_synthax_error'][$i]) 217 || isset($_SESSION['claro_mail_used_error'][$i]) 218 || isset($_SESSION['claro_username_used_error'][$i]) 219 || isset($_SESSION['claro_officialcode_used_error'][$i]) 220 || isset($_SESSION['claro_password_error'][$i]) 221 || isset($_SESSION['claro_mail_duplicate_error'][$i]) 222 || isset($_SESSION['claro_username_duplicate_error'][$i]) 223 || isset($_SESSION['claro_officialcode_duplicate_error'][$i]) 224 ) 225 ) 226 { 227 $usersToAdd[] = $_SESSION['claro_csv_userlist'][$i]; 228 } 229 230 } 231 232 // perform subscriptions of users with 'no error' found. 233 234 foreach ($usersToAdd as $user) 235 { 236 237 //set empty fields if needed 238 239 if (empty($user['phone'])) $user['phone'] = ''; 240 if (empty($user['email'])) $user['email'] = ''; 241 if (empty($user['officialCode'])) $user['officialCode'] = ''; 242 243 $user_id = user_create($user); 244 245 // for each use case alos perform thze other needed action : 246 247 switch ($AddType) 248 { 249 case 'adminTool': 250 //its all done in this case 251 break; 252 253 case 'adminClassTool': 254 user_add_to_class($user_id, $_SESSION['admin_user_class_id']); 255 break; 256 257 case 'userTool': 258 user_add_to_course($user_id, claro_get_current_course_id(), false, false, false); 259 break; 260 } 261 } 262 263 264 // notify in session that action was done (to prevent double action if user uses back button of browser 265 266 $_SESSION['claro_CSV_done'] = TRUE; 267 268 // select display type 269 270 $display = 'steptwo'; 271 272 break; 273 274 } 275 276 /** 277 * Display section 278 * 279 * PREPARE DISPLAY 280 * 281 * Deal with interbredcrumps and title variable this depends 282 * on the use case of the CSV import(see addType) 283 * 284 */ 285 286 switch ($AddType) 287 { 288 case 'adminTool': 289 { 290 $noQUERY_STRING = true; 291 $nameTools = get_lang('Add a user list'); 292 $interbredcrump[] = array ('url'=>get_path('rootAdminWeb') . claro_url_relay_context('?') , 'name'=> get_lang('Administration')); 293 } break; 294 295 case 'adminClassTool' : 296 { 297 $noQUERY_STRING = true; 298 $nameTools = get_lang('Add a user list in class'); 299 $interbredcrump[] = array ('url'=>get_path('rootAdminWeb') . claro_url_relay_context('?') , 'name'=> get_lang('Administration')); 300 $interbredcrump[] = array ('url'=>get_path('rootAdminWeb').'admin_class.php' . claro_url_relay_context('?') , 'name'=> get_lang('Classes')); 301 $interbredcrump[] = array ('url'=>get_path('rootAdminWeb').'admin_class_user.php?class_id='. $_SESSION['admin_user_class_id'] . claro_url_relay_context('&') , 'name'=> get_lang('Class members')); 302 } break; 303 304 case 'userTool': 305 { 306 $noQUERY_STRING = true; 307 $nameTools = get_lang('Add a user list in course'); 308 $interbredcrump[] = array ('url'=>'user.php' . claro_url_relay_context('?') , 'name'=> get_lang('Users')); 309 } break; 310 } 311 312 313 //modify dialogbox if user asked form to change used format 314 315 if (isset($_REQUEST['chformat']) && $_REQUEST['chformat']=='yes') 316 { 317 if (!empty($_SESSION['CSV_enclosedBy']) && $_SESSION['CSV_enclosedBy']=='dbquote') $dbquote_selected = 'selected'; else $dbquote_selected = ''; 318 if (!empty($_SESSION['CSV_enclosedBy']) && $_SESSION['CSV_enclosedBy']=='') $blank_selected = 'selected'; else $blank_selected = ''; 319 if (!empty($_SESSION['CSV_enclosedBy']) && $_SESSION['CSV_enclosedBy']==',') $coma_selected = 'selected'; else $coma_selected = ''; 320 if (!empty($_SESSION['CSV_enclosedBy']) && $_SESSION['CSV_enclosedBy']=='.') $dot_selected = 'selected'; else $dot_selected = ''; 321 322 if (!empty($_SESSION['CSV_fieldSeparator']) && $_SESSION['CSV_fieldSeparator']==';') $dot_coma_selected_sep = 'selected'; else $dot_coma_selected_sep = ''; 323 if (!empty($_SESSION['CSV_fieldSeparator']) && $_SESSION['CSV_fieldSeparator']==',') $coma_selected_sep = 'selected'; else $coma_selected_sep = ''; 324 if (!empty($_SESSION['CSV_fieldSeparator']) && $_SESSION['CSV_fieldSeparator']=='') $blank_selected_sep = 'selected'; else $blank_selected_sep = ''; 325 326 $compulsory_list = array('firstname','lastname','username','password'); 327 328 $dialogBox = get_lang('Modify the format') .' : ' . "\n" 329 . '<br /><br />' . "\n" 330 . get_lang('The fields <em>%field_list</em> are compulsory', array ('%field_list' => implode(', ',$compulsory_list)) ) 331 . '<br /><br />' 332 . '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">' 333 . claro_form_relay_context() 334 . '<input type="hidden" name="AddType" value="' . $AddType . '" />' . "\n" 335 . '<input type="text" name="usedFormat" value="' . htmlspecialchars($usedFormat) . '" size="55" />' . "\n" 336 . '<br /><br />' . "\n" 337 . '<label for="fieldSeparator">' . get_lang('Fields separator used') . ' </label>:' 338 . '<select name="fieldSeparator" id="fieldSeparator">' 339 . '<option value=";" ' . $dot_coma_selected_sep . '>;</option>' . "\n" 340 . '<option value="," ' . $coma_selected_sep . ' >,</option>' . "\n" 341 . '<option value=" " ' . $blank_selected_sep . ' >(' . get_lang('Blank space') . ') </option>' . "\n" 342 . '</select>' 343 .' ' 344 . '<label for="enclosedBy">' 345 . get_lang('Fields enclosed by') .' :' 346 . '</label>' 347 348 . '<select name="enclosedBy" id="enclosedBy">' 349 . ' <option value="" '.$blank_selected.'>' . get_lang('None') . ' </option>' 350 . ' <option value="dbquote" '.$dbquote_selected.'>"</option>' 351 . ' <option value="," '.$coma_selected.'>,</option>' 352 . ' <option value="." '.$dot_selected.'>.</option>' 353 . '</select><br />' 354 . '<input type="submit" value="' . get_lang('Ok') . '" />' . "\n" 355 . '</form>' 356 ; 357 358 } 359 360 361 362 /** 363 * DISPLAY 364 */ 365 366 include get_path('incRepositorySys').'/claro_init_header.inc.php'; 367 echo claro_html_tool_title($nameTools); 368 if( isset( $dialogBox ) ) echo claro_html_message_box($dialogBox) . '<br />'; 369 370 switch ( $display ) 371 { 372 373 //DEFAULT DISPLAY : display form to upload 374 375 case 'default' : 376 { 377 $backButtonUrl = ''; 378 unset($_SESSION['claro_csv_userlist']); 379 if (claro_is_in_a_course()) 380 { 381 $backButtonUrl = get_module_url('CLUSR') . '/user.php' . claro_url_relay_context('?') ; 382 } 383 elseif (isset($addType) && $addType =='adminClassTool') //tricky fix, the use of addtype should be avoided 384 { 385 $backButtonUrl = get_path('clarolineRepositoryWeb').'admin/admin_class_user.php?class_id='.$_SESSION['admin_user_class_id'] . claro_url_relay_context('&') ; 386 } 387 elseif (claro_is_platform_admin()) 388 { 389 $backButtonUrl = get_path('clarolineRepositoryWeb') . 'admin/' . claro_url_relay_context('?') ; 390 } 391 392 $_SESSION['claro_CSV_done'] = FALSE; 393 394 echo get_lang('You must specify the CSV format used in your file') . "\n" 395 . ':' . "\n" 396 . '<br /><br />' . "\n" 397 . '<form enctype="multipart/form-data" method="POST" action="' . $_SERVER['PHP_SELF'] . '">' . "\n" 398 . claro_form_relay_context() 399 . '<input type="radio" name="firstLineFormat" value="YES" id="firstLineFormat_YES" />' . "\n" 400 . ' ' . "\n" 401 . '<label for="firstLineFormat_YES">' . "\n" 402 . get_lang('Use format defined in first line of file') . '</label>' . "\n" 403 . '<br /><br />' . "\n" 404 . '<input type="radio" name="firstLineFormat" value="NO" checked id="firstLineFormat_NO" />' . "\n" 405 . '<label for="firstLineFormat_NO">' . "\n" 406 . get_lang('Use the following format') . ' : ' . "\n" 407 . '</label>' . "\n" 408 . '<br /><br />' . "\n" 409 . '<b>' . "\n" 410 . ' ' 411 . $usedFormat . "\n" 412 . '</b>' . "\n" 413 . '<br /><br />' . "\n" 414 . ' ' . "\n" 415 . claro_html_cmd_link( $_SERVER['PHP_SELF'] 416 . '?display=default' 417 . '&loadDefault=yes' 418 . '&AddType=' . $AddType 419 . claro_url_relay_context('&') 420 , get_lang('Load default format') 421 ) . "\n" 422 . ' | ' 423 . claro_html_cmd_link( $_SERVER['PHP_SELF'] 424 . '?display=default' 425 . '&chformat=yes' 426 . '&AddType=' . $AddType 427 . claro_url_relay_context('&') 428 , get_lang('Edit format to use') 429 ) . "\n" 430 . '<br /><br />' . "\n" 431 . ' ' . "\n" 432 ; 433 echo '<input type="hidden" name="fieldSeparator" value="'; 434 if (!empty($_SESSION['CSV_fieldSeparator'])) echo $_SESSION['CSV_fieldSeparator']; 435 else echo ';'; 436 echo '" >' . "\n" 437 . '<input type="hidden" name="enclosedBy" value="' . $_SESSION['CSV_enclosedBy'] . '" />' . "\n" 438 . '<input type="hidden" name="AddType" value="' . $AddType . '" />' . "\n" 439 . '<br />' . "\n" 440 . get_lang('CSV file with the user list : ') 441 . '<input type="file" name="CSVfile" />' . "\n" 442 . '<br /><br />' . "\n" 443 . '<input type="submit" name="submitCSV" value="' . get_lang('Add user list') . '" />' . "\n" 444 . claro_html_button($backButtonUrl,get_lang('Cancel')) . "\n" 445 . '<input type="hidden" name="cmd" value="exImp" />' . "\n" 446 . '</form>' . "\n" 447 ; 448 449 } break; 450 451 // STEP ONE DISPLAY : display the possible error with uploaded file and ask for continue or cancel 452 453 case 'stepone' : 454 { 455 456 if ((!empty($_SESSION['claro_invalid_format_error']) && $_SESSION['claro_invalid_format_error']==true) || 457 !(count($_SESSION['claro_mail_synthax_error'])==0) || 458 !(count($_SESSION['claro_mail_used_error'])==0) || 459 !(count($_SESSION['claro_username_used_error'])==0) || 460 !(count($_SESSION['claro_officialcode_used_error'])==0) || 461 !(count($_SESSION['claro_password_error'])==0) || 462 !(count($_SESSION['claro_mail_duplicate_error'])==0) || 463 !(count($_SESSION['claro_username_duplicate_error'])==0) || 464 !(count($_SESSION['claro_officialcode_duplicate_error'])==0)) 465 { 466 echo '<b>' . get_lang('The following errors were found ') . ' :</b><br /><br />' . "\n"; 467 468 //display errors encountered while trying to add users 469 470 claro_disp_CSV_error_backlog(); 471 472 $noerror = FALSE; 473 } 474 else 475 { 476 echo get_lang('No error in file found.')."<br />"; 477 478 $noerror = TRUE; 479 } 480 481 482 if (!(isset($_SESSION['claro_invalid_format_error'])) || ($_SESSION['claro_invalid_format_error'] == false)) 483 { 484 echo '<br />' 485 . get_lang('Do you want to continue?') 486 . '<br />' 487 ; 488 if (!$noerror) 489 { 490 echo '(' . get_lang('if you choose to continue, lines with errors will simply be ignored') . ')<br />'; 491 } 492 echo '<br />' 493 . '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '?cmd=exImpSec">' . "\n" 494 . '<input type="hidden" name="AddType" value="' . $AddType . '" />' 495 . '<input type="submit" value="' . get_lang('Continue') .'" />' . "\n" 496 . claro_html_button($_SERVER['PHP_SELF'] . '?AddType=' . htmlspecialchars($AddType), get_lang('Cancel')) 497 . '</form>' . "\n"; 498 499 } 500 else 501 { 502 echo '<br />' . claro_html_button($_SERVER['PHP_SELF'], get_lang('Cancel')) . '<br />'; 503 } 504 } break; 505 506 // STEP TWO DISPLAY : display what happened, confirm users added (LOG) 507 508 case 'steptwo' : 509 510 echo '<b>' . get_lang('%nb_user new users in the platform',array( '%nb_user' => sizeof($usersToAdd) ) ) . '</b> <br /><br />'; 511 512 foreach ($usersToAdd as $user) 513 { 514 515 //display messages concerning actions done to new users... 516 517 switch ($AddType) 518 { 519 case 'adminTool': 520 echo get_lang('%firstname %lastname has been added to the campus', array('%firstname'=>$user['firstname'], 521 '%lastname'=>$user['lastname']) ). "<br />\n"; 522 break; 523 524 case 'adminClassTool': 525 echo get_lang('%firstname %lastname has been added to the campus and to the class', array('%firstname'=>$user['firstname'], 526 '%lastname'=>$user['lastname']) ). "<br />\n"; 527 break; 528 529 case 'userTool': 530 echo get_lang('%firstname %lastname has been added to the campus and to the course', array('%firstname'=>$user['firstname'], 531 '%lastname'=>$user['lastname'])). "<br />\n"; 532 break; 533 } 534 } 535 536 // display back link at the end of the log 537 538 switch ($AddType) 539 { 540 case 'adminTool' : 541 { 542 echo '<br />' 543 . '<a href="../admin/adminusers.php' . claro_url_relay_context('?') . '">>> ' 544 . get_lang('See user list') 545 . '</a>' 546 ; 547 } break; 548 549 case 'adminClassTool' : 550 { 551 echo '<br />' 552 . '<a href="../admin/admin_class.php' . claro_url_relay_context('?') . '">>> ' 553 . get_lang('Back to class list') 554 . '</a>' 555 ; 556 } break; 557 558 case 'userTool' : 559 { 560 echo '<br />' 561 . '<a href="user.php' . claro_url_relay_context('?') . '"><< ' . get_lang('Back to user list') . '</a>' 562 ; 563 } break; 564 } 565 break; 566 } 567 568 //footer 569 include get_path('incRepositorySys') . '/claro_init_footer.inc.php'; 570 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Thu Nov 29 14:38:42 2007 | par Balluche grâce à PHPXref 0.7 |
|