notebook.lib.php 11 KB

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