glossary.lib.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This library provides functions for the glossary tool.
  5. * Include/require it in your code to use its functionality.
  6. * @package chamilo.library
  7. */
  8. class GlossaryManager {
  9. function __construct() {
  10. }
  11. /**
  12. * Get all glossary terms
  13. * @author Isaac Flores <isaac.flores@dokeos.com>
  14. * @return Array Contain glossary terms
  15. */
  16. public static function get_glossary_terms () {
  17. global $course;
  18. $glossary_id=array();
  19. $glossary_name=array();
  20. $glossary_desc=array();
  21. $glossary_table = Database::get_course_table(TABLE_GLOSSARY);
  22. $sql='SELECT glossary_id as id,name,description FROM '.$glossary_table;
  23. $rs=Database::query($sql);
  24. while ($row=Database::fetch_array($rs)) {
  25. $glossary_data[]=$row;
  26. }
  27. return $glossary_data;
  28. }
  29. /**
  30. * Get glossary term by glossary id
  31. * @author Isaac Flores <florespaz@bidsoftperu.com>
  32. * @param Integer The glossary id
  33. * @return String The glossary description
  34. */
  35. public static function get_glossary_term_by_glossary_id ($glossary_id) {
  36. global $course;
  37. $glossary_table = Database::get_course_table(TABLE_GLOSSARY);
  38. $sql='SELECT description FROM '.$glossary_table.' WHERE glossary_id="'.Database::escape_string($glossary_id).'"';
  39. $rs=Database::query($sql);
  40. $row=Database::fetch_array($rs);
  41. return $row['description'];
  42. }
  43. /**
  44. * Get glossary term by glossary id
  45. * @author Isaac Flores <florespaz_isaac@hotmail.com>
  46. * @param String The glossary term name
  47. * @return String The glossary description
  48. */
  49. public static function get_glossary_term_by_glossary_name ($glossary_name) {
  50. global $course;
  51. $glossary_table = Database::get_course_table(TABLE_GLOSSARY);
  52. $sql='SELECT description FROM '.$glossary_table.' WHERE name like trim("'.Database::escape_string($glossary_name).'") ';
  53. $rs=Database::query($sql);
  54. $row=Database::fetch_array($rs);
  55. return $row['description'];
  56. }
  57. /**
  58. * This functions stores the glossary in the database
  59. *
  60. * @param unknown_type $values
  61. *
  62. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  63. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  64. * @version januari 2009, dokeos 1.8.6
  65. */
  66. function save_glossary($values)
  67. {
  68. // Database table definition
  69. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  70. // get the maximum display order of all the glossary items
  71. $max_glossary_item = GlossaryManager::get_max_glossary_item();
  72. // session_id
  73. $session_id = api_get_session_id();
  74. // check if the glossary term already exists
  75. if (GlossaryManager::glossary_exists($values['glossary_title']))
  76. {
  77. // display the feedback message
  78. Display::display_error_message(get_lang('GlossaryTermAlreadyExistsYouShouldEditIt'));
  79. } else {
  80. $sql = "INSERT INTO $t_glossary (name, description, display_order, session_id)
  81. VALUES(
  82. '".Database::escape_string(Security::remove_XSS($values['glossary_title']))."',
  83. '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['glossary_comment'])),COURSEMANAGERLOWSECURITY))."',
  84. '".(int)($max_glossary_item + 1)."',
  85. '".Database::escape_string($session_id)."'
  86. )";
  87. $result = Database::query($sql);
  88. $id = Database::insert_id();
  89. if ($id>0) {
  90. //insert into item_property
  91. api_item_property_update(api_get_course_info(), TOOL_GLOSSARY, $id, 'GlossaryAdded', api_get_user_id());
  92. }
  93. $_SESSION['max_glossary_display'] = GlossaryManager::get_max_glossary_item();
  94. // display the feedback message
  95. Display::display_confirmation_message(get_lang('TermAdded'));
  96. }
  97. }
  98. /**
  99. * update the information of a glossary term in the database
  100. *
  101. * @param array $values an array containing all the form elements
  102. *
  103. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  104. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  105. * @version januari 2009, dokeos 1.8.6
  106. */
  107. function update_glossary($values)
  108. {
  109. // Database table definition
  110. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  111. // check if the glossary term already exists
  112. if (GlossaryManager::glossary_exists($values['glossary_title'],$values['glossary_id']))
  113. {
  114. // display the feedback message
  115. Display::display_error_message(get_lang('GlossaryTermAlreadyExistsYouShouldEditIt'));
  116. } else {
  117. $sql = "UPDATE $t_glossary SET
  118. name = '".Database::escape_string(Security::remove_XSS($values['glossary_title']))."',
  119. description = '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['glossary_comment'])),COURSEMANAGERLOWSECURITY))."'
  120. WHERE glossary_id = ".Database::escape_string($values['glossary_id']);
  121. $result = Database::query($sql);
  122. //update glossary into item_property
  123. api_item_property_update(api_get_course_info(), TOOL_GLOSSARY, Database::escape_string($values['glossary_id']), 'GlossaryUpdated', api_get_user_id());
  124. // display the feedback message
  125. Display::display_confirmation_message(get_lang('TermUpdated'));
  126. }
  127. }
  128. /**
  129. * Get the maximum display order of the glossary item
  130. *
  131. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  132. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  133. * @version januari 2009, dokeos 1.8.6
  134. */
  135. function get_max_glossary_item()
  136. {
  137. // Database table definition
  138. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  139. $get_max = "SELECT MAX(display_order) FROM $t_glossary";
  140. $res_max = Database::query($get_max);
  141. $dsp=0;
  142. $row = Database::fetch_array($res_max);
  143. return $row[0];
  144. }
  145. /**
  146. * check if the glossary term exists or not
  147. *
  148. * @param unknown_type $term
  149. * @param unknown_type $not_id
  150. * @return unknown
  151. *
  152. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  153. * @version januari 2009, dokeos 1.8.6
  154. */
  155. function glossary_exists($term,$not_id='')
  156. {
  157. // Database table definition
  158. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  159. $sql = "SELECT name FROM $t_glossary WHERE name = '".Database::escape_string($term)."'";
  160. if ($not_id<>'')
  161. {
  162. $sql .= " AND glossary_id <> '".Database::escape_string($not_id)."'";
  163. }
  164. $result = Database::query($sql);
  165. $count = Database::num_rows($result);
  166. if ($count > 0) {
  167. return true;
  168. } else {
  169. return false;
  170. }
  171. }
  172. /**
  173. * get all the information about one specific glossary term
  174. *
  175. * @param unknown_type $glossary_id
  176. * @return unknown
  177. *
  178. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  179. * @version januari 2009, dokeos 1.8.6
  180. */
  181. function get_glossary_information($glossary_id)
  182. {
  183. // Database table definition
  184. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  185. $t_item_propery = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  186. $sql = "SELECT g.glossary_id AS glossary_id,
  187. g.name AS glossary_title,
  188. g.description AS glossary_comment,
  189. g.display_order AS glossary_display_order
  190. FROM $t_glossary g, $t_item_propery ip
  191. WHERE g.glossary_id = ip.ref
  192. AND tool = '".TOOL_GLOSSARY."'
  193. AND g.glossary_id = '".Database::escape_string($glossary_id)."' ";
  194. $result = Database::query($sql);
  195. return Database::fetch_array($result);
  196. }
  197. /**
  198. * Delete a glossary term (and re-order all the others)
  199. *
  200. * @param integer $glossary_id the id of the glossary
  201. *
  202. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  203. * @version januari 2009, dokeos 1.8.6
  204. */
  205. function delete_glossary($glossary_id)
  206. {
  207. // Database table definition
  208. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  209. $sql = "DELETE FROM $t_glossary WHERE glossary_id='".Database::escape_string($glossary_id)."'";
  210. $result = Database::query($sql);
  211. //update item_property (delete)
  212. api_item_property_update(api_get_course_info(), TOOL_GLOSSARY, Database::escape_string($glossary_id), 'delete', api_get_user_id());
  213. // reorder the remaining terms
  214. GlossaryManager::reorder_glossary();
  215. $_SESSION['max_glossary_display'] = GlossaryManager::get_max_glossary_item();
  216. Display::display_confirmation_message(get_lang('TermDeleted'));
  217. }
  218. /**
  219. * This is the main function that display the list or the table with all the glossary terms
  220. *
  221. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  222. * @version januari 2009, dokeos 1.8.6
  223. */
  224. function display_glossary()
  225. {
  226. // action links
  227. echo '<div class="actions" style="margin-bottom:10px">';
  228. if (api_is_allowed_to_edit(null,true))
  229. {
  230. echo '<a href="index.php?'.api_get_cidreq().'&action=addglossary&msg=add">'.Display::return_icon('filenew.gif',get_lang('TermAddNew')).get_lang('TermAddNew').'</a>';
  231. }
  232. if ((isset($_SESSION['glossary_view']) && $_SESSION['glossary_view'] == 'table') or (!isset($_SESSION['glossary_view']))){
  233. echo '<a href="index.php?'.api_get_cidreq().'&action=changeview&view=list">'.Display::return_icon('view_list.gif',get_lang('ListView')).get_lang('ListView').'</a>';
  234. } else {
  235. echo '<a href="index.php?'.api_get_cidreq().'&action=changeview&view=table">'.Display::return_icon('view_table.gif',get_lang('TableView')).get_lang('TableView').'</a>';
  236. }
  237. echo '</div>';
  238. if (!$_SESSION['glossary_view'] OR $_SESSION['glossary_view'] == 'table')
  239. {
  240. $table = new SortableTable('glossary', array('GlossaryManager','get_number_glossary_terms'), array('GlossaryManager','get_glossary_data'),0);
  241. $table->set_header(0, get_lang('DisplayOrder'), true);
  242. $table->set_header(1, get_lang('TermName'), true);
  243. $table->set_header(2, get_lang('TermDefinition'), true);
  244. $table->set_header(3, get_lang('CreationDate'), false);
  245. $table->set_header(4, get_lang('UpdateDate'), false);
  246. if (api_is_allowed_to_edit(null,true)) {
  247. $table->set_header(5, get_lang('Actions'), false);
  248. $table->set_column_filter(5, array('GlossaryManager','actions_filter'));
  249. }
  250. $table->display();
  251. }
  252. if ($_SESSION['glossary_view'] == 'list')
  253. {
  254. GlossaryManager::display_glossary_list();
  255. }
  256. }
  257. /**
  258. * display the glossary terms in a list
  259. *
  260. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  261. * @version januari 2009, dokeos 1.8.6
  262. */
  263. function display_glossary_list()
  264. {
  265. $glossary_data = GlossaryManager::get_glossary_data(0,1000,0,ASC);
  266. foreach($glossary_data as $key=>$glossary_item)
  267. {
  268. echo '<div class="sectiontitle">'.$glossary_item[1].'</div>';
  269. echo '<div class="sectioncomment">'.$glossary_item[2].'</div>';
  270. if (api_is_allowed_to_edit(null,true)) {
  271. echo '<div>'.GlossaryManager::actions_filter($glossary_item[5], '',$glossary_item).'</div>';
  272. }
  273. }
  274. }
  275. /**
  276. * Get the number of glossary terms
  277. *
  278. * @return unknown
  279. *
  280. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  281. * @version januari 2009, dokeos 1.8.6
  282. */
  283. function get_number_glossary_terms()
  284. {
  285. // Database table definition
  286. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  287. $sql = "SELECT count(glossary_id) as total FROM $t_glossary";
  288. $res = Database::query($sql);
  289. $obj = Database::fetch_object($res);
  290. return $obj->total;
  291. }
  292. /**
  293. * Get all the data of a glossary
  294. *
  295. * @param unknown_type $from
  296. * @param unknown_type $number_of_items
  297. * @param unknown_type $column
  298. * @param unknown_type $direction
  299. * @return unknown
  300. *
  301. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  302. * @version januari 2009, dokeos 1.8.6
  303. */
  304. function get_glossary_data($from, $number_of_items, $column, $direction)
  305. {
  306. global $_user;
  307. // Database table definition
  308. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  309. $t_item_propery = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  310. if (api_is_allowed_to_edit(null,true)) {
  311. $col5 = ", glossary.glossary_id as col5";
  312. } else {
  313. $col5 = " ";
  314. }
  315. //condition for the session
  316. $session_id = api_get_session_id();
  317. $condition_session = api_get_session_condition($session_id);
  318. $sql = "SELECT
  319. glossary.display_order as col0,
  320. glossary.name as col1,
  321. glossary.description as col2,
  322. ip.insert_date as col3,
  323. ip.lastedit_date as col4
  324. $col5,
  325. glossary.session_id as session_id
  326. FROM $t_glossary glossary, $t_item_propery ip
  327. WHERE glossary.glossary_id = ip.ref
  328. AND tool = '".TOOL_GLOSSARY."' $condition_session";
  329. $sql .= " ORDER BY col$column $direction ";
  330. $sql .= " LIMIT $from,$number_of_items";
  331. $res = Database::query($sql);
  332. $return = array();
  333. $array = array();
  334. while ($data = Database::fetch_array($res)) {
  335. $array[0] = $data[0];
  336. //validacion when belongs to a session
  337. $session_img = api_get_session_image($data['session_id'], $_user['status']);
  338. $array[1] = $data[1] . $session_img;
  339. if (!$_SESSION['glossary_view'] || $_SESSION['glossary_view'] == 'table') {
  340. $array[2] = str_replace(array('<p>','</p>'),array('','<br />'),$data[2]);
  341. } else {
  342. $array[2] = $data[2];
  343. }
  344. $array[3] = $data[3];
  345. $array[4] = $data[4];
  346. if (api_is_allowed_to_edit(null,true)) {
  347. $array[5] = $data[5];
  348. }
  349. // Date treatment for timezones
  350. $array[3] = api_get_local_time($array[3], null, date_default_timezone_get());
  351. $array[4] = api_get_local_time($array[4], null, date_default_timezone_get());
  352. $return[] = $array;
  353. }
  354. return $return;
  355. }
  356. /**
  357. * Enter description here...
  358. *
  359. * @param unknown_type $glossary_id
  360. * @param unknown_type $url_params
  361. * @param unknown_type $row
  362. * @return unknown
  363. *
  364. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  365. * @version januari 2009, dokeos 1.8.6
  366. */
  367. function actions_filter($glossary_id,$url_params,$row)
  368. {
  369. if (!$_SESSION['max_glossary_display'] OR $_SESSION['max_glossary_display'] == '') {
  370. $_SESSION['max_glossary_display'] = GlossaryManager::get_max_glossary_item();
  371. }
  372. if (empty($_GET['glossary_column'])) {
  373. if ($row[0] > 1) {
  374. $return .= '<a href="'.api_get_self().'?action=moveup&amp;glossary_id='.$row[5].'&'.api_get_cidreq().'">'.Display::return_icon('up.gif', get_lang('Up')).'</a>';
  375. }
  376. else
  377. {
  378. $return .= Display::return_icon('up_na.gif','&nbsp;');
  379. }
  380. if ($row[0] < $_SESSION['max_glossary_display'])
  381. {
  382. $return .= '<a href="'.api_get_self().'?action=movedown&amp;glossary_id='.$row[5].'&'.api_get_cidreq().'">'.Display::return_icon('down.gif',get_lang('Down')).'</a>';
  383. }
  384. else
  385. {
  386. $return .= Display::return_icon('down_na.gif','&nbsp;');
  387. }
  388. }
  389. $return .= '<a href="'.api_get_self().'?action=edit_glossary&amp;glossary_id='.$row[5].'&'.api_get_cidreq().'&msg=edit">'.Display::return_icon('edit.gif',get_lang('Edit')).'</a>';
  390. $glossary_data = GlossaryManager::get_glossary_information($row[5]);
  391. $glossary_term = $glossary_data['glossary_title'];
  392. $return .= '<a href="'.api_get_self().'?action=delete_glossary&amp;glossary_id='.$row[5].'&'.api_get_cidreq().'" onclick="return confirmation(\''.$glossary_term.'\');">'.Display::return_icon('delete.gif', get_lang('Delete')).'</a>';
  393. return $return;
  394. }
  395. /**
  396. * a little bit of javascript to display a prettier warning when deleting a term
  397. *
  398. * @return unknown
  399. *
  400. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  401. * @version januari 2009, dokeos 1.8.6
  402. */
  403. function javascript_glossary()
  404. {
  405. return "<script type=\"text/javascript\">
  406. function confirmation (name)
  407. {
  408. if (confirm(\" ". get_lang("TermConfirmDelete") ." \"+ name + \" ?\"))
  409. {return true;}
  410. else
  411. {return false;}
  412. }
  413. </script>";
  414. }
  415. /**
  416. * Re-order glossary
  417. *
  418. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  419. * @version januari 2009, dokeos 1.8.6
  420. */
  421. function reorder_glossary()
  422. {
  423. // Database table definition
  424. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  425. $sql = "SELECT * FROM $t_glossary ORDER by display_order ASC";
  426. $res = Database::query($sql);
  427. $i = 1;
  428. while ($data = Database::fetch_array($res))
  429. {
  430. $sql_reorder = "UPDATE $t_glossary SET display_order = $i WHERE glossary_id = '".Database::escape_string($data['glossary_id'])."'";
  431. Database::query($sql_reorder);
  432. $i++;
  433. }
  434. }
  435. /**
  436. * Move a glossary
  437. *
  438. * @param unknown_type $direction
  439. * @param unknown_type $glossary_id
  440. *
  441. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  442. * @version januari 2009, dokeos 1.8.6
  443. */
  444. function move_glossary($direction, $glossary_id)
  445. {
  446. // Database table definition
  447. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  448. // sort direction
  449. if ($direction == 'up')
  450. {
  451. $sortorder = 'DESC';
  452. }
  453. else
  454. {
  455. $sortorder = 'ASC';
  456. }
  457. $sql = "SELECT * FROM $t_glossary ORDER BY display_order $sortorder";
  458. $res = Database::query($sql);
  459. $found = false;
  460. while ($row = Database::fetch_array($res))
  461. {
  462. if ($found == true and empty($next_id))
  463. {
  464. $next_id = $row['glossary_id'];
  465. $next_display_order = $row['display_order'];
  466. }
  467. if ($row['glossary_id'] == $glossary_id)
  468. {
  469. $current_id = $glossary_id;
  470. $current_display_order = $row['display_order'];
  471. $found = true;
  472. }
  473. }
  474. $sql1 = "UPDATE $t_glossary SET display_order = '".Database::escape_string($next_display_order)."' WHERE glossary_id = '".Database::escape_string($current_id)."'";
  475. $sql2 = "UPDATE $t_glossary SET display_order = '".Database::escape_string($current_display_order)."' WHERE glossary_id = '".Database::escape_string($next_id)."'";
  476. $res = Database::query($sql1);
  477. $res = Database::query($sql2);
  478. Display::display_confirmation_message(get_lang('TermMoved'));
  479. }
  480. }
  481. ?>