import_csv.php 124 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126
  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. $eventsToCreateFinal = [];
  1071. foreach ($eventsToCreate as $event) {
  1072. $update = false;
  1073. $item = null;
  1074. if (!isset($event[$extraFieldName])) {
  1075. $this->logger->addInfo('No external_calendar_itemID found. Skipping ...');
  1076. continue;
  1077. } else {
  1078. $externalEventId = $event[$extraFieldName];
  1079. if (empty($externalEventId)) {
  1080. $this->logger->addInfo('external_calendar_itemID was set but empty. Skipping ...');
  1081. continue;
  1082. }
  1083. $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
  1084. $extraFieldName,
  1085. $externalEventId,
  1086. false,
  1087. false,
  1088. false
  1089. );
  1090. if (!empty($item)) {
  1091. $update = true;
  1092. }
  1093. }
  1094. $courseInfo = api_get_course_info_by_id($event['course_id']);
  1095. $event['course_info'] = $courseInfo;
  1096. $event['update'] = $update;
  1097. $event['item'] = $item;
  1098. $calendarEvent = null;
  1099. /* Check if event changed of course code */
  1100. if (!empty($item) && isset($item['item_id']) && !empty($item['item_id'])) {
  1101. /** @var CCalendarEvent $calendarEvent */
  1102. $calendarEvent = $em->getRepository('ChamiloCourseBundle:CCalendarEvent')->find($item['item_id']);
  1103. }
  1104. if ($calendarEvent) {
  1105. $this->logger->addInfo('Calendar event found '.$item['item_id']);
  1106. if ($calendarEvent->getCId() != $courseInfo['real_id']) {
  1107. $this->logger->addInfo('Move from course #'.$calendarEvent->getCId().' to #'.$courseInfo['real_id']);
  1108. // Seems that the course id changed in the csv
  1109. $calendarEvent->setCId($courseInfo['real_id']);
  1110. $em->persist($calendarEvent);
  1111. $em->flush();
  1112. $criteria = [
  1113. 'tool' => 'calendar_event',
  1114. 'ref' => $item['item_id'],
  1115. ];
  1116. /** @var CItemProperty $itemProperty */
  1117. $itemProperty = $em->getRepository('ChamiloCourseBundle:CItemProperty')->findOneBy($criteria);
  1118. $courseEntity = $em->getRepository('ChamiloCoreBundle:Course')->find($courseInfo['real_id']);
  1119. if ($itemProperty && $courseEntity) {
  1120. $itemProperty->setCourse($courseEntity);
  1121. $em->persist($itemProperty);
  1122. $em->flush();
  1123. }
  1124. }
  1125. // Checking if session still exists
  1126. $calendarSessionId = (int) $calendarEvent->getSessionId();
  1127. if (!empty($calendarSessionId)) {
  1128. $calendarSessionInfo = api_get_session_info($calendarSessionId);
  1129. if (empty($calendarSessionInfo)) {
  1130. $calendarId = (int) $calendarEvent->getIid();
  1131. // Delete calendar events because the session was deleted!
  1132. $this->logger->addInfo(
  1133. "Delete event # $calendarId because session # $calendarSessionId doesn't exist"
  1134. );
  1135. $sql = "DELETE FROM c_calendar_event
  1136. WHERE iid = $calendarId AND session_id = $calendarSessionId";
  1137. Database::query($sql);
  1138. $this->logger->addInfo($sql);
  1139. $sql = "DELETE FROM c_item_property
  1140. WHERE
  1141. tool = 'calendar_event' AND
  1142. ref = $calendarSessionId AND
  1143. session_id = $calendarSessionId";
  1144. Database::query($sql);
  1145. $this->logger->addInfo($sql);
  1146. }
  1147. }
  1148. } else {
  1149. $this->logger->addInfo('Calendar event not found '.$item['item_id']);
  1150. }
  1151. $event['external_event_id'] = $externalEventId;
  1152. if (isset($eventStartDateList[$courseInfo['real_id']]) &&
  1153. isset($eventStartDateList[$courseInfo['real_id']][$event['session_id']])
  1154. ) {
  1155. $currentItemDate = api_strtotime($event['start']);
  1156. $firstDate = $eventStartDateList[$courseInfo['real_id']][$event['session_id']];
  1157. if ($currentItemDate < api_strtotime($firstDate)) {
  1158. $eventStartDateList[$courseInfo['real_id']][$event['session_id']] = $event['start'];
  1159. $eventEndDateList[$courseInfo['real_id']][$event['session_id']] = $event['end'];
  1160. }
  1161. } else {
  1162. // First time
  1163. $eventStartDateList[$courseInfo['real_id']][$event['session_id']] = $event['start'];
  1164. $eventEndDateList[$courseInfo['real_id']][$event['session_id']] = $event['end'];
  1165. }
  1166. $eventsToCreateFinal[] = $event;
  1167. }
  1168. $eventAlreadySent = [];
  1169. $tpl = new Template(null, false, false, false, false, false, false);
  1170. foreach ($eventsToCreateFinal as $event) {
  1171. $courseInfo = $event['course_info'];
  1172. $item = $event['item'];
  1173. $update = $event['update'];
  1174. $externalEventId = $event['external_event_id'];
  1175. $info = 'Course: '.$courseInfo['real_id'].' ('.$courseInfo['code'].') - Session: '.$event['session_id'].' external event id: '.$externalEventId;
  1176. $agenda = new Agenda(
  1177. 'course',
  1178. $event['sender_id'],
  1179. $courseInfo['real_id'],
  1180. $event['session_id']
  1181. );
  1182. $agenda->set_course($courseInfo);
  1183. $agenda->setSessionId($event['session_id']);
  1184. $agenda->setSenderId($event['sender_id']);
  1185. $agenda->setIsAllowedToEdit(true);
  1186. $eventComment = $event['comment'];
  1187. $color = $event['color'];
  1188. // To use the event comment you need
  1189. // ALTER TABLE c_calendar_event ADD COLUMN comment TEXT;
  1190. // add in configuration.php allow_agenda_event_comment = true
  1191. if (empty($courseInfo)) {
  1192. $this->logger->addInfo(
  1193. "No course found for event #$externalEventId Course #".$event['course_id']." Skipping ..."
  1194. );
  1195. continue;
  1196. }
  1197. if (empty($event['sender_id'])) {
  1198. $this->logger->addInfo(
  1199. "No sender found for event #$externalEventId Send #".$event['sender_id']." Skipping ..."
  1200. );
  1201. continue;
  1202. }
  1203. // Taking first element of course-session event
  1204. $alreadyAdded = false;
  1205. $firstDate = $eventStartDateList[$courseInfo['real_id']][$event['session_id']];
  1206. $firstEndDate = $eventEndDateList[$courseInfo['real_id']][$event['session_id']];
  1207. if (isset($eventAlreadySent[$courseInfo['real_id']]) &&
  1208. isset($eventAlreadySent[$courseInfo['real_id']][$event['session_id']])
  1209. ) {
  1210. $alreadyAdded = true;
  1211. } else {
  1212. $eventAlreadySent[$courseInfo['real_id']][$event['session_id']] = true;
  1213. }
  1214. // Working days (Mon-Fri)see BT#12156#note-16
  1215. $days = 3;
  1216. $startDatePlusDays = api_strtotime("$days weekdays");
  1217. /*
  1218. $timePart = date('H:i:s', api_strtotime('now'));
  1219. $datePart = date('Y-m-d', api_strtotime("$days weekdays"));
  1220. $startDatePlusDays = "$timePart $datePart";
  1221. */
  1222. $this->logger->addInfo(
  1223. 'startDatePlusDays: '.api_get_utc_datetime($startDatePlusDays).' - First date: '.$firstDate
  1224. );
  1225. // Send
  1226. $sendMail = false;
  1227. if ($startDatePlusDays > api_strtotime($firstDate)) {
  1228. $sendMail = true;
  1229. }
  1230. // Send announcement to users
  1231. if ($sendMail && $alreadyAdded == false) {
  1232. $start = $firstDate;
  1233. $end = $firstEndDate;
  1234. if (!empty($end) &&
  1235. api_format_date($start, DATE_FORMAT_LONG) ==
  1236. api_format_date($end, DATE_FORMAT_LONG)
  1237. ) {
  1238. $date = api_format_date($start, DATE_FORMAT_LONG).' ('.
  1239. api_format_date($start, TIME_NO_SEC_FORMAT).' '.
  1240. api_format_date($end, TIME_NO_SEC_FORMAT).')';
  1241. } else {
  1242. $date = api_format_date($start, DATE_TIME_FORMAT_LONG_24H).' - '.
  1243. api_format_date($end, DATE_TIME_FORMAT_LONG_24H);
  1244. }
  1245. $sessionName = '';
  1246. $sessionId = isset($event['session_id']) && !empty($event['session_id']) ? $event['session_id'] : 0;
  1247. if (!empty($sessionId)) {
  1248. $sessionName = api_get_session_name($sessionId);
  1249. }
  1250. $courseTitle = $courseInfo['title'];
  1251. $sessionExtraFieldValue = new ExtraFieldValue('session');
  1252. $values = $sessionExtraFieldValue->get_values_by_handler_and_field_variable(
  1253. $event['session_id'],
  1254. $this->extraFieldIdNameList['session_career']
  1255. );
  1256. $careerName = '';
  1257. if (!empty($values)) {
  1258. foreach ($values as $value) {
  1259. if (isset($value['value'])) {
  1260. $careerName = $value['value'];
  1261. }
  1262. }
  1263. }
  1264. $subject = sprintf(
  1265. get_lang('WelcomeToPortalXInCourseSessionX'),
  1266. api_get_setting('siteName'),
  1267. $courseInfo['title']
  1268. );
  1269. $tpl->assign('site_name', api_get_setting('siteName'));
  1270. $tpl->assign('course_title', $courseTitle);
  1271. $tpl->assign('career_name', $careerName);
  1272. $tpl->assign('first_lesson', $date);
  1273. $tpl->assign('location', $eventComment);
  1274. $tpl->assign('session_name', $sessionName);
  1275. if (empty($sessionId)) {
  1276. $teachersToString = CourseManager::getTeacherListFromCourseCodeToString($courseInfo['code'], ',');
  1277. } else {
  1278. $teachersToString = SessionManager::getCoachesByCourseSessionToString(
  1279. $sessionId,
  1280. $courseInfo['real_id'],
  1281. ','
  1282. );
  1283. }
  1284. $tpl->assign('teachers', $teachersToString);
  1285. $templateName = $tpl->get_template('mail/custom_calendar_welcome.tpl');
  1286. $emailBody = $tpl->fetch($templateName);
  1287. $coaches = SessionManager::getCoachesByCourseSession(
  1288. $event['session_id'],
  1289. $courseInfo['real_id']
  1290. );
  1291. // Search if an announcement exists:
  1292. $announcementsWithTitleList = AnnouncementManager::getAnnouncementsByTitle(
  1293. $subject,
  1294. $courseInfo['real_id'],
  1295. $event['session_id'],
  1296. 1
  1297. );
  1298. if (count($announcementsWithTitleList) === 0) {
  1299. $this->logger->addInfo(
  1300. 'Mail to be sent because start date: '.$event['start'].' and no announcement found.'
  1301. );
  1302. $senderId = $this->defaultAdminId;
  1303. if (!empty($coaches) && isset($coaches[0]) && !empty($coaches[0])) {
  1304. $senderId = $coaches[0];
  1305. }
  1306. $announcementId = AnnouncementManager::add_announcement(
  1307. $courseInfo,
  1308. $event['session_id'],
  1309. $subject,
  1310. $emailBody,
  1311. [
  1312. 'everyone',
  1313. 'users' => $coaches,
  1314. ],
  1315. [],
  1316. null,
  1317. null,
  1318. false,
  1319. $senderId
  1320. );
  1321. if ($announcementId) {
  1322. $this->logger->addInfo("Announcement added: $announcementId in $info");
  1323. $this->logger->addInfo("<<--SENDING MAIL Sender id: $senderId-->>");
  1324. $report['mail_sent']++;
  1325. AnnouncementManager::sendEmail(
  1326. $courseInfo,
  1327. $event['session_id'],
  1328. $announcementId,
  1329. false,
  1330. false,
  1331. $this->logger,
  1332. $senderId,
  1333. true
  1334. );
  1335. } else {
  1336. $this->logger->addError(
  1337. "Error when trying to add announcement with title $subject here: $info and SenderId = $senderId"
  1338. );
  1339. }
  1340. } else {
  1341. $report['mail_not_sent_announcement_exists']++;
  1342. $this->logger->addInfo(
  1343. "Mail NOT sent. An announcement seems to be already saved in '$info'"
  1344. );
  1345. }
  1346. } else {
  1347. $this->logger->addInfo(
  1348. "Send Mail: ".intval($sendMail).' - Already added: '.intval($alreadyAdded)
  1349. );
  1350. if ($sendMail == false) {
  1351. $report['mail_not_sent_because_date']++;
  1352. }
  1353. }
  1354. $content = '';
  1355. if ($update && isset($item['item_id'])) {
  1356. $eventInfo = $agenda->get_event($item['item_id']);
  1357. if (empty($eventInfo)) {
  1358. // Means that agenda external id exists but the event doesn't exist
  1359. $this->logger->addInfo("external event id exists: $externalEventId");
  1360. $this->logger->addInfo("but Chamilo event doesn't exists: ".$item['item_id']);
  1361. $eventId = $agenda->addEvent(
  1362. $event['start'],
  1363. $event['end'],
  1364. false,
  1365. $event['title'],
  1366. $content,
  1367. ['everyone'], // $usersToSend
  1368. false, //$addAsAnnouncement = false
  1369. null, // $parentEventId
  1370. [], //$attachmentArray = array(),
  1371. [], //$attachmentCommentList
  1372. $eventComment,
  1373. $color
  1374. );
  1375. if (!empty($eventId)) {
  1376. $this->logger->addInfo("Chamilo event created: ".$eventId);
  1377. $extraFieldValueItem = $extraFieldValue->get_values_by_handler_and_field_id(
  1378. $item['item_id'],
  1379. $extraFieldInfo['id']
  1380. );
  1381. if (!empty($extraFieldValueItem) && isset($extraFieldValueItem['id'])) {
  1382. $params = [
  1383. 'id' => $extraFieldValueItem['id'],
  1384. 'item_id' => $eventId,
  1385. ];
  1386. $extraFieldValue->update($params);
  1387. $this->logger->addInfo(
  1388. 'Updating calendar extra field #'.$extraFieldValueItem['id'].'
  1389. new item_id: '.$eventId.' old item_id: '.$item['item_id']
  1390. );
  1391. }
  1392. } else {
  1393. $this->logger->addInfo("Error while creating event external id: $externalEventId");
  1394. }
  1395. } else {
  1396. // The event already exists, just update
  1397. $eventResult = $agenda->editEvent(
  1398. $item['item_id'],
  1399. $event['start'],
  1400. $event['end'],
  1401. false,
  1402. $event['title'],
  1403. $content,
  1404. ['everyone'], // $usersToSend
  1405. [], //$attachmentArray = array(),
  1406. [], //$attachmentCommentList
  1407. $eventComment,
  1408. $color,
  1409. false,
  1410. false,
  1411. $this->defaultAdminId
  1412. );
  1413. if ($eventResult !== false) {
  1414. $this->logger->addInfo(
  1415. "Event updated #".$item['item_id']." External cal Id: (".$externalEventId.") $info"
  1416. );
  1417. } else {
  1418. $this->logger->addInfo(
  1419. "Error while updating event with external id: $externalEventId"
  1420. );
  1421. }
  1422. }
  1423. } else {
  1424. // New event. Create it.
  1425. $eventId = $agenda->addEvent(
  1426. $event['start'],
  1427. $event['end'],
  1428. false,
  1429. $event['title'],
  1430. $content,
  1431. ['everyone'], // $usersToSend
  1432. false, //$addAsAnnouncement = false
  1433. null, // $parentEventId
  1434. [], //$attachmentArray = array(),
  1435. [], //$attachmentCommentList
  1436. $eventComment,
  1437. $color
  1438. );
  1439. if (!empty($eventId)) {
  1440. $extraFieldValue->save(
  1441. [
  1442. 'value' => $externalEventId,
  1443. 'field_id' => $extraFieldInfo['id'],
  1444. 'item_id' => $eventId,
  1445. ]
  1446. );
  1447. $this->logger->addInfo(
  1448. "Event added: #$eventId External cal id: (".$externalEventId.") $info"
  1449. );
  1450. } else {
  1451. $this->logger->addInfo(
  1452. "Error while creating event external id: $externalEventId"
  1453. );
  1454. }
  1455. }
  1456. if (($counter % $batchSize) === 0) {
  1457. $em->flush();
  1458. $em->clear(); // Detaches all objects from Doctrine!
  1459. }
  1460. $counter++;
  1461. }
  1462. $em->clear(); // Detaches all objects from Doctrine!
  1463. $this->logger->addInfo('------Summary------');
  1464. foreach ($report as $title => $count) {
  1465. $this->logger->addInfo("$title: $count");
  1466. }
  1467. $this->logger->addInfo('------End Summary------');
  1468. }
  1469. if ($moveFile) {
  1470. $this->moveFile($file);
  1471. }
  1472. }
  1473. /**
  1474. * @param string $file
  1475. * @param bool $moveFile
  1476. * @param array $teacherBackup
  1477. * @param array $groupBackup
  1478. */
  1479. private function importCourses(
  1480. $file,
  1481. $moveFile = true,
  1482. &$teacherBackup = [],
  1483. &$groupBackup = []
  1484. ) {
  1485. $this->fixCSVFile($file);
  1486. $data = Import::csvToArray($file);
  1487. if (!empty($data)) {
  1488. $this->logger->addInfo(count($data)." records found.");
  1489. foreach ($data as $row) {
  1490. $row = $this->cleanCourseRow($row);
  1491. $courseId = CourseManager::get_course_id_from_original_id(
  1492. $row['extra_'.$this->extraFieldIdNameList['course']],
  1493. $this->extraFieldIdNameList['course']
  1494. );
  1495. $courseInfo = api_get_course_info_by_id($courseId);
  1496. if (empty($courseInfo)) {
  1497. // Create
  1498. $params = [];
  1499. $params['title'] = $row['title'];
  1500. $params['exemplary_content'] = false;
  1501. $params['wanted_code'] = $row['course_code'];
  1502. $params['course_category'] = $row['course_category'];
  1503. $params['course_language'] = $row['language'];
  1504. $params['teachers'] = $row['teachers'];
  1505. $params['visibility'] = $row['visibility'];
  1506. $courseInfo = CourseManager::create_course(
  1507. $params,
  1508. $this->defaultAdminId
  1509. );
  1510. if (!empty($courseInfo)) {
  1511. CourseManager::update_course_extra_field_value(
  1512. $courseInfo['code'],
  1513. 'external_course_id',
  1514. $row['extra_'.$this->extraFieldIdNameList['course']]
  1515. );
  1516. $this->logger->addInfo("Courses - Course created ".$courseInfo['code']);
  1517. } else {
  1518. $this->logger->addError("Courses - Can't create course:".$row['title']);
  1519. }
  1520. } else {
  1521. // Update
  1522. $params = [
  1523. 'title' => $row['title'],
  1524. 'category_code' => $row['course_category'],
  1525. 'visibility' => $row['visibility'],
  1526. ];
  1527. $result = CourseManager::update_attributes(
  1528. $courseInfo['real_id'],
  1529. $params
  1530. );
  1531. $addTeacherToSession = isset($courseInfo['add_teachers_to_sessions_courses']) && !empty($courseInfo['add_teachers_to_sessions_courses']) ? true : false;
  1532. $teachers = $row['teachers'];
  1533. if (!is_array($teachers)) {
  1534. $teachers = [$teachers];
  1535. }
  1536. if ($addTeacherToSession) {
  1537. $this->logger->addInfo("Add teacher to all course sessions");
  1538. CourseManager::updateTeachers(
  1539. $courseInfo,
  1540. $row['teachers'],
  1541. false,
  1542. true,
  1543. false,
  1544. $teacherBackup,
  1545. $this->logger
  1546. );
  1547. } else {
  1548. CourseManager::updateTeachers(
  1549. $courseInfo,
  1550. $row['teachers'],
  1551. true,
  1552. false,
  1553. false,
  1554. $teacherBackup,
  1555. $this->logger
  1556. );
  1557. }
  1558. foreach ($teachers as $teacherId) {
  1559. if (isset($groupBackup['tutor'][$teacherId]) &&
  1560. isset($groupBackup['tutor'][$teacherId][$courseInfo['code']])
  1561. ) {
  1562. foreach ($groupBackup['tutor'][$teacherId][$courseInfo['code']] as $data) {
  1563. $groupInfo = GroupManager::get_group_properties($data['group_id']);
  1564. GroupManager::subscribe_tutors(
  1565. [$teacherId],
  1566. $groupInfo,
  1567. $data['c_id']
  1568. );
  1569. }
  1570. }
  1571. if (isset($groupBackup['user'][$teacherId]) &&
  1572. isset($groupBackup['user'][$teacherId][$courseInfo['code']]) &&
  1573. !empty($groupBackup['user'][$teacherId][$courseInfo['code']])
  1574. ) {
  1575. foreach ($groupBackup['user'][$teacherId][$courseInfo['code']] as $data) {
  1576. $groupInfo = GroupManager::get_group_properties($data['group_id']);
  1577. GroupManager::subscribe_users(
  1578. [$teacherId],
  1579. $groupInfo,
  1580. $data['c_id']
  1581. );
  1582. }
  1583. }
  1584. }
  1585. if ($result) {
  1586. $this->logger->addInfo("Courses - Course updated ".$courseInfo['code']);
  1587. } else {
  1588. $this->logger->addError("Courses - Course NOT updated ".$courseInfo['code']);
  1589. }
  1590. }
  1591. }
  1592. }
  1593. if ($moveFile) {
  1594. $this->moveFile($file);
  1595. }
  1596. }
  1597. /**
  1598. * Parse filename: encora_subsessionsextid-static_31082016.csv.
  1599. *
  1600. * @param string $file
  1601. */
  1602. private function importSubscribeUserToCourseSessionExtStatic($file, $moveFile = true)
  1603. {
  1604. $data = Import::csv_reader($file);
  1605. if (!empty($data)) {
  1606. $this->logger->addInfo(count($data)." records found.");
  1607. $userIdList = [];
  1608. foreach ($data as $row) {
  1609. $chamiloUserName = $row['UserName'];
  1610. $chamiloCourseCode = $row['CourseCode'];
  1611. $externalSessionId = $row['ExtSessionID'];
  1612. $status = $row['Status'];
  1613. $chamiloSessionId = null;
  1614. if (!empty($externalSessionId)) {
  1615. $chamiloSessionId = SessionManager::getSessionIdFromOriginalId(
  1616. $externalSessionId,
  1617. $this->extraFieldIdNameList['session']
  1618. );
  1619. }
  1620. $sessionInfo = api_get_session_info($chamiloSessionId);
  1621. if (empty($sessionInfo)) {
  1622. $this->logger->addError('Session does not exists: '.$chamiloSessionId);
  1623. continue;
  1624. }
  1625. $courseInfo = api_get_course_info($chamiloCourseCode);
  1626. if (empty($courseInfo)) {
  1627. $this->logger->addError('Course does not exists: '.$courseInfo);
  1628. continue;
  1629. }
  1630. $userId = UserManager::get_user_id_from_username($chamiloUserName);
  1631. if (empty($userId)) {
  1632. $this->logger->addError('User does not exists: '.$chamiloUserName);
  1633. continue;
  1634. }
  1635. switch ($status) {
  1636. case 'student':
  1637. SessionManager::subscribe_users_to_session_course(
  1638. [$userId],
  1639. $chamiloSessionId,
  1640. $courseInfo['code']
  1641. );
  1642. break;
  1643. case 'teacher':
  1644. SessionManager::set_coach_to_course_session(
  1645. $userId,
  1646. $chamiloSessionId,
  1647. $courseInfo['code']
  1648. );
  1649. break;
  1650. case 'drh':
  1651. $removeAllSessionsFromUser = true;
  1652. if (in_array($userId, $userIdList)) {
  1653. $removeAllSessionsFromUser = false;
  1654. } else {
  1655. $userIdList[] = $userId;
  1656. }
  1657. $userInfo = api_get_user_info($userId);
  1658. SessionManager::subscribeSessionsToDrh(
  1659. $userInfo,
  1660. [$chamiloSessionId],
  1661. false,
  1662. $removeAllSessionsFromUser
  1663. );
  1664. break;
  1665. }
  1666. $this->logger->addError(
  1667. "User '$chamiloUserName' was added as '$status' to Session: #$chamiloSessionId - Course: ".$courseInfo['code']
  1668. );
  1669. }
  1670. }
  1671. if ($moveFile) {
  1672. $this->moveFile($file);
  1673. }
  1674. }
  1675. /**
  1676. * @param $file
  1677. * @param bool $moveFile
  1678. */
  1679. private function importUnsubSessionsExtIdStatic($file, $moveFile = true)
  1680. {
  1681. $data = Import::csv_reader($file);
  1682. if (!empty($data)) {
  1683. $this->logger->addInfo(count($data)." records found.");
  1684. foreach ($data as $row) {
  1685. $chamiloUserName = $row['UserName'];
  1686. $chamiloCourseCode = $row['CourseCode'];
  1687. $externalSessionId = $row['ExtSessionID'];
  1688. $dateStop = $row['DateStop'];
  1689. $chamiloSessionId = null;
  1690. if (!empty($externalSessionId)) {
  1691. $chamiloSessionId = SessionManager::getSessionIdFromOriginalId(
  1692. $externalSessionId,
  1693. $this->extraFieldIdNameList['session']
  1694. );
  1695. }
  1696. $sessionInfo = api_get_session_info($chamiloSessionId);
  1697. if (empty($sessionInfo)) {
  1698. $this->logger->addError('Session does not exists: '.$chamiloSessionId);
  1699. continue;
  1700. }
  1701. $courseInfo = api_get_course_info($chamiloCourseCode);
  1702. if (empty($courseInfo)) {
  1703. $this->logger->addError('Course does not exists: '.$courseInfo);
  1704. continue;
  1705. }
  1706. $userId = UserManager::get_user_id_from_username($chamiloUserName);
  1707. if (empty($userId)) {
  1708. $this->logger->addError('User does not exists: '.$chamiloUserName);
  1709. continue;
  1710. }
  1711. SessionManager::removeUsersFromCourseSession(
  1712. [$userId],
  1713. $chamiloSessionId,
  1714. $courseInfo
  1715. );
  1716. $this->logger->addError(
  1717. "User '$chamiloUserName' was remove from Session: #$chamiloSessionId - Course: ".$courseInfo['code']
  1718. );
  1719. }
  1720. }
  1721. if ($moveFile) {
  1722. $this->moveFile($file);
  1723. }
  1724. }
  1725. /**
  1726. * @param string $file
  1727. */
  1728. private function importSessionsExtIdStatic($file, $moveFile = true)
  1729. {
  1730. $data = Import::csv_reader($file);
  1731. if (!empty($data)) {
  1732. $this->logger->addInfo(count($data)." records found.");
  1733. foreach ($data as $row) {
  1734. $chamiloUserName = $row['UserName'];
  1735. $chamiloCourseCode = $row['CourseCode'];
  1736. $externalSessionId = $row['ExtSessionID'];
  1737. $type = $row['Type'];
  1738. $chamiloSessionId = null;
  1739. if (!empty($externalSessionId)) {
  1740. $chamiloSessionId = SessionManager::getSessionIdFromOriginalId(
  1741. $externalSessionId,
  1742. $this->extraFieldIdNameList['session']
  1743. );
  1744. }
  1745. $sessionInfo = api_get_session_info($chamiloSessionId);
  1746. if (empty($sessionInfo)) {
  1747. $this->logger->addError('Session does not exists: '.$chamiloSessionId);
  1748. continue;
  1749. }
  1750. $courseInfo = api_get_course_info($chamiloCourseCode);
  1751. if (empty($courseInfo)) {
  1752. $this->logger->addError('Course does not exists: '.$courseInfo);
  1753. continue;
  1754. }
  1755. $userId = UserManager::get_user_id_from_username($chamiloUserName);
  1756. if (empty($userId)) {
  1757. $this->logger->addError('User does not exists: '.$chamiloUserName);
  1758. continue;
  1759. }
  1760. switch ($type) {
  1761. case 'student':
  1762. SessionManager::subscribe_users_to_session_course(
  1763. [$userId],
  1764. $chamiloSessionId,
  1765. $courseInfo['code'],
  1766. null,
  1767. false
  1768. );
  1769. break;
  1770. case 'teacher':
  1771. SessionManager::set_coach_to_course_session(
  1772. $userId,
  1773. $chamiloSessionId,
  1774. $courseInfo['code']
  1775. );
  1776. break;
  1777. }
  1778. $this->logger->addError(
  1779. "User '$chamiloUserName' with status $type was added to session: #$chamiloSessionId - Course: ".$courseInfo['code']
  1780. );
  1781. }
  1782. }
  1783. if ($moveFile) {
  1784. $this->moveFile($file);
  1785. }
  1786. }
  1787. /**
  1788. * Updates the session synchronize with the csv file.
  1789. *
  1790. * @param bool $moveFile
  1791. * @param string $file
  1792. */
  1793. private function importSessionsStatic($file, $moveFile = true)
  1794. {
  1795. $content = file($file);
  1796. $sessions = [];
  1797. $tag_names = [];
  1798. foreach ($content as $key => $enreg) {
  1799. $enreg = explode(';', trim($enreg));
  1800. if ($key) {
  1801. foreach ($tag_names as $tag_key => $tag_name) {
  1802. if (isset($enreg[$tag_key])) {
  1803. $sessions[$key - 1][$tag_name] = $enreg[$tag_key];
  1804. }
  1805. }
  1806. } else {
  1807. foreach ($enreg as $tag_name) {
  1808. $tag_names[] = api_preg_replace(
  1809. '/[^a-zA-Z0-9_\-]/',
  1810. '',
  1811. $tag_name
  1812. );
  1813. }
  1814. if (!in_array('SessionName', $tag_names) ||
  1815. !in_array('DateStart', $tag_names) || !in_array('DateEnd', $tag_names)
  1816. ) {
  1817. $error_message = get_lang('NoNeededData');
  1818. break;
  1819. }
  1820. }
  1821. }
  1822. if (!empty($sessions)) {
  1823. // Looping the sessions.
  1824. foreach ($sessions as $session) {
  1825. if (!empty($session['SessionID'])) {
  1826. $sessionId = SessionManager::getSessionIdFromOriginalId(
  1827. $session['SessionID'],
  1828. $this->extraFieldIdNameList['session']
  1829. );
  1830. $coachUserName = isset($session['Coach']) ? $session['Coach'] : null;
  1831. $categoryId = isset($session['category_id']) ? $session['category_id'] : null;
  1832. // 2014-06-30
  1833. $dateStart = explode('/', $session['DateStart']);
  1834. $dateEnd = explode('/', $session['DateEnd']);
  1835. $visibility = $this->defaultSessionVisibility;
  1836. $coachId = null;
  1837. if (!empty($coachUserName)) {
  1838. $coachInfo = api_get_user_info_from_username($coachUserName);
  1839. $coachId = $coachInfo['user_id'];
  1840. }
  1841. $dateStart = $dateStart[0].'-'.$dateStart[1].'-'.$dateStart[2].' 00:00:00';
  1842. $dateEnd = $dateEnd[0].'-'.$dateEnd[1].'-'.$dateEnd[2].' 23:59:59';
  1843. $date = new \DateTime($dateStart);
  1844. $interval = new DateInterval('P'.$this->daysCoachAccessBeforeBeginning.'D');
  1845. $date->sub($interval);
  1846. $coachBefore = $date->format('Y-m-d h:i');
  1847. $date = new \DateTime($dateEnd);
  1848. $interval = new DateInterval('P'.$this->daysCoachAccessAfterBeginning.'D');
  1849. $date->add($interval);
  1850. $coachAfter = $date->format('Y-m-d h:i');
  1851. /*$dateStart = api_get_utc_datetime($dateStart);
  1852. $dateEnd = api_get_utc_datetime($dateEnd);
  1853. $coachBefore = api_get_utc_datetime($coachBefore);
  1854. $coachAfter = api_get_utc_datetime($coachAfter);*/
  1855. if (empty($sessionId)) {
  1856. $result = SessionManager::create_session(
  1857. $session['SessionName'],
  1858. $dateStart,
  1859. $dateEnd,
  1860. $dateStart,
  1861. $dateEnd,
  1862. $coachBefore,
  1863. $coachAfter,
  1864. $coachId,
  1865. $categoryId,
  1866. $visibility
  1867. );
  1868. if (is_numeric($result)) {
  1869. $sessionId = $result;
  1870. $this->logger->addInfo("Session #$sessionId created: ".$session['SessionName']);
  1871. SessionManager::update_session_extra_field_value(
  1872. $sessionId,
  1873. $this->extraFieldIdNameList['session'],
  1874. $session['SessionID']
  1875. );
  1876. } else {
  1877. $this->logger->addInfo("Failed creating session: ".$session['SessionName']);
  1878. }
  1879. } else {
  1880. $sessionInfo = api_get_session_info($sessionId);
  1881. $accessBefore = null;
  1882. $accessAfter = null;
  1883. if (empty($sessionInfo['nb_days_access_before_beginning']) ||
  1884. (!empty($sessionInfo['nb_days_access_before_beginning']) &&
  1885. $sessionInfo['nb_days_access_before_beginning'] < $this->daysCoachAccessBeforeBeginning)
  1886. ) {
  1887. $accessBefore = $coachBefore;
  1888. }
  1889. $accessAfter = null;
  1890. if (empty($sessionInfo['nb_days_access_after_end']) ||
  1891. (!empty($sessionInfo['nb_days_access_after_end']) &&
  1892. $sessionInfo['nb_days_access_after_end'] < $this->daysCoachAccessAfterBeginning)
  1893. ) {
  1894. $accessAfter = $coachAfter;
  1895. }
  1896. $showDescription = isset($sessionInfo['show_description']) ? $sessionInfo['show_description'] : 1;
  1897. $result = SessionManager::edit_session(
  1898. $sessionId,
  1899. $session['SessionName'],
  1900. $dateStart,
  1901. $dateEnd,
  1902. $dateStart,
  1903. $dateEnd,
  1904. $accessBefore,
  1905. $accessAfter,
  1906. $coachId,
  1907. $categoryId,
  1908. $visibility,
  1909. null, //$description = null,
  1910. $showDescription
  1911. );
  1912. if (is_numeric($result)) {
  1913. $this->logger->addInfo("Session #$sessionId updated: ".$session['SessionName']);
  1914. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  1915. $params = [
  1916. 'description' => $session['SessionDescription'],
  1917. ];
  1918. Database::update(
  1919. $tbl_session,
  1920. $params,
  1921. ['id = ?' => $sessionId]
  1922. );
  1923. }
  1924. }
  1925. if (!empty($sessionId)) {
  1926. // Courses
  1927. $courses = explode('|', $session['Courses']);
  1928. $courseList = [];
  1929. $courseListWithCoach = [];
  1930. foreach ($courses as $course) {
  1931. $courseArray = bracketsToArray($course);
  1932. $courseCode = $courseArray[0];
  1933. if (CourseManager::course_exists($courseCode)) {
  1934. $courseInfo = api_get_course_info($courseCode);
  1935. $courseList[] = $courseInfo['real_id'];
  1936. // Extracting course coaches
  1937. $courseCoaches = isset($courseArray[1]) ? $courseArray[1] : null;
  1938. $courseCoaches = explode(',', $courseCoaches);
  1939. // Extracting students
  1940. $courseUsers = isset($courseArray[2]) ? $courseArray[2] : null;
  1941. $courseUsers = explode(',', $courseUsers);
  1942. $courseListWithCoach[] = [
  1943. 'course_info' => $courseInfo,
  1944. 'coaches' => $courseCoaches,
  1945. 'course_users' => $courseUsers,
  1946. ];
  1947. }
  1948. }
  1949. SessionManager::add_courses_to_session(
  1950. $sessionId,
  1951. $courseList,
  1952. true
  1953. );
  1954. $this->logger->addInfo("Session #$sessionId: Courses added: '".implode(', ', $courseList)."'");
  1955. if (empty($courseListWithCoach)) {
  1956. $this->logger->addInfo("No users/coaches to update");
  1957. continue;
  1958. }
  1959. foreach ($courseListWithCoach as $courseData) {
  1960. $courseInfo = $courseData['course_info'];
  1961. $courseCode = $courseInfo['code'];
  1962. $courseId = $courseInfo['real_id'];
  1963. $courseCoaches = $courseData['coaches'];
  1964. $courseUsers = $courseData['course_users'];
  1965. // Coaches
  1966. if (!empty($courseCoaches)) {
  1967. $coachList = [];
  1968. foreach ($courseCoaches as $courseCoach) {
  1969. $courseCoachId = UserManager::get_user_id_from_username(
  1970. $courseCoach
  1971. );
  1972. if ($courseCoachId !== false) {
  1973. // Just insert new coaches
  1974. $coachList[] = $courseCoachId;
  1975. }
  1976. }
  1977. $this->logger->addInfo("Session #$sessionId: course '$courseCode' coaches added: '".implode(', ', $coachList)."'");
  1978. SessionManager::updateCoaches(
  1979. $sessionId,
  1980. $courseId,
  1981. $coachList,
  1982. true
  1983. );
  1984. } else {
  1985. $this->logger->addInfo("No coaches added");
  1986. }
  1987. // Students
  1988. if (!empty($courseUsers)) {
  1989. $userList = [];
  1990. foreach ($courseUsers as $username) {
  1991. $userInfo = api_get_user_info_from_username(trim($username));
  1992. if (!empty($userInfo)) {
  1993. $userList[] = $userInfo['user_id'];
  1994. }
  1995. }
  1996. $this->logger->addInfo("Session #$sessionId: course '$courseCode': Students added '".implode(', ', $userList)."'");
  1997. SessionManager::subscribe_users_to_session_course(
  1998. $userList,
  1999. $sessionId,
  2000. $courseCode,
  2001. SESSION_VISIBLE_READ_ONLY,
  2002. true
  2003. );
  2004. } else {
  2005. $this->logger->addInfo("No users to register.");
  2006. }
  2007. }
  2008. } else {
  2009. $this->logger->addInfo(
  2010. 'SessionID not found in system.'
  2011. );
  2012. }
  2013. } else {
  2014. $this->logger->addInfo('SessionID does not exists');
  2015. }
  2016. }
  2017. } else {
  2018. $this->logger->addInfo($error_message);
  2019. }
  2020. if ($moveFile) {
  2021. $this->moveFile($file);
  2022. }
  2023. }
  2024. /**
  2025. * @param $file
  2026. * @param bool $moveFile
  2027. * @param array $teacherBackup
  2028. * @param array $groupBackup
  2029. */
  2030. private function importOpenSessions(
  2031. $file,
  2032. $moveFile = true,
  2033. &$teacherBackup = [],
  2034. &$groupBackup = []
  2035. ) {
  2036. $this->importSessions($file, $moveFile, $teacherBackup, $groupBackup);
  2037. }
  2038. /**
  2039. * @param string $file
  2040. * @param bool $moveFile
  2041. * @param array $teacherBackup
  2042. * @param array $groupBackup
  2043. */
  2044. private function importSessions(
  2045. $file,
  2046. $moveFile = true,
  2047. &$teacherBackup = [],
  2048. &$groupBackup = []
  2049. ) {
  2050. $avoid = null;
  2051. if (isset($this->conditions['importSessions']) &&
  2052. isset($this->conditions['importSessions']['update'])
  2053. ) {
  2054. $avoid = $this->conditions['importSessions']['update'];
  2055. }
  2056. $result = SessionManager::importCSV(
  2057. $file,
  2058. true,
  2059. $this->defaultAdminId,
  2060. $this->logger,
  2061. [
  2062. 'SessionID' => 'extra_'.$this->extraFieldIdNameList['session'],
  2063. 'CareerId' => 'extra_'.$this->extraFieldIdNameList['session_career'],
  2064. ],
  2065. $this->extraFieldIdNameList['session'],
  2066. $this->daysCoachAccessBeforeBeginning,
  2067. $this->daysCoachAccessAfterBeginning,
  2068. $this->defaultSessionVisibility,
  2069. $avoid,
  2070. false, // deleteUsersNotInList
  2071. false, // updateCourseCoaches
  2072. true, // sessionWithCoursesModifier
  2073. true, //$addOriginalCourseTeachersAsCourseSessionCoaches
  2074. false, //$removeAllTeachersFromCourse
  2075. 1, // $showDescription,
  2076. $teacherBackup,
  2077. $groupBackup
  2078. );
  2079. if (!empty($result['error_message'])) {
  2080. $this->logger->addError($result['error_message']);
  2081. }
  2082. $this->logger->addInfo("Sessions - Sessions parsed: ".$result['session_counter']);
  2083. if ($moveFile) {
  2084. $this->moveFile($file);
  2085. }
  2086. }
  2087. /**
  2088. * @param string $file
  2089. * @param bool $moveFile
  2090. */
  2091. private function importSubscribeStatic($file, $moveFile = true)
  2092. {
  2093. $data = Import::csv_reader($file);
  2094. if (!empty($data)) {
  2095. $this->logger->addInfo(count($data)." records found.");
  2096. foreach ($data as $row) {
  2097. $chamiloUserName = $row['UserName'];
  2098. $chamiloCourseCode = $row['CourseCode'];
  2099. $chamiloSessionId = $row['SessionID'];
  2100. $type = $row['Type'];
  2101. $sessionInfo = api_get_session_info($chamiloSessionId);
  2102. if (empty($sessionInfo)) {
  2103. $this->logger->addError('Session does not exists: '.$chamiloSessionId);
  2104. continue;
  2105. }
  2106. $courseInfo = api_get_course_info($chamiloCourseCode);
  2107. if (empty($courseInfo)) {
  2108. $this->logger->addError('Course does not exists: '.$courseInfo);
  2109. continue;
  2110. }
  2111. $userId = UserManager::get_user_id_from_username($chamiloUserName);
  2112. if (empty($userId)) {
  2113. $this->logger->addError('User does not exists: '.$chamiloUserName);
  2114. continue;
  2115. }
  2116. switch ($type) {
  2117. case 'student':
  2118. SessionManager::subscribe_users_to_session_course(
  2119. [$userId],
  2120. $chamiloSessionId,
  2121. $courseInfo['code'],
  2122. null,
  2123. false
  2124. );
  2125. break;
  2126. case 'teacher':
  2127. SessionManager::set_coach_to_course_session(
  2128. $userId,
  2129. $chamiloSessionId,
  2130. $courseInfo['real_id']
  2131. );
  2132. break;
  2133. }
  2134. $this->logger->addError(
  2135. "User '$chamiloUserName' with status $type was added to session: #$chamiloSessionId - Course: ".$courseInfo['code']
  2136. );
  2137. }
  2138. }
  2139. if ($moveFile) {
  2140. $this->moveFile($file);
  2141. }
  2142. }
  2143. /**
  2144. * @param $file
  2145. * @param bool $moveFile
  2146. */
  2147. private function importSubscribeUserToCourse($file, $moveFile = false, &$teacherBackup = [])
  2148. {
  2149. $data = Import::csv_reader($file);
  2150. if (!empty($data)) {
  2151. $this->logger->addInfo(count($data)." records found.");
  2152. foreach ($data as $row) {
  2153. $chamiloUserName = $row['UserName'];
  2154. $chamiloCourseCode = $row['CourseCode'];
  2155. $status = $row['Status'];
  2156. $courseInfo = api_get_course_info($chamiloCourseCode);
  2157. if (empty($courseInfo)) {
  2158. $this->logger->addError(
  2159. 'Course does not exists: '.$chamiloCourseCode
  2160. );
  2161. continue;
  2162. }
  2163. $userId = UserManager::get_user_id_from_username(
  2164. $chamiloUserName
  2165. );
  2166. if (empty($userId)) {
  2167. $this->logger->addError(
  2168. 'User does not exists: '.$chamiloUserName
  2169. );
  2170. continue;
  2171. }
  2172. $userCourseCategory = '';
  2173. if (isset($teacherBackup[$userId]) &&
  2174. isset($teacherBackup[$userId][$courseInfo['code']])
  2175. ) {
  2176. $courseUserData = $teacherBackup[$userId][$courseInfo['code']];
  2177. $userCourseCategory = $courseUserData['user_course_cat'];
  2178. }
  2179. $result = CourseManager::subscribeUser(
  2180. $userId,
  2181. $courseInfo['code'],
  2182. $status,
  2183. 0,
  2184. $userCourseCategory
  2185. );
  2186. if ($result) {
  2187. $this->logger->addInfo(
  2188. "User $userId added to course ".$courseInfo['code']." with status '$status' with course category: '$userCourseCategory'"
  2189. );
  2190. } else {
  2191. $this->logger->addInfo(
  2192. "User $userId was NOT ADDED to course ".$courseInfo['code']." with status '$status' with course category: '$userCourseCategory'"
  2193. );
  2194. }
  2195. }
  2196. }
  2197. if ($moveFile) {
  2198. $this->moveFile($file);
  2199. }
  2200. }
  2201. /**
  2202. * 23/4/2017 to datetime.
  2203. *
  2204. * @param $string
  2205. *
  2206. * @return mixed
  2207. */
  2208. private function createDateTime($string)
  2209. {
  2210. if (empty($string)) {
  2211. return null;
  2212. }
  2213. $date = DateTime::createFromFormat('j/m/Y', $string);
  2214. if ($date) {
  2215. return $date;
  2216. }
  2217. return null;
  2218. }
  2219. /**
  2220. * @param $file
  2221. * @param bool $moveFile
  2222. * @param array $teacherBackup
  2223. * @param array $groupBackup
  2224. *
  2225. * @return bool
  2226. */
  2227. private function importCareers(
  2228. $file,
  2229. $moveFile = false,
  2230. &$teacherBackup = [],
  2231. &$groupBackup = []
  2232. ) {
  2233. $data = Import::csv_reader($file);
  2234. if (!empty($data)) {
  2235. $this->logger->addInfo(count($data)." records found.");
  2236. $extraFieldValue = new ExtraFieldValue('career');
  2237. $extraFieldName = $this->extraFieldIdNameList['career'];
  2238. $externalEventId = null;
  2239. $extraField = new ExtraField('career');
  2240. $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
  2241. $extraFieldName
  2242. );
  2243. if (empty($extraFieldInfo)) {
  2244. return false;
  2245. }
  2246. foreach ($data as $row) {
  2247. foreach ($row as $key => $value) {
  2248. $key = (string) trim($key);
  2249. // Remove utf8 bom
  2250. $key = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $key);
  2251. $row[$key] = $value;
  2252. }
  2253. $itemId = $row['CareerId'];
  2254. $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
  2255. $extraFieldName,
  2256. $itemId,
  2257. false,
  2258. false,
  2259. false
  2260. );
  2261. $career = new Career();
  2262. if (empty($item)) {
  2263. $params = [
  2264. 'status' => 1,
  2265. 'name' => $row['CareerName'],
  2266. ];
  2267. $careerId = $career->save($params);
  2268. if ($careerId) {
  2269. $params = [
  2270. 'item_id' => $careerId,
  2271. 'extra_'.$extraFieldName => $itemId,
  2272. ];
  2273. $links = isset($row['HLinks']) ? $row['HLinks'] : [];
  2274. if (!empty($links)) {
  2275. $extraFieldUrlName = $this->extraFieldIdNameList['career_urls'];
  2276. $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
  2277. $extraFieldUrlName
  2278. );
  2279. if (!empty($extraFieldInfo)) {
  2280. $params['extra_'.$extraFieldUrlName] = $links;
  2281. }
  2282. }
  2283. $extraFieldValue->saveFieldValues($params);
  2284. }
  2285. } else {
  2286. if (isset($item['item_id'])) {
  2287. $params = [
  2288. 'id' => $item['item_id'],
  2289. 'name' => $row['CareerName'],
  2290. ];
  2291. $career->update($params);
  2292. $links = isset($row['HLinks']) ? $row['HLinks'] : [];
  2293. if (!empty($links)) {
  2294. $extraFieldUrlName = $this->extraFieldIdNameList['career_urls'];
  2295. $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
  2296. $extraFieldUrlName
  2297. );
  2298. if (!empty($extraFieldInfo)) {
  2299. $params = [
  2300. 'item_id' => $item['item_id'],
  2301. 'extra_'.$extraFieldName => $itemId,
  2302. 'extra_'.$extraFieldUrlName => $links,
  2303. ];
  2304. $extraFieldValue->saveFieldValues($params);
  2305. }
  2306. }
  2307. }
  2308. }
  2309. }
  2310. }
  2311. }
  2312. /**
  2313. * @param $file
  2314. * @param bool $moveFile
  2315. * @param array $teacherBackup
  2316. * @param array $groupBackup
  2317. */
  2318. private function importCareersDiagram(
  2319. $file,
  2320. $moveFile = false,
  2321. &$teacherBackup = [],
  2322. &$groupBackup = []
  2323. ) {
  2324. $data = Import::csv_reader($file);
  2325. $extraFieldValue = new ExtraFieldValue('career');
  2326. $extraFieldName = $this->extraFieldIdNameList['career'];
  2327. $externalEventId = null;
  2328. $extraField = new ExtraField('career');
  2329. $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
  2330. $extraFieldName
  2331. );
  2332. $careerDiagramExtraFieldName = $this->extraFieldIdNameList['career_diagram'];
  2333. $extraFieldDiagramInfo = $extraField->get_handler_field_info_by_field_variable(
  2334. $careerDiagramExtraFieldName
  2335. );
  2336. if (empty($extraFieldInfo) || empty($extraFieldDiagramInfo)) {
  2337. return false;
  2338. }
  2339. if (!empty($data)) {
  2340. $this->logger->addInfo(count($data)." records found.");
  2341. $values = [];
  2342. foreach ($data as $row) {
  2343. if (empty($row)) {
  2344. continue;
  2345. }
  2346. foreach ($row as $key => $value) {
  2347. $key = (string) trim($key);
  2348. // Remove utf8 bom
  2349. $key = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $key);
  2350. $row[$key] = $value;
  2351. }
  2352. $values[$row['Column']][] = $row;
  2353. }
  2354. $careerList = [];
  2355. $careerNameList = [];
  2356. ksort($values);
  2357. $careerChamiloIdList = [];
  2358. // 1. First create all items
  2359. foreach ($values as $column => $rowList) {
  2360. foreach ($rowList as $row) {
  2361. $careerId = $row['CareerId'];
  2362. $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
  2363. $extraFieldName,
  2364. $careerId,
  2365. false,
  2366. false,
  2367. false
  2368. );
  2369. $chamiloCareerName = '';
  2370. if (empty($item)) {
  2371. $this->logger->addInfo("Career not found: $careerId");
  2372. continue;
  2373. } else {
  2374. if (isset($item['item_id'])) {
  2375. $careerChamiloId = $item['item_id'];
  2376. $career = new Career();
  2377. $career = $career->find($careerChamiloId);
  2378. $chamiloCareerName = $career['name'];
  2379. $careerNameList[$careerId] = $chamiloCareerName;
  2380. $careerChamiloIdList[$careerId] = $careerChamiloId;
  2381. } else {
  2382. continue;
  2383. }
  2384. }
  2385. if (empty($chamiloCareerName)) {
  2386. $this->logger->addInfo("Career not found: $careerId");
  2387. continue;
  2388. }
  2389. if (isset($careerList[$careerId])) {
  2390. $graph = $careerList[$careerId];
  2391. } else {
  2392. $graph = new Graph($careerId);
  2393. $graph->setAttribute('graphviz.graph.rankdir', 'LR');
  2394. $careerList[$careerId] = $graph;
  2395. }
  2396. $currentCourseId = (int) $row['CourseId'];
  2397. $name = $row['CourseName'];
  2398. $notes = $row['Notes'];
  2399. $groupValue = $row['Group'];
  2400. $boxColumn = $row['Column'];
  2401. $rowValue = $row['Row'];
  2402. $color = isset($row['DefinedColor']) ? $row['DefinedColor'] : '';
  2403. $arrow = isset($row['DrawArrowFrom']) ? $row['DrawArrowFrom'] : '';
  2404. $subGroup = isset($row['SubGroup']) ? $row['SubGroup'] : '';
  2405. $connections = isset($row['Connections']) ? $row['Connections'] : '';
  2406. $linkedElement = isset($row['LinkedElement']) ? $row['LinkedElement'] : '';
  2407. if ($graph->hasVertex($currentCourseId)) {
  2408. // Avoid double insertion
  2409. continue;
  2410. } else {
  2411. $current = $graph->createVertex($currentCourseId);
  2412. $current->setAttribute('graphviz.label', $name);
  2413. $current->setAttribute('DefinedColor', $color);
  2414. $current->setAttribute('Notes', $notes);
  2415. $current->setAttribute('Row', $rowValue);
  2416. $current->setAttribute('Group', $groupValue);
  2417. $current->setAttribute('Column', $boxColumn);
  2418. $current->setAttribute('DrawArrowFrom', $arrow);
  2419. $current->setAttribute('SubGroup', $subGroup);
  2420. $current->setAttribute('Connections', $connections);
  2421. $current->setAttribute('LinkedElement', $linkedElement);
  2422. $current->setAttribute('graphviz.shape', 'box');
  2423. $current->setGroup($column);
  2424. }
  2425. }
  2426. }
  2427. // 2. Create connections
  2428. // $column start with 1 (depending in Column row)
  2429. foreach ($values as $column => $rowList) {
  2430. foreach ($rowList as $row) {
  2431. $careerId = $row['CareerId'];
  2432. if (isset($careerList[$careerId])) {
  2433. $graph = $careerList[$careerId];
  2434. } else {
  2435. continue;
  2436. }
  2437. $currentCourseId = (int) $row['CourseId'];
  2438. if ($graph->hasVertex($currentCourseId)) {
  2439. $current = $graph->getVertex($currentCourseId);
  2440. } else {
  2441. continue;
  2442. }
  2443. if (isset($row['DependedOn']) && !empty($row['DependedOn'])) {
  2444. $parentList = explode(',', $row['DependedOn']);
  2445. foreach ($parentList as $parentId) {
  2446. $parentId = (int) $parentId;
  2447. if ($graph->hasVertex($parentId)) {
  2448. /** @var Vertex $parent */
  2449. $parent = $graph->getVertex($parentId);
  2450. /*$parent->setAttribute('graphviz.color', 'red');
  2451. $parent->setAttribute('graphviz.label', $name);
  2452. $parent->setAttribute('graphviz.shape', 'square');*/
  2453. $parent->createEdgeTo($current);
  2454. }
  2455. }
  2456. }
  2457. }
  2458. }
  2459. /** @var Graph $graph */
  2460. foreach ($careerList as $id => $graph) {
  2461. if (isset($careerChamiloIdList[$id])) {
  2462. $params = [
  2463. 'item_id' => $careerChamiloIdList[$id],
  2464. 'extra_'.$careerDiagramExtraFieldName => serialize($graph),
  2465. 'extra_'.$extraFieldName => $id,
  2466. ];
  2467. $extraFieldValue->saveFieldValues($params, true);
  2468. }
  2469. }
  2470. }
  2471. }
  2472. /**
  2473. * @param string $file
  2474. * @param bool $moveFile
  2475. * @param array $teacherBackup
  2476. * @param array $groupBackup
  2477. */
  2478. private function importUnsubscribeStatic(
  2479. $file,
  2480. $moveFile = false,
  2481. &$teacherBackup = [],
  2482. &$groupBackup = []
  2483. ) {
  2484. $data = Import::csv_reader($file);
  2485. if (!empty($data)) {
  2486. $this->logger->addInfo(count($data)." records found.");
  2487. foreach ($data as $row) {
  2488. $chamiloUserName = $row['UserName'];
  2489. $chamiloCourseCode = $row['CourseCode'];
  2490. $chamiloSessionId = $row['SessionID'];
  2491. $sessionInfo = api_get_session_info($chamiloSessionId);
  2492. if (empty($sessionInfo)) {
  2493. $this->logger->addError('Session does not exists: '.$chamiloSessionId);
  2494. continue;
  2495. }
  2496. $courseInfo = api_get_course_info($chamiloCourseCode);
  2497. if (empty($courseInfo)) {
  2498. $this->logger->addError('Course does not exists: '.$courseInfo);
  2499. continue;
  2500. }
  2501. $userId = UserManager::get_user_id_from_username($chamiloUserName);
  2502. if (empty($userId)) {
  2503. $this->logger->addError('User does not exists: '.$chamiloUserName);
  2504. continue;
  2505. }
  2506. $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
  2507. WHERE
  2508. user_id = ".$userId." AND
  2509. c_id = '".$courseInfo['real_id']."'
  2510. ";
  2511. $result = Database::query($sql);
  2512. $rows = Database::num_rows($result);
  2513. if ($rows > 0) {
  2514. $userCourseData = Database::fetch_array($result, 'ASSOC');
  2515. if (!empty($userCourseData)) {
  2516. $teacherBackup[$userId][$courseInfo['code']] = $userCourseData;
  2517. }
  2518. }
  2519. $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_USER)."
  2520. WHERE
  2521. user_id = ".$userId." AND
  2522. c_id = '".$courseInfo['real_id']."'
  2523. ";
  2524. $result = Database::query($sql);
  2525. while ($groupData = Database::fetch_array($result, 'ASSOC')) {
  2526. $groupBackup['user'][$userId][$courseInfo['code']][$groupData['group_id']] = $groupData;
  2527. }
  2528. $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)."
  2529. WHERE
  2530. user_id = ".$userId." AND
  2531. c_id = '".$courseInfo['real_id']."'
  2532. ";
  2533. $result = Database::query($sql);
  2534. while ($groupData = Database::fetch_array($result, 'ASSOC')) {
  2535. $groupBackup['tutor'][$userId][$courseInfo['code']][$groupData['group_id']] = $groupData;
  2536. }
  2537. CourseManager::unsubscribe_user(
  2538. $userId,
  2539. $courseInfo['code'],
  2540. $chamiloSessionId
  2541. );
  2542. $this->logger->addError(
  2543. "User '$chamiloUserName' was removed from session: #$chamiloSessionId, Course: ".$courseInfo['code']
  2544. );
  2545. }
  2546. }
  2547. if ($moveFile) {
  2548. $this->moveFile($file);
  2549. }
  2550. }
  2551. /**
  2552. * Dump database tables.
  2553. */
  2554. private function dumpDatabaseTables()
  2555. {
  2556. echo 'Dumping tables'.PHP_EOL;
  2557. // User
  2558. $table = Database::get_main_table(TABLE_MAIN_USER);
  2559. $tableAdmin = Database::get_main_table(TABLE_MAIN_ADMIN);
  2560. $sql = "DELETE FROM $table
  2561. WHERE user_id not in (select user_id from $tableAdmin) and status <> ".ANONYMOUS;
  2562. Database::query($sql);
  2563. echo $sql.PHP_EOL;
  2564. // Truncate tables
  2565. $truncateTables = [
  2566. Database::get_main_table(TABLE_MAIN_COURSE),
  2567. Database::get_main_table(TABLE_MAIN_COURSE_USER),
  2568. Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE),
  2569. Database::get_main_table(TABLE_MAIN_CATEGORY),
  2570. Database::get_main_table(TABLE_MAIN_COURSE_MODULE),
  2571. Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER),
  2572. Database::get_main_table(TABLE_MAIN_SESSION),
  2573. Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY),
  2574. Database::get_main_table(TABLE_MAIN_SESSION_COURSE),
  2575. Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER),
  2576. Database::get_main_table(TABLE_MAIN_SESSION_USER),
  2577. Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION),
  2578. Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES),
  2579. Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES),
  2580. Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES),
  2581. Database::get_main_table(TABLE_MAIN_USER_FIELD),
  2582. Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS),
  2583. Database::get_main_table(TABLE_MAIN_COURSE_FIELD),
  2584. Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES),
  2585. Database::get_main_table(TABLE_MAIN_SESSION_FIELD),
  2586. Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES),
  2587. Database::get_course_table(TABLE_AGENDA),
  2588. Database::get_course_table(TABLE_AGENDA_ATTACHMENT),
  2589. Database::get_course_table(TABLE_AGENDA_REPEAT),
  2590. Database::get_course_table(TABLE_AGENDA_REPEAT_NOT),
  2591. Database::get_main_table(TABLE_PERSONAL_AGENDA),
  2592. Database::get_main_table(TABLE_PERSONAL_AGENDA_REPEAT_NOT),
  2593. Database::get_main_table(TABLE_PERSONAL_AGENDA_REPEAT),
  2594. Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_VALUES),
  2595. Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS),
  2596. Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS),
  2597. Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN),
  2598. Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS),
  2599. Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES),
  2600. Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT),
  2601. Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING),
  2602. Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT),
  2603. Database::get_main_table(TABLE_STATISTIC_TRACK_E_UPLOADS),
  2604. Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT),
  2605. Database::get_main_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY),
  2606. Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY),
  2607. Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION),
  2608. Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG),
  2609. Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT),
  2610. Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT_LOG),
  2611. Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK),
  2612. Database::get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY),
  2613. Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE),
  2614. Database::get_course_table(TABLE_STUDENT_PUBLICATION),
  2615. Database::get_course_table(TABLE_QUIZ_QUESTION),
  2616. Database::get_course_table(TABLE_QUIZ_TEST),
  2617. Database::get_course_table(TABLE_QUIZ_ORDER),
  2618. Database::get_course_table(TABLE_QUIZ_ANSWER),
  2619. Database::get_course_table(TABLE_QUIZ_TEST_QUESTION),
  2620. Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION),
  2621. Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY),
  2622. Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY),
  2623. Database::get_course_table(TABLE_LP_MAIN),
  2624. Database::get_course_table(TABLE_LP_ITEM),
  2625. Database::get_course_table(TABLE_LP_VIEW),
  2626. Database::get_course_table(TABLE_LP_ITEM_VIEW),
  2627. Database::get_course_table(TABLE_DOCUMENT),
  2628. Database::get_course_table(TABLE_ITEM_PROPERTY),
  2629. Database::get_course_table(TABLE_TOOL_LIST),
  2630. Database::get_course_table(TABLE_TOOL_INTRO),
  2631. Database::get_course_table(TABLE_COURSE_SETTING),
  2632. Database::get_course_table(TABLE_SURVEY),
  2633. Database::get_course_table(TABLE_SURVEY_QUESTION),
  2634. Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION),
  2635. Database::get_course_table(TABLE_SURVEY_INVITATION),
  2636. Database::get_course_table(TABLE_SURVEY_ANSWER),
  2637. Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP),
  2638. Database::get_course_table(TABLE_SURVEY_REPORT),
  2639. Database::get_course_table(TABLE_GLOSSARY),
  2640. Database::get_course_table(TABLE_LINK),
  2641. Database::get_course_table(TABLE_LINK_CATEGORY),
  2642. Database::get_course_table(TABLE_GROUP),
  2643. Database::get_course_table(TABLE_GROUP_USER),
  2644. Database::get_course_table(TABLE_GROUP_TUTOR),
  2645. Database::get_course_table(TABLE_GROUP_CATEGORY),
  2646. Database::get_course_table(TABLE_DROPBOX_CATEGORY),
  2647. Database::get_course_table(TABLE_DROPBOX_FEEDBACK),
  2648. Database::get_course_table(TABLE_DROPBOX_POST),
  2649. Database::get_course_table(TABLE_DROPBOX_FILE),
  2650. Database::get_course_table(TABLE_DROPBOX_PERSON),
  2651. ];
  2652. foreach ($truncateTables as $table) {
  2653. $sql = "TRUNCATE $table";
  2654. Database::query($sql);
  2655. echo $sql.PHP_EOL;
  2656. }
  2657. $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
  2658. $sql = "DELETE FROM $table WHERE tool = 'calendar_event'";
  2659. Database::query($sql);
  2660. echo $sql.PHP_EOL;
  2661. }
  2662. /**
  2663. * If csv file ends with '"' character then a '";' is added.
  2664. *
  2665. * @param string $file
  2666. */
  2667. private function fixCSVFile($file)
  2668. {
  2669. /*$f = fopen($file, 'r+');
  2670. $cursor = -1;
  2671. fseek($f, $cursor, SEEK_END);
  2672. $char = fgetc($f);
  2673. while ($char === "\n" || $char === "\r") {
  2674. fseek($f, $cursor--, SEEK_END);
  2675. $char = fgetc($f);
  2676. }
  2677. if ($char === "\"") {
  2678. fseek($f, -1, SEEK_CUR);
  2679. fwrite($f, '";');
  2680. }*/
  2681. }
  2682. }
  2683. $logger = new Logger('cron');
  2684. $emails = isset($_configuration['cron_notification_mails']) ? $_configuration['cron_notification_mails'] : null;
  2685. $minLevel = Logger::DEBUG;
  2686. if (!is_array($emails)) {
  2687. $emails = [$emails];
  2688. }
  2689. $subject = "Cron main/cron/import_csv.php ".date('Y-m-d h:i:s');
  2690. $from = api_get_setting('emailAdministrator');
  2691. /*
  2692. if (!empty($emails)) {
  2693. foreach ($emails as $email) {
  2694. $stream = new NativeMailerHandler($email, $subject, $from, $minLevel);
  2695. $logger->pushHandler(new BufferHandler($stream, 0, $minLevel));
  2696. }
  2697. }*/
  2698. $stream = new StreamHandler(
  2699. api_get_path(SYS_ARCHIVE_PATH).'import_csv.log',
  2700. $minLevel
  2701. );
  2702. $logger->pushHandler(new BufferHandler($stream, 0, $minLevel));
  2703. $logger->pushHandler(new RotatingFileHandler('import_csv', 5, $minLevel));
  2704. $cronImportCSVConditions = isset($_configuration['cron_import_csv_conditions']) ? $_configuration['cron_import_csv_conditions'] : null;
  2705. echo 'See the error log here: '.api_get_path(SYS_ARCHIVE_PATH).'import_csv.log'."\n";
  2706. $import = new ImportCsv($logger, $cronImportCSVConditions);
  2707. if (isset($_configuration['default_admin_user_id_for_cron'])) {
  2708. $import->defaultAdminId = $_configuration['default_admin_user_id_for_cron'];
  2709. }
  2710. // @todo in production disable the dump option
  2711. $dump = false;
  2712. if (isset($argv[1]) && $argv[1] = '--dump') {
  2713. $dump = true;
  2714. }
  2715. if (isset($_configuration['import_csv_disable_dump']) &&
  2716. $_configuration['import_csv_disable_dump'] == true
  2717. ) {
  2718. $import->setDumpValues(false);
  2719. } else {
  2720. $import->setDumpValues($dump);
  2721. }
  2722. $import->setUpdateEmailToDummy(api_get_configuration_value('update_users_email_to_dummy_except_admins'));
  2723. // Do not moves the files to treated
  2724. if (isset($_configuration['import_csv_test'])) {
  2725. $import->test = $_configuration['import_csv_test'];
  2726. } else {
  2727. $import->test = true;
  2728. }
  2729. $languageFilesToLoad = api_get_language_files_to_load($import->defaultLanguage);
  2730. foreach ($languageFilesToLoad as $languageFile) {
  2731. include $languageFile;
  2732. }
  2733. // Set default language to be loaded
  2734. $language = $import->defaultLanguage;
  2735. global $language_interface;
  2736. $language_interface = $language;
  2737. global $language_interface_initial_value;
  2738. $language_interface_initial_value = $language;
  2739. $timeStart = microtime(true);
  2740. $import->run();
  2741. $timeEnd = microtime(true);
  2742. $executionTime = round(($timeEnd - $timeStart) / 60, 2);
  2743. $logger->addInfo("Total execution Time $executionTime Min");
  2744. if (isset($_configuration['import_csv_fix_permissions']) &&
  2745. $_configuration['import_csv_fix_permissions'] == true
  2746. ) {
  2747. $command = "sudo find ".api_get_path(SYS_COURSE_PATH)." -type d -exec chmod 777 {} \; ";
  2748. echo "Executing: ".$command.PHP_EOL;
  2749. system($command);
  2750. $command = "sudo find ".api_get_path(SYS_CODE_PATH)."upload/users -type d -exec chmod 777 {} \;";
  2751. echo "Executing: ".$command.PHP_EOL;
  2752. system($command);
  2753. }