import_csv.php 115 KB

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