index.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * @package chamilo.notebook
  6. *
  7. * @author Christian Fasanando, initial version
  8. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium,
  9. * refactoring and tighter integration
  10. */
  11. require_once __DIR__.'/../inc/global.inc.php';
  12. $current_course_tool = TOOL_NOTEBOOK;
  13. // The section (tabs)
  14. $this_section = SECTION_COURSES;
  15. // Notice for unauthorized people.
  16. api_protect_course_script(true);
  17. // Additional javascript
  18. $htmlHeadXtra[] = NotebookManager::javascript_notebook();
  19. $htmlHeadXtra[] = '<script>
  20. function setFocus(){
  21. $("#note_title").focus();
  22. }
  23. $(function() {
  24. setFocus();
  25. });
  26. </script>';
  27. // Setting the tool constants
  28. $tool = TOOL_NOTEBOOK;
  29. // Tracking
  30. Event::event_access_tool(TOOL_NOTEBOOK);
  31. $action = isset($_GET['action']) ? $_GET['action'] : '';
  32. $logInfo = [
  33. 'tool' => TOOL_NOTEBOOK,
  34. 'tool_id' => 0,
  35. 'tool_id_detail' => 0,
  36. 'action' => $action,
  37. 'action_details' => '',
  38. ];
  39. Event::registerLog($logInfo);
  40. // Tool name
  41. if ($action === 'addnote') {
  42. $tool = 'NoteAddNew';
  43. $interbreadcrumb[] = [
  44. 'url' => 'index.php?'.api_get_cidreq(),
  45. 'name' => get_lang('ToolNotebook'),
  46. ];
  47. }
  48. if ($action === 'editnote') {
  49. $tool = 'ModifyNote';
  50. $interbreadcrumb[] = [
  51. 'url' => 'index.php?'.api_get_cidreq(),
  52. 'name' => get_lang('ToolNotebook'),
  53. ];
  54. }
  55. // Displaying the header
  56. Display::display_header(get_lang(ucfirst($tool)));
  57. // Tool introduction
  58. Display::display_introduction_section(TOOL_NOTEBOOK);
  59. // Action handling: Adding a note
  60. if ($action === 'addnote') {
  61. if (api_get_session_id() != 0 && !api_is_allowed_to_session_edit(false, true)) {
  62. api_not_allowed();
  63. }
  64. if (!empty($_GET['isStudentView'])) {
  65. NotebookManager::display_notes();
  66. exit;
  67. }
  68. Session::write('notebook_view', 'creation_date');
  69. $form = new FormValidator(
  70. 'note',
  71. 'post',
  72. api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&'.api_get_cidreq()
  73. );
  74. // Setting the form elements
  75. $form->addElement('header', '', get_lang('NoteAddNew'));
  76. $form->addElement('text', 'note_title', get_lang('NoteTitle'), ['id' => 'note_title']);
  77. $form->addElement(
  78. 'html_editor',
  79. 'note_comment',
  80. get_lang('NoteComment'),
  81. null,
  82. api_is_allowed_to_edit() ? ['ToolbarSet' => 'Notebook', 'Width' => '100%', 'Height' => '300'] : ['ToolbarSet' => 'NotebookStudent', 'Width' => '100%', 'Height' => '300', 'UserStatus' => 'student']
  83. );
  84. $form->addButtonCreate(get_lang('AddNote'), 'SubmitNote');
  85. // Setting the rules
  86. $form->addRule('note_title', get_lang('ThisFieldIsRequired'), 'required');
  87. // The validation or display
  88. if ($form->validate()) {
  89. $check = Security::check_token('post');
  90. if ($check) {
  91. $values = $form->exportValues();
  92. $res = NotebookManager::save_note($values);
  93. if ($res) {
  94. echo Display::return_message(get_lang('NoteAdded'), 'confirmation');
  95. }
  96. }
  97. Security::clear_token();
  98. NotebookManager::display_notes();
  99. } else {
  100. echo Display::toolbarAction(
  101. 'add_glossary',
  102. [
  103. Display::url(
  104. Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM),
  105. api_get_self().'?'.api_get_cidreq()
  106. ),
  107. ]
  108. );
  109. $token = Security::get_token();
  110. $form->addElement('hidden', 'sec_token');
  111. $form->setConstants(['sec_token' => $token]);
  112. $form->display();
  113. }
  114. } elseif ($action === 'editnote' && is_numeric($_GET['notebook_id'])) {
  115. // Action handling: Editing a note
  116. if (!empty($_GET['isStudentView'])) {
  117. NotebookManager::display_notes();
  118. exit;
  119. }
  120. // Initialize the object
  121. $form = new FormValidator(
  122. 'note',
  123. 'post',
  124. api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&notebook_id='.intval($_GET['notebook_id']).'&'.api_get_cidreq()
  125. );
  126. // Setting the form elements
  127. $form->addElement('header', '', get_lang('ModifyNote'));
  128. $form->addElement('hidden', 'notebook_id');
  129. $form->addElement('text', 'note_title', get_lang('NoteTitle'), ['size' => '100']);
  130. $form->addElement(
  131. 'html_editor',
  132. 'note_comment',
  133. get_lang('NoteComment'),
  134. null,
  135. api_is_allowed_to_edit()
  136. ? ['ToolbarSet' => 'Notebook', 'Width' => '100%', 'Height' => '300']
  137. : ['ToolbarSet' => 'NotebookStudent', 'Width' => '100%', 'Height' => '300', 'UserStatus' => 'student']
  138. );
  139. $form->addButtonUpdate(get_lang('ModifyNote'), 'SubmitNote');
  140. // Setting the defaults
  141. $defaults = NotebookManager::get_note_information(Security::remove_XSS($_GET['notebook_id']));
  142. $form->setDefaults($defaults);
  143. // Setting the rules
  144. $form->addRule('note_title', get_lang('ThisFieldIsRequired'), 'required');
  145. // The validation or display
  146. if ($form->validate()) {
  147. $check = Security::check_token('post');
  148. if ($check) {
  149. $values = $form->exportValues();
  150. $res = NotebookManager::update_note($values);
  151. if ($res) {
  152. echo Display::return_message(get_lang('NoteUpdated'), 'confirmation');
  153. }
  154. }
  155. Security::clear_token();
  156. NotebookManager::display_notes();
  157. } else {
  158. echo Display::toolbarAction(
  159. 'add_glossary',
  160. [
  161. Display::url(
  162. Display::return_icon('back.png', get_lang('Back'), [], ICON_SIZE_MEDIUM),
  163. api_get_self().'?'.api_get_cidreq()
  164. ),
  165. ]
  166. );
  167. $token = Security::get_token();
  168. $form->addElement('hidden', 'sec_token');
  169. $form->setConstants(['sec_token' => $token]);
  170. $form->display();
  171. }
  172. } elseif ($action === 'deletenote' && is_numeric($_GET['notebook_id'])) {
  173. // Action handling: deleting a note
  174. $res = NotebookManager::delete_note($_GET['notebook_id']);
  175. if ($res) {
  176. echo Display::return_message(get_lang('NoteDeleted'), 'confirmation');
  177. }
  178. NotebookManager::display_notes();
  179. } elseif ($action === 'changeview' &&
  180. in_array($_GET['view'], ['creation_date', 'update_date', 'title'])
  181. ) {
  182. // Action handling: changing the view (sorting order)
  183. switch ($_GET['view']) {
  184. case 'creation_date':
  185. if (!$_GET['direction'] || $_GET['direction'] == 'ASC') {
  186. echo Display::return_message(
  187. get_lang('NotesSortedByCreationDateAsc'),
  188. 'confirmation'
  189. );
  190. } else {
  191. echo Display::return_message(
  192. get_lang('NotesSortedByCreationDateDESC'),
  193. 'confirmation'
  194. );
  195. }
  196. break;
  197. case 'update_date':
  198. if (!$_GET['direction'] || $_GET['direction'] == 'ASC') {
  199. echo Display::return_message(
  200. get_lang('NotesSortedByUpdateDateAsc'),
  201. 'confirmation'
  202. );
  203. } else {
  204. echo Display::return_message(
  205. get_lang('NotesSortedByUpdateDateDESC'),
  206. 'confirmation'
  207. );
  208. }
  209. break;
  210. case 'title':
  211. if (!$_GET['direction'] || $_GET['direction'] == 'ASC') {
  212. echo Display::return_message(
  213. get_lang('NotesSortedByTitleAsc'),
  214. 'confirmation'
  215. );
  216. } else {
  217. echo Display::return_message(
  218. get_lang('NotesSortedByTitleDESC'),
  219. 'confirmation'
  220. );
  221. }
  222. break;
  223. }
  224. Session::write('notebook_view', $_GET['view']);
  225. NotebookManager::display_notes();
  226. } else {
  227. NotebookManager::display_notes();
  228. }
  229. Display::display_footer();