import_csv.php 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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. ini_set('memory_limit', -1);
  12. ini_set('max_execution_time', 0);
  13. /**
  14. * Class ImportCsv
  15. */
  16. class ImportCsv
  17. {
  18. private $logger;
  19. private $dumpValues;
  20. public $test;
  21. public $defaultLanguage = 'dutch';
  22. public $extraFieldIdNameList = array(
  23. 'session' => 'external_session_id',
  24. 'course' => 'external_course_id',
  25. 'user' => 'external_user_id',
  26. 'calendar_event' => 'external_calendar_event_id'
  27. );
  28. public $defaultAdminId = 1;
  29. public $defaultSessionVisibility = 1;
  30. /**
  31. * When creating a user the expiration date is set to registration date + this value
  32. * @var int number of years
  33. */
  34. public $expirationDateInUserCreation = 1;
  35. /**
  36. * When updating a user the expiration date is set to update date + this value
  37. * @var int number of years
  38. */
  39. public $expirationDateInUserUpdate = 1;
  40. public $daysCoachAccessBeforeBeginning = 14;
  41. public $daysCoachAccessAfterBeginning = 14;
  42. public $conditions;
  43. /**
  44. * @param Logger $logger
  45. * @param array
  46. */
  47. public function __construct($logger, $conditions)
  48. {
  49. $this->logger = $logger;
  50. $this->conditions = $conditions;
  51. }
  52. /**
  53. * @param bool $dump
  54. */
  55. public function setDumpValues($dump)
  56. {
  57. $this->dumpValues = $dump;
  58. }
  59. /**
  60. * @return mixed
  61. */
  62. public function getDumpValues()
  63. {
  64. return $this->dumpValues;
  65. }
  66. /**
  67. * Runs the import process
  68. */
  69. public function run()
  70. {
  71. $path = api_get_path(SYS_CODE_PATH).'cron/incoming/';
  72. if (!is_dir($path)) {
  73. echo "The folder! $path does not exits";
  74. return 0;
  75. }
  76. if ($this->getDumpValues()) {
  77. $this->dumpDatabaseTables();
  78. }
  79. echo "Reading files: ".PHP_EOL.PHP_EOL;
  80. $files = scandir($path);
  81. $fileToProcess = array();
  82. $fileToProcessStatic = array();
  83. if (!empty($files)) {
  84. foreach ($files as $file) {
  85. $fileInfo = pathinfo($file);
  86. if ($fileInfo['extension'] == 'csv') {
  87. // Checking teachers_yyyymmdd.csv, courses_yyyymmdd.csv, students_yyyymmdd.csv and sessions_yyyymmdd.csv
  88. $parts = explode('_', $fileInfo['filename']);
  89. $preMethod = ucwords($parts[1]);
  90. $preMethod = str_replace('-static', 'Static', $preMethod);
  91. $method = 'import'.$preMethod;
  92. $isStatic = strpos($method, 'Static');
  93. if (method_exists($this, $method)) {
  94. if (($method == 'importUnsubscribeStatic' ||
  95. $method == 'importSubscribeStatic') ||
  96. empty($isStatic)
  97. ) {
  98. $fileToProcess[$parts[1]][] = array(
  99. 'method' => $method,
  100. 'file' => $path.$fileInfo['basename']
  101. );
  102. } else {
  103. $fileToProcessStatic[$parts[1]][] = array(
  104. 'method' => $method,
  105. 'file' => $path.$fileInfo['basename']
  106. );
  107. }
  108. } else {
  109. echo "Error - This file '$file' can't be processed.".PHP_EOL;
  110. echo "Trying to call $method".PHP_EOL;
  111. echo "The file have to has this format:".PHP_EOL;
  112. echo "prefix_students_ddmmyyyy.csv, prefix_teachers_ddmmyyyy.csv, prefix_courses_ddmmyyyy.csv, prefix_sessions_ddmmyyyy.csv ".PHP_EOL;
  113. exit;
  114. }
  115. }
  116. }
  117. if (empty($fileToProcess) && empty($fileToProcessStatic)) {
  118. echo 'Error - no files to process.';
  119. return 0;
  120. }
  121. $this->prepareImport();
  122. $sections = array(
  123. 'students',
  124. 'teachers',
  125. 'courses',
  126. 'sessions',
  127. 'subscribe-static',
  128. 'unsubscribe-static'
  129. );
  130. foreach ($sections as $section) {
  131. $this->logger->addInfo("-- Import $section --");
  132. if (isset($fileToProcess[$section]) && !empty($fileToProcess[$section])) {
  133. $files = $fileToProcess[$section];
  134. foreach ($files as $fileInfo) {
  135. $method = $fileInfo['method'];
  136. $file = $fileInfo['file'];
  137. echo 'File: '.$file.PHP_EOL;
  138. $this->logger->addInfo("Reading file: $file");
  139. $this->$method($file, true);
  140. }
  141. }
  142. }
  143. $sections = array(
  144. 'students-static',
  145. 'teachers-static',
  146. 'courses-static',
  147. 'sessions-static',
  148. 'calendar-static',
  149. );
  150. foreach ($sections as $section) {
  151. $this->logger->addInfo("-- Import static files $section --");
  152. if (isset($fileToProcessStatic[$section]) &&
  153. !empty($fileToProcessStatic[$section])
  154. ) {
  155. $files = $fileToProcessStatic[$section];
  156. foreach ($files as $fileInfo) {
  157. $method = $fileInfo['method'];
  158. $file = $fileInfo['file'];
  159. echo 'Static file: '.$file.PHP_EOL;
  160. $this->logger->addInfo("Reading static file: $file");
  161. $this->$method($file, true);
  162. }
  163. }
  164. }
  165. }
  166. }
  167. /**
  168. * Prepares extra fields before the import
  169. */
  170. private function prepareImport()
  171. {
  172. // Create user extra field: extra_external_user_id
  173. UserManager::create_extra_field(
  174. $this->extraFieldIdNameList['user'],
  175. 1,
  176. 'External user id',
  177. null
  178. );
  179. // Create course extra field: extra_external_course_id
  180. CourseManager::create_course_extra_field(
  181. $this->extraFieldIdNameList['course'],
  182. 1,
  183. 'External course id'
  184. );
  185. // Create session extra field extra_external_session_id
  186. SessionManager::create_session_extra_field(
  187. $this->extraFieldIdNameList['session'],
  188. 1,
  189. 'External session id'
  190. );
  191. // Create calendar_event extra field extra_external_session_id
  192. $extraField = new ExtraField('calendar_event');
  193. $extraField->save(array(
  194. 'field_type' => ExtraField::FIELD_TYPE_TEXT,
  195. 'field_variable' => $this->extraFieldIdNameList['calendar_event'],
  196. 'field_display_text' => 'External calendar event id'
  197. ));
  198. }
  199. /**
  200. * @param string $file
  201. */
  202. private function moveFile($file)
  203. {
  204. $moved = str_replace('incoming', 'treated', $file);
  205. if ($this->test) {
  206. $result = 1;
  207. } else {
  208. $result = rename($file, $moved);
  209. }
  210. if ($result) {
  211. $this->logger->addInfo("Moving file to the treated folder: $file");
  212. } else {
  213. $this->logger->addError("Error - Cant move file to the treated folder: $file");
  214. }
  215. }
  216. /**
  217. * @param array $row
  218. *
  219. * @return array
  220. */
  221. private function cleanUserRow($row)
  222. {
  223. $row['lastname'] = $row['LastName'];
  224. $row['firstname'] = $row['FirstName'];
  225. $row['email'] = $row['Email'];
  226. $row['username'] = $row['UserName'];
  227. $row['password'] = $row['Password'];
  228. $row['auth_source'] = $row['AuthSource'];
  229. $row['official_code'] = $row['OfficialCode'];
  230. $row['phone'] = $row['PhoneNumber'];
  231. if (isset($row['StudentID'])) {
  232. $row['extra_'.$this->extraFieldIdNameList['user']] = $row['StudentID'];
  233. }
  234. if (isset($row['TeacherID'])) {
  235. $row['extra_'.$this->extraFieldIdNameList['user']] = $row['TeacherID'];
  236. }
  237. return $row;
  238. }
  239. /**
  240. * @param array $row
  241. *
  242. * @return array
  243. */
  244. private function cleanCourseRow($row)
  245. {
  246. $row['title'] = $row['Title'];
  247. $row['course_code'] = $row['Code'];
  248. $row['course_category'] = $row['CourseCategory'];
  249. $row['email'] = $row['Teacher'];
  250. $row['language'] = $row['Language'];
  251. $row['teachers'] = array();
  252. if (isset($row['Teacher']) && !empty($row['Teacher'])) {
  253. $teachers = explode(',', $row['Teacher']);
  254. if (!empty($teachers)) {
  255. foreach ($teachers as $teacherUserName) {
  256. $teacherUserName = trim($teacherUserName);
  257. $userInfo = api_get_user_info_from_username($teacherUserName);
  258. if (!empty($userInfo)) {
  259. $row['teachers'][] = $userInfo['user_id'];
  260. }
  261. }
  262. }
  263. }
  264. if (isset($row['CourseID'])) {
  265. $row['extra_'.$this->extraFieldIdNameList['course']] = $row['CourseID'];
  266. }
  267. return $row;
  268. }
  269. /**
  270. * File to import
  271. * @param string $file
  272. */
  273. private function importTeachersStatic($file)
  274. {
  275. $this->importTeachers($file, false);
  276. }
  277. /**
  278. * File to import
  279. * @param string $file
  280. * @param bool $moveFile
  281. */
  282. private function importTeachers($file, $moveFile = true)
  283. {
  284. $data = Import::csv_to_array($file);
  285. /* Unique identifier: official-code username.
  286. Email address and password should never get updated. *ok
  287. The only fields that I can think of that should update if the data changes in the csv file are FirstName and LastName. *ok
  288. A slight edit of these fields should be taken into account. ???
  289. Adding teachers is no problem, but deleting them shouldn’t be automated, but we should get a log of “to delete teachers”.
  290. We’ll handle that manually if applicable.
  291. No delete!
  292. */
  293. $language = $this->defaultLanguage;
  294. if (!empty($data)) {
  295. $this->logger->addInfo(count($data)." records found.");
  296. foreach ($data as $row) {
  297. $row = $this->cleanUserRow($row);
  298. $user_id = UserManager::get_user_id_from_original_id(
  299. $row['extra_' . $this->extraFieldIdNameList['user']],
  300. $this->extraFieldIdNameList['user']
  301. );
  302. $userInfo = array();
  303. $userInfoByOfficialCode = null;
  304. if (!empty($user_id)) {
  305. $userInfo = api_get_user_info($user_id);
  306. $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
  307. }
  308. $expirationDate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserCreation)."years"));
  309. if (empty($userInfo) && empty($userInfoByOfficialCode)) {
  310. // Create user
  311. $userId = UserManager::create_user(
  312. $row['firstname'],
  313. $row['lastname'],
  314. COURSEMANAGER,
  315. $row['email'],
  316. $row['username'],
  317. $row['password'],
  318. $row['official_code'],
  319. $language, //$row['language'],
  320. $row['phone'],
  321. null, //$row['picture'], //picture
  322. PLATFORM_AUTH_SOURCE, // ?
  323. $expirationDate, //'0000-00-00 00:00:00', //$row['expiration_date'], //$expiration_date = '0000-00-00 00:00:00',
  324. 1, //active
  325. 0,
  326. null, // extra
  327. null, //$encrypt_method = '',
  328. false //$send_mail = false
  329. );
  330. if ($userId) {
  331. foreach ($row as $key => $value) {
  332. if (substr($key, 0, 6) == 'extra_') {
  333. //an extra field
  334. UserManager::update_extra_field_value($userId, substr($key, 6), $value);
  335. }
  336. }
  337. $this->logger->addInfo("Teachers - User created: ".$row['username']);
  338. } else {
  339. $this->logger->addError("Teachers - User NOT created: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  340. }
  341. } else {
  342. if (empty($userInfo)) {
  343. $this->logger->addError("Teachers - Can't update user :".$row['username']);
  344. continue;
  345. }
  346. $expirationDate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserUpdate)."years"));
  347. // Update user
  348. $result = UserManager::update_user(
  349. $userInfo['user_id'],
  350. $row['firstname'], // <<-- changed
  351. $row['lastname'], // <<-- changed
  352. $userInfo['username'],
  353. null, //$password = null,
  354. PLATFORM_AUTH_SOURCE,
  355. $userInfo['email'],
  356. COURSEMANAGER,
  357. $userInfo['official_code'],
  358. $userInfo['phone'],
  359. $userInfo['picture_uri'],
  360. $expirationDate,
  361. $userInfo['active'],
  362. null, //$creator_id = null,
  363. 0, //$hr_dept_id = 0,
  364. null, // $extra = null,
  365. null, //$language = 'english',
  366. null, //$encrypt_method = '',
  367. false, //$send_email = false,
  368. 0 //$reset_password = 0
  369. );
  370. if ($result) {
  371. foreach ($row as $key => $value) {
  372. if (substr($key, 0, 6) == 'extra_') {
  373. //an extra field
  374. UserManager::update_extra_field_value(
  375. $userInfo['user_id'],
  376. substr($key, 6),
  377. $value
  378. );
  379. }
  380. }
  381. $this->logger->addInfo("Teachers - User updated: ".$row['username']);
  382. } else {
  383. $this->logger->addError("Teachers - User not updated: ".$row['username']);
  384. }
  385. }
  386. }
  387. }
  388. if ($moveFile) {
  389. $this->moveFile($file);
  390. }
  391. }
  392. /**
  393. * @param string $file
  394. */
  395. private function importStudentsStatic($file)
  396. {
  397. $this->importStudents($file, false);
  398. }
  399. /**
  400. * @param string $file
  401. * @param bool $moveFile
  402. */
  403. private function importStudents($file, $moveFile = true)
  404. {
  405. $data = Import::csv_to_array($file);
  406. /*
  407. * Another users import.
  408. Unique identifier: official code and username . ok
  409. Password should never get updated. ok
  410. 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.
  411. All other fields should be updateable, though passwords should of course not get updated. ok
  412. If a user gets deleted (not there anymore),
  413. 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.
  414. */
  415. if (!empty($data)) {
  416. $language = $this->defaultLanguage;
  417. $this->logger->addInfo(count($data)." records found.");
  418. foreach ($data as $row) {
  419. $row = $this->cleanUserRow($row);
  420. $user_id = UserManager::get_user_id_from_original_id(
  421. $row['extra_'.$this->extraFieldIdNameList['user']],
  422. $this->extraFieldIdNameList['user']
  423. );
  424. $userInfo = array();
  425. $userInfoByOfficialCode = null;
  426. if (!empty($user_id)) {
  427. $userInfo = api_get_user_info($user_id);
  428. $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
  429. }
  430. $expirationDate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserCreation)."years"));
  431. if (empty($userInfo) && empty($userInfoByOfficialCode)) {
  432. // Create user
  433. $result = UserManager::create_user(
  434. $row['firstname'],
  435. $row['lastname'],
  436. STUDENT,
  437. $row['email'],
  438. $row['username'],
  439. $row['password'],
  440. $row['official_code'],
  441. $language, //$row['language'],
  442. $row['phone'],
  443. null, //$row['picture'], //picture
  444. PLATFORM_AUTH_SOURCE, // ?
  445. $expirationDate, //'0000-00-00 00:00:00', //$row['expiration_date'], //$expiration_date = '0000-00-00 00:00:00',
  446. 1, //active
  447. 0,
  448. null, // extra
  449. null, //$encrypt_method = '',
  450. false //$send_mail = false
  451. );
  452. if ($result) {
  453. foreach ($row as $key => $value) {
  454. if (substr($key, 0, 6) == 'extra_') {
  455. //an extra field
  456. UserManager::update_extra_field_value($result, substr($key, 6), $value);
  457. }
  458. }
  459. $this->logger->addInfo("Students - User created: ".$row['username']);
  460. } else {
  461. $this->logger->addError("Students - User NOT created: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  462. }
  463. } else {
  464. if (empty($userInfo)) {
  465. $this->logger->addError("Students - Can't update user :".$row['username']);
  466. continue;
  467. }
  468. if ($row['action'] == 'delete') {
  469. // Inactive one year later
  470. $userInfo['expiration_date'] = api_get_utc_datetime(api_strtotime(time() + 365*24*60*60));
  471. }
  472. $password = $row['password']; // change password
  473. $email = $row['email']; // change email
  474. $resetPassword = 2; // allow password change
  475. // Conditions that disables the update of password and email:
  476. if (isset($this->conditions['importStudents'])) {
  477. if (isset($this->conditions['importStudents']['update']) &&
  478. isset($this->conditions['importStudents']['update']['avoid'])
  479. ) {
  480. // Blocking email update -
  481. // 1. Condition
  482. $avoidUsersWithEmail = $this->conditions['importStudents']['update']['avoid']['email'];
  483. if ($userInfo['email'] != $row['email'] && in_array($row['email'], $avoidUsersWithEmail)) {
  484. $this->logger->addInfo("Students - User email is not updated : ".$row['username']." because the avoid conditions (email).");
  485. // Do not change email keep the old email.
  486. $email = $userInfo['email'];
  487. }
  488. // 2. Condition
  489. if (!in_array($userInfo['email'], $avoidUsersWithEmail) && !in_array($row['email'], $avoidUsersWithEmail)) {
  490. $email = $userInfo['email'];
  491. }
  492. // 3. Condition
  493. if (in_array($userInfo['email'], $avoidUsersWithEmail) && !in_array($row['email'], $avoidUsersWithEmail)) {
  494. $email = $row['email'];
  495. }
  496. // Blocking password update
  497. $avoidUsersWithPassword = $this->conditions['importStudents']['update']['avoid']['password'];
  498. if ($userInfo['password'] != api_get_encrypted_password($row['password']) && in_array($row['password'], $avoidUsersWithPassword)) {
  499. $this->logger->addInfo("Students - User password is not updated: ".$row['username']." because the avoid conditions (password).");
  500. $password = null;
  501. $resetPassword = 0; // disallow password change
  502. }
  503. }
  504. }
  505. $expirationDate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserUpdate)."years"));
  506. // Update user
  507. $result = UserManager::update_user(
  508. $userInfo['user_id'],
  509. $row['firstname'], // <<-- changed
  510. $row['lastname'], // <<-- changed
  511. $row['username'], // <<-- changed
  512. $password, //$password = null,
  513. PLATFORM_AUTH_SOURCE,
  514. $email,
  515. STUDENT,
  516. $userInfo['official_code'],
  517. $userInfo['phone'],
  518. $userInfo['picture_uri'],
  519. $expirationDate,
  520. $userInfo['active'],
  521. null, //$creator_id = null,
  522. 0, //$hr_dept_id = 0,
  523. null, // $extra = null,
  524. null, //$language = 'english',
  525. null, //$encrypt_method = '',
  526. false, //$send_email = false,
  527. $resetPassword //$reset_password = 0
  528. );
  529. if ($result) {
  530. if ($row['username'] != $userInfo['username']) {
  531. $this->logger->addInfo("Students - Username was changes from '".$userInfo['username']."' to '".$row['username']."' ");
  532. }
  533. foreach ($row as $key => $value) {
  534. if (substr($key, 0, 6) == 'extra_') {
  535. //an extra field
  536. UserManager::update_extra_field_value(
  537. $userInfo['user_id'],
  538. substr($key, 6),
  539. $value
  540. );
  541. }
  542. }
  543. $this->logger->addInfo("Students - User updated: ".$row['username']);
  544. } else {
  545. $this->logger->addError("Students - User NOT updated: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  546. }
  547. }
  548. }
  549. }
  550. if ($moveFile) {
  551. $this->moveFile($file);
  552. }
  553. }
  554. /**
  555. * @param string $file
  556. */
  557. private function importCoursesStatic($file)
  558. {
  559. $this->importCourses($file, false);
  560. }
  561. /**
  562. * @param string $file
  563. * @param bool $moveFile
  564. *
  565. * @return int
  566. */
  567. private function importCalendarStatic($file, $moveFile = true)
  568. {
  569. $data = Import::csv_to_array($file);
  570. if ($this->getDumpValues()) {
  571. // Remove all calendar items
  572. $truncateTables = array(
  573. Database::get_course_table(TABLE_AGENDA),
  574. Database::get_course_table(TABLE_AGENDA_ATTACHMENT),
  575. Database::get_course_table(TABLE_AGENDA_REPEAT),
  576. Database::get_course_table(TABLE_AGENDA_REPEAT_NOT),
  577. Database::get_main_table(TABLE_PERSONAL_AGENDA),
  578. Database::get_main_table(TABLE_PERSONAL_AGENDA_REPEAT_NOT),
  579. Database::get_main_table(TABLE_PERSONAL_AGENDA_REPEAT)
  580. );
  581. foreach ($truncateTables as $table) {
  582. $sql = "TRUNCATE $table";
  583. Database::query($sql);
  584. }
  585. $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  586. $sql = "DELETE FROM $table WHERE tool = 'calendar_event'";
  587. Database::query($sql);
  588. }
  589. if (!empty($data)) {
  590. $this->logger->addInfo(count($data) . " records found.");
  591. $eventsToCreate = array();
  592. $errorFound = false;
  593. foreach ($data as $row) {
  594. $sessionId = null;
  595. $externalSessionId = null;
  596. if (isset($row['external_sessionID'])) {
  597. $externalSessionId = $row['external_sessionID'];
  598. $sessionId = SessionManager::get_session_id_from_original_id(
  599. $externalSessionId,
  600. $this->extraFieldIdNameList['session']
  601. );
  602. }
  603. $courseCode = null;
  604. if (isset($row['coursecode'])) {
  605. $courseCode = $row['coursecode'];
  606. }
  607. $courseInfo = api_get_course_info($courseCode);
  608. if (empty($courseInfo)) {
  609. $this->logger->addInfo("Course '$courseCode' does not exists");
  610. }
  611. if (empty($sessionId)) {
  612. $this->logger->addInfo("external_sessionID: ".$externalSessionId." does not exists.");
  613. }
  614. $teacherId = null;
  615. if (!empty($sessionId) && !empty($courseInfo)) {
  616. $courseIncluded = SessionManager::relation_session_course_exist(
  617. $sessionId,
  618. $courseInfo['code']
  619. );
  620. if ($courseIncluded == false) {
  621. $this->logger->addInfo(
  622. "Course '$courseCode' is not included in session: $sessionId"
  623. );
  624. $errorFound = true;
  625. } else {
  626. $teachers = CourseManager::get_coach_list_from_course_code(
  627. $courseInfo['code'],
  628. $sessionId
  629. );
  630. // Getting first teacher.
  631. if (!empty($teachers)) {
  632. $teacher = current($teachers);
  633. $teacherId = $teacher['user_id'];
  634. } else {
  635. $sessionInfo = api_get_session_info($sessionId);
  636. $teacherId = $sessionInfo['id_coach'];
  637. }
  638. }
  639. } else {
  640. $errorFound = true;
  641. }
  642. if (empty($teacherId)) {
  643. $errorFound = true;
  644. $this->logger->addInfo(
  645. "No teacher found in course code : '$courseCode' and session: '$sessionId'"
  646. );
  647. }
  648. $date = $row['date'];
  649. $startTime = $row['time_start'];
  650. $endTime = $row['time_end'];
  651. $title = $row['title'];
  652. $startDateYear = substr($date, 0, 4);
  653. $startDateMonth = substr($date, 4, 2);
  654. $startDateDay = substr($date, 6, 8);
  655. $startDate = $startDateYear.'-'.$startDateMonth.'-'.$startDateDay.' '.$startTime.":00";
  656. $endDate = $startDateYear.'-'.$startDateMonth.'-'.$startDateDay.' '.$endTime.":00";
  657. if (!api_is_valid_date($startDate) OR !api_is_valid_date($endDate)) {
  658. $this->logger->addInfo(
  659. "Verify your dates: '$startDate' : '$endDate' "
  660. );
  661. $errorFound = true;
  662. }
  663. if ($errorFound == false) {
  664. $eventsToCreate[] = array(
  665. 'start' => $startDate,
  666. 'end' => $endDate,
  667. 'title' => $title,
  668. 'sender_id' => $teacherId,
  669. 'course_id' => $courseInfo['real_id'],
  670. 'session_id' => $sessionId,
  671. $this->extraFieldIdNameList['calendar_event'] => $row['external_calendar_itemID']
  672. );
  673. }
  674. }
  675. if (empty($eventsToCreate)) {
  676. $this->logger->addInfo(
  677. "No events to add"
  678. );
  679. return 0;
  680. }
  681. $this->logger->addInfo(
  682. "Ready to insert events"
  683. );
  684. $content = null;
  685. $agenda = new Agenda();
  686. $extraFieldValue = new ExtraFieldValue('calendar_event');
  687. $extraFieldName = $this->extraFieldIdNameList['calendar_event'];
  688. $externalEventId = null;
  689. $extraField = new ExtraField('calendar_event');
  690. $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable($extraFieldName);
  691. if (empty($extraFieldInfo)) {
  692. $this->logger->addInfo(
  693. "No calendar event extra field created: $extraFieldName"
  694. );
  695. return 0;
  696. }
  697. foreach ($eventsToCreate as $event) {
  698. if (!isset($event[$extraFieldName])) {
  699. $this->logger->addInfo(
  700. "No external_calendar_itemID found. Skipping ..."
  701. );
  702. continue;
  703. } else {
  704. $externalEventId = $event[$extraFieldName];
  705. $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
  706. $extraFieldName,
  707. $externalEventId
  708. );
  709. if (!empty($item) || empty($externalEventId)) {
  710. $this->logger->addInfo(
  711. "Event #$externalEventId was already added . Skipping ..."
  712. );
  713. continue;
  714. }
  715. }
  716. $courseInfo = api_get_course_info_by_id($event['course_id']);
  717. $agenda->set_course($courseInfo);
  718. $agenda->setType('course');
  719. $agenda->setSessionId($event['session_id']);
  720. $agenda->setSenderId($event['sender_id']);
  721. $eventComment = $event['comment'];
  722. // To use the event comment you need
  723. // ALTER TABLE c_calendar_event ADD COLUMN comment TEXT;
  724. // add in configuration.php allow_agenda_event_comment = true
  725. if (empty($courseInfo)) {
  726. $this->logger->addInfo(
  727. "No course found for added: #".$event['course_id']." Skipping ..."
  728. );
  729. continue;
  730. }
  731. if (empty($event['sender_id'])) {
  732. $this->logger->addInfo(
  733. "No sender found: #".$event['sender_id']." Skipping ..."
  734. );
  735. continue;
  736. }
  737. $eventId = $agenda->add_event(
  738. $event['start'],
  739. $event['end'],
  740. false,
  741. $event['title'],
  742. $content,
  743. array('everyone'), // send to
  744. false, //$addAsAnnouncement = false
  745. null, // $parentEventId
  746. array(), //$attachmentArray = array(),
  747. null, //$attachmentComment = null,
  748. $eventComment
  749. );
  750. if (!empty($eventId)) {
  751. $extraFieldValue->save(
  752. array(
  753. 'field_value' => $externalEventId,
  754. 'field_id' => $extraFieldInfo['id'],
  755. 'calendar_event_id' => $eventId
  756. )
  757. );
  758. $this->logger->addInfo(
  759. "Event added: #$eventId"
  760. );
  761. } else {
  762. $this->logger->addInfo(
  763. "Error while creating event."
  764. );
  765. }
  766. }
  767. }
  768. if ($moveFile) {
  769. $this->moveFile($file);
  770. }
  771. }
  772. /**
  773. * @param string $file
  774. * @param bool $moveFile
  775. */
  776. private function importCourses($file, $moveFile = true)
  777. {
  778. $data = Import::csv_to_array($file);
  779. if (!empty($data)) {
  780. $this->logger->addInfo(count($data)." records found.");
  781. foreach ($data as $row) {
  782. $row = $this->cleanCourseRow($row);
  783. $courseCode = CourseManager::get_course_id_from_original_id(
  784. $row['extra_' . $this->extraFieldIdNameList['course']],
  785. $this->extraFieldIdNameList['course']
  786. );
  787. $courseInfo = api_get_course_info($courseCode);
  788. if (empty($courseInfo)) {
  789. // Create
  790. $params = array();
  791. $params['title'] = $row['title'];
  792. $params['exemplary_content'] = false;
  793. $params['wanted_code'] = $row['course_code'];
  794. $params['course_category'] = $row['course_category'];
  795. $params['course_language'] = $row['language'];
  796. $params['teachers'] = $row['teachers'];
  797. $courseInfo = CourseManager::create_course($params);
  798. if (!empty($courseInfo)) {
  799. CourseManager::update_course_extra_field_value(
  800. $courseInfo['code'],
  801. 'external_course_id',
  802. $row['extra_'.$this->extraFieldIdNameList['course']]
  803. );
  804. $this->logger->addInfo("Courses - Course created ".$courseInfo['code']);
  805. } else {
  806. $this->logger->addError("Courses - Can't create course:".$row['title']);
  807. }
  808. } else {
  809. // Update
  810. $params = array(
  811. 'title' => $row['title'],
  812. );
  813. $result = CourseManager::update_attributes($courseInfo['real_id'], $params);
  814. $addTeacherToSession = isset($courseInfo['add_teachers_to_sessions_courses']) && !empty($courseInfo['add_teachers_to_sessions_courses']) ? true : false;
  815. if ($addTeacherToSession) {
  816. CourseManager::updateTeachers($courseInfo['id'], $row['teachers'], false, true, false);
  817. } else {
  818. CourseManager::updateTeachers($courseInfo['id'], $row['teachers'], false, false);
  819. }
  820. if ($result) {
  821. $this->logger->addInfo("Courses - Course updated ".$courseInfo['code']);
  822. } else {
  823. $this->logger->addError("Courses - Course NOT updated ".$courseInfo['code']);
  824. }
  825. }
  826. }
  827. }
  828. if ($moveFile) {
  829. $this->moveFile($file);
  830. }
  831. }
  832. /**
  833. * Updates the session synchronize with the csv file.
  834. * @param string $file
  835. */
  836. private function importSessionsStatic($file)
  837. {
  838. $content = file($file);
  839. $sessions = array();
  840. if (!api_strstr($content[0], ';')) {
  841. $error_message = get_lang('NotCSV');
  842. } else {
  843. $tag_names = array();
  844. foreach ($content as $key => $enreg) {
  845. $enreg = explode(';', trim($enreg));
  846. if ($key) {
  847. foreach ($tag_names as $tag_key => $tag_name) {
  848. $sessions[$key - 1][$tag_name] = $enreg[$tag_key];
  849. }
  850. } else {
  851. foreach ($enreg as $tag_name) {
  852. $tag_names[] = api_preg_replace(
  853. '/[^a-zA-Z0-9_\-]/',
  854. '',
  855. $tag_name
  856. );
  857. }
  858. if (!in_array('SessionName', $tag_names) || !in_array(
  859. 'DateStart',
  860. $tag_names
  861. ) || !in_array('DateEnd', $tag_names)
  862. ) {
  863. $error_message = get_lang('NoNeededData');
  864. break;
  865. }
  866. }
  867. }
  868. }
  869. if (!empty($sessions)) {
  870. // Looping the sessions.
  871. foreach ($sessions as $session) {
  872. if (!empty($session['SessionID'])) {
  873. $sessionId = SessionManager::get_session_id_from_original_id(
  874. $session['SessionID'],
  875. $this->extraFieldIdNameList['session']
  876. );
  877. $coachUserName = isset($session['Coach']) ? $session['Coach'] : null;
  878. $categoryId = isset($session['category_id']) ? $session['category_id'] : null;
  879. // 2014-06-30
  880. $dateStart = explode('/', $session['DateStart']);
  881. $dateEnd = explode('/', $session['DateEnd']);
  882. $visibility = $this->defaultSessionVisibility;
  883. $coachId = null;
  884. if (!empty($coachUserName)) {
  885. $coachInfo = api_get_user_info_from_username($coachUserName);
  886. $coachId = $coachInfo['user_id'];
  887. }
  888. if (empty($sessionId)) {
  889. $result = SessionManager::create_session(
  890. $session['SessionName'],
  891. $dateStart[0],
  892. $dateStart[1],
  893. $dateStart[2],
  894. $dateEnd[0],
  895. $dateEnd[1],
  896. $dateEnd[2],
  897. $this->daysCoachAccessBeforeBeginning,
  898. $this->daysCoachAccessAfterBeginning,
  899. null,
  900. $coachUserName,
  901. $categoryId,
  902. $visibility
  903. );
  904. if (is_numeric($result)) {
  905. $sessionId = $result;
  906. SessionManager::update_session_extra_field_value(
  907. $sessionId,
  908. $this->extraFieldIdNameList['session'],
  909. $session['SessionID']
  910. );
  911. }
  912. } else {
  913. $sessionInfo = api_get_session_info($sessionId);
  914. $accessBefore = null;
  915. $accessAfter = null;
  916. if (empty($sessionInfo['nb_days_access_before_beginning']) ||
  917. (!empty($sessionInfo['nb_days_access_before_beginning']) &&
  918. $sessionInfo['nb_days_access_before_beginning'] < $this->daysCoachAccessBeforeBeginning)
  919. ) {
  920. $accessBefore = intval($this->daysCoachAccessBeforeBeginning);
  921. }
  922. $accessAfter = null;
  923. if (empty($sessionInfo['nb_days_access_after_end']) ||
  924. (!empty($sessionInfo['nb_days_access_after_end']) &&
  925. $sessionInfo['nb_days_access_after_end'] < $this->daysCoachAccessAfterBeginning)
  926. ) {
  927. $accessAfter = intval($this->daysCoachAccessAfterBeginning);
  928. }
  929. $result = SessionManager::edit_session(
  930. $sessionId,
  931. $session['SessionName'],
  932. $dateStart[0],
  933. $dateStart[1],
  934. $dateStart[2],
  935. $dateEnd[0],
  936. $dateEnd[1],
  937. $dateEnd[2],
  938. $accessBefore,
  939. $accessAfter,
  940. null,
  941. $coachId,
  942. $categoryId,
  943. $visibility
  944. );
  945. if (is_numeric($result)) {
  946. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  947. $params = array(
  948. 'description' => $session['SessionDescription'],
  949. );
  950. Database::update(
  951. $tbl_session,
  952. $params,
  953. array('id = ?' => $sessionId)
  954. );
  955. }
  956. }
  957. // Courses
  958. $courses = explode('|', $session['Courses']);
  959. $courseList = array();
  960. foreach ($courses as $course) {
  961. $courseArray = bracketsToArray($course);
  962. $courseCode = $courseArray[0];
  963. if (CourseManager::course_exists($courseCode)) {
  964. $courseList[] = $courseCode;
  965. }
  966. }
  967. SessionManager::add_courses_to_session(
  968. $sessionId,
  969. $courseList,
  970. true
  971. );
  972. if (!empty($sessionId)) {
  973. $courses = explode('|', $session['Courses']);
  974. foreach ($courses as $course) {
  975. $courseArray = bracketsToArray($course);
  976. $courseCode = $courseArray[0];
  977. if (CourseManager::course_exists($courseCode)) {
  978. // Coaches
  979. $courseCoaches = isset($courseArray[1]) ? $courseArray[1] : null;
  980. $courseCoaches = explode(',', $courseCoaches);
  981. if (!empty($courseCoaches)) {
  982. $coachList = array();
  983. foreach ($courseCoaches as $courseCoach) {
  984. $courseCoachId = UserManager::get_user_id_from_username($courseCoach);
  985. if ($courseCoachId !== false) {
  986. // Just insert new coaches
  987. $coachList[] = $courseCoachId;
  988. }
  989. }
  990. SessionManager::updateCoaches(
  991. $sessionId,
  992. $courseCode,
  993. $coachList,
  994. true
  995. );
  996. }
  997. // Students
  998. $courseUsers = isset($courseArray[2]) ? $courseArray[2] : null;
  999. $courseUsers = explode(',', $courseUsers);
  1000. if (!empty($courseUsers)) {
  1001. $userList = array();
  1002. foreach ($courseUsers as $username) {
  1003. $userInfo = api_get_user_info_from_username(trim($username));
  1004. if (!empty($userInfo)) {
  1005. $userList[] = $userInfo['user_id'];
  1006. }
  1007. }
  1008. SessionManager::subscribe_users_to_session_course(
  1009. $userList,
  1010. $sessionId,
  1011. $courseCode,
  1012. SESSION_VISIBLE_READ_ONLY,
  1013. true
  1014. );
  1015. } else {
  1016. $this->logger->addInfo("No users to register.");
  1017. }
  1018. } else {
  1019. $this->logger->addInfo("Course does not exists $courseCode");
  1020. }
  1021. }
  1022. } else {
  1023. $this->logger->addInfo('SessionID not found in system.');
  1024. }
  1025. } else {
  1026. $this->logger->addInfo('SessionID does not exists');
  1027. }
  1028. }
  1029. } else {
  1030. $this->logger->addInfo($error_message);
  1031. }
  1032. }
  1033. /**
  1034. * @param string $file
  1035. * @param bool $moveFile
  1036. */
  1037. private function importSessions($file, $moveFile = true)
  1038. {
  1039. $avoid = null;
  1040. if (isset($this->conditions['importSessions']) &&
  1041. isset($this->conditions['importSessions']['update'])
  1042. ) {
  1043. $avoid = $this->conditions['importSessions']['update'];
  1044. }
  1045. $result = SessionManager::importCSV(
  1046. $file,
  1047. true,
  1048. $this->defaultAdminId,
  1049. $this->logger,
  1050. array('SessionID' => 'extra_'.$this->extraFieldIdNameList['session']),
  1051. $this->extraFieldIdNameList['session'],
  1052. $this->daysCoachAccessBeforeBeginning,
  1053. $this->daysCoachAccessAfterBeginning,
  1054. $this->defaultSessionVisibility,
  1055. $avoid,
  1056. false, // deleteUsersNotInList
  1057. false, // updateCourseCoaches
  1058. true // sessionWithCoursesModifier
  1059. );
  1060. if (!empty($result['error_message'])) {
  1061. $this->logger->addError($result['error_message']);
  1062. }
  1063. $this->logger->addInfo("Sessions - Sessions parsed: ".$result['session_counter']);
  1064. if ($moveFile) {
  1065. $this->moveFile($file);
  1066. }
  1067. }
  1068. /**
  1069. * @param string $file
  1070. */
  1071. private function importSubscribeStatic($file)
  1072. {
  1073. $data = Import::csv_reader($file);
  1074. if (!empty($data)) {
  1075. $this->logger->addInfo(count($data) . " records found.");
  1076. foreach ($data as $row) {
  1077. $chamiloUserName = $row['UserName'];
  1078. $chamiloCourseCode = $row['CourseCode'];
  1079. $chamiloSessionId = $row['SessionID'];
  1080. $type = $row['Type'];
  1081. $sessionInfo = api_get_session_info($chamiloSessionId);
  1082. if (empty($sessionInfo)) {
  1083. $this->logger->addError('Session does not exists: '.$chamiloSessionId);
  1084. continue;
  1085. }
  1086. $courseInfo = api_get_course_info($chamiloCourseCode);
  1087. if (empty($courseInfo)) {
  1088. $this->logger->addError('Course does not exists: '.$courseInfo);
  1089. continue;
  1090. }
  1091. $userId = Usermanager::get_user_id_from_username($chamiloUserName);
  1092. if (empty($userId)) {
  1093. $this->logger->addError('User does not exists: '.$chamiloUserName);
  1094. continue;
  1095. }
  1096. $status = null;
  1097. switch ($type) {
  1098. case 'student':
  1099. SessionManager::subscribe_users_to_session_course(
  1100. array($userId),
  1101. $chamiloSessionId,
  1102. $courseInfo['code'],
  1103. null,
  1104. false
  1105. );
  1106. break;
  1107. case 'teacher':
  1108. SessionManager::set_coach_to_course_session(
  1109. $userId,
  1110. $chamiloSessionId,
  1111. $courseInfo['code']
  1112. );
  1113. break;
  1114. }
  1115. $this->logger->addError(
  1116. "User '$chamiloUserName' with status $type was added to session: #$chamiloSessionId - Course: " . $courseInfo['code']
  1117. );
  1118. }
  1119. }
  1120. }
  1121. /**
  1122. * @param string $file
  1123. */
  1124. private function importUnsubscribeStatic($file)
  1125. {
  1126. $data = Import::csv_reader($file);
  1127. if (!empty($data)) {
  1128. $this->logger->addInfo(count($data)." records found.");
  1129. foreach ($data as $row) {
  1130. $chamiloUserName = $row['UserName'];
  1131. $chamiloCourseCode = $row['CourseCode'];
  1132. $chamiloSessionId = $row['SessionID'];
  1133. $sessionInfo = api_get_session_info($chamiloSessionId);
  1134. if (empty($sessionInfo)) {
  1135. $this->logger->addError('Session does not exists: '.$chamiloSessionId);
  1136. continue;
  1137. }
  1138. $courseInfo = api_get_course_info($chamiloCourseCode);
  1139. if (empty($courseInfo)) {
  1140. $this->logger->addError('Course does not exists: '.$courseInfo);
  1141. continue;
  1142. }
  1143. $userId = Usermanager::get_user_id_from_username($chamiloUserName);
  1144. if (empty($userId)) {
  1145. $this->logger->addError('User does not exists: '.$chamiloUserName);
  1146. continue;
  1147. }
  1148. CourseManager::unsubscribe_user($userId, $courseInfo['code'], $chamiloSessionId);
  1149. $this->logger->addError(
  1150. "User '$chamiloUserName' was removed from session: #$chamiloSessionId, Course: ".$courseInfo['code']
  1151. );
  1152. }
  1153. }
  1154. }
  1155. /**
  1156. * Dump database tables
  1157. */
  1158. private function dumpDatabaseTables()
  1159. {
  1160. echo 'Dumping tables'.PHP_EOL;
  1161. // User
  1162. $table = Database::get_main_table(TABLE_MAIN_USER);
  1163. $tableAdmin = Database::get_main_table(TABLE_MAIN_ADMIN);
  1164. $sql = "DELETE FROM $table
  1165. WHERE user_id not in (select user_id from $tableAdmin) and status <> ".ANONYMOUS;
  1166. Database::query($sql);
  1167. echo $sql.PHP_EOL;
  1168. // Course
  1169. $table = Database::get_main_table(TABLE_MAIN_COURSE);
  1170. $sql = "DELETE FROM $table";
  1171. Database::query($sql);
  1172. echo $sql.PHP_EOL;
  1173. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE);
  1174. $sql = "DELETE FROM $table";
  1175. Database::query($sql);
  1176. echo $sql.PHP_EOL;
  1177. $table = Database::get_main_table(TABLE_MAIN_COURSE_USER);
  1178. $sql = "DELETE FROM $table";
  1179. Database::query($sql);
  1180. echo $sql.PHP_EOL;
  1181. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER);
  1182. $sql = "DELETE FROM $table";
  1183. Database::query($sql);
  1184. echo $sql.PHP_EOL;
  1185. // Sessions
  1186. $table = Database::get_main_table(TABLE_MAIN_SESSION);
  1187. $sql = "DELETE FROM $table";
  1188. Database::query($sql);
  1189. echo $sql.PHP_EOL;
  1190. $table = Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY);
  1191. $sql = "DELETE FROM $table";
  1192. Database::query($sql);
  1193. echo $sql.PHP_EOL;
  1194. $table = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  1195. $sql = "DELETE FROM $table";
  1196. Database::query($sql);
  1197. echo $sql.PHP_EOL;
  1198. $table = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  1199. $sql = "DELETE FROM $table";
  1200. Database::query($sql);
  1201. echo $sql.PHP_EOL;
  1202. $table = Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION);
  1203. $sql = "DELETE FROM $table";
  1204. Database::query($sql);
  1205. echo $sql.PHP_EOL;
  1206. // Extra fields
  1207. $table = Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES);
  1208. $sql = "DELETE FROM $table";
  1209. Database::query($sql);
  1210. echo $sql.PHP_EOL;
  1211. $table = Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES);
  1212. $sql = "DELETE FROM $table";
  1213. Database::query($sql);
  1214. echo $sql.PHP_EOL;
  1215. $table = Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES);
  1216. $sql = "DELETE FROM $table";
  1217. Database::query($sql);
  1218. echo $sql.PHP_EOL;
  1219. }
  1220. }
  1221. use Monolog\Logger;
  1222. use Monolog\Handler\StreamHandler;
  1223. use Monolog\Handler\NativeMailerHandler;
  1224. use Monolog\Handler\RotatingFileHandler;
  1225. use Monolog\Handler\BufferHandler;
  1226. $logger = new Logger('cron');
  1227. $emails = isset($_configuration['cron_notification_mails']) ? $_configuration['cron_notification_mails'] : null;
  1228. $minLevel = Logger::DEBUG;
  1229. if (!is_array($emails)) {
  1230. $emails = array($emails);
  1231. }
  1232. $subject = "Cron main/cron/import_csv.php ".date('Y-m-d h:i:s');
  1233. $from = api_get_setting('emailAdministrator');
  1234. /*
  1235. if (!empty($emails)) {
  1236. foreach ($emails as $email) {
  1237. $stream = new NativeMailerHandler($email, $subject, $from, $minLevel);
  1238. $logger->pushHandler(new BufferHandler($stream, 0, $minLevel));
  1239. }
  1240. }*/
  1241. $stream = new StreamHandler(api_get_path(SYS_ARCHIVE_PATH).'import_csv.log', $minLevel);
  1242. $logger->pushHandler(new BufferHandler($stream, 0, $minLevel));
  1243. $logger->pushHandler(new RotatingFileHandler('import_csv', 5, $minLevel));
  1244. $cronImportCSVConditions = isset($_configuration['cron_import_csv_conditions']) ? $_configuration['cron_import_csv_conditions'] : null;
  1245. echo 'See the error log here: '.api_get_path(SYS_ARCHIVE_PATH).'import_csv.log'."\n";
  1246. $import = new ImportCsv($logger, $cronImportCSVConditions);
  1247. if (isset($_configuration['default_admin_user_id_for_cron'])) {
  1248. $import->defaultAdminId = $_configuration['default_admin_user_id_for_cron'];
  1249. }
  1250. // @todo in production disable the dump option
  1251. $dump = false;
  1252. if (isset($argv[1]) && $argv[1] = '--dump') {
  1253. $dump = true;
  1254. }
  1255. if (isset($_configuration['import_csv_disable_dump']) &&
  1256. $_configuration['import_csv_disable_dump'] == true
  1257. ) {
  1258. $import->setDumpValues(false);
  1259. } else {
  1260. $import->setDumpValues($dump);
  1261. }
  1262. // Do not moves the files to treated
  1263. if (isset($_configuration['import_csv_test'])) {
  1264. $import->test = $_configuration['import_csv_test'];
  1265. } else {
  1266. $import->test = true;
  1267. }
  1268. $import->run();
  1269. if (isset($_configuration['import_csv_fix_permissions']) &&
  1270. $_configuration['import_csv_fix_permissions'] == true
  1271. ) {
  1272. $command = "sudo find ".api_get_path(SYS_COURSE_PATH)." -type d -exec chmod 777 {} \; ";
  1273. echo "Executing: ".$command.PHP_EOL;
  1274. system($command);
  1275. $command = "sudo find ".api_get_path(SYS_CODE_PATH)."upload/users -type d -exec chmod 777 {} \;";
  1276. echo "Executing: ".$command.PHP_EOL;
  1277. system($command);
  1278. }