notebook.lib.php 10.0 KB

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