glossary.lib.php 26 KB

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