user_import.php 6.5 KB

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