glossary.lib.php 20 KB

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