Passed
Push — 1.11.x ( 03d338...91ebb4 )
by Julito
10:11
created

ImportCsv::importSessionsUsersCareers()   C

Complexity

Conditions 14
Paths 27

Size

Total Lines 81
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 14
eloc 46
c 1
b 0
f 1
nc 27
nop 4
dl 0
loc 81
rs 6.2666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CourseBundle\Entity\CCalendarEvent;
5
use Chamilo\CourseBundle\Entity\CItemProperty;
6
use Chamilo\PluginBundle\Entity\StudentFollowUp\CarePost;
7
use Fhaculty\Graph\Graph;
8
use Monolog\Handler\BufferHandler;
9
use Monolog\Handler\ErrorLogHandler;
10
use Monolog\Handler\RotatingFileHandler;
11
use Monolog\Handler\StreamHandler;
12
use Monolog\Logger;
13
14
if (PHP_SAPI != 'cli') {
15
    exit('Run this script through the command line or comment this line in the code');
16
}
17
18
if (file_exists('multiple_url_fix.php')) {
19
    require 'multiple_url_fix.php';
20
}
21
22
require_once __DIR__.'/../inc/global.inc.php';
23
24
ini_set('memory_limit', -1);
25
ini_set('max_execution_time', 0);
26
ini_set('log_errors', '1');
27
ini_set('display_errors', '1');
28
29
/**
30
 * Class ImportCsv.
31
 */
32
class ImportCsv
33
{
34
    public $test;
35
    public $defaultLanguage = 'dutch';
36
    public $extraFieldIdNameList = [
37
        'session' => 'external_session_id',
38
        'session_career' => 'external_career_id',
39
        'course' => 'external_course_id',
40
        'user' => 'external_user_id',
41
        'calendar_event' => 'external_calendar_event_id',
42
        'career' => 'external_career_id',
43
        'career_urls' => 'career_urls',
44
        'career_diagram' => 'career_diagram',
45
    ];
46
    public $defaultAdminId = 1;
47
    public $defaultSessionVisibility = 1;
48
49
    /**
50
     * When creating a user the expiration date is set to registration date + this value.
51
     *
52
     * @var int number of years
53
     */
54
    public $expirationDateInUserCreation = 1;
55
56
    public $batchSize = 20;
57
58
    /**
59
     * When updating a user the expiration date is set to update date + this value.
60
     *
61
     * @var int number of years
62
     */
63
    public $expirationDateInUserUpdate = 1;
64
    public $daysCoachAccessBeforeBeginning = 14;
65
    public $daysCoachAccessAfterBeginning = 14;
66
    public $conditions;
67
    private $logger;
68
    private $dumpValues;
69
    private $updateEmailToDummy;
70
71
    /**
72
     * @param Monolog\Logger $logger
73
     * @param array
74
     */
75
    public function __construct($logger, $conditions)
76
    {
77
        $this->logger = $logger;
78
        $this->conditions = $conditions;
79
        $this->updateEmailToDummy = false;
80
    }
81
82
    /**
83
     * @param bool $dump
84
     */
85
    public function setDumpValues($dump)
86
    {
87
        $this->dumpValues = $dump;
88
    }
89
90
    /**
91
     * @return bool
92
     */
93
    public function getDumpValues()
94
    {
95
        return $this->dumpValues;
96
    }
97
98
    /**
99
     * Runs the import process.
100
     */
101
    public function run()
102
    {
103
        global $_configuration;
104
105
        $value = api_get_configuration_value('import_csv_custom_url_id');
106
        if (!empty($value)) {
107
            $_configuration['access_url'] = $value;
108
        }
109
110
        $path = api_get_path(SYS_CODE_PATH).'cron/incoming/';
111
        if (!is_dir($path)) {
112
            echo "The folder! $path does not exits";
113
114
            return 0;
115
        }
116
117
        if ($this->getDumpValues()) {
118
            $this->dumpDatabaseTables();
119
        }
120
121
        echo 'Reading files: '.PHP_EOL.PHP_EOL;
122
123
        $files = scandir($path);
124
        $fileToProcess = [];
125
        $fileToProcessStatic = [];
126
        $teacherBackup = [];
127
        $groupBackup = [];
128
129
        $this->prepareImport();
130
131
        if (!empty($files)) {
132
            foreach ($files as $file) {
133
                $fileInfo = pathinfo($file);
134
                if (isset($fileInfo['extension']) && $fileInfo['extension'] === 'csv') {
135
                    // Checking teachers_yyyymmdd.csv,
136
                    // courses_yyyymmdd.csv, students_yyyymmdd.csv and sessions_yyyymmdd.csv
137
                    $parts = explode('_', $fileInfo['filename']);
138
                    $preMethod = ucwords($parts[1]);
139
                    $preMethod = str_replace('-static', 'Static', $preMethod);
140
                    $method = 'import'.$preMethod;
141
                    $isStatic = strpos($method, 'Static');
142
143
                    if ($method == 'importSessionsextidStatic') {
144
                        $method = 'importSessionsExtIdStatic';
145
                    }
146
147
                    if ($method == 'importCourseinsertStatic') {
148
                        $method = 'importSubscribeUserToCourse';
149
                    }
150
151
                    if ($method == 'importUnsubsessionsextidStatic') {
152
                        $method = 'importUnsubsessionsExtidStatic';
153
                    }
154
155
                    if ($method == 'importCareersdiagram') {
156
                        $method = 'importCareersDiagram';
157
                    }
158
159
                    if ($method == 'importCareersresults') {
160
                        $method = 'importCareersResults';
161
                    }
162
163
                    if ($method == 'importOpensessions') {
164
                        $method = 'importOpenSessions';
165
                    }
166
167
                    if ($method === 'importSessionsall') {
168
                        $method = 'importSessionsUsersCareers';
169
                    }
170
171
                    if ($method === 'importSubsessionsextidStatic') {
172
                        $method = 'importSubscribeUserToCourseSessionExtStatic';
173
                    }
174
175
                    if (method_exists($this, $method)) {
176
                        if ((
177
                                $method == 'importSubscribeStatic' ||
178
                                $method == 'importSubscribeUserToCourse'
179
                            ) ||
180
                            empty($isStatic)
181
                        ) {
182
                            $fileToProcess[$parts[1]][] = [
183
                                'method' => $method,
184
                                'file' => $path.$fileInfo['basename'],
185
                            ];
186
                        } else {
187
                            $fileToProcessStatic[$parts[1]][] = [
188
                                'method' => $method,
189
                                'file' => $path.$fileInfo['basename'],
190
                            ];
191
                        }
192
                    } else {
193
                        echo "Error - This file '$file' can't be processed.".PHP_EOL;
194
                        echo "Trying to call $method".PHP_EOL;
195
                        echo "The file have to has this format:".PHP_EOL;
196
                        echo "prefix_students_ddmmyyyy.csv, prefix_teachers_ddmmyyyy.csv,
197
                        prefix_courses_ddmmyyyy.csv, prefix_sessions_ddmmyyyy.csv ".PHP_EOL;
198
                        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
199
                    }
200
                }
201
            }
202
203
            if (empty($fileToProcess) && empty($fileToProcessStatic)) {
204
                echo 'Error - no files to process.';
205
206
                return 0;
207
            }
208
209
            $sections = [
210
                'students',
211
                'teachers',
212
                'courses',
213
                'sessions',
214
                'sessionsall',
215
                'opensessions',
216
                'subscribe-static',
217
                'courseinsert-static',
218
                'unsubscribe-static',
219
                'care',
220
                'careers',
221
                'careersdiagram',
222
                'careersresults',
223
            ];
224
225
            foreach ($sections as $section) {
226
                if (isset($fileToProcess[$section]) && !empty($fileToProcess[$section])) {
227
                    $this->logger->addInfo("-- Import $section --");
228
                    $files = $fileToProcess[$section];
229
                    foreach ($files as $fileInfo) {
230
                        $method = $fileInfo['method'];
231
                        $file = $fileInfo['file'];
232
                        echo 'File: '.$file.PHP_EOL;
233
                        echo 'Method : '.$method.PHP_EOL;
234
                        echo PHP_EOL;
235
236
                        $this->logger->addInfo('====================================================');
237
                        $this->logger->addInfo("Reading file: $file");
238
                        $this->logger->addInfo("Loading method $method ");
239
                        if ($method == 'importSessions' || $method == 'importOpenSessions') {
240
                            $this->$method(
241
                                $file,
242
                                true,
243
                                $teacherBackup,
244
                                $groupBackup
245
                            );
246
                        } else {
247
                            $this->$method($file, true);
248
                        }
249
                        $this->logger->addInfo('--Finish reading file--');
250
                    }
251
                }
252
            }
253
254
            $sections = [
255
                'students-static',
256
                'teachers-static',
257
                'courses-static',
258
                'sessions-static',
259
                'sessionsextid-static',
260
                'unsubscribe-static',
261
                'unsubsessionsextid-static',
262
                'subsessionsextid-static',
263
                'calendar-static',
264
            ];
265
266
            foreach ($sections as $section) {
267
                if (isset($fileToProcessStatic[$section]) &&
268
                    !empty($fileToProcessStatic[$section])
269
                ) {
270
                    $this->logger->addInfo("-- Import static files $section --");
271
                    $files = $fileToProcessStatic[$section];
272
                    foreach ($files as $fileInfo) {
273
                        $method = $fileInfo['method'];
274
275
                        $file = $fileInfo['file'];
276
                        echo 'Static file: '.$file.PHP_EOL;
277
                        echo 'Method : '.$method.PHP_EOL;
278
                        echo PHP_EOL;
279
                        $this->logger->addInfo("Reading static file: $file");
280
                        $this->logger->addInfo("Loading method $method ");
281
                        $this->$method(
282
                            $file,
283
                            true,
284
                            $teacherBackup,
285
                            $groupBackup
286
                        );
287
                        $this->logger->addInfo('--Finish reading file--');
288
                    }
289
                }
290
            }
291
            $this->logger->addInfo('teacher backup');
292
            $this->logger->addInfo(print_r($teacherBackup, 1));
293
        }
294
    }
295
296
    /**
297
     * @param $file
298
     * @param bool $moveFile
299
     */
300
    public function importCare($file, $moveFile = false)
301
    {
302
        $data = Import::csv_reader($file);
303
        $counter = 1;
304
        $batchSize = $this->batchSize;
305
        $em = Database::getManager();
306
307
        if (!empty($data)) {
308
            $this->logger->addInfo(count($data)." records found.");
309
            $items = [];
310
            foreach ($data as $list) {
311
                $post = [];
312
                foreach ($list as $key => $value) {
313
                    $key = (string) trim($key);
314
                    // Remove utf8 bom
315
                    $key = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $key);
316
                    $post[$key] = $value;
317
                }
318
319
                if (empty($post)) {
320
                    continue;
321
                }
322
323
                $externalId = $post['External_care_id'];
324
                $items[$externalId] = $post;
325
            }
326
            ksort($items);
327
328
            foreach ($items as $row) {
329
                // Insert user
330
                //$insertUserInfo = api_get_user_info_from_username($row['Added_by']);
331
332
                // User about the post
333
                $userId = UserManager::get_user_id_from_original_id(
334
                    $row['Added_by'],
335
                    $this->extraFieldIdNameList['user']
336
                );
337
338
                $insertUserInfo = api_get_user_info($userId);
339
340
                if (empty($insertUserInfo)) {
341
                    $this->logger->addInfo("User: '".$row['Added_by']."' doesn't exists. Skip this entry.");
342
                    continue;
343
                }
344
                $insertUserInfo = api_get_user_entity($insertUserInfo['user_id']);
345
346
                // User about the post
347
                $userId = UserManager::get_user_id_from_original_id(
348
                    $row['External_user_id'],
349
                    $this->extraFieldIdNameList['user']
350
                );
351
352
                if (empty($userId)) {
353
                    $this->logger->addInfo("User does '".$row['External_user_id']."' not exists skip this entry.");
354
                    continue;
355
                }
356
357
                $userInfo = api_get_user_entity($userId);
358
359
                if (empty($userInfo)) {
360
                    $this->logger->addInfo("Chamilo user does not found: #".$userId."' ");
361
                    continue;
362
                }
363
364
                // Dates
365
                $createdAt = $this->createDateTime($row['Added_On']);
366
                $updatedAt = $this->createDateTime($row['Edited_on']);
367
368
                // Parent
369
                $parent = null;
370
                if (!empty($row['Parent_id'])) {
371
                    $parentId = $items[$row['Parent_id']];
372
                    $criteria = [
373
                        'externalCareId' => $parentId,
374
                    ];
375
                    $parent = $em->getRepository('ChamiloPluginBundle:StudentFollowUp\CarePost')->findOneBy($criteria);
376
                }
377
378
                // Tags
379
                $tags = explode(',', $row['Tags']);
380
381
                // Check if post already was added:
382
                $criteria = [
383
                    'externalCareId' => $row['External_care_id'],
384
                ];
385
                $post = $em->getRepository('ChamiloPluginBundle:StudentFollowUp\CarePost')->findOneBy($criteria);
386
387
                if (empty($post)) {
388
                    $post = new CarePost();
389
                    $this->logger->addInfo("New post will be created no match for externalCareId = ".$row['External_care_id']);
390
                }
391
392
                $contentDecoded = utf8_encode(base64_decode($row['Article']));
393
394
                $post
395
                    ->setTitle($row['Title'])
396
                    ->setContent($contentDecoded)
397
                    ->setExternalCareId($row['External_care_id'])
398
                    ->setCreatedAt($createdAt)
399
                    ->setUpdatedAt($updatedAt)
400
                    ->setPrivate((int) $row['Private'])
401
                    ->setInsertUser($insertUserInfo)
402
                    ->setExternalSource((int) $row['Source_is_external'])
403
                    ->setParent($parent)
404
                    ->setTags($tags)
405
                    ->setUser($userInfo)
406
                    ->setAttachment($row['Attachement'])
407
                ;
408
                $em->persist($post);
409
                $em->flush();
410
411
                $this->logger->addInfo("Post id saved #".$post->getId());
412
413
                if (($counter % $batchSize) === 0) {
414
                    $em->flush();
415
                    $em->clear(); // Detaches all objects from Doctrine!
416
                }
417
                $counter++;
418
            }
419
420
            $em->clear(); // Detaches all objects from Doctrine!
421
        }
422
    }
423
424
    /**
425
     * @return mixed
426
     */
427
    public function getUpdateEmailToDummy()
428
    {
429
        return $this->updateEmailToDummy;
430
    }
431
432
    /**
433
     * @param mixed $updateEmailToDummy
434
     */
435
    public function setUpdateEmailToDummy($updateEmailToDummy)
436
    {
437
        $this->updateEmailToDummy = $updateEmailToDummy;
438
    }
439
440
    /**
441
     * Change emails of all users except admins.
442
     */
443
    public function updateUsersEmails()
444
    {
445
        if ($this->getUpdateEmailToDummy() === true) {
446
            $sql = "UPDATE user SET email = CONCAT(username,'@example.com') WHERE id NOT IN (SELECT user_id FROM admin)";
447
            Database::query($sql);
448
        }
449
    }
450
451
    /**
452
     * Prepares extra fields before the import.
453
     */
454
    private function prepareImport()
455
    {
456
        // Create user extra field: extra_external_user_id
457
        UserManager::create_extra_field(
458
            $this->extraFieldIdNameList['user'],
459
            1,
460
            'External user id',
461
            null
462
        );
463
464
        // Create course extra field: extra_external_course_id
465
        CourseManager::create_course_extra_field(
466
            $this->extraFieldIdNameList['course'],
467
            1,
468
            'External course id',
469
            ''
470
        );
471
472
        CourseManager::create_course_extra_field(
473
            'disable_import_calendar',
474
            13,
475
            'Disable import calendar',
476
            ''
477
        );
478
479
        // Create session extra field extra_external_session_id
480
        SessionManager::create_session_extra_field(
481
            $this->extraFieldIdNameList['session'],
482
            1,
483
            'External session id'
484
        );
485
486
        SessionManager::create_session_extra_field(
487
            $this->extraFieldIdNameList['session_career'],
488
            1,
489
            'Career id'
490
        );
491
492
        // Create calendar_event extra field extra_external_session_id
493
        $extraField = new ExtraField('calendar_event');
494
        $extraField->save(
495
            [
496
                'field_type' => ExtraField::FIELD_TYPE_TEXT,
497
                'variable' => $this->extraFieldIdNameList['calendar_event'],
498
                'display_text' => 'External calendar event id',
499
            ]
500
        );
501
502
        $extraField = new ExtraField('career');
503
        $extraField->save(
504
            [
505
                'visible_to_self' => 1,
506
                'field_type' => ExtraField::FIELD_TYPE_TEXT,
507
                'variable' => $this->extraFieldIdNameList['career'],
508
                'display_text' => 'External career id',
509
            ]
510
        );
511
512
        $extraField->save(
513
            [
514
                'visible_to_self' => 1,
515
                'field_type' => ExtraField::FIELD_TYPE_TEXTAREA,
516
                'variable' => $this->extraFieldIdNameList['career_diagram'],
517
                'display_text' => 'Career diagram',
518
            ]
519
        );
520
521
        $extraField->save(
522
            [
523
                'visible_to_self' => 1,
524
                'field_type' => ExtraField::FIELD_TYPE_TEXTAREA,
525
                'variable' => $this->extraFieldIdNameList['career_urls'],
526
                'display_text' => 'Career urls',
527
            ]
528
        );
529
    }
530
531
    /**
532
     * @param string $file
533
     */
534
    private function moveFile($file)
535
    {
536
        $moved = str_replace('incoming', 'treated', $file);
537
538
        if ($this->test) {
539
            $result = 1;
540
        } else {
541
            $result = rename($file, $moved);
542
        }
543
544
        if ($result) {
545
            $this->logger->addInfo("Moving file to the treated folder: $file");
546
        } else {
547
            $this->logger->addError(
548
                "Error - Cant move file to the treated folder: $file"
549
            );
550
        }
551
    }
552
553
    /**
554
     * @param array $row
555
     *
556
     * @return array
557
     */
558
    private function cleanUserRow($row)
559
    {
560
        $row['lastname'] = $row['LastName'];
561
        $row['firstname'] = $row['FirstName'];
562
        $row['email'] = $row['Email'];
563
        $row['username'] = $row['UserName'];
564
        $row['password'] = $row['Password'];
565
        $row['auth_source'] = isset($row['AuthSource']) ? $row['AuthSource'] : PLATFORM_AUTH_SOURCE;
566
        $row['official_code'] = $row['OfficialCode'];
567
        $row['phone'] = isset($row['PhoneNumber']) ? $row['PhoneNumber'] : '';
568
569
        if (isset($row['StudentID'])) {
570
            $row['extra_'.$this->extraFieldIdNameList['user']] = $row['StudentID'];
571
        }
572
573
        if (isset($row['TeacherID'])) {
574
            $row['extra_'.$this->extraFieldIdNameList['user']] = $row['TeacherID'];
575
        }
576
577
        return $row;
578
    }
579
580
    /**
581
     * @param array $row
582
     *
583
     * @return array
584
     */
585
    private function cleanCourseRow($row)
586
    {
587
        $row['title'] = $row['Title'];
588
        $row['course_code'] = $row['Code'];
589
        $row['course_category'] = $row['CourseCategory'];
590
        $row['email'] = $row['Teacher'];
591
        $row['language'] = $row['Language'];
592
        $row['visibility'] = isset($row['Visibility']) ? $row['Visibility'] : COURSE_VISIBILITY_REGISTERED;
593
594
        $row['teachers'] = [];
595
        if (isset($row['Teacher']) && !empty($row['Teacher'])) {
596
            $this->logger->addInfo("Teacher list found: ".$row['Teacher']);
597
            $teachers = explode(',', $row['Teacher']);
598
            if (!empty($teachers)) {
599
                foreach ($teachers as $teacherUserName) {
600
                    $teacherUserName = trim($teacherUserName);
601
                    $userInfo = api_get_user_info_from_username($teacherUserName);
602
                    if (!empty($userInfo)) {
603
                        $this->logger->addInfo("Username found: $teacherUserName");
604
                        $row['teachers'][] = $userInfo['user_id'];
605
                    }
606
                }
607
            }
608
        }
609
610
        if (isset($row['CourseID'])) {
611
            $row['extra_'.$this->extraFieldIdNameList['course']] = $row['CourseID'];
612
        }
613
614
        return $row;
615
    }
616
617
    /**
618
     * File to import.
619
     *
620
     * @param string $file
621
     */
622
    private function importTeachersStatic($file)
623
    {
624
        $this->importTeachers($file, true);
625
    }
626
627
    /**
628
     * File to import.
629
     *
630
     * @param string $file
631
     * @param bool   $moveFile
632
     */
633
    private function importTeachers($file, $moveFile = true)
634
    {
635
        $this->fixCSVFile($file);
636
        $data = Import::csvToArray($file);
637
638
        /* Unique identifier: official-code username.
639
        Email address and password should never get updated. *ok
640
        The only fields that I can think of that should update if the data changes in the csv file are FirstName and LastName. *ok
641
        A slight edit of these fields should be taken into account. ???
642
        Adding teachers is no problem, but deleting them shouldn’t be automated, but we should get a log of “to delete teachers”.
643
        We’ll handle that manually if applicable.
644
        No delete!
645
        */
646
        $language = $this->defaultLanguage;
647
648
        if (!empty($data)) {
649
            $this->logger->addInfo(count($data)." records found.");
650
            $expirationDateOnCreation = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserCreation)."years"));
651
            $expirationDateOnUpdate = api_get_utc_datetime(strtotime("+".intval($this->expirationDateInUserUpdate)."years"));
652
653
            $batchSize = $this->batchSize;
654
            $em = Database::getManager();
655
            $counter = 1;
656
            foreach ($data as $row) {
657
                $row = $this->cleanUserRow($row);
658
                $externalUserId = $row['official_code'];
659
                $row['extra_'.$this->extraFieldIdNameList['user']] = $externalUserId;
660
661
                $user_id = UserManager::get_user_id_from_original_id(
662
                    $row['extra_'.$this->extraFieldIdNameList['user']],
663
                    $this->extraFieldIdNameList['user']
664
                );
665
                $userInfo = [];
666
                $userInfoByOfficialCode = null;
667
668
                if (!empty($user_id)) {
669
                    $userInfo = api_get_user_info($user_id);
670
                    $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
671
                }
672
673
                if (empty($userInfo) && empty($userInfoByOfficialCode)) {
674
                    // Create user
675
                    $userId = UserManager::create_user(
676
                        $row['firstname'],
677
                        $row['lastname'],
678
                        COURSEMANAGER,
679
                        $row['email'],
680
                        $row['username'],
681
                        $row['password'],
682
                        $row['official_code'],
683
                        $language, //$row['language'],
684
                        $row['phone'],
685
                        null, //$row['picture'], //picture
686
                        $row['auth_source'], // ?
687
                        $expirationDateOnCreation, //'0000-00-00 00:00:00', //$row['expiration_date'], //$expiration_date = '0000-00-00 00:00:00',
688
                        1, //active
689
                        0,
690
                        null, // extra
691
                        null, //$encrypt_method = '',
692
                        false //$send_mail = false
693
                    );
694
695
                    $row['extra_mail_notify_invitation'] = 1;
696
                    $row['extra_mail_notify_message'] = 1;
697
                    $row['extra_mail_notify_group_message'] = 1;
698
699
                    if ($userId) {
700
                        foreach ($row as $key => $value) {
701
                            if (substr($key, 0, 6) == 'extra_') {
702
                                //an extra field
703
                                UserManager::update_extra_field_value(
704
                                    $userId,
705
                                    substr($key, 6),
706
                                    $value
707
                                );
708
                            }
709
                        }
710
                        $this->logger->addInfo("Teachers - User created: ".$row['username']);
711
                    } else {
712
                        $this->logger->addError("Teachers - User NOT created: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
713
                        $this->logger->addError(strip_tags(Display::getFlashToString()));
714
                        Display::cleanFlashMessages();
715
                    }
716
                } else {
717
                    if (empty($userInfo)) {
718
                        $this->logger->addError("Teachers - Can't update user :".$row['username']);
719
                        continue;
720
                    }
721
722
                    // Update user
723
                    $result = UserManager::update_user(
724
                        $userInfo['user_id'],
725
                        $row['firstname'], // <<-- changed
726
                        $row['lastname'], // <<-- changed
727
                        $userInfo['username'],
728
                        null, //$password = null,
729
                        $row['auth_source'],
730
                        $userInfo['email'],
731
                        COURSEMANAGER,
732
                        $userInfo['official_code'],
733
                        $userInfo['phone'],
734
                        $userInfo['picture_uri'],
735
                        $expirationDateOnUpdate,
736
                        $userInfo['active'],
737
                        null, //$creator_id = null,
738
                        0, //$hr_dept_id = 0,
739
                        null, // $extra = null,
740
                        null, //$language = 'english',
741
                        null, //$encrypt_method = '',
742
                        false, //$send_email = false,
743
                        0 //$reset_password = 0
744
                    );
745
746
                    if ($result) {
747
                        foreach ($row as $key => $value) {
748
                            if (substr($key, 0, 6) == 'extra_') {
749
                                //an extra field
750
                                UserManager::update_extra_field_value(
751
                                    $userInfo['user_id'],
752
                                    substr($key, 6),
753
                                    $value
754
                                );
755
                            }
756
                        }
757
                        $this->logger->addInfo("Teachers - User updated: ".$row['username']);
758
                    } else {
759
                        $this->logger->addError("Teachers - User not updated: ".$row['username']);
760
                    }
761
                }
762
763
                if (($counter % $batchSize) === 0) {
764
                    $em->flush();
765
                    $em->clear(); // Detaches all objects from Doctrine!
766
                }
767
                $counter++;
768
            }
769
770
            $em->clear(); // Detaches all objects from Doctrine!
771
        }
772
773
        if ($moveFile) {
774
            $this->moveFile($file);
775
        }
776
777
        $this->updateUsersEmails();
778
    }
779
780
    /**
781
     * @param string $file
782
     */
783
    private function importStudentsStatic($file)
784
    {
785
        $this->importStudents($file, true);
786
    }
787
788
    /**
789
     * @param string $file
790
     * @param bool   $moveFile
791
     */
792
    private function importStudents($file, $moveFile = true)
793
    {
794
        $this->fixCSVFile($file);
795
        $data = Import::csvToArray($file);
796
797
        /*
798
         * Another users import.
799
        Unique identifier: official code and username . ok
800
        Password should never get updated. ok
801
        If an update should need to occur (because it changed in the .csv),
802
        we’ll want that logged. We will handle this manually in that case.
803
        All other fields should be updateable, though passwords should of course not get updated. ok
804
        If a user gets deleted (not there anymore),
805
        He should be set inactive one year after the current date.
806
        So I presume you’ll just update the expiration date.
807
        We want to grant access to courses up to a year after deletion.
808
         */
809
        $timeStart = microtime(true);
810
811
        $batchSize = $this->batchSize;
812
        $em = Database::getManager();
813
814
        if (!empty($data)) {
815
            $language = $this->defaultLanguage;
816
            $this->logger->addInfo(count($data)." records found.");
817
818
            $expirationDateOnCreate = api_get_utc_datetime(
819
                strtotime("+".intval($this->expirationDateInUserCreation)."years")
820
            );
821
            $expirationDateOnUpdate = api_get_utc_datetime(
822
                strtotime("+".intval($this->expirationDateInUserUpdate)."years")
823
            );
824
825
            $counter = 1;
826
            $secondsInYear = 365 * 24 * 60 * 60;
827
828
            foreach ($data as $row) {
829
                $row = $this->cleanUserRow($row);
830
                $externalUserId = $row['official_code'];
831
                $row['extra_'.$this->extraFieldIdNameList['user']] = $externalUserId;
832
833
                $user_id = UserManager::get_user_id_from_original_id(
834
                    $row['extra_'.$this->extraFieldIdNameList['user']],
835
                    $this->extraFieldIdNameList['user']
836
                );
837
838
                $userInfo = [];
839
                $userInfoByOfficialCode = null;
840
                if (!empty($user_id)) {
841
                    $userInfo = api_get_user_info($user_id, false, true);
842
                    $userInfoByOfficialCode = api_get_user_info_from_official_code($row['official_code']);
843
                }
844
845
                $userInfoFromUsername = api_get_user_info_from_username($row['username']);
846
                if (!empty($userInfoFromUsername)) {
847
                    $extraFieldValue = new ExtraFieldValue('user');
848
                    $extraFieldValues = $extraFieldValue->get_values_by_handler_and_field_variable(
849
                        $userInfoFromUsername['user_id'],
850
                        $this->extraFieldIdNameList['user']
851
                    );
852
853
                    if (!empty($extraFieldValues)) {
854
                        $value = 0;
855
                        foreach ($extraFieldValues as $extraFieldValue) {
856
                            if (isset($extraFieldValue['value'])) {
857
                                $value = $extraFieldValue['value'];
858
                            }
859
                        }
860
                        if (!empty($user_id) && $value != $user_id) {
861
                            $emails = api_get_configuration_value('cron_notification_help_desk');
862
                            if (!empty($emails)) {
863
                                $this->logger->addInfo('Preparing email to users in configuration: "cron_notification_help_desk"');
864
                                $subject = 'User not added due to same username';
865
                                $body = 'Cannot add username: "'.$row['username'].'"
866
                                    with external_user_id: '.$row['extra_'.$this->extraFieldIdNameList['user']].'
867
                                    because '.$userInfoFromUsername['username'].' with external_user_id '.$value.' exists on the portal';
868
                                $this->logger->addInfo($body);
869
                                foreach ($emails as $email) {
870
                                    api_mail_html('', $email, $subject, $body);
871
                                }
872
                            }
873
                        }
874
                    }
875
                }
876
877
                if (empty($userInfo) && empty($userInfoByOfficialCode)) {
878
                    // Create user
879
                    $result = UserManager::create_user(
880
                        $row['firstname'],
881
                        $row['lastname'],
882
                        STUDENT,
883
                        $row['email'],
884
                        $row['username'],
885
                        $row['password'],
886
                        $row['official_code'],
887
                        $language, //$row['language'],
888
                        $row['phone'],
889
                        null, //$row['picture'], //picture
890
                        $row['auth_source'], // ?
891
                        $expirationDateOnCreate,
892
                        1, //active
893
                        0,
894
                        null, // extra
895
                        null, //$encrypt_method = '',
896
                        false //$send_mail = false
897
                    );
898
899
                    $row['extra_mail_notify_invitation'] = 1;
900
                    $row['extra_mail_notify_message'] = 1;
901
                    $row['extra_mail_notify_group_message'] = 1;
902
903
                    if ($result) {
904
                        foreach ($row as $key => $value) {
905
                            if (substr($key, 0, 6) === 'extra_') {
906
                                //an extra field
907
                                UserManager::update_extra_field_value(
908
                                    $result,
909
                                    substr($key, 6),
910
                                    $value
911
                                );
912
                            }
913
                        }
914
                        $this->logger->addInfo("Students - User created: ".$row['username']);
915
                    } else {
916
                        $this->logger->addError("Students - User NOT created: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
917
                        $this->logger->addError(strip_tags(Display::getFlashToString()));
918
                        Display::cleanFlashMessages();
919
                    }
920
                } else {
921
                    if (empty($userInfo)) {
922
                        $this->logger->addError("Students - Can't update user :".$row['username']);
923
                        continue;
924
                    }
925
926
                    if (isset($row['action']) && $row['action'] === 'delete') {
927
                        // Inactive one year later
928
                        $userInfo['expiration_date'] = api_get_utc_datetime(api_strtotime(time() + $secondsInYear));
929
                    }
930
931
                    $password = $row['password']; // change password
932
                    $email = $row['email']; // change email
933
                    $resetPassword = 2; // allow password change
934
935
                    // Conditions that disables the update of password and email:
936
                    if (isset($this->conditions['importStudents'])) {
937
                        if (isset($this->conditions['importStudents']['update']) &&
938
                            isset($this->conditions['importStudents']['update']['avoid'])
939
                        ) {
940
                            // Blocking email update -
941
                            // 1. Condition
942
                            $avoidUsersWithEmail = $this->conditions['importStudents']['update']['avoid']['email'];
943
                            if ($userInfo['email'] != $row['email'] && in_array($row['email'], $avoidUsersWithEmail)) {
944
                                $this->logger->addInfo("Students - User email is not updated : ".$row['username']." because the avoid conditions (email).");
945
                                // Do not change email keep the old email.
946
                                $email = $userInfo['email'];
947
                            }
948
949
                            // 2. Condition
950
                            if (!in_array($userInfo['email'], $avoidUsersWithEmail) && !in_array($row['email'], $avoidUsersWithEmail)) {
951
                                $this->logger->addInfo("Students - User email is not updated from ".$userInfo['email']." to ".$row['email']." because the avoid conditions (email).");
952
                                $email = $userInfo['email'];
953
                            }
954
955
                            // 3. Condition
956
                            if (in_array($userInfo['email'], $avoidUsersWithEmail) && !in_array($row['email'], $avoidUsersWithEmail)) {
957
                                $this->logger->addInfo('Email to be updated from:'.$userInfo['email'].' to '.$row['email']);
958
                                $email = $row['email'];
959
                            }
960
961
                            // Blocking password update
962
                            //$avoidUsersWithPassword = $this->conditions['importStudents']['update']['avoid']['password'];
963
964
                            /*if (isset($row['password'])) {
965
                                $user = api_get_user_entity($userInfo['id']);
966
                                $encoded = UserManager::encryptPassword(
967
                                    $row['password'],
968
                                    $user
969
                                );
970
971
                                if ($userInfo['password'] != $encoded &&
972
                                    in_array($row['password'], $avoidUsersWithPassword)
973
                                ) {
974
                                    $this->logger->addInfo(
975
                                        "Students - User password is not updated: ".$row['username']." because the avoid conditions (password)."
976
                                    );
977
                                    $password = null;
978
                                    $resetPassword = 0; // disallow password change
979
                                }
980
                            }*/
981
                        }
982
                    }
983
984
                    // Always disallow password change during update
985
                    $password = null;
986
                    $resetPassword = 0; // disallow password change
987
988
                    // Update user
989
                    $result = UserManager::update_user(
990
                        $userInfo['user_id'],
991
                        $row['firstname'], // <<-- changed
992
                        $row['lastname'], // <<-- changed
993
                        $row['username'], // <<-- changed
994
                        $password, //$password = null,
995
                        $row['auth_source'],
996
                        $email,
997
                        STUDENT,
998
                        $userInfo['official_code'],
999
                        $userInfo['phone'],
1000
                        $userInfo['picture_uri'],
1001
                        $expirationDateOnUpdate,
1002
                        $userInfo['active'],
1003
                        null, //$creator_id = null,
1004
                        0, //$hr_dept_id = 0,
1005
                        null, // $extra = null,
1006
                        null, //$language = 'english',
1007
                        null, //$encrypt_method = '',
1008
                        false, //$send_email = false,
1009
                        $resetPassword //$reset_password = 0
1010
                    );
1011
1012
                    if ($result) {
1013
                        if ($row['username'] != $userInfo['username']) {
1014
                            $this->logger->addInfo("Students - Username was changes from '".$userInfo['username']."' to '".$row['username']."' ");
1015
                        }
1016
                        foreach ($row as $key => $value) {
1017
                            if (substr($key, 0, 6) === 'extra_') {
1018
                                //an extra field
1019
                                UserManager::update_extra_field_value(
1020
                                    $userInfo['user_id'],
1021
                                    substr($key, 6),
1022
                                    $value
1023
                                );
1024
                            }
1025
                        }
1026
                        $this->logger->addInfo(
1027
                            'Students - User updated: username:'.$row['username'].' firstname:'.$row['firstname'].' lastname:'.$row['lastname'].' email:'.$email
1028
                        );
1029
                    } else {
1030
                        $this->logger->addError("Students - User NOT updated: ".$row['username']." ".$row['firstname']." ".$row['lastname']);
1031
                    }
1032
                }
1033
1034
                if (($counter % $batchSize) === 0) {
1035
                    $em->flush();
1036
                    $em->clear(); // Detaches all objects from Doctrine!
1037
                    $this->logger->addInfo("Detaches all objects");
1038
                }
1039
                $counter++;
1040
            }
1041
            $em->clear(); // Detaches all objects from Doctrine!
1042
        }
1043
1044
        $timeEnd = microtime(true);
1045
        $executionTime = round(($timeEnd - $timeStart) / 60, 2);
1046
        $this->logger->addInfo("Execution Time for process students: $executionTime Min");
1047
1048
        if ($moveFile) {
1049
            $this->moveFile($file);
1050
        }
1051
1052
        $this->updateUsersEmails();
1053
    }
1054
1055
    /**
1056
     * @param string $file
1057
     */
1058
    private function importCoursesStatic($file, $moveFile, &$teacherBackup = [], &$groupBackup = [])
1059
    {
1060
        $this->importCourses($file, true, $teacherBackup, $groupBackup);
1061
    }
1062
1063
    /**
1064
     * @param string $file
1065
     * @param bool   $moveFile
1066
     *
1067
     * @return int
1068
     */
1069
    private function importCalendarStatic($file, $moveFile = true)
1070
    {
1071
        $this->fixCSVFile($file);
1072
1073
        $this->updateUsersEmails();
1074
        $data = Import::csvToArray($file);
1075
1076
        if (!empty($data)) {
1077
            $this->logger->addInfo(count($data).' records found.');
1078
            $eventsToCreate = [];
1079
            $errorFound = false;
1080
1081
            $courseExtraFieldValue = new ExtraFieldValue('course');
1082
1083
            foreach ($data as $row) {
1084
                $sessionId = null;
1085
                $externalSessionId = null;
1086
                if (isset($row['external_sessionID'])) {
1087
                    $externalSessionId = $row['external_sessionID'];
1088
                    $sessionId = SessionManager::getSessionIdFromOriginalId(
1089
                        $externalSessionId,
1090
                        $this->extraFieldIdNameList['session']
1091
                    );
1092
                }
1093
1094
                $courseCode = null;
1095
                if (isset($row['coursecode'])) {
1096
                    $courseCode = $row['coursecode'];
1097
                }
1098
                $courseInfo = api_get_course_info($courseCode);
1099
                $courseId = $courseInfo['real_id'] ?? 0;
1100
1101
                $item = $courseExtraFieldValue->get_values_by_handler_and_field_variable(
1102
                    $courseId,
1103
                    'disable_import_calendar'
1104
                );
1105
1106
                if (!empty($item) && isset($item['value']) && $item['value'] == 1) {
1107
                    $this->logger->addInfo(
1108
                        "Course '".$courseInfo['code']."' has 'disable_import_calendar' turn on. Skip"
1109
                    );
1110
                    $errorFound = true;
1111
                }
1112
1113
                if (empty($courseInfo)) {
1114
                    $this->logger->addInfo("Course '$courseCode' does not exists");
1115
                } else {
1116
                    if ($courseInfo['visibility'] == COURSE_VISIBILITY_HIDDEN) {
1117
                        $this->logger->addInfo("Course '".$courseInfo['code']."' has hidden visibility. Skip");
1118
                        $errorFound = true;
1119
                    }
1120
                }
1121
1122
                if (empty($sessionId)) {
1123
                    $this->logger->addInfo("external_sessionID: $externalSessionId does not exists.");
1124
                }
1125
                $teacherId = null;
1126
                $sessionInfo = [];
1127
                if (!empty($sessionId) && !empty($courseInfo)) {
1128
                    $sessionInfo = api_get_session_info($sessionId);
1129
                    $courseIncluded = SessionManager::relation_session_course_exist($sessionId, $courseId);
1130
1131
                    if ($courseIncluded == false) {
1132
                        $this->logger->addInfo(
1133
                            "Course '$courseCode' is not included in session: $sessionId"
1134
                        );
1135
                        $errorFound = true;
1136
                    } else {
1137
                        $teachers = CourseManager::get_coach_list_from_course_code($courseInfo['code'], $sessionId);
1138
1139
                        // Getting first teacher.
1140
                        if (!empty($teachers)) {
1141
                            $teacher = current($teachers);
1142
                            $teacherId = $teacher['user_id'];
1143
                        } else {
1144
                            $teacherId = $sessionInfo['id_coach'];
1145
                        }
1146
                    }
1147
                } else {
1148
                    $errorFound = true;
1149
                }
1150
1151
                if (empty($teacherId)) {
1152
                    $errorFound = true;
1153
                    $this->logger->addInfo(
1154
                        "No teacher found in course code : '$courseCode' and session: '$sessionId'"
1155
                    );
1156
                }
1157
1158
                $date = $row['date'];
1159
                $startTime = $row['time_start'];
1160
                $endTime = $row['time_end'];
1161
                $title = $row['title'];
1162
                $comment = $row['comment'] ?? '';
1163
                $color = $row['color'] ?? '';
1164
1165
                $startDateYear = substr($date, 0, 4);
1166
                $startDateMonth = substr($date, 4, 2);
1167
                $startDateDay = substr($date, 6, 8);
1168
1169
                $startDate = $startDateYear.'-'.$startDateMonth.'-'.$startDateDay.' '.$startTime.':00';
1170
                $endDate = $startDateYear.'-'.$startDateMonth.'-'.$startDateDay.' '.$endTime.':00';
1171
1172
                if (!api_is_valid_date($startDate) || !api_is_valid_date($endDate)) {
1173
                    $this->logger->addInfo("Verify your dates: '$startDate' : '$endDate' ");
1174
                    $errorFound = true;
1175
                }
1176
1177
                // Check session dates.
1178
                if ($sessionInfo && !empty($sessionInfo['access_start_date'])) {
1179
                    $date = new \DateTime($sessionInfo['access_start_date']);
1180
                    $interval = new \DateInterval('P7D');
1181
                    $date->sub($interval);
1182
                    if ($date->getTimestamp() > time()) {
1183
                        $this->logger->addInfo(
1184
                            "Calendar event # ".$row['external_calendar_itemID']."
1185
                            in session [$externalSessionId] was not added
1186
                            because the startdate is more than 7 days in the future: ".$sessionInfo['access_start_date']
1187
                        );
1188
                        $errorFound = true;
1189
                    }
1190
                }
1191
1192
                $sendAnnouncement = false;
1193
                if (isset($row['sendmail']) && 1 === (int) $row['sendmail']) {
1194
                    $sendAnnouncement = true;
1195
                }
1196
1197
                // New condition.
1198
                if ($errorFound == false) {
1199
                    $eventsToCreate[] = [
1200
                        'start' => $startDate,
1201
                        'end' => $endDate,
1202
                        'title' => $title,
1203
                        'sender_id' => $teacherId,
1204
                        'course_id' => $courseInfo['real_id'],
1205
                        'session_id' => $sessionId,
1206
                        'comment' => $comment,
1207
                        'color' => $color,
1208
                        'send_announcement' => $sendAnnouncement,
1209
                        $this->extraFieldIdNameList['calendar_event'] => $row['external_calendar_itemID'],
1210
                    ];
1211
                }
1212
                $errorFound = false;
1213
            }
1214
1215
            if (empty($eventsToCreate)) {
1216
                $this->logger->addInfo('No events to add');
1217
1218
                return 0;
1219
            }
1220
1221
            $extraFieldValue = new ExtraFieldValue('calendar_event');
1222
            $extraFieldName = $this->extraFieldIdNameList['calendar_event'];
1223
            $externalEventId = null;
1224
1225
            $extraField = new ExtraField('calendar_event');
1226
            $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable($extraFieldName);
1227
1228
            if (empty($extraFieldInfo)) {
1229
                $this->logger->addInfo(
1230
                    "No calendar event extra field created: $extraFieldName"
1231
                );
1232
1233
                return 0;
1234
            }
1235
1236
            $this->logger->addInfo('Ready to insert # '.count($eventsToCreate).' events');
1237
            $batchSize = $this->batchSize;
1238
            $counter = 1;
1239
            $em = Database::getManager();
1240
            $eventStartDateList = [];
1241
            $eventEndDateList = [];
1242
            $report = [
1243
                'mail_sent' => 0,
1244
                'mail_not_sent_announcement_exists' => 0,
1245
                'mail_not_sent_because_date' => 0,
1246
                'mail_not_sent_because_setting' => 0,
1247
            ];
1248
1249
            $eventsToCreateFinal = [];
1250
            foreach ($eventsToCreate as $event) {
1251
                $update = false;
1252
                $item = null;
1253
                if (!isset($event[$extraFieldName])) {
1254
                    $this->logger->addInfo('No external_calendar_itemID found. Skipping ...');
1255
                    continue;
1256
                } else {
1257
                    $externalEventId = $event[$extraFieldName];
1258
                    if (empty($externalEventId)) {
1259
                        $this->logger->addInfo('external_calendar_itemID was set but empty. Skipping ...');
1260
                        continue;
1261
                    }
1262
1263
                    $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
1264
                        $extraFieldName,
1265
                        $externalEventId,
1266
                        false,
1267
                        false,
1268
                        false
1269
                    );
1270
1271
                    if (!empty($item)) {
1272
                        $update = true;
1273
                    }
1274
                }
1275
1276
                $courseInfo = api_get_course_info_by_id($event['course_id']);
1277
                $event['course_info'] = $courseInfo;
1278
                $event['update'] = $update;
1279
                $event['item'] = $item;
1280
1281
                $calendarEvent = null;
1282
                /* Check if event changed of course code */
1283
                if (!empty($item) && isset($item['item_id']) && !empty($item['item_id'])) {
1284
                    /** @var CCalendarEvent $calendarEvent */
1285
                    $calendarEvent = $em->getRepository('ChamiloCourseBundle:CCalendarEvent')->find($item['item_id']);
1286
                }
1287
1288
                if ($calendarEvent) {
1289
                    $this->logger->addInfo('Calendar event found '.$item['item_id']);
1290
                    if ($calendarEvent->getCId() != $courseInfo['real_id']) {
1291
                        $this->logger->addInfo('Move from course #'.$calendarEvent->getCId().' to #'.$courseInfo['real_id']);
1292
                        // Seems that the course id changed in the csv
1293
                        $calendarEvent->setCId($courseInfo['real_id']);
1294
                        $em->persist($calendarEvent);
1295
                        $em->flush();
1296
1297
                        $criteria = [
1298
                            'tool' => 'calendar_event',
1299
                            'ref' => $item['item_id'],
1300
                        ];
1301
                        /** @var CItemProperty $itemProperty */
1302
                        $itemProperty = $em->getRepository('ChamiloCourseBundle:CItemProperty')->findOneBy($criteria);
1303
                        $courseEntity = $em->getRepository('ChamiloCoreBundle:Course')->find($courseInfo['real_id']);
1304
                        if ($itemProperty && $courseEntity) {
1305
                            $itemProperty->setCourse($courseEntity);
1306
                            $em->persist($itemProperty);
1307
                            $em->flush();
1308
                        }
1309
                    }
1310
1311
                    // Checking if session still exists
1312
                    $calendarSessionId = (int) $calendarEvent->getSessionId();
1313
                    if (!empty($calendarSessionId)) {
1314
                        $calendarSessionInfo = api_get_session_info($calendarSessionId);
1315
                        if (empty($calendarSessionInfo)) {
1316
                            $calendarId = (int) $calendarEvent->getIid();
1317
1318
                            // Delete calendar events because the session was deleted!
1319
                            $this->logger->addInfo(
1320
                                "Delete event # $calendarId because session # $calendarSessionId doesn't exist"
1321
                            );
1322
1323
                            $sql = "DELETE FROM c_calendar_event
1324
                                    WHERE iid = $calendarId AND session_id = $calendarSessionId";
1325
                            Database::query($sql);
1326
                            $this->logger->addInfo($sql);
1327
1328
                            $sql = "DELETE FROM c_item_property
1329
                                    WHERE
1330
                                        tool = 'calendar_event' AND
1331
                                        ref = $calendarSessionId AND
1332
                                        session_id = $calendarSessionId";
1333
                            Database::query($sql);
1334
                            $this->logger->addInfo($sql);
1335
                        }
1336
                    }
1337
                } else {
1338
                    $this->logger->addInfo('Calendar event not found '.$item['item_id']);
1339
                }
1340
1341
                $event['external_event_id'] = $externalEventId;
1342
                if (isset($eventStartDateList[$courseInfo['real_id']]) &&
1343
                    isset($eventStartDateList[$courseInfo['real_id']][$event['session_id']])
1344
                ) {
1345
                    $currentItemDate = api_strtotime($event['start']);
1346
                    $firstDate = $eventStartDateList[$courseInfo['real_id']][$event['session_id']];
1347
                    if ($currentItemDate < api_strtotime($firstDate)) {
1348
                        $eventStartDateList[$courseInfo['real_id']][$event['session_id']] = $event['start'];
1349
                        $eventEndDateList[$courseInfo['real_id']][$event['session_id']] = $event['end'];
1350
                    }
1351
                } else {
1352
                    // First time
1353
                    $eventStartDateList[$courseInfo['real_id']][$event['session_id']] = $event['start'];
1354
                    $eventEndDateList[$courseInfo['real_id']][$event['session_id']] = $event['end'];
1355
                }
1356
                $eventsToCreateFinal[] = $event;
1357
            }
1358
1359
            $eventAlreadySent = [];
1360
1361
            $tpl = new Template(null, false, false, false, false, false, false);
1362
1363
            foreach ($eventsToCreateFinal as $event) {
1364
                $courseInfo = $event['course_info'];
1365
                $item = $event['item'];
1366
                $update = $event['update'];
1367
                $externalEventId = $event['external_event_id'];
1368
                $info = 'Course: '.$courseInfo['real_id'].' ('.$courseInfo['code'].') - Session: '.$event['session_id'].' external event id: '.$externalEventId;
1369
1370
                $agenda = new Agenda(
1371
                    'course',
1372
                    $event['sender_id'],
1373
                    $courseInfo['real_id'],
1374
                    $event['session_id']
1375
                );
1376
                $agenda->set_course($courseInfo);
1377
                $agenda->setSessionId($event['session_id']);
1378
                $agenda->setSenderId($event['sender_id']);
1379
                $agenda->setIsAllowedToEdit(true);
1380
                $eventComment = $event['comment'];
1381
                $color = $event['color'];
1382
1383
                // To use the event comment you need
1384
                // ALTER TABLE c_calendar_event ADD COLUMN comment TEXT;
1385
                // add in configuration.php allow_agenda_event_comment = true
1386
                if (empty($courseInfo)) {
1387
                    $this->logger->addInfo(
1388
                        "No course found for event #$externalEventId Course #".$event['course_id']." Skipping ..."
1389
                    );
1390
                    continue;
1391
                }
1392
1393
                if (empty($event['sender_id'])) {
1394
                    $this->logger->addInfo(
1395
                        "No sender found for event #$externalEventId Send #".$event['sender_id']." Skipping ..."
1396
                    );
1397
                    continue;
1398
                }
1399
1400
                // Taking first element of course-session event
1401
                $alreadyAdded = false;
1402
                $firstDate = $eventStartDateList[$courseInfo['real_id']][$event['session_id']];
1403
                $firstEndDate = $eventEndDateList[$courseInfo['real_id']][$event['session_id']];
1404
1405
                if (isset($eventAlreadySent[$courseInfo['real_id']]) &&
1406
                    isset($eventAlreadySent[$courseInfo['real_id']][$event['session_id']])
1407
                ) {
1408
                    $alreadyAdded = true;
1409
                } else {
1410
                    $eventAlreadySent[$courseInfo['real_id']][$event['session_id']] = true;
1411
                }
1412
1413
                // Working days (Mon-Fri) see BT#12156#note-16
1414
                $days = 3;
1415
                $startDatePlusDays = api_strtotime("$days weekdays");
1416
1417
                /*
1418
                $timePart = date('H:i:s', api_strtotime('now'));
1419
                $datePart = date('Y-m-d', api_strtotime("$days weekdays"));
1420
                $startDatePlusDays = "$timePart $datePart";
1421
                */
1422
                $this->logger->addInfo(
1423
                    'startDatePlusDays: '.api_get_utc_datetime($startDatePlusDays).' - First date: '.$firstDate
1424
                );
1425
1426
                // Send.
1427
                $sendMail = false;
1428
                if ($startDatePlusDays > api_strtotime($firstDate)) {
1429
                    $sendMail = true;
1430
                }
1431
1432
                $allowAnnouncementSendEmail = false;
1433
                if ($event['send_announcement']) {
1434
                    $allowAnnouncementSendEmail = true;
1435
                }
1436
1437
                // Send announcement to users
1438
                if ($allowAnnouncementSendEmail) {
1439
                    if ($sendMail && $alreadyAdded == false) {
1440
                        $start = $firstDate;
1441
                        $end = $firstEndDate;
1442
1443
                        if (!empty($end) &&
1444
                            api_format_date($start, DATE_FORMAT_LONG) ==
1445
                            api_format_date($end, DATE_FORMAT_LONG)
1446
                        ) {
1447
                            $date = api_format_date($start, DATE_FORMAT_LONG).' ('.
1448
                                api_format_date($start, TIME_NO_SEC_FORMAT).' '.
1449
                                api_format_date($end, TIME_NO_SEC_FORMAT).')';
1450
                        } else {
1451
                            $date = api_format_date($start, DATE_TIME_FORMAT_LONG_24H).' - '.
1452
                                api_format_date($end, DATE_TIME_FORMAT_LONG_24H);
1453
                        }
1454
1455
                        $sessionName = '';
1456
                        $sessionId = isset($event['session_id']) && !empty($event['session_id']) ? $event['session_id'] : 0;
1457
                        if (!empty($sessionId)) {
1458
                            $sessionName = api_get_session_name($sessionId);
1459
                        }
1460
1461
                        $courseTitle = $courseInfo['title'];
1462
1463
                        // Get the value of the "careerid" extra field of this
1464
                        // session
1465
                        $sessionExtraFieldValue = new ExtraFieldValue('session');
1466
                        $externalCareerIdList = $sessionExtraFieldValue->get_values_by_handler_and_field_variable(
1467
                            $event['session_id'],
1468
                            'careerid'
1469
                        );
1470
                        $externalCareerIdList = $externalCareerIdList['value'];
1471
                        if (substr($externalCareerIdList, 0, 1) === '[') {
1472
                            $externalCareerIdList = substr($externalCareerIdList, 1, -1);
1473
                            $externalCareerIds = preg_split('/,/', $externalCareerIdList);
1474
                        } else {
1475
                            $externalCareerIds = [$externalCareerIdList];
1476
                        }
1477
1478
                        $careerExtraFieldValue = new ExtraFieldValue('career');
1479
                        $career = new Career();
1480
                        $careerName = '';
1481
1482
                        // Concat the names of each career linked to this session
1483
                        foreach ($externalCareerIds as $externalCareerId) {
1484
                            // Using the external_career_id field (from above),
1485
                            // find the career ID
1486
                            $careerValue = $careerExtraFieldValue->get_item_id_from_field_variable_and_field_value(
1487
                                'external_career_id',
1488
                                $externalCareerId
1489
                            );
1490
                            $career = $career->find($careerValue['item_id']);
1491
                            $careerName .= $career['name'].', ';
1492
                        }
1493
                        // Remove trailing comma
1494
                        $careerName = substr($careerName, 0, -2);
1495
                        $subject = sprintf(
1496
                            get_lang('WelcomeToPortalXInCourseSessionX'),
1497
                            api_get_setting('Institution'),
1498
                            $courseInfo['title']
1499
                        );
1500
1501
                        $tpl->assign('course_title', $courseTitle);
1502
                        $tpl->assign('career_name', $careerName);
1503
                        $tpl->assign('first_lesson', $date);
1504
                        $tpl->assign('location', $eventComment);
1505
                        $tpl->assign('session_name', $sessionName);
1506
1507
                        if (empty($sessionId)) {
1508
                            $teachersToString = CourseManager::getTeacherListFromCourseCodeToString(
1509
                                $courseInfo['code'],
1510
                                ','
1511
                            );
1512
                        } else {
1513
                            $teachersToString = SessionManager::getCoachesByCourseSessionToString(
1514
                                $sessionId,
1515
                                $courseInfo['real_id'],
1516
                                ','
1517
                            );
1518
                        }
1519
1520
                        $tpl->assign('teachers', $teachersToString);
1521
1522
                        $templateName = $tpl->get_template('mail/custom_calendar_welcome.tpl');
1523
                        $emailBody = $tpl->fetch($templateName);
1524
1525
                        $coaches = SessionManager::getCoachesByCourseSession(
1526
                            $event['session_id'],
1527
                            $courseInfo['real_id']
1528
                        );
1529
1530
                        // Search if an announcement exists:
1531
                        $announcementsWithTitleList = AnnouncementManager::getAnnouncementsByTitle(
1532
                            $subject,
1533
                            $courseInfo['real_id'],
1534
                            $event['session_id'],
1535
                            1
1536
                        );
1537
1538
                        if (count($announcementsWithTitleList) === 0) {
1539
                            $this->logger->addInfo(
1540
                                'Mail to be sent because start date: '.$event['start'].' and no announcement found.'
1541
                            );
1542
1543
                            $senderId = $this->defaultAdminId;
1544
                            if (!empty($coaches) && isset($coaches[0]) && !empty($coaches[0])) {
1545
                                $senderId = $coaches[0];
1546
                            }
1547
1548
                            $announcementId = AnnouncementManager::add_announcement(
1549
                                $courseInfo,
1550
                                $event['session_id'],
1551
                                $subject,
1552
                                $emailBody,
1553
                                [
1554
                                    'everyone',
1555
                                    'users' => $coaches,
1556
                                ],
1557
                                [],
1558
                                null,
1559
                                null,
1560
                                false,
1561
                                $senderId
1562
                            );
1563
1564
                            if ($announcementId) {
1565
                                $this->logger->addInfo("Announcement added: $announcementId in $info");
1566
                                $this->logger->addInfo("<<--SENDING MAIL Sender id: $senderId-->>");
1567
                                $report['mail_sent']++;
1568
                                AnnouncementManager::sendEmail(
1569
                                    $courseInfo,
1570
                                    $event['session_id'],
1571
                                    $announcementId,
1572
                                    false,
1573
                                    false,
1574
                                    $this->logger,
1575
                                    $senderId,
1576
                                    true
1577
                                );
1578
                            } else {
1579
                                $this->logger->addError(
1580
                                    "Error when trying to add announcement with title $subject here: $info and SenderId = $senderId"
1581
                                );
1582
                            }
1583
                        } else {
1584
                            $report['mail_not_sent_announcement_exists']++;
1585
                            $this->logger->addInfo(
1586
                                "Mail NOT sent. An announcement seems to be already saved in '$info'"
1587
                            );
1588
                        }
1589
                    } else {
1590
                        $this->logger->addInfo(
1591
                            "Send Mail: ".intval($sendMail).' - Already added: '.intval($alreadyAdded)
1592
                        );
1593
                        if ($sendMail == false) {
1594
                            $report['mail_not_sent_because_date']++;
1595
                        }
1596
                    }
1597
                } else {
1598
                    $this->logger->addInfo("Announcement not sent because config 'sendmail' in CSV");
1599
                    $report['mail_not_sent_because_setting']++;
1600
                }
1601
1602
                $content = '';
1603
                if ($update && isset($item['item_id'])) {
1604
                    $eventInfo = $agenda->get_event($item['item_id']);
1605
                    if (empty($eventInfo)) {
1606
                        // Means that agenda external id exists but the event doesn't exist
1607
                        $this->logger->addInfo("external event id exists: $externalEventId");
1608
                        $this->logger->addInfo("but Chamilo event doesn't exists: ".$item['item_id']);
1609
1610
                        $eventId = $agenda->addEvent(
1611
                            $event['start'],
1612
                            $event['end'],
1613
                            false,
1614
                            $event['title'],
1615
                            $content,
1616
                            ['everyone'], // $usersToSend
1617
                            false, //$addAsAnnouncement = false
1618
                            null, //  $parentEventId
1619
                            [], //$attachmentArray = array(),
1620
                            [], //$attachmentCommentList
1621
                            $eventComment,
1622
                            $color
1623
                        );
1624
1625
                        if (!empty($eventId)) {
1626
                            $this->logger->addInfo("Chamilo event created: ".$eventId);
1627
                            $extraFieldValueItem = $extraFieldValue->get_values_by_handler_and_field_id(
1628
                                $item['item_id'],
1629
                                $extraFieldInfo['id']
1630
                            );
1631
1632
                            if (!empty($extraFieldValueItem) && isset($extraFieldValueItem['id'])) {
1633
                                $params = [
1634
                                    'id' => $extraFieldValueItem['id'],
1635
                                    'item_id' => $eventId,
1636
                                ];
1637
                                $extraFieldValue->update($params);
1638
                                $this->logger->addInfo(
1639
                                    'Updating calendar extra field #'.$extraFieldValueItem['id'].'
1640
                                    new item_id: '.$eventId.' old item_id: '.$item['item_id']
1641
                                );
1642
                            }
1643
                        } else {
1644
                            $this->logger->addInfo("Error while creating event external id: $externalEventId");
1645
                        }
1646
                    } else {
1647
                        // The event already exists, just update
1648
                        $eventResult = $agenda->editEvent(
1649
                            $item['item_id'],
1650
                            $event['start'],
1651
                            $event['end'],
1652
                            false,
1653
                            $event['title'],
1654
                            $content,
1655
                            ['everyone'], // $usersToSend
1656
                            [], //$attachmentArray = array(),
1657
                            [], //$attachmentCommentList
1658
                            $eventComment,
1659
                            $color,
1660
                            false,
1661
                            false,
1662
                            $this->defaultAdminId
1663
                        );
1664
1665
                        if ($eventResult !== false) {
1666
                            $this->logger->addInfo(
1667
                                "Event updated #".$item['item_id']." External cal Id: (".$externalEventId.") $info"
1668
                            );
1669
                        } else {
1670
                            $this->logger->addInfo(
1671
                                "Error while updating event with external id: $externalEventId"
1672
                            );
1673
                        }
1674
                    }
1675
                } else {
1676
                    // New event. Create it.
1677
                    $eventId = $agenda->addEvent(
1678
                        $event['start'],
1679
                        $event['end'],
1680
                        false,
1681
                        $event['title'],
1682
                        $content,
1683
                        ['everyone'], // $usersToSend
1684
                        false, //$addAsAnnouncement = false
1685
                        null, //  $parentEventId
1686
                        [], //$attachmentArray = array(),
1687
                        [], //$attachmentCommentList
1688
                        $eventComment,
1689
                        $color
1690
                    );
1691
1692
                    if (!empty($eventId)) {
1693
                        $extraFieldValue->save(
1694
                            [
1695
                                'value' => $externalEventId,
1696
                                'field_id' => $extraFieldInfo['id'],
1697
                                'item_id' => $eventId,
1698
                            ]
1699
                        );
1700
                        $this->logger->addInfo(
1701
                            "Event added: #$eventId External cal id: (".$externalEventId.") $info"
1702
                        );
1703
                    } else {
1704
                        $this->logger->addInfo(
1705
                            "Error while creating event external id: $externalEventId"
1706
                        );
1707
                    }
1708
                }
1709
1710
                if (($counter % $batchSize) === 0) {
1711
                    $em->flush();
1712
                    $em->clear(); // Detaches all objects from Doctrine!
1713
                }
1714
                $counter++;
1715
            }
1716
1717
            $em->clear(); // Detaches all objects from Doctrine!
1718
            $this->logger->addInfo('------Summary------');
1719
            foreach ($report as $title => $count) {
1720
                $this->logger->addInfo("$title: $count");
1721
            }
1722
            $this->logger->addInfo('------End Summary------');
1723
        }
1724
1725
        if ($moveFile) {
1726
            $this->moveFile($file);
1727
        }
1728
    }
1729
1730
    /**
1731
     * @param string $file
1732
     * @param bool   $moveFile
1733
     * @param array  $teacherBackup
1734
     * @param array  $groupBackup
1735
     */
1736
    private function importCourses(
1737
        $file,
1738
        $moveFile = true,
1739
        &$teacherBackup = [],
1740
        &$groupBackup = []
1741
    ) {
1742
        $this->fixCSVFile($file);
1743
        $data = Import::csvToArray($file);
1744
1745
        if (!empty($data)) {
1746
            $this->logger->addInfo(count($data)." records found.");
1747
1748
            foreach ($data as $row) {
1749
                $row = $this->cleanCourseRow($row);
1750
1751
                $courseId = CourseManager::get_course_id_from_original_id(
1752
                    $row['extra_'.$this->extraFieldIdNameList['course']],
1753
                    $this->extraFieldIdNameList['course']
1754
                );
1755
1756
                $courseInfo = api_get_course_info_by_id($courseId);
1757
1758
                if (empty($courseInfo)) {
1759
                    // Create
1760
                    $params = [];
1761
                    $params['title'] = $row['title'];
1762
                    $params['exemplary_content'] = false;
1763
                    $params['wanted_code'] = $row['course_code'];
1764
                    $params['course_category'] = $row['course_category'];
1765
                    $params['course_language'] = $row['language'];
1766
                    $params['teachers'] = $row['teachers'];
1767
                    $params['visibility'] = $row['visibility'];
1768
1769
                    $courseInfo = CourseManager::create_course(
1770
                        $params,
1771
                        $this->defaultAdminId
1772
                    );
1773
1774
                    if (!empty($courseInfo)) {
1775
                        CourseManager::update_course_extra_field_value(
1776
                            $courseInfo['code'],
1777
                            'external_course_id',
1778
                            $row['extra_'.$this->extraFieldIdNameList['course']]
1779
                        );
1780
1781
                        $this->logger->addInfo("Courses - Course created ".$courseInfo['code']);
1782
                    } else {
1783
                        $this->logger->addError("Courses - Can't create course:".$row['title']);
1784
                    }
1785
                } else {
1786
                    // Update
1787
                    $params = [
1788
                        'title' => $row['title'],
1789
                        'category_code' => $row['course_category'],
1790
                        'visibility' => $row['visibility'],
1791
                    ];
1792
1793
                    $result = CourseManager::update_attributes(
1794
                        $courseInfo['real_id'],
1795
                        $params
1796
                    );
1797
1798
                    $addTeacherToSession = isset($courseInfo['add_teachers_to_sessions_courses']) && !empty($courseInfo['add_teachers_to_sessions_courses']) ? true : false;
1799
1800
                    $teachers = $row['teachers'];
1801
                    if (!is_array($teachers)) {
1802
                        $teachers = [$teachers];
1803
                    }
1804
1805
                    if ($addTeacherToSession) {
1806
                        $this->logger->addInfo("Add teacher to all course sessions");
1807
                        CourseManager::updateTeachers(
1808
                            $courseInfo,
1809
                            $row['teachers'],
1810
                            false,
1811
                            true,
1812
                            false,
1813
                            $teacherBackup,
1814
                            $this->logger
1815
                        );
1816
                    } else {
1817
                        CourseManager::updateTeachers(
1818
                            $courseInfo,
1819
                            $row['teachers'],
1820
                            true,
1821
                            false,
1822
                            false,
1823
                            $teacherBackup,
1824
                            $this->logger
1825
                        );
1826
                    }
1827
1828
                    foreach ($teachers as $teacherId) {
1829
                        if (isset($groupBackup['tutor'][$teacherId]) &&
1830
                            isset($groupBackup['tutor'][$teacherId][$courseInfo['code']])
1831
                        ) {
1832
                            foreach ($groupBackup['tutor'][$teacherId][$courseInfo['code']] as $data) {
1833
                                $groupInfo = GroupManager::get_group_properties($data['group_id']);
1834
                                GroupManager::subscribe_tutors(
1835
                                    [$teacherId],
1836
                                    $groupInfo,
1837
                                    $data['c_id']
1838
                                );
1839
                            }
1840
                        }
1841
1842
                        if (isset($groupBackup['user'][$teacherId]) &&
1843
                            isset($groupBackup['user'][$teacherId][$courseInfo['code']]) &&
1844
                            !empty($groupBackup['user'][$teacherId][$courseInfo['code']])
1845
                        ) {
1846
                            foreach ($groupBackup['user'][$teacherId][$courseInfo['code']] as $data) {
1847
                                $groupInfo = GroupManager::get_group_properties($data['group_id']);
1848
                                GroupManager::subscribe_users(
1849
                                    [$teacherId],
1850
                                    $groupInfo,
1851
                                    $data['c_id']
1852
                                );
1853
                            }
1854
                        }
1855
                    }
1856
1857
                    if ($result) {
1858
                        $this->logger->addInfo("Courses - Course updated ".$courseInfo['code']);
1859
                    } else {
1860
                        $this->logger->addError("Courses - Course NOT updated ".$courseInfo['code']);
1861
                    }
1862
                }
1863
            }
1864
        }
1865
1866
        if ($moveFile) {
1867
            $this->moveFile($file);
1868
        }
1869
    }
1870
1871
    /**
1872
     * Parse filename: encora_subsessionsextid-static_31082016.csv.
1873
     *
1874
     * @param string $file
1875
     */
1876
    private function importSubscribeUserToCourseSessionExtStatic($file, $moveFile = true)
1877
    {
1878
        $data = Import::csv_reader($file);
1879
        if (!empty($data)) {
1880
            $this->logger->addInfo(count($data)." records found.");
1881
            $userIdList = [];
1882
            foreach ($data as $row) {
1883
                $chamiloUserName = $row['UserName'];
1884
                $chamiloCourseCode = $row['CourseCode'];
1885
                $externalSessionId = $row['ExtSessionID'];
1886
                $status = $row['Status'];
1887
1888
                $chamiloSessionId = null;
1889
                if (!empty($externalSessionId)) {
1890
                    $chamiloSessionId = SessionManager::getSessionIdFromOriginalId(
1891
                        $externalSessionId,
1892
                        $this->extraFieldIdNameList['session']
1893
                    );
1894
                }
1895
1896
                $sessionInfo = api_get_session_info($chamiloSessionId);
1897
1898
                if (empty($sessionInfo)) {
1899
                    $this->logger->addError('Session does not exists: '.$chamiloSessionId);
1900
                    continue;
1901
                }
1902
1903
                $courseInfo = api_get_course_info($chamiloCourseCode);
1904
                if (empty($courseInfo)) {
1905
                    $this->logger->addError('Course does not exists: '.$courseInfo);
1906
                    continue;
1907
                }
1908
1909
                $userId = UserManager::get_user_id_from_username($chamiloUserName);
1910
1911
                if (empty($userId)) {
1912
                    $this->logger->addError('User does not exists: '.$chamiloUserName);
1913
                    continue;
1914
                }
1915
1916
                switch ($status) {
1917
                    case 'student':
1918
                        SessionManager::subscribe_users_to_session_course(
1919
                            [$userId],
1920
                            $chamiloSessionId,
1921
                            $courseInfo['code']
1922
                        );
1923
                        break;
1924
                    case 'teacher':
1925
                        SessionManager::set_coach_to_course_session(
1926
                            $userId,
1927
                            $chamiloSessionId,
1928
                            $courseInfo['code']
1929
                        );
1930
                        break;
1931
                    case 'drh':
1932
                        $removeAllSessionsFromUser = true;
1933
                        if (in_array($userId, $userIdList)) {
1934
                            $removeAllSessionsFromUser = false;
1935
                        } else {
1936
                            $userIdList[] = $userId;
1937
                        }
1938
1939
                        $userInfo = api_get_user_info($userId);
1940
                        SessionManager::subscribeSessionsToDrh(
1941
                            $userInfo,
1942
                            [$chamiloSessionId],
1943
                            false,
1944
                            $removeAllSessionsFromUser
1945
                        );
1946
                        break;
1947
                }
1948
1949
                $this->logger->addError(
1950
                    "User '$chamiloUserName' was added as '$status' to Session: #$chamiloSessionId - Course: ".$courseInfo['code']
1951
                );
1952
            }
1953
        }
1954
1955
        if ($moveFile) {
1956
            $this->moveFile($file);
1957
        }
1958
    }
1959
1960
    /**
1961
     * @param $file
1962
     * @param bool $moveFile
1963
     */
1964
    private function importUnsubSessionsExtIdStatic($file, $moveFile = true)
1965
    {
1966
        $data = Import::csv_reader($file);
1967
1968
        if (!empty($data)) {
1969
            $this->logger->addInfo(count($data)." records found.");
1970
            foreach ($data as $row) {
1971
                $chamiloUserName = $row['UserName'];
1972
                $chamiloCourseCode = $row['CourseCode'];
1973
                $externalSessionId = $row['ExtSessionID'];
1974
                $dateStop = $row['DateStop'];
1975
1976
                $chamiloSessionId = null;
1977
                if (!empty($externalSessionId)) {
1978
                    $chamiloSessionId = SessionManager::getSessionIdFromOriginalId(
1979
                        $externalSessionId,
1980
                        $this->extraFieldIdNameList['session']
1981
                    );
1982
                }
1983
1984
                $sessionInfo = api_get_session_info($chamiloSessionId);
1985
1986
                if (empty($sessionInfo)) {
1987
                    $this->logger->addError('Session does not exists: '.$chamiloSessionId);
1988
                    continue;
1989
                }
1990
1991
                $courseInfo = api_get_course_info($chamiloCourseCode);
1992
                if (empty($courseInfo)) {
1993
                    $this->logger->addError('Course does not exists: '.$courseInfo);
1994
                    continue;
1995
                }
1996
1997
                $userId = UserManager::get_user_id_from_username($chamiloUserName);
1998
1999
                if (empty($userId)) {
2000
                    $this->logger->addError('User does not exists: '.$chamiloUserName);
2001
                    continue;
2002
                }
2003
2004
                SessionManager::removeUsersFromCourseSession(
2005
                    [$userId],
2006
                    $chamiloSessionId,
2007
                    $courseInfo
2008
                );
2009
2010
                $this->logger->addError(
2011
                    "User '$chamiloUserName' was remove from Session: #$chamiloSessionId - Course: ".$courseInfo['code']
2012
                );
2013
            }
2014
        }
2015
2016
        if ($moveFile) {
2017
            $this->moveFile($file);
2018
        }
2019
    }
2020
2021
    /**
2022
     * @param string $file
2023
     */
2024
    private function importSessionsExtIdStatic($file, $moveFile = true)
2025
    {
2026
        $data = Import::csv_reader($file);
2027
2028
        if (!empty($data)) {
2029
            $this->logger->addInfo(count($data)." records found.");
2030
            foreach ($data as $row) {
2031
                $chamiloUserName = $row['UserName'];
2032
                $chamiloCourseCode = $row['CourseCode'];
2033
                $externalSessionId = $row['ExtSessionID'];
2034
                $type = $row['Type'];
2035
2036
                $chamiloSessionId = null;
2037
                if (!empty($externalSessionId)) {
2038
                    $chamiloSessionId = SessionManager::getSessionIdFromOriginalId(
2039
                        $externalSessionId,
2040
                        $this->extraFieldIdNameList['session']
2041
                    );
2042
                }
2043
2044
                $sessionInfo = api_get_session_info($chamiloSessionId);
2045
2046
                if (empty($sessionInfo)) {
2047
                    $this->logger->addError('Session does not exists: '.$chamiloSessionId);
2048
                    continue;
2049
                }
2050
2051
                $courseInfo = api_get_course_info($chamiloCourseCode);
2052
                if (empty($courseInfo)) {
2053
                    $this->logger->addError('Course does not exists: '.$courseInfo);
2054
                    continue;
2055
                }
2056
2057
                $userId = UserManager::get_user_id_from_username($chamiloUserName);
2058
2059
                if (empty($userId)) {
2060
                    $this->logger->addError('User does not exists: '.$chamiloUserName);
2061
                    continue;
2062
                }
2063
                switch ($type) {
2064
                    case 'student':
2065
                        SessionManager::subscribe_users_to_session_course(
2066
                            [$userId],
2067
                            $chamiloSessionId,
2068
                            $courseInfo['code'],
2069
                            null,
2070
                            false
2071
                        );
2072
                        break;
2073
                    case 'teacher':
2074
                        SessionManager::set_coach_to_course_session(
2075
                            $userId,
2076
                            $chamiloSessionId,
2077
                            $courseInfo['code']
2078
                        );
2079
                        break;
2080
                }
2081
2082
                $this->logger->addError(
2083
                    "User '$chamiloUserName' with status $type was added to session: #$chamiloSessionId - Course: ".$courseInfo['code']
2084
                );
2085
            }
2086
        }
2087
2088
        if ($moveFile) {
2089
            $this->moveFile($file);
2090
        }
2091
    }
2092
2093
    /**
2094
     * Updates the session synchronize with the csv file.
2095
     *
2096
     * @param bool   $moveFile
2097
     * @param string $file
2098
     */
2099
    private function importSessionsStatic($file, $moveFile = true)
2100
    {
2101
        $content = file($file);
2102
        $sessions = [];
2103
        $tag_names = [];
2104
2105
        foreach ($content as $key => $enreg) {
2106
            $enreg = explode(';', trim($enreg));
2107
            if ($key) {
2108
                foreach ($tag_names as $tag_key => $tag_name) {
2109
                    if (isset($enreg[$tag_key])) {
2110
                        $sessions[$key - 1][$tag_name] = $enreg[$tag_key];
2111
                    }
2112
                }
2113
            } else {
2114
                foreach ($enreg as $tag_name) {
2115
                    $tag_names[] = api_preg_replace(
2116
                        '/[^a-zA-Z0-9_\-]/',
2117
                        '',
2118
                        $tag_name
2119
                    );
2120
                }
2121
                if (!in_array('SessionName', $tag_names) ||
2122
                    !in_array('DateStart', $tag_names) || !in_array('DateEnd', $tag_names)
2123
                ) {
2124
                    $error_message = get_lang('NoNeededData');
2125
                    break;
2126
                }
2127
            }
2128
        }
2129
2130
        if (!empty($sessions)) {
2131
            // Looping the sessions.
2132
            foreach ($sessions as $session) {
2133
                if (!empty($session['SessionID'])) {
2134
                    $sessionId = SessionManager::getSessionIdFromOriginalId(
2135
                        $session['SessionID'],
2136
                        $this->extraFieldIdNameList['session']
2137
                    );
2138
2139
                    $coachUserName = isset($session['Coach']) ? $session['Coach'] : null;
2140
                    $categoryId = isset($session['category_id']) ? $session['category_id'] : null;
2141
2142
                    // 2014-06-30
2143
                    $dateStart = explode('/', $session['DateStart']);
2144
                    $dateEnd = explode('/', $session['DateEnd']);
2145
                    $visibility = $this->defaultSessionVisibility;
2146
2147
                    $coachId = null;
2148
                    if (!empty($coachUserName)) {
2149
                        $coachInfo = api_get_user_info_from_username($coachUserName);
2150
                        $coachId = $coachInfo['user_id'];
2151
                    }
2152
2153
                    $dateStart = $dateStart[0].'-'.$dateStart[1].'-'.$dateStart[2].' 00:00:00';
2154
                    $dateEnd = $dateEnd[0].'-'.$dateEnd[1].'-'.$dateEnd[2].' 23:59:59';
2155
2156
                    $date = new \DateTime($dateStart);
2157
                    $interval = new DateInterval('P'.$this->daysCoachAccessBeforeBeginning.'D');
2158
                    $date->sub($interval);
2159
                    $coachBefore = $date->format('Y-m-d h:i');
2160
2161
                    $date = new \DateTime($dateEnd);
2162
                    $interval = new DateInterval('P'.$this->daysCoachAccessAfterBeginning.'D');
2163
                    $date->add($interval);
2164
                    $coachAfter = $date->format('Y-m-d h:i');
2165
2166
                    /*$dateStart = api_get_utc_datetime($dateStart);
2167
                    $dateEnd = api_get_utc_datetime($dateEnd);
2168
                    $coachBefore = api_get_utc_datetime($coachBefore);
2169
                    $coachAfter = api_get_utc_datetime($coachAfter);*/
2170
2171
                    if (empty($sessionId)) {
2172
                        $result = SessionManager::create_session(
2173
                            $session['SessionName'],
2174
                            $dateStart,
2175
                            $dateEnd,
2176
                            $dateStart,
2177
                            $dateEnd,
2178
                            $coachBefore,
2179
                            $coachAfter,
2180
                            $coachId,
2181
                            $categoryId,
2182
                            $visibility
2183
                        );
2184
2185
                        if (is_numeric($result)) {
2186
                            $sessionId = $result;
2187
                            $this->logger->addInfo("Session #$sessionId created: ".$session['SessionName']);
2188
                            SessionManager::update_session_extra_field_value(
2189
                                $sessionId,
2190
                                $this->extraFieldIdNameList['session'],
2191
                                $session['SessionID']
2192
                            );
2193
                        } else {
2194
                            $this->logger->addInfo("Failed creating session: ".$session['SessionName']);
2195
                        }
2196
                    } else {
2197
                        $sessionInfo = api_get_session_info($sessionId);
2198
                        $accessBefore = null;
2199
                        $accessAfter = null;
2200
2201
                        if (empty($sessionInfo['nb_days_access_before_beginning']) ||
2202
                            (!empty($sessionInfo['nb_days_access_before_beginning']) &&
2203
                                $sessionInfo['nb_days_access_before_beginning'] < $this->daysCoachAccessBeforeBeginning)
2204
                        ) {
2205
                            $accessBefore = $coachBefore;
2206
                        }
2207
2208
                        $accessAfter = null;
2209
                        if (empty($sessionInfo['nb_days_access_after_end']) ||
2210
                            (!empty($sessionInfo['nb_days_access_after_end']) &&
2211
                                $sessionInfo['nb_days_access_after_end'] < $this->daysCoachAccessAfterBeginning)
2212
                        ) {
2213
                            $accessAfter = $coachAfter;
2214
                        }
2215
2216
                        $showDescription = isset($sessionInfo['show_description']) ? $sessionInfo['show_description'] : 1;
2217
2218
                        $result = SessionManager::edit_session(
2219
                            $sessionId,
2220
                            $session['SessionName'],
2221
                            $dateStart,
2222
                            $dateEnd,
2223
                            $dateStart,
2224
                            $dateEnd,
2225
                            $accessBefore,
2226
                            $accessAfter,
2227
                            $coachId,
2228
                            $categoryId,
2229
                            $visibility,
2230
                            null, //$description = null,
2231
                            $showDescription
2232
                        );
2233
2234
                        if (is_numeric($result)) {
2235
                            $this->logger->addInfo("Session #$sessionId updated: ".$session['SessionName']);
2236
                            $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
2237
                            $params = [
2238
                                'description' => $session['SessionDescription'],
2239
                            ];
2240
                            Database::update(
2241
                                $tbl_session,
2242
                                $params,
2243
                                ['id = ?' => $sessionId]
2244
                            );
2245
                        }
2246
                    }
2247
2248
                    if (!empty($sessionId)) {
2249
                        // Courses
2250
                        $courses = explode('|', $session['Courses']);
2251
                        $courseList = [];
2252
                        $courseListWithCoach = [];
2253
                        foreach ($courses as $course) {
2254
                            $courseArray = bracketsToArray($course);
2255
                            $courseCode = $courseArray[0];
2256
                            if (CourseManager::course_exists($courseCode)) {
2257
                                $courseInfo = api_get_course_info($courseCode);
2258
                                $courseList[] = $courseInfo['real_id'];
2259
                                // Extracting course coaches
2260
                                $courseCoaches = isset($courseArray[1]) ? $courseArray[1] : null;
2261
                                $courseCoaches = explode(',', $courseCoaches);
2262
2263
                                // Extracting students
2264
                                $courseUsers = isset($courseArray[2]) ? $courseArray[2] : null;
2265
                                $courseUsers = explode(',', $courseUsers);
2266
2267
                                $courseListWithCoach[] = [
2268
                                    'course_info' => $courseInfo,
2269
                                    'coaches' => $courseCoaches,
2270
                                    'course_users' => $courseUsers,
2271
                                ];
2272
                            }
2273
                        }
2274
2275
                        SessionManager::add_courses_to_session(
2276
                            $sessionId,
2277
                            $courseList,
2278
                            true
2279
                        );
2280
2281
                        $this->logger->addInfo("Session #$sessionId: Courses added: '".implode(', ', $courseList)."'");
2282
2283
                        if (empty($courseListWithCoach)) {
2284
                            $this->logger->addInfo("No users/coaches to update");
2285
                            continue;
2286
                        }
2287
2288
                        foreach ($courseListWithCoach as $courseData) {
2289
                            $courseInfo = $courseData['course_info'];
2290
                            $courseCode = $courseInfo['code'];
2291
                            $courseId = $courseInfo['real_id'];
2292
                            $courseCoaches = $courseData['coaches'];
2293
                            $courseUsers = $courseData['course_users'];
2294
2295
                            // Coaches
2296
                            if (!empty($courseCoaches)) {
2297
                                $coachList = [];
2298
                                foreach ($courseCoaches as $courseCoach) {
2299
                                    $courseCoachId = UserManager::get_user_id_from_username(
2300
                                        $courseCoach
2301
                                    );
2302
                                    if ($courseCoachId !== false) {
2303
                                        // Just insert new coaches
2304
                                        $coachList[] = $courseCoachId;
2305
                                    }
2306
                                }
2307
2308
                                $this->logger->addInfo("Session #$sessionId: course '$courseCode' coaches added: '".implode(', ', $coachList)."'");
2309
2310
                                SessionManager::updateCoaches(
2311
                                    $sessionId,
2312
                                    $courseId,
2313
                                    $coachList,
2314
                                    true
2315
                                );
2316
                            } else {
2317
                                $this->logger->addInfo("No coaches added");
2318
                            }
2319
2320
                            // Students
2321
                            if (!empty($courseUsers)) {
2322
                                $userList = [];
2323
                                foreach ($courseUsers as $username) {
2324
                                    $userInfo = api_get_user_info_from_username(trim($username));
2325
                                    if (!empty($userInfo)) {
2326
                                        $userList[] = $userInfo['user_id'];
2327
                                    }
2328
                                }
2329
2330
                                $this->logger->addInfo("Session #$sessionId: course '$courseCode': Students added '".implode(', ', $userList)."'");
2331
                                SessionManager::subscribe_users_to_session_course(
2332
                                    $userList,
2333
                                    $sessionId,
2334
                                    $courseCode,
2335
                                    SESSION_VISIBLE_READ_ONLY,
2336
                                    true
2337
                                );
2338
                            } else {
2339
                                $this->logger->addInfo("No users to register.");
2340
                            }
2341
                        }
2342
                    } else {
2343
                        $this->logger->addInfo(
2344
                            'SessionID not found in system.'
2345
                        );
2346
                    }
2347
                } else {
2348
                    $this->logger->addInfo('SessionID does not exists');
2349
                }
2350
            }
2351
        } else {
2352
            $this->logger->addInfo($error_message);
2353
        }
2354
2355
        if ($moveFile) {
2356
            $this->moveFile($file);
2357
        }
2358
    }
2359
2360
    /**
2361
     * @param $file
2362
     * @param bool  $moveFile
2363
     * @param array $teacherBackup
2364
     * @param array $groupBackup
2365
     */
2366
    private function importOpenSessions(
2367
        $file,
2368
        $moveFile = true,
2369
        &$teacherBackup = [],
2370
        &$groupBackup = []
2371
    ) {
2372
        $this->importSessions($file, $moveFile, $teacherBackup, $groupBackup);
2373
    }
2374
2375
    private function importSessionsUsersCareers(
2376
        $file,
2377
        $moveFile = false,
2378
        &$teacherBackup = [],
2379
        &$groupBackup = []
2380
    ) {
2381
        $data = Import::csvToArray($file);
2382
        if (!empty($data)) {
2383
            $extraFieldValueCareer = new ExtraFieldValue('career');
2384
            $sessionExtraFieldValue = new ExtraFieldValue('session');
2385
            $career = new Career();
2386
2387
            $this->logger->addInfo(count($data)." records found.");
2388
            foreach ($data as $row) {
2389
                $users = $row['Users'];
2390
                if (empty($users)) {
2391
                    $this->logger->addError('No users found');
2392
                    continue;
2393
                }
2394
2395
                $users = explode('|', $users);
2396
                $careerList = str_replace(['[', ']'], '', $row['extra_careerid']);
2397
                $careerList = explode(',', $careerList);
2398
2399
                $finalCareerIdList = [];
2400
                foreach ($careerList as $careerId) {
2401
                    $realCareerIdList = $extraFieldValueCareer->get_item_id_from_field_variable_and_field_value(
2402
                        'external_career_id',
2403
                        $careerId
2404
                    );
2405
                    if (isset($realCareerIdList['item_id'])) {
2406
                        $finalCareerIdList[] = $realCareerIdList['item_id'];
2407
                    }
2408
                }
2409
2410
                if (empty($finalCareerIdList)) {
2411
                    $this->logger->addError('Careers not found: '.print_r($finalCareerIdList, 1));
2412
                    continue;
2413
                }
2414
2415
                $chamiloSessionId = $row['SessionID'];
2416
                $sessionInfo = api_get_session_info($chamiloSessionId);
2417
2418
                if (empty($sessionInfo)) {
2419
                    $this->logger->addError('Session does not exists: '.$chamiloSessionId);
2420
                    continue;
2421
                }
2422
2423
                $sessionId = $sessionInfo['id'];
2424
2425
                // Add career to session.
2426
                $externalCareerIdList = $sessionExtraFieldValue->get_values_by_handler_and_field_variable(
2427
                    $sessionId,
2428
                    'careerid'
2429
                );
2430
2431
                if (empty($externalCareerIdList) ||
2432
                    (isset($externalCareerIdList['value']) && empty($externalCareerIdList['value']))
2433
                ) {
2434
                    $careerItem = '['.implode(',', $externalCareerIdList).']';
2435
                    $params = ['item_id' => $sessionId, 'extra_careerid' => $careerItem];
2436
                    $sessionExtraFieldValue->saveFieldValues($params, true);
2437
                } else {
2438
                    /*foreach ($finalCareerIdList as $careerId) {
2439
                        if (empty($externalCareerIdList)) {
2440
                            $params = ['item_id' => $sessionId, 'extra_careerid' => $careerId];
2441
                            $sessionExtraFieldValue->saveFieldValues($params, true);
2442
                        }
2443
                    }*/
2444
                }
2445
2446
                // Add career to users.
2447
                foreach ($users as $username) {
2448
                    $userInfo = api_get_user_info_from_username($username);
2449
                    if (empty($userInfo)) {
2450
                        $this->logger->addError('username not found: '.$username);
2451
                        continue;
2452
                    }
2453
2454
                    foreach ($finalCareerIdList as $careerId) {
2455
                        UserManager::addUserCareer($userInfo['id'], $careerId);
2456
                    }
2457
                }
2458
            }
2459
        }
2460
    }
2461
2462
    /**
2463
     * @param string $file
2464
     * @param bool   $moveFile
2465
     * @param array  $teacherBackup
2466
     * @param array  $groupBackup
2467
     */
2468
    private function importSessions(
2469
        $file,
2470
        $moveFile = true,
2471
        &$teacherBackup = [],
2472
        &$groupBackup = []
2473
    ) {
2474
        $avoid = null;
2475
        if (isset($this->conditions['importSessions']) &&
2476
            isset($this->conditions['importSessions']['update'])
2477
        ) {
2478
            $avoid = $this->conditions['importSessions']['update'];
2479
        }
2480
        $result = SessionManager::importCSV(
2481
            $file,
2482
            true,
2483
            $this->defaultAdminId,
2484
            $this->logger,
2485
            [
2486
                'SessionID' => 'extra_'.$this->extraFieldIdNameList['session'],
2487
                'CareerId' => 'extra_'.$this->extraFieldIdNameList['session_career'],
2488
            ],
2489
            $this->extraFieldIdNameList['session'],
2490
            $this->daysCoachAccessBeforeBeginning,
2491
            $this->daysCoachAccessAfterBeginning,
2492
            $this->defaultSessionVisibility,
2493
            $avoid,
2494
            false, // deleteUsersNotInList
2495
            false, // updateCourseCoaches
2496
            true, // sessionWithCoursesModifier
2497
            true, //$addOriginalCourseTeachersAsCourseSessionCoaches
2498
            false, //$removeAllTeachersFromCourse
2499
            1, // $showDescription,
2500
            $teacherBackup,
2501
            $groupBackup
2502
        );
2503
2504
        if (!empty($result['error_message'])) {
2505
            $this->logger->addError($result['error_message']);
2506
        }
2507
        $this->logger->addInfo("Sessions - Sessions parsed: ".$result['session_counter']);
2508
2509
        if ($moveFile) {
2510
            $this->moveFile($file);
2511
        }
2512
    }
2513
2514
    /**
2515
     * @param string $file
2516
     * @param bool   $moveFile
2517
     */
2518
    private function importSubscribeStatic($file, $moveFile = true)
2519
    {
2520
        $data = Import::csv_reader($file);
2521
2522
        if (!empty($data)) {
2523
            $this->logger->addInfo(count($data)." records found.");
2524
            foreach ($data as $row) {
2525
                $chamiloUserName = $row['UserName'];
2526
                $chamiloCourseCode = $row['CourseCode'];
2527
                $chamiloSessionId = $row['SessionID'];
2528
                $type = $row['Type'];
2529
2530
                $sessionInfo = api_get_session_info($chamiloSessionId);
2531
2532
                if (empty($sessionInfo)) {
2533
                    $this->logger->addError('Session does not exists: '.$chamiloSessionId);
2534
                    continue;
2535
                }
2536
2537
                $courseInfo = api_get_course_info($chamiloCourseCode);
2538
                if (empty($courseInfo)) {
2539
                    $this->logger->addError('Course does not exists: '.$courseInfo);
2540
                    continue;
2541
                }
2542
2543
                $userId = UserManager::get_user_id_from_username($chamiloUserName);
2544
2545
                if (empty($userId)) {
2546
                    $this->logger->addError('User does not exists: '.$chamiloUserName);
2547
                    continue;
2548
                }
2549
2550
                switch ($type) {
2551
                    case 'student':
2552
                        SessionManager::subscribe_users_to_session_course(
2553
                            [$userId],
2554
                            $chamiloSessionId,
2555
                            $courseInfo['code'],
2556
                            null,
2557
                            false
2558
                        );
2559
                        break;
2560
                    case 'teacher':
2561
                        SessionManager::set_coach_to_course_session(
2562
                            $userId,
2563
                            $chamiloSessionId,
2564
                            $courseInfo['real_id']
2565
                        );
2566
                        break;
2567
                }
2568
2569
                $this->logger->addError(
2570
                    "User '$chamiloUserName' with status $type was added to session: #$chamiloSessionId - Course: ".$courseInfo['code']
2571
                );
2572
            }
2573
        }
2574
2575
        if ($moveFile) {
2576
            $this->moveFile($file);
2577
        }
2578
    }
2579
2580
    /**
2581
     * @param $file
2582
     * @param bool $moveFile
2583
     */
2584
    private function importSubscribeUserToCourse($file, $moveFile = false, &$teacherBackup = [])
2585
    {
2586
        $data = Import::csv_reader($file);
2587
2588
        if (!empty($data)) {
2589
            $this->logger->addInfo(count($data)." records found.");
2590
            foreach ($data as $row) {
2591
                $chamiloUserName = $row['UserName'];
2592
                $chamiloCourseCode = $row['CourseCode'];
2593
                $status = $row['Status'];
2594
2595
                $courseInfo = api_get_course_info($chamiloCourseCode);
2596
2597
                if (empty($courseInfo)) {
2598
                    $this->logger->addError(
2599
                        'Course does not exists: '.$chamiloCourseCode
2600
                    );
2601
                    continue;
2602
                }
2603
2604
                $userId = UserManager::get_user_id_from_username(
2605
                    $chamiloUserName
2606
                );
2607
2608
                if (empty($userId)) {
2609
                    $this->logger->addError(
2610
                        'User does not exists: '.$chamiloUserName
2611
                    );
2612
                    continue;
2613
                }
2614
2615
                $userCourseCategory = '';
2616
                if (isset($teacherBackup[$userId]) &&
2617
                    isset($teacherBackup[$userId][$courseInfo['code']])
2618
                ) {
2619
                    $courseUserData = $teacherBackup[$userId][$courseInfo['code']];
2620
                    $userCourseCategory = $courseUserData['user_course_cat'];
2621
                }
2622
2623
                $result = CourseManager::subscribeUser(
2624
                    $userId,
2625
                    $courseInfo['code'],
2626
                    $status,
2627
                    0,
2628
                    $userCourseCategory
2629
                );
2630
2631
                if ($result) {
2632
                    $this->logger->addInfo(
2633
                        "User $userId added to course ".$courseInfo['code']." with status '$status' with course category: '$userCourseCategory'"
2634
                    );
2635
                } else {
2636
                    $this->logger->addInfo(
2637
                        "User $userId was NOT ADDED to course ".$courseInfo['code']." with status '$status' with course category: '$userCourseCategory'"
2638
                    );
2639
                }
2640
            }
2641
        }
2642
2643
        if ($moveFile) {
2644
            $this->moveFile($file);
2645
        }
2646
    }
2647
2648
    /**
2649
     * 23/4/2017 to datetime.
2650
     *
2651
     * @param $string
2652
     *
2653
     * @return mixed
2654
     */
2655
    private function createDateTime($string)
2656
    {
2657
        if (empty($string)) {
2658
            return null;
2659
        }
2660
2661
        $date = DateTime::createFromFormat('j/m/Y', $string);
2662
        if ($date) {
2663
            return $date;
2664
        }
2665
2666
        return null;
2667
    }
2668
2669
    /**
2670
     * @param $file
2671
     * @param bool  $moveFile
2672
     * @param array $teacherBackup
2673
     * @param array $groupBackup
2674
     *
2675
     * @return bool
2676
     */
2677
    private function importCareers(
2678
        $file,
2679
        $moveFile = false,
2680
        &$teacherBackup = [],
2681
        &$groupBackup = []
2682
    ) {
2683
        $data = Import::csv_reader($file);
2684
2685
        if (!empty($data)) {
2686
            $this->logger->addInfo(count($data).' records found.');
2687
            $extraFieldValue = new ExtraFieldValue('career');
2688
            $extraFieldName = $this->extraFieldIdNameList['career'];
2689
            $externalEventId = null;
2690
2691
            $extraField = new ExtraField('career');
2692
            $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable($extraFieldName);
2693
2694
            if (empty($extraFieldInfo)) {
2695
                $this->logger->addInfo("Extra field doesn't exists: $extraFieldName");
2696
2697
                return false;
2698
            }
2699
2700
            foreach ($data as $row) {
2701
                foreach ($row as $key => $value) {
2702
                    $key = (string) trim($key);
2703
                    // Remove utf8 bom
2704
                    $key = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $key);
2705
                    $row[$key] = $value;
2706
                }
2707
2708
                $itemId = $row['CareerId'];
2709
                $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
2710
                    $extraFieldName,
2711
                    $itemId,
2712
                    false,
2713
                    false,
2714
                    false
2715
                );
2716
2717
                $career = new Career();
2718
                if (empty($item)) {
2719
                    $params = [
2720
                        'status' => 1,
2721
                        'name' => $row['CareerName'],
2722
                    ];
2723
                    $careerId = $career->save($params);
2724
                    if ($careerId) {
2725
                        $this->logger->addInfo('Career saved: '.print_r($params, 1));
2726
                        $params = [
2727
                            'item_id' => $careerId,
2728
                            'extra_'.$extraFieldName => $itemId,
2729
                        ];
2730
                        $links = isset($row['HLinks']) ? $row['HLinks'] : [];
2731
                        if (!empty($links)) {
2732
                            $extraFieldUrlName = $this->extraFieldIdNameList['career_urls'];
2733
                            $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
2734
                                $extraFieldUrlName
2735
                            );
2736
                            if (!empty($extraFieldInfo)) {
2737
                                $params['extra_'.$extraFieldUrlName] = $links;
2738
                            }
2739
                        }
2740
                        $extraFieldValue->saveFieldValues($params);
2741
                    }
2742
                } else {
2743
                    if (isset($item['item_id'])) {
2744
                        $params = [
2745
                            'id' => $item['item_id'],
2746
                            'name' => $row['CareerName'],
2747
                        ];
2748
                        $career->update($params);
2749
                        $this->logger->addInfo('Career updated: '.print_r($params, 1));
2750
                        $links = isset($row['HLinks']) ? $row['HLinks'] : [];
2751
2752
                        if (!empty($links)) {
2753
                            $extraFieldUrlName = $this->extraFieldIdNameList['career_urls'];
2754
                            $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable(
2755
                                $extraFieldUrlName
2756
                            );
2757
                            if (!empty($extraFieldInfo)) {
2758
                                $params = [
2759
                                    'item_id' => $item['item_id'],
2760
                                    'extra_'.$extraFieldName => $itemId,
2761
                                    'extra_'.$extraFieldUrlName => $links,
2762
                                ];
2763
                                $extraFieldValue->saveFieldValues($params);
2764
                            }
2765
                        }
2766
                    }
2767
                }
2768
            }
2769
        }
2770
    }
2771
2772
    /**
2773
     * @param $file
2774
     * @param bool  $moveFile
2775
     * @param array $teacherBackup
2776
     * @param array $groupBackup
2777
     */
2778
    private function importCareersResults(
2779
        $file,
2780
        $moveFile = false,
2781
        &$teacherBackup = [],
2782
        &$groupBackup = []
2783
    ) {
2784
        $data = Import::csv_reader($file);
2785
2786
        $userTable = Database::get_main_table(TABLE_MAIN_USER);
2787
        $careerIdList = [];
2788
        $userIdList = [];
2789
        if (!empty($data)) {
2790
            $totalCount = count($data);
2791
            $this->logger->addInfo($totalCount.' records found.');
2792
2793
            $extraFieldValue = new ExtraFieldValue('career');
2794
            $extraFieldName = $this->extraFieldIdNameList['career'];
2795
            $rowCounter = 0;
2796
            foreach ($data as $row) {
2797
                $this->logger->addInfo("---------- Row: # $rowCounter");
2798
                $rowCounter++;
2799
                if (empty($row)) {
2800
                    continue;
2801
                }
2802
2803
                foreach ($row as $key => $value) {
2804
                    $key = (string) trim($key);
2805
                    // Remove utf8 bom
2806
                    $key = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $key);
2807
                    $row[$key] = $value;
2808
                }
2809
2810
                $rowStudentId = $row['StudentId'];
2811
2812
                if (isset($userIdList[$rowStudentId])) {
2813
                    $studentId = $userIdList[$rowStudentId];
2814
                } else {
2815
                    $studentId = UserManager::get_user_id_from_original_id(
2816
                        $rowStudentId,
2817
                        $this->extraFieldIdNameList['user']
2818
                    );
2819
2820
                    $userIdList[$rowStudentId] = $studentId;
2821
                }
2822
2823
                //$studentInfo = api_get_user_info($studentId);
2824
2825
                /*$sql = "SELECT id FROM $userTable WHERE id = $studentId";
2826
                $result = Database::query($sql);
2827
                if (empty(Database::num_rows($result))) {
2828
                    $this->logger->addInfo("Student chamilo id not found: $studentId row data StudentId: ".$row['StudentId']);
2829
                    continue;
2830
                }*/
2831
2832
                $careerId = $row['CareerId'];
2833
2834
                //$careerChamiloId = 0;
2835
                if (isset($careerIdList[$careerId])) {
2836
                    $careerChamiloId = $careerIdList[$careerId];
2837
                } else {
2838
                    $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
2839
                        $extraFieldName,
2840
                        $careerId
2841
                    );
2842
2843
                    if (empty($item)) {
2844
                        //$this->logger->addInfo("Career not found: $careerId case 1");
2845
                        $careerIdList[$careerId] = 0;
2846
                        continue;
2847
                    } else {
2848
                        if (isset($item['item_id'])) {
2849
                            $careerChamiloId = $item['item_id'];
2850
                            $careerIdList[$careerId] = $careerChamiloId;
2851
                        } else {
2852
                            $careerIdList[$careerId] = 0;
2853
                            //$this->logger->addInfo("Career not found: $careerId case 2");
2854
                            continue;
2855
                        }
2856
                    }
2857
                }
2858
2859
                if (empty($careerChamiloId)) {
2860
                    $this->logger->addInfo("Career not found: $careerId ");
2861
                    continue;
2862
                }
2863
2864
                $userCareerData = UserManager::getUserCareer($studentId, $careerChamiloId);
2865
2866
                if (empty($userCareerData)) {
2867
                    $this->logger->addInfo(
2868
                        "User chamilo id # $studentId (".$row['StudentId'].") has no career #$careerChamiloId (ext #$careerId)"
2869
                    );
2870
                    continue;
2871
                }
2872
2873
                $extraData = isset($userCareerData['extra_data']) && !empty($userCareerData['extra_data']) ? unserialize($userCareerData['extra_data']) : [];
2874
2875
                //$teacherInfo = api_get_user_info_from_username($row['TeacherUsername']);
2876
                $sql = "SELECT firstname, lastname FROM $userTable
2877
                        WHERE username='".Database::escape_string($row['TeacherUsername'])."'";
2878
                $result = Database::query($sql);
2879
2880
                $teacherName = $row['TeacherUsername'];
2881
                if (Database::num_rows($result)) {
2882
                    $teacherInfo = Database::fetch_array($result);
2883
                    $teacherName = $teacherInfo['firstname'].' '.$teacherInfo['lastname'];
2884
                }
2885
2886
                $extraData[$row['CourseId']][$row['ResultId']] = [
2887
                    'Description' => $row['Description'],
2888
                    'Period' => $row['Period'],
2889
                    'TeacherText' => $row['TeacherText'],
2890
                    'TeacherUsername' => $teacherName,
2891
                    'ScoreText' => $row['ScoreText'],
2892
                    'ScoreValue' => $row['ScoreValue'],
2893
                    'Info' => $row['Info'],
2894
                    'BgColor' => $row['BgColor'],
2895
                    'Color' => $row['Color'],
2896
                    'BorderColor' => $row['BorderColor'],
2897
                    'Icon' => $row['Icon'],
2898
                    'IconColor' => $row['IconColor'],
2899
                ];
2900
                $serializedValue = serialize($extraData);
2901
2902
                UserManager::updateUserCareer($userCareerData['id'], $serializedValue);
2903
2904
                $this->logger->addInfo(
2905
                    "Saving graph for user chamilo # $studentId (".$row['StudentId'].") with career #$careerChamiloId (ext #$careerId)"
2906
                );
2907
            }
2908
        }
2909
    }
2910
2911
    /**
2912
     * @param $file
2913
     * @param bool  $moveFile
2914
     * @param array $teacherBackup
2915
     * @param array $groupBackup
2916
     */
2917
    private function importCareersDiagram(
2918
        $file,
2919
        $moveFile = false,
2920
        &$teacherBackup = [],
2921
        &$groupBackup = []
2922
    ) {
2923
        $data = Import::csv_reader($file);
2924
2925
        $extraFieldValue = new ExtraFieldValue('career');
2926
        $extraFieldName = $this->extraFieldIdNameList['career'];
2927
        $externalEventId = null;
2928
2929
        $extraField = new ExtraField('career');
2930
        $extraFieldInfo = $extraField->get_handler_field_info_by_field_variable($extraFieldName);
2931
2932
        $careerDiagramExtraFieldName = $this->extraFieldIdNameList['career_diagram'];
2933
        $extraFieldDiagramInfo = $extraField->get_handler_field_info_by_field_variable(
2934
            $careerDiagramExtraFieldName
2935
        );
2936
2937
        if (empty($extraFieldInfo) || empty($extraFieldDiagramInfo)) {
2938
            return false;
2939
        }
2940
2941
        if (!empty($data)) {
2942
            $this->logger->addInfo(count($data).' records found.');
2943
            $values = [];
2944
            foreach ($data as $row) {
2945
                if (empty($row)) {
2946
                    continue;
2947
                }
2948
                foreach ($row as $key => $value) {
2949
                    $key = (string) trim($key);
2950
                    // Remove utf8 bom
2951
                    $key = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $key);
2952
                    $row[$key] = $value;
2953
                }
2954
                $values[$row['Column']][] = $row;
2955
            }
2956
2957
            $careerList = [];
2958
            $careerNameList = [];
2959
            ksort($values);
2960
            $careerChamiloIdList = [];
2961
            // 1. First create all items
2962
            foreach ($values as $column => $rowList) {
2963
                foreach ($rowList as $row) {
2964
                    $careerId = $row['CareerId'];
2965
                    $item = $extraFieldValue->get_item_id_from_field_variable_and_field_value(
2966
                        $extraFieldName,
2967
                        $careerId,
2968
                        false,
2969
                        false,
2970
                        false
2971
                    );
2972
2973
                    if (empty($item)) {
2974
                        $this->logger->addInfo("Career not found: $careerId");
2975
                        continue;
2976
                    } else {
2977
                        if (isset($item['item_id'])) {
2978
                            $careerChamiloId = $item['item_id'];
2979
                            $career = new Career();
2980
                            $career = $career->find($careerChamiloId);
2981
                            $chamiloCareerName = $career['name'];
2982
                            $careerNameList[$careerId] = $chamiloCareerName;
2983
                            $careerChamiloIdList[$careerId] = $careerChamiloId;
2984
                        } else {
2985
                            continue;
2986
                        }
2987
                    }
2988
2989
                    if (empty($chamiloCareerName)) {
2990
                        $this->logger->addInfo("Career not found: $careerId");
2991
                        continue;
2992
                    }
2993
2994
                    if (isset($careerList[$careerId])) {
2995
                        $graph = $careerList[$careerId];
2996
                    } else {
2997
                        $graph = new Graph($careerId);
2998
                        $graph->setAttribute('graphviz.graph.rankdir', 'LR');
2999
                        $careerList[$careerId] = $graph;
3000
                    }
3001
3002
                    $currentCourseId = $row['CourseId'];
3003
                    $name = $row['CourseName'];
3004
                    $notes = $row['Notes'];
3005
                    $groupValue = $row['Group'];
3006
                    $boxColumn = $row['Column'];
3007
                    $rowValue = $row['Row'];
3008
                    $color = isset($row['DefinedColor']) ? $row['DefinedColor'] : '';
3009
                    $arrow = isset($row['DrawArrowFrom']) ? $row['DrawArrowFrom'] : '';
3010
                    $subGroup = isset($row['SubGroup']) ? $row['SubGroup'] : '';
3011
                    $connections = isset($row['Connections']) ? $row['Connections'] : '';
3012
                    $linkedElement = isset($row['LinkedElement']) ? $row['LinkedElement'] : '';
3013
3014
                    if ($graph->hasVertex($currentCourseId)) {
3015
                        // Avoid double insertion
3016
                        continue;
3017
                    } else {
3018
                        $current = $graph->createVertex($currentCourseId);
3019
                        $current->setAttribute('graphviz.label', $name);
3020
                        $current->setAttribute('DefinedColor', $color);
3021
                        $current->setAttribute('Notes', $notes);
3022
                        $current->setAttribute('Row', $rowValue);
3023
                        $current->setAttribute('Group', $groupValue);
3024
                        $current->setAttribute('Column', $boxColumn);
3025
                        $current->setAttribute('DrawArrowFrom', $arrow);
3026
                        $current->setAttribute('SubGroup', $subGroup);
3027
                        $current->setAttribute('Connections', $connections);
3028
                        $current->setAttribute('LinkedElement', $linkedElement);
3029
                        $current->setAttribute('graphviz.shape', 'box');
3030
                        $current->setGroup($column);
3031
                    }
3032
                }
3033
            }
3034
3035
            // 2. Create connections
3036
            // $column start with 1 (depending in Column row)
3037
            foreach ($values as $column => $rowList) {
3038
                foreach ($rowList as $row) {
3039
                    $careerId = $row['CareerId'];
3040
                    if (isset($careerList[$careerId])) {
3041
                        $graph = $careerList[$careerId];
3042
                    } else {
3043
                        continue;
3044
                    }
3045
3046
                    $currentCourseId = $row['CourseId'];
3047
                    if ($graph->hasVertex($currentCourseId)) {
3048
                        $current = $graph->getVertex($currentCourseId);
3049
                    } else {
3050
                        continue;
3051
                    }
3052
3053
                    if (isset($row['DependedOn']) && !empty($row['DependedOn'])) {
3054
                        $parentList = explode(',', $row['DependedOn']);
3055
                        foreach ($parentList as $parentId) {
3056
                            $parentId = (int) $parentId;
3057
                            if ($graph->hasVertex($parentId)) {
3058
                                /** @var Vertex $parent */
3059
                                $parent = $graph->getVertex($parentId);
3060
                                /*$parent->setAttribute('graphviz.color', 'red');
3061
                                $parent->setAttribute('graphviz.label', $name);
3062
                                $parent->setAttribute('graphviz.shape', 'square');*/
3063
                                $parent->createEdgeTo($current);
3064
                            }
3065
                        }
3066
                    }
3067
                }
3068
            }
3069
3070
            /** @var Graph $graph */
3071
            foreach ($careerList as $id => $graph) {
3072
                if (isset($careerChamiloIdList[$id])) {
3073
                    $params = [
3074
                        'item_id' => $careerChamiloIdList[$id],
3075
                        'extra_'.$careerDiagramExtraFieldName => serialize($graph),
3076
                        'extra_'.$extraFieldName => $id,
3077
                    ];
3078
                    $extraFieldValue->saveFieldValues($params, true);
3079
                }
3080
            }
3081
        }
3082
    }
3083
3084
    /**
3085
     * @param string $file
3086
     * @param bool   $moveFile
3087
     * @param array  $teacherBackup
3088
     * @param array  $groupBackup
3089
     */
3090
    private function importUnsubscribeStatic(
3091
        $file,
3092
        $moveFile = false,
3093
        &$teacherBackup = [],
3094
        &$groupBackup = []
3095
    ) {
3096
        $data = Import::csv_reader($file);
3097
3098
        if (!empty($data)) {
3099
            $this->logger->addInfo(count($data)." records found.");
3100
            foreach ($data as $row) {
3101
                $chamiloUserName = $row['UserName'];
3102
                $chamiloCourseCode = $row['CourseCode'];
3103
                $chamiloSessionId = $row['SessionID'];
3104
3105
                $sessionInfo = api_get_session_info($chamiloSessionId);
3106
3107
                if (empty($sessionInfo)) {
3108
                    $this->logger->addError('Session does not exists: '.$chamiloSessionId);
3109
                    continue;
3110
                }
3111
3112
                $courseInfo = api_get_course_info($chamiloCourseCode);
3113
                if (empty($courseInfo)) {
3114
                    $this->logger->addError('Course does not exists: '.$courseInfo);
3115
                    continue;
3116
                }
3117
3118
                $userId = UserManager::get_user_id_from_username($chamiloUserName);
3119
3120
                if (empty($userId)) {
3121
                    $this->logger->addError('User does not exists: '.$chamiloUserName);
3122
                    continue;
3123
                }
3124
3125
                $sql = "SELECT * FROM ".Database::get_main_table(TABLE_MAIN_COURSE_USER)."
3126
                        WHERE
3127
                            user_id = ".$userId." AND
3128
                            c_id = '".$courseInfo['real_id']."'
3129
                        ";
3130
                $result = Database::query($sql);
3131
                $rows = Database::num_rows($result);
3132
                if ($rows > 0) {
3133
                    $userCourseData = Database::fetch_array($result, 'ASSOC');
3134
                    if (!empty($userCourseData)) {
3135
                        $teacherBackup[$userId][$courseInfo['code']] = $userCourseData;
3136
                    }
3137
                }
3138
3139
                $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_USER)."
3140
                        WHERE
3141
                            user_id = ".$userId." AND
3142
                            c_id = '".$courseInfo['real_id']."'
3143
                        ";
3144
3145
                $result = Database::query($sql);
3146
                while ($groupData = Database::fetch_array($result, 'ASSOC')) {
3147
                    $groupBackup['user'][$userId][$courseInfo['code']][$groupData['group_id']] = $groupData;
3148
                }
3149
3150
                $sql = "SELECT * FROM ".Database::get_course_table(TABLE_GROUP_TUTOR)."
3151
                        WHERE
3152
                            user_id = ".$userId." AND
3153
                            c_id = '".$courseInfo['real_id']."'
3154
                        ";
3155
3156
                $result = Database::query($sql);
3157
                while ($groupData = Database::fetch_array($result, 'ASSOC')) {
3158
                    $groupBackup['tutor'][$userId][$courseInfo['code']][$groupData['group_id']] = $groupData;
3159
                }
3160
3161
                CourseManager::unsubscribe_user(
3162
                    $userId,
3163
                    $courseInfo['code'],
3164
                    $chamiloSessionId
3165
                );
3166
3167
                $this->logger->addError(
3168
                    "User '$chamiloUserName' was removed from session: #$chamiloSessionId, Course: ".$courseInfo['code']
3169
                );
3170
            }
3171
        }
3172
3173
        if ($moveFile) {
3174
            $this->moveFile($file);
3175
        }
3176
    }
3177
3178
    /**
3179
     *  Dump database tables.
3180
     */
3181
    private function dumpDatabaseTables()
3182
    {
3183
        echo 'Dumping tables'.PHP_EOL;
3184
3185
        // User
3186
        $table = Database::get_main_table(TABLE_MAIN_USER);
3187
        $tableAdmin = Database::get_main_table(TABLE_MAIN_ADMIN);
3188
        $sql = "DELETE FROM $table
3189
                WHERE user_id not in (select user_id from $tableAdmin) and status <> ".ANONYMOUS;
3190
        Database::query($sql);
3191
        echo $sql.PHP_EOL;
3192
3193
        // Truncate tables
3194
        $truncateTables = [
3195
            Database::get_main_table(TABLE_MAIN_COURSE),
3196
            Database::get_main_table(TABLE_MAIN_COURSE_USER),
3197
            Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_COURSE),
3198
            Database::get_main_table(TABLE_MAIN_CATEGORY),
3199
            Database::get_main_table(TABLE_MAIN_COURSE_MODULE),
3200
            Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_USER),
3201
            Database::get_main_table(TABLE_MAIN_SESSION),
3202
            Database::get_main_table(TABLE_MAIN_SESSION_CATEGORY),
3203
            Database::get_main_table(TABLE_MAIN_SESSION_COURSE),
3204
            Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER),
3205
            Database::get_main_table(TABLE_MAIN_SESSION_USER),
3206
            Database::get_main_table(TABLE_MAIN_ACCESS_URL_REL_SESSION),
3207
            Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES),
3208
            Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES),
3209
            Database::get_main_table(TABLE_MAIN_USER_FIELD_VALUES),
3210
            Database::get_main_table(TABLE_MAIN_USER_FIELD),
3211
            Database::get_main_table(TABLE_MAIN_USER_FIELD_OPTIONS),
3212
            Database::get_main_table(TABLE_MAIN_COURSE_FIELD),
3213
            Database::get_main_table(TABLE_MAIN_COURSE_FIELD_VALUES),
3214
            Database::get_main_table(TABLE_MAIN_SESSION_FIELD),
3215
            Database::get_main_table(TABLE_MAIN_SESSION_FIELD_VALUES),
3216
            Database::get_course_table(TABLE_AGENDA),
3217
            Database::get_course_table(TABLE_AGENDA_ATTACHMENT),
3218
            Database::get_course_table(TABLE_AGENDA_REPEAT),
3219
            Database::get_course_table(TABLE_AGENDA_REPEAT_NOT),
3220
            Database::get_main_table(TABLE_PERSONAL_AGENDA),
3221
            Database::get_main_table(TABLE_PERSONAL_AGENDA_REPEAT_NOT),
3222
            Database::get_main_table(TABLE_PERSONAL_AGENDA_REPEAT),
3223
            Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_VALUES),
3224
            Database::get_main_table(TABLE_STATISTIC_TRACK_E_LASTACCESS),
3225
            Database::get_main_table(TABLE_STATISTIC_TRACK_E_ACCESS),
3226
            Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN),
3227
            Database::get_main_table(TABLE_STATISTIC_TRACK_E_DOWNLOADS),
3228
            Database::get_main_table(TABLE_STATISTIC_TRACK_E_EXERCICES),
3229
            Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT),
3230
            Database::get_main_table(TABLE_STATISTIC_TRACK_E_ATTEMPT_RECORDING),
3231
            Database::get_main_table(TABLE_STATISTIC_TRACK_E_DEFAULT),
3232
            Database::get_main_table(TABLE_STATISTIC_TRACK_E_UPLOADS),
3233
            Database::get_main_table(TABLE_STATISTIC_TRACK_E_HOTSPOT),
3234
            Database::get_main_table(TABLE_STATISTIC_TRACK_E_ITEM_PROPERTY),
3235
            Database::get_main_table(TABLE_MAIN_GRADEBOOK_CATEGORY),
3236
            Database::get_main_table(TABLE_MAIN_GRADEBOOK_EVALUATION),
3237
            Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINKEVAL_LOG),
3238
            Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT),
3239
            Database::get_main_table(TABLE_MAIN_GRADEBOOK_RESULT_LOG),
3240
            Database::get_main_table(TABLE_MAIN_GRADEBOOK_LINK),
3241
            Database::get_main_table(TABLE_MAIN_GRADEBOOK_SCORE_DISPLAY),
3242
            Database::get_main_table(TABLE_MAIN_GRADEBOOK_CERTIFICATE),
3243
            Database::get_course_table(TABLE_STUDENT_PUBLICATION),
3244
            Database::get_course_table(TABLE_QUIZ_QUESTION),
3245
            Database::get_course_table(TABLE_QUIZ_TEST),
3246
            Database::get_course_table(TABLE_QUIZ_ORDER),
3247
            Database::get_course_table(TABLE_QUIZ_ANSWER),
3248
            Database::get_course_table(TABLE_QUIZ_TEST_QUESTION),
3249
            Database::get_course_table(TABLE_QUIZ_QUESTION_OPTION),
3250
            Database::get_course_table(TABLE_QUIZ_QUESTION_CATEGORY),
3251
            Database::get_course_table(TABLE_QUIZ_QUESTION_REL_CATEGORY),
3252
            Database::get_course_table(TABLE_LP_MAIN),
3253
            Database::get_course_table(TABLE_LP_ITEM),
3254
            Database::get_course_table(TABLE_LP_VIEW),
3255
            Database::get_course_table(TABLE_LP_ITEM_VIEW),
3256
            Database::get_course_table(TABLE_DOCUMENT),
3257
            Database::get_course_table(TABLE_ITEM_PROPERTY),
3258
            Database::get_course_table(TABLE_TOOL_LIST),
3259
            Database::get_course_table(TABLE_TOOL_INTRO),
3260
            Database::get_course_table(TABLE_COURSE_SETTING),
3261
            Database::get_course_table(TABLE_SURVEY),
3262
            Database::get_course_table(TABLE_SURVEY_QUESTION),
3263
            Database::get_course_table(TABLE_SURVEY_QUESTION_OPTION),
3264
            Database::get_course_table(TABLE_SURVEY_INVITATION),
3265
            Database::get_course_table(TABLE_SURVEY_ANSWER),
3266
            Database::get_course_table(TABLE_SURVEY_QUESTION_GROUP),
3267
            Database::get_course_table(TABLE_SURVEY_REPORT),
3268
            Database::get_course_table(TABLE_GLOSSARY),
3269
            Database::get_course_table(TABLE_LINK),
3270
            Database::get_course_table(TABLE_LINK_CATEGORY),
3271
            Database::get_course_table(TABLE_GROUP),
3272
            Database::get_course_table(TABLE_GROUP_USER),
3273
            Database::get_course_table(TABLE_GROUP_TUTOR),
3274
            Database::get_course_table(TABLE_GROUP_CATEGORY),
3275
            Database::get_course_table(TABLE_DROPBOX_CATEGORY),
3276
            Database::get_course_table(TABLE_DROPBOX_FEEDBACK),
3277
            Database::get_course_table(TABLE_DROPBOX_POST),
3278
            Database::get_course_table(TABLE_DROPBOX_FILE),
3279
            Database::get_course_table(TABLE_DROPBOX_PERSON),
3280
        ];
3281
3282
        foreach ($truncateTables as $table) {
3283
            $sql = "TRUNCATE $table";
3284
            Database::query($sql);
3285
            echo $sql.PHP_EOL;
3286
        }
3287
3288
        $table = Database::get_course_table(TABLE_ITEM_PROPERTY);
3289
        $sql = "DELETE FROM $table WHERE tool = 'calendar_event'";
3290
        Database::query($sql);
3291
        echo $sql.PHP_EOL;
3292
    }
3293
3294
    /**
3295
     * If csv file ends with '"' character then a '";' is added.
3296
     *
3297
     * @param string $file
3298
     */
3299
    private function fixCSVFile($file)
3300
    {
3301
        /*$f = fopen($file, 'r+');
3302
        $cursor = -1;
3303
3304
        fseek($f, $cursor, SEEK_END);
3305
        $char = fgetc($f);
3306
        while ($char === "\n" || $char === "\r") {
3307
            fseek($f, $cursor--, SEEK_END);
3308
            $char = fgetc($f);
3309
        }
3310
3311
        if ($char === "\"") {
3312
            fseek($f, -1, SEEK_CUR);
3313
            fwrite($f, '";');
3314
        }*/
3315
    }
3316
}
3317
3318
$logger = new Logger('cron');
3319
$emails = isset($_configuration['cron_notification_mails']) ? $_configuration['cron_notification_mails'] : null;
3320
3321
$minLevel = Logger::DEBUG;
3322
3323
if (!is_array($emails)) {
3324
    $emails = [$emails];
3325
}
3326
$subject = "Cron main/cron/import_csv.php ".date('Y-m-d h:i:s');
3327
$from = api_get_setting('emailAdministrator');
3328
/*
3329
if (!empty($emails)) {
3330
    foreach ($emails as $email) {
3331
        $stream = new NativeMailerHandler($email, $subject, $from, $minLevel);
3332
        $logger->pushHandler(new BufferHandler($stream, 0, $minLevel));
3333
    }
3334
}*/
3335
3336
$stream = new StreamHandler(
3337
    api_get_path(SYS_ARCHIVE_PATH).'import_csv.log',
3338
    $minLevel
3339
);
3340
$logger->pushHandler(new BufferHandler($stream, 0, $minLevel));
3341
$logger->pushHandler(new RotatingFileHandler('import_csv', 5, $minLevel));
3342
3343
$verbose = false;
3344
if (isset($argv[1]) && $argv[1] === '--verbose') {
3345
    $verbose = true;
3346
}
3347
if ($verbose) {
3348
    $logger->pushHandler(new ErrorLogHandler());
3349
}
3350
3351
$cronImportCSVConditions = isset($_configuration['cron_import_csv_conditions']) ? $_configuration['cron_import_csv_conditions'] : null;
3352
3353
echo 'See the error log here: '.api_get_path(SYS_ARCHIVE_PATH).'import_csv.log'."\n";
3354
3355
$import = new ImportCsv($logger, $cronImportCSVConditions);
3356
3357
if (isset($_configuration['default_admin_user_id_for_cron'])) {
3358
    $import->defaultAdminId = $_configuration['default_admin_user_id_for_cron'];
3359
}
3360
// @todo in production disable the dump option
3361
$dump = false;
3362
if (isset($argv[1]) && $argv[1] === '--dump') {
3363
    $dump = true;
3364
}
3365
3366
if (isset($_configuration['import_csv_disable_dump']) &&
3367
    $_configuration['import_csv_disable_dump'] == true
3368
) {
3369
    $import->setDumpValues(false);
3370
} else {
3371
    $import->setDumpValues($dump);
3372
}
3373
3374
$import->setUpdateEmailToDummy(api_get_configuration_value('update_users_email_to_dummy_except_admins'));
3375
3376
// Do not moves the files to treated
3377
if (isset($_configuration['import_csv_test'])) {
3378
    $import->test = $_configuration['import_csv_test'];
3379
} else {
3380
    $import->test = true;
3381
}
3382
3383
$languageFilesToLoad = api_get_language_files_to_load($import->defaultLanguage);
3384
3385
foreach ($languageFilesToLoad as $languageFile) {
3386
    include $languageFile;
3387
}
3388
3389
// Set default language to be loaded
3390
$language = $import->defaultLanguage;
3391
global $language_interface;
3392
$language_interface = $language;
3393
global $language_interface_initial_value;
3394
$language_interface_initial_value = $language;
3395
3396
$timeStart = microtime(true);
3397
$import->run();
3398
$timeEnd = microtime(true);
3399
$executionTime = round(($timeEnd - $timeStart) / 60, 2);
3400
$logger->addInfo("Total execution Time $executionTime Min");
3401
3402
if (isset($_configuration['import_csv_fix_permissions']) &&
3403
    $_configuration['import_csv_fix_permissions'] == true
3404
) {
3405
    $command = "sudo find ".api_get_path(SYS_COURSE_PATH)." -type d -exec chmod 777 {} \; ";
3406
    echo "Executing: ".$command.PHP_EOL;
3407
    system($command);
3408
3409
    $command = "sudo find ".api_get_path(SYS_CODE_PATH)."upload/users  -type d -exec chmod 777 {} \;";
3410
    echo "Executing: ".$command.PHP_EOL;
3411
    system($command);
3412
}
3413