import_csv.php 98 KB

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