index.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // The language file that needs to be included.
  9. $language_file = array('glossary', 'admin');
  10. // Including the global initialization file.
  11. require_once '../inc/global.inc.php';
  12. require_once api_get_path(LIBRARY_PATH).'glossary.lib.php';
  13. require_once api_get_path(LIBRARY_PATH).'export.lib.inc.php';
  14. require_once api_get_path(LIBRARY_PATH).'import.lib.php';
  15. $current_course_tool = TOOL_GLOSSARY;
  16. // The section (tabs).
  17. $this_section = SECTION_COURSES;
  18. // Notification for unauthorized people.
  19. api_protect_course_script(true);
  20. // Additional javascripts.
  21. $htmlHeadXtra[] = GlossaryManager::javascript_glossary();
  22. $htmlHeadXtra[] = '<script>
  23. function setFocus(){
  24. $("#glossary_title").focus();
  25. }
  26. $(document).ready(function () {
  27. setFocus();
  28. });
  29. </script>';
  30. // setting the tool constants
  31. $tool = TOOL_GLOSSARY;
  32. // Tracking
  33. event_access_tool(TOOL_GLOSSARY);
  34. function sorter($item1, $item2) {
  35. if ($item1[2] == $item2[2])
  36. return 0;
  37. return $item1[2] < $item2[2] ? -1 : 1;
  38. }
  39. // Displaying the header
  40. $action = isset($_GET['action']) ? $_GET['action'] : null;
  41. $tool = 'GlossaryManagement';
  42. $interbreadcrumb[] = array ("url"=>"index.php", "name"=> get_lang('Glossary'));
  43. if (!empty($action)) {
  44. }
  45. switch ($action) {
  46. case 'addglossary':
  47. $tool_name = get_lang('Add');
  48. break;
  49. case 'edit_glossary':
  50. $tool_name = get_lang('Edit');
  51. break;
  52. case 'import':
  53. $tool_name = get_lang('ImportGlossary');
  54. break;
  55. case 'changeview':
  56. $tool_name = get_lang('List');
  57. break;
  58. }
  59. if (isset($_GET['action']) && $_GET['action'] == 'export') {
  60. $data = GlossaryManager::get_glossary_data(0, GlossaryManager::get_number_glossary_terms (api_get_session_id()), 0, 'ASC');
  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::export_table_csv_utf8($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' AND 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('glossary','post', api_get_self().'?action='.Security::remove_XSS($_GET['action']));
  87. // Setting the form elements
  88. $form->addElement('header', '', get_lang('TermAddNew'));
  89. $form->addElement('text', 'glossary_title', get_lang('TermName'), array('size'=>'80', 'id'=>'glossary_title'));
  90. //$form->applyFilter('glossary_title', 'html_filter');
  91. $form->addElement('html_editor', 'glossary_comment', get_lang('TermDefinition'), null, array('ToolbarSet' => 'Glossary', 'Width' => '90%', 'Height' => '300'));
  92. $form->addElement('style_submit_button', 'SubmitGlossary', get_lang('TermAddButton'), 'class="save"');
  93. // setting the rules
  94. $form->addRule('glossary_title',get_lang('ThisFieldIsRequired'), 'required');
  95. // The validation or display
  96. if ($form->validate()) {
  97. $check = Security::check_token('post');
  98. if ($check) {
  99. $values = $form->exportValues();
  100. GlossaryManager::save_glossary($values);
  101. }
  102. Security::clear_token();
  103. GlossaryManager::display_glossary();
  104. } else {
  105. $token = Security::get_token();
  106. $form->addElement('hidden','sec_token');
  107. $form->setConstants(array('sec_token' => $token));
  108. $form->display();
  109. }
  110. break;
  111. case 'edit_glossary':
  112. if (is_numeric($_GET['glossary_id'])) {
  113. // initiate the object
  114. $form = new FormValidator('glossary','post', api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&glossary_id='.Security::remove_XSS($_GET['glossary_id']));
  115. // Setting the form elements
  116. $form->addElement('header', '', get_lang('TermEdit'));
  117. $form->addElement('hidden', 'glossary_id');
  118. $form->addElement('text', 'glossary_title', get_lang('TermName'),array('size'=>'80'));
  119. //$form->applyFilter('glossary_title', 'html_filter');
  120. $form->addElement('html_editor', 'glossary_comment', get_lang('TermDefinition'), null, array('ToolbarSet' => 'Glossary', 'Width' => '90%', 'Height' => '300'));
  121. $element = $form->addElement('text', 'insert_date', get_lang('CreationDate'),array('size'=>'100'));
  122. $element ->freeze();
  123. $element = $form->addElement('text', 'update_date', get_lang('UpdateDate'),array('size'=>'100'));
  124. $element ->freeze();
  125. $form->addElement('style_submit_button', 'SubmitGlossary', get_lang('TermUpdateButton'), 'class="save"');
  126. // setting the defaults
  127. $glossary_data = GlossaryManager::get_glossary_information($_GET['glossary_id']);
  128. // Date treatment for timezones
  129. if (!empty($glossary_data['insert_date']) && $glossary_data['insert_date'] != '0000-00-00 00:00:00:') {
  130. $glossary_data['insert_date'] = api_get_local_time($glossary_data['insert_date']);
  131. } else {
  132. $glossary_data['insert_date'] = '';
  133. }
  134. if (!empty($glossary_data['update_date']) && $glossary_data['update_date'] != '0000-00-00 00:00:00:') {
  135. $glossary_data['update_date'] = api_get_local_time($glossary_data['update_date']);
  136. } else {
  137. $glossary_data['update_date'] = '';
  138. }
  139. $form->setDefaults($glossary_data);
  140. // setting the rules
  141. $form->addRule('glossary_title', get_lang('ThisFieldIsRequired'), 'required');
  142. // The validation or display
  143. if ($form->validate()) {
  144. $check = Security::check_token('post');
  145. if ($check) {
  146. $values = $form->exportValues();
  147. GlossaryManager::update_glossary($values);
  148. }
  149. Security::clear_token();
  150. GlossaryManager::display_glossary();
  151. } else {
  152. $token = Security::get_token();
  153. $form->addElement('hidden', 'sec_token');
  154. $form->setConstants(array('sec_token' => $token));
  155. $form->display();
  156. }
  157. }
  158. break;
  159. case 'delete_glossary':
  160. GlossaryManager::delete_glossary($_GET['glossary_id']);
  161. GlossaryManager::display_glossary();
  162. break;
  163. case 'moveup':
  164. //GlossaryManager::move_glossary('up',$_GET['glossary_id']); //actions not available
  165. GlossaryManager::display_glossary();
  166. break;
  167. case 'movedown':
  168. //GlossaryManager::move_glossary('down',$_GET['glossary_id']); //actions not available
  169. GlossaryManager::display_glossary();
  170. break;
  171. case 'import':
  172. $form = new FormValidator('glossary','post', api_get_self().'?action=import');
  173. $form->addElement('header', '', get_lang('ImportGlossary'));
  174. $form->addElement('file', 'file', get_lang('ImportCSVFileLocation'));
  175. $form->addElement('checkbox', 'replace', null, get_lang('DeleteAllGlossaryTerms'));
  176. $form->addElement('style_submit_button', 'SubmitImport', get_lang('Import'), 'class="save"');
  177. $form->display();
  178. echo get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')';
  179. echo '<pre>
  180. <strong>term</strong>;<strong>definition</strong>;
  181. "Hello";"Hola";
  182. "Good";"Bueno";</pre>';
  183. if ($form->validate()) {
  184. //this is a bad idea //jm
  185. if (isset($_POST['replace']) && $_POST['replace']) {
  186. foreach (GlossaryManager::get_glossary_terms() as $term) {
  187. if (!GlossaryManager::delete_glossary($term['id'], false)) {
  188. Display::display_error_message (get_lang ("CannotDeleteGlossary") . ':' . $term['id']);
  189. }
  190. }
  191. }
  192. //$data = Import::csv_to_array($_FILES['file']['tmp_name']);
  193. $data = Import::csv_reader($_FILES['file']['tmp_name']);
  194. $good = 0;
  195. $bad = 0;
  196. foreach ($data as $item) {
  197. if (GlossaryManager::save_glossary(array('glossary_title' => $item['term'], 'glossary_comment' => $item['definition']), false))
  198. $good++;
  199. else
  200. $bad++;
  201. }
  202. Display::display_confirmation_message (get_lang ("TermsImported") . ':' . $good);
  203. if ($bad)
  204. Display::display_error_message (get_lang ("TermsNotImported") . ':' . $bad);
  205. GlossaryManager::display_glossary();
  206. }
  207. break;
  208. default:
  209. GlossaryManager::display_glossary();
  210. break;
  211. }
  212. } else {
  213. GlossaryManager::display_glossary();
  214. }
  215. // Footer
  216. Display::display_footer();