notebook.lib.php 10 KB

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