Completed
Push — master ( faab0f...53f6ec )
by Julito
09:22
created

ToolChain::createTools()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

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