index.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * @package chamilo.glossary
  6. *
  7. * @author Christian Fasanando, initial version
  8. * @author Bas Wijnen import/export to CSV
  9. */
  10. require_once __DIR__.'/../inc/global.inc.php';
  11. $current_course_tool = TOOL_GLOSSARY;
  12. // The section (tabs).
  13. $this_section = SECTION_COURSES;
  14. // Notification for unauthorized people.
  15. api_protect_course_script(true);
  16. // Additional javascripts.
  17. $htmlHeadXtra[] = GlossaryManager::javascript_glossary();
  18. $htmlHeadXtra[] = '<script>
  19. function setFocus(){
  20. $("#glossary_title").focus();
  21. }
  22. $(function() {
  23. setFocus();
  24. $( "#dialog:ui-dialog" ).dialog( "destroy" );
  25. $( "#dialog-confirm" ).dialog({
  26. autoOpen: false,
  27. show: "blind",
  28. resizable: false,
  29. height:300,
  30. modal: true
  31. });
  32. $("#export_opener").click(function() {
  33. var targetUrl = $(this).attr("href");
  34. $( "#dialog-confirm" ).dialog({
  35. width:400,
  36. height:300,
  37. buttons: {
  38. "'.addslashes(get_lang('Download')).'": function() {
  39. var export_format = $("input[name=export_format]:checked").val();
  40. location.href = targetUrl+"&export_format="+export_format;
  41. $( this ).dialog( "close" );
  42. }
  43. }
  44. });
  45. $( "#dialog-confirm" ).dialog("open");
  46. return false;
  47. });
  48. });
  49. </script>';
  50. // Tracking
  51. Event::event_access_tool(TOOL_GLOSSARY);
  52. function sorter($item1, $item2)
  53. {
  54. if ($item1[2] == $item2[2]) {
  55. return 0;
  56. }
  57. return $item1[2] < $item2[2] ? -1 : 1;
  58. }
  59. // Displaying the header
  60. $action = isset($_GET['action']) ? Security::remove_XSS($_GET['action']) : '';
  61. $currentUrl = api_get_self().'?'.api_get_cidreq();
  62. $interbreadcrumb[] = ['url' => 'index.php?'.api_get_cidreq(), 'name' => get_lang('Glossary')];
  63. $content = '';
  64. $tool_name = '';
  65. switch ($action) {
  66. case 'addglossary':
  67. if (!api_is_allowed_to_edit(null, true)) {
  68. api_not_allowed(true);
  69. }
  70. $tool_name = get_lang('Add');
  71. $form = new FormValidator(
  72. 'glossary',
  73. 'post',
  74. api_get_self().'?action=addglossary&'.api_get_cidreq()
  75. );
  76. // Setting the form elements
  77. $form->addElement('header', get_lang('TermAddNew'));
  78. if (api_get_configuration_value('save_titles_as_html')) {
  79. $form->addHtmlEditor(
  80. 'name',
  81. get_lang('TermName'),
  82. false,
  83. false,
  84. ['ToolbarSet' => 'TitleAsHtml']
  85. );
  86. } else {
  87. $form->addElement('text', 'name', get_lang('TermName'), ['id' => 'glossary_title']);
  88. }
  89. $form->addElement(
  90. 'html_editor',
  91. 'description',
  92. get_lang('TermDefinition'),
  93. null,
  94. ['ToolbarSet' => 'Glossary', 'Height' => '300']
  95. );
  96. $form->addButtonCreate(get_lang('TermAddButton'), 'SubmitGlossary');
  97. // setting the rules
  98. $form->addRule('name', 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. header('Location: '.$currentUrl);
  108. exit;
  109. } else {
  110. $token = Security::get_token();
  111. $form->addElement('hidden', 'sec_token');
  112. $form->setConstants(['sec_token' => $token]);
  113. $content = Display::toolbarAction(
  114. 'add_glossary',
  115. [
  116. Display::url(
  117. Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM),
  118. api_get_self().'?'.api_get_cidreq()
  119. ),
  120. ]
  121. );
  122. $content .= $form->returnForm();
  123. }
  124. break;
  125. case 'edit_glossary':
  126. if (!api_is_allowed_to_edit(null, true)) {
  127. api_not_allowed(true);
  128. }
  129. $tool_name = get_lang('Edit');
  130. $glossaryId = isset($_GET['glossary_id']) ? (int) $_GET['glossary_id'] : 0;
  131. if (!empty($glossaryId)) {
  132. // initiate the object
  133. $form = new FormValidator(
  134. 'glossary',
  135. 'post',
  136. api_get_self().'?action=edit_glossary&glossary_id='.$glossaryId.'&'.api_get_cidreq()
  137. );
  138. // Setting the form elements
  139. $form->addElement('header', get_lang('TermEdit'));
  140. $form->addElement('hidden', 'glossary_id');
  141. if (api_get_configuration_value('save_titles_as_html')) {
  142. $form->addHtmlEditor(
  143. 'name',
  144. get_lang('TermName'),
  145. false,
  146. false,
  147. ['ToolbarSet' => 'TitleAsHtml']
  148. );
  149. } else {
  150. $form->addElement('text', 'name', get_lang('TermName'), ['id' => 'glossary_title']);
  151. }
  152. $form->addElement(
  153. 'html_editor',
  154. 'description',
  155. get_lang('TermDefinition'),
  156. null,
  157. ['ToolbarSet' => 'Glossary', 'Height' => '300']
  158. );
  159. // setting the defaults
  160. $glossary_data = GlossaryManager::get_glossary_information($glossaryId);
  161. // Date treatment for timezones
  162. if (!empty($glossary_data['insert_date'])) {
  163. $glossary_data['insert_date'] = Display::dateToStringAgoAndLongDate($glossary_data['insert_date']);
  164. } else {
  165. $glossary_data['insert_date'] = '';
  166. }
  167. if (!empty($glossary_data['update_date'])) {
  168. $glossary_data['update_date'] = Display::dateToStringAgoAndLongDate($glossary_data['update_date']);
  169. } else {
  170. $glossary_data['update_date'] = '';
  171. }
  172. $form->addLabel(get_lang('CreationDate'), $glossary_data['insert_date']);
  173. $form->addLabel(get_lang('UpdateDate'), $glossary_data['update_date']);
  174. $form->addButtonUpdate(get_lang('TermUpdateButton'), 'SubmitGlossary');
  175. $form->setDefaults($glossary_data);
  176. // setting the rules
  177. $form->addRule('name', get_lang('ThisFieldIsRequired'), 'required');
  178. // The validation or display
  179. if ($form->validate()) {
  180. $check = Security::check_token('post');
  181. if ($check) {
  182. $values = $form->exportValues();
  183. GlossaryManager::update_glossary($values);
  184. }
  185. Security::clear_token();
  186. header('Location: '.$currentUrl);
  187. exit;
  188. } else {
  189. $token = Security::get_token();
  190. $form->addElement('hidden', 'sec_token');
  191. $form->setConstants(['sec_token' => $token]);
  192. $content = Display::toolbarAction(
  193. 'edit_glossary',
  194. [
  195. Display::url(
  196. Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM),
  197. api_get_self().'?'.api_get_cidreq()
  198. ),
  199. ]
  200. );
  201. $content .= $form->returnForm();
  202. }
  203. }
  204. break;
  205. case 'delete_glossary':
  206. if (!api_is_allowed_to_edit(null, true)) {
  207. api_not_allowed(true);
  208. }
  209. GlossaryManager::delete_glossary($_GET['glossary_id']);
  210. Security::clear_token();
  211. header('Location: '.$currentUrl);
  212. exit;
  213. break;
  214. case 'moveup':
  215. //GlossaryManager::move_glossary('up',$_GET['glossary_id']); //actions not available
  216. GlossaryManager::display_glossary();
  217. break;
  218. case 'movedown':
  219. //GlossaryManager::move_glossary('down',$_GET['glossary_id']); //actions not available
  220. GlossaryManager::display_glossary();
  221. break;
  222. case 'import':
  223. if (!api_is_allowed_to_edit(null, true)) {
  224. api_not_allowed(true);
  225. }
  226. $tool_name = get_lang('ImportGlossary');
  227. $form = new FormValidator(
  228. 'glossary',
  229. 'post',
  230. api_get_self().'?action=import&'.api_get_cidreq()
  231. );
  232. $form->addHeader(get_lang('ImportGlossary'));
  233. $form->addElement('file', 'file', get_lang('File'));
  234. $group = [];
  235. $group[] = $form->createElement(
  236. 'radio',
  237. 'file_type',
  238. '',
  239. 'CSV',
  240. 'csv'
  241. );
  242. $group[] = $form->createElement(
  243. 'radio',
  244. 'file_type',
  245. '',
  246. 'XLS',
  247. 'xls'
  248. );
  249. $form->addGroup($group, '', get_lang('FileType'), null);
  250. $form->addElement('checkbox', 'replace', null, get_lang('DeleteAllGlossaryTerms'));
  251. $form->addElement('checkbox', 'update', null, get_lang('UpdateExistingGlossaryTerms'));
  252. $form->addButtonImport(get_lang('Import'), 'SubmitImport');
  253. $form->setDefaults(['file_type' => 'csv']);
  254. $content = $form->returnForm();
  255. $content .= get_lang('CSVMustLookLike').' ('.get_lang('MandatoryFields').')';
  256. $content .= '<pre>
  257. <strong>term</strong>;<strong>definition</strong>;
  258. "Hello";"Hola";
  259. "Goodbye";"Adiós";
  260. </pre>';
  261. if ($form->validate()) {
  262. $values = $form->getSubmitValues();
  263. $termsDeleted = [];
  264. //this is a bad idea //jm
  265. if (isset($_POST['replace']) && $_POST['replace']) {
  266. foreach (GlossaryManager::get_glossary_terms() as $term) {
  267. if (!GlossaryManager::delete_glossary($term['id'], false)) {
  268. Display::addFlash(
  269. Display::return_message(get_lang('CannotDeleteGlossary').':'.$term['id'], 'error')
  270. );
  271. } else {
  272. $termsDeleted[] = $term['name'];
  273. }
  274. }
  275. }
  276. $updateTerms = isset($_POST['update']) && $_POST['update'] ? true : false;
  277. $format = $values['file_type'];
  278. switch ($format) {
  279. case 'csv':
  280. $data = Import::csvToArray($_FILES['file']['tmp_name']);
  281. break;
  282. case 'xls':
  283. $data = Import::xlsToArray($_FILES['file']['tmp_name']);
  284. break;
  285. }
  286. $goodList = [];
  287. $updatedList = [];
  288. $addedList = [];
  289. $badList = [];
  290. $doubles = [];
  291. $added = [];
  292. $termsPerKey = [];
  293. if ($data) {
  294. $termsToAdd = [];
  295. foreach ($data as $item) {
  296. if (!isset($item['term'])) {
  297. continue;
  298. }
  299. $items = [
  300. 'name' => $item['term'],
  301. 'description' => $item['definition'],
  302. ];
  303. $termsToAdd[] = $items;
  304. $termsPerKey[$item['term']] = $items;
  305. }
  306. if (empty($termsToAdd)) {
  307. Display::addFlash(
  308. Display::return_message(get_lang('NothingToAdd'), 'warning')
  309. );
  310. header('Location: '.$currentUrl);
  311. exit;
  312. }
  313. $repeatItems = array_count_values(array_column($termsToAdd, 'name'));
  314. foreach ($repeatItems as $item => $count) {
  315. if ($count > 1) {
  316. $doubles[] = $item;
  317. }
  318. }
  319. $uniqueTerms = array_unique(array_keys($repeatItems));
  320. foreach ($uniqueTerms as $itemTerm) {
  321. $item = $termsPerKey[$itemTerm];
  322. if ($updateTerms) {
  323. $glossaryInfo = GlossaryManager::get_glossary_term_by_glossary_name($item['name']);
  324. if (!empty($glossaryInfo)) {
  325. $glossaryInfo['description'] = $item['description'];
  326. GlossaryManager::update_glossary($glossaryInfo, false);
  327. $updatedList[] = $item['name'];
  328. } else {
  329. $result = GlossaryManager::save_glossary($item, false);
  330. if ($result) {
  331. $addedList[] = $item['name'];
  332. } else {
  333. $badList[] = $item['name'];
  334. }
  335. }
  336. } else {
  337. $result = GlossaryManager::save_glossary($item, false);
  338. if ($result) {
  339. $addedList[] = $item['name'];
  340. } else {
  341. $badList[] = $item['name'];
  342. }
  343. }
  344. }
  345. }
  346. if (count($termsDeleted) > 0) {
  347. Display::addFlash(
  348. Display::return_message(get_lang('TermDeleted').': '.implode(', ', $termsDeleted))
  349. );
  350. }
  351. if (count($updatedList) > 0) {
  352. Display::addFlash(
  353. Display::return_message(get_lang('TermsUpdated').': '.implode(', ', $updatedList))
  354. );
  355. }
  356. if (count($addedList) > 0) {
  357. Display::addFlash(
  358. Display::return_message(get_lang('TermsAdded').': '.implode(', ', $addedList))
  359. );
  360. }
  361. if (count($badList) > 0) {
  362. Display::addFlash(
  363. Display::return_message(
  364. get_lang('GlossaryTermAlreadyExists').': '.implode(', ', $badList),
  365. 'error'
  366. )
  367. );
  368. }
  369. if (count($doubles) > 0) {
  370. Display::addFlash(
  371. Display::return_message(
  372. get_lang('TermsDuplicatedInFile').': '.implode(', ', $doubles),
  373. 'warning'
  374. )
  375. );
  376. }
  377. header('Location: '.$currentUrl);
  378. exit;
  379. }
  380. break;
  381. case 'export':
  382. if (!api_is_allowed_to_edit(null, true)) {
  383. api_not_allowed(true);
  384. }
  385. $format = isset($_GET['export_format']) ? $_GET['export_format'] : 'csv';
  386. GlossaryManager::exportToFormat($format);
  387. break;
  388. case 'changeview':
  389. if (in_array($_GET['view'], ['list', 'table'])) {
  390. Session::write('glossary_view', $_GET['view']);
  391. } else {
  392. $view = Session::read('glossary_view');
  393. $defaultView = api_get_configuration_value('default_glossary_view');
  394. if (empty($defaultView)) {
  395. $defaultView = 'table';
  396. }
  397. if (empty($view)) {
  398. Session::write('glossary_view', $defaultView);
  399. }
  400. }
  401. header('Location: '.$currentUrl);
  402. exit;
  403. break;
  404. case 'export_documents':
  405. GlossaryManager::movePdfToDocuments();
  406. header('Location: '.$currentUrl);
  407. exit;
  408. break;
  409. default:
  410. $tool_name = get_lang('List');
  411. $htmlHeadXtra[] = '<script type="text/javascript" src="'.api_get_path(WEB_CODE_PATH).'glossary/glossary.js.php?add_ready=1&'.api_get_cidreq().'"></script>';
  412. $htmlHeadXtra[] = api_get_js('jquery.highlight.js');
  413. $content = GlossaryManager::display_glossary();
  414. break;
  415. }
  416. Display::display_header($tool_name);
  417. Display::display_introduction_section(TOOL_GLOSSARY);
  418. echo $content;
  419. $extra = '<div id="dialog-confirm" title="'.get_lang('ConfirmYourChoice').'">';
  420. $form = new FormValidator(
  421. 'report',
  422. 'post',
  423. api_get_self().'?'.api_get_cidreq(),
  424. null,
  425. ['class' => 'form-vertical']
  426. );
  427. $form->addElement(
  428. 'radio',
  429. 'export_format',
  430. null,
  431. get_lang('ExportAsCSV'),
  432. 'csv',
  433. ['id' => 'export_format_csv_label']
  434. );
  435. $form->addElement(
  436. 'radio',
  437. 'export_format',
  438. null,
  439. get_lang('ExportAsXLS'),
  440. 'xls',
  441. ['id' => 'export_format_xls_label']
  442. );
  443. $form->addElement(
  444. 'radio',
  445. 'export_format',
  446. null,
  447. get_lang('ExportToPDF'),
  448. 'pdf',
  449. ['id' => 'export_format_pdf_label']
  450. );
  451. $form->setDefaults(['export_format' => 'csv']);
  452. $extra .= $form->returnForm();
  453. $extra .= '</div>';
  454. echo $extra;
  455. Display::display_footer();