Completed
Push — master ( cf4f4c...f3c478 )
by Julito
36:36
created

ExerciseCategoryManager::getJqgridActionLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 17
nc 1
nop 1
dl 0
loc 29
rs 9.7
c 1
b 0
f 1
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Entity\Resource\ResourceLink;
5
use Chamilo\CoreBundle\Framework\Container;
6
use Chamilo\CourseBundle\Entity\CExerciseCategory;
7
8
/**
9
 * Class ExtraFieldValue
10
 * Declaration for the ExtraFieldValue class, managing the values in extra
11
 * fields for any data type.
12
 */
13
class ExerciseCategoryManager extends Model
14
{
15
    public $type = '';
16
    public $columns = [
17
        'id',
18
        'name',
19
        'c_id',
20
        'description',
21
        'created_at',
22
        'updated_at',
23
    ];
24
25
    /**
26
     * Formats the necessary elements for the given datatype.
27
     *
28
     * @param string $type The type of data to which this extra field
29
     *                     applies (user, course, session, ...)
30
     *
31
     * @assert (-1) === false
32
     */
33
    public function __construct()
34
    {
35
        parent::__construct();
36
        $this->is_course_model = true;
37
        $this->table = Database::get_course_table('exercise_category');
38
    }
39
40
    /**
41
     * @param int $courseId
42
     *
43
     * @return array
44
     */
45
    public function getCategories($courseId)
46
    {
47
        return Container::getExerciseCategoryRepository()->getCategories($courseId);
48
    }
49
50
    /**
51
     * @param int $courseId
52
     *
53
     * @return array
54
     */
55
    public function getCategoriesForSelect($courseId)
56
    {
57
        $categories = $this->getCategories($courseId);
58
        $options = [];
59
60
        if (!empty($categories)) {
61
            /** @var CExerciseCategory $category */
62
            foreach ($categories as $category) {
63
                $options[$category->getId()] = $category->getName();
64
            }
65
        }
66
67
        return $options;
68
    }
69
70
    /**
71
     * @param int $id
72
     */
73
    public function delete($id)
74
    {
75
        $repo = Container::getExerciseCategoryRepository();
76
        $category = $repo->find($id);
77
        $repo->hardDelete($category);
0 ignored issues
show
Bug introduced by
It seems like $category can also be of type null; however, parameter $resource of Chamilo\CoreBundle\Repos...epository::hardDelete() does only seem to accept Chamilo\CoreBundle\Entit...source\AbstractResource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        $repo->hardDelete(/** @scrutinizer ignore-type */ $category);
Loading history...
78
    }
79
80
    /**
81
     * @param array $params
82
     * @param bool  $showQuery
83
     *
84
     * @return bool
85
     */
86
    public function update($params, $showQuery = false)
87
    {
88
        $id = $params['id'];
89
90
        $repo = Container::getExerciseCategoryRepository();
91
        /** @var CExerciseCategory $category */
92
        $category = $repo->find($id);
93
94
        if ($category) {
0 ignored issues
show
introduced by
$category is of type Chamilo\CourseBundle\Entity\CExerciseCategory, thus it always evaluated to true.
Loading history...
95
            $category
96
                ->setName($params['name'])
97
                ->setDescription($params['description'])
98
            ;
99
100
            $repo->getEntityManager()->persist($category);
101
            $repo->getEntityManager()->flush();
102
103
            return true;
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * Save values in the *_field_values table.
111
     *
112
     * @param array $params    Structured array with the values to save
113
     * @param bool  $showQuery Whether to show the insert query (passed to the parent save() method)
114
     */
115
    public function save($params, $showQuery = false)
116
    {
117
        $courseId = api_get_course_int_id();
118
        $repo = Container::getExerciseCategoryRepository();
119
        $em = Database::getManager();
120
        $category = new CExerciseCategory();
121
        $category
122
            ->setName($params['name'])
123
            ->setCId($courseId)
124
            ->setDescription($params['name'])
125
        ;
126
        /*
127
            // Update position
128
            $query = $em->getRepository('ChamiloCourseBundle:CExerciseCategory')->createQueryBuilder('e');
129
            $query
130
                ->where('e.cId = :cId')
131
                ->setParameter('cId', $courseId)
132
                ->setMaxResults(1)
133
                ->orderBy('e.position', 'DESC');
134
            $last = $query->getQuery()->getOneOrNullResult();
135
            $position = 0;
136
            if (!empty($last)) {
137
                $position = $last->getPosition() + 1;
138
            }
139
            $category->setPosition($position);
140
*/
141
        $em->persist($category);
142
        $repo->addResourceToCourse(
143
            $category,
144
            ResourceLink::VISIBILITY_PUBLISHED,
145
            api_get_user_entity(api_get_user_id()),
146
            api_get_course_entity($courseId),
147
            api_get_session_entity(),
148
            api_get_group_entity()
149
        );
150
151
        $em->flush();
152
153
        return $category;
154
    }
155
156
    /**
157
     * @param string $token
158
     *
159
     * @return string
160
     */
161
    public function getJqgridActionLinks($token)
162
    {
163
        //With this function we can add actions to the jgrid (edit, delete, etc)
164
        $editIcon = Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL);
165
        $deleteIcon = Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL);
166
        /*$editIcon = Display::returnFontAwesomeIcon('pencil');
167
        $deleteIcon = Display::returnFontAwesomeIcon('trash');*/
168
        $confirmMessage = addslashes(
169
            api_htmlentities(get_lang('Please confirm your choice'), ENT_QUOTES)
170
        );
171
172
        $courseParams = api_get_cidreq();
173
174
        $editButton = <<<JAVASCRIPT
175
            <a href="?action=edit&{$courseParams}&id=' + options.rowId + '" class="">\
176
                $editIcon\
177
            </a>
178
JAVASCRIPT;
179
        $deleteButton = <<<JAVASCRIPT
180
            <a \
181
                onclick="if (!confirm(\'$confirmMessage\')) {return false;}" \
182
                href="?sec_token=$token&{$courseParams}&id=' + options.rowId + '&action=delete" \
183
                class="">\
184
                $deleteIcon\
185
            </a>
186
JAVASCRIPT;
187
188
        return "function action_formatter(cellvalue, options, rowObject) {        
189
            return '$editButton $deleteButton';
190
        }";
191
    }
192
193
    /**
194
     * @param string $url
195
     * @param string $action
196
     *
197
     * @return FormValidator
198
     */
199
    public function return_form($url, $action)
200
    {
201
        $form = new FormValidator('category', 'post', $url);
202
        $id = isset($_GET['id']) ? (int) $_GET['id'] : null;
203
        $form->addElement('hidden', 'id', $id);
204
205
        // Setting the form elements
206
        $header = get_lang('Add');
207
        $defaults = [];
208
209
        if ($action === 'edit') {
210
            $header = get_lang('Edit');
211
            // Setting the defaults
212
            $defaults = $this->get($id, false);
213
        }
214
215
        $form->addElement('header', $header);
216
217
        $form->addText(
218
            'name',
219
            get_lang('Name')
220
        );
221
222
        $form->addHtmlEditor('description', get_lang('Description'));
223
224
        if ($action === 'edit') {
225
            $form->addButtonUpdate(get_lang('Edit'));
226
        } else {
227
            $form->addButtonCreate(get_lang('Add'));
228
        }
229
230
        /*if (!empty($defaults['created_at'])) {
231
            $defaults['created_at'] = api_convert_and_format_date($defaults['created_at']);
232
        }
233
        if (!empty($defaults['updated_at'])) {
234
            $defaults['updated_at'] = api_convert_and_format_date($defaults['updated_at']);
235
        }*/
236
        $form->setDefaults($defaults);
237
238
        // Setting the rules
239
        $form->addRule('name', get_lang('Required field'), 'required');
240
241
        return $form;
242
    }
243
244
    /**
245
     * @return string
246
     */
247
    public function display()
248
    {
249
        // action links
250
        $content = '<div class="actions">';
251
        $content .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'">';
252
        $content .= Display::return_icon(
253
            'back.png',
254
            get_lang('Back to').' '.get_lang('Administration'),
255
            '',
256
            ICON_SIZE_MEDIUM
257
        );
258
        $content .= '</a>';
259
        $content .= '<a href="'.api_get_self().'?action=add&'.api_get_cidreq().'">';
260
        $content .= Display::return_icon(
261
            'add.png',
262
            get_lang('Add'),
263
            '',
264
            ICON_SIZE_MEDIUM
265
        );
266
        $content .= '</a>';
267
        $content .= '</div>';
268
        $content .= Display::grid_html('categories');
269
270
        return $content;
271
    }
272
}
273