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