index.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 (api_get_session_id()!=0 && api_is_allowed_to_session_edit(false,true)==false) {
  43. api_not_allowed();
  44. }
  45. if (!empty($_GET['isStudentView'])) {
  46. display_notes();
  47. exit;
  48. }
  49. $_SESSION['notebook_view'] = 'creation_date';
  50. // initiate the object
  51. $form = new FormValidator('note','post', api_get_self().'?action='.Security::remove_XSS($_GET['action']));
  52. // settting the form elements
  53. $form->addElement('header', '', get_lang('NoteAddNew'));
  54. $form->addElement('text', 'note_title', get_lang('NoteTitle'),array('size'=>'95'));
  55. //$form->applyFilter('note_title', 'html_filter');
  56. $form->addElement('html_editor', 'note_comment', get_lang('NoteComment'), null, api_is_allowed_to_edit()
  57. ? array('ToolbarSet' => 'Notebook', 'Width' => '100%', 'Height' => '300')
  58. : array('ToolbarSet' => 'NotebookStudent', 'Width' => '100%', 'Height' => '300', 'UserStatus' => 'student')
  59. );
  60. $form->addElement('style_submit_button', 'SubmitNote', get_lang('AddNote'), 'class="add"');
  61. // setting the rules
  62. $form->addRule('note_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  63. // The validation or display
  64. if ( $form->validate() )
  65. {
  66. $check = Security::check_token('post');
  67. if ($check)
  68. {
  69. $values = $form->exportValues();
  70. save_note($values);
  71. }
  72. Security::clear_token();
  73. display_notes();
  74. }
  75. else
  76. {
  77. echo '<div class="actions">';
  78. echo '<a href="index.php">'.Display::return_icon('back.png').' '.get_lang('BackToNotesList').'</a>';
  79. echo '</div>';
  80. $token = Security::get_token();
  81. $form->addElement('hidden','sec_token');
  82. $form->setConstants(array('sec_token' => $token));
  83. $form->display();
  84. }
  85. }
  86. // Action handling: Editing a note
  87. else if (isset($_GET['action']) && $_GET['action'] == 'editnote' && is_numeric($_GET['notebook_id']))
  88. {
  89. if (!empty($_GET['isStudentView'])) {
  90. display_notes();
  91. exit;
  92. }
  93. // initiate the object
  94. $form = new FormValidator('note','post', api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&notebook_id='.Security::remove_XSS($_GET['notebook_id']));
  95. // settting the form elements
  96. $form->addElement('header', '', get_lang('ModifyNote'));
  97. $form->addElement('hidden', 'notebook_id');
  98. $form->addElement('text', 'note_title', get_lang('NoteTitle'),array('size'=>'100'));
  99. //$form->applyFilter('note_title', 'html_filter');
  100. $form->addElement('html_editor', 'note_comment', get_lang('NoteComment'), null, api_is_allowed_to_edit()
  101. ? array('ToolbarSet' => 'Notebook', 'Width' => '100%', 'Height' => '300')
  102. : array('ToolbarSet' => 'NotebookStudent', 'Width' => '100%', 'Height' => '300', 'UserStatus' => 'student')
  103. );
  104. $form->addElement('style_submit_button', 'SubmitNote', get_lang('ModifyNote'), 'class="save"');
  105. // setting the defaults
  106. $defaults = get_note_information(Security::remove_XSS($_GET['notebook_id']));
  107. $form->setDefaults($defaults);
  108. // setting the rules
  109. $form->addRule('note_title', '<div class="required">'.get_lang('ThisFieldIsRequired'), 'required');
  110. // The validation or display
  111. if ( $form->validate() )
  112. {
  113. $check = Security::check_token('post');
  114. if ($check)
  115. {
  116. $values = $form->exportValues();
  117. update_note($values);
  118. }
  119. Security::clear_token();
  120. display_notes();
  121. }
  122. else
  123. {
  124. echo '<div class="actions">';
  125. echo '<a href="index.php">'.Display::return_icon('back.png').' '.get_lang('BackToNotesList').'</a>';
  126. echo '</div>';
  127. $token = Security::get_token();
  128. $form->addElement('hidden','sec_token');
  129. $form->setConstants(array('sec_token' => $token));
  130. $form->display();
  131. }
  132. }
  133. // Action handling: deleting a note
  134. else if (isset($_GET['action']) && $_GET['action'] == 'deletenote' && is_numeric($_GET['notebook_id']))
  135. {
  136. delete_note(Security::remove_XSS($_GET['notebook_id']));
  137. display_notes();
  138. }
  139. // Action handling: changing the view (sorting order)
  140. else if ($_GET['action'] == 'changeview' AND in_array($_GET['view'],array('creation_date','update_date', 'title')))
  141. {
  142. switch ($_GET['view'])
  143. {
  144. case 'creation_date':
  145. if (!$_GET['direction'] OR $_GET['direction'] == 'ASC')
  146. {
  147. Display::display_confirmation_message(get_lang('NotesSortedByCreationDateAsc'));
  148. }
  149. else
  150. {
  151. Display::display_confirmation_message(get_lang('NotesSortedByCreationDateDESC'));
  152. }
  153. break;
  154. case 'update_date':
  155. if (!$_GET['direction'] OR $_GET['direction'] == 'ASC')
  156. {
  157. Display::display_confirmation_message(get_lang('NotesSortedByUpdateDateAsc'));
  158. }
  159. else
  160. {
  161. Display::display_confirmation_message(get_lang('NotesSortedByUpdateDateDESC'));
  162. }
  163. break;
  164. case 'title':
  165. if (!$_GET['direction'] OR $_GET['direction'] == 'ASC')
  166. {
  167. Display::display_confirmation_message(get_lang('NotesSortedByTitleAsc'));
  168. }
  169. else
  170. {
  171. Display::display_confirmation_message(get_lang('NotesSortedByTitleDESC'));
  172. }
  173. break;
  174. }
  175. $_SESSION['notebook_view'] = $_GET['view'];
  176. display_notes();
  177. } else {
  178. display_notes();
  179. }
  180. // footer
  181. Display::display_footer();
  182. /**
  183. * a little bit of javascript to display a prettier warning when deleting a note
  184. *
  185. * @return unknown
  186. *
  187. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  188. * @version januari 2009, dokeos 1.8.6
  189. */
  190. function javascript_notebook()
  191. {
  192. return "<script type=\"text/javascript\">
  193. function confirmation (name)
  194. {
  195. if (confirm(\" ". get_lang("NoteConfirmDelete") ." \"+ name + \" ?\"))
  196. {return true;}
  197. else
  198. {return false;}
  199. }
  200. </script>";
  201. }
  202. /**
  203. * This functions stores the note in the database
  204. *
  205. * @param array $values
  206. *
  207. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  208. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  209. * @version januari 2009, dokeos 1.8.6
  210. */
  211. function save_note($values) {
  212. // Database table definition
  213. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  214. $sql = "INSERT INTO $t_notebook (user_id, course, session_id, title, description, creation_date,update_date,status)
  215. VALUES(
  216. '".Database::escape_string(api_get_user_id())."',
  217. '".Database::escape_string(api_get_course_id())."',
  218. '".Database::escape_string($_SESSION['id_session'])."',
  219. '".Database::escape_string(Security::remove_XSS($values['note_title']))."',
  220. '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['note_comment'])),COURSEMANAGERLOWSECURITY))."',
  221. '".Database::escape_string(date('Y-m-d H:i:s'))."',
  222. '".Database::escape_string(date('Y-m-d H:i:s'))."',
  223. '0')";
  224. $result = Database::query($sql, __FILE__, __LINE__);
  225. $id = Database::insert_id();
  226. if ($id > 0) {
  227. //insert into item_property
  228. api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, $id, 'NotebookAdded', api_get_user_id());
  229. }
  230. // display the feedback message
  231. Display::display_confirmation_message(get_lang('NoteAdded'));
  232. }
  233. function get_note_information($notebook_id) {
  234. // Database table definition
  235. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  236. $sql = "SELECT notebook_id AS notebook_id,
  237. title AS note_title,
  238. description AS note_comment,
  239. session_id AS session_id
  240. FROM $t_notebook
  241. WHERE notebook_id = '".Database::escape_string($notebook_id)."' ";
  242. $result = Database::query($sql, __FILE__, __LINE__);
  243. return Database::fetch_array($result);
  244. }
  245. /**
  246. * This functions updates the note in the database
  247. *
  248. * @param array $values
  249. *
  250. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  251. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  252. * @version januari 2009, dokeos 1.8.6
  253. */
  254. function update_note($values) {
  255. // Database table definition
  256. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  257. $sql = "UPDATE $t_notebook SET
  258. user_id = '".Database::escape_string(api_get_user_id())."',
  259. course = '".Database::escape_string(api_get_course_id())."',
  260. session_id = '".Database::escape_string($_SESSION['id_session'])."',
  261. title = '".Database::escape_string(Security::remove_XSS($values['note_title']))."',
  262. description = '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['note_comment'])),COURSEMANAGERLOWSECURITY))."',
  263. update_date = '".Database::escape_string(date('Y-m-d H:i:s'))."'
  264. WHERE notebook_id = '".Database::escape_string($values['notebook_id'])."'";
  265. $result = Database::query($sql, __FILE__, __LINE__);
  266. //update item_property (update)
  267. api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, Database::escape_string($values['notebook_id']), 'NotebookUpdated', api_get_user_id());
  268. // display the feedback message
  269. Display::display_confirmation_message(get_lang('NoteUpdated'));
  270. }
  271. function delete_note($notebook_id) {
  272. // Database table definition
  273. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  274. $sql = "DELETE FROM $t_notebook WHERE notebook_id='".Database::escape_string($notebook_id)."' AND user_id = '".Database::escape_string(api_get_user_id())."'";
  275. $result = Database::query($sql, __FILE__, __LINE__);
  276. //update item_property (delete)
  277. api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, Database::escape_string($notebook_id), 'delete', api_get_user_id());
  278. Display::display_confirmation_message(get_lang('NoteDeleted'));
  279. }
  280. function display_notes() {
  281. global $_user;
  282. if (!$_GET['direction'])
  283. {
  284. $sort_direction = 'ASC';
  285. $link_sort_direction = 'DESC';
  286. }
  287. elseif ($_GET['direction'] == 'ASC')
  288. {
  289. $sort_direction = 'ASC';
  290. $link_sort_direction = 'DESC';
  291. }
  292. else
  293. {
  294. $sort_direction = 'DESC';
  295. $link_sort_direction = 'ASC';
  296. }
  297. // action links
  298. echo '<div class="actions" style="margin-bottom:20px">';
  299. //if (api_is_allowed_to_edit())
  300. //{
  301. if (!api_is_anonymous()) {
  302. if (api_get_session_id()==0)
  303. echo '<a href="index.php?'.api_get_cidreq().'&amp;action=addnote">'.Display::return_icon('filenew.gif',get_lang('NoteAddNew')).get_lang('NoteAddNew').'</a>';
  304. elseif(api_is_allowed_to_session_edit(false,true)){
  305. echo '<a href="index.php?'.api_get_cidreq().'&amp;action=addnote">'.Display::return_icon('filenew.gif',get_lang('NoteAddNew')).get_lang('NoteAddNew').'</a>';
  306. }
  307. } else {
  308. echo '<a href="javascript:void(0)">'.Display::return_icon('filenew.gif',get_lang('NoteAddNew')).get_lang('NoteAddNew').'</a>';
  309. }
  310. //}
  311. 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>';
  312. 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>';
  313. 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>';
  314. echo '</div>';
  315. if (!in_array($_SESSION['notebook_view'],array('creation_date','update_date', 'title'))) {
  316. $_SESSION['notebook_view'] = 'creation_date';
  317. }
  318. // Database table definition
  319. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  320. $order_by = "";
  321. if ($_SESSION['notebook_view'] == 'creation_date' || $_SESSION['notebook_view'] == 'update_date') {
  322. $order_by = " ORDER BY ".$_SESSION['notebook_view']." $sort_direction ";
  323. } else {
  324. $order_by = " ORDER BY ".$_SESSION['notebook_view']." $sort_direction ";
  325. }
  326. //condition for the session
  327. $session_id = api_get_session_id();
  328. $condition_session = api_get_session_condition($session_id);
  329. $cond_extra = ($_SESSION['notebook_view']== 'update_date')?" AND update_date <> '0000-00-00 00:00:00'":" ";
  330. $sql = "SELECT * FROM $t_notebook WHERE user_id = '".Database::escape_string(api_get_user_id())."' $condition_session $cond_extra $order_by";
  331. $result = Database::query($sql, __FILE__, __LINE__);
  332. while ($row = Database::fetch_array($result)) {
  333. //validacion when belongs to a session
  334. $session_img = api_get_session_image($row['session_id'], $_user['status']);
  335. echo '<div class="sectiontitle">';
  336. 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>';
  337. if ($row['update_date'] <> $row['creation_date']) {
  338. echo ', '.get_lang('UpdateDate').': '.date_to_str_ago($row['update_date']).'&nbsp;&nbsp;<span class="dropbox_date">'.$row['update_date'].'</span>';
  339. }
  340. echo ')</span>';
  341. echo $row['title'] . $session_img;
  342. echo '</div>';
  343. echo '<div class="sectioncomment">'.$row['description'].'</div>';
  344. echo '<div>';
  345. echo '<a href="'.api_get_self().'?action=editnote&amp;notebook_id='.$row['notebook_id'].'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
  346. 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>';
  347. echo '</div>';
  348. }
  349. //return $return;
  350. }
  351. ?>