index.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * @package chamilo.glossary
  6. * @author Christian Fasanando, initial version
  7. * @author Bas Wijnen import/export to CSV
  8. */
  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. // Tracking
  25. Event::event_access_tool(TOOL_GLOSSARY);
  26. function sorter($item1, $item2) {
  27. if ($item1[2] == $item2[2]) {
  28. return 0;
  29. }
  30. return $item1[2] < $item2[2] ? -1 : 1;
  31. }
  32. // Displaying the header
  33. $action = isset($_GET['action']) ? $_GET['action'] : '';
  34. $currentUrl = api_get_self().'?'.api_get_cidreq();
  35. $interbreadcrumb[] = array('url' => 'index.php?'.api_get_cidreq(), 'name' => get_lang('Glossary'));
  36. $content = '';
  37. $tool_name = '';
  38. switch ($action) {
  39. case 'addglossary':
  40. if (!api_is_allowed_to_edit(null, true)) {
  41. api_not_allowed(true);
  42. }
  43. $tool_name = get_lang('Add');
  44. $form = new FormValidator(
  45. 'glossary',
  46. 'post',
  47. api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&'.api_get_cidreq()
  48. );
  49. // Setting the form elements
  50. $form->addElement('header', get_lang('TermAddNew'));
  51. $form->addElement(
  52. 'text',
  53. 'name',
  54. get_lang('TermName'),
  55. array('id' => 'glossary_title')
  56. );
  57. $form->addElement(
  58. 'html_editor',
  59. 'description',
  60. get_lang('TermDefinition'),
  61. null,
  62. array('ToolbarSet' => 'Glossary', 'Height' => '300')
  63. );
  64. $form->addButtonCreate(get_lang('TermAddButton'), 'SubmitGlossary');
  65. // setting the rules
  66. $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  67. // The validation or display
  68. if ($form->validate()) {
  69. $check = Security::check_token('post');
  70. if ($check) {
  71. $values = $form->exportValues();
  72. GlossaryManager::save_glossary($values);
  73. }
  74. Security::clear_token();
  75. header('Location: '.$currentUrl);
  76. exit;
  77. } else {
  78. $token = Security::get_token();
  79. $form->addElement('hidden', 'sec_token');
  80. $form->setConstants(array('sec_token' => $token));
  81. $content = $form->returnForm();
  82. }
  83. break;
  84. case 'edit_glossary':
  85. if (!api_is_allowed_to_edit(null, true)) {
  86. api_not_allowed(true);
  87. }
  88. $tool_name = get_lang('Edit');
  89. if (is_numeric($_GET['glossary_id'])) {
  90. // initiate the object
  91. $form = new FormValidator(
  92. 'glossary',
  93. 'post',
  94. api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&glossary_id='.intval($_GET['glossary_id']).'&'.api_get_cidreq()
  95. );
  96. // Setting the form elements
  97. $form->addElement('header', get_lang('TermEdit'));
  98. $form->addElement('hidden', 'glossary_id');
  99. $form->addElement('text', 'name', get_lang('TermName'));
  100. $form->addElement(
  101. 'html_editor',
  102. 'description',
  103. get_lang('TermDefinition'),
  104. null,
  105. array('ToolbarSet' => 'Glossary', 'Height' => '300')
  106. );
  107. // setting the defaults
  108. $glossary_data = GlossaryManager::get_glossary_information($_GET['glossary_id']);
  109. // Date treatment for timezones
  110. if (!empty($glossary_data['insert_date'])) {
  111. $glossary_data['insert_date'] = Display::dateToStringAgoAndLongDate($glossary_data['insert_date']);
  112. } else {
  113. $glossary_data['insert_date'] = '';
  114. }
  115. if (!empty($glossary_data['update_date'])) {
  116. $glossary_data['update_date'] = Display::dateToStringAgoAndLongDate($glossary_data['update_date']);
  117. } else {
  118. $glossary_data['update_date'] = '';
  119. }
  120. $form->addLabel(get_lang('CreationDate'), $glossary_data['insert_date']);
  121. $form->addLabel(get_lang('UpdateDate'), $glossary_data['update_date']);
  122. $form->addButtonUpdate(get_lang('TermUpdateButton'), 'SubmitGlossary');
  123. $form->setDefaults($glossary_data);
  124. // setting the rules
  125. $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  126. // The validation or display
  127. if ($form->validate()) {
  128. $check = Security::check_token('post');
  129. if ($check) {
  130. $values = $form->exportValues();
  131. GlossaryManager::update_glossary($values);
  132. }
  133. Security::clear_token();
  134. header('Location: '.$currentUrl);
  135. exit;
  136. } else {
  137. $token = Security::get_token();
  138. $form->addElement('hidden', 'sec_token');
  139. $form->setConstants(array('sec_token' => $token));
  140. $content = $form->returnForm();
  141. }
  142. }
  143. break;
  144. case 'delete_glossary':
  145. if (!api_is_allowed_to_edit(null, true)) {
  146. api_not_allowed(true);
  147. }
  148. GlossaryManager::delete_glossary($_GET['glossary_id']);
  149. Security::clear_token();
  150. header('Location: '.$currentUrl);
  151. exit;
  152. break;
  153. case 'moveup':
  154. //GlossaryManager::move_glossary('up',$_GET['glossary_id']); //actions not available
  155. GlossaryManager::display_glossary();
  156. break;
  157. case 'movedown':
  158. //GlossaryManager::move_glossary('down',$_GET['glossary_id']); //actions not available
  159. GlossaryManager::display_glossary();
  160. break;
  161. case 'import':
  162. if (!api_is_allowed_to_edit(null, true)) {
  163. api_not_allowed(true);
  164. }
  165. $tool_name = get_lang('ImportGlossary');
  166. $form = new FormValidator(
  167. 'glossary',
  168. 'post',
  169. api_get_self().'?action=import&'.api_get_cidreq()
  170. );
  171. $form->addElement('header', '', get_lang('ImportGlossary'));
  172. $form->addElement('file', 'file', get_lang('ImportCSVFileLocation'));
  173. $form->addElement('checkbox', 'replace', null, get_lang('DeleteAllGlossaryTerms'));
  174. $form->addElement('checkbox', 'update', null, get_lang('UpdateExistingGlossaryTerms'));
  175. $form->addButtonImport(get_lang('Import'), 'SubmitImport');
  176. $content = $form->returnForm();
  177. $content .= get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')';
  178. $content .= '<pre>
  179. <strong>term</strong>;<strong>definition</strong>;
  180. "Hello";"Hola";
  181. "Goodbye";"Adiós";
  182. </pre>';
  183. if ($form->validate()) {
  184. $termsDeleted = [];
  185. //this is a bad idea //jm
  186. if (isset($_POST['replace']) && $_POST['replace']) {
  187. foreach (GlossaryManager::get_glossary_terms() as $term) {
  188. if (!GlossaryManager::delete_glossary($term['id'], false)) {
  189. Display::addFlash(
  190. Display::return_message(get_lang("CannotDeleteGlossary") . ':' . $term['id'], 'error')
  191. );
  192. } else {
  193. $termsDeleted[] = $term['name'];
  194. }
  195. }
  196. }
  197. $updateTerms = isset($_POST['update']) && $_POST['update'] ? true : false;
  198. $data = Import::csv_reader($_FILES['file']['tmp_name']);
  199. $goodList = [];
  200. $updatedList = [];
  201. $addedList = [];
  202. $badList = [];
  203. $doubles = [];
  204. $added = [];
  205. $termsPerKey = [];
  206. if ($data) {
  207. $termsToAdd = [];
  208. foreach ($data as $item) {
  209. $items = [
  210. 'name' => $item['term'],
  211. 'description' => $item['definition']
  212. ];
  213. $termsToAdd[] = $items;
  214. $termsPerKey[$item['term']] = $items;
  215. }
  216. if (empty($termsToAdd)) {
  217. Display::addFlash(
  218. Display::return_message(get_lang('NothingToAdd'), 'warning')
  219. );
  220. header('Location: '.$currentUrl);
  221. exit;
  222. }
  223. $repeatItems = array_count_values(array_column($termsToAdd, 'name'));
  224. foreach ($repeatItems as $item => $count) {
  225. if ($count > 1) {
  226. $doubles[] = $item;
  227. }
  228. }
  229. $uniqueTerms = array_unique(array_keys($repeatItems));
  230. foreach ($uniqueTerms as $itemTerm) {
  231. $item = $termsPerKey[$itemTerm];
  232. if ($updateTerms) {
  233. $glossaryInfo = GlossaryManager::get_glossary_term_by_glossary_name($item['name']);
  234. if (!empty($glossaryInfo)) {
  235. $glossaryInfo['description'] = $item['description'];
  236. GlossaryManager::update_glossary($glossaryInfo, false);
  237. $updatedList[] = $item['name'];
  238. } else {
  239. $result = GlossaryManager::save_glossary($item, false);
  240. if ($result) {
  241. $addedList[] = $item['name'];
  242. } else {
  243. $badList[] = $item['name'];
  244. }
  245. }
  246. } else {
  247. $result = GlossaryManager::save_glossary($item, false);
  248. if ($result) {
  249. $addedList[] = $item['name'];
  250. } else {
  251. $badList[] = $item['name'];
  252. }
  253. }
  254. }
  255. }
  256. if (count($termsDeleted) > 0) {
  257. Display::addFlash(
  258. Display::return_message(get_lang("TermDeleted").': '.implode(', ', $termsDeleted))
  259. );
  260. }
  261. if (count($updatedList) > 0) {
  262. Display::addFlash(
  263. Display::return_message(get_lang("TermsUpdated").': '.implode(', ', $updatedList))
  264. );
  265. }
  266. if (count($addedList) > 0) {
  267. Display::addFlash(
  268. Display::return_message(get_lang("TermsAdded").': '.implode(', ', $addedList))
  269. );
  270. }
  271. if (count($badList) > 0) {
  272. Display::addFlash(
  273. Display::return_message(
  274. get_lang("GlossaryTermAlreadyExists").': ' . implode(', ', $badList),
  275. 'error'
  276. )
  277. );
  278. }
  279. if (count($doubles) > 0) {
  280. Display::addFlash(
  281. Display::return_message(
  282. get_lang("TermsDuplicatedInFile").': '.implode(', ', $doubles),
  283. 'warning'
  284. )
  285. );
  286. }
  287. header('Location: '.$currentUrl);
  288. exit;
  289. }
  290. break;
  291. case 'export':
  292. if (!api_is_allowed_to_edit(null, true)) {
  293. api_not_allowed(true);
  294. }
  295. $data = GlossaryManager::get_glossary_data(
  296. 0,
  297. GlossaryManager::get_number_glossary_terms(api_get_session_id()),
  298. 0,
  299. 'ASC'
  300. );
  301. usort($data, "sorter");
  302. $list = array();
  303. $list[] = array('term', 'definition');
  304. foreach ($data as $line) {
  305. $list[] = array($line[0], $line[1]);
  306. }
  307. $filename = 'glossary_course_'.api_get_course_id();
  308. Export::arrayToCsv($list, $filename);
  309. break;
  310. case 'export_to_pdf':
  311. GlossaryManager::export_to_pdf();
  312. break;
  313. case 'changeview':
  314. if (in_array($_GET['view'], array('list','table'))) {
  315. Session::write('glossary_view', $_GET['view']);
  316. } else {
  317. $view = Session::read('glossary_view');
  318. if (empty($view)) {
  319. Session::write('glossary_view', 'table');
  320. }
  321. }
  322. header('Location: '.$currentUrl);
  323. exit;
  324. break;
  325. default:
  326. $tool_name = get_lang('List');
  327. $content = GlossaryManager::display_glossary();
  328. break;
  329. }
  330. Display::display_header($tool_name);
  331. // Tool introduction
  332. Display::display_introduction_section(TOOL_GLOSSARY);
  333. echo $content;
  334. Display::display_footer();