glossary.lib.php 28 KB

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