notebook.lib.php 11 KB

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