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