index.php 14 KB

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