Completed
Push — master ( b05893...eedd1b )
by Julito
09:17
created

ToolChain::setToolPermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 1
dl 0
loc 16
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle;
5
6
use Chamilo\CoreBundle\Entity\Course;
7
use Chamilo\CoreBundle\Entity\Resource\ResourceType;
8
use Chamilo\CoreBundle\Entity\Tool;
9
use Chamilo\CoreBundle\Entity\ToolResourceRight;
10
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
11
use Chamilo\CoreBundle\Tool\AbstractTool;
12
use Chamilo\CourseBundle\Entity\CTool;
13
use Chamilo\SettingsBundle\Manager\SettingsManager;
14
use Doctrine\Common\Persistence\ObjectManager;
15
16
/**
17
 * Class ToolChain.
18
 *
19
 * The course tools classes (agenda, blog, etc) are located in:
20
 *
21
 * src/Chamilo/CourseBundle/Tool
22
 *
23
 * All this classes are registered as a service with the tag "chamilo_core.tool" here:
24
25
 * src/Chamilo/CoreBundle/Resources/config/tools.yml
26
 *
27
 * The register process is made using the class ToolCompilerClass:
28
 *
29
 * src/Chamilo/CoreBundle/DependencyInjection/Compiler/ToolCompilerClass.php
30
31
 * The tool chain is just an array that includes all the tools registered in services.yml
32
 *
33
 * The tool chain is hook when a new course is created via a listener here:
34
35
 * src/Chamilo/CoreBundle/Entity/Listener/CourseListener.php
36
37
 * After a course is created this function is called: CourseListener::prePersist()
38
 * This function includes the called to the function "addToolsInCourse" inside the tool chain.
39
40
 * This allows to tools more easily. Steps:
41
42
 * 1. Create a new tool class here: src/Chamilo/CoreBundle/Tool
43
 * 2. Add the class as a service here: src/Chamilo/CoreBundle/Resources/config/tools.yml  (see examples there)
44
 * 3. Create a new course. When you create a new course the new tool will be created.
45
 */
46
class ToolChain
47
{
48
    protected $tools;
49
50
    /**
51
     * Construct.
52
     */
53
    public function __construct()
54
    {
55
        $this->tools = [];
56
    }
57
58
    /**
59
     * @param AbstractTool $tool
60
     */
61
    public function addTool(AbstractTool $tool): void
62
    {
63
        $this->tools[$tool->getName()] = $tool;
64
    }
65
66
    /**
67
     * @return array
68
     */
69
    public function getTools(): array
70
    {
71
        return $this->tools;
72
    }
73
74
    /**
75
     * @param ObjectManager $manager
76
     */
77
    public function createTools(ObjectManager $manager): void
78
    {
79
        $tools = $this->getTools();
80
81
        /** @var AbstractTool $tool */
82
        foreach ($tools as $tool) {
83
            $toolEntity = new Tool();
84
            $toolEntity
85
                ->setName($tool->getName())
86
                ->setImage($tool->getImage())
87
                ->setDescription('')
88
            ;
89
90
            if ($tool->getAdmin() === 1) {
91
                // Only check ROLE_ADMIN
92
            } else {
93
                $this->setToolPermissions($toolEntity);
94
            }
95
96
            $manager->persist($toolEntity);
97
98
            $types = $tool->getTypes();
99
            if (!empty($types)) {
100
                foreach ($types as $type) {
101
                    $resourceType = new ResourceType();
102
                    $resourceType->setName($type);
103
                    $resourceType->setTool($toolEntity);
104
                    $manager->persist($resourceType);
105
                }
106
            }
107
108
            $manager->flush();
109
        }
110
    }
111
112
    /**
113
     * @param Tool $tool
114
     */
115
    public function setToolPermissions(Tool $tool): void
116
    {
117
        $toolResourceRight = new ToolResourceRight();
118
        $toolResourceRight
119
            ->setRole('ROLE_TEACHER')
120
            ->setMask(ResourceNodeVoter::getEditorMask())
121
        ;
122
123
        $toolResourceRightReader = new ToolResourceRight();
124
        $toolResourceRightReader
125
            ->setRole('ROLE_STUDENT')
126
            ->setMask(ResourceNodeVoter::getReaderMask())
127
        ;
128
129
        $tool->addToolResourceRight($toolResourceRight);
130
        $tool->addToolResourceRight($toolResourceRightReader);
131
    }
132
133
    /**
134
     * @param Course          $course
135
     * @param SettingsManager $settingsManager
136
     *
137
     * @return Course
138
     */
139
    public function addToolsInCourse(Course $course, SettingsManager $settingsManager): Course
140
    {
141
        $tools = $this->getTools();
142
143
        $toolVisibility = $settingsManager->getSetting('course.active_tools_on_create');
144
        /** @var AbstractTool $tool */
145
        foreach ($tools as $tool) {
146
            $toolEntity = new CTool();
147
            $visibility = in_array($tool->getName(), $toolVisibility);
148
149
            $toolEntity
150
                ->setCourse($course)
151
                ->setImage($tool->getImage())
152
                ->setName($tool->getName())
153
                ->setVisibility($visibility)
154
                ->setLink($tool->getLink())
155
                ->setTarget($tool->getTarget())
156
                ->setCategory($tool->getCategory());
157
158
            $course->addTools($toolEntity);
159
        }
160
161
        return $course;
162
    }
163
164
    /**
165
     * @param string $name
166
     *
167
     * @return AbstractTool|false
168
     */
169
    public function getToolFromName($name)
170
    {
171
        $tools = $this->getTools();
172
173
        if (array_key_exists($name, $tools)) {
174
            return $tools[$name];
175
        }
176
177
        return false;
178
    }
179
}
180