class_user_import.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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(MAIN_CLASS_TABLE);
  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. $user_table = Database :: get_main_table(MAIN_USER_TABLE);
  83. $class_user_table = Database :: get_main_table(MAIN_CLASS_USER_TABLE);
  84. $class_table = Database :: get_main_table(MAIN_CLASS_TABLE);
  85. $csv_data = array ();
  86. foreach ($users_classes as $index => $user_class)
  87. {
  88. $sql = "SELECT * FROM $class_table WHERE name = '".mysql_real_escape_string($user_class['ClassName'])."'";
  89. $res = api_sql_query($sql, __FILE__, __LINE__);
  90. $obj = mysql_fetch_object($res);
  91. $csv_data[$user_class['UserName']][$obj->id] = 1;
  92. }
  93. foreach ($csv_data as $username => $csv_subscriptions)
  94. {
  95. $user_id = 0;
  96. $sql = "SELECT * FROM $user_table u WHERE u.username = '".mysql_real_escape_string($username)."'";
  97. $res = api_sql_query($sql, __FILE__, __LINE__);
  98. $obj = mysql_fetch_object($res);
  99. $user_id = $obj->user_id;
  100. $sql = "SELECT * FROM $class_user_table cu WHERE cu.user_id = $user_id";
  101. $res = api_sql_query($sql, __FILE__, __LINE__);
  102. $db_subscriptions = array ();
  103. while ($obj = mysql_fetch_object($res))
  104. {
  105. $db_subscriptions[$obj->class_id] = 1;
  106. }
  107. $to_subscribe = array_diff(array_keys($csv_subscriptions), array_keys($db_subscriptions));
  108. $to_unsubscribe = array_diff(array_keys($db_subscriptions), array_keys($csv_subscriptions));
  109. if ($_POST['subscribe'])
  110. {
  111. foreach ($to_subscribe as $index => $class_id)
  112. {
  113. ClassManager :: add_user($user_id, $class_id);
  114. //echo get_lang('Subscription').' : '.$course_code.'<br />';
  115. }
  116. }
  117. if ($_POST['unsubscribe'])
  118. {
  119. foreach ($to_unsubscribe as $index => $class_id)
  120. {
  121. ClassManager :: unsubscribe_user($user_id, $class_id);
  122. //echo get_lang('Unsubscription').' : '.$course_code.'<br />';
  123. }
  124. }
  125. }
  126. }
  127. /**
  128. * Read the CSV-file
  129. * @param string $file Path to the CSV-file
  130. * @return array All course-information read from the file
  131. */
  132. function parse_csv_data($file)
  133. {
  134. $courses = Import :: csv_to_array($file);
  135. return $courses;
  136. }
  137. $langFile = array ('admin', 'registration');
  138. $cidReset = true;
  139. include ('../inc/global.inc.php');
  140. require_once (api_get_path(LIBRARY_PATH).'fileManage.lib.php');
  141. require_once (api_get_path(LIBRARY_PATH).'import.lib.php');
  142. require_once (api_get_path(LIBRARY_PATH).'usermanager.lib.php');
  143. require_once (api_get_path(LIBRARY_PATH).'classmanager.lib.php');
  144. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  145. $formSent = 0;
  146. $errorMsg = '';
  147. $tool_name = get_lang('AddUsersToAClass').' CSV';
  148. $interbredcrump[] = array ('url' => 'index.php', 'name' => get_lang('PlatformAdmin'));
  149. set_time_limit(0);
  150. $form = new FormValidator('class_user_import');
  151. $form->addElement('file', 'import_file', get_lang('ImportFileLocation'));
  152. $form->addElement('checkbox', 'subscribe', get_lang('Action'), get_lang('SubscribeUserIfNotAllreaySubscribed'));
  153. $form->addElement('checkbox', 'unsubscribe', '', get_lang('UnsubscribeUserIfSubscriptionIsNotInFile'));
  154. $form->addElement('submit', 'submit', get_lang('Ok'));
  155. if ($form->validate())
  156. {
  157. $users_classes = parse_csv_data($_FILES['import_file']['tmp_name']);
  158. $errors = validate_data($users_classes);
  159. if (count($errors) == 0)
  160. {
  161. save_data($users_classes);
  162. header('Location: class_list.php?action=show_message&message='.urlencode(get_lang('FileImported')));
  163. exit ();
  164. }
  165. }
  166. Display :: display_header($tool_name);
  167. api_display_tool_title($tool_name);
  168. if (count($errors) != 0)
  169. {
  170. $error_message = '<ul>';
  171. foreach ($errors as $index => $error_class_user)
  172. {
  173. $error_message .= '<li>'.get_lang('Line').' '.$error_class_user['line'].': <b>'.$error_class_user['error'].'</b>: ';
  174. $error_message .= '</li>';
  175. }
  176. $error_message .= '</ul>';
  177. Display :: display_error_message($error_message);
  178. }
  179. $form->display();
  180. ?>
  181. <p><?php echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')'; ?> :</p>
  182. <blockquote>
  183. <pre>
  184. <b>UserName</b>;<b>ClassName</b>
  185. jdoe;class01
  186. a.dam;class01
  187. </pre>
  188. </blockquote>
  189. <?php
  190. /*
  191. ==============================================================================
  192. FOOTER
  193. ==============================================================================
  194. */
  195. Display :: display_footer();
  196. ?>