index.php 15 KB

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