course_user_import.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php // $Id: course_user_import.php 8216 2006-03-15 16:33:13Z turboke $
  2. /* For licensing terms, see /dokeos_license.txt */
  3. /**
  4. ==============================================================================
  5. * This tool allows platform admins to update course-user relations by uploading
  6. * a CSVfile
  7. * @package dokeos.admin
  8. ==============================================================================
  9. */
  10. /**
  11. * Validates the imported data.
  12. */
  13. function validate_data($users_courses) {
  14. $errors = array ();
  15. $coursecodes = array ();
  16. foreach ($users_courses as $index => $user_course) {
  17. $user_course['line'] = $index +1;
  18. // 1. Check whether mandatory fields are set.
  19. $mandatory_fields = array ('UserName', 'CourseCode', 'Status');
  20. foreach ($mandatory_fields as $key => $field) {
  21. if (!isset($user_course[$field]) || strlen($user_course[$field]) == 0) {
  22. $user_course['error'] = get_lang($field.'Mandatory');
  23. $errors[] = $user_course;
  24. }
  25. }
  26. // 2. Check whether coursecode exists.
  27. if (isset ($user_course['CourseCode']) && strlen($user_course['CourseCode']) != 0) {
  28. // 2.1 Check whethher code has been allready used by this CVS-file.
  29. if (!isset($coursecodes[$user_course['CourseCode']])) {
  30. // 2.1.1 Check whether course with this code exists in the system.
  31. $course_table = Database :: get_main_table(TABLE_MAIN_COURSE);
  32. $sql = "SELECT * FROM $course_table WHERE code = '".Database::escape_string($user_course['CourseCode'])."'";
  33. $res = Database::query($sql);
  34. if (Database::num_rows($res) == 0) {
  35. $user_course['error'] = get_lang('CodeDoesNotExists');
  36. $errors[] = $user_course;
  37. } else {
  38. $coursecodes[$user_course['CourseCode']] = 1;
  39. }
  40. }
  41. }
  42. // 3. Check whether username exists.
  43. if (isset ($user_course['UserName']) && strlen($user_course['UserName']) != 0)
  44. {
  45. if (UserManager::is_username_available($user_course['UserName'])) {
  46. $user_course['error'] = get_lang('UnknownUser');
  47. $errors[] = $user_course;
  48. }
  49. }
  50. // 4. Check whether status is valid.
  51. if (isset ($user_course['Status']) && strlen($user_course['Status']) != 0) {
  52. if ($user_course['Status'] != COURSEMANAGER && $user_course['Status'] != STUDENT) {
  53. $user_course['error'] = get_lang('UnknownStatus');
  54. $errors[] = $user_course;
  55. }
  56. }
  57. }
  58. return $errors;
  59. }
  60. /**
  61. * Saves imported data.
  62. */
  63. function save_data($users_courses) {
  64. $user_table= Database::get_main_table(TABLE_MAIN_USER);
  65. $course_user_table= Database::get_main_table(TABLE_MAIN_COURSE_USER);
  66. $csv_data = array();
  67. foreach ($users_courses as $index => $user_course) {
  68. $csv_data[$user_course['UserName']][$user_course['CourseCode']] = $user_course['Status'];
  69. }
  70. foreach($csv_data as $username => $csv_subscriptions) {
  71. $user_id = 0;
  72. $sql = "SELECT * FROM $user_table u WHERE u.username = '".Database::escape_string($username)."'";
  73. $res = Database::query($sql);
  74. $obj = Database::fetch_object($res);
  75. $user_id = $obj->user_id;
  76. $sql = "SELECT * FROM $course_user_table cu WHERE cu.user_id = $user_id AND cu.relation_type<>".COURSE_RELATION_TYPE_RRHH." ";
  77. $res = Database::query($sql);
  78. $db_subscriptions = array();
  79. while($obj = Database::fetch_object($res)) {
  80. $db_subscriptions[$obj->course_code] = $obj->status;
  81. }
  82. $to_subscribe = array_diff(array_keys($csv_subscriptions),array_keys($db_subscriptions));
  83. $to_unsubscribe = array_diff(array_keys($db_subscriptions),array_keys($csv_subscriptions));
  84. global $inserted_in_course;
  85. if (!isset($inserted_in_course)) {
  86. $inserted_in_course = array();
  87. }
  88. if($_POST['subscribe']) {
  89. foreach($to_subscribe as $index => $course_code) {
  90. if(CourseManager :: course_exists($course_code)) {
  91. CourseManager::add_user_to_course($user_id,$course_code,$csv_subscriptions[$course_code]);
  92. $course_info = CourseManager::get_course_information($course_code);
  93. $inserted_in_course[$course_code] = $course_info['title'];
  94. }
  95. if (CourseManager :: course_exists($course_code,true)) {
  96. // Also subscribe to virtual courses through check on visual code.
  97. $list = CourseManager :: get_courses_info_from_visual_code($course_code);
  98. foreach ($list as $vcourse) {
  99. if ($vcourse['code'] == $course_code) {
  100. // Ignore, this has already been inserted.
  101. } else {
  102. CourseManager::add_user_to_course($user_id,$vcourse['code'],$csv_subscriptions[$course_code]);
  103. $inserted_in_course[$vcourse['code']] = $vcourse['title'];
  104. }
  105. }
  106. }
  107. }
  108. }
  109. if($_POST['unsubscribe']) {
  110. foreach($to_unsubscribe as $index => $course_code) {
  111. if(CourseManager :: course_exists($course_code)) {
  112. CourseManager::unsubscribe_user($user_id,$course_code);
  113. $course_info = CourseManager::get_course_information($course_code);
  114. $inserted_in_course[$course_code] = $course_info['title'];
  115. }
  116. if (CourseManager :: course_exists($course_code,true)) {
  117. // also subscribe to virtual courses through check on visual code
  118. $list = CourseManager :: get_courses_info_from_visual_code($course_code);
  119. foreach ($list as $vcourse) {
  120. if ($vcourse['code'] == $course_code) {
  121. // Ignore, this has already been inserted.
  122. } else {
  123. CourseManager::unsubscribe_user($user_id,$vcourse['code']);
  124. $inserted_in_course[$vcourse['code']] = $vcourse['title'];
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }
  131. }
  132. /**
  133. * Reads CSV-file.
  134. * @param string $file Path to the CSV-file
  135. * @return array All course-information read from the file
  136. */
  137. function parse_csv_data($file) {
  138. $courses = Import :: csv_to_array($file);
  139. return $courses;
  140. }
  141. // Language files that should be included,
  142. $language_file = array ('admin', 'registration');
  143. $cidReset = true;
  144. // Including the global Dokeos file.
  145. include '../inc/global.inc.php';
  146. // Setting the section (for the tabs).
  147. $this_section = SECTION_PLATFORM_ADMIN;
  148. // Protecting the admin section.
  149. api_protect_admin_script();
  150. // Including additional libraries.
  151. require_once api_get_path(LIBRARY_PATH).'fileManage.lib.php';
  152. require_once api_get_path(LIBRARY_PATH).'import.lib.php';
  153. require_once api_get_path(LIBRARY_PATH).'usermanager.lib.php';
  154. require_once api_get_path(LIBRARY_PATH).'course.lib.php';
  155. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  156. $tool_name = get_lang('AddUsersToACourse').' CSV';
  157. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  158. set_time_limit(0);
  159. // Creating the form.
  160. $form = new FormValidator('course_user_import');
  161. $form->addElement('header', '', $tool_name);
  162. $form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
  163. $form->addElement('checkbox', 'subscribe', get_lang('Action'), get_lang('SubscribeUserIfNotAllreadySubscribed'));
  164. $form->addElement('checkbox', 'unsubscribe', '', get_lang('UnsubscribeUserIfSubscriptionIsNotInFile'));
  165. $form->addElement('style_submit_button', 'submit',get_lang('Import'),'class="save"');
  166. if ($form->validate()) {
  167. $users_courses = parse_csv_data($_FILES['import_file']['tmp_name']);
  168. $errors = validate_data($users_courses);
  169. if (count($errors) == 0) {
  170. $inserted_in_course = array();
  171. save_data($users_courses);
  172. // Build the alert message in case there were visual codes subscribed to.
  173. if ($_POST['subscribe']) {
  174. $warn = get_lang('UsersSubscribedToBecauseVisualCode').': ';
  175. } else {
  176. $warn = get_lang('UsersUnsubscribedFromBecauseVisualCode').': ';
  177. }
  178. if (count($inserted_in_course) > 1) {
  179. // The users have been inserted in more than one course.
  180. foreach ($inserted_in_course as $code => $info) {
  181. $warn .= ' '.$info.' ('.$code.'),';
  182. }
  183. $warn = substr($warn,0,-1);
  184. }
  185. Security::clear_token();
  186. $tok = Security::get_token();
  187. header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('FileImported')).'&warn='.urlencode($warn).'&sec_token='.$tok);
  188. exit ();
  189. }
  190. }
  191. // Displaying the header.
  192. Display :: display_header($tool_name);
  193. // Displaying the tool title.
  194. // api_display_tool_title($tool_name);
  195. if (count($errors) != 0) {
  196. $error_message = '<ul>';
  197. foreach ($errors as $index => $error_course) {
  198. $error_message .= '<li>'.get_lang('Line').' '.$error_course['line'].': <strong>'.$error_course['error'].'</strong>: ';
  199. $error_message .= $error_course['Code'].' '.$error_course['Title'];
  200. $error_message .= '</li>';
  201. }
  202. $error_message .= '</ul>';
  203. Display :: display_error_message($error_message);
  204. }
  205. // Displaying the form.
  206. $form->display();
  207. ?>
  208. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  209. <blockquote>
  210. <pre>
  211. <b>UserName</b>;<b>CourseCode</b>;<b>Status</b>
  212. jdoe;course01;<?php echo COURSEMANAGER; ?>
  213. adam;course01;<?php echo STUDENT; ?>
  214. </pre>
  215. <?php
  216. echo COURSEMANAGER.': '.get_lang('Teacher').'<br />';
  217. echo STUDENT.': '.get_lang('Student').'<br />';
  218. ?>
  219. </blockquote>
  220. <?php
  221. // Footer.
  222. Display :: display_footer();