notebook.lib.php 10.0 KB

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