Completed
Push — master ( f77763...d3c71d )
by Julito
11:18
created

Container::getQuestionRepository()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Framework;
6
7
use Chamilo\CoreBundle\Component\Editor\Editor;
8
use Chamilo\CoreBundle\Manager\SettingsManager;
9
use Chamilo\CoreBundle\Repository\AccessUrlRepository;
10
use Chamilo\CoreBundle\Repository\CourseCategoryRepository;
11
use Chamilo\CoreBundle\Repository\CourseRepository;
12
use Chamilo\CoreBundle\Repository\IllustrationRepository;
13
use Chamilo\CoreBundle\Repository\SequenceRepository;
14
use Chamilo\CoreBundle\Repository\SequenceResourceRepository;
15
use Chamilo\CoreBundle\Repository\UserRepository;
16
use Chamilo\CoreBundle\ToolChain;
17
use Chamilo\CourseBundle\Repository\CAnnouncementAttachmentRepository;
18
use Chamilo\CourseBundle\Repository\CAnnouncementRepository;
19
use Chamilo\CourseBundle\Repository\CAttendanceRepository;
20
use Chamilo\CourseBundle\Repository\CCalendarEventAttachmentRepository;
21
use Chamilo\CourseBundle\Repository\CCalendarEventRepository;
22
use Chamilo\CourseBundle\Repository\CDocumentRepository;
23
use Chamilo\CourseBundle\Repository\CExerciseCategoryRepository;
24
use Chamilo\CourseBundle\Repository\CForumAttachmentRepository;
25
use Chamilo\CourseBundle\Repository\CForumCategoryRepository;
26
use Chamilo\CourseBundle\Repository\CForumForumRepository;
27
use Chamilo\CourseBundle\Repository\CForumPostRepository;
28
use Chamilo\CourseBundle\Repository\CForumThreadRepository;
29
use Chamilo\CourseBundle\Repository\CGroupRepository;
30
use Chamilo\CourseBundle\Repository\CLinkCategoryRepository;
31
use Chamilo\CourseBundle\Repository\CLinkRepository;
32
use Chamilo\CourseBundle\Repository\CLpCategoryRepository;
33
use Chamilo\CourseBundle\Repository\CLpRepository;
34
use Chamilo\CourseBundle\Repository\CQuizQuestionCategoryRepository;
35
use Chamilo\CourseBundle\Repository\CQuizQuestionRepository;
36
use Chamilo\CourseBundle\Repository\CQuizRepository;
37
use Chamilo\CourseBundle\Repository\CShortcutRepository;
38
use Chamilo\CourseBundle\Repository\CStudentPublicationAssignmentRepository;
39
use Chamilo\CourseBundle\Repository\CStudentPublicationCommentRepository;
40
use Chamilo\CourseBundle\Repository\CStudentPublicationRepository;
41
use Chamilo\CourseBundle\Repository\CThematicAdvanceRepository;
42
use Chamilo\CourseBundle\Repository\CThematicPlanRepository;
43
use Chamilo\CourseBundle\Repository\CThematicRepository;
44
use Symfony\Component\DependencyInjection\ContainerInterface;
45
use Symfony\Component\HttpFoundation\Request;
46
use Symfony\Component\HttpFoundation\Session\Session;
47
use Symfony\Component\Routing\Router;
48
use Symfony\Component\Security\Core\Authorization\AuthorizationChecker;
49
use Symfony\Component\Security\Core\Role\RoleHierarchy;
50
use Symfony\Contracts\Translation\TranslatorInterface;
51
use Twig\Environment;
52
53
/**
54
 * Class Container
55
 * This class is a way to access Symfony2 services in legacy Chamilo code.
56
 */
57
class Container
58
{
59
    /**
60
     * @var ContainerInterface
61
     */
62
    public static $container;
63
    public static $session;
64
    public static $request;
65
    public static $configuration;
66
    public static $environment;
67
    public static $urlGenerator;
68
    public static $checker;
69
    /** @var TranslatorInterface */
70
    public static $translator;
71
    public static $mailer;
72
    public static $template;
73
74
    public static $rootDir;
75
    public static $logDir;
76
    public static $tempDir;
77
    public static $dataDir;
78
    public static $courseDir;
79
    public static $assets;
80
    public static $htmlEditor;
81
    public static $twig;
82
    public static $roles;
83
    /** @var string */
84
    public static $legacyTemplate = '@ChamiloCore/Layout/layout_one_col.html.twig';
85
    //private static $settingsManager;
86
    //private static $userManager;
87
    //private static $siteManager;
88
89
    /**
90
     * @param ContainerInterface $container
91
     */
92
    public static function setContainer($container)
93
    {
94
        self::$container = $container;
95
    }
96
97
    /**
98
     * @param string $parameter
99
     */
100
    public static function getParameter($parameter)
101
    {
102
        if (self::$container->hasParameter($parameter)) {
103
            return self::$container->getParameter($parameter);
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public static function getEnvironment()
113
    {
114
        return self::$container->get('kernel')->getEnvironment();
115
    }
116
117
    /**
118
     * @return RoleHierarchy
119
     */
120
    public static function getRoles()
121
    {
122
        return self::$container->get('security.role_hierarchy');
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public static function getLogDir()
129
    {
130
        return self::$container->get('kernel')->getLogDir();
131
    }
132
133
    /**
134
     * @return string
135
     */
136
    public static function getCacheDir()
137
    {
138
        return self::$container->get('kernel')->getCacheDir().'/';
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public static function getProjectDir()
145
    {
146
        if (isset(self::$container)) {
147
            return self::$container->get('kernel')->getProjectDir().'/';
148
        }
149
150
        return str_replace('\\', '/', realpath(__DIR__.'/../../../')).'/';
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public static function isInstalled()
157
    {
158
        return self::$container->get('kernel')->isInstalled();
159
    }
160
161
    /**
162
     * @return Environment
163
     */
164
    public static function getTwig()
165
    {
166
        return self::$container->get('twig');
167
    }
168
169
    /**
170
     * @return object|Environment|null
171
     */
172
    public static function getTemplating()
173
    {
174
        return self::$container->get('twig');
175
    }
176
177
    /**
178
     * @return Editor
179
     */
180
    public static function getHtmlEditor()
181
    {
182
        return self::$container->get('chamilo_core.html_editor');
183
    }
184
185
    /**
186
     * @return object|Request
187
     */
188
    public static function getRequest()
189
    {
190
        if (null === self::$container) {
191
            return null;
192
        }
193
194
        if (!empty(self::$request)) {
195
            return self::$request;
196
        }
197
198
        return self::$container->get('request_stack');
199
    }
200
201
    /**
202
     * @param Request $request
203
     */
204
    public static function setRequest($request)
205
    {
206
        self::$request = $request;
207
    }
208
209
    /**
210
     * @return Session|false
211
     */
212
    public static function getSession()
213
    {
214
        if (self::$container && self::$container->has('session')) {
215
            return self::$container->get('session');
216
        }
217
218
        return false;
219
    }
220
221
    /**
222
     * @return AuthorizationChecker
223
     */
224
    public static function getAuthorizationChecker()
225
    {
226
        return self::$container->get('security.authorization_checker');
227
    }
228
229
    /**
230
     * @return object|\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage
231
     */
232
    public static function getTokenStorage()
233
    {
234
        return self::$container->get('security.token_storage');
235
    }
236
237
    /**
238
     * @return TranslatorInterface
239
     */
240
    public static function getTranslator()
241
    {
242
        if (isset(self::$translator)) {
243
            return self::$translator;
244
        }
245
246
        if (self::$container) {
247
            return self::$container->get('translator');
248
        }
249
250
        return false;
251
    }
252
253
    public static function getMailer()
254
    {
255
        return self::$container->get('Symfony\Component\Mailer\Mailer');
256
    }
257
258
    /**
259
     * @return SettingsManager
260
     */
261
    public static function getSettingsManager()
262
    {
263
        return self::$container->get('chamilo.settings.manager');
264
    }
265
266
    /*public static function setSettingsManager($manager)
267
    {
268
        self::$settingsManager = $manager;
269
    }*/
270
271
    /**
272
     * @return \Chamilo\CourseBundle\Manager\SettingsManager
273
     */
274
    public static function getCourseSettingsManager()
275
    {
276
        return self::$container->get('Chamilo\CourseBundle\Manager\SettingsManager');
277
    }
278
279
    /**
280
     * @return \Doctrine\ORM\EntityManager
281
     */
282
    public static function getEntityManager()
283
    {
284
        return \Database::getManager();
285
    }
286
287
    public static function getUserManager()
288
    {
289
        return self::$container->get(UserRepository::class);
290
    }
291
292
    /**
293
     * @return CAttendanceRepository
294
     */
295
    public static function getAttendanceRepository()
296
    {
297
        return self::$container->get(CAttendanceRepository::class);
298
    }
299
300
    /**
301
     * @return CAnnouncementRepository
302
     */
303
    public static function getAnnouncementRepository()
304
    {
305
        return self::$container->get(CAnnouncementRepository::class);
306
    }
307
308
    /**
309
     * @return AccessUrlRepository
310
     */
311
    public static function getAccessUrlRepository()
312
    {
313
        return self::$container->get(AccessUrlRepository::class);
314
    }
315
316
    /**
317
     * @return CAnnouncementAttachmentRepository
318
     */
319
    public static function getAnnouncementAttachmentRepository()
320
    {
321
        return self::$container->get(CAnnouncementAttachmentRepository::class);
322
    }
323
324
    /**
325
     * @return CourseRepository
326
     */
327
    public static function getCourseRepository()
328
    {
329
        return self::$container->get(CourseRepository::class);
330
    }
331
332
    /**
333
     * @return CourseCategoryRepository|object|null
334
     */
335
    public static function getCourseCategoryRepository()
336
    {
337
        return self::$container->get(CourseCategoryRepository::class);
338
    }
339
340
    /**
341
     * @return CCalendarEventRepository
342
     */
343
    public static function getCalendarEventRepository()
344
    {
345
        return self::$container->get(CCalendarEventRepository::class);
346
    }
347
348
    /**
349
     * @return CCalendarEventAttachmentRepository
350
     */
351
    public static function getCalendarEventAttachmentRepository()
352
    {
353
        return self::$container->get(CCalendarEventAttachmentRepository::class);
354
    }
355
356
    /**
357
     * @return CDocumentRepository
358
     */
359
    public static function getDocumentRepository()
360
    {
361
        return self::$container->get(CDocumentRepository::class);
362
    }
363
364
    /**
365
     * @return CQuizRepository
366
     */
367
    public static function getExerciseRepository()
368
    {
369
        return self::$container->get(CQuizRepository::class);
370
    }
371
372
    /**
373
     * @return CExerciseCategoryRepository
374
     */
375
    public static function getExerciseCategoryRepository()
376
    {
377
        return self::$container->get(CExerciseCategoryRepository::class);
378
    }
379
380
    /**
381
     * @return CForumForumRepository
382
     */
383
    public static function getForumRepository()
384
    {
385
        return self::$container->get(CForumForumRepository::class);
386
    }
387
388
    /**
389
     * @return CForumCategoryRepository
390
     */
391
    public static function getForumCategoryRepository()
392
    {
393
        return self::$container->get(CForumCategoryRepository::class);
394
    }
395
396
    /**
397
     * @return CForumPostRepository
398
     */
399
    public static function getForumPostRepository()
400
    {
401
        return self::$container->get(CForumPostRepository::class);
402
    }
403
404
    /**
405
     * @return CForumAttachmentRepository
406
     */
407
    public static function getForumAttachmentRepository()
408
    {
409
        return self::$container->get(CForumAttachmentRepository::class);
410
    }
411
412
    /**
413
     * @return CForumThreadRepository
414
     */
415
    public static function getForumThreadRepository()
416
    {
417
        return self::$container->get(CForumThreadRepository::class);
418
    }
419
420
    /**
421
     * @return CGroupRepository
422
     */
423
    public static function getGroupRepository()
424
    {
425
        return self::$container->get(CGroupRepository::class);
426
    }
427
428
    /**
429
     * @return CQuizQuestionRepository
430
     */
431
    public static function getQuestionRepository()
432
    {
433
        return self::$container->get(CQuizQuestionRepository::class);
434
    }
435
436
    /**
437
     * @return CQuizQuestionCategoryRepository
438
     */
439
    public static function getQuestionCategoryRepository()
440
    {
441
        return self::$container->get(CQuizQuestionCategoryRepository::class);
442
    }
443
444
    /**
445
     * @return CLinkRepository
446
     */
447
    public static function getLinkRepository()
448
    {
449
        return self::$container->get(CLinkRepository::class);
450
    }
451
452
    /**
453
     * @return CLinkCategoryRepository
454
     */
455
    public static function getLinkCategoryRepository()
456
    {
457
        return self::$container->get(CLinkCategoryRepository::class);
458
    }
459
460
    /**
461
     * @return CLpRepository
462
     */
463
    public static function getLpRepository()
464
    {
465
        return self::$container->get(CLpRepository::class);
466
    }
467
468
    /**
469
     * @return CLpCategoryRepository
470
     */
471
    public static function getLpCategoryRepository()
472
    {
473
        return self::$container->get(CLpCategoryRepository::class);
474
    }
475
476
    /**
477
     * @return UserRepository
478
     */
479
    public static function getUserRepository()
480
    {
481
        return self::$container->get(UserRepository::class);
482
    }
483
484
    /**
485
     * @return IllustrationRepository
486
     */
487
    public static function getIllustrationRepository()
488
    {
489
        return self::$container->get(IllustrationRepository::class);
490
    }
491
492
    /**
493
     * @return CShortcutRepository
494
     */
495
    public static function getShortcutRepository()
496
    {
497
        return self::$container->get(CShortcutRepository::class);
498
    }
499
500
    /**
501
     * @return CStudentPublicationRepository
502
     */
503
    public static function getStudentPublicationRepository()
504
    {
505
        return self::$container->get(CStudentPublicationRepository::class);
506
    }
507
508
    /**
509
     * @return CStudentPublicationAssignmentRepository
510
     */
511
    public static function getStudentPublicationAssignmentRepository()
512
    {
513
        return self::$container->get(CStudentPublicationAssignmentRepository::class);
514
    }
515
516
    /**
517
     * @return CStudentPublicationCommentRepository
518
     */
519
    public static function getStudentPublicationCommentRepository()
520
    {
521
        return self::$container->get(CStudentPublicationCommentRepository::class);
522
    }
523
524
    /**
525
     * @return SequenceResourceRepository
526
     */
527
    public static function getSequenceResourceRepository()
528
    {
529
        return self::$container->get(SequenceResourceRepository::class);
530
    }
531
532
    /**
533
     * @return SequenceRepository
534
     */
535
    public static function getSequenceRepository()
536
    {
537
        return self::$container->get(SequenceRepository::class);
538
    }
539
540
    public static function getThematicRepository()
541
    {
542
        return self::$container->get(CThematicRepository::class);
543
    }
544
545
    public static function getThematicPlanRepository()
546
    {
547
        return self::$container->get(CThematicPlanRepository::class);
548
    }
549
550
    public static function getThematicAdvanceRepository()
551
    {
552
        return self::$container->get(CThematicAdvanceRepository::class);
553
    }
554
555
    /**
556
     * @return \Symfony\Component\Form\FormFactory
557
     */
558
    public static function getFormFactory()
559
    {
560
        return self::$container->get('form.factory');
561
    }
562
563
    /**
564
     * @param string $message
565
     * @param string $type    error|success|warning|danger
566
     */
567
    public static function addFlash($message, $type = 'success')
568
    {
569
        $session = self::getSession();
570
        $session->getFlashBag()->add($type, $message);
571
    }
572
573
    /**
574
     * @return Router
575
     */
576
    public static function getRouter()
577
    {
578
        return self::$container->get('router');
579
    }
580
581
    /**
582
     * @return ToolChain
583
     */
584
    public static function getToolChain()
585
    {
586
        return self::$container->get(ToolChain::class);
587
    }
588
589
    /**
590
     * @param ContainerInterface $container
591
     * @param bool               $setSession
592
     */
593
    public static function setLegacyServices($container, $setSession = true)
594
    {
595
        \Database::setConnection($container->get('doctrine.dbal.default_connection'));
596
        $em = $container->get('doctrine.orm.entity_manager');
597
        \Database::setManager($em);
598
        \CourseManager::setEntityManager($em);
599
        //self::setSettingsManager($container->get('chamilo.settings.manager'));
600
        //self::setUserManager($container->get('fos_user.user_manager'));
601
        \CourseManager::setCourseSettingsManager($container->get('Chamilo\CourseBundle\Manager\SettingsManager'));
602
        // Setting course tool chain (in order to create tools to a course)
603
        \CourseManager::setToolList($container->get(ToolChain::class));
604
        if ($setSession) {
605
            self::$session = $container->get('session');
606
        }
607
    }
608
}
609