glossary.lib.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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(get_lang('TermDeleted').': '.$glossaryInfo['name'])
  323. );
  324. }
  325. return true;
  326. }
  327. /**
  328. * @return string
  329. */
  330. public static function getGlossaryView()
  331. {
  332. $view = Session::read('glossary_view');
  333. if (empty($view)) {
  334. $defaultView = api_get_configuration_value('default_glossary_view');
  335. if (empty($defaultView)) {
  336. $defaultView = 'table';
  337. }
  338. return $defaultView;
  339. } else {
  340. return $view;
  341. }
  342. }
  343. /**
  344. * This is the main function that displays the list or the table with all
  345. * the glossary terms
  346. * Defaults to 'table' and prefers glossary_view from the session by default.
  347. *
  348. * @return string
  349. */
  350. public static function display_glossary()
  351. {
  352. // This function should always be called with the corresponding
  353. // parameter for view type. Meanwhile, use this cheap trick.
  354. $view = self::getGlossaryView();
  355. // action links
  356. $actionsLeft = '';
  357. if (api_is_allowed_to_edit(null, true)) {
  358. $actionsLeft .= '<a href="index.php?'.api_get_cidreq().'&action=addglossary&msg=add?'.api_get_cidreq().'">'.
  359. Display::return_icon('new_glossary_term.png', get_lang('TermAddNew'), '', ICON_SIZE_MEDIUM).'</a>';
  360. }
  361. if (api_is_allowed_to_edit(null, true)) {
  362. $actionsLeft .= '<a href="index.php?'.api_get_cidreq().'&action=import">'.
  363. Display::return_icon('import.png', get_lang('ImportGlossary'), '', ICON_SIZE_MEDIUM).'</a>';
  364. }
  365. if (!api_is_anonymous()) {
  366. $actionsLeft .= '<a id="export_opener" href="'.api_get_self().'?'.api_get_cidreq().'&action=export">'.
  367. Display::return_icon('save.png', get_lang('Export'), '', ICON_SIZE_MEDIUM).'</a>';
  368. }
  369. if ($view === 'table' || !isset($view)) {
  370. $actionsLeft .= '<a href="index.php?'.api_get_cidreq().'&action=changeview&view=list">'.
  371. Display::return_icon('view_detailed.png', get_lang('ListView'), '', ICON_SIZE_MEDIUM).'</a>';
  372. } else {
  373. $actionsLeft .= '<a href="index.php?'.api_get_cidreq().'&action=changeview&view=table">'.
  374. Display::return_icon('view_text.png', get_lang('TableView'), '', ICON_SIZE_MEDIUM).'</a>';
  375. }
  376. if (api_is_allowed_to_edit(true, true, true)) {
  377. $actionsLeft .= Display::url(
  378. Display::return_icon('export_to_documents.png', get_lang('ExportToDocArea'), [], ICON_SIZE_MEDIUM),
  379. api_get_self().'?'.api_get_cidreq().'&'.http_build_query(['action' => 'export_documents'])
  380. );
  381. }
  382. /* BUILD SEARCH FORM */
  383. $form = new FormValidator(
  384. 'search',
  385. 'get',
  386. api_get_self().'?'.api_get_cidreq(),
  387. '',
  388. [],
  389. FormValidator::LAYOUT_INLINE
  390. );
  391. $form->addText('keyword', '', false, ['class' => 'col-md-2']);
  392. $form->addElement('hidden', 'cidReq', api_get_course_id());
  393. $form->addElement('hidden', 'id_session', api_get_session_id());
  394. $form->addButtonSearch(get_lang('Search'));
  395. $actionsRight = $form->returnForm();
  396. $toolbar = Display::toolbarAction(
  397. 'toolbar-document',
  398. [$actionsLeft, $actionsRight]
  399. );
  400. $content = $toolbar;
  401. if (!$view || $view === 'table') {
  402. $table = new SortableTable(
  403. 'glossary',
  404. ['GlossaryManager', 'get_number_glossary_terms'],
  405. ['GlossaryManager', 'get_glossary_data'],
  406. 0
  407. );
  408. $table->set_header(0, get_lang('TermName'), true);
  409. $table->set_header(1, get_lang('TermDefinition'), true);
  410. if (api_is_allowed_to_edit(null, true)) {
  411. $table->set_header(2, get_lang('Actions'), false, 'width=90px', ['class' => 'td_actions']);
  412. $table->set_column_filter(2, ['GlossaryManager', 'actions_filter']);
  413. }
  414. $content .= $table->return_table();
  415. }
  416. if ($view === 'list') {
  417. $content .= self::displayGlossaryList();
  418. }
  419. return $content;
  420. }
  421. /**
  422. * Display the glossary terms in a list.
  423. *
  424. * @return bool true
  425. */
  426. public static function displayGlossaryList()
  427. {
  428. $glossaryList = self::get_glossary_data(0, 1000, 0, 'ASC');
  429. $content = '';
  430. foreach ($glossaryList as $key => $glossary_item) {
  431. $actions = '';
  432. if (api_is_allowed_to_edit(null, true)) {
  433. $actions = '<div class="pull-right">'.self::actions_filter($glossary_item[2], '', $glossary_item).'</div>';
  434. }
  435. $content .= Display::panel($glossary_item[1], $glossary_item[0].' '.$actions);
  436. }
  437. return $content;
  438. }
  439. /**
  440. * Get the number of glossary terms in the course (or course+session).
  441. *
  442. * @param int Session ID filter (optional)
  443. *
  444. * @return int Count of glossary terms
  445. */
  446. public static function get_number_glossary_terms($session_id = 0)
  447. {
  448. // Database table definition
  449. $t_glossary = Database::get_course_table(TABLE_GLOSSARY);
  450. $course_id = api_get_course_int_id();
  451. $session_id = (int) $session_id;
  452. $sql_filter = api_get_session_condition($session_id, true, true);
  453. $keyword = isset($_GET['keyword']) ? Database::escape_string($_GET['keyword']) : '';
  454. $keywordCondition = '';
  455. if (!empty($keyword)) {
  456. $keywordCondition = "AND (name LIKE '%$keyword%' OR description LIKE '%$keyword%')";
  457. }
  458. $sql = "SELECT count(glossary_id) as total
  459. FROM $t_glossary
  460. WHERE c_id = $course_id $sql_filter
  461. $keywordCondition ";
  462. $res = Database::query($sql);
  463. if ($res === false) {
  464. return 0;
  465. }
  466. $obj = Database::fetch_object($res);
  467. return $obj->total;
  468. }
  469. /**
  470. * Get all the data of a glossary.
  471. *
  472. * @param int $from From which item
  473. * @param int $number_of_items Number of items to collect
  474. * @param string $column Name of column on which to order
  475. * @param string $direction Whether to sort in ascending (ASC) or descending (DESC)
  476. *
  477. * @return array
  478. */
  479. public static function get_glossary_data(
  480. $from,
  481. $number_of_items,
  482. $column,
  483. $direction
  484. ) {
  485. $_user = api_get_user_info();
  486. $view = self::getGlossaryView();
  487. // Database table definition
  488. $t_glossary = Database::get_course_table(TABLE_GLOSSARY);
  489. $t_item_propery = Database::get_course_table(TABLE_ITEM_PROPERTY);
  490. if (api_is_allowed_to_edit(null, true)) {
  491. $col2 = ' glossary.glossary_id as col2, ';
  492. } else {
  493. $col2 = ' ';
  494. }
  495. // Condition for the session
  496. $session_id = api_get_session_id();
  497. $condition_session = api_get_session_condition(
  498. $session_id,
  499. true,
  500. true,
  501. 'glossary.session_id'
  502. );
  503. $column = (int) $column;
  504. $from = (int) $from;
  505. $number_of_items = (int) $number_of_items;
  506. if (!in_array($direction, ['DESC', 'ASC'])) {
  507. $direction = 'ASC';
  508. }
  509. $keyword = isset($_GET['keyword']) ? Database::escape_string($_GET['keyword']) : '';
  510. $keywordCondition = '';
  511. if (!empty($keyword)) {
  512. $keywordCondition = "AND (glossary.name LIKE '%$keyword%' OR glossary.description LIKE '%$keyword%')";
  513. }
  514. $sql = "SELECT
  515. glossary.name as col0,
  516. glossary.description as col1,
  517. $col2
  518. glossary.session_id
  519. FROM $t_glossary glossary
  520. INNER JOIN $t_item_propery ip
  521. ON (glossary.glossary_id = ip.ref AND glossary.c_id = ip.c_id)
  522. WHERE
  523. tool = '".TOOL_GLOSSARY."'
  524. $condition_session AND
  525. glossary.c_id = ".api_get_course_int_id()." AND
  526. ip.c_id = ".api_get_course_int_id()."
  527. $keywordCondition
  528. ORDER BY col$column $direction
  529. LIMIT $from, $number_of_items";
  530. $res = Database::query($sql);
  531. $return = [];
  532. $array = [];
  533. while ($data = Database::fetch_array($res)) {
  534. // Validation when belongs to a session
  535. $session_img = api_get_session_image($data['session_id'], $_user['status']);
  536. $array[0] = $data[0].$session_img;
  537. if (!$view || $view === 'table') {
  538. $array[1] = str_replace(['<p>', '</p>'], ['', '<br />'], $data[1]);
  539. } else {
  540. $array[1] = $data[1];
  541. }
  542. if (isset($_GET['action']) && $_GET['action'] === 'export') {
  543. $array[1] = api_html_entity_decode($data[1]);
  544. }
  545. if (api_is_allowed_to_edit(null, true)) {
  546. $array[2] = $data[2];
  547. }
  548. $return[] = $array;
  549. }
  550. return $return;
  551. }
  552. /**
  553. * Update action icons column.
  554. *
  555. * @param int $glossary_id
  556. * @param array $url_params Parameters to use to affect links
  557. * @param array $row The line of results from a query on the glossary table
  558. *
  559. * @return string HTML string for the action icons columns
  560. */
  561. public static function actions_filter($glossary_id, $url_params, $row)
  562. {
  563. $glossary_id = $row[2];
  564. $return = '<a href="'.api_get_self().'?action=edit_glossary&glossary_id='.$glossary_id.'&'.api_get_cidreq().'&msg=edit">'.
  565. Display::return_icon('edit.png', get_lang('Edit'), '', 22).'</a>';
  566. $glossary_data = self::get_glossary_information($glossary_id);
  567. $glossary_term = $glossary_data['name'];
  568. if (api_is_allowed_to_edit(null, true)) {
  569. if ($glossary_data['session_id'] == api_get_session_id()) {
  570. $return .= '<a href="'.api_get_self().'?action=delete_glossary&glossary_id='.$glossary_id.'&'.api_get_cidreq().'" onclick="return confirmation(\''.$glossary_term.'\');">'.
  571. Display::return_icon('delete.png', get_lang('Delete'), '', 22).'</a>';
  572. } else {
  573. $return = get_lang('EditionNotAvailableFromSession');
  574. }
  575. }
  576. return $return;
  577. }
  578. /**
  579. * a little bit of javascript to display a prettier warning when deleting a term.
  580. *
  581. * @return string HTML string including JavaScript
  582. */
  583. public static function javascript_glossary()
  584. {
  585. return "<script>
  586. function confirmation (name) {
  587. if (confirm(\" ".get_lang("TermConfirmDelete")." \"+ name + \" ?\")) {
  588. return true;
  589. } else {
  590. return false;
  591. }
  592. }
  593. </script>";
  594. }
  595. /**
  596. * Re-order glossary.
  597. */
  598. public static function reorder_glossary()
  599. {
  600. // Database table definition
  601. $table = Database::get_course_table(TABLE_GLOSSARY);
  602. $course_id = api_get_course_int_id();
  603. $sql = "SELECT * FROM $table
  604. WHERE c_id = $course_id
  605. ORDER by display_order ASC";
  606. $res = Database::query($sql);
  607. $i = 1;
  608. while ($data = Database::fetch_array($res)) {
  609. $sql = "UPDATE $table SET display_order = $i
  610. WHERE c_id = $course_id AND glossary_id = '".intval($data['glossary_id'])."'";
  611. Database::query($sql);
  612. $i++;
  613. }
  614. }
  615. /**
  616. * Move a glossary term.
  617. *
  618. * @param string $direction
  619. * @param string $glossary_id
  620. */
  621. public static function move_glossary($direction, $glossary_id)
  622. {
  623. // Database table definition
  624. $table = Database::get_course_table(TABLE_GLOSSARY);
  625. // sort direction
  626. if ($direction === 'up') {
  627. $sortorder = 'DESC';
  628. } else {
  629. $sortorder = 'ASC';
  630. }
  631. $course_id = api_get_course_int_id();
  632. $sql = "SELECT * FROM $table
  633. WHERE c_id = $course_id
  634. ORDER BY display_order $sortorder";
  635. $res = Database::query($sql);
  636. $found = false;
  637. while ($row = Database::fetch_array($res)) {
  638. if ($found && empty($next_id)) {
  639. $next_id = $row['glossary_id'];
  640. $next_display_order = $row['display_order'];
  641. }
  642. if ($row['glossary_id'] == $glossary_id) {
  643. $current_id = $glossary_id;
  644. $current_display_order = $row['display_order'];
  645. $found = true;
  646. }
  647. }
  648. $sql1 = "UPDATE $table SET display_order = '".Database::escape_string($next_display_order)."'
  649. WHERE c_id = $course_id AND glossary_id = '".Database::escape_string($current_id)."'";
  650. $sql2 = "UPDATE $table SET display_order = '".Database::escape_string($current_display_order)."'
  651. WHERE c_id = $course_id AND glossary_id = '".Database::escape_string($next_id)."'";
  652. Database::query($sql1);
  653. Database::query($sql2);
  654. Display::addFlash(Display::return_message(get_lang('TermMoved')));
  655. }
  656. /**
  657. * Export to pdf.
  658. */
  659. public static function export_to_pdf()
  660. {
  661. $data = self::get_glossary_data(
  662. 0,
  663. self::get_number_glossary_terms(api_get_session_id()),
  664. 0,
  665. 'ASC'
  666. );
  667. $template = new Template('', false, false, false, true, false, false);
  668. $layout = $template->get_template('glossary/export_pdf.tpl');
  669. $template->assign('items', $data);
  670. $html = $template->fetch($layout);
  671. $courseCode = api_get_course_id();
  672. $pdf = new PDF();
  673. $pdf->content_to_pdf($html, '', get_lang('Glossary').'_'.$courseCode, $courseCode);
  674. }
  675. /**
  676. * Generate a PDF with all glossary terms and move file to documents.
  677. *
  678. * @return bool false if there's nothing in the glossary
  679. */
  680. public static function movePdfToDocuments()
  681. {
  682. $sessionId = api_get_session_id();
  683. $courseId = api_get_course_int_id();
  684. $data = self::get_glossary_data(
  685. 0,
  686. self::get_number_glossary_terms($sessionId),
  687. 0,
  688. 'ASC'
  689. );
  690. if (!empty($data)) {
  691. $template = new Template('', false, false, false, true, false, false);
  692. $layout = $template->get_template('glossary/export_pdf.tpl');
  693. $template->assign('items', $data);
  694. $fileName = get_lang('Glossary').'-'.api_get_local_time();
  695. $signatures = ['Drh', 'Teacher', 'Date'];
  696. $pdf = new PDF(
  697. 'A4-P',
  698. 'P',
  699. [
  700. 'filename' => $fileName,
  701. 'pdf_title' => $fileName,
  702. 'add_signatures' => $signatures,
  703. ]
  704. );
  705. $pdf->exportFromHtmlToDocumentsArea(
  706. $template->fetch($layout),
  707. $fileName,
  708. $courseId
  709. );
  710. return true;
  711. } else {
  712. Display::addFlash(Display::return_message(get_lang('NothingToAdd')));
  713. }
  714. return false;
  715. }
  716. /**
  717. * @param string $format
  718. */
  719. public static function exportToFormat($format)
  720. {
  721. if ($format == 'pdf') {
  722. self::export_to_pdf();
  723. return;
  724. }
  725. $data = self::get_glossary_data(
  726. 0,
  727. self::get_number_glossary_terms(api_get_session_id()),
  728. 0,
  729. 'ASC'
  730. );
  731. usort($data, 'sorter');
  732. $list = [];
  733. $list[] = ['term', 'definition'];
  734. $allowStrip = api_get_configuration_value('allow_remove_tags_in_glossary_export');
  735. foreach ($data as $line) {
  736. $definition = $line[1];
  737. if ($allowStrip) {
  738. // htmlspecialchars_decode replace &#39 to '
  739. // strip_tags remove HTML tags
  740. $definition = htmlspecialchars_decode(strip_tags($definition), ENT_QUOTES);
  741. }
  742. $list[] = [$line[0], $definition];
  743. }
  744. $filename = 'glossary_course_'.api_get_course_id();
  745. switch ($format) {
  746. case 'csv':
  747. Export::arrayToCsv($list, $filename);
  748. break;
  749. case 'xls':
  750. Export::arrayToXls($list, $filename);
  751. break;
  752. }
  753. }
  754. }