notebook.lib.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. if (!is_array($values) or empty($values['note_title'])) {
  45. return false;
  46. }
  47. // Database table definition
  48. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  49. $sql = "INSERT INTO $t_notebook (user_id, course, session_id, title, description, creation_date,update_date,status)
  50. VALUES(
  51. '".api_get_user_id()."',
  52. '".Database::escape_string(api_get_course_id())."',
  53. '".Database::escape_string($_SESSION['id_session'])."',
  54. '".Database::escape_string($values['note_title'])."',
  55. '".Database::escape_string($values['note_comment'])."',
  56. '".Database::escape_string(date('Y-m-d H:i:s'))."',
  57. '".Database::escape_string(date('Y-m-d H:i:s'))."',
  58. '0')";
  59. $result = Database::query($sql);
  60. $id = Database::insert_id();
  61. if ($id > 0) {
  62. //insert into item_property
  63. api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, $id, 'NotebookAdded', api_get_user_id());
  64. }
  65. $affected_rows = Database::affected_rows();
  66. if (!empty($affected_rows)){
  67. return $id;
  68. }
  69. }
  70. function get_note_information($notebook_id) {
  71. if (empty($notebook_id)) { return array(); }
  72. // Database table definition
  73. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  74. $sql = "SELECT notebook_id AS notebook_id,
  75. title AS note_title,
  76. description AS note_comment,
  77. session_id AS session_id
  78. FROM $t_notebook
  79. WHERE notebook_id = '".Database::escape_string($notebook_id)."' ";
  80. $result = Database::query($sql);
  81. if (Database::num_rows($result)!=1) { return array(); }
  82. return Database::fetch_array($result);
  83. }
  84. /**
  85. * This functions updates the note in the database
  86. *
  87. * @param array $values
  88. *
  89. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  90. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  91. * @version januari 2009, dokeos 1.8.6
  92. */
  93. function update_note($values) {
  94. if (!is_array($values) or empty($values['note_title'])) {
  95. return false;
  96. }
  97. // Database table definition
  98. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  99. $sql = "UPDATE $t_notebook SET
  100. user_id = '".api_get_user_id()."',
  101. course = '".Database::escape_string(api_get_course_id())."',
  102. session_id = '".Database::escape_string($_SESSION['id_session'])."',
  103. title = '".Database::escape_string($values['note_title'])."',
  104. description = '".Database::escape_string($values['note_comment'])."',
  105. update_date = '".Database::escape_string(date('Y-m-d H:i:s'))."'
  106. WHERE notebook_id = '".Database::escape_string($values['notebook_id'])."'";
  107. $result = Database::query($sql);
  108. //update item_property (update)
  109. api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, $values['notebook_id'], 'NotebookUpdated', api_get_user_id());
  110. $affected_rows = Database::affected_rows();
  111. if (!empty($affected_rows)){
  112. return true;
  113. }
  114. }
  115. function delete_note($notebook_id) {
  116. if (empty($notebook_id) or $notebook_id != strval(intval($notebook_id))) { return false; }
  117. // Database table definition
  118. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  119. $sql = "DELETE FROM $t_notebook WHERE notebook_id='".intval($notebook_id)."' AND user_id = '".api_get_user_id()."'";
  120. $result = Database::query($sql);
  121. $affected_rows = Database::affected_rows();
  122. if ($affected_rows != 1){
  123. return false;
  124. }
  125. //update item_property (delete)
  126. api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, intval($notebook_id), 'delete', api_get_user_id());
  127. return true;
  128. }
  129. function display_notes() {
  130. global $_user;
  131. if (!$_GET['direction'])
  132. {
  133. $sort_direction = 'ASC';
  134. $link_sort_direction = 'DESC';
  135. }
  136. elseif ($_GET['direction'] == 'ASC')
  137. {
  138. $sort_direction = 'ASC';
  139. $link_sort_direction = 'DESC';
  140. }
  141. else
  142. {
  143. $sort_direction = 'DESC';
  144. $link_sort_direction = 'ASC';
  145. }
  146. // action links
  147. echo '<div class="actions" style="margin-bottom:20px">';
  148. //if (api_is_allowed_to_edit())
  149. //{
  150. if (!api_is_anonymous()) {
  151. if (api_get_session_id()==0)
  152. echo '<a href="index.php?'.api_get_cidreq().'&amp;action=addnote">'.Display::return_icon('new_note.png',get_lang('NoteAddNew'),'','32').'</a>';
  153. elseif(api_is_allowed_to_session_edit(false,true)){
  154. echo '<a href="index.php?'.api_get_cidreq().'&amp;action=addnote">'.Display::return_icon('new_note.png',get_lang('NoteAddNew'),'','32').'</a>';
  155. }
  156. } else {
  157. echo '<a href="javascript:void(0)">'.Display::return_icon('new_note.png',get_lang('NoteAddNew'),'','32').'</a>';
  158. }
  159. //}
  160. echo '<a href="index.php?'.api_get_cidreq().'&amp;action=changeview&amp;view=creation_date&amp;direction='.$link_sort_direction.'">'.Display::return_icon('notes_order_by_date_new.png',get_lang('OrderByCreationDate'),'','32').'</a>';
  161. echo '<a href="index.php?'.api_get_cidreq().'&amp;action=changeview&amp;view=update_date&amp;direction='.$link_sort_direction.'">'.Display::return_icon('notes_order_by_date_mod.png',get_lang('OrderByModificationDate'),'','32').'</a>';
  162. echo '<a href="index.php?'.api_get_cidreq().'&amp;action=changeview&amp;view=title&amp;direction='.$link_sort_direction.'">'.Display::return_icon('notes_order_by_title.png',get_lang('OrderByTitle'),'','32').'</a>';
  163. echo '</div>';
  164. if (!in_array($_SESSION['notebook_view'],array('creation_date','update_date', 'title'))) {
  165. $_SESSION['notebook_view'] = 'creation_date';
  166. }
  167. // Database table definition
  168. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  169. $order_by = "";
  170. if ($_SESSION['notebook_view'] == 'creation_date' || $_SESSION['notebook_view'] == 'update_date') {
  171. $order_by = " ORDER BY ".$_SESSION['notebook_view']." $sort_direction ";
  172. } else {
  173. $order_by = " ORDER BY ".$_SESSION['notebook_view']." $sort_direction ";
  174. }
  175. //condition for the session
  176. $session_id = api_get_session_id();
  177. $condition_session = api_get_session_condition($session_id);
  178. $cond_extra = ($_SESSION['notebook_view']== 'update_date')?" AND update_date <> '0000-00-00 00:00:00'":" ";
  179. $sql = "SELECT * FROM $t_notebook WHERE user_id = '".api_get_user_id()."' $condition_session $cond_extra $order_by";
  180. $result = Database::query($sql);
  181. while ($row = Database::fetch_array($result)) {
  182. //validacion when belongs to a session
  183. $session_img = api_get_session_image($row['session_id'], $_user['status']);
  184. $creation_date = api_get_local_time($row['creation_date'], null, date_default_timezone_get());
  185. $update_date = api_get_local_time($row['update_date'], null, date_default_timezone_get());
  186. echo '<div class="sectiontitle">';
  187. echo '<span style="float: right;"> ('.get_lang('CreationDate').': '.date_to_str_ago($creation_date).'&nbsp;&nbsp;<span class="dropbox_date">'.$creation_date.'</span>';
  188. if ($row['update_date'] <> $row['creation_date']) {
  189. echo ', '.get_lang('UpdateDate').': '.date_to_str_ago($update_date).'&nbsp;&nbsp;<span class="dropbox_date">'.$update_date.'</span>';
  190. }
  191. echo ')</span>';
  192. echo $row['title'] . $session_img;
  193. echo '</div>';
  194. echo '<div class="sectioncomment">'.$row['description'].'</div>';
  195. echo '<div>';
  196. echo '<a href="'.api_get_self().'?action=editnote&amp;notebook_id='.$row['notebook_id'].'">'.Display::return_icon('edit.png', get_lang('Edit'),'',22).'</a>';
  197. echo '<a href="'.api_get_self().'?action=deletenote&amp;notebook_id='.$row['notebook_id'].'" onclick="return confirmation(\''.$row['title'].'\');">'.Display::return_icon('delete.png', get_lang('Delete'),'',22).'</a>';
  198. echo '</div>';
  199. }
  200. //return $return;
  201. }
  202. }
  203. ?>