notebook.lib.php 9.9 KB

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