import_csv.php 71 KB

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