glossary.lib.php 26 KB

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