Passed
Push — master ( b61354...af9f4d )
by Julito
19:31
created

UserToJsonNormalizer   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 796
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 438
dl 0
loc 796
rs 8.64
c 2
b 0
f 0
wmc 47

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
F getPersonalDataToJson() 0 778 46

How to fix   Complexity   

Complex Class

Complex classes like UserToJsonNormalizer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use UserToJsonNormalizer, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chamilo\CoreBundle\Serializer;
6
7
use Agenda;
8
use Chamilo\CoreBundle\Entity\GradebookCertificate;
9
use Chamilo\CoreBundle\Entity\GradebookResult;
10
use Chamilo\CoreBundle\Entity\Message;
11
use Chamilo\CoreBundle\Entity\SkillRelUserComment;
12
use Chamilo\CoreBundle\Entity\Ticket;
13
use Chamilo\CoreBundle\Entity\TicketMessage;
14
use Chamilo\CoreBundle\Entity\TrackEAccess;
15
use Chamilo\CoreBundle\Entity\TrackEAttempt;
16
use Chamilo\CoreBundle\Entity\TrackECourseAccess;
17
use Chamilo\CoreBundle\Entity\TrackEDefault;
18
use Chamilo\CoreBundle\Entity\TrackEDownloads;
19
use Chamilo\CoreBundle\Entity\TrackEExercises;
20
use Chamilo\CoreBundle\Entity\TrackELastaccess;
21
use Chamilo\CoreBundle\Entity\TrackELogin;
22
use Chamilo\CoreBundle\Entity\TrackEOnline;
23
use Chamilo\CoreBundle\Entity\TrackEUploads;
24
use Chamilo\CoreBundle\Entity\User;
25
use Chamilo\CoreBundle\Entity\UserCourseCategory;
26
use Chamilo\CoreBundle\Entity\UserRelCourseVote;
27
use Chamilo\CoreBundle\Repository\Node\UserRepository;
28
use Chamilo\CourseBundle\Entity\CAttendanceResult;
29
use Chamilo\CourseBundle\Entity\CAttendanceSheet;
30
use Chamilo\CourseBundle\Entity\CBlogPost;
31
use Chamilo\CourseBundle\Entity\CDropboxFeedback;
32
use Chamilo\CourseBundle\Entity\CDropboxFile;
33
use Chamilo\CourseBundle\Entity\CDropboxPerson;
34
use Chamilo\CourseBundle\Entity\CForumPost;
35
use Chamilo\CourseBundle\Entity\CForumThread;
36
use Chamilo\CourseBundle\Entity\CGroupRelUser;
37
use Chamilo\CourseBundle\Entity\CLpView;
38
use Chamilo\CourseBundle\Entity\CNotebook;
39
use Chamilo\CourseBundle\Entity\CStudentPublication;
40
use Chamilo\CourseBundle\Entity\CStudentPublicationComment;
41
use Chamilo\CourseBundle\Entity\CSurveyAnswer;
42
use Chamilo\CourseBundle\Entity\CWiki;
43
use DateTime;
44
use DateTimeInterface;
45
use Doctrine\ORM\EntityManager;
46
use SocialManager;
47
use Symfony\Component\Serializer\Encoder\JsonEncoder;
48
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
49
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
50
use Symfony\Component\Serializer\Serializer;
51
52
final class UserToJsonNormalizer
53
{
54
    private EntityManager $em;
55
    private UserRepository $userRepository;
56
57
    public function __construct(EntityManager $em, UserRepository $userRepository)
58
    {
59
        $this->em = $em;
60
        $this->userRepository = $userRepository;
61
    }
62
63
    /**
64
     * Serialize the whole entity to an array.
65
     *
66
     * @param array $substitutionTerms Substitute terms for some elements
67
     *
68
     * @return string
69
     */
70
    public function getPersonalDataToJson(int $userId, array $substitutionTerms)
71
    {
72
        $em = $this->em;
73
        $dateFormat = DateTimeInterface::ATOM;
74
75
        /** @var User $user */
76
        $user = $this->userRepository->find($userId);
77
78
        $user->setPassword($substitutionTerms['password']);
79
        $user->setSalt($substitutionTerms['salt']);
80
81
        $noDataLabel = $substitutionTerms['empty'];
82
83
        // Dummy content
84
        $user->setDateOfBirth(null);
85
        //$user->setBiography($noDataLabel);
86
        /*$user->setFacebookData($noDataLabel);
87
        $user->setFacebookName($noDataLabel);
88
        $user->setFacebookUid($noDataLabel);*/
89
        //$user->setImageName($noDataLabel);
90
        //$user->setTwoStepVerificationCode($noDataLabel);
91
        //$user->setGender($noDataLabel);
92
        /*$user->setGplusData($noDataLabel);
93
        $user->setGplusName($noDataLabel);
94
        $user->setGplusUid($noDataLabel);*/
95
        $user->setLocale($noDataLabel);
96
        $user->setTimezone($noDataLabel);
97
        /*$user->setTwitterData($noDataLabel);
98
        $user->setTwitterName($noDataLabel);
99
        $user->setTwitterUid($noDataLabel);*/
100
        $user->setWebsite($noDataLabel);
101
        //$user->setToken($noDataLabel);
102
103
        $friends = SocialManager::get_friends($userId);
104
        $friendList = [];
105
        if (!empty($friends)) {
106
            foreach ($friends as $friend) {
107
                $friendList[] = $friend['user_info']['complete_name'];
108
            }
109
        }
110
111
        $agenda = new Agenda('personal');
112
        $events = $agenda->getEvents(0, 0, 0, 0, $userId, 'array');
113
        $eventList = [];
114
        if (!empty($events)) {
115
            foreach ($events as $event) {
116
                $eventList[] = $event['title'].' '.$event['start_date_localtime'].' / '.$event['end_date_localtime'];
117
            }
118
        }
119
120
        // GradebookCertificate
121
        $result = $em->getRepository(GradebookCertificate::class)->findBy([
122
            'user' => $userId,
123
        ]);
124
        $gradebookCertificate = [];
125
        /** @var GradebookCertificate $item */
126
        foreach ($result as $item) {
127
            $createdAt = $item->getCreatedAt()->format($dateFormat);
128
            $list = [
129
                'Score: '.$item->getScoreCertificate(),
130
                'Path: '.$item->getPathCertificate(),
131
                'Created at: '.$createdAt,
132
            ];
133
            $gradebookCertificate[] = implode(', ', $list);
134
        }
135
136
        // TrackEExercises
137
        $criteria = [
138
            'exeUserId' => $userId,
139
        ];
140
        $result = $em->getRepository(TrackEExercises::class)->findBy($criteria);
141
        $trackEExercises = [];
142
        /** @var TrackEExercises $item */
143
        foreach ($result as $item) {
144
            $date = $item->getExeDate()->format($dateFormat);
145
            $list = [
146
                'IP: '.$item->getUserIp(),
147
                'Start: '.$date,
148
                'Status: '.$item->getStatus(),
149
                // 'Result: '.$item->getExeResult(),
150
                // 'Weighting: '.$item->getExeWeighting(),
151
            ];
152
            $trackEExercises[] = implode(', ', $list);
153
        }
154
155
        // TrackEAttempt
156
        $criteria = [
157
            'user' => $userId,
158
        ];
159
        $result = $em->getRepository(TrackEAttempt::class)->findBy($criteria);
160
        $trackEAttempt = [];
161
        /** @var TrackEAttempt $item */
162
        foreach ($result as $item) {
163
            $date = $item->getTms()->format($dateFormat);
164
            $list = [
165
                'Attempt #'.$item->getExeId(),
166
                'Course # '.$item->getCourse()->getCode(),
167
                //'Answer: '.$item->getAnswer(),
168
                'Session #'.$item->getSessionId(),
169
                //'Marks: '.$item->getMarks(),
170
                'Position: '.$item->getPosition(),
171
                'Date: '.$date,
172
            ];
173
            $trackEAttempt[] = implode(', ', $list);
174
        }
175
176
        // TrackECourseAccess
177
        $criteria = [
178
            'user' => $userId,
179
        ];
180
        $result = $em->getRepository(TrackECourseAccess::class)->findBy($criteria);
181
        $trackECourseAccessList = [];
182
        /** @var TrackECourseAccess $item */
183
        foreach ($result as $item) {
184
            $startDate = $item->getLoginCourseDate()->format($dateFormat);
185
            $endDate = null !== $item->getLogoutCourseDate() ? $item->getLogoutCourseDate()->format($dateFormat) : '';
186
            $list = [
187
                'IP: '.$item->getUserIp(),
188
                'Start: '.$startDate,
189
                'End: '.$endDate,
190
            ];
191
            $trackECourseAccessList[] = implode(', ', $list);
192
        }
193
194
        $checkEntities = [
195
            TrackELogin::class => 'loginUserId',
196
            TrackEAccess::class => 'accessUserId',
197
            TrackEOnline::class => 'loginUserId',
198
            TrackEDefault::class => 'defaultUserId',
199
            TrackELastaccess::class => 'accessUserId',
200
            TrackEUploads::class => 'uploadUserId',
201
            GradebookResult::class => 'user',
202
            TrackEDownloads::class => 'downUserId',
203
        ];
204
205
        $maxResults = 1000;
206
        $trackResults = [];
207
        foreach ($checkEntities as $entity => $field) {
208
            $qb = $em->createQueryBuilder();
209
            $qb->select($qb->expr()->count('l'))
210
                ->from($entity, 'l')
211
                ->where("l.$field = :login")
212
                ->setParameter('login', $userId)
213
            ;
214
            $query = $qb->getQuery();
215
            $count = $query->getSingleScalarResult();
216
217
            if ($count > $maxResults) {
218
                $qb = $em->getRepository($entity)->createQueryBuilder('l');
219
                $qb
220
                    ->select('l')
221
                    ->where("l.$field = :login")
222
                    ->setParameter('login', $userId)
223
                ;
224
                $qb
225
                    ->setFirstResult(0)
226
                    ->setMaxResults($maxResults)
227
                ;
228
                $result = $qb->getQuery()->getResult();
229
            } else {
230
                $criteria = [
231
                    $field => $userId,
232
                ];
233
                $result = $em->getRepository($entity)->findBy($criteria);
234
            }
235
            $trackResults[$entity] = $result;
236
        }
237
238
        $trackELoginList = [];
239
        /** @var TrackELogin $item */
240
        foreach ($trackResults[TrackELogin::class] as $item) {
241
            $startDate = $item->getLoginDate()->format($dateFormat);
242
            $endDate = null !== $item->getLogoutDate() ? $item->getLogoutDate()->format($dateFormat) : '';
243
            $list = [
244
                'IP: '.$item->getUserIp(),
245
                'Start: '.$startDate,
246
                'End: '.$endDate,
247
            ];
248
            $trackELoginList[] = implode(', ', $list);
249
        }
250
251
        // TrackEAccess
252
        $trackEAccessList = [];
253
        /** @var TrackEAccess $item */
254
        foreach ($trackResults[TrackEAccess::class] as $item) {
255
            $date = $item->getAccessDate()->format($dateFormat);
256
            $list = [
257
                'IP: '.$item->getUserIp(),
258
                'Tool: '.$item->getAccessTool(),
259
                'End: '.$date,
260
            ];
261
            $trackEAccessList[] = implode(', ', $list);
262
        }
263
264
        // TrackEOnline
265
        $trackEOnlineList = [];
266
        /** @var TrackEOnline $item */
267
        foreach ($trackResults[TrackEOnline::class] as $item) {
268
            $date = $item->getLoginDate()->format($dateFormat);
269
            $list = [
270
                'IP: '.$item->getUserIp(),
271
                'Login date: '.$date,
272
                'Course # '.$item->getCId(),
273
                'Session # '.$item->getSessionId(),
274
            ];
275
            $trackEOnlineList[] = implode(', ', $list);
276
        }
277
278
        // TrackEDefault
279
        $trackEDefault = [];
280
        /** @var TrackEDefault $item */
281
        foreach ($trackResults[TrackEDefault::class] as $item) {
282
            $date = $item->getDefaultDate()->format($dateFormat);
283
            $list = [
284
                'Type: '.$item->getDefaultEventType(),
285
                'Value: '.$item->getDefaultValue(),
286
                'Value type: '.$item->getDefaultValueType(),
287
                'Date: '.$date,
288
                'Course #'.$item->getCId(),
289
                'Session # '.$item->getSessionId(),
290
            ];
291
            $trackEDefault[] = implode(', ', $list);
292
        }
293
294
        // TrackELastaccess
295
        $trackELastaccess = [];
296
        /** @var TrackELastaccess $item */
297
        foreach ($trackResults[TrackELastaccess::class] as $item) {
298
            $date = $item->getAccessDate()->format($dateFormat);
299
            $list = [
300
                'Course #'.$item->getCId(),
301
                'Session # '.$item->getAccessSessionId(),
302
                'Tool: '.$item->getAccessTool(),
303
                'Access date: '.$date,
304
            ];
305
            $trackELastaccess[] = implode(', ', $list);
306
        }
307
308
        // TrackEUploads
309
        $trackEUploads = [];
310
        /** @var TrackEUploads $item */
311
        foreach ($trackResults[TrackEUploads::class] as $item) {
312
            $date = $item->getUploadDate()->format($dateFormat);
313
            $list = [
314
                'Course #'.$item->getCId(),
315
                'Uploaded at: '.$date,
316
                'Upload id # '.$item->getUploadId(),
317
            ];
318
            $trackEUploads[] = implode(', ', $list);
319
        }
320
321
        $gradebookResult = [];
322
        /** @var GradebookResult $item */
323
        foreach ($trackResults[GradebookResult::class] as $item) {
324
            $date = $item->getCreatedAt()->format($dateFormat);
325
            $list = [
326
                'Evaluation id# '.$item->getEvaluation()->getId(),
327
                //'Score: '.$item->getScore(),
328
                'Creation date: '.$date,
329
            ];
330
            $gradebookResult[] = implode(', ', $list);
331
        }
332
333
        $trackEDownloads = [];
334
        /** @var TrackEDownloads $item */
335
        foreach ($trackResults[TrackEDownloads::class] as $item) {
336
            $date = $item->getDownDate()->format($dateFormat);
337
            $list = [
338
                'File: '.$item->getDownDocPath(),
339
                'Download at: '.$date,
340
            ];
341
            $trackEDownloads[] = implode(', ', $list);
342
        }
343
344
        // UserCourseCategory
345
        $criteria = [
346
            'user' => $userId,
347
        ];
348
        $result = $em->getRepository(UserCourseCategory::class)->findBy($criteria);
349
        $userCourseCategory = [];
350
        /** @var UserCourseCategory $item */
351
        foreach ($result as $item) {
352
            $list = [
353
                'Title: '.$item->getTitle(),
354
            ];
355
            $userCourseCategory[] = implode(', ', $list);
356
        }
357
358
        // Forum
359
        $criteria = [
360
            'user' => $userId,
361
        ];
362
        $result = $em->getRepository(CForumPost::class)->findBy($criteria);
363
        $cForumPostList = [];
364
        /** @var CForumPost $item */
365
        foreach ($result as $item) {
366
            $date = $item->getPostDate()->format($dateFormat);
367
            $list = [
368
                'Title: '.$item->getPostTitle(),
369
                'Creation date: '.$date,
370
            ];
371
            $cForumPostList[] = implode(', ', $list);
372
        }
373
374
        // CForumThread
375
        $criteria = [
376
            'user' => $userId,
377
        ];
378
        $result = $em->getRepository(CForumThread::class)->findBy($criteria);
379
        $cForumThreadList = [];
380
        /** @var CForumThread $item */
381
        foreach ($result as $item) {
382
            $date = $item->getThreadDate()->format($dateFormat);
383
            $list = [
384
                'Title: '.$item->getThreadTitle(),
385
                'Creation date: '.$date,
386
            ];
387
            $cForumThreadList[] = implode(', ', $list);
388
        }
389
390
        // CForumAttachment
391
        /*$criteria = [
392
            'threadPosterId' => $userId,
393
        ];
394
        $result = $em->getRepository('ChamiloCourseBundle:CForumAttachment')->findBy($criteria);
395
        $cForumThreadList = [];
396
        * @var CForumThread $item
397
        foreach ($result as $item) {
398
            $list = [
399
                'Title: '.$item->getThreadTitle(),
400
                'Creation date: '.$item->getThreadDate()->format($dateFormat),
401
            ];
402
            $cForumThreadList[] = implode(', ', $list);
403
        }*/
404
405
        // cGroupRelUser
406
        $criteria = [
407
            'user' => $userId,
408
        ];
409
        $result = $em->getRepository(CGroupRelUser::class)->findBy($criteria);
410
        $cGroupRelUser = [];
411
        /** @var CGroupRelUser $item */
412
        foreach ($result as $item) {
413
            $list = [
414
                'Course # '.$item->getCId(),
415
                'Group #'.$item->getGroup()->getIid(),
416
                'Role: '.$item->getStatus(),
417
            ];
418
            $cGroupRelUser[] = implode(', ', $list);
419
        }
420
421
        // CAttendanceSheet
422
        $criteria = [
423
            'user' => $userId,
424
        ];
425
        $result = $em->getRepository(CAttendanceSheet::class)->findBy($criteria);
426
        $cAttendanceSheetList = [];
427
        /** @var CAttendanceSheet $item */
428
        foreach ($result as $item) {
429
            $list = [
430
                'Presence: '.$item->getPresence(),
431
                'Calendar id: '.$item->getAttendanceCalendar()->getIid(),
432
            ];
433
            $cAttendanceSheetList[] = implode(', ', $list);
434
        }
435
436
        // CBlogPost
437
        $criteria = [
438
            'authorId' => $userId,
439
        ];
440
        $result = $em->getRepository(CBlogPost::class)->findBy($criteria);
441
        $cBlog = [];
442
        /** @var CBlogPost $item */
443
        foreach ($result as $item) {
444
            $date = $item->getDateCreation()->format($dateFormat);
445
            $list = [
446
                'Title: '.$item->getTitle(),
447
                'Date: '.$date,
448
            ];
449
            $cBlog[] = implode(', ', $list);
450
        }
451
452
        // CAttendanceResult
453
        $criteria = [
454
            'user' => $userId,
455
        ];
456
        $result = $em->getRepository(CAttendanceResult::class)->findBy($criteria);
457
        $cAttendanceResult = [];
458
        /** @var CAttendanceResult $item */
459
        foreach ($result as $item) {
460
            $list = [
461
                'Score : '.$item->getScore(),
462
                'Calendar id: '.$item->getAttendance()->getIid(),
463
            ];
464
            $cAttendanceResult[] = implode(', ', $list);
465
        }
466
467
        // Message
468
        $criteria = [
469
            'userSender' => $userId,
470
        ];
471
        $result = $em->getRepository(Message::class)->findBy($criteria);
472
        $messageList = [];
473
        /** @var Message $item */
474
        foreach ($result as $item) {
475
            $date = $item->getSendDate()->format($dateFormat);
476
            $userName = '';
477
            if ($item->getUserReceiver()) {
478
                $userName = $item->getUserReceiver()->getUsername();
479
            }
480
            $list = [
481
                'Title: '.$item->getTitle(),
482
                'Sent date: '.$date,
483
                'To user: '.$userName,
484
                'Status'.$item->getMsgStatus(),
485
            ];
486
            $messageList[] = implode(', ', $list);
487
        }
488
489
        // CSurveyAnswer
490
        $criteria = [
491
            'user' => $userId,
492
        ];
493
        $result = $em->getRepository(CSurveyAnswer::class)->findBy($criteria);
494
        $cSurveyAnswer = [];
495
        /** @var CSurveyAnswer $item */
496
        foreach ($result as $item) {
497
            $list = [
498
                'Answer # '.$item->getIid(),
499
                'Value: '.$item->getValue(),
500
            ];
501
            $cSurveyAnswer[] = implode(', ', $list);
502
        }
503
504
        // CDropboxFile
505
        $criteria = [
506
            'uploaderId' => $userId,
507
        ];
508
        $result = $em->getRepository(CDropboxFile::class)->findBy($criteria);
509
        $cDropboxFile = [];
510
        /** @var CDropboxFile $item */
511
        foreach ($result as $item) {
512
            $date = $item->getUploadDate()->format($dateFormat);
513
            $list = [
514
                'Title: '.$item->getTitle(),
515
                'Uploaded date: '.$date,
516
                'File: '.$item->getFilename(),
517
            ];
518
            $cDropboxFile[] = implode(', ', $list);
519
        }
520
521
        // CDropboxPerson
522
        $criteria = [
523
            'userId' => $userId,
524
        ];
525
        $result = $em->getRepository(CDropboxPerson::class)->findBy($criteria);
526
        $cDropboxPerson = [];
527
        /** @var CDropboxPerson $item */
528
        foreach ($result as $item) {
529
            $list = [
530
                'File #'.$item->getFileId(),
531
                'Course #'.$item->getCId(),
532
            ];
533
            $cDropboxPerson[] = implode(', ', $list);
534
        }
535
536
        // CDropboxPerson
537
        $criteria = [
538
            'authorUserId' => $userId,
539
        ];
540
        $result = $em->getRepository(CDropboxFeedback::class)->findBy($criteria);
541
        $cDropboxFeedback = [];
542
        /** @var CDropboxFeedback $item */
543
        foreach ($result as $item) {
544
            $date = $item->getFeedbackDate()->format($dateFormat);
545
            $list = [
546
                'File #'.$item->getFileId(),
547
                'Feedback: '.$item->getFeedback(),
548
                'Date: '.$date,
549
            ];
550
            $cDropboxFeedback[] = implode(', ', $list);
551
        }
552
553
        // CNotebook
554
        $criteria = [
555
            'user' => $userId,
556
        ];
557
        $result = $em->getRepository(CNotebook::class)->findBy($criteria);
558
        $cNotebook = [];
559
        /** @var CNotebook $item */
560
        foreach ($result as $item) {
561
            $date = $item->getUpdateDate()->format($dateFormat);
562
            $list = [
563
                'Title: '.$item->getTitle(),
564
                'Date: '.$date,
565
            ];
566
            $cNotebook[] = implode(', ', $list);
567
        }
568
569
        // CLpView
570
        $criteria = [
571
            'user' => $userId,
572
        ];
573
        $result = $em->getRepository(CLpView::class)->findBy($criteria);
574
        $cLpView = [];
575
        /** @var CLpView $item */
576
        foreach ($result as $item) {
577
            $list = [
578
                //'Id #'.$item->getId(),
579
                'LP #'.$item->getLp()->getIid(),
580
                'Progress: '.$item->getProgress(),
581
                //'Course #'.$item->getCId(),
582
                //'Session #'.$item->getSessionId(),
583
            ];
584
            $cLpView[] = implode(', ', $list);
585
        }
586
587
        // CStudentPublication
588
        $criteria = [
589
            'user' => $userId,
590
        ];
591
        $result = $em->getRepository(CStudentPublication::class)->findBy($criteria);
592
        $cStudentPublication = [];
593
        /** @var CStudentPublication $item */
594
        foreach ($result as $item) {
595
            $list = [
596
                'Title: '.$item->getTitle(),
597
                //'URL: '.$item->getTitle(),
598
            ];
599
            $cStudentPublication[] = implode(', ', $list);
600
        }
601
602
        // CStudentPublicationComment
603
        $criteria = [
604
            'user' => $userId,
605
        ];
606
        $result = $em->getRepository(CStudentPublicationComment::class)->findBy($criteria);
607
        $cStudentPublicationComment = [];
608
        /** @var CStudentPublicationComment $item */
609
        foreach ($result as $item) {
610
            $date = $item->getSentAt()->format($dateFormat);
611
            $list = [
612
                'Commment: '.$item->getComment(),
613
                'File '.$item->getFile(),
614
                //'Course # '.$item->getCId(),
615
                'Date: '.$date,
616
            ];
617
            $cStudentPublicationComment[] = implode(', ', $list);
618
        }
619
620
        // CWiki
621
        $criteria = [
622
            'userId' => $userId,
623
        ];
624
        $result = $em->getRepository(CWiki::class)->findBy($criteria);
625
        $cWiki = [];
626
        /** @var CWiki $item */
627
        foreach ($result as $item) {
628
            $list = [
629
                'Title: '.$item->getTitle(),
630
                'Progress: '.$item->getProgress(),
631
                'IP: '.$item->getUserIp(),
632
            ];
633
            $cWiki[] = implode(', ', $list);
634
        }
635
636
        // Ticket
637
        $criteria = [
638
            'insertUserId' => $userId,
639
        ];
640
        $result = $em->getRepository(Ticket::class)->findBy($criteria);
641
        $ticket = [];
642
        /** @var Ticket $item */
643
        foreach ($result as $item) {
644
            $list = [
645
                'Code: '.$item->getCode(),
646
                'Subject: '.$item->getSubject(),
647
            ];
648
            $ticket[] = implode(', ', $list);
649
        }
650
651
        // Message
652
        $criteria = [
653
            'insertUserId' => $userId,
654
        ];
655
        $result = $em->getRepository(TicketMessage::class)->findBy($criteria);
656
        $ticketMessage = [];
657
        /** @var TicketMessage $item */
658
        foreach ($result as $item) {
659
            $date = $item->getInsertDateTime()->format($dateFormat);
660
            $list = [
661
                'Subject: '.$item->getSubject(),
662
                'IP: '.$item->getIpAddress(),
663
                'Status: '.$item->getStatus(),
664
                'Creation date: '.$date,
665
            ];
666
            $ticketMessage[] = implode(', ', $list);
667
        }
668
669
        // SkillRelUserComment
670
        $criteria = [
671
            'feedbackGiver' => $userId,
672
        ];
673
        $result = $em->getRepository(SkillRelUserComment::class)->findBy($criteria);
674
        $skillRelUserComment = [];
675
        /** @var SkillRelUserComment $item */
676
        foreach ($result as $item) {
677
            $date = $item->getFeedbackDateTime()->format($dateFormat);
678
            $list = [
679
                'Feedback: '.$item->getFeedbackText(),
680
                'Value: '.$item->getFeedbackValue(),
681
                'Created at: '.$date,
682
            ];
683
            $skillRelUserComment[] = implode(', ', $list);
684
        }
685
686
        // UserRelCourseVote
687
        $criteria = [
688
            'user' => $userId,
689
        ];
690
        $result = $em->getRepository(UserRelCourseVote::class)->findBy($criteria);
691
        $userRelCourseVote = [];
692
        /** @var UserRelCourseVote $item */
693
        foreach ($result as $item) {
694
            $list = [
695
                'Course #'.$item->getCourse()->getId(),
696
                //'Session #'.$item->getSession()->getId(),
697
                'Vote: '.$item->getVote(),
698
            ];
699
            $userRelCourseVote[] = implode(', ', $list);
700
        }
701
702
        /*$user->setDropBoxSentFiles(
703
            [
704
                'Friends' => $friendList,
705
                'Events' => $eventList,
706
                'GradebookCertificate' => $gradebookCertificate,
707
708
                'TrackECourseAccess' => $trackECourseAccessList,
709
                'TrackELogin' => $trackELoginList,
710
                'TrackEAccess' => $trackEAccessList,
711
                'TrackEDefault' => $trackEDefault,
712
                'TrackEOnline' => $trackEOnlineList,
713
                'TrackEUploads' => $trackEUploads,
714
                'TrackELastaccess' => $trackELastaccess,
715
                'GradebookResult' => $gradebookResult,
716
                'Downloads' => $trackEDownloads,
717
                'UserCourseCategory' => $userCourseCategory,
718
                'SkillRelUserComment' => $skillRelUserComment,
719
                'UserRelCourseVote' => $userRelCourseVote,
720
721
                // courses
722
                'AttendanceResult' => $cAttendanceResult,
723
                'Blog' => $cBlog,
724
                'DocumentsAdded' => $documents,
725
                'Chat' => $chatFiles,
726
                'ForumPost' => $cForumPostList,
727
                'ForumThread' => $cForumThreadList,
728
                'TrackEExercises' => $trackEExercises,
729
                'TrackEAttempt' => $trackEAttempt,
730
731
                'GroupRelUser' => $cGroupRelUser,
732
                'Message' => $messageList,
733
                'Survey' => $cSurveyAnswer,
734
                'StudentPublication' => $cStudentPublication,
735
                'StudentPublicationComment' => $cStudentPublicationComment,
736
                'DropboxFile' => $cDropboxFile,
737
                'DropboxPerson' => $cDropboxPerson,
738
                'DropboxFeedback' => $cDropboxFeedback,
739
740
                'LpView' => $cLpView,
741
                'Notebook' => $cNotebook,
742
743
                'Wiki' => $cWiki,
744
                // Tickets
745
746
                'Ticket' => $ticket,
747
                'TicketMessage' => $ticketMessage,
748
            ]
749
        );*/
750
751
        //$user->setDropBoxReceivedFiles([]);
752
        //$user->setGroups([]);
753
        //$user->setCurriculumItems([]);
754
755
        /*$portals = $user->getPortals();
756
        if (!empty($portals)) {
757
            $list = [];
758
            /** @var AccessUrlRelUser $portal */
759
        /*foreach ($portals as $portal) {
760
            $portalInfo = UrlManager::get_url_data_from_id($portal->getUrl()->getId());
761
            $list[] = $portalInfo['url'];
762
        }
763
        }
764
        $user->setPortals($list);*/
765
766
        /*$skillRelUserList = $user->getAchievedSkills();
767
        $list = [];
768
        foreach ($skillRelUserList as $skillRelUser) {
769
            $list[] = $skillRelUser->getSkill()->getName();
770
        }
771
        $user->setAchievedSkills($list);
772
        $user->setCommentedUserSkills([]);*/
773
774
        //$extraFieldValues = new \ExtraFieldValue('user');
775
776
        $lastLogin = $user->getLastLogin();
777
        if (null === $lastLogin) {
778
            $login = $this->userRepository->getLastLogin($user);
779
            if (null !== $login) {
780
                $lastLogin = $login->getLoginDate();
781
            }
782
        }
783
        $user->setLastLogin($lastLogin);
784
785
        /*$dateNormalizer = new GetSetMethodNormalizer();
786
        $dateNormalizer->setCircularReferenceHandler(function ($object) {
787
            return get_class($object);
788
        });*/
789
790
        $ignore = [
791
            'twoStepVerificationCode',
792
            'biography',
793
            'dateOfBirth',
794
            'gender',
795
            'facebookData',
796
            'facebookName',
797
            'facebookUid',
798
            'gplusData',
799
            'gplusName',
800
            'gplusUid',
801
            'locale',
802
            'timezone',
803
            'twitterData',
804
            'twitterName',
805
            'twitterUid',
806
            'gplusUid',
807
            'token',
808
            'website',
809
            'plainPassword',
810
            'completeNameWithUsername',
811
            'completeName',
812
            'completeNameWithClasses',
813
            'salt',
814
            'dropBoxSentFiles',
815
            'dropBoxReceivedFiles',
816
            'currentUrl',
817
            'uuid',
818
            'curriculumItems',
819
            'currentSession',
820
            'currentCourse',
821
            'resourceNode',
822
        ];
823
824
        $callback = function ($dateTime) {
825
            return $dateTime instanceof DateTime ? $dateTime->format(DateTimeInterface::ATOM) : '';
826
        };
827
828
        $defaultContext = [
829
            AbstractNormalizer::CALLBACKS => [
830
                'createdAt' => $callback,
831
                'lastLogin' => $callback,
832
                'registrationDate' => $callback,
833
                'memberSince' => $callback,
834
            ],
835
            AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER => function ($object, $format, $context) {
836
                return \get_class($object);
837
            },
838
        ];
839
840
        $normalizer = new GetSetMethodNormalizer(null, null, null, null, null, $defaultContext);
841
        $serializer = new Serializer(
842
            [$normalizer],
843
            [new JsonEncoder()]
844
        );
845
846
        return $serializer->serialize($user, 'json', [
847
            AbstractNormalizer::IGNORED_ATTRIBUTES => $ignore,
848
        ]);
849
    }
850
}
851