glossary.lib.php 20 KB

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