index.php 13 KB

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