index.php 9.7 KB

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