Passed
Push — master ( 804eb4...5e2840 )
by Julito
10:59
created

ToolChain::getToolFromName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Entity\ResourceType;
9
use Chamilo\CoreBundle\Entity\Tool;
10
use Chamilo\CoreBundle\Entity\ToolResourceRight;
11
use Chamilo\CoreBundle\Manager\SettingsManager;
12
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
13
use Chamilo\CoreBundle\Tool\AbstractTool;
14
use Chamilo\CourseBundle\Entity\CTool;
15
use Doctrine\ORM\EntityManagerInterface;
16
use Symfony\Component\PropertyAccess\Exception\InvalidArgumentException;
17
use Symfony\Component\Security\Core\Security;
18
19
/**
20
 * Class ToolChain.
21
 *
22
 * The course tools classes (agenda, blog, etc) are located in:
23
 *
24
 * src/Chamilo/CourseBundle/Tool
25
 *
26
 * All this classes are registered as a service with the tag "chamilo_core.tool" here:
27
28
 * src/Chamilo/CoreBundle/Resources/config/tools.yml
29
 *
30
 * The register process is made using the class ToolCompilerClass:
31
 *
32
 * src/Chamilo/CoreBundle/DependencyInjection/Compiler/ToolCompilerClass.php
33
34
 * The tool chain is just an array that includes all the tools registered in services.yml
35
 *
36
 * The tool chain is hook when a new course is created via a listener here:
37
38
 * src/Chamilo/CoreBundle/Entity/Listener/CourseListener.php
39
40
 * After a course is created this function is called: CourseListener::prePersist()
41
 * This function includes the called to the function "addToolsInCourse" inside the tool chain.
42
43
 * This allows to tools more easily. Steps:
44
45
 * 1. Create a new tool class here: src/Chamilo/CoreBundle/Tool
46
 * 2. Add the class as a service here: src/Chamilo/CoreBundle/Resources/config/tools.yml  (see examples there)
47
 * 3. Create a new course. When you create a new course the new tool will be created.
48
 */
49
class ToolChain
50
{
51
    protected $tools;
52
    protected $typeList;
53
    protected $entityManager;
54
    protected $settingsManager;
55
    protected $security;
56
57
    public function __construct(EntityManagerInterface $entityManager, SettingsManager $settingsManager, Security $security)
58
    {
59
        $this->tools = [];
60
        $this->typeList = [];
61
        $this->entityManager = $entityManager;
62
        $this->settingsManager = $settingsManager;
63
        $this->security = $security;
64
    }
65
66
    public function addTool(AbstractTool $tool): void
67
    {
68
        $this->tools[$tool->getName()] = $tool;
69
        if ($tool->getResourceTypes()) {
70
            foreach ($tool->getResourceTypes() as $key => $type) {
71
                $this->typeList[$type['repository']] = $key;
72
            }
73
        }
74
    }
75
76
    public function getTools(): array
77
    {
78
        return $this->tools;
79
    }
80
81
    public function createTools(): void
82
    {
83
        $manager = $this->entityManager;
84
        $tools = $this->getTools();
85
        $repo = $manager->getRepository(Tool::class);
86
87
        /** @var AbstractTool $tool */
88
        foreach ($tools as $tool) {
89
            $name = $tool->getName();
90
            $toolExists = $repo->findOneBy(['name' => $name]);
91
            if ($toolExists) {
92
                continue;
93
            }
94
95
            $toolEntity = new Tool();
96
            $toolEntity->setName($name);
97
            if ($tool->isCourseTool()) {
98
                $this->setToolPermissions($toolEntity);
99
            }
100
            $manager->persist($toolEntity);
101
            $types = $tool->getResourceTypes();
102
            if (!empty($types)) {
103
                foreach ($types as $name => $data) {
104
                    $resourceType = new ResourceType();
105
                    $resourceType->setName($name);
106
                    $resourceType->setTool($toolEntity);
107
                    $manager->persist($resourceType);
108
                }
109
            }
110
            $manager->flush();
111
        }
112
    }
113
114
    public function setToolPermissions(Tool $tool): void
115
    {
116
        $toolResourceRight = new ToolResourceRight();
117
        $toolResourceRight
118
            ->setRole('ROLE_TEACHER')
119
            ->setMask(ResourceNodeVoter::getEditorMask())
120
        ;
121
122
        $toolResourceRightReader = new ToolResourceRight();
123
        $toolResourceRightReader
124
            ->setRole('ROLE_STUDENT')
125
            ->setMask(ResourceNodeVoter::getReaderMask())
126
        ;
127
128
        //$tool->addToolResourceRight($toolResourceRight);
129
        //$tool->addToolResourceRight($toolResourceRightReader);
130
    }
131
132
    public function addToolsInCourse(Course $course): Course
133
    {
134
        $tools = $this->getTools();
135
        $manager = $this->entityManager;
136
        $toolVisibility = $this->settingsManager->getSetting('course.active_tools_on_create');
137
138
        // Hardcoded tool list order
139
        $toolList = [
140
            'course_description',
141
            'document',
142
            'learnpath',
143
            'link',
144
            'quiz',
145
            'announcement',
146
            'gradebook',
147
            'glossary',
148
            'attendance',
149
            'course_progress',
150
            'agenda',
151
            'forum',
152
            'dropbox',
153
            'member',
154
            'group',
155
            'chat',
156
            'student_publication',
157
            'survey',
158
            'wiki',
159
            'notebook',
160
            'blog',
161
            'course_tool',
162
            'tracking',
163
            'course_setting',
164
            'course_maintenance',
165
        ];
166
        $toolList = array_flip($toolList);
167
168
        // @todo handle plugin
169
170
        /** @var AbstractTool $tool */
171
        foreach ($tools as $tool) {
172
            $visibility = in_array($tool->getName(), $toolVisibility, true);
173
            $criteria = ['name' => $tool->getName()];
174
            if (!isset($toolList[$tool->getName()])) {
175
                continue;
176
            }
177
            $toolEntity = $manager->getRepository('ChamiloCoreBundle:Tool')->findOneBy($criteria);
178
            $position = $toolList[$tool->getName()] + 1;
179
180
            $courseTool = new CTool();
181
            $courseTool
182
                ->setTool($toolEntity)
183
                ->setName($tool->getName())
184
                ->setPosition($position)
185
                ->setVisibility($visibility)
186
                ->setParent($course)
187
                ->addCourseLink($course)
188
            ;
189
            $course->addTool($courseTool);
190
        }
191
192
        return $course;
193
    }
194
195
    /**
196
     * @return AbstractTool
197
     */
198
    public function getToolFromName(string $name)
199
    {
200
        $tools = $this->getTools();
201
202
        if (array_key_exists($name, $tools)) {
203
            return $tools[$name];
204
        }
205
206
        throw new InvalidArgumentException(sprintf("The Tool '%s' doesn't exist.", $name));
207
    }
208
209
    public function getResourceTypeNameFromRepository(string $repo)
210
    {
211
        if (isset($this->typeList[$repo]) && !empty($this->typeList[$repo])) {
212
            return $this->typeList[$repo];
213
        }
214
215
        throw new InvalidArgumentException(sprintf("The Resource type '%s' doesn't exist.", $repo));
216
    }
217
}
218