notebook.lib.php 10 KB

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