ProjetsController   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 264
Duplicated Lines 5.3 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 28
Bugs 1 Features 5
Metric Value
wmc 22
c 28
b 1
f 5
lcom 1
cbo 6
dl 14
loc 264
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 6 1
A indexAction() 0 20 2
A updateAction() 0 14 2
B readAction() 0 60 6
A messagesAction() 0 12 1
B resumeAction() 7 43 3
A usecasesAction() 7 58 3
A contributorsAction() 0 17 2
A avancementReel() 0 16 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
use Phalcon\Mvc\View;
3
use Phalcon\Mvc\Url;
4
5
class ProjetsController extends \ControllerBase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
8
    protected $model;
9
    protected $title;
10
    protected $controller;
11
12
    public function initialize()
13
    {
14
        $this->model = "Projet";
15
        $this->title = "Projets";
16
        $this->controller = "Projets";
17
    }
18
19
    public function indexAction($message = null)
20
    {
21
        if ($this->verifyAccessAction($this->controller, "index")) {
22
23
        $projets = Projet::find();
24
        $this->view->setVar("projets", $projets);
25
26
        $dialog = $this->jquery->bootstrap()->htmlModal("modal", "Ajouter un nouveau projet", "test");
27
        $buttonFrm = $this->jquery->bootstrap()->htmlButton("btFrm", "Nouveau");
28
        $dialog->addCancelButton();
29
        $clients = User::find();
30
        $dialog->renderContent($this->view, "projets", "frm", array("clients" => $clients));
31
32
        $buttonFrm->onClick($dialog->jsShow());
33
34
        $this->jquery->compile($this->view);
35
        }else {
36
            $this->view->pick("main/error");
37
        }
38
    }
39
40
    public function updateAction()
41
    {
42
        if ($this->verifyAccessAction($this->controller, "write")) {
43
            parent::updateAction();
44
            //Retrouve l'id du dernier projet créer (En theorie celui qui vient d'être créé)
45
            $projet = Projet::findFirst(array(
46
                "order" => "id DESC"
47
            ));
48
            $idproj = $projet->getId();
49
            $this->response->redirect("Projets/read/$idproj");
50
        }else {
51
            $this->view->pick("main/error");
52
        }
53
    }
54
55
    public function readAction($id = null, $redirect = null)
56
    {
57
        if ($this->verifyAccessAction($this->controller, "read")) {
58
            $projet = Projet::findFirst($id);
59
            $usecases = Usecase::find("idProjet = $id");
60
            $messages = Message::find("idProjet = $id");
61
            $colorTexte = "black";
62
63
            $color = array();
64
65
            $color["r"] = 245;
66
            $color["g"] = 245;
67
            $color["b"] = 247;
68
69
            $avancementReel = $this->avancementReel($usecases);
70
71
            //Passage des différentes variables
72
            $this->view->setVar("colorTexte", $colorTexte);
73
            $this->view->setVar("color", $color);
74
            $this->view->setVar("projet", $projet);
75
            $this->view->setVar("messages", $messages);
76
            $this->view->setVar("usecases", $usecases);
77
            $this->view->setVar("avancement", $avancementReel);
78
79
            //Création de la progressbar
80
            $progress = $this->jquery->bootstrap()->htmlProgressbar("progress", "info", $avancementReel);
81
            $progress->showcaption(true);
82
            $this->jquery->get("Projets/resume/$id", "#contentProjet");
83
            //Creation des évenements onClick et des éléments sur le menu
84
            $this->jquery->getOnClick("#menu1", "Projets/resume/$id", "#contentProjet");
85
            $this->jquery->getOnClick("#menu2", "Projets/contributors/$id", "#contentProjet");
86
            $this->jquery->getOnClick("#menu3", "Projets/usecases/$id", "#contentProjet");
87
            $this->jquery->getOnClick("#menu5", "Projets/messages/$id", "#contentProjet");
88
89
            if ($redirect != null) {
90
                switch ($redirect) {
91
                    case (1):
92
                        $this->jquery->get("Projets/contributors/$id", "#contentProjet");
93
                        break;
94
                    case (2):
95
                        $this->jquery->get("Projets/usecases/$id", "#contentProjet");
96
                        break;
97
                    case (3):
98
                        $this->jquery->get("Projets/messages/$id", "#contentProjet");
99
                        break;
100
                }
101
            }
102
103
104
            //Xeditable
105
106
                $this->jquery->exec("$('#nom').editable()", true);
107
                $this->jquery->exec("$('#image').editable()", true);
108
109
            //Compilation de Jquery dans la vue
110
            $this->jquery->compile($this->view);
111
        }else {
112
            $this->view->pick("main/error");
113
        }
114
    }
115
116
    public function messagesAction($id = null)
117
    {
118
        $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
119
        $messages = Message::find("idProjet = $id");
120
        $this->jquery->postFormOnClick("#submitMsg", "Messages/update", "newMsgForm", null, array("jsCallback" => $this->jquery->getDeferred("Projets/messages/$id", "#contentProjet")));
121
        $this->jquery->compile($this->view);
122
123
        $this->view->setVar("msg", $messages);
124
        $this->view->setVar("idProj", $id);
125
126
        $this->view->render("projets", "messages");
127
    }
128
129
    public function resumeAction($id = null)
130
    {
131
        $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
132
        $projet = Projet::findFirst($id);
133
        $usecases = Usecase::find("idProjet = $id");
134
        $messages = Message::find("idProjet = $id");
135
136
        //Créer un array en javascript contenant la liste des utilisateurs pour la liste des clients sur Xeditable
137
        $client = '[';
138
        $i = 0;
139 View Code Duplication
        foreach (User::find() as $c) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
            $i += 1;
141
            if ($id != 1) {
142
                $client .= ',';
143
            }
144
            $client .= '{value: ' . $c->getId() . ', text:"' . $c->getIdentite() . '"}';
145
        }
146
147
        $client .= ']';
148
149
        //Select Xeditable
150
        $this->jquery->exec("$('#idClient').editable({
151
                                type: 'select',
152
                                pk: $id,
153
                                title: 'Enter username',
154
                                source: $client
155
        })", true);
156
157
        //Xeditable
158
159
        $this->jquery->exec("$('#description').editable()", true);
160
        $this->jquery->exec("$('#dateLancement').editable()", true);
161
        $this->jquery->exec("$('#dateFinPrevue').editable()", true);
162
163
        //Passage des variables à la vue
164
        $this->view->setVar("projet", $projet);
165
        $this->view->setVar("messages", $messages);
166
        $this->view->setVar("usecases", $usecases);
167
168
        //Compilation de Jquery dans la vue
169
        $this->jquery->compile($this->view);
170
171
    }
172
173
    public function usecasesAction($id = null)
174
    {
175
        $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
176
        $taches = Tache::find();
177
        $usecases = Usecase::find(array(
178
            "idProjet = '$id'",
179
            "order" => "poids DESC"
180
        ));
181
182
        $this->view->setVar("usecases", $usecases);
183
        $this->view->setVar("taches", $taches);
184
185
        //Création du modal pour la création d'une UseCase.
186
        $dialog = $this->jquery->bootstrap()->htmlModal("modal", "Ajouter une Usecase", "test");
187
        $buttonFrm = $this->jquery->bootstrap()->htmlButton("btFrm", "Nouvelle UseCase");
188
        $dialog->addCancelButton();
189
        $users = User::find();
190
        $dialog->renderContent($this->view, "usecases", "frm", array("users" => $users, "id" => $id));
191
192
        $buttonFrm->onClick($dialog->jsShow());
193
194
        //Création du modal pour les taches.
195
        $dialogTaches = $this->jquery->bootstrap()->htmlModal("modalTache", "Ajouter une Tache", "test");
196
        $buttonFrmEdit = $this->jquery->bootstrap()->htmlButton("btFrmTache", "Nouvelle Tache");
197
        $usecases = Usecase::find();
198
        $dialogTaches->addCancelButton();
199
        $dialogTaches->renderContent($this->view, "taches", "frm", array("usecases" => $usecases, "id" => $id));
200
        $buttonFrmEdit->onClick($dialogTaches->jsShow());
201
202
203
        //Créer un array en javascript contenant la liste des utilisateurs pour la liste des développeurs sur Xeditable
204
        $dev = '[';
205
        $i = 0;
206 View Code Duplication
        foreach (User::find() as $c) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
207
            $i += 1;
208
            if ($id != 1) {
209
                $dev .= ',';
210
            }
211
            $dev .= '{value: ' . $c->getId() . ', text:"' . $c->getIdentite() . '"}';
212
        }
213
214
        $dev .= ']';
215
216
        //Select Xeditable
217
        $this->jquery->exec("$('.idDev').editable({
218
                                type: 'select',
219
                                title: 'Enter username',
220
                                source: $dev
221
        })", true);
222
223
224
        // Xeditable
225
        $this->jquery->exec("$('.nom').editable()", true);
226
        $this->jquery->exec("$('.poids').editable()", true);
227
        $this->jquery->exec("$('.avancement').editable()", true);
228
229
        $this->jquery->compile($this->view);
230
    }
231
232
    public function contributorsAction($id = null)
233
    {
234
        $this->view->setRenderLevel(View::LEVEL_ACTION_VIEW);
235
        $contributor = [];
236
237
        $usecases = Usecase::find("idProjet = $id");
238
239
        foreach ($usecases as $uc) {
240
            $contributor[] = $uc->getUser();
241
        }
242
243
        $contributor = array_unique($contributor, SORT_REGULAR);
244
245
        $this->view->setVar("contributors", $contributor);
246
        $this->view->setVar("usecases", $usecases);;
247
248
    }
249
250
    //Calcule le taux d'avancement total d'un projet
251
    public function avancementReel($usecases)
252
    {
253
        //Calcul le taux de finition du projet en fonction du nombre d'usecases total et du taux d'avancement sur chaque usecases.
254
255
        $countUsecases = count($usecases);
256
        $totalAvancementFini = $countUsecases * 100;
257
        $totalAvancementReel = 0;
258
259
        foreach ($usecases as $u) {
260
261
            $totalAvancementReel = $totalAvancementReel + $u->getAvancement();
262
        }
263
264
        $avancementReel = ($totalAvancementReel / $totalAvancementFini) * 100;
265
        return number_format($avancementReel, 1);
266
    }
267
268
}
269