|
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\Source\Entity; |
|
7
|
|
|
use Chamilo\CoreBundle\Entity\Resource\ResourceLink; |
|
8
|
|
|
use Chamilo\CoreBundle\Framework\Container; |
|
9
|
|
|
use Chamilo\CoreBundle\Security\Authorization\Voter\ResourceNodeVoter; |
|
10
|
|
|
use Chamilo\CourseBundle\Entity\CExerciseCategory; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class ExtraFieldValue |
|
15
|
|
|
* Declaration for the ExtraFieldValue class, managing the values in extra |
|
16
|
|
|
* fields for any data type. |
|
17
|
|
|
*/ |
|
18
|
|
|
class ExerciseCategoryManager extends Model |
|
19
|
|
|
{ |
|
20
|
|
|
public $type = ''; |
|
21
|
|
|
public $columns = [ |
|
22
|
|
|
'id', |
|
23
|
|
|
'name', |
|
24
|
|
|
'c_id', |
|
25
|
|
|
'description', |
|
26
|
|
|
'created_at', |
|
27
|
|
|
'updated_at', |
|
28
|
|
|
]; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Formats the necessary elements for the given datatype. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $type The type of data to which this extra field |
|
34
|
|
|
* applies (user, course, session, ...) |
|
35
|
|
|
* |
|
36
|
|
|
* @assert (-1) === false |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct() |
|
39
|
|
|
{ |
|
40
|
|
|
parent::__construct(); |
|
41
|
|
|
$this->is_course_model = true; |
|
42
|
|
|
$this->table = Database::get_course_table('exercise_category'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param int $courseId |
|
47
|
|
|
* |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getCategories($courseId) |
|
51
|
|
|
{ |
|
52
|
|
|
return Container::getExerciseCategoryRepository()->getCategories($courseId); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param int $courseId |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getCategoriesForSelect($courseId) |
|
61
|
|
|
{ |
|
62
|
|
|
$categories = $this->getCategories($courseId); |
|
63
|
|
|
$options = []; |
|
64
|
|
|
|
|
65
|
|
|
if (!empty($categories)) { |
|
66
|
|
|
/** @var CExerciseCategory $category */ |
|
67
|
|
|
foreach ($categories as $category) { |
|
68
|
|
|
$options[$category->getId()] = $category->getName(); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $options; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param int $id |
|
77
|
|
|
*/ |
|
78
|
|
|
public function delete($id) |
|
79
|
|
|
{ |
|
80
|
|
|
$repo = Container::getExerciseCategoryRepository(); |
|
81
|
|
|
$category = $repo->find($id); |
|
82
|
|
|
$repo->hardDelete($category); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param $primaryKeys |
|
89
|
|
|
* @param $allPrimaryKeys |
|
90
|
|
|
* @param \Symfony\Component\HttpFoundation\Session\Session $session |
|
91
|
|
|
* @param $parameters |
|
92
|
|
|
*/ |
|
93
|
|
|
public function deleteResource( |
|
94
|
|
|
$primaryKeys, |
|
95
|
|
|
$allPrimaryKeys, |
|
|
|
|
|
|
96
|
|
|
Symfony\Component\HttpFoundation\Session\Session $session, |
|
|
|
|
|
|
97
|
|
|
$parameters |
|
|
|
|
|
|
98
|
|
|
) { |
|
99
|
|
|
$repo = Container::getExerciseCategoryRepository(); |
|
100
|
|
|
$translator = Container::getTranslator(); |
|
101
|
|
|
foreach ($primaryKeys as $id) { |
|
102
|
|
|
$category = $repo->find($id); |
|
103
|
|
|
$repo->hardDelete($category); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
Display::addFlash(Display::return_message($translator->trans('Deleted'))); |
|
107
|
|
|
header('Location:'. api_get_self().'?'.api_get_cidreq()); |
|
108
|
|
|
exit; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param array $params |
|
113
|
|
|
* @param bool $showQuery |
|
114
|
|
|
* |
|
115
|
|
|
* @return bool |
|
116
|
|
|
*/ |
|
117
|
|
|
public function update($params, $showQuery = false) |
|
118
|
|
|
{ |
|
119
|
|
|
$id = $params['id']; |
|
120
|
|
|
|
|
121
|
|
|
$repo = Container::getExerciseCategoryRepository(); |
|
122
|
|
|
/** @var CExerciseCategory $category */ |
|
123
|
|
|
$category = $repo->find($id); |
|
124
|
|
|
|
|
125
|
|
|
if ($category) { |
|
|
|
|
|
|
126
|
|
|
$category |
|
127
|
|
|
->setName($params['name']) |
|
128
|
|
|
->setDescription($params['description']) |
|
129
|
|
|
; |
|
130
|
|
|
|
|
131
|
|
|
$repo->getEntityManager()->persist($category); |
|
132
|
|
|
$repo->getEntityManager()->flush(); |
|
133
|
|
|
|
|
134
|
|
|
return true; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
return false; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Save values in the *_field_values table. |
|
142
|
|
|
* |
|
143
|
|
|
* @param array $params Structured array with the values to save |
|
144
|
|
|
* @param bool $showQuery Whether to show the insert query (passed to the parent save() method) |
|
145
|
|
|
*/ |
|
146
|
|
|
public function save($params, $showQuery = false) |
|
147
|
|
|
{ |
|
148
|
|
|
$courseId = api_get_course_int_id(); |
|
149
|
|
|
$course = api_get_course_entity($courseId); |
|
150
|
|
|
|
|
151
|
|
|
$repo = Container::getExerciseCategoryRepository(); |
|
152
|
|
|
$em = $repo->getEntityManager(); |
|
153
|
|
|
|
|
154
|
|
|
$category = new CExerciseCategory(); |
|
155
|
|
|
$category |
|
156
|
|
|
->setName($params['name']) |
|
157
|
|
|
->setCourse($course) |
|
158
|
|
|
->setDescription($params['description']) |
|
159
|
|
|
; |
|
160
|
|
|
|
|
161
|
|
|
/* |
|
162
|
|
|
// Update position |
|
163
|
|
|
$query = $em->getRepository('ChamiloCourseBundle:CExerciseCategory')->createQueryBuilder('e'); |
|
164
|
|
|
$query |
|
165
|
|
|
->where('e.cId = :cId') |
|
166
|
|
|
->setParameter('cId', $courseId) |
|
167
|
|
|
->setMaxResults(1) |
|
168
|
|
|
->orderBy('e.position', 'DESC'); |
|
169
|
|
|
$last = $query->getQuery()->getOneOrNullResult(); |
|
170
|
|
|
$position = 0; |
|
171
|
|
|
if (!empty($last)) { |
|
172
|
|
|
$position = $last->getPosition() + 1; |
|
173
|
|
|
} |
|
174
|
|
|
$category->setPosition($position); |
|
175
|
|
|
*/ |
|
176
|
|
|
$em->persist($category); |
|
177
|
|
|
$em->flush(); |
|
178
|
|
|
|
|
179
|
|
|
$repo->addResourceToCourse( |
|
180
|
|
|
$category, |
|
181
|
|
|
ResourceLink::VISIBILITY_PUBLISHED, |
|
182
|
|
|
api_get_user_entity(api_get_user_id()), |
|
183
|
|
|
$course, |
|
184
|
|
|
api_get_session_entity(), |
|
185
|
|
|
api_get_group_entity() |
|
186
|
|
|
); |
|
187
|
|
|
|
|
188
|
|
|
$em->flush(); |
|
189
|
|
|
|
|
190
|
|
|
return $category; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param string $token |
|
195
|
|
|
* |
|
196
|
|
|
* @return string |
|
197
|
|
|
*/ |
|
198
|
|
|
public function getJqgridActionLinks($token) |
|
199
|
|
|
{ |
|
200
|
|
|
//With this function we can add actions to the jgrid (edit, delete, etc) |
|
201
|
|
|
$editIcon = Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL); |
|
202
|
|
|
$deleteIcon = Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL); |
|
203
|
|
|
/*$editIcon = Display::returnFontAwesomeIcon('pencil'); |
|
204
|
|
|
$deleteIcon = Display::returnFontAwesomeIcon('trash');*/ |
|
205
|
|
|
$confirmMessage = addslashes( |
|
206
|
|
|
api_htmlentities(get_lang('Please confirm your choice'), ENT_QUOTES) |
|
207
|
|
|
); |
|
208
|
|
|
|
|
209
|
|
|
$courseParams = api_get_cidreq(); |
|
210
|
|
|
|
|
211
|
|
|
$editButton = <<<JAVASCRIPT |
|
212
|
|
|
<a href="?action=edit&{$courseParams}&id=' + options.rowId + '" class="">\ |
|
213
|
|
|
$editIcon\ |
|
214
|
|
|
</a> |
|
215
|
|
|
JAVASCRIPT; |
|
216
|
|
|
$deleteButton = <<<JAVASCRIPT |
|
217
|
|
|
<a \ |
|
218
|
|
|
onclick="if (!confirm(\'$confirmMessage\')) {return false;}" \ |
|
219
|
|
|
href="?sec_token=$token&{$courseParams}&id=' + options.rowId + '&action=delete" \ |
|
220
|
|
|
class="">\ |
|
221
|
|
|
$deleteIcon\ |
|
222
|
|
|
</a> |
|
223
|
|
|
JAVASCRIPT; |
|
224
|
|
|
|
|
225
|
|
|
return "function action_formatter(cellvalue, options, rowObject) { |
|
226
|
|
|
return '$editButton $deleteButton'; |
|
227
|
|
|
}"; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* @param string $url |
|
232
|
|
|
* @param string $action |
|
233
|
|
|
* |
|
234
|
|
|
* @return FormValidator |
|
235
|
|
|
*/ |
|
236
|
|
|
public function return_form($url, $action) |
|
237
|
|
|
{ |
|
238
|
|
|
$form = new FormValidator('category', 'post', $url); |
|
239
|
|
|
$id = isset($_GET['id']) ? (int) $_GET['id'] : null; |
|
240
|
|
|
$form->addElement('hidden', 'id', $id); |
|
241
|
|
|
|
|
242
|
|
|
// Setting the form elements |
|
243
|
|
|
$header = get_lang('Add'); |
|
244
|
|
|
$defaults = []; |
|
245
|
|
|
|
|
246
|
|
|
if ($action === 'edit') { |
|
247
|
|
|
$header = get_lang('Edit'); |
|
248
|
|
|
// Setting the defaults |
|
249
|
|
|
$defaults = $this->get($id, false); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
$form->addElement('header', $header); |
|
253
|
|
|
|
|
254
|
|
|
$form->addText( |
|
255
|
|
|
'name', |
|
256
|
|
|
get_lang('Name') |
|
257
|
|
|
); |
|
258
|
|
|
|
|
259
|
|
|
$form->addHtmlEditor('description', get_lang('Description')); |
|
260
|
|
|
|
|
261
|
|
|
if ($action === 'edit') { |
|
262
|
|
|
$form->addButtonUpdate(get_lang('Edit')); |
|
263
|
|
|
} else { |
|
264
|
|
|
$form->addButtonCreate(get_lang('Add')); |
|
265
|
|
|
} |
|
266
|
|
|
$form->setDefaults($defaults); |
|
267
|
|
|
|
|
268
|
|
|
// Setting the rules |
|
269
|
|
|
$form->addRule('name', get_lang('Required field'), 'required'); |
|
270
|
|
|
|
|
271
|
|
|
return $form; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* @return string |
|
276
|
|
|
*/ |
|
277
|
|
|
public function display() |
|
278
|
|
|
{ |
|
279
|
|
|
// Action links |
|
280
|
|
|
$content = '<div class="actions">'; |
|
281
|
|
|
$content .= '<a href="'.api_get_path(WEB_CODE_PATH).'exercise/exercise.php?'.api_get_cidreq().'">'; |
|
282
|
|
|
$content .= Display::return_icon( |
|
283
|
|
|
'back.png', |
|
284
|
|
|
get_lang('Back to').' '.get_lang('Administration'), |
|
285
|
|
|
'', |
|
286
|
|
|
ICON_SIZE_MEDIUM |
|
287
|
|
|
); |
|
288
|
|
|
$content .= '</a>'; |
|
289
|
|
|
$content .= '<a href="'.api_get_self().'?action=add&'.api_get_cidreq().'">'; |
|
290
|
|
|
$content .= Display::return_icon( |
|
291
|
|
|
'add.png', |
|
292
|
|
|
get_lang('Add'), |
|
293
|
|
|
'', |
|
294
|
|
|
ICON_SIZE_MEDIUM |
|
295
|
|
|
); |
|
296
|
|
|
$content .= '</a>'; |
|
297
|
|
|
$content .= '</div>'; |
|
298
|
|
|
|
|
299
|
|
|
// 1. Set entity |
|
300
|
|
|
$source = new Entity('ChamiloCourseBundle:CExerciseCategory'); |
|
301
|
|
|
// 2. Get query builder from repo. |
|
302
|
|
|
$qb = Container::getExerciseCategoryRepository()->getResourcesByCourse(api_get_course_entity()); |
|
303
|
|
|
|
|
304
|
|
|
// 3. Set QueryBuilder to the source. |
|
305
|
|
|
$source-> initQueryBuilder($qb); |
|
306
|
|
|
|
|
307
|
|
|
// 4. Get the grid builder. |
|
308
|
|
|
$builder = Container::$container->get('apy_grid.factory'); |
|
309
|
|
|
|
|
310
|
|
|
// 5. Set parameters and properties. |
|
311
|
|
|
$grid = $builder->createBuilder( |
|
312
|
|
|
'grid', |
|
313
|
|
|
$source, |
|
314
|
|
|
[ |
|
315
|
|
|
'persistence' => false, |
|
316
|
|
|
'route' => 'home', |
|
317
|
|
|
'filterable' => true, |
|
318
|
|
|
'sortable' => true, |
|
319
|
|
|
'max_per_page' => 10, |
|
320
|
|
|
] |
|
321
|
|
|
)->add( |
|
322
|
|
|
'id', |
|
323
|
|
|
'number', |
|
324
|
|
|
[ |
|
325
|
|
|
'title' => '#', |
|
326
|
|
|
'primary' => 'true', |
|
327
|
|
|
] |
|
328
|
|
|
)->add( |
|
329
|
|
|
'name', |
|
330
|
|
|
'text', |
|
331
|
|
|
[ |
|
332
|
|
|
'title' => 'name', |
|
333
|
|
|
] |
|
334
|
|
|
)->add( |
|
335
|
|
|
'description', |
|
336
|
|
|
'text', |
|
337
|
|
|
[ |
|
338
|
|
|
'title' => 'description', |
|
339
|
|
|
] |
|
340
|
|
|
); |
|
341
|
|
|
$grid = $grid->getGrid(); |
|
342
|
|
|
|
|
343
|
|
|
if (Container::getAuthorizationChecker()->isGranted(ResourceNodeVoter::ROLE_CURRENT_COURSE_TEACHER)) { |
|
344
|
|
|
// Add row actions |
|
345
|
|
|
$myRowAction = new RowAction( |
|
346
|
|
|
get_lang('Edit'), |
|
347
|
|
|
'legacy_main', |
|
348
|
|
|
false, |
|
349
|
|
|
'_self', |
|
350
|
|
|
['class' => 'btn btn-secondary'] |
|
351
|
|
|
); |
|
352
|
|
|
$myRowAction->setRouteParameters( |
|
353
|
|
|
['id', 'name' => 'exercise/category.php', 'cidReq' => api_get_course_id(), 'action' => 'edit'] |
|
354
|
|
|
); |
|
355
|
|
|
$grid->addRowAction($myRowAction); |
|
356
|
|
|
|
|
357
|
|
|
$myRowAction = new RowAction( |
|
358
|
|
|
get_lang('Delete'), |
|
359
|
|
|
'legacy_main', |
|
360
|
|
|
true, |
|
361
|
|
|
'_self', |
|
362
|
|
|
['class' => 'btn btn-danger', 'form_delete' => true] |
|
363
|
|
|
); |
|
364
|
|
|
$myRowAction->setRouteParameters( |
|
365
|
|
|
['id', 'name' => 'exercise/category.php', 'cidReq' => api_get_course_id(), 'action' => 'delete'] |
|
366
|
|
|
); |
|
367
|
|
|
$grid->addRowAction($myRowAction); |
|
368
|
|
|
|
|
369
|
|
|
// Add mass actions |
|
370
|
|
|
$deleteMassAction = new MassAction( |
|
371
|
|
|
'Delete', |
|
372
|
|
|
['ExerciseCategoryManager', 'deleteResource'], |
|
373
|
|
|
true, |
|
374
|
|
|
[] |
|
375
|
|
|
); |
|
376
|
|
|
$grid->addMassAction($deleteMassAction); |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
// 8. Set route and request |
|
380
|
|
|
$grid |
|
381
|
|
|
->setRouteUrl(api_get_self().'?'.api_get_cidreq()) |
|
382
|
|
|
->handleRequest(Container::getRequest()) |
|
383
|
|
|
; |
|
384
|
|
|
|
|
385
|
|
|
$content .= Container::$container->get('twig')->render( |
|
386
|
|
|
'@ChamiloTheme/Resource/grid.html.twig', |
|
387
|
|
|
['grid' => $grid] |
|
388
|
|
|
); |
|
389
|
|
|
|
|
390
|
|
|
return $content; |
|
391
|
|
|
} |
|
392
|
|
|
} |
|
393
|
|
|
|