index.php 17 KB

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