import_csv.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. if (PHP_SAPI !='cli') {
  4. die('Run this script through the command line or comment this line in the code');
  5. }
  6. if (file_exists('multiple_url_fix.php')) {
  7. require 'multiple_url_fix.php';
  8. }
  9. //require_once __DIR__.'/../inc/global.inc.php';
  10. //require_once api_get_path(LIBRARY_PATH).'log.class.php';
  11. /**
  12. * Class ImportCsv
  13. */
  14. class ImportCsv
  15. {
  16. private $logger;
  17. private $dumpValues;
  18. public $test;
  19. public $defaultLanguage = 'dutch';
  20. public $extraFieldIdNameList = array(
  21. 'session' => 'external_session_id',
  22. 'course' => 'external_course_id',
  23. 'user' => 'external_user_id',
  24. );
  25. public $defaultAdminId = 1;
  26. public $defaultSessionVisibility = 1;
  27. /**
  28. * When creating a user the expiration date is set to registration date + this value
  29. * @var int number of years
  30. */
  31. public $expirationDateInUserCreation = 1;
  32. /**
  33. * When updating a user the expiration date is set to update date + this value
  34. * @var int number of years
  35. */
  36. public $expirationDateInUserUpdate = 1;
  37. public $daysCoachAccessBeforeBeginning = 30;
  38. public $daysCoachAccessAfterBeginning = 60;
  39. public $conditions;
  40. /**
  41. * @param Logger $logger
  42. * @param array
  43. */
  44. public function __construct($logger, $conditions)
  45. {
  46. $this->logger = $logger;
  47. $this->conditions = $conditions;
  48. }
  49. /**
  50. * @param bool $dump
  51. */
  52. public function setDumpValues($dump)
  53. {
  54. $this->dumpValues = $dump;
  55. }
  56. /**
  57. * @return mixed
  58. */
  59. public function getDumpValues()
  60. {
  61. return $this->dumpValues;
  62. }
  63. /**
  64. * Runs the import process
  65. */
  66. public function run()
  67. {
  68. $path = api_get_path(SYS_CODE_PATH).'cron/incoming/';
  69. if (!is_dir($path)) {
  70. echo "The folder! $path does not exits";
  71. return 0;
  72. }
  73. if ($this->getDumpValues()) {
  74. $this->dumpDatabaseTables();
  75. }
  76. echo "Reading files: ".PHP_EOL.PHP_EOL;
  77. $files = scandir($path);
  78. $fileToProcess = array();
  79. $fileToProcessStatic = array();
  80. if (!empty($files)) {
  81. foreach ($files as $file) {
  82. $fileInfo = pathinfo($file);
  83. if ($fileInfo['extension'] == 'csv') {
  84. // Checking teachers_yyyymmdd.csv, courses_yyyymmdd.csv, students_yyyymmdd.csv and sessions_yyyymmdd.csv
  85. $parts = explode('_', $fileInfo['filename']);
  86. $preMethod = ucwords($parts[1]);
  87. $preMethod = str_replace('-static', 'Static', $preMethod);
  88. $method = 'import'.$preMethod;
  89. $isStatic = strpos($method, 'Static');
  90. if (method_exists($this, $method)) {
  91. if ($method == 'importUnsubscribeStatic' || empty($isStatic)) {
  92. $fileToProcess[$parts[1]][] = array(
  93. 'method' => $method,
  94. 'file' => $path.$fileInfo['basename']
  95. );
  96. } else {
  97. $fileToProcessStatic[$parts[1]][] = array(
  98. 'method' => $method,
  99. 'file' => $path.$fileInfo['basename']
  100. );
  101. }
  102. } else {
  103. echo "Error - This file '$file' can't be processed.".PHP_EOL;
  104. echo "Trying to call $method".PHP_EOL;
  105. echo "The file have to has this format:".PHP_EOL;
  106. echo "prefix_students_ddmmyyyy.csv, prefix_teachers_ddmmyyyy.csv, prefix_courses_ddmmyyyy.csv, prefix_sessions_ddmmyyyy.csv ".PHP_EOL;
  107. exit;
  108. }
  109. }
  110. }
  111. if (empty($fileToProcess)) {
  112. echo 'Error - no files to process.';
  113. return 0;
  114. }
  115. $this->prepareImport();
  116. $sections = array('students', 'teachers', 'courses', 'sessions', 'unsubscribe-static');
  117. foreach ($sections as $section) {
  118. $this->logger->addInfo("-- Import $section --");
  119. if (isset($fileToProcess[$section]) && !empty($fileToProcess[$section])) {
  120. $files = $fileToProcess[$section];
  121. foreach ($files as $fileInfo) {
  122. $method = $fileInfo['method'];
  123. $file = $fileInfo['file'];
  124. echo 'File: '.$file.PHP_EOL;
  125. $this->logger->addInfo("Reading file: $file");
  126. $this->$method($file, true);
  127. }
  128. }
  129. }
  130. $sections = array('students-static', 'teachers-static', 'courses-static', 'sessions-static');
  131. foreach ($sections as $section) {
  132. $this->logger->addInfo("-- Import static files $section --");
  133. if (isset($fileToProcessStatic[$section]) && !empty($fileToProcessStatic[$section])) {
  134. $files = $fileToProcessStatic[$section];
  135. foreach ($files as $fileInfo) {
  136. $method = $fileInfo['method'];
  137. $file = $fileInfo['file'];
  138. echo 'Static file: '.$file.PHP_EOL;
  139. $this->logger->addInfo("Reading static file: $file");
  140. $this->$method($file, true);
  141. }
  142. }
  143. }
  144. }
  145. }
  146. /**
  147. * Prepares extra fields before the import
  148. */
  149. private function prepareImport()
  150. {
  151. // Create user extra field: extra_external_user_id
  152. UserManager::create_extra_field($this->extraFieldIdNameList['user'], 1, 'External user id', null);
  153. // Create course extra field: extra_external_course_id
  154. CourseManager::create_course_extra_field($this->extraFieldIdNameList['course'], 1, 'External course id');
  155. // Create session extra field extra_external_session_id
  156. SessionManager::create_session_extra_field($this->extraFieldIdNameList['session'], 1, 'External session id');
  157. }
  158. /**
  159. * @param string $file
  160. */
  161. private function moveFile($file)
  162. {
  163. $moved = str_replace('incoming', 'treated', $file);
  164. if ($this->test) {
  165. $result = 1;
  166. } else {
  167. $result = rename($file, $moved);
  168. }
  169. if ($result) {
  170. $this->logger->addInfo("Moving file to the treated folder: $file");
  171. } else {
  172. $this->logger->addError("Error - Cant move file to the treated folder: $file");
  173. }
  174. }
  175. /**
  176. * @param array $row
  177. *
  178. * @return array
  179. */
  180. private function cleanUserRow($row)
  181. {
  182. $row['lastname'] = $row['LastName'];
  183. $row['firstname'] = $row['FirstName'];
  184. $row['email'] = $row['Email'];
  185. $row['username'] = $row['UserName'];
  186. $row['password'] = $row['Password'];
  187. $row['auth_source'] = $row['AuthSource'];
  188. $row['official_code'] = $row['OfficialCode'];
  189. $row['phone'] = $row['PhoneNumber'];
  190. if (isset($row['StudentID'])) {
  191. $row['extra_'.$this->extraFieldIdNameList['user']] = $row['StudentID'];
  192. }
  193. if (isset($row['TeacherID'])) {
  194. $row['extra_'.$this->extraFieldIdNameList['user']] = $row['TeacherID'];
  195. }
  196. return $row;
  197. }
  198. /**
  199. * @param array $row
  200. * @return array
  201. */
  202. private function cleanCourseRow($row)
  203. {
  204. $row['title'] = $row['Title'];
  205. $row['course_code'] = $row['Code'];
  206. $row['course_category'] = $row['CourseCategory'];
  207. $row['email'] = $row['Teacher'];
  208. $row['language'] = $row['Language'];
  209. $row['teachers'] = array();
  210. if (isset($row['Teacher']) && !empty($row['Teacher'])) {
  211. $teachers = explode(',', $row['Teacher']);
  212. if (!empty($teachers)) {
  213. foreach ($teachers as $teacherUserName) {
  214. $teacherUserName = trim($teacherUserName);
  215. $userInfo = api_get_user_info_from_username($teacherUserName);
  216. if (!empty($userInfo)) {
  217. $row['teachers'][] = $userInfo['user_id'];
  218. }
  219. }
  220. }
  221. }
  222. if (isset($row['CourseID'])) {
  223. $row['extra_'.$this->extraFieldIdNameList['course']] = $row['CourseID'];
  224. }
  225. return $row;
  226. }
  227. /**
  228. * File to import
  229. * @param string $file
  230. */
  231. private function importTeachersStatic($file)
  232. {
  233. $this->importTeachers($file, false);
  234. }
  235. /**
  236. * File to import
  237. * @param string $file
  238. * @param bool $moveFile
  239. */
  240. private function importTeachers($file, $moveFile = true)
  241. {
  242. $data = Import::csv_to_array($file);
  243. /* Unique identifier: official-code username.
  244. Email address and password should never get updated. *ok
  245. The only fields that I can think of that should update if the data changes in the csv file are FirstName and LastName. *ok
  246. A slight edit of these fields should be taken into account. ???
  247. Adding teachers is no problem, but deleting them shouldn’t be automated, but we should get a log of “to delete teachers”.
  248. We’ll handle that manually if applicable.
  249. No delete!
  250. */
  251. $language = $this->defaultLanguage;
  252. if (!empty($data)) {
  253. $this->logger->addInfo(count($data)." records found.");
  254. foreach ($data as $row) {
  255. $row = $this->cleanUserRow($row);
  256. $user_id = UserManager::get_user_id_from_original_id($row['extra_'.$this->extraFieldIdNameList['user']], $this->extraFieldIdNameList['user']);
  257. $userInfo = array();
  258. $userInfoByOfficialCode = null;
  259. if (!empty($user_id)) {
  260. $userInfo = api_get_user_info($user_id);
  261. //$userInfo = api_get_user_info_from_username($row['username']);
  262. $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
  263. }
  264. $expirationDate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserCreation)."years"));
  265. if (empty($userInfo) && empty($userInfoByOfficialCode)) {
  266. // Create user
  267. $userId = UserManager::create_user(
  268. $row['firstname'],
  269. $row['lastname'],
  270. COURSEMANAGER,
  271. $row['email'],
  272. $row['username'],
  273. $row['password'],
  274. $row['official_code'],
  275. $language, //$row['language'],
  276. $row['phone'],
  277. null, //$row['picture'], //picture
  278. PLATFORM_AUTH_SOURCE, // ?
  279. $expirationDate, //'0000-00-00 00:00:00', //$row['expiration_date'], //$expiration_date = '0000-00-00 00:00:00',
  280. 1, //active
  281. 0,
  282. null, // extra
  283. null, //$encrypt_method = '',
  284. false //$send_mail = false
  285. );
  286. if ($userId) {
  287. foreach ($row as $key => $value) {
  288. if (substr($key, 0, 6) == 'extra_') { //an extra field
  289. UserManager::update_extra_field_value($userId, substr($key, 6), $value);
  290. }
  291. }
  292. $this->logger->addInfo("Teachers - User created: ".$row['username']);
  293. } else {
  294. $this->logger->addError("Teachers - User NOT created: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  295. }
  296. } else {
  297. if (empty($userInfo)) {
  298. $this->logger->addError("Teachers - Can't update user :".$row['username']);
  299. continue;
  300. }
  301. $expirationDate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserUpdate)."years"));
  302. // Update user
  303. $result = UserManager::update_user(
  304. $userInfo['user_id'],
  305. $row['firstname'], // <<-- changed
  306. $row['lastname'], // <<-- changed
  307. $userInfo['username'],
  308. null, //$password = null,
  309. PLATFORM_AUTH_SOURCE,
  310. $userInfo['email'],
  311. COURSEMANAGER,
  312. $userInfo['official_code'],
  313. $userInfo['phone'],
  314. $userInfo['picture_uri'],
  315. $expirationDate,
  316. $userInfo['active'],
  317. null, //$creator_id = null,
  318. 0, //$hr_dept_id = 0,
  319. null, // $extra = null,
  320. null, //$language = 'english',
  321. null, //$encrypt_method = '',
  322. false, //$send_email = false,
  323. 0 //$reset_password = 0
  324. );
  325. if ($result) {
  326. foreach ($row as $key => $value) {
  327. if (substr($key, 0, 6) == 'extra_') { //an extra field
  328. UserManager::update_extra_field_value($userInfo['user_id'], substr($key, 6), $value);
  329. }
  330. }
  331. $this->logger->addInfo("Teachers - User updated: ".$row['username']);
  332. } else {
  333. $this->logger->addError("Teachers - User not updated: ".$row['username']);
  334. }
  335. }
  336. }
  337. }
  338. if ($moveFile) {
  339. $this->moveFile($file);
  340. }
  341. }
  342. /**
  343. * @param string $file
  344. */
  345. private function importStudentsStatic($file)
  346. {
  347. $this->importStudents($file, false);
  348. }
  349. /**
  350. * @param string $file
  351. * @param bool $moveFile
  352. */
  353. private function importStudents($file, $moveFile = true)
  354. {
  355. $data = Import::csv_to_array($file);
  356. /*
  357. * Another users import.
  358. Unique identifier: official code and username . ok
  359. Password should never get updated. ok
  360. 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.
  361. All other fields should be updateable, though passwords should of course not get updated. ok
  362. If a user gets deleted (not there anymore),
  363. 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.
  364. */
  365. if (!empty($data)) {
  366. $language = $this->defaultLanguage;
  367. $this->logger->addInfo(count($data)." records found.");
  368. foreach ($data as $row) {
  369. $row = $this->cleanUserRow($row);
  370. $user_id = UserManager::get_user_id_from_original_id(
  371. $row['extra_'.$this->extraFieldIdNameList['user']],
  372. $this->extraFieldIdNameList['user']
  373. );
  374. $userInfo = array();
  375. $userInfoByOfficialCode = null;
  376. if (!empty($user_id)) {
  377. $userInfo = api_get_user_info($user_id);
  378. $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
  379. }
  380. $expirationDate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserCreation)."years"));
  381. if (empty($userInfo) && empty($userInfoByOfficialCode)) {
  382. // Create user
  383. $result = UserManager::create_user(
  384. $row['firstname'],
  385. $row['lastname'],
  386. STUDENT,
  387. $row['email'],
  388. $row['username'],
  389. $row['password'],
  390. $row['official_code'],
  391. $language, //$row['language'],
  392. $row['phone'],
  393. null, //$row['picture'], //picture
  394. PLATFORM_AUTH_SOURCE, // ?
  395. $expirationDate, //'0000-00-00 00:00:00', //$row['expiration_date'], //$expiration_date = '0000-00-00 00:00:00',
  396. 1, //active
  397. 0,
  398. null, // extra
  399. null, //$encrypt_method = '',
  400. false //$send_mail = false
  401. );
  402. if ($result) {
  403. foreach ($row as $key => $value) {
  404. if (substr($key, 0, 6) == 'extra_') { //an extra field
  405. UserManager::update_extra_field_value($result, substr($key, 6), $value);
  406. }
  407. }
  408. $this->logger->addInfo("Students - User created: ".$row['username']);
  409. } else {
  410. $this->logger->addError("Students - User NOT created: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  411. }
  412. } else {
  413. if (empty($userInfo)) {
  414. $this->logger->addError("Students - Can't update user :".$row['username']);
  415. continue;
  416. }
  417. if ($row['action'] == 'delete') {
  418. // Inactive one year later
  419. $userInfo['expiration_date'] = api_get_utc_datetime(api_strtotime(time() + 365*24*60*60));
  420. }
  421. $password = $row['password']; // change password
  422. $email = $row['email']; // change email
  423. $resetPassword = 2; // allow password change
  424. // Conditions that disables the update of password and email:
  425. if (isset($this->conditions['importStudents'])) {
  426. if (isset($this->conditions['importStudents']['update']) && isset($this->conditions['importStudents']['update']['avoid'])) {
  427. // Blocking email update -
  428. // 1. Condition
  429. $avoidUsersWithEmail = $this->conditions['importStudents']['update']['avoid']['email'];
  430. if ($userInfo['email'] != $row['email'] && in_array($row['email'], $avoidUsersWithEmail)) {
  431. $this->logger->addInfo("Students - User email is not updated : ".$row['username']." because the avoid conditions (email).");
  432. // Do not change email keep the old email.
  433. $email = $userInfo['email'];
  434. }
  435. // 2. Condition
  436. if (!in_array($userInfo['email'], $avoidUsersWithEmail) && !in_array($row['email'], $avoidUsersWithEmail)) {
  437. $email = $userInfo['email'];
  438. }
  439. // 3. Condition
  440. if (in_array($userInfo['email'], $avoidUsersWithEmail) && !in_array($row['email'], $avoidUsersWithEmail)) {
  441. $email = $row['email'];
  442. }
  443. // Blocking password update
  444. $avoidUsersWithPassword = $this->conditions['importStudents']['update']['avoid']['password'];
  445. if ($userInfo['password'] != api_get_encrypted_password($row['password']) && in_array($row['password'], $avoidUsersWithPassword)) {
  446. $this->logger->addInfo("Students - User password is not updated: ".$row['username']." because the avoid conditions (password).");
  447. $password = null;
  448. $resetPassword = 0; // disallow password change
  449. }
  450. }
  451. }
  452. $expirationDate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserUpdate)."years"));
  453. // Update user
  454. $result = UserManager::update_user(
  455. $userInfo['user_id'],
  456. $row['firstname'], // <<-- changed
  457. $row['lastname'], // <<-- changed
  458. $row['username'], // <<-- changed
  459. $password, //$password = null,
  460. PLATFORM_AUTH_SOURCE,
  461. $email,
  462. STUDENT,
  463. $userInfo['official_code'],
  464. $userInfo['phone'],
  465. $userInfo['picture_uri'],
  466. $expirationDate,
  467. $userInfo['active'],
  468. null, //$creator_id = null,
  469. 0, //$hr_dept_id = 0,
  470. null, // $extra = null,
  471. null, //$language = 'english',
  472. null, //$encrypt_method = '',
  473. false, //$send_email = false,
  474. $resetPassword //$reset_password = 0
  475. );
  476. if ($result) {
  477. if ($row['username'] != $userInfo['username']) {
  478. $this->logger->addInfo("Students - Username was changes from '".$userInfo['username']."' to '".$row['username']."' ");
  479. }
  480. foreach ($row as $key => $value) {
  481. if (substr($key, 0, 6) == 'extra_') { //an extra field
  482. UserManager::update_extra_field_value($userInfo['user_id'], substr($key, 6), $value);
  483. }
  484. }
  485. $this->logger->addInfo("Students - User updated: ".$row['username']);
  486. } else {
  487. $this->logger->addError("Students - User NOT updated: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  488. }
  489. }
  490. }
  491. }
  492. if ($moveFile) {
  493. $this->moveFile($file);
  494. }
  495. }
  496. /**
  497. * @param string $file
  498. */
  499. private function importCoursesStatic($file)
  500. {
  501. $this->importCourses($file, false);
  502. }
  503. /**
  504. * @param string $file
  505. * @param bool $moveFile
  506. */
  507. private function importCourses($file, $moveFile = true)
  508. {
  509. $data = Import::csv_to_array($file);
  510. //$language = $this->defaultLanguage;
  511. if (!empty($data)) {
  512. $this->logger->addInfo(count($data)." records found.");
  513. foreach ($data as $row) {
  514. $row = $this->cleanCourseRow($row);
  515. $courseCode = CourseManager::get_course_id_from_original_id($row['extra_'.$this->extraFieldIdNameList['course']], $this->extraFieldIdNameList['course']);
  516. $courseInfo = api_get_course_info($courseCode);
  517. if (empty($courseInfo)) {
  518. // Create
  519. $params = array();
  520. $params['title'] = $row['title'];
  521. $params['exemplary_content'] = false;
  522. $params['wanted_code'] = $row['course_code'];
  523. $params['course_category'] = $row['course_category'];
  524. $params['course_language'] = $row['language'];
  525. $params['teachers'] = $row['teachers'];
  526. $courseInfo = CourseManager::create_course($params);
  527. if (!empty($courseInfo)) {
  528. CourseManager::update_course_extra_field_value($courseInfo['code'], 'external_course_id', $row['extra_'.$this->extraFieldIdNameList['course']]);
  529. $this->logger->addInfo("Courses - Course created ".$courseInfo['code']);
  530. } else {
  531. $this->logger->addError("Courses - Can't create course:".$row['title']);
  532. }
  533. } else {
  534. // Update
  535. $params = array(
  536. 'title' => $row['title'],
  537. );
  538. $result = CourseManager::update_attributes($courseInfo['real_id'], $params);
  539. $addTeacherToSession = isset($courseInfo['add_teachers_to_sessions_courses']) && !empty($courseInfo['add_teachers_to_sessions_courses']) ? true : false;
  540. if ($addTeacherToSession) {
  541. CourseManager::updateTeachers($courseInfo['id'], $row['teachers'], false, true, false);
  542. } else {
  543. CourseManager::updateTeachers($courseInfo['id'], $row['teachers'], false, false);
  544. }
  545. if ($result) {
  546. $this->logger->addInfo("Courses - Course updated ".$courseInfo['code']);
  547. } else {
  548. $this->logger->addError("Courses - Course NOT updated ".$courseInfo['code']);
  549. }
  550. }
  551. }
  552. }
  553. if ($moveFile) {
  554. $this->moveFile($file);
  555. }
  556. }
  557. /**
  558. * @param string $file
  559. */
  560. private function importSessionsStatic($file)
  561. {
  562. $this->importSessions($file, false);
  563. }
  564. /**
  565. * @param string $file
  566. * @param bool $moveFile
  567. */
  568. private function importSessions($file, $moveFile = true)
  569. {
  570. $avoid = null;
  571. if (isset($this->conditions['importSessions']) && isset($this->conditions['importSessions']['update'])) {
  572. $avoid = $this->conditions['importSessions']['update'];
  573. }
  574. $result = SessionManager::importCSV(
  575. $file,
  576. true,
  577. $this->defaultAdminId,
  578. $this->logger,
  579. array('SessionID' => 'extra_'.$this->extraFieldIdNameList['session']),
  580. $this->extraFieldIdNameList['session'],
  581. $this->daysCoachAccessBeforeBeginning,
  582. $this->daysCoachAccessAfterBeginning,
  583. $this->defaultSessionVisibility,
  584. $avoid,
  585. false,
  586. false,
  587. true
  588. );
  589. if (!empty($result['error_message'])) {
  590. $this->logger->addError($result['error_message']);
  591. }
  592. $this->logger->addInfo("Sessions - Sessions parsed: ".$result['session_counter']);
  593. if ($moveFile) {
  594. $this->moveFile($file);
  595. }
  596. }
  597. /**
  598. * @param string $file
  599. */
  600. private function importUnsubscribeStatic($file)
  601. {
  602. $data = Import::csv_reader($file);
  603. if (!empty($data)) {
  604. $this->logger->addInfo(count($data)." records found.");
  605. foreach ($data as $row) {
  606. $chamiloUserName = $row['UserName'];
  607. $chamiloCourseCode = $row['CourseCode'];
  608. $chamiloSessionId = $row['SessionID'];
  609. $sessionInfo = api_get_session_info($chamiloSessionId);
  610. if (empty($sessionInfo)) {
  611. $this->logger->addError('Session does not exists: '.$chamiloSessionId);
  612. continue;
  613. }
  614. $courseInfo = api_get_course_info($chamiloCourseCode);
  615. if (empty($courseInfo)) {
  616. $this->logger->addError('Course does not exists: '.$courseInfo);
  617. continue;
  618. }
  619. $userId = Usermanager::get_user_id_from_username($chamiloUserName);
  620. if (empty($userId)) {
  621. $this->logger->addError('User does not exists: '.$chamiloUserName);
  622. continue;
  623. }
  624. CourseManager::unsubscribe_user($userId, $courseInfo['code'], $chamiloSessionId);
  625. $this->logger->addError("User '$chamiloUserName' was removed from session: #$chamiloSessionId, Course: ".$courseInfo['code']);
  626. }
  627. }
  628. }
  629. /**
  630. * Dump database tables
  631. */
  632. private function dumpDatabaseTables()
  633. {
  634. echo 'Dumping tables'.PHP_EOL;
  635. // User
  636. $table = Database::get_main_table(TABLE_MAIN_USER);
  637. $tableAdmin = Database::get_main_table(TABLE_MAIN_ADMIN);
  638. //$sql = "DELETE FROM $table WHERE username NOT IN ('admin') AND lastname <> 'Anonymous' ";
  639. $sql = "DELETE FROM $table WHERE user_id not in (select user_id from $tableAdmin) and status <> ".ANONYMOUS;
  640. Database::query($sql);
  641. echo $sql.PHP_EOL;
  642. // Course
  643. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  644. $sql = "DELETE FROM $table";
  645. Database::query($sql);
  646. echo $sql.PHP_EOL;
  647. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  648. $sql = "DELETE FROM $table";
  649. Database::query($sql);
  650. echo $sql.PHP_EOL;
  651. $table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  652. $sql = "DELETE FROM $table";
  653. Database::query($sql);
  654. echo $sql.PHP_EOL;
  655. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  656. $sql = "DELETE FROM $table";
  657. Database::query($sql);
  658. echo $sql.PHP_EOL;
  659. // Sessions
  660. $table = Database::get_main_table(TABLE_MAIN_SESSION);
  661. $sql = "DELETE FROM $table";
  662. Database::query($sql);
  663. echo $sql.PHP_EOL;
  664. $table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  665. $sql = "DELETE FROM $table";
  666. Database::query($sql);
  667. echo $sql.PHP_EOL;
  668. $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  669. $sql = "DELETE FROM $table";
  670. Database::query($sql);
  671. echo $sql.PHP_EOL;
  672. $table = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  673. $sql = "DELETE FROM $table";
  674. Database::query($sql);
  675. echo $sql.PHP_EOL;
  676. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  677. $sql = "DELETE FROM $table";
  678. Database::query($sql);
  679. echo $sql.PHP_EOL;
  680. // Extra fields
  681. $table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  682. $sql = "DELETE FROM $table";
  683. Database::query($sql);
  684. echo $sql.PHP_EOL;
  685. $table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  686. $sql = "DELETE FROM $table";
  687. Database::query($sql);
  688. echo $sql.PHP_EOL;
  689. $table = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  690. $sql = "DELETE FROM $table";
  691. Database::query($sql);
  692. echo $sql.PHP_EOL;
  693. }
  694. }
  695. use Monolog\Logger;
  696. use Monolog\Handler\StreamHandler;
  697. use Monolog\Handler\NativeMailerHandler;
  698. use Monolog\Handler\RotatingFileHandler;
  699. use Monolog\Handler\BufferHandler;
  700. $logger = new Logger('cron');
  701. $emails = isset($_configuration['cron_notification_mails']) ? $_configuration['cron_notification_mails'] : null;
  702. $minLevel = Logger::DEBUG;
  703. if (!is_array($emails)) {
  704. $emails = array($emails);
  705. }
  706. $subject = "Cron main/cron/import_csv.php ".date('Y-m-d h:i:s');
  707. $from = api_get_setting('platform.administrator_email');
  708. /*
  709. if (!empty($emails)) {
  710. foreach ($emails as $email) {
  711. $stream = new NativeMailerHandler($email, $subject, $from, $minLevel);
  712. $logger->pushHandler(new BufferHandler($stream, 0, $minLevel));
  713. }
  714. }*/
  715. $stream = new StreamHandler(api_get_path(SYS_ARCHIVE_PATH).'import_csv.log', $minLevel);
  716. $logger->pushHandler(new BufferHandler($stream, 0, $minLevel));
  717. $logger->pushHandler(new RotatingFileHandler('import_csv', 5, $minLevel));
  718. $cronImportCSVConditions = isset($_configuration['cron_import_csv_conditions']) ? $_configuration['cron_import_csv_conditions'] : null;
  719. $import = new ImportCsv($logger, $cronImportCSVConditions);
  720. if (isset($_configuration['default_admin_user_id_for_cron'])) {
  721. $import->defaultAdminId = $_configuration['default_admin_user_id_for_cron'];
  722. }
  723. // @todo in production disable the dump option
  724. $dump = false;
  725. if (isset($argv[1]) && $argv[1] = '--dump') {
  726. $dump = true;
  727. }
  728. if (isset($_configuration['import_csv_disable_dump']) && $_configuration['import_csv_disable_dump'] == true) {
  729. $import->setDumpValues(false);
  730. } else {
  731. $import->setDumpValues($dump);
  732. }
  733. // Do not moves the files to treated
  734. if (isset($_configuration['import_csv_test'])) {
  735. $import->test = $_configuration['import_csv_test'];
  736. } else {
  737. $import->test = true;
  738. }
  739. $import->run();
  740. if (isset($_configuration['import_csv_fix_permissions']) && $_configuration['import_csv_fix_permissions'] == true) {
  741. $command = "sudo find ".api_get_path(SYS_COURSE_PATH)." -type d -exec chmod 777 {} \; ";
  742. echo "Executing: ".$command.PHP_EOL;
  743. system($command);
  744. $command = "sudo find ".api_get_path(SYS_CODE_PATH)."upload/users -type d -exec chmod 777 {} \;";
  745. echo "Executing: ".$command.PHP_EOL;
  746. system($command);
  747. }