user_import.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This tool allows platform admins to add users by uploading a CSV or XML file
  5. * This code is inherited from admin/user_import.php
  6. * @package chamilo.reporting
  7. * Created on 26 julio 2008 by Julio Montoya gugli100@gmail.com
  8. */
  9. $cidReset = true;
  10. require_once '../inc/global.inc.php';
  11. $this_section = SECTION_PLATFORM_ADMIN; // TODO: Platform admin section?
  12. $tool_name = get_lang('ImportUserListXMLCSV');
  13. api_block_anonymous_users();
  14. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('MySpace'));
  15. $id_session = '';
  16. if (isset($_GET['id_session']) && $_GET['id_session'] != '') {
  17. $id_session = intval($_GET['id_session']);
  18. $interbreadcrumb[] = array ('url' => 'session.php', 'name' => get_lang('Sessions'));
  19. $interbreadcrumb[] = array ('url' => 'course.php?id_session='.$id_session.'', 'name' => get_lang('Course'));
  20. }
  21. // Set this option to true to enforce strict purification for usenames.
  22. $purification_option_for_usernames = false;
  23. // Checking whether the current coach is the admin coach.
  24. if (api_get_setting('add_users_by_coach') == 'true') {
  25. if (!api_is_platform_admin()) {
  26. if (isset($_REQUEST['id_session'])) {
  27. $id_session = intval($_REQUEST['id_session']);
  28. $sql = 'SELECT id_coach FROM '.Database :: get_main_table(TABLE_MAIN_SESSION).'
  29. WHERE id='.$id_session;
  30. $rs = Database::query($sql);
  31. if (Database::result($rs, 0, 0) != $_user['user_id']) {
  32. api_not_allowed(true);
  33. }
  34. } else {
  35. api_not_allowed(true);
  36. }
  37. }
  38. } else {
  39. api_not_allowed(true);
  40. }
  41. set_time_limit(0);
  42. if ($_POST['formSent'] && $_FILES['import_file']['size'] !== 0) {
  43. $file_type = $_POST['file_type'];
  44. $id_session = intval($_POST['id_session']);
  45. if ($file_type == 'csv') {
  46. $users = MySpace::parse_csv_data($_FILES['import_file']['tmp_name']);
  47. } else {
  48. $users = MySpace::parse_xml_data($_FILES['import_file']['tmp_name']);
  49. }
  50. if (count($users) > 0) {
  51. $results = MySpace::validate_data($users);
  52. $errors = $results['errors'];
  53. $users = $results['users'];
  54. if (count($errors) == 0) {
  55. if (!empty($id_session)) {
  56. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  57. // Selecting all the courses from the session id requested.
  58. $sql = "SELECT c_id FROM $tbl_session_rel_course WHERE session_id ='$id_session'";
  59. $result = Database::query($sql);
  60. $course_list = array();
  61. while ($row = Database::fetch_array($result)) {
  62. $course_list[] = $row['c_id'];
  63. }
  64. $errors = MySpace::get_user_creator($users);
  65. $users = MySpace::check_all_usernames($users, $course_list, $id_session);
  66. if (count($errors) == 0) {
  67. MySpace::save_data($users, $course_list, $id_session);
  68. }
  69. } else {
  70. header('Location: course.php?id_session='.$id_session.'&action=error_message&message='.urlencode(get_lang('NoSessionId')));
  71. }
  72. }
  73. } else {
  74. header('Location: course.php?id_session='.$id_session.'&action=error_message&message='.urlencode(get_lang('NoUsersRead')));
  75. }
  76. }
  77. Display :: display_header($tool_name);
  78. if ($_FILES['import_file']['size'] == 0 && $_POST) {
  79. Display::display_error_message(get_lang('ThisFieldIsRequired'));
  80. }
  81. if (count($errors) != 0) {
  82. $error_message = '<ul>';
  83. foreach ($errors as $index => $error_user) {
  84. $error_message .= '<li><strong>'.$error_user['error'].'</strong>: ';
  85. $error_message .= api_get_person_name($error_user['FirstName'], $error_user['LastName']);
  86. $error_message .= '</li>';
  87. }
  88. $error_message .= '</ul>';
  89. Display :: display_error_message($error_message, false);
  90. }
  91. $form = new FormValidator('user_import');
  92. $form->addElement('hidden', 'formSent');
  93. $form->addElement('hidden', 'id_session',$id_session);
  94. $form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
  95. $form->addRule('import_file', get_lang('ThisFieldIsRequired'), 'required');
  96. $allowed_file_types = array ('xml', 'csv');
  97. $form->addRule('import_file', get_lang('InvalidExtension').' ('.implode(',', $allowed_file_types).')', 'filetype', $allowed_file_types);
  98. $form->addElement('radio', 'file_type', get_lang('FileType'), 'XML (<a href="../admin/example.xml" target="_blank">'.get_lang('ExampleXMLFile').'</a>)', 'xml');
  99. $form->addElement('radio', 'file_type', null, 'CSV (<a href="../admin/example.csv" target="_blank">'.get_lang('ExampleCSVFile').'</a>)', 'csv');
  100. $form->addElement('radio', 'sendMail', get_lang('SendMailToUsers'), get_lang('Yes'), 1);
  101. $form->addElement('radio', 'sendMail', null, get_lang('No'), 0);
  102. $form->addElement('submit', 'submit', get_lang('Ok'));
  103. $defaults['formSent'] = 1;
  104. $defaults['sendMail'] = 0;
  105. $defaults['file_type'] = 'xml';
  106. $form->setDefaults($defaults);
  107. $form->display();
  108. ?>
  109. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  110. <blockquote>
  111. <pre>
  112. <b>LastName</b>;<b>FirstName</b>;<b>Email</b>;UserName;Password;OfficialCode;PhoneNumber;
  113. <b>Montoya</b>;<b>Julio</b>;<b>info@localhost</b>;jmontoya;123456789;code1;3141516
  114. <b>Doewing</b>;<b>Johny</b>;<b>info@localhost</b>;jdoewing;123456789;code2;3141516
  115. </pre>
  116. </blockquote>
  117. <p><?php echo get_lang('XMLMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  118. <blockquote>
  119. <pre>
  120. &lt;?xml version=&quot;1.0&quot; encoding=&quot;<?php echo api_refine_encoding_id(api_get_system_encoding()); ?>&quot;?&gt;
  121. &lt;Contacts&gt;
  122. &lt;Contact&gt;
  123. <b>&lt;LastName&gt;Montoya&lt;/LastName&gt;</b>
  124. <b>&lt;FirstName&gt;Julio&lt;/FirstName&gt;</b>
  125. <b>&lt;Email&gt;info@localhost&lt;/Email&gt;</b>
  126. &lt;UserName&gt;jmontoya&lt;/UserName&gt;
  127. &lt;Password&gt;123456&lt;/Password&gt;
  128. &lt;OfficialCode&gt;code1&lt;/OfficialCode&gt;
  129. &lt;PhoneNumber&gt;3141516&lt;/PhoneNumber&gt;
  130. &lt;/Contact&gt;
  131. &lt;/Contacts&gt;
  132. </pre>
  133. </blockquote>
  134. <?php
  135. Display :: display_footer();