user_import.php 6.3 KB

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