index.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.glossary
  5. * @author Christian Fasanando, initial version
  6. * @author Bas Wijnen import/export to CSV
  7. */
  8. require_once '../inc/global.inc.php';
  9. $current_course_tool = TOOL_GLOSSARY;
  10. // The section (tabs).
  11. $this_section = SECTION_COURSES;
  12. // Notification for unauthorized people.
  13. api_protect_course_script(true);
  14. // Additional javascripts.
  15. $htmlHeadXtra[] = GlossaryManager::javascript_glossary();
  16. $htmlHeadXtra[] = '<script>
  17. function setFocus(){
  18. $("#glossary_title").focus();
  19. }
  20. $(document).ready(function () {
  21. setFocus();
  22. });
  23. </script>';
  24. // setting the tool constants
  25. $tool = TOOL_GLOSSARY;
  26. // Tracking
  27. Event::event_access_tool(TOOL_GLOSSARY);
  28. function sorter($item1, $item2) {
  29. if ($item1[2] == $item2[2])
  30. return 0;
  31. return $item1[2] < $item2[2] ? -1 : 1;
  32. }
  33. // Displaying the header
  34. $action = isset($_GET['action']) ? $_GET['action'] : null;
  35. $tool = 'GlossaryManagement';
  36. $interbreadcrumb[] = array ("url"=>"index.php", "name"=> get_lang('Glossary'));
  37. if (!empty($action)) {
  38. }
  39. switch ($action) {
  40. case 'addglossary':
  41. $tool_name = get_lang('Add');
  42. break;
  43. case 'edit_glossary':
  44. $tool_name = get_lang('Edit');
  45. break;
  46. case 'import':
  47. $tool_name = get_lang('ImportGlossary');
  48. break;
  49. case 'changeview':
  50. default:
  51. $tool_name = get_lang('List');
  52. break;
  53. }
  54. if (isset($_GET['action']) && $_GET['action'] == 'export') {
  55. $data = GlossaryManager::get_glossary_data(
  56. 0,
  57. GlossaryManager::get_number_glossary_terms(api_get_session_id()),
  58. 0,
  59. 'ASC'
  60. );
  61. usort($data, "sorter");
  62. $list = array();
  63. $list[] = array('term','definition');
  64. foreach($data as $line) {
  65. $list[] = array ($line[0], $line[1]);
  66. }
  67. $filename = 'glossary_course_'.api_get_course_id();
  68. Export::arrayToCsv($list, $filename);
  69. }
  70. if (isset($_GET['action']) && $_GET['action'] == 'export_to_pdf') {
  71. GlossaryManager::export_to_pdf();
  72. }
  73. Display::display_header($tool_name);
  74. // Tool introduction
  75. Display::display_introduction_section(TOOL_GLOSSARY);
  76. if (isset($_GET['action']) && $_GET['action'] == 'changeview' && in_array($_GET['view'], array('list','table'))) {
  77. $_SESSION['glossary_view'] = $_GET['view'];
  78. } else {
  79. if (!isset($_SESSION['glossary_view'])) {
  80. $_SESSION['glossary_view'] = 'table';//Default option
  81. }
  82. }
  83. if (api_is_allowed_to_edit(null, true)) {
  84. switch ($action) {
  85. case 'addglossary':
  86. $form = new FormValidator(
  87. 'glossary',
  88. 'post',
  89. api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&'.api_get_cidreq()
  90. );
  91. // Setting the form elements
  92. $form->addElement('header', '', get_lang('TermAddNew'));
  93. $form->addElement('text', 'glossary_title', get_lang('TermName'), array('size'=>'80', 'id'=>'glossary_title'));
  94. //$form->applyFilter('glossary_title', 'html_filter');
  95. $form->addElement('html_editor', 'glossary_comment', get_lang('TermDefinition'), null, array('ToolbarSet' => 'Glossary', 'Height' => '300'));
  96. $form->addButtonCreate(get_lang('TermAddButton'), 'SubmitGlossary');
  97. // setting the rules
  98. $form->addRule('glossary_title', get_lang('ThisFieldIsRequired'), 'required');
  99. // The validation or display
  100. if ($form->validate()) {
  101. $check = Security::check_token('post');
  102. if ($check) {
  103. $values = $form->exportValues();
  104. GlossaryManager::save_glossary($values);
  105. }
  106. Security::clear_token();
  107. GlossaryManager::display_glossary();
  108. } else {
  109. $token = Security::get_token();
  110. $form->addElement('hidden', 'sec_token');
  111. $form->setConstants(array('sec_token' => $token));
  112. $form->display();
  113. }
  114. break;
  115. case 'edit_glossary':
  116. if (is_numeric($_GET['glossary_id'])) {
  117. // initiate the object
  118. $form = new FormValidator(
  119. 'glossary',
  120. 'post',
  121. api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&glossary_id='.intval($_GET['glossary_id']).'&'.api_get_cidreq()
  122. );
  123. // Setting the form elements
  124. $form->addElement('header', '', get_lang('TermEdit'));
  125. $form->addElement('hidden', 'glossary_id');
  126. $form->addElement('text', 'glossary_title', get_lang('TermName'),array('size'=>'80'));
  127. //$form->applyFilter('glossary_title', 'html_filter');
  128. $form->addElement('html_editor', 'glossary_comment', get_lang('TermDefinition'), null, array('ToolbarSet' => 'Glossary', 'Height' => '300'));
  129. $element = $form->addElement('text', 'insert_date', get_lang('CreationDate'),array('size'=>'100'));
  130. $element->freeze();
  131. $element = $form->addElement('text', 'update_date', get_lang('UpdateDate'),array('size'=>'100'));
  132. $element->freeze();
  133. $form->addButtonUpdate(get_lang('TermUpdateButton'), 'SubmitGlossary');
  134. // setting the defaults
  135. $glossary_data = GlossaryManager::get_glossary_information($_GET['glossary_id']);
  136. // Date treatment for timezones
  137. if (!empty($glossary_data['insert_date']) && $glossary_data['insert_date'] != '0000-00-00 00:00:00:') {
  138. $glossary_data['insert_date'] = api_get_local_time($glossary_data['insert_date']);
  139. } else {
  140. $glossary_data['insert_date'] = '';
  141. }
  142. if (!empty($glossary_data['update_date']) && $glossary_data['update_date'] != '0000-00-00 00:00:00:') {
  143. $glossary_data['update_date'] = api_get_local_time($glossary_data['update_date']);
  144. } else {
  145. $glossary_data['update_date'] = '';
  146. }
  147. $form->setDefaults($glossary_data);
  148. // setting the rules
  149. $form->addRule('glossary_title', get_lang('ThisFieldIsRequired'), 'required');
  150. // The validation or display
  151. if ($form->validate()) {
  152. $check = Security::check_token('post');
  153. if ($check) {
  154. $values = $form->exportValues();
  155. GlossaryManager::update_glossary($values);
  156. }
  157. Security::clear_token();
  158. GlossaryManager::display_glossary();
  159. } else {
  160. $token = Security::get_token();
  161. $form->addElement('hidden', 'sec_token');
  162. $form->setConstants(array('sec_token' => $token));
  163. $form->display();
  164. }
  165. }
  166. break;
  167. case 'delete_glossary':
  168. GlossaryManager::delete_glossary($_GET['glossary_id']);
  169. GlossaryManager::display_glossary();
  170. break;
  171. case 'moveup':
  172. //GlossaryManager::move_glossary('up',$_GET['glossary_id']); //actions not available
  173. GlossaryManager::display_glossary();
  174. break;
  175. case 'movedown':
  176. //GlossaryManager::move_glossary('down',$_GET['glossary_id']); //actions not available
  177. GlossaryManager::display_glossary();
  178. break;
  179. case 'import':
  180. $form = new FormValidator(
  181. 'glossary',
  182. 'post',
  183. api_get_self().'?action=import&'.api_get_cidreq()
  184. );
  185. $form->addElement('header', '', get_lang('ImportGlossary'));
  186. $form->addElement('file', 'file', get_lang('ImportCSVFileLocation'));
  187. $form->addElement('checkbox', 'replace', null, get_lang('DeleteAllGlossaryTerms'));
  188. $form->addButtonImport(get_lang('Import'), 'SubmitImport');
  189. $form->display();
  190. echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')';
  191. echo '<pre>
  192. <strong>term</strong>;<strong>definition</strong>;
  193. "Hello";"Hola";
  194. "Good";"Bueno";</pre>';
  195. if ($form->validate()) {
  196. //this is a bad idea //jm
  197. if (isset($_POST['replace']) && $_POST['replace']) {
  198. foreach (GlossaryManager::get_glossary_terms() as $term) {
  199. if (!GlossaryManager::delete_glossary($term['id'], false)) {
  200. Display::display_error_message(get_lang ("CannotDeleteGlossary") . ':' . $term['id']);
  201. }
  202. }
  203. }
  204. //$data = Import::csvToArray($_FILES['file']['tmp_name']);
  205. $data = Import::csv_reader($_FILES['file']['tmp_name']);
  206. $good = 0;
  207. $bad = 0;
  208. foreach ($data as $item) {
  209. $result = GlossaryManager::save_glossary(
  210. array(
  211. 'glossary_title' => $item['term'],
  212. 'glossary_comment' => $item['definition'],
  213. ),
  214. false
  215. );
  216. if ($result)
  217. $good++;
  218. else
  219. $bad++;
  220. }
  221. Display::display_confirmation_message(get_lang ("TermsImported") . ':' . $good);
  222. if ($bad) {
  223. Display::display_error_message (get_lang ("TermsNotImported") . ':' . $bad);
  224. }
  225. GlossaryManager::display_glossary();
  226. }
  227. break;
  228. default:
  229. GlossaryManager::display_glossary();
  230. break;
  231. }
  232. } else {
  233. GlossaryManager::display_glossary();
  234. }
  235. // Footer
  236. Display::display_footer();