Completed
Push — master ( 1759c8...52446f )
by Julito
29:44
created

ToolChain::getTools()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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\Resource\ResourceNode;
8
use Chamilo\CoreBundle\Entity\Tool;
9
use Chamilo\CoreBundle\Entity\ToolResourceRights;
10
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
11
use Chamilo\CourseBundle\Entity\CTool;
12
use Chamilo\CourseBundle\Tool\BaseTool;
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 = array();
57
    }
58
59
    /**
60
     * @param BaseTool $tool
61
     */
62
    public function addTool(BaseTool $tool)
63
    {
64
        $this->tools[$tool->getName()] = $tool;
65
    }
66
67
    /**
68
     * @return array
69
     */
70
    public function getTools()
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
        $toolResourceRight = new ToolResourceRights();
82
        $toolResourceRight
83
            ->setRole('ROLE_TEACHER')
84
            ->setMask(ResourceNodeVoter::getEditorMask())
85
        ;
86
87
        $toolResourceRightReader = new ToolResourceRights();
88
        $toolResourceRightReader
89
            ->setRole('ROLE_STUDENT')
90
            ->setMask(ResourceNodeVoter::getReaderMask())
91
        ;
92
93
        /** @var BaseTool $tool */
94
        foreach ($tools as $tool) {
95
            $toolEntity = new Tool();
96
            $toolEntity
97
                ->setName($tool->getName())
98
                ->setImage($tool->getImage())
99
                ->setDescription($tool->getName().' - description')
100
                ->addToolResourceRights($toolResourceRight)
101
                ->addToolResourceRights($toolResourceRightReader)
102
            ;
103
            $manager->persist($toolEntity);
104
        }
105
    }
106
107
    /**
108
     * @param Course $course
109
     * @return Course
110
     */
111
    public function addToolsInCourse(Course $course)
112
    {
113
        $tools = $this->getTools();
114
115
        /** @var BaseTool $tool */
116
        foreach ($tools as $tool) {
117
            $toolEntity = new CTool();
118
            $toolEntity
119
                ->setCId($course->getId())
120
                ->setImage($tool->getImage())
121
                ->setName($tool->getName())
122
                ->setVisibility(1)
123
                ->setLink($tool->getLink())
124
                ->setTarget($tool->getTarget())
125
                ->setCategory($tool->getCategory());
126
127
            $course->addTools($toolEntity);
128
        }
129
130
        return $course;
131
    }
132
133
    /**
134
     * @param string $name
135
     * @return BaseTool
136
     */
137
    public function getToolFromName($name)
138
    {
139
        $tools = $this->getTools();
140
141
        if (array_key_exists($name, $tools)) {
142
            return $tools[$name];
143
        }
144
145
        return false;
146
    }
147
}
148