Total Complexity | 87 |
Total Lines | 874 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like GlossaryManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GlossaryManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class GlossaryManager |
||
17 | { |
||
18 | /** |
||
19 | * Get all glossary terms. |
||
20 | * |
||
21 | * @author Isaac Flores <[email protected]> |
||
22 | * |
||
23 | * @return array Contain glossary terms |
||
24 | */ |
||
25 | public static function get_glossary_terms() |
||
26 | { |
||
27 | $glossary_data = []; |
||
28 | $table = Database::get_course_table(TABLE_GLOSSARY); |
||
29 | $session_id = api_get_session_id(); |
||
30 | $sql_filter = api_get_session_condition($session_id); |
||
31 | $course_id = api_get_course_int_id(); |
||
32 | |||
33 | $sql = "SELECT glossary_id as id, name, description |
||
34 | FROM $table |
||
35 | WHERE c_id = $course_id $sql_filter"; |
||
36 | $rs = Database::query($sql); |
||
37 | while ($row = Database::fetch_array($rs)) { |
||
38 | $glossary_data[] = $row; |
||
39 | } |
||
40 | |||
41 | return $glossary_data; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Get glossary description by glossary id. |
||
46 | * |
||
47 | * @author Isaac Flores <[email protected]> |
||
48 | * |
||
49 | * @param int $glossary_id |
||
50 | * |
||
51 | * @return string The glossary description |
||
52 | */ |
||
53 | public static function get_glossary_term_by_glossary_id($glossary_id) |
||
54 | { |
||
55 | $table = Database::get_course_table(TABLE_GLOSSARY); |
||
56 | $course_id = api_get_course_int_id(); |
||
57 | $glossary_id = (int) $glossary_id; |
||
58 | |||
59 | $sql = "SELECT description |
||
60 | FROM $table |
||
61 | WHERE c_id = $course_id AND glossary_id =".$glossary_id; |
||
62 | $rs = Database::query($sql); |
||
63 | if (Database::num_rows($rs) > 0) { |
||
64 | $row = Database::fetch_array($rs); |
||
65 | |||
66 | return $row['description']; |
||
67 | } |
||
68 | |||
69 | return ''; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Get glossary term by glossary id. |
||
74 | * |
||
75 | * @author Isaac Flores <[email protected]> |
||
76 | * |
||
77 | * @param string $name The glossary term name |
||
78 | * |
||
79 | * @return array The glossary info |
||
80 | */ |
||
81 | public static function get_glossary_term_by_glossary_name($name) |
||
82 | { |
||
83 | $table = Database::get_course_table(TABLE_GLOSSARY); |
||
84 | $session_id = api_get_session_id(); |
||
85 | $course_id = api_get_course_int_id(); |
||
86 | $glossaryName = Security::remove_XSS($name); |
||
87 | $glossaryName = api_convert_encoding($glossaryName, 'UTF-8', 'UTF-8'); |
||
88 | $glossaryName = trim($glossaryName); |
||
89 | $parsed = $glossaryName; |
||
90 | |||
91 | if (api_get_configuration_value('save_titles_as_html')) { |
||
92 | $parsed = api_htmlentities($parsed); |
||
93 | $parsed = "%$parsed%"; |
||
94 | } |
||
95 | |||
96 | $sql = "SELECT * FROM $table |
||
97 | WHERE |
||
98 | c_id = $course_id AND |
||
99 | ( |
||
100 | name LIKE '".Database::escape_string($glossaryName)."' OR |
||
101 | name LIKE '".Database::escape_string($parsed)."' |
||
102 | ) AND |
||
103 | session_id = $session_id |
||
104 | LIMIT 1"; |
||
105 | |||
106 | $rs = Database::query($sql); |
||
107 | |||
108 | if (Database::num_rows($rs) > 0) { |
||
109 | return Database::fetch_array($rs, 'ASSOC'); |
||
110 | } |
||
111 | |||
112 | $sql = "SELECT * FROM $table |
||
113 | WHERE |
||
114 | c_id = $course_id AND |
||
115 | ( |
||
116 | name LIKE '".Database::escape_string($glossaryName)."' OR |
||
117 | name LIKE '".Database::escape_string($parsed)."' |
||
118 | ) AND |
||
119 | (session_id IS NULL OR session_id = 0) |
||
120 | LIMIT 1"; |
||
121 | |||
122 | $rs = Database::query($sql); |
||
123 | |||
124 | if (Database::num_rows($rs) > 0) { |
||
125 | return Database::fetch_array($rs, 'ASSOC'); |
||
126 | } |
||
127 | |||
128 | return []; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * This functions stores the glossary in the database. |
||
133 | * |
||
134 | * @param array $values Array of title + description (name => $title, description => $comment) |
||
135 | * |
||
136 | * @throws \Doctrine\ORM\ORMException |
||
137 | * @throws \Doctrine\ORM\OptimisticLockException |
||
138 | * |
||
139 | * @return bool|int Term id on success, false on failure |
||
140 | */ |
||
141 | public static function save_glossary(array $values, bool $showMessage = true) |
||
142 | { |
||
143 | if (!isset($values['name'])) { |
||
144 | return false; |
||
145 | } |
||
146 | |||
147 | // get the maximum display order of all the glossary items |
||
148 | $max_glossary_item = self::get_max_glossary_item(); |
||
149 | |||
150 | // session_id |
||
151 | $session_id = api_get_session_id(); |
||
152 | |||
153 | // check if the glossary term already exists |
||
154 | if (self::glossary_exists($values['name'])) { |
||
155 | // display the feedback message |
||
156 | if ($showMessage) { |
||
157 | Display::addFlash( |
||
158 | Display::return_message(get_lang('GlossaryTermAlreadyExistsYouShouldEditIt'), 'error') |
||
159 | ); |
||
160 | } |
||
161 | |||
162 | return false; |
||
163 | } |
||
164 | |||
165 | $glossary = (new CGlossary()) |
||
166 | ->setGlossaryId(0) |
||
167 | ->setCId(api_get_course_int_id()) |
||
168 | ->setName($values['name']) |
||
169 | ->setDescription($values['description'] ?? "") |
||
170 | ->setDisplayOrder($max_glossary_item + 1) |
||
171 | ->setSessionId($session_id); |
||
172 | |||
173 | Database::getManager()->persist($glossary); |
||
174 | Database::getManager()->flush(); |
||
175 | |||
176 | $glossary->setGlossaryId($glossary->getIid()); |
||
177 | Database::getManager()->flush(); |
||
178 | |||
179 | //insert into item_property |
||
180 | api_item_property_update( |
||
181 | api_get_course_info(), |
||
182 | TOOL_GLOSSARY, |
||
183 | $glossary->getIid(), |
||
184 | 'GlossaryAdded', |
||
185 | api_get_user_id() |
||
186 | ); |
||
187 | // display the feedback message |
||
188 | if ($showMessage) { |
||
189 | Display::addFlash( |
||
190 | Display::return_message(get_lang('TermAdded')) |
||
191 | ); |
||
192 | } |
||
193 | |||
194 | return $glossary->getIid(); |
||
195 | } |
||
196 | |||
197 | /** |
||
198 | * update the information of a glossary term in the database. |
||
199 | * |
||
200 | * @param array $values an array containing all the form elements |
||
201 | * |
||
202 | * @return bool True on success, false on failure |
||
203 | */ |
||
204 | public static function update_glossary($values, $showMessage = true) |
||
205 | { |
||
206 | // Database table definition |
||
207 | $table = Database::get_course_table(TABLE_GLOSSARY); |
||
208 | $course_id = api_get_course_int_id(); |
||
209 | |||
210 | // check if the glossary term already exists |
||
211 | if (self::glossary_exists($values['name'], $values['glossary_id'])) { |
||
212 | // display the feedback message |
||
213 | if ($showMessage) { |
||
214 | Display::addFlash( |
||
215 | Display::return_message(get_lang('GlossaryTermAlreadyExistsYouShouldEditIt'), 'error') |
||
216 | ); |
||
217 | } |
||
218 | |||
219 | return false; |
||
220 | } else { |
||
221 | $sql = "UPDATE $table SET |
||
222 | name = '".Database::escape_string($values['name'])."', |
||
223 | description = '".Database::escape_string($values['description'])."' |
||
224 | WHERE |
||
225 | c_id = $course_id AND |
||
226 | glossary_id = ".intval($values['glossary_id']); |
||
227 | $result = Database::query($sql); |
||
228 | if (false === $result) { |
||
229 | return false; |
||
230 | } |
||
231 | |||
232 | //update glossary into item_property |
||
233 | api_item_property_update( |
||
234 | api_get_course_info(), |
||
235 | TOOL_GLOSSARY, |
||
236 | intval($values['glossary_id']), |
||
237 | 'GlossaryUpdated', |
||
238 | api_get_user_id() |
||
239 | ); |
||
240 | |||
241 | if ($showMessage) { |
||
242 | // display the feedback message |
||
243 | Display::addFlash( |
||
244 | Display::return_message(get_lang('TermUpdated')) |
||
245 | ); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | return true; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Get the maximum display order of the glossary item. |
||
254 | * |
||
255 | * @return int Maximum glossary display order |
||
256 | */ |
||
257 | public static function get_max_glossary_item() |
||
258 | { |
||
259 | // Database table definition |
||
260 | $table = Database::get_course_table(TABLE_GLOSSARY); |
||
261 | $course_id = api_get_course_int_id(); |
||
262 | $get_max = "SELECT MAX(display_order) FROM $table |
||
263 | WHERE c_id = $course_id "; |
||
264 | $res_max = Database::query($get_max); |
||
265 | if (Database::num_rows($res_max) == 0) { |
||
266 | return 0; |
||
267 | } |
||
268 | $row = Database::fetch_array($res_max); |
||
269 | if (!empty($row[0])) { |
||
270 | return $row[0]; |
||
271 | } |
||
272 | |||
273 | return 0; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * check if the glossary term exists or not. |
||
278 | * |
||
279 | * @param string $term Term to look for |
||
280 | * @param int $not_id ID to counter-check if the term exists with this ID as well (optional) |
||
281 | * |
||
282 | * @return bool True if term exists |
||
283 | */ |
||
284 | public static function glossary_exists($term, $not_id = '') |
||
285 | { |
||
286 | // Database table definition |
||
287 | $table = Database::get_course_table(TABLE_GLOSSARY); |
||
288 | $course_id = api_get_course_int_id(); |
||
289 | |||
290 | $sql = "SELECT name FROM $table |
||
291 | WHERE |
||
292 | c_id = $course_id AND |
||
293 | name = '".Database::escape_string($term)."'"; |
||
294 | if ($not_id != '') { |
||
295 | $sql .= " AND glossary_id <> '".intval($not_id)."'"; |
||
296 | } |
||
297 | $result = Database::query($sql); |
||
298 | $count = Database::num_rows($result); |
||
299 | if ($count > 0) { |
||
300 | return true; |
||
301 | } else { |
||
302 | return false; |
||
303 | } |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Get one specific glossary term data. |
||
308 | * |
||
309 | * @param int $glossary_id ID of the glossary term |
||
310 | * |
||
311 | * @return mixed Array(glossary_id,name,description,glossary_display_order) or false on error |
||
312 | */ |
||
313 | public static function get_glossary_information($glossary_id) |
||
314 | { |
||
315 | // Database table definition |
||
316 | $t_glossary = Database::get_course_table(TABLE_GLOSSARY); |
||
317 | $t_item_propery = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
318 | if (empty($glossary_id)) { |
||
319 | return false; |
||
320 | } |
||
321 | $sql = "SELECT |
||
322 | g.glossary_id as glossary_id, |
||
323 | g.name as name, |
||
324 | g.description as description, |
||
325 | g.display_order as glossary_display_order, |
||
326 | ip.insert_date as insert_date, |
||
327 | ip.lastedit_date as update_date, |
||
328 | g.session_id |
||
329 | FROM $t_glossary g |
||
330 | INNER JOIN $t_item_propery ip |
||
331 | ON (g.glossary_id = ip.ref AND g.c_id = ip.c_id) |
||
332 | WHERE |
||
333 | tool = '".TOOL_GLOSSARY."' AND |
||
334 | g.glossary_id = '".intval($glossary_id)."' AND |
||
335 | g.c_id = ".api_get_course_int_id()." AND |
||
336 | ip.c_id = ".api_get_course_int_id(); |
||
337 | |||
338 | $result = Database::query($sql); |
||
339 | if ($result === false || Database::num_rows($result) != 1) { |
||
340 | return false; |
||
341 | } |
||
342 | |||
343 | return Database::fetch_array($result); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * Delete a glossary term (and re-order all the others). |
||
348 | * |
||
349 | * @param int $glossary_id |
||
350 | * @param bool $showMessage |
||
351 | * |
||
352 | * @return bool True on success, false on failure |
||
353 | */ |
||
354 | public static function delete_glossary($glossary_id, $showMessage = true) |
||
355 | { |
||
356 | // Database table definition |
||
357 | $table = Database::get_course_table(TABLE_GLOSSARY); |
||
358 | $course_id = api_get_course_int_id(); |
||
359 | $glossaryInfo = self::get_glossary_information($glossary_id); |
||
360 | |||
361 | if (empty($glossaryInfo)) { |
||
362 | return false; |
||
363 | } |
||
364 | |||
365 | $glossary_id = (int) $glossary_id; |
||
366 | |||
367 | $sql = "DELETE FROM $table |
||
368 | WHERE |
||
369 | c_id = $course_id AND |
||
370 | glossary_id='".$glossary_id."'"; |
||
371 | $result = Database::query($sql); |
||
372 | if ($result === false || Database::affected_rows($result) < 1) { |
||
373 | return false; |
||
374 | } |
||
375 | |||
376 | // update item_property (delete) |
||
377 | api_item_property_update( |
||
378 | api_get_course_info(), |
||
379 | TOOL_GLOSSARY, |
||
380 | $glossary_id, |
||
381 | 'delete', |
||
382 | api_get_user_id() |
||
383 | ); |
||
384 | |||
385 | // reorder the remaining terms |
||
386 | self::reorder_glossary(); |
||
387 | |||
388 | if ($showMessage) { |
||
389 | Display::addFlash( |
||
390 | Display::return_message( |
||
391 | get_lang('TermDeleted').': '.Security::remove_XSS($glossaryInfo['name']), |
||
392 | 'normal', |
||
393 | false |
||
394 | ) |
||
395 | ); |
||
396 | } |
||
397 | |||
398 | return true; |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * @return string |
||
403 | */ |
||
404 | public static function getGlossaryView() |
||
405 | { |
||
406 | $view = Session::read('glossary_view'); |
||
407 | if (empty($view)) { |
||
408 | $defaultView = api_get_configuration_value('default_glossary_view'); |
||
409 | if (empty($defaultView)) { |
||
410 | $defaultView = 'table'; |
||
411 | } |
||
412 | |||
413 | return $defaultView; |
||
|
|||
414 | } else { |
||
415 | return $view; |
||
416 | } |
||
417 | } |
||
418 | |||
419 | /** |
||
420 | * This is the main function that displays the list or the table with all |
||
421 | * the glossary terms |
||
422 | * Defaults to 'table' and prefers glossary_view from the session by default. |
||
423 | * |
||
424 | * @return string |
||
425 | */ |
||
426 | public static function display_glossary() |
||
427 | { |
||
428 | // This function should always be called with the corresponding |
||
429 | // parameter for view type. Meanwhile, use this cheap trick. |
||
430 | $view = self::getGlossaryView(); |
||
431 | // action links |
||
432 | $actionsLeft = ''; |
||
433 | if (api_is_allowed_to_edit(null, true)) { |
||
434 | $actionsLeft .= '<a href="index.php?'.api_get_cidreq().'&action=addglossary&msg=add?'.api_get_cidreq().'">'. |
||
435 | Display::return_icon('new_glossary_term.png', get_lang('TermAddNew'), '', ICON_SIZE_MEDIUM).'</a>'; |
||
436 | } |
||
437 | |||
438 | if (api_is_allowed_to_edit(null, true)) { |
||
439 | $actionsLeft .= '<a href="index.php?'.api_get_cidreq().'&action=import">'. |
||
440 | Display::return_icon('import.png', get_lang('ImportGlossary'), '', ICON_SIZE_MEDIUM).'</a>'; |
||
441 | } |
||
442 | |||
443 | if (!api_is_anonymous()) { |
||
444 | $actionsLeft .= '<a id="export_opener" href="'.api_get_self().'?'.api_get_cidreq().'&action=export">'. |
||
445 | Display::return_icon('save.png', get_lang('Export'), '', ICON_SIZE_MEDIUM).'</a>'; |
||
446 | } |
||
447 | |||
448 | if ($view === 'table' || !isset($view)) { |
||
449 | $actionsLeft .= '<a href="index.php?'.api_get_cidreq().'&action=changeview&view=list">'. |
||
450 | Display::return_icon('view_detailed.png', get_lang('ListView'), '', ICON_SIZE_MEDIUM).'</a>'; |
||
451 | } else { |
||
452 | $actionsLeft .= '<a href="index.php?'.api_get_cidreq().'&action=changeview&view=table">'. |
||
453 | Display::return_icon('view_text.png', get_lang('TableView'), '', ICON_SIZE_MEDIUM).'</a>'; |
||
454 | } |
||
455 | |||
456 | if (api_is_allowed_to_edit(true, true, true)) { |
||
457 | $actionsLeft .= Display::url( |
||
458 | Display::return_icon('export_to_documents.png', get_lang('ExportToDocArea'), [], ICON_SIZE_MEDIUM), |
||
459 | api_get_self().'?'.api_get_cidreq().'&'.http_build_query(['action' => 'export_documents']) |
||
460 | ); |
||
461 | } |
||
462 | |||
463 | /* BUILD SEARCH FORM */ |
||
464 | $form = new FormValidator( |
||
465 | 'search', |
||
466 | 'get', |
||
467 | api_get_self().'?'.api_get_cidreq(), |
||
468 | '', |
||
469 | [], |
||
470 | FormValidator::LAYOUT_INLINE |
||
471 | ); |
||
472 | $form->addText('keyword', '', false, ['class' => 'col-md-2']); |
||
473 | $form->addElement('hidden', 'cidReq', api_get_course_id()); |
||
474 | $form->addElement('hidden', 'id_session', api_get_session_id()); |
||
475 | $form->addButtonSearch(get_lang('Search')); |
||
476 | $actionsRight = $form->returnForm(); |
||
477 | |||
478 | $toolbar = Display::toolbarAction( |
||
479 | 'toolbar-document', |
||
480 | [$actionsLeft, $actionsRight] |
||
481 | ); |
||
482 | |||
483 | $content = $toolbar; |
||
484 | |||
485 | if (!$view || $view === 'table') { |
||
486 | $table = new SortableTable( |
||
487 | 'glossary', |
||
488 | ['GlossaryManager', 'get_number_glossary_terms'], |
||
489 | ['GlossaryManager', 'get_glossary_data'], |
||
490 | 0 |
||
491 | ); |
||
492 | $table->set_header(0, get_lang('TermName'), true); |
||
493 | $table->set_header(1, get_lang('TermDefinition'), true); |
||
494 | if (api_is_allowed_to_edit(null, true)) { |
||
495 | $table->set_header(2, get_lang('Actions'), false, 'width=90px', ['class' => 'td_actions']); |
||
496 | $table->set_column_filter(2, ['GlossaryManager', 'actions_filter']); |
||
497 | } |
||
498 | $content .= $table->return_table(); |
||
499 | } |
||
500 | if ($view === 'list') { |
||
501 | $content .= self::displayGlossaryList(); |
||
502 | } |
||
503 | |||
504 | return $content; |
||
505 | } |
||
506 | |||
507 | /** |
||
508 | * Display the glossary terms in a list. |
||
509 | * |
||
510 | * @return bool true |
||
511 | */ |
||
512 | public static function displayGlossaryList() |
||
513 | { |
||
514 | $glossaryList = self::get_glossary_data(0, 1000, 0, 'ASC'); |
||
515 | $content = ''; |
||
516 | foreach ($glossaryList as $key => $glossary_item) { |
||
517 | $actions = ''; |
||
518 | if (api_is_allowed_to_edit(null, true)) { |
||
519 | $actions = '<div class="pull-right">'. |
||
520 | self::actions_filter($glossary_item[2], '', $glossary_item). |
||
521 | '</div>'; |
||
522 | } |
||
523 | $content .= Display::panel($glossary_item[1], $glossary_item[0].' '.$actions); |
||
524 | } |
||
525 | |||
526 | return $content; |
||
527 | } |
||
528 | |||
529 | /** |
||
530 | * Get the number of glossary terms in the course (or course+session). |
||
531 | * |
||
532 | * @param int Session ID filter (optional) |
||
533 | * |
||
534 | * @return int Count of glossary terms |
||
535 | */ |
||
536 | public static function get_number_glossary_terms($session_id = 0) |
||
537 | { |
||
538 | // Database table definition |
||
539 | $t_glossary = Database::get_course_table(TABLE_GLOSSARY); |
||
540 | $course_id = api_get_course_int_id(); |
||
541 | $session_id = (int) $session_id; |
||
542 | if (empty($session_id)) { |
||
543 | $session_id = api_get_session_id(); |
||
544 | } |
||
545 | $sql_filter = api_get_session_condition($session_id, true, true); |
||
546 | |||
547 | $keyword = isset($_GET['keyword']) ? Database::escape_string($_GET['keyword']) : ''; |
||
548 | $keywordCondition = ''; |
||
549 | if (!empty($keyword)) { |
||
550 | $keywordCondition = "AND (name LIKE '%$keyword%' OR description LIKE '%$keyword%')"; |
||
551 | } |
||
552 | |||
553 | $sql = "SELECT count(glossary_id) as total |
||
554 | FROM $t_glossary |
||
555 | WHERE c_id = $course_id $sql_filter |
||
556 | $keywordCondition "; |
||
557 | |||
558 | $res = Database::query($sql); |
||
559 | if ($res === false) { |
||
560 | return 0; |
||
561 | } |
||
562 | |||
563 | $obj = Database::fetch_object($res); |
||
564 | |||
565 | return $obj->total; |
||
566 | } |
||
567 | |||
568 | /** |
||
569 | * Get all the data of a glossary. |
||
570 | * |
||
571 | * @param int $from From which item |
||
572 | * @param int $number_of_items Number of items to collect |
||
573 | * @param string $column Name of column on which to order |
||
574 | * @param string $direction Whether to sort in ascending (ASC) or descending (DESC) |
||
575 | * |
||
576 | * @return array |
||
577 | */ |
||
578 | public static function get_glossary_data( |
||
579 | $from, |
||
580 | $number_of_items, |
||
581 | $column, |
||
582 | $direction |
||
583 | ) { |
||
584 | $_user = api_get_user_info(); |
||
585 | $view = self::getGlossaryView(); |
||
586 | $t_glossary = Database::get_course_table(TABLE_GLOSSARY); |
||
587 | $t_item_propery = Database::get_course_table(TABLE_ITEM_PROPERTY); |
||
588 | |||
589 | $col2 = ' '; |
||
590 | if (api_is_allowed_to_edit(null, true)) { |
||
591 | $col2 = ' glossary.glossary_id as col2, '; |
||
592 | } |
||
593 | |||
594 | // Condition for the session |
||
595 | $session_id = api_get_session_id(); |
||
596 | $condition_session = api_get_session_condition( |
||
597 | $session_id, |
||
598 | true, |
||
599 | true, |
||
600 | 'glossary.session_id' |
||
601 | ); |
||
602 | |||
603 | $column = (int) $column; |
||
604 | $from = (int) $from; |
||
605 | $number_of_items = (int) $number_of_items; |
||
606 | |||
607 | if (!in_array($direction, ['DESC', 'ASC'])) { |
||
608 | $direction = 'ASC'; |
||
609 | } |
||
610 | |||
611 | $keyword = isset($_GET['keyword']) ? Database::escape_string($_GET['keyword']) : ''; |
||
612 | $keywordCondition = ''; |
||
613 | if (!empty($keyword)) { |
||
614 | $keywordCondition = "AND (glossary.name LIKE '%$keyword%' OR glossary.description LIKE '%$keyword%')"; |
||
615 | } |
||
616 | $sql = "SELECT |
||
617 | glossary.name as col0, |
||
618 | glossary.description as col1, |
||
619 | $col2 |
||
620 | glossary.session_id |
||
621 | FROM $t_glossary glossary |
||
622 | INNER JOIN $t_item_propery ip |
||
623 | ON (glossary.glossary_id = ip.ref AND glossary.c_id = ip.c_id) |
||
624 | WHERE |
||
625 | tool = '".TOOL_GLOSSARY."' |
||
626 | $condition_session AND |
||
627 | glossary.c_id = ".api_get_course_int_id()." AND |
||
628 | ip.c_id = ".api_get_course_int_id()." |
||
629 | $keywordCondition |
||
630 | ORDER BY col$column $direction |
||
631 | LIMIT $from, $number_of_items"; |
||
632 | |||
633 | $res = Database::query($sql); |
||
634 | |||
635 | $return = []; |
||
636 | $array = []; |
||
637 | while ($data = Database::fetch_array($res)) { |
||
638 | // Validation when belongs to a session |
||
639 | $session_img = api_get_session_image($data['session_id'], $_user['status']); |
||
640 | $array[0] = Security::remove_XSS($data[0]).$session_img; |
||
641 | |||
642 | if (!$view || $view === 'table') { |
||
643 | $array[1] = Security::remove_XSS(str_replace(['<p>', '</p>'], ['', '<br />'], $data[1])); |
||
644 | } else { |
||
645 | $array[1] = $data[1]; |
||
646 | } |
||
647 | |||
648 | if (isset($_GET['action']) && $_GET['action'] === 'export') { |
||
649 | $array[1] = api_html_entity_decode($data[1]); |
||
650 | } |
||
651 | |||
652 | if (api_is_allowed_to_edit(null, true)) { |
||
653 | $array[2] = $data[2]; |
||
654 | } |
||
655 | $return[] = $array; |
||
656 | } |
||
657 | |||
658 | return $return; |
||
659 | } |
||
660 | |||
661 | /** |
||
662 | * Update action icons column. |
||
663 | * |
||
664 | * @param int $glossary_id |
||
665 | * @param array $url_params Parameters to use to affect links |
||
666 | * @param array $row The line of results from a query on the glossary table |
||
667 | * |
||
668 | * @return string HTML string for the action icons columns |
||
669 | */ |
||
670 | public static function actions_filter($glossary_id, $url_params, $row) |
||
671 | { |
||
672 | $glossary_id = $row[2]; |
||
673 | $return = '<a href="'.api_get_self().'?action=edit_glossary&glossary_id='.$glossary_id.'&'.api_get_cidreq().'&msg=edit">'. |
||
674 | Display::return_icon('edit.png', get_lang('Edit'), '', 22).'</a>'; |
||
675 | $glossary_data = self::get_glossary_information($glossary_id); |
||
676 | $glossary_term = Security::remove_XSS(strip_tags($glossary_data['name'])); |
||
677 | if (api_is_allowed_to_edit(null, true)) { |
||
678 | if ($glossary_data['session_id'] == api_get_session_id()) { |
||
679 | $return .= Display::url( |
||
680 | Display::return_icon('delete.png', get_lang('Delete')), |
||
681 | '#', |
||
682 | [ |
||
683 | 'data-item-title' => $glossary_term, |
||
684 | 'data-href' => api_get_self().'?action=delete_glossary&glossary_id='.$glossary_id.'&'.api_get_cidreq(), |
||
685 | 'data-toggle' => 'modal', |
||
686 | 'data-target' => '#confirm-delete', |
||
687 | ] |
||
688 | ); |
||
689 | } else { |
||
690 | $return = get_lang('EditionNotAvailableFromSession'); |
||
691 | } |
||
692 | } |
||
693 | |||
694 | return $return; |
||
695 | } |
||
696 | |||
697 | /** |
||
698 | * a little bit of javascript to display a prettier warning when deleting a term. |
||
699 | * |
||
700 | * @return string HTML string including JavaScript |
||
701 | */ |
||
702 | public static function javascript_glossary() |
||
707 | return true; |
||
708 | } else { |
||
709 | return false; |
||
710 | } |
||
711 | } |
||
712 | </script>"; |
||
713 | } |
||
714 | |||
715 | /** |
||
716 | * Re-order glossary. |
||
717 | */ |
||
718 | public static function reorder_glossary() |
||
719 | { |
||
720 | // Database table definition |
||
721 | $table = Database::get_course_table(TABLE_GLOSSARY); |
||
722 | $course_id = api_get_course_int_id(); |
||
723 | $sql = "SELECT * FROM $table |
||
724 | WHERE c_id = $course_id |
||
725 | ORDER by display_order ASC"; |
||
726 | $res = Database::query($sql); |
||
727 | |||
728 | $i = 1; |
||
729 | while ($data = Database::fetch_array($res)) { |
||
730 | $sql = "UPDATE $table SET display_order = $i |
||
731 | WHERE c_id = $course_id AND glossary_id = '".intval($data['glossary_id'])."'"; |
||
732 | Database::query($sql); |
||
733 | $i++; |
||
734 | } |
||
735 | } |
||
736 | |||
737 | /** |
||
738 | * Move a glossary term. |
||
739 | * |
||
740 | * @param string $direction |
||
741 | * @param string $glossary_id |
||
742 | */ |
||
743 | public static function move_glossary($direction, $glossary_id) |
||
744 | { |
||
745 | // Database table definition |
||
746 | $table = Database::get_course_table(TABLE_GLOSSARY); |
||
747 | |||
748 | // sort direction |
||
749 | if ($direction === 'up') { |
||
750 | $sortorder = 'DESC'; |
||
751 | } else { |
||
752 | $sortorder = 'ASC'; |
||
753 | } |
||
754 | $course_id = api_get_course_int_id(); |
||
755 | |||
756 | $sql = "SELECT * FROM $table |
||
757 | WHERE c_id = $course_id |
||
758 | ORDER BY display_order $sortorder"; |
||
759 | $res = Database::query($sql); |
||
760 | $found = false; |
||
761 | while ($row = Database::fetch_array($res)) { |
||
762 | if ($found && empty($next_id)) { |
||
763 | $next_id = $row['glossary_id']; |
||
764 | $next_display_order = $row['display_order']; |
||
765 | } |
||
766 | |||
767 | if ($row['glossary_id'] == $glossary_id) { |
||
768 | $current_id = $glossary_id; |
||
769 | $current_display_order = $row['display_order']; |
||
770 | $found = true; |
||
771 | } |
||
772 | } |
||
773 | |||
774 | $sql1 = "UPDATE $table SET display_order = '".Database::escape_string($next_display_order)."' |
||
775 | WHERE c_id = $course_id AND glossary_id = '".Database::escape_string($current_id)."'"; |
||
776 | $sql2 = "UPDATE $table SET display_order = '".Database::escape_string($current_display_order)."' |
||
777 | WHERE c_id = $course_id AND glossary_id = '".Database::escape_string($next_id)."'"; |
||
778 | Database::query($sql1); |
||
779 | Database::query($sql2); |
||
780 | |||
781 | Display::addFlash(Display::return_message(get_lang('TermMoved'))); |
||
782 | } |
||
783 | |||
784 | /** |
||
785 | * Export to pdf. |
||
786 | */ |
||
787 | public static function export_to_pdf() |
||
788 | { |
||
789 | $data = self::get_glossary_data( |
||
790 | 0, |
||
791 | self::get_number_glossary_terms(api_get_session_id()), |
||
792 | 0, |
||
793 | 'ASC' |
||
794 | ); |
||
795 | $template = new Template('', false, false, false, true, false, false); |
||
796 | $layout = $template->get_template('glossary/export_pdf.tpl'); |
||
797 | $template->assign('items', $data); |
||
798 | |||
799 | $html = $template->fetch($layout); |
||
800 | $courseCode = api_get_course_id(); |
||
801 | $pdf = new PDF(); |
||
802 | $pdf->content_to_pdf($html, '', get_lang('Glossary').'_'.$courseCode, $courseCode); |
||
803 | } |
||
804 | |||
805 | /** |
||
806 | * Generate a PDF with all glossary terms and move file to documents. |
||
807 | * |
||
808 | * @return bool false if there's nothing in the glossary |
||
809 | */ |
||
810 | public static function movePdfToDocuments() |
||
811 | { |
||
812 | $sessionId = api_get_session_id(); |
||
813 | $courseId = api_get_course_int_id(); |
||
814 | $data = self::get_glossary_data( |
||
815 | 0, |
||
816 | self::get_number_glossary_terms($sessionId), |
||
817 | 0, |
||
818 | 'ASC' |
||
819 | ); |
||
820 | |||
821 | if (!empty($data)) { |
||
822 | $template = new Template('', false, false, false, true, false, false); |
||
823 | $layout = $template->get_template('glossary/export_pdf.tpl'); |
||
824 | $template->assign('items', $data); |
||
825 | $fileName = get_lang('Glossary').'-'.api_get_local_time(); |
||
826 | $signatures = ['Drh', 'Teacher', 'Date']; |
||
827 | |||
828 | $pdf = new PDF( |
||
829 | 'A4-P', |
||
830 | 'P', |
||
831 | [ |
||
832 | 'filename' => $fileName, |
||
833 | 'pdf_title' => $fileName, |
||
834 | 'add_signatures' => $signatures, |
||
835 | ] |
||
836 | ); |
||
837 | $pdf->exportFromHtmlToDocumentsArea( |
||
838 | $template->fetch($layout), |
||
839 | $fileName, |
||
840 | $courseId |
||
841 | ); |
||
842 | |||
843 | return true; |
||
844 | } else { |
||
845 | Display::addFlash(Display::return_message(get_lang('NothingToAdd'))); |
||
846 | } |
||
847 | |||
848 | return false; |
||
849 | } |
||
850 | |||
851 | /** |
||
852 | * @param string $format |
||
853 | */ |
||
854 | public static function exportToFormat($format) |
||
890 | } |
||
891 | } |
||
892 | } |
||
893 |