index.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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. // display the feedback message
  165. Display::display_confirmation_message(get_lang('TermAdded'));
  166. }
  167. }
  168. /**
  169. * update the information of a glossary term in the database
  170. *
  171. * @param array $values an array containing all the form elements
  172. *
  173. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  174. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  175. * @version januari 2009, dokeos 1.8.6
  176. */
  177. function update_glossary($values)
  178. {
  179. // Database table definition
  180. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  181. // check if the glossary term already exists
  182. if (glossary_exists($values['glossary_title'],$values['glossary_id']))
  183. {
  184. // display the feedback message
  185. Display::display_error_message('GlossaryAlreadyExistsYouShouldEditIt');
  186. }
  187. else
  188. {
  189. $sql = "UPDATE $t_glossary SET
  190. name = '".Database::escape_string($values['glossary_title'])."',
  191. description = '".Database::escape_string($values['glossary_comment'])."'
  192. WHERE glossary_id = ".Database::escape_string($values['glossary_id']);
  193. $result = api_sql_query($sql, __FILE__, __LINE__);
  194. //update glossary into item_property
  195. api_item_property_update(api_get_course_info(),TOOL_GLOSSARY,Database::escape_string($values['glossary_id']),'GlossaryModified',api_get_user_id());
  196. // display the feedback message
  197. Display::display_confirmation_message(get_lang('TermUpdated'));
  198. }
  199. }
  200. /**
  201. * Get the maximum display order of the glossary item
  202. *
  203. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  204. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  205. * @version januari 2009, dokeos 1.8.6
  206. */
  207. function get_max_glossary_item()
  208. {
  209. // Database table definition
  210. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  211. $get_max = "SELECT MAX(display_order) FROM $t_glossary";
  212. $res_max = api_sql_query($get_max, __FILE__, __LINE__);
  213. $dsp=0;
  214. $row = Database::fetch_array($res_max);
  215. return $row[0];
  216. }
  217. /**
  218. * check if the glossary term exists or not
  219. *
  220. * @param unknown_type $term
  221. * @param unknown_type $not_id
  222. * @return unknown
  223. *
  224. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  225. * @version januari 2009, dokeos 1.8.6
  226. */
  227. function glossary_exists($term,$not_id='')
  228. {
  229. // Database table definition
  230. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  231. $sql = "SELECT name FROM $t_glossary WHERE name = '".Database::escape_string($term)."'";
  232. if ($not_id<>'')
  233. {
  234. $sql .= " AND glossary_id <> '".Database::escape_string($not_id)."'";
  235. }
  236. $result = api_sql_query($sql,__FILE__,__LINE__);
  237. $count = Database::num_rows($result);
  238. if ($count > 0)
  239. {
  240. return true;
  241. }
  242. else
  243. {
  244. return false;
  245. }
  246. }
  247. /**
  248. * get all the information about one specific glossary term
  249. *
  250. * @param unknown_type $glossary_id
  251. * @return unknown
  252. *
  253. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  254. * @version januari 2009, dokeos 1.8.6
  255. */
  256. function get_glossary_information($glossary_id)
  257. {
  258. // Database table definition
  259. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  260. $t_item_propery = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  261. $sql = "SELECT g.glossary_id AS glossary_id,
  262. g.name AS glossary_title,
  263. g.description AS glossary_comment,
  264. g.display_order AS glossary_display_order
  265. FROM $t_glossary g, $t_item_propery ip
  266. WHERE g.glossary_id = ip.ref
  267. AND tool = '".TOOL_GLOSSARY."'
  268. AND g.glossary_id = '".Database::escape_string($glossary_id)."' ";
  269. $result = api_sql_query($sql, __FILE__, __LINE__);
  270. return Database::fetch_array($result);
  271. }
  272. /**
  273. * Delete a glossary term (and re-order all the others)
  274. *
  275. * @param integer $glossary_id the id of the glossary
  276. *
  277. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  278. * @version januari 2009, dokeos 1.8.6
  279. */
  280. function delete_glossary($glossary_id)
  281. {
  282. // Database table definition
  283. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  284. $sql = "DELETE FROM $t_glossary WHERE glossary_id='".Database::escape_string($glossary_id)."'";
  285. $result = api_sql_query($sql, __FILE__, __LINE__);
  286. // reorder the remaining terms
  287. reorder_glossary();
  288. Display::display_confirmation_message(get_lang('TermDeleted'));
  289. }
  290. /**
  291. * This is the main function that display the list or the table with all the glossary terms
  292. *
  293. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  294. * @version januari 2009, dokeos 1.8.6
  295. */
  296. function display_glossary()
  297. {
  298. if (!$_SESSION['glossary_view'] OR $_SESSION['glossary_view'] == 'table')
  299. {
  300. $table = new SortableTable('glossary', 'get_number_glossary_terms', 'get_glossary_data',0);
  301. $table->set_header(0, get_lang('DisplayOrder'), true);
  302. $table->set_header(1, get_lang('TermName'), true);
  303. $table->set_header(2, get_lang('TermDefinition'), true);
  304. $table->set_header(3, get_lang('CreationDate'), true);
  305. $table->set_header(4, get_lang('UpdateDate'), true);
  306. if (api_is_allowed_to_edit()) {
  307. $table->set_header(5, get_lang('Actions'), false);
  308. $table->set_column_filter(5, 'actions_filter');
  309. }
  310. $table->display();
  311. }
  312. if ($_SESSION['glossary_view'] == 'list')
  313. {
  314. display_glossary_list();
  315. }
  316. }
  317. /**
  318. * display the glossary terms in a list
  319. *
  320. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  321. * @version januari 2009, dokeos 1.8.6
  322. */
  323. function display_glossary_list()
  324. {
  325. $glossary_data = get_glossary_data(0,1000,0,ASC);
  326. foreach($glossary_data as $key=>$glossary_item)
  327. {
  328. echo '<div class="sectiontitle">'.$glossary_item[1].'</div>';
  329. echo '<div class="sectioncomment">'.$glossary_item[2].'</div>';
  330. if (api_is_allowed_to_edit()) {
  331. echo '<div>'.actions_filter($glossary_item[5], '',$glossary_item).'</div>';
  332. }
  333. }
  334. }
  335. /**
  336. * Get the number of glossary terms
  337. *
  338. * @return unknown
  339. *
  340. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  341. * @version januari 2009, dokeos 1.8.6
  342. */
  343. function get_number_glossary_terms()
  344. {
  345. // Database table definition
  346. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  347. $sql = "SELECT count(glossary_id) as total FROM $t_glossary";
  348. $res = api_sql_query($sql, __FILE__, __LINE__);
  349. $obj = Database::fetch_object($res);
  350. return $obj->total;
  351. }
  352. /**
  353. * get all the data of the glossary
  354. *
  355. * @param unknown_type $from
  356. * @param unknown_type $number_of_items
  357. * @param unknown_type $column
  358. * @param unknown_type $direction
  359. * @return unknown
  360. *
  361. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  362. * @version januari 2009, dokeos 1.8.6
  363. */
  364. function get_glossary_data($from, $number_of_items, $column, $direction)
  365. {
  366. // Database table definition
  367. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  368. $t_item_propery = Database :: get_course_table(TABLE_ITEM_PROPERTY);
  369. if (api_is_allowed_to_edit()) {
  370. $col5 = ", glossary.glossary_id as col5";
  371. } else {
  372. $col5 = " ";
  373. }
  374. $sql = "SELECT
  375. glossary.display_order as col0,
  376. glossary.name as col1,
  377. glossary.description as col2,
  378. ip.insert_date as col3,
  379. ip.lastedit_date as col4
  380. $col5
  381. FROM $t_glossary glossary, $t_item_propery ip
  382. WHERE glossary.glossary_id = ip.ref
  383. AND tool = '".TOOL_GLOSSARY."' ";
  384. $sql .= " ORDER BY col$column $direction ";
  385. $sql .= " LIMIT $from,$number_of_items";
  386. $res = api_sql_query($sql, __FILE__, __LINE__);
  387. $return = array ();
  388. while ($data = Database::fetch_row($res))
  389. {
  390. $return[] = $data;
  391. }
  392. return $return;
  393. }
  394. /**
  395. * Enter description here...
  396. *
  397. * @param unknown_type $glossary_id
  398. * @param unknown_type $url_params
  399. * @param unknown_type $row
  400. * @return unknown
  401. *
  402. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  403. * @version januari 2009, dokeos 1.8.6
  404. */
  405. function actions_filter($glossary_id,$url_params,$row)
  406. {
  407. if (!$_SESSION['max_glossary_display'] OR $_SESSION['max_glossary_display'] == '')
  408. {
  409. $_SESSION['max_glossary_display'] = get_max_glossary_item();
  410. }
  411. $return = '';
  412. if ($row[0] > 1)
  413. {
  414. $return .= '<a href="'.api_get_self().'?action=moveup&amp;glossary_id='.$row[5].'">'.Display::return_icon('up.gif').'</a>';
  415. }
  416. else
  417. {
  418. $return .= Display::return_icon('blanco.png');
  419. }
  420. if ($row[0] < $_SESSION['max_glossary_display'])
  421. {
  422. $return .= '<a href="'.api_get_self().'?action=movedown&amp;glossary_id='.$row[5].'">'.Display::return_icon('down.gif').'</a>';
  423. }
  424. else
  425. {
  426. $return .= Display::return_icon('blanco.png');
  427. }
  428. $return .= '<a href="'.api_get_self().'?action=edit_glossary&amp;glossary_id='.$row[5].'">'.Display::return_icon('edit.gif').'</a>';
  429. $return .= '<a href="'.api_get_self().'?action=delete_glossary&amp;glossary_id='.$row[5].'" onclick="return confirmation(\''.$row[1].'\');">'.Display::return_icon('delete.gif').'</a>';
  430. return $return;
  431. }
  432. /**
  433. * a little bit of javascript to display a prettier warning when deleting a term
  434. *
  435. * @return unknown
  436. *
  437. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  438. * @version januari 2009, dokeos 1.8.6
  439. */
  440. function javascript_glossary()
  441. {
  442. return "<script type=\"text/javascript\">
  443. function confirmation (name)
  444. {
  445. if (confirm(\" ". get_lang("TermConfirmDelete") ." \"+ name + \" ?\"))
  446. {return true;}
  447. else
  448. {return false;}
  449. }
  450. </script>";
  451. }
  452. /**
  453. * Enter description here...
  454. *
  455. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  456. * @version januari 2009, dokeos 1.8.6
  457. */
  458. function reorder_glossary()
  459. {
  460. // Database table definition
  461. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  462. $sql = "SELECT * FROM $t_glossary ORDER by display_order ASC";
  463. $res = api_sql_query($sql, __FILE__, __LINE__);
  464. $i = 1;
  465. while ($data = Database::fetch_array($res))
  466. {
  467. $sql_reorder = "UPDATE $t_glossary SET display_order = $i WHERE glossary_id = '".Database::escape_string($data['glossary_id'])."'";
  468. api_sql_query($sql_reorder, __FILE__, __LINE__);
  469. $i++;
  470. }
  471. }
  472. /**
  473. * Enter description here...
  474. *
  475. * @param unknown_type $direction
  476. * @param unknown_type $glossary_id
  477. *
  478. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  479. * @version januari 2009, dokeos 1.8.6
  480. */
  481. function move_glossary($direction, $glossary_id)
  482. {
  483. // Database table definition
  484. $t_glossary = Database :: get_course_table(TABLE_GLOSSARY);
  485. // sort direction
  486. if ($direction == 'up')
  487. {
  488. $sortorder = 'DESC';
  489. }
  490. else
  491. {
  492. $sortorder = 'ASC';
  493. }
  494. $sql = "SELECT * FROM $t_glossary ORDER BY display_order $sortorder";
  495. $res = api_sql_query($sql, __FILE__, __LINE__);
  496. while ($row = Database::fetch_array($res))
  497. {
  498. if ($found == true and empty($next_id))
  499. {
  500. $next_id = $row['glossary_id'];
  501. $next_display_order = $row['display_order'];
  502. }
  503. if ($row['glossary_id'] == $glossary_id)
  504. {
  505. $current_id = $glossary_id;
  506. $current_display_order = $row['display_order'];
  507. $found = true;
  508. }
  509. }
  510. $sql1 = "UPDATE $t_glossary SET display_order = '".Database::escape_string($next_display_order)."' WHERE glossary_id = '".Database::escape_string($current_id)."'";
  511. $sql2 = "UPDATE $t_glossary SET display_order = '".Database::escape_string($current_display_order)."' WHERE glossary_id = '".Database::escape_string($next_id)."'";
  512. $res = api_sql_query($sql1, __FILE__, __LINE__);
  513. $res = api_sql_query($sql2, __FILE__, __LINE__);
  514. }