Completed
Push — master ( d0dde4...04c511 )
by Julito
15:02
created

IndexController::editWelcomeAction()   C

Complexity

Conditions 12
Paths 28

Size

Total Lines 65
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 40
nc 28
nop 1
dl 0
loc 65
rs 5.9833
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
namespace Chamilo\CoreBundle\Controller;
5
6
//use Chamilo\CoreBundle\Admin\CourseAdmin;
7
use Chamilo\CoreBundle\Entity\ExtraField;
8
use Chamilo\CoreBundle\Entity\ExtraFieldValues;
9
use Chamilo\CoreBundle\Framework\PageController;
10
use Chamilo\PageBundle\Entity\Block;
11
use Chamilo\UserBundle\Entity\User;
12
use Ivory\CKEditorBundle\Form\Type\CKEditorType;
13
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
14
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
15
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
16
use Sonata\PageBundle\Model\Page;
17
use Sylius\Component\Attribute\AttributeType\TextAttributeType;
18
use Sylius\Component\Attribute\Model\AttributeValueInterface;
19
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
23
/**
24
 * Class IndexController
25
 * author Julio Montoya <[email protected]>.
26
 *
27
 * @package Chamilo\CoreBundle\Controller
28
 */
29
class IndexController extends BaseController
30
{
31
    /**
32
     * The Chamilo index home page.
33
     *
34
     * @Route("/edit_welcome", name="edit_welcome")
35
     * @Method({"GET|POST"})
36
     * @Security("has_role('ROLE_ADMIN')")
37
     *
38
     * @return Response
39
     */
40
    public function editWelcomeAction(Request $request)
41
    {
42
        $siteSelector = $this->get('sonata.page.site.selector');
43
        $site = $siteSelector->retrieve();
44
        $em = $this->getDoctrine()->getManager();
45
        $page = null;
46
47
        $form = $this->createFormBuilder()
48
            ->add('content', CKEditorType::class)
49
            ->add('save', SubmitType::class, array('label' => 'Update'))
50
            ->getForm();
51
52
        $blockToEdit = null;
53
        if ($site) {
54
            $pageManager = $this->get('sonata.page.manager.page');
55
            // Parents only of homepage
56
            $criteria = ['site' => $site, 'enabled' => true, 'parent' => 1, 'slug' => 'welcome'];
57
            /** @var Page $page */
58
            $page = $pageManager->findOneBy($criteria);
59
            if ($page) {
0 ignored issues
show
introduced by
$page is of type Sonata\PageBundle\Model\Page, thus it always evaluated to true. If $page can have other possible types, add them to src/CoreBundle/Controller/IndexController.php:57
Loading history...
60
                $blocks = $page->getBlocks();
61
                /** @var Block $block */
62
                foreach ($blocks as $block) {
63
                    if ($block->getName() == 'Main content') {
64
                        $code = $block->getSetting('code');
65
                        if ($code == 'content') {
66
                            $children = $block->getChildren();
67
                            /** @var Block $child */
68
                            foreach ($children as $child) {
69
                                if ($child->getType() == 'sonata.formatter.block.formatter') {
70
                                    $blockToEdit = $child;
71
                                    break (2);
72
                                }
73
                            }
74
                        }
75
                    }
76
                }
77
            }
78
        }
79
80
        if ($blockToEdit) {
81
            $form->setData(['content' => $blockToEdit->getSetting('content')]);
82
        }
83
84
        $form->handleRequest($request);
85
86
        if ($form->isSubmitted() && $form->isValid() && $blockToEdit) {
87
            $data = $form->getData();
88
            $content = $data['content'];
89
            /** @var Block $blockToEdit */
90
            $blockToEdit->setSetting('rawContent', $content);
91
            $blockToEdit->setSetting('content', $content);
92
            $em->merge($blockToEdit);
93
            $em->flush();
94
95
            $this->addFlash('success', $this->trans('Updated'));
96
97
            return $this->redirectToRoute('home');
98
        }
99
100
        return $this->render(
101
            '@ChamiloCore/Index/edit_welcome.html.twig',
102
            [
103
                'page' => $page,
104
                'form' => $form->createView(),
105
            ]
106
        );
107
    }
108
109
    /**
110
     * The Chamilo index home page.
111
     *
112
     * @Route("/welcome", name="welcome")
113
     * @Method({"GET"})
114
     *
115
     * @return Response
116
     */
117
    public function welcomeAction()
118
    {
119
        $siteSelector = $this->get('sonata.page.site.selector');
120
        $site = $siteSelector->retrieve();
121
        $page = null;
122
        if ($site) {
123
            $pageManager = $this->get('sonata.page.manager.page');
124
            // Parents only of homepage
125
            $criteria = ['site' => $site, 'enabled' => true, 'parent' => 1, 'slug' => 'welcome'];
126
            /** @var Page $page */
127
            $page = $pageManager->findOneBy($criteria);
128
        }
129
130
        return $this->render(
131
            '@ChamiloCore/Index/welcome.html.twig',
132
            [
133
                'page' => $page,
134
                'content' => 'welcome',
135
            ]
136
        );
137
    }
138
139
    /**
140
     * The Chamilo index home page.
141
     *
142
     * @Route("/", name="home")
143
     * @Method({"GET", "POST"})
144
     *
145
     * @param string $type   courses|sessions|mycoursecategories
146
     * @param string $filter for the userportal courses page. Only works when setting 'history'
147
     * @param int    $page
148
     *
149
     * @return Response
150
     */
151
    public function indexAction(Request $request)
152
    {
153
        /** @var \PageController $pageController */
154
        //$pageController = $this->get('page_controller');
155
        $pageController = new PageController();
156
157
        //$sessionHandler = $request->getSession();
158
        //$sessionHandler->remove('coursesAlreadyVisited');
159
160
        $user = $this->getUser();
161
        $userId = 0;
162
        if ($user) {
163
            $userId = $this->getUser()->getId();
164
        }
165
        $announcementsBlock = $pageController->getAnnouncements($userId);
166
167
        /** @var User $user */
168
        //$userManager = $this->container->get('fos_user.user_manager');
169
        //$user = $userManager->find(1);
170
171
        //$attribute = $this->container->get('doctrine')->getRepository('ChamiloCoreBundle:ExtraField')->find(1);
172
        /*
173
                $attribute = new ExtraField();
174
                $attribute->setName('size');
175
                $attribute->setVariable('size');
176
                $attribute->setType(TextAttributeType::TYPE);
177
                $attribute->setStorageType(AttributeValueInterface::STORAGE_TEXT);
178
                $this->getDoctrine()->getManager()->persist($attribute);
179
                $this->getDoctrine()->getManager()->flush();
180
181
                $attributeColor = new ExtraField();
182
                $attributeColor->setName('color');
183
                $attributeColor->setVariable('color');
184
                $attributeColor->setType(TextAttributeType::TYPE);
185
                $attributeColor->setStorageType(AttributeValueInterface::STORAGE_TEXT);
186
                $this->getDoctrine()->getManager()->persist($attributeColor);
187
                $this->getDoctrine()->getManager()->flush();
188
189
                $color = new ExtraFieldValues();
190
                $color->setComment('lol');
191
                $color->setAttribute($attributeColor);
192
                $color->setValue('blue');
193
194
                $user->addAttribute($color);
195
196
                $smallSize = new ExtraFieldValues();
197
                $smallSize->setComment('lol');
198
                $smallSize->setAttribute($attribute);
199
                $smallSize->setValue('S');
200
201
                $user->addAttribute($smallSize);
202
                $userManager->updateUser($user);
203
        */
204
        //$this->get('session')->remove('id_session');
205
206
        return $this->render(
207
            '@ChamiloCore/Index/index.html.twig',
208
            [
209
                'content' => '',
210
                'announcements_block' => $announcementsBlock,
211
                //'home_page_block' => $pageController->returnHomePage()
212
            ]
213
        );
214
    }
215
216
    /**
217
     * Toggle the student view action.
218
     *
219
     * @Route("/toggle_student_view")
220
     * @Security("has_role('ROLE_TEACHER')")
221
     * @Method({"GET"})
222
     *
223
     * @param Request $request
224
     *
225
     * @return Response
226
     */
227
    public function toggleStudentViewAction(Request $request)
228
    {
229
        if (!api_is_allowed_to_edit(false, false, false, false)) {
230
            return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the documented return type Symfony\Component\HttpFoundation\Response.
Loading history...
231
        }
232
        $studentView = $request->getSession()->get('studentview');
233
        if (empty($studentView) || $studentView == 'studentview') {
234
            $request->getSession()->set('studentview', 'teacherview');
235
236
            return 'teacherview';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'teacherview' returns the type string which is incompatible with the documented return type Symfony\Component\HttpFoundation\Response.
Loading history...
237
        } else {
238
            $request->getSession()->set('studentview', 'studentview');
239
240
            return 'studentview';
0 ignored issues
show
Bug Best Practice introduced by
The expression return 'studentview' returns the type string which is incompatible with the documented return type Symfony\Component\HttpFoundation\Response.
Loading history...
241
        }
242
    }
243
}
244