glossary.lib.php 20 KB

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