glossary.lib.php 29 KB

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