glossary.lib.php 26 KB

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