index.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <?php //$id: $
  2. /* For licensing terms, see /dokeos_license.txt */
  3. /**
  4. * @package dokeos.glossary
  5. * @author Christian Fasanando, initial version
  6. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium, refactoring and tighter integration in Dokeos
  7. */
  8. // name of the language file that needs to be included
  9. $language_file = array('glossary');
  10. // including the global dokeos file
  11. require_once('../inc/global.inc.php');
  12. // the section (tabs)
  13. $this_section=SECTION_COURSES;
  14. // notice for unauthorized people.
  15. api_protect_course_script(true);
  16. // including additional libraries
  17. require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');
  18. // additional javascript
  19. $htmlHeadXtra[] = javascript_glossary();
  20. // setting the tool constants
  21. $tool = TOOL_GLOSSARY;
  22. // displaying the header
  23. Display::display_header(get_lang(ucfirst($tool)));
  24. // tool introduction
  25. $fck_attribute['Width'] = '100%';
  26. $fck_attribute['Height'] = '400';
  27. $fck_attribute['ToolbarSet'] = 'Full';
  28. Display::display_introduction_section(TOOL_GLOSSARY,'left');
  29. $fck_attribute = null; // Clearing this global variable immediatelly after it has been used.
  30. // action links
  31. echo '<div class="actions">';
  32. if (api_is_allowed_to_edit())
  33. {
  34. echo '<a href="index.php?'.api_get_cidreq().'&action=addglossary">'.Display::return_icon('filenew.gif',get_lang('TermAddNew')).get_lang('TermAddNew').'</a>';
  35. }
  36. 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>';
  37. 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>';
  38. echo '</div>';
  39. if (api_is_allowed_to_edit())
  40. {
  41. // Adding a glossary
  42. if (isset($_GET['action']) && $_GET['action'] == 'addglossary')
  43. {
  44. // initiate the object
  45. $form = new FormValidator('glossary','post', api_get_self().'?action='.Security::remove_XSS($_GET['action']));
  46. // settting the form elements
  47. $form->addElement('header', '', get_lang('TermAddNew'));
  48. $form->addElement('text', 'glossary_title', get_lang('TermName'));
  49. $form->addElement('html_editor', 'glossary_comment', get_lang('TermDefinition'));
  50. $form->addElement('submit', 'SubmitGlossary', get_lang('Ok'));
  51. // setting the rules
  52. $form->addRule('glossary_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  53. // The validation or display
  54. if ( $form->validate() )
  55. {
  56. $check = Security::check_token('post');
  57. if ($check)
  58. {
  59. $values = $form->exportValues();
  60. save_glossary($values);
  61. }
  62. Security::clear_token();
  63. }
  64. else
  65. {
  66. $token = Security::get_token();
  67. $form->addElement('hidden','sec_token');
  68. $form->setConstants(array('sec_token' => $token));
  69. $form->display();
  70. }
  71. }
  72. // Editing a glossary
  73. if (isset($_GET['action']) && $_GET['action'] == 'edit_glossary' && is_numeric($_GET['glossary_id']))
  74. {
  75. // initiate the object
  76. $form = new FormValidator('glossary','post', api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&glossary_id='.Security::remove_XSS($_GET['glossary_id']));
  77. // settting the form elements
  78. $form->addElement('header', '', get_lang('TermEdit'));
  79. $form->addElement('hidden', 'glossary_id');
  80. $form->addElement('text', 'glossary_title', get_lang('TermName'));
  81. $form->addElement('html_editor', 'glossary_comment', get_lang('TermDefinition'));
  82. $form->addElement('submit', 'SubmitGlossary', get_lang('Ok'));
  83. // setting the defaults
  84. $defaults = get_glossary_information(Security::remove_XSS($_GET['glossary_id']));
  85. $form->setDefaults($defaults);
  86. // setting the rules
  87. $form->addRule('glossary_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  88. // The validation or display
  89. if ( $form->validate() )
  90. {
  91. $check = Security::check_token('post');
  92. if ($check)
  93. {
  94. $values = $form->exportValues();
  95. update_glossary($values);
  96. }
  97. Security::clear_token();
  98. }
  99. else
  100. {
  101. $token = Security::get_token();
  102. $form->addElement('hidden','sec_token');
  103. $form->setConstants(array('sec_token' => $token));
  104. $form->display();
  105. }
  106. }
  107. // deleting a glossary
  108. if (isset($_GET['action']) && $_GET['action'] == 'delete_glossary' && is_numeric($_GET['glossary_id']))
  109. {
  110. delete_glossary(Security::remove_XSS($_GET['glossary_id']));
  111. }
  112. // moving a glossary term up
  113. if (isset($_GET['action']) && $_GET['action'] == 'moveup' && is_numeric($_GET['glossary_id']))
  114. {
  115. move_glossary('up',$_GET['glossary_id']);
  116. }
  117. // moving a glossary term up
  118. if (isset($_GET['action']) && $_GET['action'] == 'movedown' && is_numeric($_GET['glossary_id']))
  119. {
  120. move_glossary('down',$_GET['glossary_id']);
  121. }
  122. }
  123. if ($_GET['action'] == 'changeview' AND in_array($_GET['view'],array('list','table')))
  124. {
  125. $_SESSION['glossary_view'] = $_GET['view'];
  126. }
  127. display_glossary();
  128. // footer
  129. Display::display_footer();
  130. /**
  131. * This functions stores the glossary in the database
  132. *
  133. * @param unknown_type $values
  134. *
  135. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  136. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  137. * @version januari 2009, dokeos 1.8.6
  138. */
  139. function save_glossary($values)
  140. {
  141. // Database table definition
  142. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  143. // get the maximum display order of all the glossary items
  144. $max_glossary_item = get_max_glossary_item();
  145. // check if the glossary term already exists
  146. if (glossary_exists($values['glossary_title']))
  147. {
  148. // display the feedback message
  149. Display::display_error_message('GlossaryAlreadyExistsYouShouldEditIt');
  150. }
  151. else
  152. {
  153. $sql = "INSERT INTO $t_glossary (name, description,display_order)
  154. VALUES(
  155. '".Database::escape_string($values['glossary_title'])."',
  156. '".Database::escape_string($values['glossary_comment'])."',
  157. '".(int)($max_glossary_item + 1)."')";
  158. $result = api_sql_query($sql, __FILE__, __LINE__);
  159. $id = Database::get_last_insert_id();
  160. if ($id>0) {
  161. //insert into item_property
  162. api_item_property_update(api_get_course_info(),TOOL_GLOSSARY,$id,'GlossaryAdded',api_get_user_id());
  163. }
  164. $_SESSION['max_glossary_display'] = get_max_glossary_item();
  165. // display the feedback message
  166. Display::display_confirmation_message(get_lang('TermAdded'));
  167. }
  168. }
  169. /**
  170. * update the information of a glossary term in the database
  171. *
  172. * @param array $values an array containing all the form elements
  173. *
  174. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  175. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  176. * @version januari 2009, dokeos 1.8.6
  177. */
  178. function update_glossary($values)
  179. {
  180. // Database table definition
  181. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  182. // check if the glossary term already exists
  183. if (glossary_exists($values['glossary_title'],$values['glossary_id']))
  184. {
  185. // display the feedback message
  186. Display::display_error_message('GlossaryAlreadyExistsYouShouldEditIt');
  187. }
  188. else
  189. {
  190. $sql = "UPDATE $t_glossary SET
  191. name = '".Database::escape_string($values['glossary_title'])."',
  192. description = '".Database::escape_string($values['glossary_comment'])."'
  193. WHERE glossary_id = ".Database::escape_string($values['glossary_id']);
  194. $result = api_sql_query($sql, __FILE__, __LINE__);
  195. //update glossary into item_property
  196. api_item_property_update(api_get_course_info(),TOOL_GLOSSARY,Database::escape_string($values['glossary_id']),'GlossaryModified',api_get_user_id());
  197. // display the feedback message
  198. Display::display_confirmation_message(get_lang('TermUpdated'));
  199. }
  200. }
  201. /**
  202. * Get the maximum display order of the glossary item
  203. *
  204. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  205. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  206. * @version januari 2009, dokeos 1.8.6
  207. */
  208. function get_max_glossary_item()
  209. {
  210. // Database table definition
  211. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  212. $get_max = "SELECT MAX(display_order) FROM $t_glossary";
  213. $res_max = api_sql_query($get_max, __FILE__, __LINE__);
  214. $dsp=0;
  215. $row = Database::fetch_array($res_max);
  216. return $row[0];
  217. }
  218. /**
  219. * check if the glossary term exists or not
  220. *
  221. * @param unknown_type $term
  222. * @param unknown_type $not_id
  223. * @return unknown
  224. *
  225. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  226. * @version januari 2009, dokeos 1.8.6
  227. */
  228. function glossary_exists($term,$not_id='')
  229. {
  230. // Database table definition
  231. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  232. $sql = "SELECT name FROM $t_glossary WHERE name = '".Database::escape_string($term)."'";
  233. if ($not_id<>'')
  234. {
  235. $sql .= " AND glossary_id <> '".Database::escape_string($not_id)."'";
  236. }
  237. $result = api_sql_query($sql,__FILE__,__LINE__);
  238. $count = Database::num_rows($result);
  239. if ($count > 0)
  240. {
  241. return true;
  242. }
  243. else
  244. {
  245. return false;
  246. }
  247. }
  248. /**
  249. * get all the information about one specific glossary term
  250. *
  251. * @param unknown_type $glossary_id
  252. * @return unknown
  253. *
  254. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  255. * @version januari 2009, dokeos 1.8.6
  256. */
  257. function get_glossary_information($glossary_id)
  258. {
  259. // Database table definition
  260. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  261. $t_item_propery = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  262. $sql = "SELECT g.glossary_id AS glossary_id,
  263. g.name AS glossary_title,
  264. g.description AS glossary_comment,
  265. g.display_order AS glossary_display_order
  266. FROM $t_glossary g, $t_item_propery ip
  267. WHERE g.glossary_id = ip.ref
  268. AND tool = '".TOOL_GLOSSARY."'
  269. AND g.glossary_id = '".Database::escape_string($glossary_id)."' ";
  270. $result = api_sql_query($sql, __FILE__, __LINE__);
  271. return Database::fetch_array($result);
  272. }
  273. /**
  274. * Delete a glossary term (and re-order all the others)
  275. *
  276. * @param integer $glossary_id the id of the glossary
  277. *
  278. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  279. * @version januari 2009, dokeos 1.8.6
  280. */
  281. function delete_glossary($glossary_id)
  282. {
  283. // Database table definition
  284. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  285. $sql = "DELETE FROM $t_glossary WHERE glossary_id='".Database::escape_string($glossary_id)."'";
  286. $result = api_sql_query($sql, __FILE__, __LINE__);
  287. // reorder the remaining terms
  288. reorder_glossary();
  289. $_SESSION['max_glossary_display'] = get_max_glossary_item();
  290. Display::display_confirmation_message(get_lang('TermDeleted'));
  291. }
  292. /**
  293. * This is the main function that display the list or the table with all the glossary terms
  294. *
  295. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  296. * @version januari 2009, dokeos 1.8.6
  297. */
  298. function display_glossary()
  299. {
  300. if (!$_SESSION['glossary_view'] OR $_SESSION['glossary_view'] == 'table')
  301. {
  302. $table = new SortableTable('glossary', 'get_number_glossary_terms', 'get_glossary_data',0);
  303. $table->set_header(0, get_lang('DisplayOrder'), true);
  304. $table->set_header(1, get_lang('TermName'), true);
  305. $table->set_header(2, get_lang('TermDefinition'), true);
  306. $table->set_header(3, get_lang('CreationDate'), true);
  307. $table->set_header(4, get_lang('UpdateDate'), true);
  308. if (api_is_allowed_to_edit()) {
  309. $table->set_header(5, get_lang('Actions'), false);
  310. $table->set_column_filter(5, 'actions_filter');
  311. }
  312. $table->display();
  313. }
  314. if ($_SESSION['glossary_view'] == 'list')
  315. {
  316. display_glossary_list();
  317. }
  318. }
  319. /**
  320. * display the glossary terms in a list
  321. *
  322. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  323. * @version januari 2009, dokeos 1.8.6
  324. */
  325. function display_glossary_list()
  326. {
  327. $glossary_data = get_glossary_data(0,1000,0,ASC);
  328. foreach($glossary_data as $key=>$glossary_item)
  329. {
  330. echo '<div class="sectiontitle">'.$glossary_item[1].'</div>';
  331. echo '<div class="sectioncomment">'.$glossary_item[2].'</div>';
  332. if (api_is_allowed_to_edit()) {
  333. echo '<div>'.actions_filter($glossary_item[5], '',$glossary_item).'</div>';
  334. }
  335. }
  336. }
  337. /**
  338. * Get the number of glossary terms
  339. *
  340. * @return unknown
  341. *
  342. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  343. * @version januari 2009, dokeos 1.8.6
  344. */
  345. function get_number_glossary_terms()
  346. {
  347. // Database table definition
  348. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  349. $sql = "SELECT count(glossary_id) as total FROM $t_glossary";
  350. $res = api_sql_query($sql, __FILE__, __LINE__);
  351. $obj = Database::fetch_object($res);
  352. return $obj->total;
  353. }
  354. /**
  355. * get all the data of the glossary
  356. *
  357. * @param unknown_type $from
  358. * @param unknown_type $number_of_items
  359. * @param unknown_type $column
  360. * @param unknown_type $direction
  361. * @return unknown
  362. *
  363. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  364. * @version januari 2009, dokeos 1.8.6
  365. */
  366. function get_glossary_data($from, $number_of_items, $column, $direction)
  367. {
  368. // Database table definition
  369. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  370. $t_item_propery = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  371. if (api_is_allowed_to_edit()) {
  372. $col5 = ", glossary.glossary_id as col5";
  373. } else {
  374. $col5 = " ";
  375. }
  376. $sql = "SELECT
  377. glossary.display_order as col0,
  378. glossary.name as col1,
  379. glossary.description as col2,
  380. ip.insert_date as col3,
  381. ip.lastedit_date as col4
  382. $col5
  383. FROM $t_glossary glossary, $t_item_propery ip
  384. WHERE glossary.glossary_id = ip.ref
  385. AND tool = '".TOOL_GLOSSARY."' ";
  386. $sql .= " ORDER BY col$column $direction ";
  387. $sql .= " LIMIT $from,$number_of_items";
  388. $res = api_sql_query($sql, __FILE__, __LINE__);
  389. $return = array ();
  390. while ($data = Database::fetch_row($res))
  391. {
  392. $return[] = $data;
  393. }
  394. return $return;
  395. }
  396. /**
  397. * Enter description here...
  398. *
  399. * @param unknown_type $glossary_id
  400. * @param unknown_type $url_params
  401. * @param unknown_type $row
  402. * @return unknown
  403. *
  404. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  405. * @version januari 2009, dokeos 1.8.6
  406. */
  407. function actions_filter($glossary_id,$url_params,$row)
  408. {
  409. if (!$_SESSION['max_glossary_display'] OR $_SESSION['max_glossary_display'] == '')
  410. {
  411. $_SESSION['max_glossary_display'] = get_max_glossary_item();
  412. }
  413. $return = '';
  414. if (empty($_GET['glossary_column'])) {
  415. if ($row[0] > 1)
  416. {
  417. $return .= '<a href="'.api_get_self().'?action=moveup&amp;glossary_id='.$row[5].'">'.Display::return_icon('up.gif', get_lang('Up')).'</a>';
  418. }
  419. else
  420. {
  421. $return .= Display::return_icon('blanco.png');
  422. }
  423. if ($row[0] < $_SESSION['max_glossary_display'])
  424. {
  425. $return .= '<a href="'.api_get_self().'?action=movedown&amp;glossary_id='.$row[5].'">'.Display::return_icon('down.gif',get_lang('Down')).'</a>';
  426. }
  427. else
  428. {
  429. $return .= Display::return_icon('blanco.png');
  430. }
  431. }
  432. $return .= '<a href="'.api_get_self().'?action=edit_glossary&amp;glossary_id='.$row[5].'">'.Display::return_icon('edit.gif',get_lang('Edit')).'</a>';
  433. $return .= '<a href="'.api_get_self().'?action=delete_glossary&amp;glossary_id='.$row[5].'" onclick="return confirmation(\''.$row[1].'\');">'.Display::return_icon('delete.gif', get_lang('Delete')).'</a>';
  434. return $return;
  435. }
  436. /**
  437. * a little bit of javascript to display a prettier warning when deleting a term
  438. *
  439. * @return unknown
  440. *
  441. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  442. * @version januari 2009, dokeos 1.8.6
  443. */
  444. function javascript_glossary()
  445. {
  446. return "<script type=\"text/javascript\">
  447. function confirmation (name)
  448. {
  449. if (confirm(\" ". get_lang("TermConfirmDelete") ." \"+ name + \" ?\"))
  450. {return true;}
  451. else
  452. {return false;}
  453. }
  454. </script>";
  455. }
  456. /**
  457. * Enter description here...
  458. *
  459. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  460. * @version januari 2009, dokeos 1.8.6
  461. */
  462. function reorder_glossary()
  463. {
  464. // Database table definition
  465. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  466. $sql = "SELECT * FROM $t_glossary ORDER by display_order ASC";
  467. $res = api_sql_query($sql, __FILE__, __LINE__);
  468. $i = 1;
  469. while ($data = Database::fetch_array($res))
  470. {
  471. $sql_reorder = "UPDATE $t_glossary SET display_order = $i WHERE glossary_id = '".Database::escape_string($data['glossary_id'])."'";
  472. api_sql_query($sql_reorder, __FILE__, __LINE__);
  473. $i++;
  474. }
  475. }
  476. /**
  477. * Enter description here...
  478. *
  479. * @param unknown_type $direction
  480. * @param unknown_type $glossary_id
  481. *
  482. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  483. * @version januari 2009, dokeos 1.8.6
  484. */
  485. function move_glossary($direction, $glossary_id)
  486. {
  487. // Database table definition
  488. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  489. // sort direction
  490. if ($direction == 'up')
  491. {
  492. $sortorder = 'DESC';
  493. }
  494. else
  495. {
  496. $sortorder = 'ASC';
  497. }
  498. $sql = "SELECT * FROM $t_glossary ORDER BY display_order $sortorder";
  499. $res = api_sql_query($sql, __FILE__, __LINE__);
  500. while ($row = Database::fetch_array($res))
  501. {
  502. if ($found == true and empty($next_id))
  503. {
  504. $next_id = $row['glossary_id'];
  505. $next_display_order = $row['display_order'];
  506. }
  507. if ($row['glossary_id'] == $glossary_id)
  508. {
  509. $current_id = $glossary_id;
  510. $current_display_order = $row['display_order'];
  511. $found = true;
  512. }
  513. }
  514. $sql1 = "UPDATE $t_glossary SET display_order = '".Database::escape_string($next_display_order)."' WHERE glossary_id = '".Database::escape_string($current_id)."'";
  515. $sql2 = "UPDATE $t_glossary SET display_order = '".Database::escape_string($current_display_order)."' WHERE glossary_id = '".Database::escape_string($next_id)."'";
  516. $res = api_sql_query($sql1, __FILE__, __LINE__);
  517. $res = api_sql_query($sql2, __FILE__, __LINE__);
  518. }