Passed
Push — master ( 34bdad...b152c5 )
by Julito
20:20
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\CourseBundle;
5
6
use Chamilo\CoreBundle\Entity\Course;
7
use Chamilo\CoreBundle\Entity\Tool;
8
use Chamilo\CoreBundle\Entity\ToolResourceRights;
9
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
10
use Chamilo\CourseBundle\Entity\CTool;
11
use Chamilo\CourseBundle\Tool\BaseTool;
12
use Chamilo\SettingsBundle\Manager\SettingsManager;
13
use Doctrine\Common\Persistence\ObjectManager;
14
15
/**
16
 * Class ToolChain.
17
 *
18
 * The course tools classes (agenda, blog, etc) are located in:
19
 *
20
 * src/Chamilo/CourseBundle/Tool
21
 *
22
 * All this classes are registered as a service with the tag "chamilo_course.tool" here:
23
24
 * src/Chamilo/CourseBundle/Resources/config/services.yml
25
 *
26
 * The register process is made using the class ToolCompilerClass:
27
 *
28
 * src/Chamilo/CourseBundle/DependencyInjection/Compiler/ToolCompilerClass.php
29
30
 * The tool chain is just an array that includes all the tools registered in services.yml
31
 *
32
 * The tool chain is hook when a new course is created via a listener here:
33
34
 * src/Chamilo/CoreBundle/Entity/Listener/CourseListener.php
35
36
 * After a course is created this function is called: CourseListener::prePersist()
37
 * This function includes the called to the function "addToolsInCourse" inside the tool chain.
38
39
 * This allows to create course tools more easily. Steps:
40
41
 * 1. Create a new tool class here: src/Chamilo/CourseBundle/Tool
42
 * 2. Add the class as a service here: src/Chamilo/CourseBundle/Resources/config/services.yml  (see examples there)
43
 * 3. Create a new course. When you create a new course the new tool will be created
44
 *
45
 * @package Chamilo\CourseBundle
46
 */
47
class ToolChain
48
{
49
    protected $tools;
50
51
    /**
52
     * Construct.
53
     */
54
    public function __construct()
55
    {
56
        $this->tools = [];
57
    }
58
59
    /**
60
     * @param BaseTool $tool
61
     */
62
    public function addTool(BaseTool $tool): void
63
    {
64
        $this->tools[$tool->getName()] = $tool;
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function getTools(): array
71
    {
72
        return $this->tools;
73
    }
74
75
    /**
76
     * @param ObjectManager $manager
77
     */
78
    public function createTools(ObjectManager $manager)
79
    {
80
        $tools = $this->getTools();
81
82
        /** @var BaseTool $tool */
83
        foreach ($tools as $tool) {
84
            $toolEntity = new Tool();
85
            $toolEntity
86
                ->setName($tool->getName())
87
                ->setImage($tool->getImage())
88
                ->setDescription($tool->getName().' - description')
89
            ;
90
            $this->setToolPermissions($toolEntity);
91
            $manager->persist($toolEntity);
92
            $manager->flush();
93
        }
94
    }
95
96
    /**
97
     * @param Tool $tool
98
     */
99
    public function setToolPermissions(Tool $tool)
100
    {
101
        $toolResourceRight = new ToolResourceRights();
102
        $toolResourceRight
103
            ->setRole('ROLE_TEACHER')
104
            ->setMask(ResourceNodeVoter::getEditorMask())
105
        ;
106
107
        $toolResourceRightReader = new ToolResourceRights();
108
        $toolResourceRightReader
109
            ->setRole('ROLE_STUDENT')
110
            ->setMask(ResourceNodeVoter::getReaderMask())
111
        ;
112
113
        $tool->addToolResourceRights($toolResourceRight);
114
        $tool->addToolResourceRights($toolResourceRightReader);
115
    }
116
117
    /**
118
     * @param Course          $course
119
     * @param SettingsManager $settingsManager
120
     *
121
     * @return Course
122
     */
123
    public function addToolsInCourse(Course $course, SettingsManager $settingsManager)
124
    {
125
        $tools = $this->getTools();
126
127
        $toolVisibility = $settingsManager->getSetting('course.active_tools_on_create');
128
        /** @var BaseTool $tool */
129
        foreach ($tools as $tool) {
130
            $toolEntity = new CTool();
131
            $visibility = in_array($tool->getName(), $toolVisibility);
132
133
            $toolEntity
134
                ->setCourse($course)
135
                ->setImage($tool->getImage())
136
                ->setName($tool->getName())
137
                ->setVisibility($visibility)
138
                ->setLink($tool->getLink())
139
                ->setTarget($tool->getTarget())
140
                ->setCategory($tool->getCategory());
141
142
            $course->addTools($toolEntity);
143
        }
144
145
        return $course;
146
    }
147
148
    /**
149
     * @param string $name
150
     *
151
     * @return BaseTool
152
     */
153
    public function getToolFromName($name)
154
    {
155
        $tools = $this->getTools();
156
157
        if (array_key_exists($name, $tools)) {
158
            return $tools[$name];
159
        }
160
161
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false returns the type false which is incompatible with the documented return type Chamilo\CourseBundle\Tool\BaseTool.
Loading history...
162
    }
163
}
164