course_user_import.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. // $Id: course_user_import.php 8216 2006-03-15 16:33:13Z turboke $
  3. /*
  4. ==============================================================================
  5. Dokeos - elearning and course management software
  6. Copyright (c) 2005 Bart Mollet <bart.mollet@hogent.be>
  7. For a full list of contributors, see "credits.txt".
  8. The full license can be read in "license.txt".
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13. See the GNU General Public License for more details.
  14. Contact: Dokeos, 181 rue Royale, B-1000 Brussels, Belgium, info@dokeos.com
  15. ==============================================================================
  16. */
  17. /**
  18. ==============================================================================
  19. * This tool allows platform admins to update course-user relations by uploading
  20. * a CSVfile
  21. * @package dokeos.admin
  22. ==============================================================================
  23. */
  24. /**
  25. * validate the imported data
  26. */
  27. function validate_data($users_courses)
  28. {
  29. $errors = array ();
  30. $coursecodes = array ();
  31. foreach ($users_courses as $index => $user_course)
  32. {
  33. $user_course['line'] = $index +1;
  34. //1. check if mandatory fields are set
  35. $mandatory_fields = array ('UserName', 'CourseCode', 'Status');
  36. foreach ($mandatory_fields as $key => $field)
  37. {
  38. if (!isset ($user_course[$field]) || strlen($user_course[$field]) == 0)
  39. {
  40. $user_course['error'] = get_lang($field.'Mandatory');
  41. $errors[] = $user_course;
  42. }
  43. }
  44. //2. check if coursecode exists
  45. if (isset ($user_course['CourseCode']) && strlen($user_course['CourseCode']) != 0)
  46. {
  47. //2.1 check if code allready used in this CVS-file
  48. if (!isset ($coursecodes[$user_course['CourseCode']]))
  49. {
  50. //2.1.1 check if code exists in DB
  51. $course_table = Database :: get_main_table(MAIN_COURSE_TABLE);
  52. $sql = "SELECT * FROM $course_table WHERE code = '".mysql_real_escape_string($user_course['CourseCode'])."'";
  53. $res = api_sql_query($sql, __FILE__, __LINE__);
  54. if (mysql_num_rows($res) == 0)
  55. {
  56. $user_course['error'] = get_lang('CodeDoesNotExists');
  57. $errors[] = $user_course;
  58. }
  59. else
  60. {
  61. $coursecodes[$user_course['CourseCode']] = 1;
  62. }
  63. }
  64. }
  65. //3. check if username exists
  66. if (isset ($user_course['UserName']) && strlen($user_course['UserName']) != 0)
  67. {
  68. if (UserManager :: is_username_available($user_course['UserName']))
  69. {
  70. $user_course['error'] = get_lang('UnknownUser');
  71. $errors[] = $user_course;
  72. }
  73. }
  74. //4. check if status is valid
  75. if (isset ($user_course['Status']) && strlen($user_course['Status']) != 0)
  76. {
  77. if ($user_course['Status'] != COURSEMANAGER && $user_course['Status'] != STUDENT)
  78. {
  79. $user_course['error'] = get_lang('UnknownStatus');
  80. $errors[] = $user_course;
  81. }
  82. }
  83. }
  84. return $errors;
  85. }
  86. /**
  87. * Save the imported data
  88. */
  89. function save_data($users_courses)
  90. {
  91. $user_table= Database::get_main_table(MAIN_USER_TABLE);
  92. $course_user_table= Database::get_main_table(MAIN_COURSE_USER_TABLE);
  93. $csv_data = array();
  94. foreach ($users_courses as $index => $user_course)
  95. {
  96. $csv_data[$user_course['UserName']][$user_course['CourseCode']] = $user_course['Status'];
  97. }
  98. foreach($csv_data as $username => $csv_subscriptions)
  99. {
  100. $user_id = 0;
  101. $sql = "SELECT * FROM $user_table u WHERE u.username = '".mysql_real_escape_string($username)."'";
  102. $res = api_sql_query($sql,__FILE__,__LINE__);
  103. $obj = mysql_fetch_object($res);
  104. $user_id = $obj->user_id;
  105. $sql = "SELECT * FROM $course_user_table cu WHERE cu.user_id = $user_id";
  106. $res = api_sql_query($sql,__FILE__,__LINE__);
  107. $db_subscriptions = array();
  108. while($obj = mysql_fetch_object($res))
  109. {
  110. $db_subscriptions[$obj->course_code] = $obj->status;
  111. }
  112. //echo '<b>User '.$username.'</b><br />';
  113. $to_subscribe = array_diff(array_keys($csv_subscriptions),array_keys($db_subscriptions));
  114. $to_unsubscribe = array_diff(array_keys($db_subscriptions),array_keys($csv_subscriptions));
  115. // echo '<pre>';
  116. // echo "CSV SUBSCRIPTION\n";
  117. // print_r($csv_subscriptions);
  118. // echo "DB SUBSCRIPTION\n";
  119. // print_r($db_subscriptions);
  120. // echo "TO SUBSCRIBE\n";
  121. // print_r($to_subscribe);
  122. // echo "TO UNSUBSCRIBE\n";
  123. // print_r($to_unsubscribe);
  124. // echo '</pre>';
  125. if($_POST['subscribe'])
  126. {
  127. foreach($to_subscribe as $index => $course_code)
  128. {
  129. CourseManager::add_user_to_course($user_id,$course_code,$csv_subscriptions[$course_code]);
  130. echo get_lang('Subscription').' : '.$course_code.'<br />';
  131. }
  132. }
  133. if($_POST['unsubscribe'])
  134. {
  135. foreach($to_unsubscribe as $index => $course_code)
  136. {
  137. CourseManager::unsubscribe_user($user_id,$course_code);
  138. echo get_lang('Unsubscription').' : '.$course_code.'<br />';
  139. }
  140. }
  141. }
  142. }
  143. /**
  144. * Read the CSV-file
  145. * @param string $file Path to the CSV-file
  146. * @return array All course-information read from the file
  147. */
  148. function parse_csv_data($file)
  149. {
  150. $courses = Import :: csv_to_array($file);
  151. return $courses;
  152. }
  153. $langFile = array ('admin', 'registration');
  154. $cidReset = true;
  155. include ('../inc/global.inc.php');
  156. api_protect_admin_script();
  157. require_once (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  158. require_once (api_get_path(LIBRARY_PATH).'import.lib.php');
  159. require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  160. require_once (api_get_path(LIBRARY_PATH).'course.lib.php');
  161. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  162. $formSent = 0;
  163. $errorMsg = '';
  164. $tool_name = get_lang('AddUsersToACourse').' CSV';
  165. $interbredcrump[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  166. set_time_limit(0);
  167. $form = new FormValidator('course_user_import');
  168. $form->addElement('file','import_file', get_lang('ImportFileLocation'));
  169. $form->addElement('checkbox','subscribe',get_lang('Action'),get_lang('SubscribeUserIfNotAllreadySubscribed'));
  170. $form->addElement('checkbox','unsubscribe','',get_lang('UnsubscribeUserIfSubscriptionIsNotInFile'));
  171. $form->addElement('submit','submit',get_lang('Ok'));
  172. if ($form->validate())
  173. {
  174. $users_courses = parse_csv_data($_FILES['import_file']['tmp_name']);
  175. $errors = validate_data($users_courses);
  176. if (count($errors) == 0)
  177. {
  178. save_data($users_courses);
  179. header('Location: user_list.php?action=show_message&message='.urlencode(get_lang('FileImported')));
  180. exit ();
  181. }
  182. }
  183. Display :: display_header($tool_name);
  184. api_display_tool_title($tool_name);
  185. if (count($errors) != 0)
  186. {
  187. $error_message = '<ul>';
  188. foreach ($errors as $index => $error_course)
  189. {
  190. $error_message .= '<li>'.get_lang('Line').' '.$error_course['line'].': <b>'.$error_course['error'].'</b>: ';
  191. $error_message .= $error_course['Code'].' '.$error_course['Title'];
  192. $error_message .= '</li>';
  193. }
  194. $error_message .= '</ul>';
  195. Display :: display_error_message($error_message);
  196. }
  197. $form->display();
  198. ?>
  199. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  200. <blockquote>
  201. <pre>
  202. <b>UserName</b>;<b>CourseCode</b>;<b>Status</b>
  203. jdoe;course01;<?php echo COURSEMANAGER; ?>
  204. a.dam;course01;<?php echo STUDENT; ?>
  205. </pre>
  206. <?php
  207. echo COURSEMANAGER.': '.get_lang('Teacher').'<br />';
  208. echo STUDENT.': '.get_lang('Student').'<br />';
  209. ?>
  210. </blockquote>
  211. <?php
  212. /*
  213. ==============================================================================
  214. FOOTER
  215. ==============================================================================
  216. */
  217. Display :: display_footer();
  218. ?>