index.php 19 KB

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