notebook.lib.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This class provides methods for the notebook management.
  5. * Include/require it in your code to use its features.
  6. * @author Carlos Vargas <litox84@gmail.com>, move code of main/notebook up here
  7. * @package chamilo.library
  8. */
  9. class NotebookManager
  10. {
  11. private function __construct() {
  12. }
  13. /**
  14. * a little bit of javascript to display a prettier warning when deleting a note
  15. *
  16. * @return unknown
  17. *
  18. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  19. * @version januari 2009, dokeos 1.8.6
  20. */
  21. function javascript_notebook()
  22. {
  23. return "<script type=\"text/javascript\">
  24. function confirmation (name)
  25. {
  26. if (confirm(\" ". get_lang("NoteConfirmDelete") ." \"+ name + \" ?\"))
  27. {return true;}
  28. else
  29. {return false;}
  30. }
  31. </script>";
  32. }
  33. /**
  34. * This functions stores the note in the database
  35. *
  36. * @param array $values
  37. * @return bool
  38. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  39. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  40. * @version januari 2009, dokeos 1.8.6
  41. *
  42. */
  43. function save_note($values) {
  44. // Database table definition
  45. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  46. $sql = "INSERT INTO $t_notebook (user_id, course, session_id, title, description, creation_date,update_date,status)
  47. VALUES(
  48. '".Database::escape_string(api_get_user_id())."',
  49. '".Database::escape_string(api_get_course_id())."',
  50. '".Database::escape_string($_SESSION['id_session'])."',
  51. '".Database::escape_string(Security::remove_XSS($values['note_title']))."',
  52. '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['note_comment'])),COURSEMANAGERLOWSECURITY))."',
  53. '".Database::escape_string(date('Y-m-d H:i:s'))."',
  54. '".Database::escape_string(date('Y-m-d H:i:s'))."',
  55. '0')";
  56. $result = Database::query($sql);
  57. $id = Database::insert_id();
  58. if ($id > 0) {
  59. //insert into item_property
  60. api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, $id, 'NotebookAdded', api_get_user_id());
  61. }
  62. $affected_rows = Database::affected_rows();
  63. if (!empty($affected_rows)){
  64. return true;
  65. }
  66. }
  67. function get_note_information($notebook_id) {
  68. // Database table definition
  69. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  70. $sql = "SELECT notebook_id AS notebook_id,
  71. title AS note_title,
  72. description AS note_comment,
  73. session_id AS session_id
  74. FROM $t_notebook
  75. WHERE notebook_id = '".Database::escape_string($notebook_id)."' ";
  76. $result = Database::query($sql);
  77. return Database::fetch_array($result);
  78. }
  79. /**
  80. * This functions updates the note in the database
  81. *
  82. * @param array $values
  83. *
  84. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  85. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  86. * @version januari 2009, dokeos 1.8.6
  87. */
  88. function update_note($values) {
  89. // Database table definition
  90. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  91. $sql = "UPDATE $t_notebook SET
  92. user_id = '".Database::escape_string(api_get_user_id())."',
  93. course = '".Database::escape_string(api_get_course_id())."',
  94. session_id = '".Database::escape_string($_SESSION['id_session'])."',
  95. title = '".Database::escape_string(Security::remove_XSS($values['note_title']))."',
  96. description = '".Database::escape_string(Security::remove_XSS(stripslashes(api_html_entity_decode($values['note_comment'])),COURSEMANAGERLOWSECURITY))."',
  97. update_date = '".Database::escape_string(date('Y-m-d H:i:s'))."'
  98. WHERE notebook_id = '".Database::escape_string($values['notebook_id'])."'";
  99. $result = Database::query($sql);
  100. //update item_property (update)
  101. api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, Database::escape_string($values['notebook_id']), 'NotebookUpdated', api_get_user_id());
  102. $affected_rows = Database::affected_rows();
  103. if (!empty($affected_rows)){
  104. return true;
  105. }
  106. }
  107. function delete_note($notebook_id) {
  108. // Database table definition
  109. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  110. $sql = "DELETE FROM $t_notebook WHERE notebook_id='".Database::escape_string($notebook_id)."' AND user_id = '".Database::escape_string(api_get_user_id())."'";
  111. $result = Database::query($sql);
  112. //update item_property (delete)
  113. api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, Database::escape_string($notebook_id), 'delete', api_get_user_id());
  114. $affected_rows = Database::affected_rows();
  115. if (!empty($affected_rows)){
  116. return true;
  117. }
  118. }
  119. function display_notes() {
  120. global $_user;
  121. if (!$_GET['direction'])
  122. {
  123. $sort_direction = 'ASC';
  124. $link_sort_direction = 'DESC';
  125. }
  126. elseif ($_GET['direction'] == 'ASC')
  127. {
  128. $sort_direction = 'ASC';
  129. $link_sort_direction = 'DESC';
  130. }
  131. else
  132. {
  133. $sort_direction = 'DESC';
  134. $link_sort_direction = 'ASC';
  135. }
  136. // action links
  137. echo '<div class="actions" style="margin-bottom:20px">';
  138. //if (api_is_allowed_to_edit())
  139. //{
  140. if (!api_is_anonymous()) {
  141. if (api_get_session_id()==0)
  142. echo '<a href="index.php?'.api_get_cidreq().'&amp;action=addnote">'.Display::return_icon('filenew.gif',get_lang('NoteAddNew')).get_lang('NoteAddNew').'</a>';
  143. elseif(api_is_allowed_to_session_edit(false,true)){
  144. echo '<a href="index.php?'.api_get_cidreq().'&amp;action=addnote">'.Display::return_icon('filenew.gif',get_lang('NoteAddNew')).get_lang('NoteAddNew').'</a>';
  145. }
  146. } else {
  147. echo '<a href="javascript:void(0)">'.Display::return_icon('filenew.gif',get_lang('NoteAddNew')).get_lang('NoteAddNew').'</a>';
  148. }
  149. //}
  150. 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>';
  151. 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>';
  152. 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>';
  153. echo '</div>';
  154. if (!in_array($_SESSION['notebook_view'],array('creation_date','update_date', 'title'))) {
  155. $_SESSION['notebook_view'] = 'creation_date';
  156. }
  157. // Database table definition
  158. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  159. $order_by = "";
  160. if ($_SESSION['notebook_view'] == 'creation_date' || $_SESSION['notebook_view'] == 'update_date') {
  161. $order_by = " ORDER BY ".$_SESSION['notebook_view']." $sort_direction ";
  162. } else {
  163. $order_by = " ORDER BY ".$_SESSION['notebook_view']." $sort_direction ";
  164. }
  165. //condition for the session
  166. $session_id = api_get_session_id();
  167. $condition_session = api_get_session_condition($session_id);
  168. $cond_extra = ($_SESSION['notebook_view']== 'update_date')?" AND update_date <> '0000-00-00 00:00:00'":" ";
  169. $sql = "SELECT * FROM $t_notebook WHERE user_id = '".Database::escape_string(api_get_user_id())."' $condition_session $cond_extra $order_by";
  170. $result = Database::query($sql);
  171. while ($row = Database::fetch_array($result)) {
  172. //validacion when belongs to a session
  173. $session_img = api_get_session_image($row['session_id'], $_user['status']);
  174. $creation_date = api_get_local_time($row['creation_date'], null, date_default_timezone_get());
  175. $update_date = api_get_local_time($row['update_date'], null, date_default_timezone_get());
  176. echo '<div class="sectiontitle">';
  177. echo '<span style="float: right;"> ('.get_lang('CreationDate').': '.date_to_str_ago($creation_date).'&nbsp;&nbsp;<span class="dropbox_date">'.$creation_date.'</span>';
  178. if ($row['update_date'] <> $row['creation_date']) {
  179. echo ', '.get_lang('UpdateDate').': '.date_to_str_ago($update_date).'&nbsp;&nbsp;<span class="dropbox_date">'.$update_date.'</span>';
  180. }
  181. echo ')</span>';
  182. echo $row['title'] . $session_img;
  183. echo '</div>';
  184. echo '<div class="sectioncomment">'.$row['description'].'</div>';
  185. echo '<div>';
  186. echo '<a href="'.api_get_self().'?action=editnote&amp;notebook_id='.$row['notebook_id'].'">'.Display::return_icon('edit.gif', get_lang('Edit')).'</a>';
  187. 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>';
  188. echo '</div>';
  189. }
  190. //return $return;
  191. }
  192. }
  193. ?>