notebook.lib.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use ChamiloSession as Session;
  4. /**
  5. * This class provides methods for the notebook management.
  6. * Include/require it in your code to use its features.
  7. *
  8. * @author Carlos Vargas <litox84@gmail.com>, move code of main/notebook up here
  9. *
  10. * @package chamilo.library
  11. */
  12. class NotebookManager
  13. {
  14. /**
  15. * Constructor.
  16. */
  17. public function __construct()
  18. {
  19. }
  20. /**
  21. * a little bit of javascript to display a prettier warning when deleting a note.
  22. *
  23. * @return string
  24. *
  25. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  26. *
  27. * @version januari 2009, dokeos 1.8.6
  28. */
  29. public static function javascript_notebook()
  30. {
  31. return "<script>
  32. function confirmation (name)
  33. {
  34. if (confirm(\" ".get_lang("Are you sure you want to delete this note")." \"+ name + \" ?\"))
  35. {return true;}
  36. else
  37. {return false;}
  38. }
  39. </script>";
  40. }
  41. /**
  42. * This functions stores the note in the database.
  43. *
  44. * @param array $values
  45. * @param int $userId Optional. The user ID
  46. * @param int $courseId Optional. The course ID
  47. * @param int $sessionId Optional. The session ID
  48. *
  49. * @return bool
  50. *
  51. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  52. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  53. *
  54. * @version januari 2009, dokeos 1.8.6
  55. */
  56. public static function save_note($values, $userId = 0, $courseId = 0, $sessionId = 0)
  57. {
  58. if (!is_array($values) || empty($values['note_title'])) {
  59. return false;
  60. }
  61. // Database table definition
  62. $table = Database::get_course_table(TABLE_NOTEBOOK);
  63. $userId = $userId ?: api_get_user_id();
  64. $courseId = $courseId ?: api_get_course_int_id();
  65. $courseInfo = api_get_course_info_by_id($courseId);
  66. $courseCode = $courseInfo['code'];
  67. $sessionId = $sessionId ?: api_get_session_id();
  68. $now = api_get_utc_datetime();
  69. $params = [
  70. 'notebook_id' => 0,
  71. 'c_id' => $courseId,
  72. 'user_id' => $userId,
  73. 'course' => $courseCode,
  74. 'session_id' => $sessionId,
  75. 'title' => $values['note_title'],
  76. 'description' => $values['note_comment'],
  77. 'creation_date' => $now,
  78. 'update_date' => $now,
  79. 'status' => 0,
  80. ];
  81. $id = Database::insert($table, $params);
  82. if ($id > 0) {
  83. $sql = "UPDATE $table SET notebook_id = $id WHERE iid = $id";
  84. Database::query($sql);
  85. //insert into item_property
  86. api_item_property_update(
  87. $courseInfo,
  88. TOOL_NOTEBOOK,
  89. $id,
  90. 'NotebookAdded',
  91. $userId
  92. );
  93. return $id;
  94. }
  95. }
  96. /**
  97. * @param int $notebook_id
  98. *
  99. * @return array
  100. */
  101. public static function get_note_information($notebook_id)
  102. {
  103. if (empty($notebook_id)) {
  104. return [];
  105. }
  106. // Database table definition
  107. $table = Database::get_course_table(TABLE_NOTEBOOK);
  108. $course_id = api_get_course_int_id();
  109. $notebook_id = (int) $notebook_id;
  110. $sql = "SELECT
  111. notebook_id AS notebook_id,
  112. title AS note_title,
  113. description AS note_comment,
  114. session_id AS session_id
  115. FROM $table
  116. WHERE c_id = $course_id AND notebook_id = '".$notebook_id."' ";
  117. $result = Database::query($sql);
  118. if (Database::num_rows($result) != 1) {
  119. return [];
  120. }
  121. return Database::fetch_array($result);
  122. }
  123. /**
  124. * This functions updates the note in the database.
  125. *
  126. * @param array $values
  127. *
  128. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  129. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  130. *
  131. * @return bool
  132. *
  133. * @version januari 2009, dokeos 1.8.6
  134. */
  135. public static function update_note($values)
  136. {
  137. if (!is_array($values) || empty($values['note_title'])) {
  138. return false;
  139. }
  140. // Database table definition
  141. $table = Database::get_course_table(TABLE_NOTEBOOK);
  142. $course_id = api_get_course_int_id();
  143. $sessionId = api_get_session_id();
  144. $params = [
  145. 'user_id' => api_get_user_id(),
  146. 'course' => api_get_course_id(),
  147. 'session_id' => $sessionId,
  148. 'title' => $values['note_title'],
  149. 'description' => $values['note_comment'],
  150. 'update_date' => api_get_utc_datetime(),
  151. ];
  152. Database::update(
  153. $table,
  154. $params,
  155. [
  156. 'c_id = ? AND notebook_id = ?' => [
  157. $course_id,
  158. $values['notebook_id'],
  159. ],
  160. ]
  161. );
  162. // update item_property (update)
  163. api_item_property_update(
  164. api_get_course_info(),
  165. TOOL_NOTEBOOK,
  166. $values['notebook_id'],
  167. 'NotebookUpdated',
  168. api_get_user_id()
  169. );
  170. return true;
  171. }
  172. /**
  173. * @param int $notebook_id
  174. *
  175. * @return bool
  176. */
  177. public static function delete_note($notebook_id)
  178. {
  179. $notebook_id = (int) $notebook_id;
  180. if (empty($notebook_id)) {
  181. return false;
  182. }
  183. // Database table definition
  184. $table = Database::get_course_table(TABLE_NOTEBOOK);
  185. $course_id = api_get_course_int_id();
  186. $sql = "DELETE FROM $table
  187. WHERE
  188. c_id = $course_id AND
  189. notebook_id='".$notebook_id."' AND
  190. user_id = '".api_get_user_id()."'";
  191. $result = Database::query($sql);
  192. $affected_rows = Database::affected_rows($result);
  193. if ($affected_rows != 1) {
  194. return false;
  195. }
  196. // Update item_property (delete)
  197. api_item_property_update(
  198. api_get_course_info(),
  199. TOOL_NOTEBOOK,
  200. $notebook_id,
  201. 'delete',
  202. api_get_user_id()
  203. );
  204. return true;
  205. }
  206. /**
  207. * Display notes.
  208. */
  209. public static function display_notes()
  210. {
  211. $sessionId = api_get_session_id();
  212. $_user = api_get_user_info();
  213. if (!isset($_GET['direction'])) {
  214. $sort_direction = 'ASC';
  215. $link_sort_direction = 'DESC';
  216. } elseif ($_GET['direction'] == 'ASC') {
  217. $sort_direction = 'ASC';
  218. $link_sort_direction = 'DESC';
  219. } else {
  220. $sort_direction = 'DESC';
  221. $link_sort_direction = 'ASC';
  222. }
  223. // action links
  224. echo '<div class="actions">';
  225. if (!api_is_anonymous()) {
  226. if ($sessionId == 0 || api_is_allowed_to_session_edit(false, true)) {
  227. echo '<a href="index.php?'.api_get_cidreq().'&action=addnote">'.
  228. Display::return_icon('new_note.png', get_lang('Add new note in my personal notebook'), '', '32').'</a>';
  229. }
  230. }
  231. echo '<a href="index.php?'.api_get_cidreq().'&action=changeview&view=creation_date&direction='.$link_sort_direction.'">'.
  232. Display::return_icon('notes_order_by_date_new.png', get_lang('Sort by date created'), '', '32').'</a>';
  233. echo '<a href="index.php?'.api_get_cidreq().'&action=changeview&view=update_date&direction='.$link_sort_direction.'">'.
  234. Display::return_icon('notes_order_by_date_mod.png', get_lang('Sort by date last modified'), '', '32').'</a>';
  235. echo '<a href="index.php?'.api_get_cidreq().'&action=changeview&view=title&direction='.$link_sort_direction.'">'.
  236. Display::return_icon('notes_order_by_title.png', get_lang('Sort by title'), '', '32').'</a>';
  237. echo '</div>';
  238. $notebookView = Session::read('notebook_view');
  239. if (empty($notebookView)) {
  240. $notebookView = 'creation_date';
  241. }
  242. if (!in_array($notebookView, ['creation_date', 'update_date', 'title'])) {
  243. Session::write('notebook_view', 'creation_date');
  244. }
  245. // Database table definition
  246. $table = Database::get_course_table(TABLE_NOTEBOOK);
  247. $order_by = ' ORDER BY '.$notebookView." $sort_direction ";
  248. // Condition for the session
  249. $condition_session = api_get_session_condition($sessionId);
  250. $cond_extra = $notebookView == 'update_date' ? " AND update_date <> ''" : ' ';
  251. $course_id = api_get_course_int_id();
  252. $sql = "SELECT * FROM $table
  253. WHERE
  254. c_id = $course_id AND
  255. user_id = '".api_get_user_id()."'
  256. $condition_session
  257. $cond_extra $order_by
  258. ";
  259. $result = Database::query($sql);
  260. while ($row = Database::fetch_array($result)) {
  261. // Validation when belongs to a session
  262. $session_img = api_get_session_image($row['session_id'], $_user['status']);
  263. $updateValue = '';
  264. if ($row['update_date'] != $row['creation_date']) {
  265. $updateValue = ', '.get_lang('Updated').': '.Display::dateToStringAgoAndLongDate($row['update_date']);
  266. }
  267. $actions = '<a href="'.api_get_self().'?action=editnote&notebook_id='.$row['notebook_id'].'">'.
  268. Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>';
  269. $actions .= '<a href="'.api_get_self().'?action=deletenote&notebook_id='.$row['notebook_id'].'" onclick="return confirmation(\''.$row['title'].'\');">'.
  270. Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>';
  271. echo Display::panel(
  272. $row['description'],
  273. $row['title'].$session_img.' <div class="pull-right">'.$actions.'</div>',
  274. get_lang('Creation date').': '.Display::dateToStringAgoAndLongDate($row['creation_date']).$updateValue
  275. );
  276. }
  277. }
  278. }