Passed
Push — 1.11.x ( cf7018...7ebf39 )
by Angel Fernando Quiroz
10:11
created

ImsLtiTool::filterSpecialChars()   A

Complexity

Conditions 6
Paths 3

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 19
rs 9.2222
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\PluginBundle\Entity\ImsLti;
5
6
use Chamilo\CoreBundle\Entity\Course;
7
use Chamilo\CoreBundle\Entity\GradebookEvaluation;
8
use Chamilo\CoreBundle\Entity\Session;
9
use Doctrine\Common\Collections\ArrayCollection;
10
use Doctrine\Common\Collections\Criteria;
11
use Doctrine\ORM\Mapping as ORM;
12
13
/**
14
 * Class ImsLtiTool.
15
 *
16
 * @ORM\Table(name="plugin_ims_lti_tool")
17
 * @ORM\Entity()
18
 */
19
class ImsLtiTool
20
{
21
    /**
22
     * @var string|null
23
     *
24
     * @ORM\Column(name="public_key", type="text", nullable=true)
25
     */
26
    public $publicKey;
27
    /**
28
     * @var int
29
     *
30
     * @ORM\Column(name="id", type="integer")
31
     * @ORM\Id
32
     * @ORM\GeneratedValue
33
     */
34
    protected $id;
35
    /**
36
     * @var string
37
     *
38
     * @ORM\Column(name="name", type="string")
39
     */
40
    private $name = '';
41
    /**
42
     * @var string|null
43
     *
44
     * @ORM\Column(name="description", type="text", nullable=true)
45
     */
46
    private $description = null;
47
    /**
48
     * @var string
49
     *
50
     * @ORM\Column(name="launch_url", type="string")
51
     */
52
    private $launchUrl = '';
53
    /**
54
     * @var string
55
     *
56
     * @ORM\Column(name="consumer_key", type="string", nullable=true)
57
     */
58
    private $consumerKey = '';
59
    /**
60
     * @var string
61
     *
62
     * @ORM\Column(name="shared_secret", type="string", nullable=true)
63
     */
64
    private $sharedSecret = '';
65
    /**
66
     * @var string|null
67
     *
68
     * @ORM\Column(name="custom_params", type="text", nullable=true)
69
     */
70
    private $customParams = null;
71
    /**
72
     * @var bool
73
     *
74
     * @ORM\Column(name="active_deep_linking", type="boolean", nullable=false, options={"default": false})
75
     */
76
    private $activeDeepLinking = false;
77
    /**
78
     * @var string|null
79
     *
80
     * @ORM\Column(name="privacy", type="text", nullable=true, options={"default": null})
81
     */
82
    private $privacy = null;
83
    /**
84
     * @var Course|null
85
     *
86
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course")
87
     * @ORM\JoinColumn(name="c_id", referencedColumnName="id")
88
     */
89
    private $course = null;
90
    /**
91
     * @var Session|null
92
     *
93
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Session")
94
     * @ORM\JoinColumn(name="session_id", referencedColumnName="id")
95
     */
96
    private $session = null;
97
    /**
98
     * @var GradebookEvaluation|null
99
     *
100
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\GradebookEvaluation")
101
     * @ORM\JoinColumn(name="gradebook_eval_id", referencedColumnName="id", onDelete="SET NULL")
102
     */
103
    private $gradebookEval = null;
104
    /**
105
     * @var ImsLtiTool|null
106
     *
107
     * @ORM\ManyToOne(targetEntity="Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool", inversedBy="children")
108
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
109
     */
110
    private $parent;
111
    /**
112
     * @var ArrayCollection
113
     *
114
     * @ORM\OneToMany(targetEntity="Chamilo\PluginBundle\Entity\ImsLti\ImsLtiTool", mappedBy="parent")
115
     */
116
    private $children;
117
    /**
118
     * @var string
119
     *
120
     * @ORM\Column(name="client_id", type="string", nullable=true)
121
     */
122
    private $clientId;
123
    /**
124
     * @var string|null
125
     *
126
     * @ORM\Column(name="login_url", type="string", nullable=true)
127
     */
128
    private $loginUrl;
129
130
    /**
131
     * @var string|null
132
     *
133
     * @ORM\Column(name="redirect_url", type="string", nullable=true)
134
     */
135
    private $redirectUrl;
136
137
    /**
138
     * @var array
139
     *
140
     * @ORM\Column(name="advantage_services", type="json", nullable=true)
141
     */
142
    private $advantageServices;
143
144
    /**
145
     * @var ArrayCollection
146
     *
147
     * @ORM\OneToMany(targetEntity="Chamilo\PluginBundle\Entity\ImsLti\LineItem", mappedBy="tool")
148
     */
149
    private $lineItems;
150
151
    /**
152
     * @var string
153
     *
154
     * @ORM\Column(name="version", type="string", options={"default": "lti1p1"})
155
     */
156
    private $version;
157
    /**
158
     * @var array
159
     *
160
     * @ORM\Column(name="launch_presentation", type="json")
161
     */
162
    private $launchPresentation;
163
164
    /**
165
     * @var array
166
     *
167
     * @ORM\Column(name="replacement_params", type="json")
168
     */
169
    private $replacementParams;
170
171
    /**
172
     * ImsLtiTool constructor.
173
     */
174
    public function __construct()
175
    {
176
        $this->description = null;
177
        $this->customParams = null;
178
        $this->activeDeepLinking = false;
179
        $this->course = null;
180
        $this->gradebookEval = null;
181
        $this->privacy = null;
182
        $this->children = new ArrayCollection();
183
        $this->consumerKey = null;
184
        $this->sharedSecret = null;
185
        $this->lineItems = new ArrayCollection();
186
        $this->version = \ImsLti::V_1P1;
187
        $this->launchPresentation = [
188
            'document_target' => 'iframe',
189
        ];
190
        $this->replacementParams = [];
191
    }
192
193
    /**
194
     * @return int
195
     */
196
    public function getId()
197
    {
198
        return $this->id;
199
    }
200
201
    /**
202
     * @return string
203
     */
204
    public function getName()
205
    {
206
        return $this->name;
207
    }
208
209
    /**
210
     * @param string $name
211
     *
212
     * @return ImsLtiTool
213
     */
214
    public function setName($name)
215
    {
216
        $this->name = $name;
217
218
        return $this;
219
    }
220
221
    /**
222
     * @return string|null
223
     */
224
    public function getDescription()
225
    {
226
        return $this->description;
227
    }
228
229
    /**
230
     * @param string|null $description
231
     *
232
     * @return ImsLtiTool
233
     */
234
    public function setDescription($description)
235
    {
236
        $this->description = $description;
237
238
        return $this;
239
    }
240
241
    /**
242
     * @return string
243
     */
244
    public function getLaunchUrl()
245
    {
246
        return $this->launchUrl;
247
    }
248
249
    /**
250
     * @param string $launchUrl
251
     *
252
     * @return ImsLtiTool
253
     */
254
    public function setLaunchUrl($launchUrl)
255
    {
256
        $this->launchUrl = $launchUrl;
257
258
        return $this;
259
    }
260
261
    /**
262
     * @return string|null
263
     */
264
    public function getCustomParams()
265
    {
266
        return $this->customParams;
267
    }
268
269
    /**
270
     * @param string|null $customParams
271
     *
272
     * @return ImsLtiTool
273
     */
274
    public function setCustomParams($customParams)
275
    {
276
        $this->customParams = $customParams;
277
278
        return $this;
279
    }
280
281
    /**
282
     * @return bool
283
     */
284
    public function isGlobal()
285
    {
286
        return $this->course === null;
287
    }
288
289
    /**
290
     * @return string|null
291
     */
292
    public function encodeCustomParams(array $params)
293
    {
294
        if (empty($params)) {
295
            return null;
296
        }
297
298
        $pairs = [];
299
300
        foreach ($params as $key => $value) {
301
            $pairs[] = "$key=$value";
302
        }
303
304
        return implode("\n", $pairs);
305
    }
306
307
    /**
308
     * @return array
309
     */
310
    public function getCustomParamsAsArray()
311
    {
312
        $params = [];
313
        $lines = explode("\n", $this->customParams);
314
        $lines = array_filter($lines);
315
316
        foreach ($lines as $line) {
317
            list($key, $value) = explode('=', $line, 2);
318
319
            $key = self::filterSpecialChars($key);
320
            $value = self::filterSpaces($value);
321
322
            $params[$key] = $value;
323
        }
324
325
        return $params;
326
    }
327
328
    /**
329
     * @return array
330
     */
331
    public function parseCustomParams()
332
    {
333
        if (empty($this->customParams)) {
334
            return [];
335
        }
336
337
        $params = [];
338
        $strings = explode("\n", $this->customParams);
339
340
        foreach ($strings as $string) {
341
            if (empty($string)) {
342
                continue;
343
            }
344
345
            $pairs = explode('=', $string, 2);
346
            $key = self::filterSpecialChars($pairs[0]);
347
            $value = $pairs[1];
348
349
            $params['custom_'.$key] = $value;
350
        }
351
352
        return $params;
353
    }
354
355
    /**
356
     * Get activeDeepLinking.
357
     *
358
     * @return bool
359
     */
360
    public function isActiveDeepLinking()
361
    {
362
        return $this->activeDeepLinking;
363
    }
364
365
    /**
366
     * Set activeDeepLinking.
367
     *
368
     * @param bool $activeDeepLinking
369
     *
370
     * @return ImsLtiTool
371
     */
372
    public function setActiveDeepLinking($activeDeepLinking)
373
    {
374
        $this->activeDeepLinking = $activeDeepLinking;
375
376
        return $this;
377
    }
378
379
    /**
380
     * Get course.
381
     *
382
     * @return Course|null
383
     */
384
    public function getCourse()
385
    {
386
        return $this->course;
387
    }
388
389
    /**
390
     * Set course.
391
     *
392
     * @return ImsLtiTool
393
     */
394
    public function setCourse(Course $course = null)
395
    {
396
        $this->course = $course;
397
398
        return $this;
399
    }
400
401
    /**
402
     * Get session.
403
     *
404
     * @return Session|null
405
     */
406
    public function getSession()
407
    {
408
        return $this->session;
409
    }
410
411
    /**
412
     * Set session.
413
     *
414
     * @param Session|null $course
415
     *
416
     * @return ImsLtiTool
417
     */
418
    public function setSession(Session $session = null)
419
    {
420
        $this->session = $session;
421
422
        return $this;
423
    }
424
425
    /**
426
     * Get gradebookEval.
427
     *
428
     * @return GradebookEvaluation|null
429
     */
430
    public function getGradebookEval()
431
    {
432
        return $this->gradebookEval;
433
    }
434
435
    /**
436
     * Set gradebookEval.
437
     *
438
     * @param GradebookEvaluation|null $gradebookEval
439
     *
440
     * @return ImsLtiTool
441
     */
442
    public function setGradebookEval($gradebookEval)
443
    {
444
        $this->gradebookEval = $gradebookEval;
445
446
        return $this;
447
    }
448
449
    /**
450
     * @return bool
451
     */
452
    public function isSharingName()
453
    {
454
        $unserialize = $this->unserializePrivacy();
455
456
        return (bool) $unserialize['share_name'];
457
    }
458
459
    /**
460
     * @return mixed
461
     */
462
    public function unserializePrivacy()
463
    {
464
        return \UnserializeApi::unserialize('not_allowed_classes', $this->privacy);
465
    }
466
467
    /**
468
     * @return bool
469
     */
470
    public function isSharingEmail()
471
    {
472
        $unserialize = $this->unserializePrivacy();
473
474
        return (bool) $unserialize['share_email'];
475
    }
476
477
    /**
478
     * @return bool
479
     */
480
    public function isSharingPicture()
481
    {
482
        $unserialize = $this->unserializePrivacy();
483
484
        return (bool) $unserialize['share_picture'];
485
    }
486
487
    /**
488
     * @return ImsLtiTool|null
489
     */
490
    public function getParent()
491
    {
492
        return $this->parent;
493
    }
494
495
    /**
496
     * @return ImsLtiTool
497
     */
498
    public function setParent(ImsLtiTool $parent)
499
    {
500
        $this->parent = $parent;
501
502
        $this->sharedSecret = $parent->getSharedSecret();
503
        $this->consumerKey = $parent->getConsumerKey();
504
        $this->privacy = $parent->getPrivacy();
505
506
        return $this;
507
    }
508
509
    /**
510
     * @return string
511
     */
512
    public function getSharedSecret()
513
    {
514
        return $this->sharedSecret;
515
    }
516
517
    /**
518
     * @param string $sharedSecret
519
     *
520
     * @return ImsLtiTool
521
     */
522
    public function setSharedSecret($sharedSecret)
523
    {
524
        $this->sharedSecret = $sharedSecret;
525
526
        return $this;
527
    }
528
529
    /**
530
     * @return string
531
     */
532
    public function getConsumerKey()
533
    {
534
        return $this->consumerKey;
535
    }
536
537
    /**
538
     * @param string $consumerKey
539
     *
540
     * @return ImsLtiTool
541
     */
542
    public function setConsumerKey($consumerKey)
543
    {
544
        $this->consumerKey = $consumerKey;
545
546
        return $this;
547
    }
548
549
    /**
550
     * Get privacy.
551
     *
552
     * @return string|null
553
     */
554
    public function getPrivacy()
555
    {
556
        return $this->privacy;
557
    }
558
559
    /**
560
     * Set privacy.
561
     *
562
     * @param bool $shareName
563
     * @param bool $shareEmail
564
     * @param bool $sharePicture
565
     *
566
     * @return ImsLtiTool
567
     */
568
    public function setPrivacy($shareName = false, $shareEmail = false, $sharePicture = false)
569
    {
570
        $this->privacy = serialize(
571
            [
572
                'share_name' => $shareName,
573
                'share_email' => $shareEmail,
574
                'share_picture' => $sharePicture,
575
            ]
576
        );
577
578
        return $this;
579
    }
580
581
    /**
582
     * Get loginUrl.
583
     *
584
     * @return string|null
585
     */
586
    public function getLoginUrl()
587
    {
588
        return $this->loginUrl;
589
    }
590
591
    /**
592
     * Set loginUrl.
593
     *
594
     * @param string|null $loginUrl
595
     *
596
     * @return ImsLtiTool
597
     */
598
    public function setLoginUrl($loginUrl)
599
    {
600
        $this->loginUrl = $loginUrl;
601
602
        return $this;
603
    }
604
605
    /**
606
     * Get redirectUlr.
607
     *
608
     * @return string|null
609
     */
610
    public function getRedirectUrl()
611
    {
612
        return $this->redirectUrl;
613
    }
614
615
    /**
616
     * Set redirectUrl.
617
     *
618
     * @param string|null $redirectUrl
619
     *
620
     * @return ImsLtiTool
621
     */
622
    public function setRedirectUrl($redirectUrl)
623
    {
624
        $this->redirectUrl = $redirectUrl;
625
626
        return $this;
627
    }
628
629
    /**
630
     * Get clientId.
631
     *
632
     * @return string
633
     */
634
    public function getClientId()
635
    {
636
        return $this->clientId;
637
    }
638
639
    /**
640
     * Set clientId.
641
     *
642
     * @param string $clientId
643
     *
644
     * @return ImsLtiTool
645
     */
646
    public function setClientId($clientId)
647
    {
648
        $this->clientId = $clientId;
649
650
        return $this;
651
    }
652
653
    /**
654
     * Get advantageServices.
655
     *
656
     * @return array
657
     */
658
    public function getAdvantageServices()
659
    {
660
        if (empty($this->advantageServices)) {
661
            $this->advantageServices = [];
662
        }
663
664
        return array_merge(
665
            [
666
                'ags' => \LtiAssignmentGradesService::AGS_NONE,
667
                'nrps' => \LtiNamesRoleProvisioningService::NRPS_NONE,
668
            ],
669
            $this->advantageServices
670
        );
671
    }
672
673
    /**
674
     * Set advantageServices.
675
     *
676
     * @param array $advantageServices
677
     *
678
     * @return ImsLtiTool
679
     */
680
    public function setAdvantageServices($advantageServices)
681
    {
682
        $this->advantageServices = $advantageServices;
683
684
        return $this;
685
    }
686
687
    /**
688
     * Add LineItem to lineItems.
689
     *
690
     * @return $this
691
     */
692
    public function addLineItem(LineItem $lineItem)
693
    {
694
        $lineItem->setTool($this);
695
696
        $this->lineItems[] = $lineItem;
697
698
        return $this;
699
    }
700
701
    /**
702
     * @param int    $resourceLinkId
703
     * @param int    $resourceId
704
     * @param string $tag
705
     * @param int    $limit
706
     * @param int    $page
707
     *
708
     * @return ArrayCollection
709
     */
710
    public function getLineItems($resourceLinkId = 0, $resourceId = 0, $tag = '', $limit = 0, $page = 1)
711
    {
712
        $criteria = Criteria::create();
713
714
        if ($resourceLinkId) {
715
            $criteria->andWhere(Criteria::expr()->eq('tool', $resourceId));
716
        }
717
718
        if ($resourceId) {
719
            $criteria->andWhere(Criteria::expr()->eq('tool', $resourceId));
720
        }
721
722
        if (!empty($tag)) {
723
            $criteria->andWhere(Criteria::expr()->eq('tag', $tag));
724
        }
725
726
        $limit = (int) $limit;
727
        $page = (int) $page;
728
729
        if ($limit > 0) {
730
            $criteria->setMaxResults($limit);
731
732
            if ($page > 0) {
733
                $criteria->setFirstResult($page * $limit);
734
            }
735
        }
736
737
        return $this->lineItems->matching($criteria);
738
    }
739
740
    /**
741
     * Set lineItems.
742
     *
743
     * @return $this
744
     */
745
    public function setLineItems(ArrayCollection $lineItems)
746
    {
747
        $this->lineItems = $lineItems;
748
749
        return $this;
750
    }
751
752
    /**
753
     * Get version.
754
     *
755
     * @return string
756
     */
757
    public function getVersion()
758
    {
759
        return $this->version;
760
    }
761
762
    /**
763
     * Set version.
764
     *
765
     * @param string $version
766
     *
767
     * @return ImsLtiTool
768
     */
769
    public function setVersion($version)
770
    {
771
        $this->version = $version;
772
773
        return $this;
774
    }
775
776
    /**
777
     * @return string
778
     */
779
    public function getVersionName()
780
    {
781
        if (\ImsLti::V_1P1 === $this->version) {
782
            return 'LTI 1.0 / 1.1';
783
        }
784
785
        return 'LTI 1.3';
786
    }
787
788
    /**
789
     * @return ArrayCollection
790
     */
791
    public function getChildren()
792
    {
793
        return $this->children;
794
    }
795
796
    /**
797
     * @param string $target
798
     *
799
     * @return $this
800
     */
801
    public function setDocumenTarget($target)
802
    {
803
        $this->launchPresentation['document_target'] = in_array($target, ['iframe', 'window']) ? $target : 'iframe';
804
805
        return $this;
806
    }
807
808
    /**
809
     * @return string
810
     */
811
    public function getDocumentTarget()
812
    {
813
        return $this->launchPresentation['document_target'] ?: 'iframe';
814
    }
815
816
    /**
817
     * @return array
818
     */
819
    public function getLaunchPresentation()
820
    {
821
        return $this->launchPresentation;
822
    }
823
824
    /**
825
     * @param string $replacement
826
     *
827
     * @return ImsLtiTool
828
     */
829
    public function setReplacementForUserId($replacement)
830
    {
831
        $this->replacementParams['user_id'] = $replacement;
832
833
        return $this;
834
    }
835
836
    /**
837
     * @return string|null
838
     */
839
    public function getReplacementForUserId()
840
    {
841
        if (!empty($this->replacementParams['user_id'])) {
842
            return $this->replacementParams['user_id'];
843
        }
844
845
        return null;
846
    }
847
848
    /**
849
     * @return ArrayCollection
850
     */
851
    public function getChildrenInCourses(array $coursesId)
852
    {
853
        return $this->children->filter(
854
            function (ImsLtiTool $child) use ($coursesId) {
855
                return in_array($child->getCourse()->getId(), $coursesId);
856
            }
857
        );
858
    }
859
860
    /**
861
     * Map the key from custom param.
862
     *
863
     * @param string $key
864
     *
865
     * @return string
866
     */
867
    private static function filterSpecialChars($key)
868
    {
869
        $newKey = '';
870
        $key = strtolower($key);
871
        $split = str_split($key);
872
873
        foreach ($split as $char) {
874
            if (
875
                ($char >= 'a' && $char <= 'z') || ($char >= '0' && $char <= '9')
876
            ) {
877
                $newKey .= $char;
878
879
                continue;
880
            }
881
882
            $newKey .= '_';
883
        }
884
885
        return $newKey;
886
    }
887
888
    /**
889
     * @param string $value
890
     *
891
     * @return string
892
     */
893
    private static function filterSpaces($value)
894
    {
895
        $newValue = preg_replace('/\s+/', ' ', $value);
896
897
        return trim($newValue);
898
    }
899
}
900