notebook.lib.php 8.7 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. {
  11. private function __construct() {
  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. function javascript_notebook()
  22. {
  23. return "<script type=\"text/javascript\">
  24. function confirmation (name)
  25. {
  26. if (confirm(\" ". get_lang("NoteConfirmDelete") ." \"+ name + \" ?\"))
  27. {return true;}
  28. else
  29. {return false;}
  30. }
  31. </script>";
  32. }
  33. /**
  34. * This functions stores the note in the database
  35. *
  36. * @param array $values
  37. * @return bool
  38. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  39. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  40. * @version januari 2009, dokeos 1.8.6
  41. *
  42. */
  43. function save_note($values) {
  44. if (!is_array($values) or empty($values['note_title'])) {
  45. return false;
  46. }
  47. // Database table definition
  48. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  49. $course_id = api_get_course_int_id();
  50. $sql = "INSERT INTO $t_notebook (c_id, user_id, course, session_id, title, description, creation_date,update_date,status)
  51. VALUES(
  52. $course_id,
  53. '".api_get_user_id()."',
  54. '".Database::escape_string(api_get_course_id())."',
  55. '".Database::escape_string($_SESSION['id_session'])."',
  56. '".Database::escape_string($values['note_title'])."',
  57. '".Database::escape_string($values['note_comment'])."',
  58. '".Database::escape_string(date('Y-m-d H:i:s'))."',
  59. '".Database::escape_string(date('Y-m-d H:i:s'))."',
  60. '0')";
  61. $result = Database::query($sql);
  62. $id = Database::insert_id();
  63. if ($id > 0) {
  64. //insert into item_property
  65. api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, $id, 'NotebookAdded', api_get_user_id());
  66. }
  67. $affected_rows = Database::affected_rows();
  68. if (!empty($affected_rows)){
  69. return $id;
  70. }
  71. }
  72. function get_note_information($notebook_id) {
  73. if (empty($notebook_id)) { return array(); }
  74. // Database table definition
  75. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  76. $sql = "SELECT notebook_id AS notebook_id,
  77. title AS note_title,
  78. description AS note_comment,
  79. session_id AS session_id
  80. FROM $t_notebook
  81. WHERE notebook_id = '".Database::escape_string($notebook_id)."' ";
  82. $result = Database::query($sql);
  83. if (Database::num_rows($result)!=1) { return array(); }
  84. return Database::fetch_array($result);
  85. }
  86. /**
  87. * This functions updates the note in the database
  88. *
  89. * @param array $values
  90. *
  91. * @author Christian Fasanando <christian.fasanando@dokeos.com>
  92. * @author Patrick Cool <patrick.cool@ugent.be>, Ghent University, Belgium
  93. * @version januari 2009, dokeos 1.8.6
  94. */
  95. function update_note($values) {
  96. if (!is_array($values) or empty($values['note_title'])) {
  97. return false;
  98. }
  99. // Database table definition
  100. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  101. $sql = "UPDATE $t_notebook SET
  102. user_id = '".api_get_user_id()."',
  103. course = '".Database::escape_string(api_get_course_id())."',
  104. session_id = '".Database::escape_string($_SESSION['id_session'])."',
  105. title = '".Database::escape_string($values['note_title'])."',
  106. description = '".Database::escape_string($values['note_comment'])."',
  107. update_date = '".Database::escape_string(date('Y-m-d H:i:s'))."'
  108. WHERE notebook_id = '".Database::escape_string($values['notebook_id'])."'";
  109. $result = Database::query($sql);
  110. //update item_property (update)
  111. api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, $values['notebook_id'], 'NotebookUpdated', api_get_user_id());
  112. $affected_rows = Database::affected_rows();
  113. if (!empty($affected_rows)){
  114. return true;
  115. }
  116. }
  117. function delete_note($notebook_id) {
  118. if (empty($notebook_id) or $notebook_id != strval(intval($notebook_id))) { return false; }
  119. // Database table definition
  120. $t_notebook = Database :: get_course_table(TABLE_NOTEBOOK);
  121. $sql = "DELETE FROM $t_notebook WHERE notebook_id='".intval($notebook_id)."' AND user_id = '".api_get_user_id()."'";
  122. $result = Database::query($sql);
  123. $affected_rows = Database::affected_rows();
  124. if ($affected_rows != 1){
  125. return false;
  126. }
  127. //update item_property (delete)
  128. api_item_property_update(api_get_course_info(), TOOL_NOTEBOOK, intval($notebook_id), 'delete', api_get_user_id());
  129. return true;
  130. }
  131. function display_notes() {
  132. global $_user;
  133. if (!$_GET['direction'])
  134. {
  135. $sort_direction = 'ASC';
  136. $link_sort_direction = 'DESC';
  137. }
  138. elseif ($_GET['direction'] == 'ASC')
  139. {
  140. $sort_direction = 'ASC';
  141. $link_sort_direction = 'DESC';
  142. }
  143. else
  144. {
  145. $sort_direction = 'DESC';
  146. $link_sort_direction = 'ASC';
  147. }
  148. // action links
  149. echo '<div class="actions" style="margin-bottom:20px">';
  150. //if (api_is_allowed_to_edit())
  151. //{
  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. //}
  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'),'',22).'</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'),'',22).'</a>';
  201. echo '</div>';
  202. }
  203. //return $return;
  204. }
  205. }
  206. ?>