Passed
Push — master ( 5deee6...e0f7b0 )
by Julito
10:06
created

ExerciseCategoryManager::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use APY\DataGridBundle\Grid\Action\MassAction;
5
use APY\DataGridBundle\Grid\Action\RowAction;
6
use APY\DataGridBundle\Grid\Row;
7
use APY\DataGridBundle\Grid\Source\Entity;
8
use Chamilo\CoreBundle\Framework\Container;
9
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter;
10
use Chamilo\CourseBundle\Entity\CExerciseCategory;
11
12
/**
13
 * Class ExtraFieldValue
14
 * Declaration for the ExtraFieldValue class, managing the values in extra
15
 * fields for any data type.
16
 */
17
class ExerciseCategoryManager extends Model
18
{
19
    public $type = '';
20
    public $columns = [
21
        'id',
22
        'name',
23
        'c_id',
24
        'description',
25
        'created_at',
26
        'updated_at',
27
    ];
28
29
    /**
30
     * Formats the necessary elements for the given datatype.
31
     *
32
     * @assert (-1) === false
33
     */
34
    public function __construct()
35
    {
36
        parent::__construct();
37
        $this->is_course_model = true;
38
        $this->table = Database::get_course_table('exercise_category');
39
    }
40
41
    /**
42
     * @param int $courseId
43
     *
44
     * @return array
45
     */
46
    public function getCategories($courseId)
47
    {
48
        return Container::getExerciseCategoryRepository()->getCategories($courseId);
49
    }
50
51
    /**
52
     * @param int $courseId
53
     *
54
     * @return array
55
     */
56
    public function getCategoriesForSelect($courseId)
57
    {
58
        $categories = $this->getCategories($courseId);
59
        $options = [];
60
61
        if (!empty($categories)) {
62
            /** @var CExerciseCategory $category */
63
            foreach ($categories as $category) {
64
                $options[$category->getId()] = $category->getName();
65
            }
66
        }
67
68
        return $options;
69
    }
70
71
    /**
72
     * @param int $id
73
     */
74
    public function delete($id)
75
    {
76
        $repo = Container::getExerciseCategoryRepository();
77
        $category = $repo->find($id);
78
        $repo->hardDelete($category);
79
80
        return true;
81
    }
82
83
    /**
84
     * @param                                                   $primaryKeys
85
     * @param                                                   $allPrimaryKeys
86
     * @param \Symfony\Component\HttpFoundation\Session\Session $session
87
     * @param                                                   $parameters
88
     */
89
    public function deleteResource(
90
        $primaryKeys,
91
        $allPrimaryKeys,
92
        Symfony\Component\HttpFoundation\Session\Session $session,
93
        $parameters
94
    ) {
95
        $repo = Container::getExerciseCategoryRepository();
96
        $translator = Container::getTranslator();
97
        foreach ($primaryKeys as $id) {
98
            $category = $repo->find($id);
99
            $repo->hardDelete($category);
100
        }
101
102
        Display::addFlash(Display::return_message($translator->trans('Deleted')));
103
        header('Location:'.api_get_self().'?'.api_get_cidreq());
104
        exit;
105
    }
106
107
    /**
108
     * @param array $params
109
     * @param bool  $showQuery
110
     *
111
     * @return bool
112
     */
113
    public function update($params, $showQuery = false)
114
    {
115
        $id = $params['id'];
116
117
        $repo = Container::getExerciseCategoryRepository();
118
        /** @var CExerciseCategory $category */
119
        $category = $repo->find($id);
120
121
        if ($category) {
0 ignored issues
show
introduced by
$category is of type Chamilo\CourseBundle\Entity\CExerciseCategory, thus it always evaluated to true.
Loading history...
122
            $category
123
                ->setName($params['name'])
124
                ->setDescription($params['description'])
125
            ;
126
127
            $repo->update($category);
128
129
            return true;
130
        }
131
132
        return false;
133
    }
134
135
    /**
136
     * Save values in the *_field_values table.
137
     *
138
     * @param array $params    Structured array with the values to save
139
     * @param bool  $showQuery Whether to show the insert query (passed to the parent save() method)
140
     */
141
    public function save($params, $showQuery = false)
142
    {
143
        $courseId = api_get_course_int_id();
144
        $course = api_get_course_entity($courseId);
145
146
        $repo = Container::getExerciseCategoryRepository();
147
        $category = new CExerciseCategory();
148
        $category
149
            ->setName($params['name'])
150
            ->setCourse($course)
151
            ->setDescription($params['description'])
152
            ->setParent($course)
153
            ->addCourseLink($course, api_get_session_entity())
154
        ;
155
156
        /*
157
            // Update position
158
            $query = $em->getRepository('ChamiloCourseBundle:CExerciseCategory')->createQueryBuilder('e');
159
            $query
160
                ->where('e.cId = :cId')
161
                ->setParameter('cId', $courseId)
162
                ->setMaxResults(1)
163
                ->orderBy('e.position', 'DESC');
164
            $last = $query->getQuery()->getOneOrNullResult();
165
            $position = 0;
166
            if (!empty($last)) {
167
                $position = $last->getPosition() + 1;
168
            }
169
            $category->setPosition($position);
170
*/
171
        $repo->create($category);
172
173
        return $category;
174
    }
175
176
    /**
177
     * @param $token
178
     *
179
     * @return string
180
     */
181
    public function getJqgridActionLinks($token)
182
    {
183
        //With this function we can add actions to the jgrid (edit, delete, etc)
184
        $editIcon = Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL);
185
        $deleteIcon = Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
186
        $confirmMessage = addslashes(
187
            api_htmlentities(get_lang('ConfirmYourChoice'), ENT_QUOTES)
188
        );
189
190
        $courseParams = api_get_cidreq();
191
192
        $editButton = <<<JAVASCRIPT
193
            <a href="?action=edit&{$courseParams}&id=' + options.rowId + '" class="btn btn-link btn-xs">\
194
                $editIcon\
195
            </a>
196
JAVASCRIPT;
197
        $deleteButton = <<<JAVASCRIPT
198
            <a \
199
                onclick="if (!confirm(\'$confirmMessage\')) {return false;}" \
200
                href="?sec_token=$token&{$courseParams}&id=' + options.rowId + '&action=delete" \
201
                class="btn btn-link btn-xs">\
202
                $deleteIcon\
203
            </a>
204
JAVASCRIPT;
205
206
        return "function action_formatter(cellvalue, options, rowObject) {
207
            return '$editButton $deleteButton';
208
        }";
209
    }
210
211
    /**
212
     * @param string $url
213
     * @param string $action
214
     *
215
     * @return FormValidator
216
     */
217
    public function return_form($url, $action)
218
    {
219
        $form = new FormValidator('category', 'post', $url);
220
        $id = isset($_GET['id']) ? (int) $_GET['id'] : null;
221
        $form->addElement('hidden', 'id', $id);
222
223
        // Setting the form elements
224
        $header = get_lang('Add');
225
        $defaults = [];
226
227
        if ('edit' === $action) {
228
            $header = get_lang('Edit');
229
            // Setting the defaults
230
            $defaults = $this->get($id, false);
231
        }
232
233
        $form->addElement('header', $header);
234
235
        $form->addText(
236
            'name',
237
            get_lang('Name')
238
        );
239
240
        $form->addHtmlEditor('description', get_lang('Description'));
241
242
        if ('edit' === $action) {
243
            $form->addButtonUpdate(get_lang('Edit'));
244
        } else {
245
            $form->addButtonCreate(get_lang('Add'));
246
        }
247
        $form->setDefaults($defaults);
248
249
        // Setting the rules
250
        $form->addRule('name', get_lang('Required field'), 'required');
251
252
        return $form;
253
    }
254
255
    /**
256
     * @return string
257
     */
258
    public function display()
259
    {
260
        $session = api_get_session_entity();
261
        $course = api_get_course_entity();
262
263
        // Action links
264
        $content = '<div class="actions">';
265
        $content .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'">';
266
        $content .= Display::return_icon(
267
            'back.png',
268
            get_lang('Back to').' '.get_lang('Administration'),
269
            '',
270
            ICON_SIZE_MEDIUM
271
        );
272
        $content .= '</a>';
273
        $content .= '<a href="'.api_get_self().'?action=add&'.api_get_cidreq().'">';
274
        $content .= Display::return_icon(
275
            'add.png',
276
            get_lang('Add'),
277
            '',
278
            ICON_SIZE_MEDIUM
279
        );
280
        $content .= '</a>';
281
        $content .= '</div>';
282
283
        $content .= Display::grid_html('categories');
284
285
        return $content;
286
    }
287
}
288