class_user_import.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. // $Id: class_user_import.php 9018 2006-06-28 15:11:16Z evie_em $
  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 class-user relations by uploading a
  20. * CSVfile
  21. * @package dokeos.admin
  22. ==============================================================================
  23. */
  24. /**
  25. * validate the imported data
  26. */
  27. function validate_data($user_classes)
  28. {
  29. $errors = array ();
  30. $classcodes = array ();
  31. foreach ($user_classes as $index => $user_class)
  32. {
  33. $user_class['line'] = $index +1;
  34. //1. check if mandatory fields are set
  35. $mandatory_fields = array ('UserName', 'ClassName');
  36. foreach ($mandatory_fields as $key => $field)
  37. {
  38. if (!isset ($user_class[$field]) || strlen($user_class[$field]) == 0)
  39. {
  40. $user_class['error'] = get_lang($field.'Mandatory');
  41. $errors[] = $user_class;
  42. }
  43. }
  44. //2. check if classcode exists
  45. if (isset ($user_class['ClassName']) && strlen($user_class['ClassName']) != 0)
  46. {
  47. //2.1 check if code allready used in this CVS-file
  48. if (!isset ($classcodes[$user_class['ClassName']]))
  49. {
  50. //2.1.1 check if code exists in DB
  51. $class_table = Database :: get_main_table(TABLE_MAIN_CLASS);
  52. $sql = "SELECT * FROM $class_table WHERE name = '".mysql_real_escape_string($user_class['ClassName'])."'";
  53. $res = api_sql_query($sql, __FILE__, __LINE__);
  54. if (mysql_num_rows($res) == 0)
  55. {
  56. $user_class['error'] = get_lang('CodeDoesNotExists');
  57. $errors[] = $user_class;
  58. }
  59. else
  60. {
  61. $classcodes[$user_class['CourseCode']] = 1;
  62. }
  63. }
  64. }
  65. //3. check if username exists
  66. if (isset ($user_class['UserName']) && strlen($user_class['UserName']) != 0)
  67. {
  68. if (UserManager :: is_username_available($user_class['UserName']))
  69. {
  70. $user_class['error'] = get_lang('UnknownUser');
  71. $errors[] = $user_class;
  72. }
  73. }
  74. }
  75. return $errors;
  76. }
  77. /**
  78. * Save the imported data
  79. */
  80. function save_data($users_classes)
  81. {
  82. // table definitions
  83. $user_table = Database :: get_main_table(TABLE_MAIN_USER);
  84. $class_user_table = Database :: get_main_table(TABLE_MAIN_CLASS_USER);
  85. $class_table = Database :: get_main_table(TABLE_MAIN_CLASS);
  86. $csv_data = array ();
  87. foreach ($users_classes as $index => $user_class)
  88. {
  89. $sql = "SELECT * FROM $class_table WHERE name = '".mysql_real_escape_string($user_class['ClassName'])."'";
  90. $res = api_sql_query($sql, __FILE__, __LINE__);
  91. $obj = mysql_fetch_object($res);
  92. $csv_data[$user_class['UserName']][$obj->id] = 1;
  93. }
  94. foreach ($csv_data as $username => $csv_subscriptions)
  95. {
  96. $user_id = 0;
  97. $sql = "SELECT * FROM $user_table u WHERE u.username = '".mysql_real_escape_string($username)."'";
  98. $res = api_sql_query($sql, __FILE__, __LINE__);
  99. $obj = mysql_fetch_object($res);
  100. $user_id = $obj->user_id;
  101. $sql = "SELECT * FROM $class_user_table cu WHERE cu.user_id = $user_id";
  102. $res = api_sql_query($sql, __FILE__, __LINE__);
  103. $db_subscriptions = array ();
  104. while ($obj = mysql_fetch_object($res))
  105. {
  106. $db_subscriptions[$obj->class_id] = 1;
  107. }
  108. $to_subscribe = array_diff(array_keys($csv_subscriptions), array_keys($db_subscriptions));
  109. $to_unsubscribe = array_diff(array_keys($db_subscriptions), array_keys($csv_subscriptions));
  110. if ($_POST['subscribe'])
  111. {
  112. foreach ($to_subscribe as $index => $class_id)
  113. {
  114. ClassManager :: add_user($user_id, $class_id);
  115. //echo get_lang('Subscription').' : '.$course_code.'<br />';
  116. }
  117. }
  118. if ($_POST['unsubscribe'])
  119. {
  120. foreach ($to_unsubscribe as $index => $class_id)
  121. {
  122. ClassManager :: unsubscribe_user($user_id, $class_id);
  123. //echo get_lang('Unsubscription').' : '.$course_code.'<br />';
  124. }
  125. }
  126. }
  127. }
  128. /**
  129. * Read the CSV-file
  130. * @param string $file Path to the CSV-file
  131. * @return array All course-information read from the file
  132. */
  133. function parse_csv_data($file)
  134. {
  135. $courses = Import :: csv_to_array($file);
  136. return $courses;
  137. }
  138. // name of the language file that needs to be included
  139. $language_file = array ('admin', 'registration');
  140. $cidReset = true;
  141. include ('../inc/global.inc.php');
  142. require_once (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  143. require_once (api_get_path(LIBRARY_PATH).'import.lib.php');
  144. require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  145. require_once (api_get_path(LIBRARY_PATH).'classmanager.lib.php');
  146. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  147. $formSent = 0;
  148. $errorMsg = '';
  149. $tool_name = get_lang('AddUsersToAClass').' CSV';
  150. $interbreadcrumb[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  151. set_time_limit(0);
  152. $form = new FormValidator('class_user_import');
  153. $form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
  154. $form->addElement('checkbox', 'subscribe', get_lang('Action'), get_lang('SubscribeUserIfNotAllreadySubscribed'));
  155. $form->addElement('checkbox', 'unsubscribe', '', get_lang('UnsubscribeUserIfSubscriptionIsNotInFile'));
  156. $form->addElement('submit', 'submit', get_lang('Ok'));
  157. if ($form->validate())
  158. {
  159. $users_classes = parse_csv_data($_FILES['import_file']['tmp_name']);
  160. $errors = validate_data($users_classes);
  161. if (count($errors) == 0)
  162. {
  163. save_data($users_classes);
  164. header('Location: class_list.php?action=show_message&message='.urlencode(get_lang('FileImported')));
  165. exit ();
  166. }
  167. }
  168. Display :: display_header($tool_name);
  169. api_display_tool_title($tool_name);
  170. if (count($errors) != 0)
  171. {
  172. $error_message = '<ul>';
  173. foreach ($errors as $index => $error_class_user)
  174. {
  175. $error_message .= '<li>'.get_lang('Line').' '.$error_class_user['line'].': <b>'.$error_class_user['error'].'</b>: ';
  176. $error_message .= '</li>';
  177. }
  178. $error_message .= '</ul>';
  179. Display :: display_error_message($error_message);
  180. }
  181. $form->display();
  182. ?>
  183. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  184. <blockquote>
  185. <pre>
  186. <b>UserName</b>;<b>ClassName</b>
  187. jdoe;class01
  188. a.dam;class01
  189. </pre>
  190. </blockquote>
  191. <?php
  192. /*
  193. ==============================================================================
  194. FOOTER
  195. ==============================================================================
  196. */
  197. Display :: display_footer();
  198. ?>