index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php //$id: $
  2. /* For licensing terms, see /dokeos_license.txt */
  3. /**
  4. * @package dokeos.glossary
  5. * @author Christian Fasanando, initial version
  6. * @author Patrick Cool <patrick.cool@UGent.be>, Ghent University, Belgium, refactoring and tighter integration in Dokeos
  7. */
  8. // name of the language file that needs to be included
  9. $language_file = array('notebook');
  10. // including the global dokeos file
  11. require_once '../inc/global.inc.php';
  12. // the section (tabs)
  13. $this_section=SECTION_COURSES;
  14. // notice for unauthorized people.
  15. api_protect_course_script(true);
  16. // including additional libraries
  17. require_once api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php';
  18. // additional javascript
  19. $htmlHeadXtra[] = javascript_notebook();
  20. // setting the tool constants
  21. $tool = TOOL_NOTEBOOK;
  22. // tracking
  23. event_access_tool(TOOL_NOTEBOOK);
  24. // tool name
  25. if ( isset($_GET['action']) && $_GET['action'] == 'addnote')
  26. {
  27. $tool = get_lang('NoteAddNew');
  28. $interbreadcrumb[] = array ("url"=>"index.php", "name"=> get_lang('Notebook'));
  29. }
  30. if ( isset($_GET['action']) && $_GET['action'] == 'editnote')
  31. {
  32. $tool = get_lang('ModifyNote');
  33. $interbreadcrumb[] = array ("url"=>"index.php", "name"=> get_lang('Notebook'));
  34. }
  35. // displaying the header
  36. Display::display_header(get_lang(ucfirst($tool)));
  37. // Tool introduction
  38. Display::display_introduction_section(TOOL_NOTEBOOK);
  39. // Action handling: Adding a note
  40. if (isset($_GET['action']) && $_GET['action'] == 'addnote')
  41. {
  42. if (!empty($_GET['isStudentView'])) {
  43. display_notes();
  44. exit;
  45. }
  46. $_SESSION['notebook_view'] = 'creation_date';
  47. // initiate the object
  48. $form = new FormValidator('note','post', api_get_self().'?action='.Security::remove_XSS($_GET['action']));
  49. // settting the form elements
  50. $form->addElement('header', '', get_lang('NoteAddNew'));
  51. $form->addElement('text', 'note_title', get_lang('NoteTitle'),array('size'=>'95'));
  52. //$form->applyFilter('note_title', 'html_filter');
  53. $form->addElement('html_editor', 'note_comment', get_lang('NoteComment'), null, api_is_allowed_to_edit()
  54. ? array('ToolbarSet' => 'Notebook', 'Width' => '100%', 'Height' => '300')
  55. : array('ToolbarSet' => 'NotebookStudent', 'Width' => '100%', 'Height' => '300', 'UserStatus' => 'student')
  56. );
  57. $form->addElement('style_submit_button', 'SubmitNote', get_lang('AddNote'), 'class="add"');
  58. // setting the rules
  59. $form->addRule('note_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  60. // The validation or display
  61. if ( $form->validate() )
  62. {
  63. $check = Security::check_token('post');
  64. if ($check)
  65. {
  66. $values = $form->exportValues();
  67. save_note($values);
  68. }
  69. Security::clear_token();
  70. display_notes();
  71. }
  72. else
  73. {
  74. echo '<div class="actions">';
  75. echo '<a href="index.php">'.Display::return_icon('back.png').' '.get_lang('BackToNotesList').'</a>';
  76. echo '</div>';
  77. $token = Security::get_token();
  78. $form->addElement('hidden','sec_token');
  79. $form->setConstants(array('sec_token' => $token));
  80. $form->display();
  81. }
  82. }
  83. // Action handling: Editing a note
  84. else if (isset($_GET['action']) && $_GET['action'] == 'editnote' && is_numeric($_GET['notebook_id']))
  85. {
  86. if (!empty($_GET['isStudentView'])) {
  87. display_notes();
  88. exit;
  89. }
  90. // initiate the object
  91. $form = new FormValidator('note','post', api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&notebook_id='.Security::remove_XSS($_GET['notebook_id']));
  92. // settting the form elements
  93. $form->addElement('header', '', get_lang('ModifyNote'));
  94. $form->addElement('hidden', 'notebook_id');
  95. $form->addElement('text', 'note_title', get_lang('NoteTitle'),array('size'=>'100'));
  96. //$form->applyFilter('note_title', 'html_filter');
  97. $form->addElement('html_editor', 'note_comment', get_lang('NoteComment'), null, api_is_allowed_to_edit()
  98. ? array('ToolbarSet' => 'Notebook', 'Width' => '100%', 'Height' => '300')
  99. : array('ToolbarSet' => 'NotebookStudent', 'Width' => '100%', 'Height' => '300', 'UserStatus' => 'student')
  100. );
  101. $form->addElement('style_submit_button', 'SubmitNote', get_lang('ModifyNote'), 'class="save"');
  102. // setting the defaults
  103. $defaults = get_note_information(Security::remove_XSS($_GET['notebook_id']));
  104. $form->setDefaults($defaults);
  105. // setting the rules
  106. $form->addRule('note_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  107. // The validation or display
  108. if ( $form->validate() )
  109. {
  110. $check = Security::check_token('post');
  111. if ($check)
  112. {
  113. $values = $form->exportValues();
  114. update_note($values);
  115. }
  116. Security::clear_token();
  117. display_notes();
  118. }
  119. else
  120. {
  121. echo '<div class="actions">';
  122. echo '<a href="index.php">'.Display::return_icon('back.png').' '.get_lang('BackToNotesList').'</a>';
  123. echo '</div>';
  124. $token = Security::get_token();
  125. $form->addElement('hidden','sec_token');
  126. $form->setConstants(array('sec_token' => $token));
  127. $form->display();
  128. }
  129. }
  130. // Action handling: deleting a note
  131. else if (isset($_GET['action']) && $_GET['action'] == 'deletenote' && is_numeric($_GET['notebook_id']))
  132. {
  133. delete_note(Security::remove_XSS($_GET['notebook_id']));
  134. display_notes();
  135. }
  136. // Action handling: changing the view (sorting order)
  137. else if ($_GET['action'] == 'changeview' AND in_array($_GET['view'],array('creation_date','update_date', 'title')))
  138. {
  139. switch ($_GET['view'])
  140. {
  141. case 'creation_date':
  142. if (!$_GET['direction'] OR $_GET['direction'] == 'ASC')
  143. {
  144. Display::display_confirmation_message(get_lang('NotesSortedByCreationDateAsc'));
  145. }
  146. else
  147. {
  148. Display::display_confirmation_message(get_lang('NotesSortedByCreationDateDESC'));
  149. }
  150. break;
  151. case 'update_date':
  152. if (!$_GET['direction'] OR $_GET['direction'] == 'ASC')
  153. {
  154. Display::display_confirmation_message(get_lang('NotesSortedByUpdateDateAsc'));
  155. }
  156. else
  157. {
  158. Display::display_confirmation_message(get_lang('NotesSortedByUpdateDateDESC'));
  159. }
  160. break;
  161. case 'title':
  162. if (!$_GET['direction'] OR $_GET['direction'] == 'ASC')
  163. {
  164. Display::display_confirmation_message(get_lang('NotesSortedByTitleAsc'));
  165. }
  166. else
  167. {
  168. Display::display_confirmation_message(get_lang('NotesSortedByTitleDESC'));
  169. }
  170. break;
  171. }
  172. $_SESSION['notebook_view'] = $_GET['view'];
  173. display_notes();
  174. } else {
  175. display_notes();
  176. }
  177. // footer
  178. Display::display_footer();
  179. /**
  180. * a little bit of javascript to display a prettier warning when deleting a note
  181. *
  182. * @return unknown
  183. *
  184. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  185. * @version januari 2009, dokeos 1.8.6
  186. */
  187. function javascript_notebook()
  188. {
  189. return "<script type=\"text/javascript\">
  190. function confirmation (name)
  191. {
  192. if (confirm(\" ". get_lang("NoteConfirmDelete") ." \"+ name + \" ?\"))
  193. {return true;}
  194. else
  195. {return false;}
  196. }
  197. </script>";
  198. }
  199. /**
  200. * This functions stores the note in the database
  201. *
  202. * @param array $values
  203. *
  204. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  205. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  206. * @version januari 2009, dokeos 1.8.6
  207. */
  208. function save_note($values) {
  209. // Database table definition
  210. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  211. $sql = "INSERT INTO $t_notebook (user_id, course, session_id, title, description, creation_date,update_date,status)
  212. VALUES(
  213. '".Database::escape_string(api_get_user_id())."',
  214. '".Database::escape_string(api_get_course_id())."',
  215. '".Database::escape_string($_SESSION['id_session'])."',
  216. '".Database::escape_string(Security::remove_XSS($values['note_title']))."',
  217. '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['note_comment'])),COURSEMANAGERLOWSECURITY))."',
  218. '".Database::escape_string(date('Y-m-d H:i:s'))."',
  219. '".Database::escape_string(date('Y-m-d H:i:s'))."',
  220. '0')";
  221. $result = api_sql_query($sql, __FILE__, __LINE__);
  222. // display the feedback message
  223. Display::display_confirmation_message(get_lang('NoteAdded'));
  224. }
  225. function get_note_information($notebook_id) {
  226. // Database table definition
  227. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  228. $sql = "SELECT notebook_id AS notebook_id,
  229. title AS note_title,
  230. description AS note_comment
  231. FROM $t_notebook
  232. WHERE notebook_id = '".Database::escape_string($notebook_id)."' ";
  233. $result = api_sql_query($sql, __FILE__, __LINE__);
  234. return Database::fetch_array($result);
  235. }
  236. /**
  237. * This functions updates the note in the database
  238. *
  239. * @param array $values
  240. *
  241. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  242. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  243. * @version januari 2009, dokeos 1.8.6
  244. */
  245. function update_note($values) {
  246. // Database table definition
  247. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  248. $sql = "UPDATE $t_notebook SET
  249. user_id = '".Database::escape_string(api_get_user_id())."',
  250. course = '".Database::escape_string(api_get_course_id())."',
  251. session_id = '".Database::escape_string($_SESSION['id_session'])."',
  252. title = '".Database::escape_string(Security::remove_XSS($values['note_title']))."',
  253. description = '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['note_comment'])),COURSEMANAGERLOWSECURITY))."',
  254. update_date = '".Database::escape_string(date('Y-m-d H:i:s'))."'
  255. WHERE notebook_id = '".Database::escape_string($values['notebook_id'])."'";
  256. $result = Database::query($sql, __FILE__, __LINE__);
  257. // display the feedback message
  258. Display::display_confirmation_message(get_lang('NoteUpdated'));
  259. }
  260. function delete_note($notebook_id) {
  261. // Database table definition
  262. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  263. $sql = "DELETE FROM $t_notebook WHERE notebook_id='".Database::escape_string($notebook_id)."' AND user_id = '".Database::escape_string(api_get_user_id())."'";
  264. $result = Database::query($sql, __FILE__, __LINE__);
  265. Display::display_confirmation_message(get_lang('NoteDeleted'));
  266. }
  267. function display_notes() {
  268. if (!$_GET['direction'])
  269. {
  270. $sort_direction = 'ASC';
  271. $link_sort_direction = 'DESC';
  272. }
  273. elseif ($_GET['direction'] == 'ASC')
  274. {
  275. $sort_direction = 'ASC';
  276. $link_sort_direction = 'DESC';
  277. }
  278. else
  279. {
  280. $sort_direction = 'DESC';
  281. $link_sort_direction = 'ASC';
  282. }
  283. // action links
  284. echo '<div class="actions" style="margin-bottom:20px">';
  285. //if (api_is_allowed_to_edit())
  286. //{
  287. if (!api_is_anonymous()) {
  288. echo '<a href="index.php?'.api_get_cidreq().'&amp;action=addnote">'.Display::return_icon('filenew.gif',get_lang('NoteAddNew')).get_lang('NoteAddNew').'</a>';
  289. } else {
  290. echo '<a href="javascript:void(0)">'.Display::return_icon('filenew.gif',get_lang('NoteAddNew')).get_lang('NoteAddNew').'</a>';
  291. }
  292. //}
  293. echo '<a href="index.php?'.api_get_cidreq().'&amp;action=changeview&amp;view=creation_date&amp;direction='.$link_sort_direction.'">'.Display::return_icon('calendar_select.gif',get_lang('OrderByCreationDate')).get_lang('OrderByCreationDate').'</a>';
  294. echo '<a href="index.php?'.api_get_cidreq().'&amp;action=changeview&amp;view=update_date&amp;direction='.$link_sort_direction.'">'.Display::return_icon('calendar_select.gif',get_lang('OrderByModificationDate')).get_lang('OrderByModificationDate').'</a>';
  295. echo '<a href="index.php?'.api_get_cidreq().'&amp;action=changeview&amp;view=title&amp;direction='.$link_sort_direction.'">'.Display::return_icon('comment.gif',get_lang('OrderByTitle')).get_lang('OrderByTitle').'</a>';
  296. echo '</div>';
  297. if (!in_array($_SESSION['notebook_view'],array('creation_date','update_date', 'title'))) {
  298. $_SESSION['notebook_view'] = 'creation_date';
  299. }
  300. // Database table definition
  301. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  302. $order_by = "";
  303. if ($_SESSION['notebook_view'] == 'creation_date' || $_SESSION['notebook_view'] == 'update_date') {
  304. $order_by = " ORDER BY ".$_SESSION['notebook_view']." $sort_direction ";
  305. } else {
  306. $order_by = " ORDER BY ".$_SESSION['notebook_view']." $sort_direction ";
  307. }
  308. $cond_extra = ($_SESSION['notebook_view']== 'update_date')?" AND update_date <> '0000-00-00 00:00:00'":" ";
  309. $sql = "SELECT * FROM $t_notebook WHERE user_id = '".Database::escape_string(api_get_user_id())."' $cond_extra $order_by";
  310. $result = Database::query($sql, __FILE__, __LINE__);
  311. while ($row = Database::fetch_array($result)) {
  312. echo '<div class="sectiontitle">';
  313. echo '<span style="float: right;"> ('.get_lang('CreationDate').': '.date_to_str_ago($row['creation_date']).'&nbsp;&nbsp;<span class="dropbox_date">'.$row['creation_date'].'</span>';
  314. if ($row['update_date'] <> $row['creation_date']) {
  315. echo ', '.get_lang('UpdateDate').': '.date_to_str_ago($row['update_date']).'&nbsp;&nbsp;<span class="dropbox_date">'.$row['update_date'].'</span>';
  316. }
  317. echo ')</span>';
  318. echo $row['title'];
  319. echo '</div>';
  320. echo '<div class="sectioncomment">'.$row['description'].'</div>';
  321. echo '<div>';
  322. echo '<a href="'.api_get_self().'?action=editnote&amp;notebook_id='.$row['notebook_id'].'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
  323. echo '<a href="'.api_get_self().'?action=deletenote&amp;notebook_id='.$row['notebook_id'].'" onclick="return confirmation(\''.$row['title'].'\');">'.Display::return_icon('delete.gif', get_lang('Delete')).'</a>';
  324. echo '</div>';
  325. }
  326. return $return;
  327. }
  328. ?>