Completed
Push — master ( 38accd...2bf9cf )
by Julito
10:36
created

ExternalTool::getCustomParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\LtiBundle\Entity;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
use Doctrine\Common\Collections\ArrayCollection;
9
use Doctrine\ORM\Mapping as ORM;
10
11
/**
12
 * Class ExternalTool.
13
 *
14
 *
15
 * @ORM\Table(name="lti_external_tool")
16
 * @ORM\Entity()
17
 */
18
class ExternalTool
19
{
20
    /**
21
     * @var int
22
     *
23
     * @ORM\Column(name="id", type="integer")
24
     * @ORM\Id
25
     * @ORM\GeneratedValue
26
     */
27
    protected $id;
28
    /**
29
     * @var string
30
     *
31
     * @ORM\Column(name="name", type="string")
32
     */
33
    private $name = '';
34
    /**
35
     * @var string|null
36
     *
37
     * @ORM\Column(name="description", type="text", nullable=true)
38
     */
39
    private $description = null;
40
    /**
41
     * @var string
42
     *
43
     * @ORM\Column(name="launch_url", type="string")
44
     */
45
    private $launchUrl = '';
46
    /**
47
     * @var string
48
     *
49
     * @ORM\Column(name="consumer_key", type="string", nullable=true)
50
     */
51
    private $consumerKey = '';
52
    /**
53
     * @var string
54
     *
55
     * @ORM\Column(name="shared_secret", type="string", nullable=true)
56
     */
57
    private $sharedSecret = '';
58
    /**
59
     * @var string|null
60
     *
61
     * @ORM\Column(name="custom_params", type="text", nullable=true)
62
     */
63
    private $customParams = null;
64
    /**
65
     * @var bool
66
     *
67
     * @ORM\Column(name="active_deep_linking", type="boolean", nullable=false, options={"default": false})
68
     */
69
    private $activeDeepLinking = false;
70
    /**
71
     * @var string|null
72
     *
73
     * @ORM\Column(name="privacy", type="text", nullable=true, options={"default": null})
74
     */
75
    private $privacy = null;
76
    /**
77
     * @var Course|null
78
     *
79
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\Course")
80
     * @ORM\JoinColumn(name="c_id", referencedColumnName="id")
81
     */
82
    private $course = null;
83
    /**
84
     * @var GradebookEvaluation|null
85
     *
86
     * @ORM\ManyToOne(targetEntity="Chamilo\CoreBundle\Entity\GradebookEvaluation")
87
     * @ORM\JoinColumn(name="gradebook_eval_id", referencedColumnName="id", onDelete="SET NULL")
88
     */
89
    private $gradebookEval = null;
90
    /**
91
     * @var ExternalTool|null
92
     *
93
     * @ORM\ManyToOne(targetEntity="Chamilo\LtiBundle\Entity\ExternalTool", inversedBy="children")
94
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
95
     */
96
    private $parent;
97
    /**
98
     * @var ArrayCollection
99
     *
100
     * @ORM\OneToMany(targetEntity="Chamilo\LtiBundle\Entity\ExternalTool", mappedBy="parent")
101
     */
102
    private $children;
103
104
    /**
105
     * ExternalTool constructor.
106
     */
107
    public function __construct()
108
    {
109
        $this->description = null;
110
        $this->customParams = null;
111
        $this->activeDeepLinking = false;
112
        $this->course = null;
113
        $this->gradebookEval = null;
114
        $this->privacy = null;
115
        $this->consumerKey = null;
116
        $this->sharedSecret = null;
117
        $this->parent = null;
118
        $this->children = new ArrayCollection();
119
    }
120
121
    public function __clone()
122
    {
123
        $this->id = 0;
124
    }
125
126
    /**
127
     * @return int
128
     */
129
    public function getId()
130
    {
131
        return $this->id;
132
    }
133
134
    /**
135
     * @param int $id
136
     *
137
     * @return $this
138
     */
139
    public function setId($id)
140
    {
141
        $this->id = $id;
142
143
        return $this;
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function getName()
150
    {
151
        return $this->name;
152
    }
153
154
    /**
155
     * @param string $name
156
     *
157
     * @return ExternalTool
158
     */
159
    public function setName($name)
160
    {
161
        $this->name = $name;
162
163
        return $this;
164
    }
165
166
    /**
167
     * @return string|null
168
     */
169
    public function getDescription()
170
    {
171
        return $this->description;
172
    }
173
174
    /**
175
     * @param string|null $description
176
     *
177
     * @return ExternalTool
178
     */
179
    public function setDescription($description)
180
    {
181
        $this->description = $description;
182
183
        return $this;
184
    }
185
186
    /**
187
     * @return string
188
     */
189
    public function getLaunchUrl()
190
    {
191
        return $this->launchUrl;
192
    }
193
194
    /**
195
     * @param string $launchUrl
196
     *
197
     * @return ExternalTool
198
     */
199
    public function setLaunchUrl($launchUrl)
200
    {
201
        $this->launchUrl = $launchUrl;
202
203
        return $this;
204
    }
205
206
    /**
207
     * @return string
208
     */
209
    public function getConsumerKey()
210
    {
211
        return $this->consumerKey;
212
    }
213
214
    /**
215
     * @param string $consumerKey
216
     *
217
     * @return ExternalTool
218
     */
219
    public function setConsumerKey($consumerKey)
220
    {
221
        $this->consumerKey = $consumerKey;
222
223
        return $this;
224
    }
225
226
    /**
227
     * @return string
228
     */
229
    public function getSharedSecret()
230
    {
231
        return $this->sharedSecret;
232
    }
233
234
    /**
235
     * @param string $sharedSecret
236
     *
237
     * @return ExternalTool
238
     */
239
    public function setSharedSecret($sharedSecret)
240
    {
241
        $this->sharedSecret = $sharedSecret;
242
243
        return $this;
244
    }
245
246
    /**
247
     * @return string|null
248
     */
249
    public function getCustomParams()
250
    {
251
        return $this->customParams;
252
    }
253
254
    /**
255
     * @param string|null $customParams
256
     *
257
     * @return ExternalTool
258
     */
259
    public function setCustomParams($customParams)
260
    {
261
        $this->customParams = $customParams;
262
263
        return $this;
264
    }
265
266
    /**
267
     * @return bool
268
     */
269
    public function isGlobal()
270
    {
271
        return null === $this->course;
272
    }
273
274
    /**
275
     * @return string|null
276
     */
277
    public function encodeCustomParams(array $params)
278
    {
279
        if (empty($params)) {
280
            return null;
281
        }
282
        $pairs = [];
283
        foreach ($params as $key => $value) {
284
            $pairs[] = "$key=$value";
285
        }
286
287
        return implode("\n", $pairs);
288
    }
289
290
    /**
291
     * @return array
292
     */
293
    public function parseCustomParams()
294
    {
295
        if (empty($this->customParams)) {
296
            return [];
297
        }
298
        $params = [];
299
        $strings = explode("\n", $this->customParams);
300
        foreach ($strings as $string) {
301
            if (empty($string)) {
302
                continue;
303
            }
304
            $pairs = explode('=', $string, 2);
305
            $key = self::parseCustomKey($pairs[0]);
306
            $value = $pairs[1];
307
            $params['custom_'.$key] = $value;
308
        }
309
310
        return $params;
311
    }
312
313
    /**
314
     * Get activeDeepLinking.
315
     *
316
     * @return bool
317
     */
318
    public function isActiveDeepLinking()
319
    {
320
        return $this->activeDeepLinking;
321
    }
322
323
    /**
324
     * Set activeDeepLinking.
325
     *
326
     * @param bool $activeDeepLinking
327
     *
328
     * @return ExternalTool
329
     */
330
    public function setActiveDeepLinking($activeDeepLinking)
331
    {
332
        $this->activeDeepLinking = $activeDeepLinking;
333
334
        return $this;
335
    }
336
337
    /**
338
     * Get course.
339
     *
340
     * @return Course|null
341
     */
342
    public function getCourse()
343
    {
344
        return $this->course;
345
    }
346
347
    /**
348
     * Set course.
349
     *
350
     * @return ExternalTool
351
     */
352
    public function setCourse(Course $course = null)
353
    {
354
        $this->course = $course;
355
356
        return $this;
357
    }
358
359
    /**
360
     * Get gradebookEval.
361
     *
362
     * @return GradebookEvaluation|null
363
     */
364
    public function getGradebookEval()
365
    {
366
        return $this->gradebookEval;
367
    }
368
369
    /**
370
     * Set gradebookEval.
371
     *
372
     * @param GradebookEvaluation|null $gradebookEval
373
     *
374
     * @return ExternalTool
375
     */
376
    public function setGradebookEval($gradebookEval)
377
    {
378
        $this->gradebookEval = $gradebookEval;
379
380
        return $this;
381
    }
382
383
    /**
384
     * Get privacy.
385
     *
386
     * @return string|null
387
     */
388
    public function getPrivacy()
389
    {
390
        return $this->privacy;
391
    }
392
393
    /**
394
     * Set privacy.
395
     *
396
     * @param bool $shareName
397
     * @param bool $shareEmail
398
     * @param bool $sharePicture
399
     *
400
     * @return ExternalTool
401
     */
402
    public function setPrivacy($shareName = false, $shareEmail = false, $sharePicture = false)
403
    {
404
        $this->privacy = serialize(
405
            [
406
                'share_name' => $shareName,
407
                'share_email' => $shareEmail,
408
                'share_picture' => $sharePicture,
409
            ]
410
        );
411
412
        return $this;
413
    }
414
415
    /**
416
     * @return bool
417
     */
418
    public function isSharingName()
419
    {
420
        $unserialize = $this->unserializePrivacy();
421
422
        return (bool) $unserialize['share_name'];
423
    }
424
425
    /**
426
     * @return mixed
427
     */
428
    public function unserializePrivacy()
429
    {
430
        return unserialize($this->privacy);
431
    }
432
433
    /**
434
     * @return bool
435
     */
436
    public function isSharingEmail()
437
    {
438
        $unserialize = $this->unserializePrivacy();
439
440
        return (bool) $unserialize['share_email'];
441
    }
442
443
    /**
444
     * @return bool
445
     */
446
    public function isSharingPicture()
447
    {
448
        $unserialize = $this->unserializePrivacy();
449
450
        return (bool) $unserialize['share_picture'];
451
    }
452
453
    /**
454
     * @return ExternalTool|null
455
     */
456
    public function getParent()
457
    {
458
        return $this->parent;
459
    }
460
461
    /**
462
     * @return ExternalTool
463
     */
464
    public function setParent(self $parent)
465
    {
466
        $this->parent = $parent;
467
        $this->sharedSecret = $parent->getSharedSecret();
468
        $this->consumerKey = $parent->getConsumerKey();
469
        $this->privacy = $parent->getPrivacy();
470
471
        return $this;
472
    }
473
474
    /**
475
     * Map the key from custom param.
476
     *
477
     * @param string $key
478
     *
479
     * @return string
480
     */
481
    private static function parseCustomKey($key)
482
    {
483
        $newKey = '';
484
        $key = strtolower($key);
485
        $split = str_split($key);
486
        foreach ($split as $char) {
487
            if (
488
                ($char >= 'a' && $char <= 'z') || ($char >= '0' && $char <= '9')
489
            ) {
490
                $newKey .= $char;
491
492
                continue;
493
            }
494
            $newKey .= '_';
495
        }
496
497
        return $newKey;
498
    }
499
500
    /**
501
     * @return ArrayCollection
502
     */
503
    public function getChildren(): ArrayCollection
504
    {
505
        return $this->children;
506
    }
507
508
    /**
509
     * @param ArrayCollection $children
510
     *
511
     * @return ExternalTool
512
     */
513
    public function setChildren(ArrayCollection $children): self
514
    {
515
        $this->children = $children;
516
517
        return $this;
518
    }
519
}
520