import_csv.php 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  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. ini_set('memory_limit', -1);
  11. ini_set('max_execution_time', 0);
  12. /**
  13. * Class ImportCsv
  14. */
  15. class ImportCsv
  16. {
  17. private $logger;
  18. private $dumpValues;
  19. public $test;
  20. public $defaultLanguage = 'dutch';
  21. public $extraFieldIdNameList = array(
  22. 'session' => 'external_session_id',
  23. 'course' => 'external_course_id',
  24. 'user' => 'external_user_id',
  25. 'calendar_event' => 'external_calendar_event_id',
  26. );
  27. public $defaultAdminId = 1;
  28. public $defaultSessionVisibility = 1;
  29. /**
  30. * When creating a user the expiration date is set to registration date + this value
  31. * @var int number of years
  32. */
  33. public $expirationDateInUserCreation = 1;
  34. public $batchSize = 20;
  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 Monolog\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 boolean
  61. */
  62. public function getDumpValues()
  63. {
  64. return $this->dumpValues;
  65. }
  66. /**
  67. * Runs the import process
  68. */
  69. public function run()
  70. {
  71. global $_configuration;
  72. $value = api_get_configuration_value('import_csv_custom_url_id');
  73. if (!empty($value)) {
  74. $_configuration['access_url'] = $value;
  75. }
  76. $path = api_get_path(SYS_CODE_PATH).'cron/incoming/';
  77. if (!is_dir($path)) {
  78. echo "The folder! $path does not exits";
  79. return 0;
  80. }
  81. if ($this->getDumpValues()) {
  82. $this->dumpDatabaseTables();
  83. }
  84. echo "Reading files: ".PHP_EOL.PHP_EOL;
  85. $files = scandir($path);
  86. $fileToProcess = array();
  87. $fileToProcessStatic = array();
  88. $teacherBackup = array();
  89. $groupBackup = array();
  90. if (!empty($files)) {
  91. foreach ($files as $file) {
  92. $fileInfo = pathinfo($file);
  93. if (isset($fileInfo['extension']) && $fileInfo['extension'] === 'csv') {
  94. // Checking teachers_yyyymmdd.csv, courses_yyyymmdd.csv, students_yyyymmdd.csv and sessions_yyyymmdd.csv
  95. $parts = explode('_', $fileInfo['filename']);
  96. $preMethod = ucwords($parts[1]);
  97. $preMethod = str_replace('-static', 'Static', $preMethod);
  98. $method = 'import'.$preMethod;
  99. $isStatic = strpos($method, 'Static');
  100. if ($method == 'importSessionsextidStatic') {
  101. $method = 'importSessionsExtIdStatic';
  102. }
  103. if ($method == 'importCourseinsertStatic') {
  104. $method = 'importSubscribeUserToCourse';
  105. }
  106. if ($method == 'importUnsubsessionsextidStatic') {
  107. $method = 'importUnsubsessionsExtidStatic';
  108. }
  109. if ($method == 'importSubsessionsextidStatic') {
  110. $method = 'importSubscribeUserToCourseSessionExtStatic';
  111. }
  112. if (method_exists($this, $method)) {
  113. if (
  114. (
  115. $method == 'importSubscribeStatic' ||
  116. $method == 'importSubscribeUserToCourse'
  117. ) ||
  118. empty($isStatic)
  119. ) {
  120. $fileToProcess[$parts[1]][] = array(
  121. 'method' => $method,
  122. 'file' => $path.$fileInfo['basename']
  123. );
  124. } else {
  125. $fileToProcessStatic[$parts[1]][] = array(
  126. 'method' => $method,
  127. 'file' => $path.$fileInfo['basename']
  128. );
  129. }
  130. } else {
  131. echo "Error - This file '$file' can't be processed.".PHP_EOL;
  132. echo "Trying to call $method".PHP_EOL;
  133. echo "The file have to has this format:".PHP_EOL;
  134. echo "prefix_students_ddmmyyyy.csv, prefix_teachers_ddmmyyyy.csv, prefix_courses_ddmmyyyy.csv, prefix_sessions_ddmmyyyy.csv ".PHP_EOL;
  135. exit;
  136. }
  137. }
  138. }
  139. if (empty($fileToProcess) && empty($fileToProcessStatic)) {
  140. echo 'Error - no files to process.';
  141. return 0;
  142. }
  143. $this->prepareImport();
  144. $sections = array(
  145. 'students',
  146. 'teachers',
  147. 'courses',
  148. 'sessions',
  149. 'subscribe-static',
  150. 'courseinsert-static',
  151. 'unsubscribe-static'
  152. );
  153. foreach ($sections as $section) {
  154. $this->logger->addInfo("-- Import $section --");
  155. if (isset($fileToProcess[$section]) && !empty($fileToProcess[$section])) {
  156. $files = $fileToProcess[$section];
  157. foreach ($files as $fileInfo) {
  158. $method = $fileInfo['method'];
  159. $file = $fileInfo['file'];
  160. echo 'File: '.$file.PHP_EOL;
  161. $this->logger->addInfo("Reading file: $file");
  162. if ($method == 'importSessions') {
  163. $this->$method(
  164. $file,
  165. true,
  166. $teacherBackup,
  167. $groupBackup
  168. );
  169. } else {
  170. $this->$method($file, true);
  171. }
  172. }
  173. }
  174. }
  175. $sections = array(
  176. 'students-static',
  177. 'teachers-static',
  178. 'courses-static',
  179. 'sessions-static',
  180. 'calendar-static',
  181. 'sessionsextid-static',
  182. 'unsubscribe-static',
  183. 'unsubsessionsextid-static',
  184. 'subsessionsextid-static'
  185. );
  186. foreach ($sections as $section) {
  187. $this->logger->addInfo("-- Import static files $section --");
  188. if (isset($fileToProcessStatic[$section]) &&
  189. !empty($fileToProcessStatic[$section])
  190. ) {
  191. $files = $fileToProcessStatic[$section];
  192. foreach ($files as $fileInfo) {
  193. $method = $fileInfo['method'];
  194. $file = $fileInfo['file'];
  195. echo 'Static file: '.$file.PHP_EOL;
  196. $this->logger->addInfo("Reading static file: $file");
  197. $this->$method(
  198. $file,
  199. true,
  200. $teacherBackup,
  201. $groupBackup
  202. );
  203. }
  204. }
  205. }
  206. }
  207. }
  208. /**
  209. * Prepares extra fields before the import
  210. */
  211. private function prepareImport()
  212. {
  213. // Create user extra field: extra_external_user_id
  214. UserManager::create_extra_field(
  215. $this->extraFieldIdNameList['user'],
  216. 1,
  217. 'External user id',
  218. null
  219. );
  220. // Create course extra field: extra_external_course_id
  221. CourseManager::create_course_extra_field(
  222. $this->extraFieldIdNameList['course'],
  223. 1,
  224. 'External course id',
  225. ''
  226. );
  227. // Create session extra field extra_external_session_id
  228. SessionManager::create_session_extra_field(
  229. $this->extraFieldIdNameList['session'],
  230. 1,
  231. 'External session id'
  232. );
  233. // Create calendar_event extra field extra_external_session_id
  234. $extraField = new ExtraField('calendar_event');
  235. $extraField->save(
  236. array(
  237. 'field_type' => ExtraField::FIELD_TYPE_TEXT,
  238. 'variable' => $this->extraFieldIdNameList['calendar_event'],
  239. 'display_text' => 'External calendar event id',
  240. )
  241. );
  242. }
  243. /**
  244. * @param string $file
  245. */
  246. private function moveFile($file)
  247. {
  248. $moved = str_replace('incoming', 'treated', $file);
  249. if ($this->test) {
  250. $result = 1;
  251. } else {
  252. $result = rename($file, $moved);
  253. }
  254. if ($result) {
  255. $this->logger->addInfo("Moving file to the treated folder: $file");
  256. } else {
  257. $this->logger->addError(
  258. "Error - Cant move file to the treated folder: $file"
  259. );
  260. }
  261. }
  262. /**
  263. * @param array $row
  264. *
  265. * @return array
  266. */
  267. private function cleanUserRow($row)
  268. {
  269. $row['lastname'] = $row['LastName'];
  270. $row['firstname'] = $row['FirstName'];
  271. $row['email'] = $row['Email'];
  272. $row['username'] = $row['UserName'];
  273. $row['password'] = $row['Password'];
  274. $row['auth_source'] = isset($row['AuthSource']) ? $row['AuthSource'] : PLATFORM_AUTH_SOURCE;
  275. $row['official_code'] = $row['OfficialCode'];
  276. $row['phone'] = isset($row['PhoneNumber']) ? $row['PhoneNumber'] : '';
  277. if (isset($row['StudentID'])) {
  278. $row['extra_'.$this->extraFieldIdNameList['user']] = $row['StudentID'];
  279. }
  280. if (isset($row['TeacherID'])) {
  281. $row['extra_'.$this->extraFieldIdNameList['user']] = $row['TeacherID'];
  282. }
  283. return $row;
  284. }
  285. /**
  286. * @param array $row
  287. *
  288. * @return array
  289. */
  290. private function cleanCourseRow($row)
  291. {
  292. $row['title'] = $row['Title'];
  293. $row['course_code'] = $row['Code'];
  294. $row['course_category'] = $row['CourseCategory'];
  295. $row['email'] = $row['Teacher'];
  296. $row['language'] = $row['Language'];
  297. $row['teachers'] = array();
  298. if (isset($row['Teacher']) && !empty($row['Teacher'])) {
  299. //$this->logger->addInfo("Teacher list found: ".$row['Teacher']);
  300. $teachers = explode(',', $row['Teacher']);
  301. if (!empty($teachers)) {
  302. foreach ($teachers as $teacherUserName) {
  303. $teacherUserName = trim($teacherUserName);
  304. $userInfo = api_get_user_info_from_username($teacherUserName);
  305. if (!empty($userInfo)) {
  306. //$this->logger->addInfo("Username found: $teacherUserName");
  307. $row['teachers'][] = $userInfo['user_id'];
  308. }
  309. }
  310. }
  311. }
  312. if (isset($row['CourseID'])) {
  313. $row['extra_'.$this->extraFieldIdNameList['course']] = $row['CourseID'];
  314. }
  315. return $row;
  316. }
  317. /**
  318. * File to import
  319. * @param string $file
  320. */
  321. private function importTeachersStatic($file)
  322. {
  323. $this->importTeachers($file, false);
  324. }
  325. /**
  326. * File to import
  327. * @param string $file
  328. * @param bool $moveFile
  329. */
  330. private function importTeachers($file, $moveFile = true)
  331. {
  332. $this->fixCSVFile($file);
  333. $data = Import::csvToArray($file);
  334. /* Unique identifier: official-code username.
  335. Email address and password should never get updated. *ok
  336. The only fields that I can think of that should update if the data changes in the csv file are FirstName and LastName. *ok
  337. A slight edit of these fields should be taken into account. ???
  338. Adding teachers is no problem, but deleting them shouldn’t be automated, but we should get a log of “to delete teachers”.
  339. We’ll handle that manually if applicable.
  340. No delete!
  341. */
  342. $language = $this->defaultLanguage;
  343. if (!empty($data)) {
  344. $this->logger->addInfo(count($data)." records found.");
  345. $expirationDateOnCreation = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserCreation)."years"));
  346. $expirationDateOnUpdate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserUpdate)."years"));
  347. $batchSize = $this->batchSize;
  348. $em = Database::getManager();
  349. $counter = 1;
  350. foreach ($data as $row) {
  351. $row = $this->cleanUserRow($row);
  352. $user_id = UserManager::get_user_id_from_original_id(
  353. $row['extra_'.$this->extraFieldIdNameList['user']],
  354. $this->extraFieldIdNameList['user']
  355. );
  356. $userInfo = array();
  357. $userInfoByOfficialCode = null;
  358. if (!empty($user_id)) {
  359. $userInfo = api_get_user_info($user_id);
  360. $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
  361. }
  362. if (empty($userInfo) && empty($userInfoByOfficialCode)) {
  363. // Create user
  364. $userId = UserManager::create_user(
  365. $row['firstname'],
  366. $row['lastname'],
  367. COURSEMANAGER,
  368. $row['email'],
  369. $row['username'],
  370. $row['password'],
  371. $row['official_code'],
  372. $language, //$row['language'],
  373. $row['phone'],
  374. null, //$row['picture'], //picture
  375. $row['auth_source'], // ?
  376. $expirationDateOnCreation, //'0000-00-00 00:00:00', //$row['expiration_date'], //$expiration_date = '0000-00-00 00:00:00',
  377. 1, //active
  378. 0,
  379. null, // extra
  380. null, //$encrypt_method = '',
  381. false //$send_mail = false
  382. );
  383. if ($userId) {
  384. foreach ($row as $key => $value) {
  385. if (substr($key, 0, 6) == 'extra_') {
  386. //an extra field
  387. UserManager::update_extra_field_value(
  388. $userId,
  389. substr($key, 6),
  390. $value
  391. );
  392. }
  393. }
  394. $this->logger->addInfo("Teachers - User created: ".$row['username']);
  395. } else {
  396. $this->logger->addError("Teachers - User NOT created: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  397. $this->logger->addError(strip_tags(Display::getFlashToString()));
  398. Display::cleanFlashMessages();
  399. }
  400. } else {
  401. if (empty($userInfo)) {
  402. $this->logger->addError("Teachers - Can't update user :".$row['username']);
  403. continue;
  404. }
  405. // Update user
  406. $result = UserManager::update_user(
  407. $userInfo['user_id'],
  408. $row['firstname'], // <<-- changed
  409. $row['lastname'], // <<-- changed
  410. $userInfo['username'],
  411. null, //$password = null,
  412. $row['auth_source'],
  413. $userInfo['email'],
  414. COURSEMANAGER,
  415. $userInfo['official_code'],
  416. $userInfo['phone'],
  417. $userInfo['picture_uri'],
  418. $expirationDateOnUpdate,
  419. $userInfo['active'],
  420. null, //$creator_id = null,
  421. 0, //$hr_dept_id = 0,
  422. null, // $extra = null,
  423. null, //$language = 'english',
  424. null, //$encrypt_method = '',
  425. false, //$send_email = false,
  426. 0 //$reset_password = 0
  427. );
  428. if ($result) {
  429. foreach ($row as $key => $value) {
  430. if (substr($key, 0, 6) == 'extra_') {
  431. //an extra field
  432. UserManager::update_extra_field_value(
  433. $userInfo['user_id'],
  434. substr($key, 6),
  435. $value
  436. );
  437. }
  438. }
  439. $this->logger->addInfo("Teachers - User updated: ".$row['username']);
  440. } else {
  441. $this->logger->addError("Teachers - User not updated: ".$row['username']);
  442. }
  443. }
  444. if (($counter % $batchSize) === 0) {
  445. $em->flush();
  446. $em->clear(); // Detaches all objects from Doctrine!
  447. }
  448. $counter++;
  449. }
  450. $em->clear(); // Detaches all objects from Doctrine!
  451. }
  452. if ($moveFile) {
  453. $this->moveFile($file);
  454. }
  455. }
  456. /**
  457. * @param string $file
  458. */
  459. private function importStudentsStatic($file)
  460. {
  461. $this->importStudents($file, false);
  462. }
  463. /**
  464. * @param string $file
  465. * @param bool $moveFile
  466. */
  467. private function importStudents($file, $moveFile = true)
  468. {
  469. $this->fixCSVFile($file);
  470. $data = Import::csvToArray($file);
  471. /*
  472. * Another users import.
  473. Unique identifier: official code and username . ok
  474. Password should never get updated. ok
  475. If an update should need to occur (because it changed in the .csv),
  476. we’ll want that logged. We will handle this manually in that case.
  477. All other fields should be updateable, though passwords should of course not get updated. ok
  478. If a user gets deleted (not there anymore),
  479. He should be set inactive one year after the current date.
  480. So I presume you’ll just update the expiration date.
  481. We want to grant access to courses up to a year after deletion.
  482. */
  483. $timeStart = microtime(true);
  484. $batchSize = $this->batchSize;
  485. $em = Database::getManager();
  486. if (!empty($data)) {
  487. $language = $this->defaultLanguage;
  488. $this->logger->addInfo(count($data)." records found.");
  489. $expirationDateOnCreate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserCreation)."years"));
  490. $expirationDateOnUpdate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserUpdate)."years"));
  491. $counter = 1;
  492. foreach ($data as $row) {
  493. $row = $this->cleanUserRow($row);
  494. $user_id = UserManager::get_user_id_from_original_id(
  495. $row['extra_'.$this->extraFieldIdNameList['user']],
  496. $this->extraFieldIdNameList['user']
  497. );
  498. $userInfo = array();
  499. $userInfoByOfficialCode = null;
  500. if (!empty($user_id)) {
  501. $userInfo = api_get_user_info($user_id, false, true);
  502. $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
  503. }
  504. if (empty($userInfo) && empty($userInfoByOfficialCode)) {
  505. // Create user
  506. $result = UserManager::create_user(
  507. $row['firstname'],
  508. $row['lastname'],
  509. STUDENT,
  510. $row['email'],
  511. $row['username'],
  512. $row['password'],
  513. $row['official_code'],
  514. $language, //$row['language'],
  515. $row['phone'],
  516. null, //$row['picture'], //picture
  517. $row['auth_source'], // ?
  518. $expirationDateOnCreate, //'0000-00-00 00:00:00', //$row['expiration_date'], //$expiration_date = '0000-00-00 00:00:00',
  519. 1, //active
  520. 0,
  521. null, // extra
  522. null, //$encrypt_method = '',
  523. false //$send_mail = false
  524. );
  525. if ($result) {
  526. foreach ($row as $key => $value) {
  527. if (substr($key, 0, 6) === 'extra_') {
  528. //an extra field
  529. UserManager::update_extra_field_value(
  530. $result,
  531. substr($key, 6),
  532. $value
  533. );
  534. }
  535. }
  536. $this->logger->addInfo("Students - User created: ".$row['username']);
  537. } else {
  538. $this->logger->addError("Students - User NOT created: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  539. $this->logger->addError(strip_tags(Display::getFlashToString()));
  540. Display::cleanFlashMessages();
  541. }
  542. } else {
  543. if (empty($userInfo)) {
  544. $this->logger->addError("Students - Can't update user :".$row['username']);
  545. continue;
  546. }
  547. if (isset($row['action']) && $row['action'] === 'delete') {
  548. // Inactive one year later
  549. $userInfo['expiration_date'] = api_get_utc_datetime(api_strtotime(time() + 365*24*60*60));
  550. }
  551. $password = $row['password']; // change password
  552. $email = $row['email']; // change email
  553. $resetPassword = 2; // allow password change
  554. // Conditions that disables the update of password and email:
  555. if (isset($this->conditions['importStudents'])) {
  556. if (isset($this->conditions['importStudents']['update']) &&
  557. isset($this->conditions['importStudents']['update']['avoid'])
  558. ) {
  559. // Blocking email update -
  560. // 1. Condition
  561. $avoidUsersWithEmail = $this->conditions['importStudents']['update']['avoid']['email'];
  562. if ($userInfo['email'] != $row['email'] && in_array($row['email'], $avoidUsersWithEmail)) {
  563. $this->logger->addInfo("Students - User email is not updated : ".$row['username']." because the avoid conditions (email).");
  564. // Do not change email keep the old email.
  565. $email = $userInfo['email'];
  566. }
  567. // 2. Condition
  568. if (!in_array($userInfo['email'], $avoidUsersWithEmail) && !in_array($row['email'], $avoidUsersWithEmail)) {
  569. $email = $userInfo['email'];
  570. }
  571. // 3. Condition
  572. if (in_array($userInfo['email'], $avoidUsersWithEmail) && !in_array($row['email'], $avoidUsersWithEmail)) {
  573. $email = $row['email'];
  574. }
  575. // Blocking password update
  576. //$avoidUsersWithPassword = $this->conditions['importStudents']['update']['avoid']['password'];
  577. /*if (isset($row['password'])) {
  578. $user = api_get_user_entity($userInfo['id']);
  579. $encoded = UserManager::encryptPassword(
  580. $row['password'],
  581. $user
  582. );
  583. if ($userInfo['password'] != $encoded &&
  584. in_array($row['password'], $avoidUsersWithPassword)
  585. ) {
  586. $this->logger->addInfo(
  587. "Students - User password is not updated: ".$row['username']." because the avoid conditions (password)."
  588. );
  589. $password = null;
  590. $resetPassword = 0; // disallow password change
  591. }
  592. }*/
  593. }
  594. }
  595. // Always disallow password change during update
  596. $password = null;
  597. $resetPassword = 0; // disallow password change
  598. // Update user
  599. $result = UserManager::update_user(
  600. $userInfo['user_id'],
  601. $row['firstname'], // <<-- changed
  602. $row['lastname'], // <<-- changed
  603. $row['username'], // <<-- changed
  604. $password, //$password = null,
  605. $row['auth_source'],
  606. $email,
  607. STUDENT,
  608. $userInfo['official_code'],
  609. $userInfo['phone'],
  610. $userInfo['picture_uri'],
  611. $expirationDateOnUpdate,
  612. $userInfo['active'],
  613. null, //$creator_id = null,
  614. 0, //$hr_dept_id = 0,
  615. null, // $extra = null,
  616. null, //$language = 'english',
  617. null, //$encrypt_method = '',
  618. false, //$send_email = false,
  619. $resetPassword //$reset_password = 0
  620. );
  621. if ($result) {
  622. if ($row['username'] != $userInfo['username']) {
  623. $this->logger->addInfo("Students - Username was changes from '".$userInfo['username']."' to '".$row['username']."' ");
  624. }
  625. foreach ($row as $key => $value) {
  626. if (substr($key, 0, 6) === 'extra_') {
  627. //an extra field
  628. UserManager::update_extra_field_value(
  629. $userInfo['user_id'],
  630. substr($key, 6),
  631. $value
  632. );
  633. }
  634. }
  635. $this->logger->addInfo("Students - User updated: ".$row['username']);
  636. } else {
  637. $this->logger->addError("Students - User NOT updated: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
  638. }
  639. }
  640. if (($counter % $batchSize) === 0) {
  641. $em->flush();
  642. $em->clear(); // Detaches all objects from Doctrine!
  643. $this->logger->addInfo("Detaches all objects");
  644. }
  645. $counter++;
  646. }
  647. $em->clear(); // Detaches all objects from Doctrine!
  648. }
  649. $timeEnd = microtime(true);
  650. $executionTime = round(($timeEnd - $timeStart)/60, 2);
  651. $this->logger->addInfo("Execution Time for process students: $executionTime Min");
  652. if ($moveFile) {
  653. $this->moveFile($file);
  654. }
  655. }
  656. /**
  657. * @param string $file
  658. */
  659. private function importCoursesStatic($file, $moveFile, &$teacherBackup = array(), &$groupBackup = array())
  660. {
  661. $this->importCourses($file, false, $teacherBackup, $groupBackup);
  662. }
  663. /**
  664. * @param string $file
  665. * @param bool $moveFile
  666. *
  667. * @return int
  668. */
  669. private function importCalendarStatic($file, $moveFile = true)
  670. {
  671. $this->fixCSVFile($file);
  672. $data = Import::csvToArray($file);
  673. if (!empty($data)) {
  674. $this->logger->addInfo(count($data)." records found.");
  675. $eventsToCreate = array();
  676. $errorFound = false;
  677. foreach ($data as $row) {
  678. $sessionId = null;
  679. $externalSessionId = null;
  680. if (isset($row['external_sessionID'])) {
  681. $externalSessionId = $row['external_sessionID'];
  682. $sessionId = SessionManager::getSessionIdFromOriginalId(
  683. $externalSessionId,
  684. $this->extraFieldIdNameList['session']
  685. );
  686. }
  687. $courseCode = null;
  688. if (isset($row['coursecode'])) {
  689. $courseCode = $row['coursecode'];
  690. }
  691. $courseInfo = api_get_course_info($courseCode);
  692. if (empty($courseInfo)) {
  693. $this->logger->addInfo("Course '$courseCode' does not exists");
  694. }
  695. if (empty($sessionId)) {
  696. $this->logger->addInfo("external_sessionID: ".$externalSessionId." does not exists.");
  697. }
  698. $teacherId = null;
  699. if (!empty($sessionId) && !empty($courseInfo)) {
  700. $courseIncluded = SessionManager::relation_session_course_exist(
  701. $sessionId,
  702. $courseInfo['code']
  703. );
  704. if ($courseIncluded == false) {
  705. $this->logger->addInfo(
  706. "Course '$courseCode' is not included in session: $sessionId"
  707. );
  708. $errorFound = true;
  709. } else {
  710. $teachers = CourseManager::get_coach_list_from_course_code(
  711. $courseInfo['code'],
  712. $sessionId
  713. );
  714. // Getting first teacher.
  715. if (!empty($teachers)) {
  716. $teacher = current($teachers);
  717. $teacherId = $teacher['user_id'];
  718. } else {
  719. $sessionInfo = api_get_session_info($sessionId);
  720. $teacherId = $sessionInfo['id_coach'];
  721. }
  722. }
  723. } else {
  724. $errorFound = true;
  725. }
  726. if (empty($teacherId)) {
  727. $errorFound = true;
  728. $this->logger->addInfo(
  729. "No teacher found in course code : '$courseCode' and session: '$sessionId'"
  730. );
  731. }
  732. $date = $row['date'];
  733. $startTime = $row['time_start'];
  734. $endTime = $row['time_end'];
  735. $title = $row['title'];
  736. $comment = $row['comment'];
  737. $color = isset($row['color']) ? $row['color'] : '';
  738. $startDateYear = substr($date, 0, 4);
  739. $startDateMonth = substr($date, 4, 2);
  740. $startDateDay = substr($date, 6, 8);
  741. $startDate = $startDateYear.'-'.$startDateMonth.'-'.$startDateDay.' '.$startTime.":00";
  742. $endDate = $startDateYear.'-'.$startDateMonth.'-'.$startDateDay.' '.$endTime.":00";
  743. if (!api_is_valid_date($startDate) || !api_is_valid_date($endDate)) {
  744. $this->logger->addInfo(
  745. "Verify your dates: '$startDate' : '$endDate' "
  746. );
  747. $errorFound = true;
  748. }
  749. // If old events do nothing.
  750. /*if (api_strtotime($startDate) < time()) {
  751. continue;
  752. }*/
  753. if ($errorFound == false) {
  754. $eventsToCreate[] = array(
  755. 'start' => $startDate,
  756. 'end' => $endDate,
  757. 'title' => $title,
  758. 'sender_id' => $teacherId,
  759. 'course_id' => $courseInfo['real_id'],
  760. 'session_id' => $sessionId,
  761. 'comment' => $comment,
  762. 'color' => $color,
  763. $this->extraFieldIdNameList['calendar_event'] => $row['external_calendar_itemID'],
  764. );
  765. }
  766. $errorFound = false;
  767. }
  768. if (empty($eventsToCreate)) {
  769. $this->logger->addInfo(
  770. "No events to add"
  771. );
  772. return 0;
  773. }
  774. $this->logger->addInfo(
  775. "Ready to insert events"
  776. );
  777. $agenda = new Agenda();
  778. $extraFieldValue = new ExtraFieldValue('calendar_event');
  779. $extraFieldName = $this->extraFieldIdNameList['calendar_event'];
  780. $externalEventId = null;
  781. $extraField = new ExtraField('calendar_event');
  782. $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
  783. $extraFieldName
  784. );
  785. if (empty($extraFieldInfo)) {
  786. $this->logger->addInfo(
  787. "No calendar event extra field created: $extraFieldName"
  788. );
  789. return 0;
  790. }
  791. foreach ($eventsToCreate as $event) {
  792. $update = false;
  793. $item = null;
  794. if (!isset($event[$extraFieldName])) {
  795. $this->logger->addInfo(
  796. "No external_calendar_itemID found. Skipping ..."
  797. );
  798. continue;
  799. } else {
  800. $externalEventId = $event[$extraFieldName];
  801. if (empty($externalEventId)) {
  802. $this->logger->addInfo(
  803. "external_calendar_itemID was set but empty. Skipping ..."
  804. );
  805. continue;
  806. }
  807. $items = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
  808. $extraFieldName,
  809. $externalEventId,
  810. false,
  811. false,
  812. true
  813. );
  814. $item = null;
  815. foreach ($items as $tempItem) {
  816. if ($tempItem['item_id'] == $event['course_id']) {
  817. $item = $tempItem;
  818. }
  819. }
  820. if (!empty($item)) {
  821. $this->logger->addInfo(
  822. "Event #$externalEventId was already added. Updating ..."
  823. );
  824. $update = true;
  825. //continue;
  826. }
  827. }
  828. $courseInfo = api_get_course_info_by_id($event['course_id']);
  829. $agenda->set_course($courseInfo);
  830. $agenda->setType('course');
  831. $agenda->setSessionId($event['session_id']);
  832. $agenda->setSenderId($event['sender_id']);
  833. $agenda->setIsAllowedToEdit(true);
  834. $eventComment = $event['comment'];
  835. $color = $event['color'];
  836. // To use the event comment you need
  837. // ALTER TABLE c_calendar_event ADD COLUMN comment TEXT;
  838. // add in configuration.php allow_agenda_event_comment = true
  839. if (empty($courseInfo)) {
  840. $this->logger->addInfo(
  841. "No course found for added: #".$event['course_id']." Skipping ..."
  842. );
  843. continue;
  844. }
  845. if (empty($event['sender_id'])) {
  846. $this->logger->addInfo(
  847. "No sender found: #".$event['sender_id']." Skipping ..."
  848. );
  849. continue;
  850. }
  851. $content = '';
  852. if ($update && isset($item['calendar_event_id'])) {
  853. //the event already exists, just update
  854. $eventId = $agenda->edit_event(
  855. $item['calendar_event_id'],
  856. $event['start'],
  857. $event['end'],
  858. false,
  859. $event['title'],
  860. $content,
  861. array('everyone'), // send to
  862. array(), //$attachmentArray = array(),
  863. null, //$attachmentComment = null,
  864. $eventComment,
  865. $color
  866. );
  867. if ($eventId !== false) {
  868. $this->logger->addInfo(
  869. "Event updated: #$eventId"
  870. );
  871. } else {
  872. $this->logger->addInfo(
  873. "Error while updating event."
  874. );
  875. }
  876. } else {
  877. // New event. Create it.
  878. $eventId = $agenda->add_event(
  879. $event['start'],
  880. $event['end'],
  881. false,
  882. $event['title'],
  883. $content,
  884. array('everyone'), // send to
  885. false, //$addAsAnnouncement = false
  886. null, // $parentEventId
  887. array(), //$attachmentArray = array(),
  888. null, //$attachmentComment = null,
  889. $eventComment,
  890. $color
  891. );
  892. if (!empty($eventId)) {
  893. $extraFieldValue->is_course_model = true;
  894. $extraFieldValue->save(
  895. array(
  896. 'field_value' => $externalEventId,
  897. 'field_id' => $extraFieldInfo['id'],
  898. 'calendar_event_id' => $eventId,
  899. 'c_id' => $event['course_id']
  900. )
  901. );
  902. $this->logger->addInfo(
  903. "Event added: #$eventId"
  904. );
  905. } else {
  906. $this->logger->addInfo(
  907. "Error while creating event."
  908. );
  909. }
  910. }
  911. }
  912. }
  913. if ($moveFile) {
  914. $this->moveFile($file);
  915. }
  916. }
  917. /**
  918. * @param string $file
  919. * @param bool $moveFile
  920. * @param array $teacherBackup
  921. * @param array $groupBackup
  922. */
  923. private function importCourses(
  924. $file,
  925. $moveFile = true,
  926. &$teacherBackup = array(),
  927. &$groupBackup = array()
  928. ) {
  929. $this->fixCSVFile($file);
  930. $data = Import::csvToArray($file);
  931. if (!empty($data)) {
  932. $this->logger->addInfo(count($data)." records found.");
  933. foreach ($data as $row) {
  934. $row = $this->cleanCourseRow($row);
  935. $courseId = CourseManager::get_course_id_from_original_id(
  936. $row['extra_'.$this->extraFieldIdNameList['course']],
  937. $this->extraFieldIdNameList['course']
  938. );
  939. $courseInfo = api_get_course_info_by_id($courseId);
  940. if (empty($courseInfo)) {
  941. // Create
  942. $params = array();
  943. $params['title'] = $row['title'];
  944. $params['exemplary_content'] = false;
  945. $params['wanted_code'] = $row['course_code'];
  946. $params['course_category'] = $row['course_category'];
  947. $params['course_language'] = $row['language'];
  948. $params['teachers'] = $row['teachers'];
  949. $courseInfo = CourseManager::create_course(
  950. $params,
  951. $this->defaultAdminId
  952. );
  953. if (!empty($courseInfo)) {
  954. CourseManager::update_course_extra_field_value(
  955. $courseInfo['code'],
  956. 'external_course_id',
  957. $row['extra_'.$this->extraFieldIdNameList['course']]
  958. );
  959. $this->logger->addInfo("Courses - Course created ".$courseInfo['code']);
  960. } else {
  961. $this->logger->addError("Courses - Can't create course:".$row['title']);
  962. }
  963. } else {
  964. // Update
  965. $params = array(
  966. 'title' => $row['title'],
  967. 'category_code' => $row['course_category']
  968. );
  969. $result = CourseManager::update_attributes(
  970. $courseInfo['real_id'],
  971. $params
  972. );
  973. $addTeacherToSession = isset($courseInfo['add_teachers_to_sessions_courses']) && !empty($courseInfo['add_teachers_to_sessions_courses']) ? true : false;
  974. $teachers = $row['teachers'];
  975. if (!is_array($teachers)) {
  976. $teachers = array($teachers);
  977. }
  978. if ($addTeacherToSession) {
  979. CourseManager::updateTeachers(
  980. $courseInfo,
  981. $row['teachers'],
  982. false,
  983. true,
  984. false,
  985. $teacherBackup
  986. );
  987. } else {
  988. CourseManager::updateTeachers(
  989. $courseInfo,
  990. $row['teachers'],
  991. true,
  992. false,
  993. false,
  994. $teacherBackup
  995. );
  996. }
  997. foreach ($teachers as $teacherId) {
  998. if (isset($groupBackup['tutor'][$teacherId]) &&
  999. isset($groupBackup['tutor'][$teacherId][$courseInfo['code']])
  1000. ) {
  1001. foreach ($groupBackup['tutor'][$teacherId][$courseInfo['code']] as $data) {
  1002. GroupManager::subscribe_tutors(
  1003. array($teacherId),
  1004. $data['group_id'],
  1005. $data['c_id']
  1006. );
  1007. }
  1008. }
  1009. if (isset($groupBackup['user'][$teacherId]) &&
  1010. isset($groupBackup['user'][$teacherId][$courseInfo['code']]) &&
  1011. !empty($groupBackup['user'][$teacherId][$courseInfo['code']])
  1012. ) {
  1013. foreach ($groupBackup['user'][$teacherId][$courseInfo['code']] as $data) {
  1014. GroupManager::subscribe_users(
  1015. array($teacherId),
  1016. $data['group_id'],
  1017. $data['c_id']
  1018. );
  1019. }
  1020. }
  1021. }
  1022. if ($result) {
  1023. $this->logger->addInfo("Courses - Course updated ".$courseInfo['code']);
  1024. } else {
  1025. $this->logger->addError("Courses - Course NOT updated ".$courseInfo['code']);
  1026. }
  1027. }
  1028. }
  1029. }
  1030. if ($moveFile) {
  1031. $this->moveFile($file);
  1032. }
  1033. }
  1034. /**
  1035. * Parse filename: encora_subsessionsextid-static_31082016.csv
  1036. * @param string $file
  1037. */
  1038. private function importSubscribeUserToCourseSessionExtStatic($file)
  1039. {
  1040. $data = Import::csv_reader($file);
  1041. if (!empty($data)) {
  1042. $this->logger->addInfo(count($data)." records found.");
  1043. $userIdList = [];
  1044. foreach ($data as $row) {
  1045. $chamiloUserName = $row['UserName'];
  1046. $chamiloCourseCode = $row['CourseCode'];
  1047. $externalSessionId = $row['ExtSessionID'];
  1048. $status = $row['Status'];
  1049. $chamiloSessionId = null;
  1050. if (!empty($externalSessionId)) {
  1051. $chamiloSessionId = SessionManager::getSessionIdFromOriginalId(
  1052. $externalSessionId,
  1053. $this->extraFieldIdNameList['session']
  1054. );
  1055. }
  1056. $sessionInfo = api_get_session_info($chamiloSessionId);
  1057. if (empty($sessionInfo)) {
  1058. $this->logger->addError('Session does not exists: '.$chamiloSessionId);
  1059. continue;
  1060. }
  1061. $courseInfo = api_get_course_info($chamiloCourseCode);
  1062. if (empty($courseInfo)) {
  1063. $this->logger->addError('Course does not exists: '.$courseInfo);
  1064. continue;
  1065. }
  1066. $userId = UserManager::get_user_id_from_username($chamiloUserName);
  1067. if (empty($userId)) {
  1068. $this->logger->addError('User does not exists: '.$chamiloUserName);
  1069. continue;
  1070. }
  1071. switch ($status) {
  1072. case 'student':
  1073. SessionManager::subscribe_users_to_session_course(
  1074. array($userId),
  1075. $chamiloSessionId,
  1076. $courseInfo['code']
  1077. );
  1078. break;
  1079. case 'teacher':
  1080. SessionManager::set_coach_to_course_session(
  1081. $userId,
  1082. $chamiloSessionId,
  1083. $courseInfo['code']
  1084. );
  1085. break;
  1086. case 'drh':
  1087. $removeAllSessionsFromUser = true;
  1088. if (in_array($userId, $userIdList)) {
  1089. $removeAllSessionsFromUser = false;
  1090. } else {
  1091. $userIdList[] = $userId;
  1092. }
  1093. $userInfo = api_get_user_info($userId);
  1094. SessionManager::subscribeSessionsToDrh(
  1095. $userInfo,
  1096. [$chamiloSessionId],
  1097. false,
  1098. $removeAllSessionsFromUser
  1099. );
  1100. break;
  1101. }
  1102. $this->logger->addError(
  1103. "User '$chamiloUserName' was added as '$status' to Session: #$chamiloSessionId - Course: ".$courseInfo['code']
  1104. );
  1105. }
  1106. }
  1107. }
  1108. /**
  1109. * @param string $file
  1110. */
  1111. private function importUnsubSessionsExtIdStatic($file)
  1112. {
  1113. $data = Import::csv_reader($file);
  1114. if (!empty($data)) {
  1115. $this->logger->addInfo(count($data)." records found.");
  1116. foreach ($data as $row) {
  1117. $chamiloUserName = $row['UserName'];
  1118. $chamiloCourseCode = $row['CourseCode'];
  1119. $externalSessionId = $row['ExtSessionID'];
  1120. $dateStop = $row['DateStop'];
  1121. $chamiloSessionId = null;
  1122. if (!empty($externalSessionId)) {
  1123. $chamiloSessionId = SessionManager::getSessionIdFromOriginalId(
  1124. $externalSessionId,
  1125. $this->extraFieldIdNameList['session']
  1126. );
  1127. }
  1128. $sessionInfo = api_get_session_info($chamiloSessionId);
  1129. if (empty($sessionInfo)) {
  1130. $this->logger->addError('Session does not exists: '.$chamiloSessionId);
  1131. continue;
  1132. }
  1133. $courseInfo = api_get_course_info($chamiloCourseCode);
  1134. if (empty($courseInfo)) {
  1135. $this->logger->addError('Course does not exists: '.$courseInfo);
  1136. continue;
  1137. }
  1138. $userId = UserManager::get_user_id_from_username($chamiloUserName);
  1139. if (empty($userId)) {
  1140. $this->logger->addError('User does not exists: '.$chamiloUserName);
  1141. continue;
  1142. }
  1143. SessionManager::removeUsersFromCourseSession(
  1144. array($userId),
  1145. $chamiloSessionId,
  1146. $courseInfo
  1147. );
  1148. $this->logger->addError(
  1149. "User '$chamiloUserName' was remove from Session: #$chamiloSessionId - Course: " . $courseInfo['code']
  1150. );
  1151. }
  1152. }
  1153. }
  1154. /**
  1155. *
  1156. * @param string $file
  1157. */
  1158. private function importSessionsExtIdStatic($file)
  1159. {
  1160. $data = Import::csv_reader($file);
  1161. if (!empty($data)) {
  1162. $this->logger->addInfo(count($data)." records found.");
  1163. foreach ($data as $row) {
  1164. $chamiloUserName = $row['UserName'];
  1165. $chamiloCourseCode = $row['CourseCode'];
  1166. $externalSessionId = $row['ExtSessionID'];
  1167. $type = $row['Type'];
  1168. $chamiloSessionId = null;
  1169. if (!empty($externalSessionId)) {
  1170. $chamiloSessionId = SessionManager::getSessionIdFromOriginalId(
  1171. $externalSessionId,
  1172. $this->extraFieldIdNameList['session']
  1173. );
  1174. }
  1175. $sessionInfo = api_get_session_info($chamiloSessionId);
  1176. if (empty($sessionInfo)) {
  1177. $this->logger->addError('Session does not exists: '.$chamiloSessionId);
  1178. continue;
  1179. }
  1180. $courseInfo = api_get_course_info($chamiloCourseCode);
  1181. if (empty($courseInfo)) {
  1182. $this->logger->addError('Course does not exists: '.$courseInfo);
  1183. continue;
  1184. }
  1185. $userId = UserManager::get_user_id_from_username($chamiloUserName);
  1186. if (empty($userId)) {
  1187. $this->logger->addError('User does not exists: '.$chamiloUserName);
  1188. continue;
  1189. }
  1190. switch ($type) {
  1191. case 'student':
  1192. SessionManager::subscribe_users_to_session_course(
  1193. array($userId),
  1194. $chamiloSessionId,
  1195. $courseInfo['code'],
  1196. null,
  1197. false
  1198. );
  1199. break;
  1200. case 'teacher':
  1201. SessionManager::set_coach_to_course_session(
  1202. $userId,
  1203. $chamiloSessionId,
  1204. $courseInfo['code']
  1205. );
  1206. break;
  1207. }
  1208. $this->logger->addError(
  1209. "User '$chamiloUserName' with status $type was added to session: #$chamiloSessionId - Course: " . $courseInfo['code']
  1210. );
  1211. }
  1212. }
  1213. }
  1214. /**
  1215. * Updates the session synchronize with the csv file.
  1216. * @param string $file
  1217. */
  1218. private function importSessionsStatic($file)
  1219. {
  1220. $content = file($file);
  1221. $sessions = array();
  1222. $tag_names = array();
  1223. foreach ($content as $key => $enreg) {
  1224. $enreg = explode(';', trim($enreg));
  1225. if ($key) {
  1226. foreach ($tag_names as $tag_key => $tag_name) {
  1227. if (isset($enreg[$tag_key])) {
  1228. $sessions[$key - 1][$tag_name] = $enreg[$tag_key];
  1229. }
  1230. }
  1231. } else {
  1232. foreach ($enreg as $tag_name) {
  1233. $tag_names[] = api_preg_replace(
  1234. '/[^a-zA-Z0-9_\-]/',
  1235. '',
  1236. $tag_name
  1237. );
  1238. }
  1239. if (!in_array('SessionName', $tag_names) || !in_array(
  1240. 'DateStart',
  1241. $tag_names
  1242. ) || !in_array('DateEnd', $tag_names)
  1243. ) {
  1244. $error_message = get_lang('NoNeededData');
  1245. break;
  1246. }
  1247. }
  1248. }
  1249. if (!empty($sessions)) {
  1250. // Looping the sessions.
  1251. foreach ($sessions as $session) {
  1252. if (!empty($session['SessionID'])) {
  1253. $sessionId = SessionManager::getSessionIdFromOriginalId(
  1254. $session['SessionID'],
  1255. $this->extraFieldIdNameList['session']
  1256. );
  1257. $coachUserName = isset($session['Coach']) ? $session['Coach'] : null;
  1258. $categoryId = isset($session['category_id']) ? $session['category_id'] : null;
  1259. // 2014-06-30
  1260. $dateStart = explode('/', $session['DateStart']);
  1261. $dateEnd = explode('/', $session['DateEnd']);
  1262. $visibility = $this->defaultSessionVisibility;
  1263. $coachId = null;
  1264. if (!empty($coachUserName)) {
  1265. $coachInfo = api_get_user_info_from_username($coachUserName);
  1266. $coachId = $coachInfo['user_id'];
  1267. }
  1268. $dateStart = $dateStart[0].'-'.$dateStart[1].'-'.$dateStart[2].' 00:00:00';
  1269. $dateEnd = $dateEnd[0].'-'.$dateEnd[1].'-'.$dateEnd[2].' 23:59:59';
  1270. $date = new \DateTime($dateStart);
  1271. $interval = new DateInterval('P'.$this->daysCoachAccessBeforeBeginning.'D');
  1272. $date->sub($interval);
  1273. $coachBefore = $date->format('Y-m-d h:i');
  1274. $date = new \DateTime($dateStart);
  1275. $interval = new DateInterval('P'.$this->daysCoachAccessAfterBeginning.'D');
  1276. $date->add($interval);
  1277. $coachAfter = $date->format('Y-m-d h:i');
  1278. $dateStart = api_get_utc_datetime($dateStart);
  1279. $dateEnd = api_get_utc_datetime($dateEnd);
  1280. $coachBefore = api_get_utc_datetime($coachBefore);
  1281. $coachAfter = api_get_utc_datetime($coachAfter);
  1282. if (empty($sessionId)) {
  1283. $result = SessionManager::create_session(
  1284. $session['SessionName'],
  1285. $dateStart,
  1286. $dateEnd,
  1287. $dateStart,
  1288. $dateEnd,
  1289. $coachBefore,
  1290. $coachAfter,
  1291. $coachId,
  1292. $categoryId,
  1293. $visibility
  1294. );
  1295. if (is_numeric($result)) {
  1296. $sessionId = $result;
  1297. $this->logger->addInfo("Session #$sessionId created: ".$session['SessionName']);
  1298. SessionManager::update_session_extra_field_value(
  1299. $sessionId,
  1300. $this->extraFieldIdNameList['session'],
  1301. $session['SessionID']
  1302. );
  1303. } else {
  1304. $this->logger->addInfo("Failed creating session: ".$session['SessionName']);
  1305. }
  1306. } else {
  1307. $sessionInfo = api_get_session_info($sessionId);
  1308. $accessBefore = null;
  1309. $accessAfter = null;
  1310. if (empty($sessionInfo['nb_days_access_before_beginning']) ||
  1311. (!empty($sessionInfo['nb_days_access_before_beginning']) &&
  1312. $sessionInfo['nb_days_access_before_beginning'] < $this->daysCoachAccessBeforeBeginning)
  1313. ) {
  1314. $accessBefore = $coachBefore;
  1315. }
  1316. $accessAfter = null;
  1317. if (empty($sessionInfo['nb_days_access_after_end']) ||
  1318. (!empty($sessionInfo['nb_days_access_after_end']) &&
  1319. $sessionInfo['nb_days_access_after_end'] < $this->daysCoachAccessAfterBeginning)
  1320. ) {
  1321. $accessAfter = $coachAfter;
  1322. }
  1323. $showDescription = isset($sessionInfo['show_description']) ? $sessionInfo['show_description'] : 1;
  1324. $result = SessionManager::edit_session(
  1325. $sessionId,
  1326. $session['SessionName'],
  1327. $dateStart,
  1328. $dateEnd,
  1329. $dateStart,
  1330. $dateEnd,
  1331. $accessBefore,
  1332. $accessAfter,
  1333. $coachId,
  1334. $categoryId,
  1335. $visibility,
  1336. null, //$description = null,
  1337. $showDescription
  1338. );
  1339. if (is_numeric($result)) {
  1340. $this->logger->addInfo("Session #$sessionId updated: ".$session['SessionName']);
  1341. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  1342. $params = array(
  1343. 'description' => $session['SessionDescription']
  1344. );
  1345. Database::update(
  1346. $tbl_session,
  1347. $params,
  1348. array('id = ?' => $sessionId)
  1349. );
  1350. }
  1351. }
  1352. if (!empty($sessionId)) {
  1353. // Courses
  1354. $courses = explode('|', $session['Courses']);
  1355. $courseList = [];
  1356. $courseListWithCoach = [];
  1357. foreach ($courses as $course) {
  1358. $courseArray = bracketsToArray($course);
  1359. $courseCode = $courseArray[0];
  1360. if (CourseManager::course_exists($courseCode)) {
  1361. $courseInfo = api_get_course_info($courseCode);
  1362. $courseList[] = $courseInfo['real_id'];
  1363. // Extracting course coaches
  1364. $courseCoaches = isset($courseArray[1]) ? $courseArray[1] : null;
  1365. $courseCoaches = explode(',', $courseCoaches);
  1366. // Extracting students
  1367. $courseUsers = isset($courseArray[2]) ? $courseArray[2] : null;
  1368. $courseUsers = explode(',', $courseUsers);
  1369. $courseListWithCoach[] = [
  1370. 'course_info' => $courseInfo,
  1371. 'coaches' => $courseCoaches,
  1372. 'course_users' => $courseUsers
  1373. ];
  1374. }
  1375. }
  1376. SessionManager::add_courses_to_session(
  1377. $sessionId,
  1378. $courseList,
  1379. true
  1380. );
  1381. $this->logger->addInfo("Session #$sessionId: Courses added: '".implode(', ', $courseList)."'");
  1382. if (empty($courseListWithCoach)) {
  1383. $this->logger->addInfo("No users/coaches to update");
  1384. continue;
  1385. }
  1386. foreach ($courseListWithCoach as $courseData) {
  1387. $courseInfo = $courseData['course_info'];
  1388. $courseCode = $courseInfo['code'];
  1389. $courseId = $courseInfo['real_id'];
  1390. $courseCoaches = $courseData['coaches'];
  1391. $courseUsers = $courseData['course_users'];
  1392. // Coaches
  1393. if (!empty($courseCoaches)) {
  1394. $coachList = array();
  1395. foreach ($courseCoaches as $courseCoach) {
  1396. $courseCoachId = UserManager::get_user_id_from_username(
  1397. $courseCoach
  1398. );
  1399. if ($courseCoachId !== false) {
  1400. // Just insert new coaches
  1401. $coachList[] = $courseCoachId;
  1402. }
  1403. }
  1404. $this->logger->addInfo("Session #$sessionId: course '$courseCode' coaches added: '".implode(', ', $coachList)."'");
  1405. SessionManager::updateCoaches(
  1406. $sessionId,
  1407. $courseId,
  1408. $coachList,
  1409. true
  1410. );
  1411. } else {
  1412. $this->logger->addInfo("No coaches added");
  1413. }
  1414. // Students
  1415. if (!empty($courseUsers)) {
  1416. $userList = array();
  1417. foreach ($courseUsers as $username) {
  1418. $userInfo = api_get_user_info_from_username(trim($username));
  1419. if (!empty($userInfo)) {
  1420. $userList[] = $userInfo['user_id'];
  1421. }
  1422. }
  1423. $this->logger->addInfo("Session #$sessionId: course '$courseCode': Students added '".implode(', ', $userList)."'");
  1424. SessionManager::subscribe_users_to_session_course(
  1425. $userList,
  1426. $sessionId,
  1427. $courseCode,
  1428. SESSION_VISIBLE_READ_ONLY,
  1429. true
  1430. );
  1431. } else {
  1432. $this->logger->addInfo("No users to register.");
  1433. }
  1434. }
  1435. } else {
  1436. $this->logger->addInfo(
  1437. 'SessionID not found in system.'
  1438. );
  1439. }
  1440. } else {
  1441. $this->logger->addInfo('SessionID does not exists');
  1442. }
  1443. }
  1444. } else {
  1445. $this->logger->addInfo($error_message);
  1446. }
  1447. }
  1448. /**
  1449. * @param string $file
  1450. * @param bool $moveFile
  1451. * @param array $teacherBackup
  1452. * @param array $groupBackup
  1453. */
  1454. private function importSessions(
  1455. $file,
  1456. $moveFile = true,
  1457. &$teacherBackup = array(),
  1458. &$groupBackup = array()
  1459. ) {
  1460. $avoid = null;
  1461. if (isset($this->conditions['importSessions']) &&
  1462. isset($this->conditions['importSessions']['update'])
  1463. ) {
  1464. $avoid = $this->conditions['importSessions']['update'];
  1465. }
  1466. $result = SessionManager::importCSV(
  1467. $file,
  1468. true,
  1469. $this->defaultAdminId,
  1470. $this->logger,
  1471. array('SessionID' => 'extra_'.$this->extraFieldIdNameList['session']),
  1472. $this->extraFieldIdNameList['session'],
  1473. $this->daysCoachAccessBeforeBeginning,
  1474. $this->daysCoachAccessAfterBeginning,
  1475. $this->defaultSessionVisibility,
  1476. $avoid,
  1477. false, // deleteUsersNotInList
  1478. false, // updateCourseCoaches
  1479. true, // sessionWithCoursesModifier
  1480. true, //$addOriginalCourseTeachersAsCourseSessionCoaches
  1481. true, //$removeAllTeachersFromCourse
  1482. 1, // $showDescription,
  1483. $teacherBackup,
  1484. $groupBackup
  1485. );
  1486. if (!empty($result['error_message'])) {
  1487. $this->logger->addError($result['error_message']);
  1488. }
  1489. $this->logger->addInfo("Sessions - Sessions parsed: ".$result['session_counter']);
  1490. if ($moveFile) {
  1491. $this->moveFile($file);
  1492. }
  1493. }
  1494. /**
  1495. * @param string $file
  1496. */
  1497. private function importSubscribeStatic($file)
  1498. {
  1499. $data = Import::csv_reader($file);
  1500. if (!empty($data)) {
  1501. $this->logger->addInfo(count($data) . " records found.");
  1502. foreach ($data as $row) {
  1503. $chamiloUserName = $row['UserName'];
  1504. $chamiloCourseCode = $row['CourseCode'];
  1505. $chamiloSessionId = $row['SessionID'];
  1506. $type = $row['Type'];
  1507. $sessionInfo = api_get_session_info($chamiloSessionId);
  1508. if (empty($sessionInfo)) {
  1509. $this->logger->addError('Session does not exists: '.$chamiloSessionId);
  1510. continue;
  1511. }
  1512. $courseInfo = api_get_course_info($chamiloCourseCode);
  1513. if (empty($courseInfo)) {
  1514. $this->logger->addError('Course does not exists: '.$courseInfo);
  1515. continue;
  1516. }
  1517. $userId = UserManager::get_user_id_from_username($chamiloUserName);
  1518. if (empty($userId)) {
  1519. $this->logger->addError('User does not exists: '.$chamiloUserName);
  1520. continue;
  1521. }
  1522. switch ($type) {
  1523. case 'student':
  1524. SessionManager::subscribe_users_to_session_course(
  1525. array($userId),
  1526. $chamiloSessionId,
  1527. $courseInfo['code'],
  1528. null,
  1529. false
  1530. );
  1531. break;
  1532. case 'teacher':
  1533. SessionManager::set_coach_to_course_session(
  1534. $userId,
  1535. $chamiloSessionId,
  1536. $courseInfo['real_id']
  1537. );
  1538. break;
  1539. }
  1540. $this->logger->addError(
  1541. "User '$chamiloUserName' with status $type was added to session: #$chamiloSessionId - Course: " . $courseInfo['code']
  1542. );
  1543. }
  1544. }
  1545. }
  1546. /**
  1547. * @param $file
  1548. * @param bool $moveFile
  1549. */
  1550. private function importSubscribeUserToCourse($file, $moveFile = false)
  1551. {
  1552. $data = Import::csv_reader($file);
  1553. if (!empty($data)) {
  1554. $this->logger->addInfo(count($data) . " records found.");
  1555. foreach ($data as $row) {
  1556. $chamiloUserName = $row['UserName'];
  1557. $chamiloCourseCode = $row['CourseCode'];
  1558. $status = $row['Status'];
  1559. $courseInfo = api_get_course_info($chamiloCourseCode);
  1560. if (empty($courseInfo)) {
  1561. $this->logger->addError(
  1562. 'Course does not exists: '.$chamiloCourseCode
  1563. );
  1564. continue;
  1565. }
  1566. $userId = UserManager::get_user_id_from_username(
  1567. $chamiloUserName
  1568. );
  1569. if (empty($userId)) {
  1570. $this->logger->addError(
  1571. 'User does not exists: '.$chamiloUserName
  1572. );
  1573. continue;
  1574. }
  1575. CourseManager::subscribe_user(
  1576. $userId,
  1577. $courseInfo['code'],
  1578. $status
  1579. );
  1580. $this->logger->addInfo(
  1581. "User $userId added to course $chamiloCourseCode as $status"
  1582. );
  1583. }
  1584. }
  1585. }
  1586. /**
  1587. * @param string $file
  1588. */
  1589. private function importUnsubscribeStatic(
  1590. $file,
  1591. $moveFile = false,
  1592. &$teacherBackup = array(),
  1593. &$groupBackup = array()
  1594. ) {
  1595. $data = Import::csv_reader($file);
  1596. if (!empty($data)) {
  1597. $this->logger->addInfo(count($data)." records found.");
  1598. foreach ($data as $row) {
  1599. $chamiloUserName = $row['UserName'];
  1600. $chamiloCourseCode = $row['CourseCode'];
  1601. $chamiloSessionId = $row['SessionID'];
  1602. $sessionInfo = api_get_session_info($chamiloSessionId);
  1603. if (empty($sessionInfo)) {
  1604. $this->logger->addError('Session does not exists: '.$chamiloSessionId);
  1605. continue;
  1606. }
  1607. $courseInfo = api_get_course_info($chamiloCourseCode);
  1608. if (empty($courseInfo)) {
  1609. $this->logger->addError('Course does not exists: '.$courseInfo);
  1610. continue;
  1611. }
  1612. $userId = UserManager::get_user_id_from_username($chamiloUserName);
  1613. if (empty($userId)) {
  1614. $this->logger->addError('User does not exists: '.$chamiloUserName);
  1615. continue;
  1616. }
  1617. $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  1618. WHERE
  1619. user_id = ".$userId." AND
  1620. course_code = '".$courseInfo['code']."'
  1621. ";
  1622. $result = Database::query($sql);
  1623. $userCourseData = Database::fetch_array($result, 'ASSOC');
  1624. $teacherBackup[$userId][$courseInfo['code']] = $userCourseData;
  1625. $sql = "SELECT * FROM ".Database::get_course_table(
  1626. TABLE_GROUP_USER
  1627. )."
  1628. WHERE
  1629. user_id = ".$userId." AND
  1630. c_id = '".$courseInfo['real_id']."'
  1631. ";
  1632. $result = Database::query($sql);
  1633. while ($groupData = Database::fetch_array($result, 'ASSOC')) {
  1634. $groupBackup['user'][$userId][$courseInfo['code']][$groupData['group_id']] = $groupData;
  1635. }
  1636. $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)."
  1637. WHERE
  1638. user_id = ".$userId." AND
  1639. c_id = '".$courseInfo['real_id']."'
  1640. ";
  1641. $result = Database::query($sql);
  1642. while ($groupData = Database::fetch_array($result, 'ASSOC')) {
  1643. $groupBackup['tutor'][$userId][$courseInfo['code']][$groupData['group_id']] = $groupData;
  1644. }
  1645. CourseManager::unsubscribe_user(
  1646. $userId,
  1647. $courseInfo['code'],
  1648. $chamiloSessionId
  1649. );
  1650. $this->logger->addError(
  1651. "User '$chamiloUserName' was removed from session: #$chamiloSessionId, Course: ".$courseInfo['code']
  1652. );
  1653. }
  1654. }
  1655. }
  1656. /**
  1657. * Dump database tables
  1658. */
  1659. private function dumpDatabaseTables()
  1660. {
  1661. echo 'Dumping tables'.PHP_EOL;
  1662. // User
  1663. $table = Database::get_main_table(TABLE_MAIN_USER);
  1664. $tableAdmin = Database::get_main_table(TABLE_MAIN_ADMIN);
  1665. $sql = "DELETE FROM $table
  1666. WHERE user_id not in (select user_id from $tableAdmin) and status <> ".ANONYMOUS;
  1667. Database::query($sql);
  1668. echo $sql.PHP_EOL;
  1669. // Truncate tables
  1670. $truncateTables = array(
  1671. Database::get_main_table(TABLE_MAIN_COURSE),
  1672. Database::get_main_table(TABLE_MAIN_COURSE_USER),
  1673. Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE),
  1674. Database::get_main_table(TABLE_MAIN_CATEGORY),
  1675. Database::get_main_table(TABLE_MAIN_COURSE_MODULE),
  1676. Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER),
  1677. Database::get_main_table(TABLE_MAIN_SESSION),
  1678. Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY),
  1679. Database::get_main_table(TABLE_MAIN_SESSION_COURSE),
  1680. Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER),
  1681. Database::get_main_table(TABLE_MAIN_SESSION_USER),
  1682. Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION),
  1683. Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES),
  1684. Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES),
  1685. Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES),
  1686. Database::get_main_table(TABLE_MAIN_USER_FIELD),
  1687. Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS),
  1688. Database::get_main_table(TABLE_MAIN_COURSE_FIELD),
  1689. Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES),
  1690. Database::get_main_table(TABLE_MAIN_SESSION_FIELD),
  1691. Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES),
  1692. Database::get_course_table(TABLE_AGENDA),
  1693. Database::get_course_table(TABLE_AGENDA_ATTACHMENT),
  1694. Database::get_course_table(TABLE_AGENDA_REPEAT),
  1695. Database::get_course_table(TABLE_AGENDA_REPEAT_NOT),
  1696. Database::get_main_table(TABLE_PERSONAL_AGENDA),
  1697. Database::get_main_table(TABLE_PERSONAL_AGENDA_REPEAT_NOT),
  1698. Database::get_main_table(TABLE_PERSONAL_AGENDA_REPEAT),
  1699. Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_VALUES),
  1700. Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS),
  1701. Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS),
  1702. Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN),
  1703. Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS),
  1704. Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES),
  1705. Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT),
  1706. Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING),
  1707. Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT),
  1708. Database::get_main_table(TABLE_STATISTIC_TRACK_E_UPLOADS),
  1709. Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT),
  1710. Database::get_main_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY),
  1711. Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY),
  1712. Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION),
  1713. Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG),
  1714. Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT),
  1715. Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT_LOG),
  1716. Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK),
  1717. Database::get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY),
  1718. Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE),
  1719. Database::get_course_table(TABLE_STUDENT_PUBLICATION),
  1720. Database::get_course_table(TABLE_QUIZ_QUESTION),
  1721. Database::get_course_table(TABLE_QUIZ_TEST),
  1722. Database::get_course_table(TABLE_QUIZ_ORDER),
  1723. Database::get_course_table(TABLE_QUIZ_ANSWER),
  1724. Database::get_course_table(TABLE_QUIZ_TEST_QUESTION),
  1725. Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION),
  1726. Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY),
  1727. Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY),
  1728. Database::get_course_table(TABLE_LP_MAIN),
  1729. Database::get_course_table(TABLE_LP_ITEM),
  1730. Database::get_course_table(TABLE_LP_VIEW),
  1731. Database::get_course_table(TABLE_LP_ITEM_VIEW),
  1732. Database::get_course_table(TABLE_DOCUMENT),
  1733. Database::get_course_table(TABLE_ITEM_PROPERTY),
  1734. Database::get_course_table(TABLE_TOOL_LIST),
  1735. Database::get_course_table(TABLE_TOOL_INTRO),
  1736. Database::get_course_table(TABLE_COURSE_SETTING),
  1737. Database::get_course_table(TABLE_SURVEY),
  1738. Database::get_course_table(TABLE_SURVEY_QUESTION),
  1739. Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION),
  1740. Database::get_course_table(TABLE_SURVEY_INVITATION),
  1741. Database::get_course_table(TABLE_SURVEY_ANSWER),
  1742. Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP),
  1743. Database::get_course_table(TABLE_SURVEY_REPORT),
  1744. Database::get_course_table(TABLE_GLOSSARY),
  1745. Database::get_course_table(TABLE_LINK),
  1746. Database::get_course_table(TABLE_LINK_CATEGORY),
  1747. Database::get_course_table(TABLE_GROUP),
  1748. Database::get_course_table(TABLE_GROUP_USER),
  1749. Database::get_course_table(TABLE_GROUP_TUTOR),
  1750. Database::get_course_table(TABLE_GROUP_CATEGORY),
  1751. Database::get_course_table(TABLE_DROPBOX_CATEGORY),
  1752. Database::get_course_table(TABLE_DROPBOX_FEEDBACK),
  1753. Database::get_course_table(TABLE_DROPBOX_POST),
  1754. Database::get_course_table(TABLE_DROPBOX_FILE),
  1755. Database::get_course_table(TABLE_DROPBOX_PERSON)
  1756. );
  1757. foreach ($truncateTables as $table) {
  1758. $sql = "TRUNCATE $table";
  1759. Database::query($sql);
  1760. echo $sql.PHP_EOL;
  1761. }
  1762. $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  1763. $sql = "DELETE FROM $table WHERE tool = 'calendar_event'";
  1764. Database::query($sql);
  1765. echo $sql.PHP_EOL;
  1766. }
  1767. /**
  1768. * If csv file ends with '"' character then a '";' is added
  1769. * @param string $file
  1770. */
  1771. private function fixCSVFile($file)
  1772. {
  1773. /*$f = fopen($file, 'r+');
  1774. $cursor = -1;
  1775. fseek($f, $cursor, SEEK_END);
  1776. $char = fgetc($f);
  1777. while ($char === "\n" || $char === "\r") {
  1778. fseek($f, $cursor--, SEEK_END);
  1779. $char = fgetc($f);
  1780. }
  1781. if ($char === "\"") {
  1782. fseek($f, -1, SEEK_CUR);
  1783. fwrite($f, '";');
  1784. }*/
  1785. }
  1786. }
  1787. use Monolog\Logger;
  1788. use Monolog\Handler\StreamHandler;
  1789. use Monolog\Handler\NativeMailerHandler;
  1790. use Monolog\Handler\RotatingFileHandler;
  1791. use Monolog\Handler\BufferHandler;
  1792. $logger = new Logger('cron');
  1793. $emails = isset($_configuration['cron_notification_mails']) ? $_configuration['cron_notification_mails'] : null;
  1794. $minLevel = Logger::DEBUG;
  1795. if (!is_array($emails)) {
  1796. $emails = array($emails);
  1797. }
  1798. $subject = "Cron main/cron/import_csv.php ".date('Y-m-d h:i:s');
  1799. $from = api_get_setting('emailAdministrator');
  1800. /*
  1801. if (!empty($emails)) {
  1802. foreach ($emails as $email) {
  1803. $stream = new NativeMailerHandler($email, $subject, $from, $minLevel);
  1804. $logger->pushHandler(new BufferHandler($stream, 0, $minLevel));
  1805. }
  1806. }*/
  1807. $stream = new StreamHandler(
  1808. api_get_path(SYS_ARCHIVE_PATH).'import_csv.log',
  1809. $minLevel
  1810. );
  1811. $logger->pushHandler(new BufferHandler($stream, 0, $minLevel));
  1812. $logger->pushHandler(new RotatingFileHandler('import_csv', 5, $minLevel));
  1813. $cronImportCSVConditions = isset($_configuration['cron_import_csv_conditions']) ? $_configuration['cron_import_csv_conditions'] : null;
  1814. echo 'See the error log here: '.api_get_path(SYS_ARCHIVE_PATH).'import_csv.log'."\n";
  1815. $import = new ImportCsv($logger, $cronImportCSVConditions);
  1816. if (isset($_configuration['default_admin_user_id_for_cron'])) {
  1817. $import->defaultAdminId = $_configuration['default_admin_user_id_for_cron'];
  1818. }
  1819. // @todo in production disable the dump option
  1820. $dump = false;
  1821. if (isset($argv[1]) && $argv[1] = '--dump') {
  1822. $dump = true;
  1823. }
  1824. if (isset($_configuration['import_csv_disable_dump']) &&
  1825. $_configuration['import_csv_disable_dump'] == true
  1826. ) {
  1827. $import->setDumpValues(false);
  1828. } else {
  1829. $import->setDumpValues($dump);
  1830. }
  1831. // Do not moves the files to treated
  1832. if (isset($_configuration['import_csv_test'])) {
  1833. $import->test = $_configuration['import_csv_test'];
  1834. } else {
  1835. $import->test = true;
  1836. }
  1837. $timeStart = microtime(true);
  1838. $import->run();
  1839. $timeEnd = microtime(true);
  1840. $executionTime = round(($timeEnd - $timeStart) / 60, 2);
  1841. $logger->addInfo("Total execution Time $executionTime Min");
  1842. if (isset($_configuration['import_csv_fix_permissions']) &&
  1843. $_configuration['import_csv_fix_permissions'] == true
  1844. ) {
  1845. $command = "sudo find ".api_get_path(SYS_COURSE_PATH)." -type d -exec chmod 777 {} \; ";
  1846. echo "Executing: ".$command.PHP_EOL;
  1847. system($command);
  1848. $command = "sudo find ".api_get_path(SYS_CODE_PATH)."upload/users -type d -exec chmod 777 {} \;";
  1849. echo "Executing: ".$command.PHP_EOL;
  1850. system($command);
  1851. }