import_csv.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php
  2. if (PHP_SAPI!='cli') {
  3. die('Run this script through the command line or comment this line in the code');
  4. }
  5. require_once __DIR__.'/../inc/global.inc.php';
  6. require_once api_get_path(LIBRARY_PATH).'log.class.php';
  7. class ImportCsv
  8. {
  9. private $logger;
  10. public function __construct($logger)
  11. {
  12. $this->logger = $logger;
  13. }
  14. public function run()
  15. {
  16. $path = api_get_path(SYS_CODE_PATH).'cron/incoming/';
  17. if (!is_dir($path)) {
  18. echo "The folder! $path does not exits";
  19. exit;
  20. }
  21. $files = scandir($path);
  22. if (!empty($files)) {
  23. foreach ($files as $file) {
  24. $fileInfo = pathinfo($file);
  25. if ($fileInfo['extension'] == 'csv') {
  26. // teachers_yyyymmdd.csv, courses_yyyymmdd.csv, students_yyyymmdd.csv and sessions_yyyymmdd.csv
  27. $parts = explode('_', $fileInfo['filename']);
  28. $method = 'import'.ucwords($parts[0]);
  29. if (method_exists($this, $method)) {
  30. $this->$method($path.$fileInfo['basename']);
  31. } else {
  32. echo "Error - This file can't be processed.";
  33. exit;
  34. }
  35. }
  36. }
  37. }
  38. }
  39. /**
  40. * @param string $file
  41. */
  42. function moveFile($file)
  43. {
  44. $moved = str_replace('incoming', 'treated', $file);
  45. // $result = rename($file, $moved);
  46. $result = 1;
  47. if ($result) {
  48. $this->logger->addInfo("Moving file to the treated folder: $file");
  49. } else {
  50. $this->logger->addError("Error - Cant move file to the treated folder: $file");
  51. }
  52. }
  53. /**
  54. * File to import
  55. * @param string $file
  56. */
  57. function importTeachers($file)
  58. {
  59. $data = Import::csv_to_array($file);
  60. $this->logger->addInfo("-- Import Teachers --");
  61. $this->logger->addInfo("Reading file: $file");
  62. /* Unique identifier: official-code username.
  63. Email address and password should never get updated. *ok
  64. The only fields that I can think of that should update if the data changes in the csv file are FirstName and LastName. *ok
  65. A slight edit of these fields should be taken into account. ???
  66. Adding teachers is no problem, but deleting them shouldn’t be automated, but we should get a log of “to delete teachers”.
  67. We’ll handle that manually if applicable.
  68. No delete!
  69. */
  70. if (!empty($data)) {
  71. foreach ($data as $row) {
  72. $userInfo = api_get_user_info_from_username($row['username']);
  73. $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
  74. if (empty($userInfo) && empty($userInfoByOfficialCode)) {
  75. // Create user
  76. $result = UserManager::create_user(
  77. $row['firstname'],
  78. $row['lastname'],
  79. COURSEMANAGER,
  80. $row['email'],
  81. $row['username'],
  82. $row['password'],
  83. $row['official_code'],
  84. $row['language'],
  85. $row['phone'],
  86. $row['picture'], //picture
  87. PLATFORM_AUTH_SOURCE, // ?
  88. $row['expiration_date'], //$expiration_date = '0000-00-00 00:00:00',
  89. 1, //active
  90. 0,
  91. null, // extra
  92. null, //$encrypt_method = '',
  93. false //$send_mail = false
  94. );
  95. if ($result) {
  96. $this->logger->addInfo("Info - Teachers - User created: ".$row['username']);
  97. } else {
  98. $this->logger->addError("Error - Teachers - User NOT created: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  99. }
  100. } else {
  101. if (empty($userInfo)) {
  102. $this->logger->addError("Error - Teachers - Can't update user :".$row['username']);
  103. continue;
  104. }
  105. // Update user
  106. $result = UserManager::update_user(
  107. $userInfo['user_id'],
  108. $row['firstname'], // <<-- changed
  109. $row['lastname'], // <<-- changed
  110. $userInfo['username'],
  111. null, //$password = null,
  112. $auth_source = null,
  113. $userInfo['email'],
  114. COURSEMANAGER,
  115. $userInfo['official_code'],
  116. $userInfo['phone'],
  117. $userInfo['picture_uri'],
  118. $userInfo['expiration_date'],
  119. $userInfo['active'],
  120. null, //$creator_id = null,
  121. 0, //$hr_dept_id = 0,
  122. null, // $extra = null,
  123. null, //$language = 'english',
  124. null, //$encrypt_method = '',
  125. false, //$send_email = false,
  126. 0 //$reset_password = 0
  127. );
  128. if ($result) {
  129. $this->logger->addInfo("Teachers - User updated: ".$row['username']);
  130. } else {
  131. $this->logger->addError("Teachers - User not updated: ".$row['username']);
  132. }
  133. }
  134. // UserManager::delete_user();
  135. $this->moveFile($file);
  136. }
  137. }
  138. }
  139. /**
  140. * @param string $file
  141. */
  142. function importStudents($file)
  143. {
  144. $data = Import::csv_to_array($file);
  145. $this->logger->addInfo("-- Import Students --");
  146. $this->logger->addInfo("Reading file: $file");
  147. /*
  148. * Another users import.
  149. Unique identifier: official code and username . ok
  150. Username and password should never get updated. ok
  151. If an update should need to occur (because it changed in the .csv), we’ll want that logged. We will handle this manually in that case.
  152. All other fields should be updateable, though passwords should of course not get updated. ok
  153. If a user gets deleted (not there anymore),
  154. He should be set inactive one year after the current date. So I presume you’ll just update the expiration date. We want to grant access to courses up to a year after deletion.
  155. */
  156. if (!empty($data)) {
  157. foreach ($data as $row) {
  158. $userInfo = api_get_user_info_from_username($row['username']);
  159. $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
  160. if (empty($userInfo) && empty($userInfoByOfficialCode)) {
  161. // Create user
  162. $result = UserManager::create_user(
  163. $row['firstname'],
  164. $row['lastname'],
  165. STUDENT,
  166. $row['email'],
  167. $row['username'],
  168. $row['password'],
  169. $row['official_code'],
  170. $row['language'],
  171. $row['phone'],
  172. $row['picture'], //picture
  173. PLATFORM_AUTH_SOURCE, // ?
  174. $row['expiration_date'], //$expiration_date = '0000-00-00 00:00:00',
  175. 1, //active
  176. 0,
  177. null, // extra
  178. null, //$encrypt_method = '',
  179. false //$send_mail = false
  180. );
  181. if ($result) {
  182. $this->logger->addInfo("Students - User created: ".$row['username']);
  183. } else {
  184. $this->logger->addError("Students - User NOT created: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  185. }
  186. } else {
  187. if (empty($userInfo)) {
  188. $this->logger->addError("Students - Can't update user :".$row['username']);
  189. continue;
  190. }
  191. if ($row['action'] == 'delete') {
  192. // INactive one year later
  193. $userInfo['expiration_date'] = api_get_utc_datetime(api_strtotime(time() + 365*24*60*60));
  194. }
  195. // Update user
  196. $result = UserManager::update_user(
  197. $userInfo['user_id'],
  198. $row['firstname'], // <<-- changed
  199. $row['lastname'], // <<-- changed
  200. $userInfo['username'],
  201. null, //$password = null,
  202. $auth_source = null,
  203. $userInfo['email'],
  204. STUDENT,
  205. $userInfo['official_code'],
  206. $userInfo['phone'],
  207. $userInfo['picture_uri'],
  208. $userInfo['expiration_date'],
  209. $userInfo['active'],
  210. null, //$creator_id = null,
  211. 0, //$hr_dept_id = 0,
  212. null, // $extra = null,
  213. null, //$language = 'english',
  214. null, //$encrypt_method = '',
  215. false, //$send_email = false,
  216. 0 //$reset_password = 0
  217. );
  218. if ($result) {
  219. $this->logger->addInfo("Students - User updated: ".$row['username']);
  220. } else {
  221. $this->logger->addError("Students - User NOT updated: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  222. }
  223. }
  224. // UserManager::delete_user();
  225. $this->moveFile($file);
  226. }
  227. }
  228. $this->moveFile($file);
  229. }
  230. /**
  231. * @param string $file
  232. */
  233. function importCourses($file)
  234. {
  235. $data = Import::csv_to_array($file);
  236. $this->logger->addInfo("Reading file: $file");
  237. if (!empty($data)) {
  238. foreach ($data as $row) {
  239. $courseInfo = api_get_course_info($row['course_code']);
  240. if (empty($courseInfo)) {
  241. // Create
  242. $params = array();
  243. $params['title'] = $row['title'];
  244. $params['exemplary_content'] = false;
  245. $params['wanted_code'] = $row['course_code'];
  246. $params['category_code'] = $row['category_code'];
  247. $params['course_language'] = $row['language'];
  248. //$params['gradebook_model_id'] = isset($course_values['gradebook_model_id']) ? $course_values['gradebook_model_id'] : null;
  249. $courseInfo = CourseManager::create_course($params);
  250. if (!empty($courseInfo)) {
  251. $this->logger->addInfo("Courses - Course created ".$courseInfo['code']);
  252. } else {
  253. $this->logger->addError("Courses - Can't create course:".$row['title']);
  254. }
  255. } else {
  256. // Update
  257. $params = array(
  258. 'title' => $row['title'],
  259. );
  260. $result = CourseManager::update_attributes($courseInfo['id'], $params);
  261. if ($result) {
  262. $this->logger->addInfo("Courses - Course updated ".$courseInfo['code']);
  263. } else {
  264. $this->logger->addError("Courses - Course NOT updated ".$courseInfo['code']);
  265. }
  266. /*course_language='".Database::escape_string($course_language)."',
  267. title='".Database::escape_string($title)."',
  268. category_code='".Database::escape_string($category_code)."',
  269. tutor_name='".Database::escape_string($tutor_name)."',
  270. visual_code='".Database::escape_string($visual_code)."',
  271. department_name='".Database::escape_string($department_name)."',
  272. department_url='".Database::escape_string($department_url)."',
  273. disk_quota='".Database::escape_string($disk_quota)."',
  274. visibility = '".Database::escape_string($visibility)."',
  275. subscribe = '".Database::escape_string($subscribe)."',
  276. unsubscribe='*/
  277. }
  278. }
  279. }
  280. $this->moveFile($file);
  281. }
  282. /**
  283. * @param string $file
  284. */
  285. function importSessions($file)
  286. {
  287. $result = SessionManager::importCSV($file, true, 1, $this->logger);
  288. if (!empty($result['error_message'])) {
  289. $this->logger->addError($result['error_message']);
  290. }
  291. $this->logger->addInfo("Sessions - Sessions parsed: ".$result['session_counter']);
  292. $this->moveFile($file);
  293. }
  294. }
  295. use Monolog\Logger;
  296. use Monolog\Handler\StreamHandler;
  297. use Monolog\Handler\NativeMailerHandler;
  298. use Monolog\Handler\RotatingFileHandler;
  299. //use Monolog\Handler\SwiftMailerHandler;
  300. //require_once api_get_path(LIBRARY_PATH).'swiftmailer/lib/swift_required.php';
  301. $logger = new Logger('cron');
  302. $emails = isset($_configuration['cron_notification_mails']) ? $_configuration['cron_notification_mails'] : null;
  303. $minLevel = Logger::DEBUG;
  304. if (!is_array($emails)) {
  305. $emails = array($emails);
  306. }
  307. $subject = "Cron main/cron/import_csv.php ".date('Y-m-d h:i:s');
  308. $from = api_get_setting('emailAdministrator');
  309. if (!empty($emails)) {
  310. foreach ($emails as $email) {
  311. $logger->pushHandler(new NativeMailerHandler($email, $subject, $from, $minLevel));
  312. }
  313. }
  314. $logger->pushHandler(new StreamHandler(api_get_path(SYS_ARCHIVE_PATH).'import_csv.log', $minLevel));
  315. $logger->pushHandler(new RotatingFileHandler('import_csv', 5, $minLevel));
  316. $import = new ImportCsv($logger);
  317. $import->run();