user_import.php 6.4 KB

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