import_csv.php 123 KB

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