cm_webservice_announcements.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. require_once(dirname(__FILE__).'/../inc/global.inc.php');
  4. $libpath = api_get_path(LIBRARY_PATH);
  5. require_once(dirname(__FILE__).'/cm_webservice.php');
  6. /**
  7. * Description of cm_soap_inbox
  8. *
  9. * @author marcosousa
  10. */
  11. class WSCMAnnouncements extends WSCM
  12. {
  13. public function get_announcements_id($username, $password, $course_code)
  14. {
  15. if($this->verifyUserPass($username, $password) == "valid")
  16. {
  17. $result = self::get_announcements($username, $course_code);
  18. $announcements = "#";
  19. while ($announcement = Database::fetch_array($result)) {
  20. $announcements .= $announcement['id']."#";
  21. }
  22. return $announcements;
  23. } else
  24. return get_lang('InvalidId');
  25. }
  26. public function get_announcement_data($username, $password, $course_code, $announcement_id, $field)
  27. {
  28. if($this->verifyUserPass($username, $password) == "valid")
  29. {
  30. $htmlcode = false;
  31. $user_id = UserManager::get_user_id_from_username($username);
  32. $result = self::get_announcements($username, $course_code, $announcement_id);
  33. while($announcement = Database::fetch_array($result))
  34. {
  35. $announcements[] = $announcement;
  36. }
  37. switch ($field)
  38. {
  39. case 'sender':
  40. $field_table = "insert_user_id";
  41. $sender = api_get_user_info($announcements[0][$field_table]);
  42. $announcements[0][$field_table] = $sender['firstname']." ".$sender['lastname'];
  43. break;
  44. case 'title' :
  45. $htmlcode = true;
  46. $field_table = "title";
  47. break;
  48. case 'date' :
  49. $field_table = "end_date";
  50. break;
  51. case 'content' :
  52. $htmlcode = true;
  53. $field_table = "content";
  54. $announcements[0][$field_table] = nl2br_revert($announcements[0][$field_table]);
  55. break;
  56. default :
  57. $field_table = "title";
  58. }
  59. return (htmlcode) ? html_entity_decode($announcements[0][$field_table]) : $announcements[0][$field_table];
  60. }else
  61. return get_lang('InvalidId');
  62. }
  63. private function get_announcements($username, $course_code, $announcement_id = 0)
  64. {
  65. $session_id = api_get_session_id();
  66. $condition_session = api_get_session_condition($session_id);
  67. $announcement_id = ($announcement_id == 0) ? "" : "AND announcement.id=".$announcement_id;
  68. $user_id = UserManager::get_user_id_from_username($username);
  69. $course_info = CourseManager::get_course_information($course_code);
  70. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY);
  71. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT);
  72. $maximum = '12';
  73. $group_memberships=GroupManager::get_group_ids($course_info['real_id'], $user_id);
  74. if (api_get_group_id() == 0) {
  75. $cond_user_id = " AND (
  76. ip.to_user_id='".$user_id."' OR
  77. ip.to_group_id IN (0, ".implode(", ", $group_memberships).") OR
  78. ip.to_group_id IS NULL
  79. ) ";
  80. } else {
  81. $cond_user_id = " AND (
  82. ip.to_user_id='".$user_id."' OR
  83. ip.to_group_id IN (0, ".api_get_group_id().") OR
  84. ip.to_group_id IS NULL
  85. ) ";
  86. }
  87. // the user is member of several groups => display personal
  88. // announcements AND his group announcements AND the general announcements
  89. if (is_array($group_memberships) && count($group_memberships)>0) {
  90. $sql="SELECT
  91. announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  92. FROM $tbl_announcement announcement, $tbl_item_property ip
  93. WHERE
  94. announcement.id = ip.ref AND
  95. ip.tool='announcement' AND
  96. ip.visibility='1'
  97. $announcement_id
  98. $cond_user_id
  99. $condition_session
  100. GROUP BY ip.ref
  101. ORDER BY display_order DESC
  102. LIMIT 0,$maximum";
  103. } else {
  104. // the user is not member of any group
  105. // this is an identified user => show the general announcements AND his personal announcements
  106. if ($user_id) {
  107. if ((api_get_course_setting('announcement.allow_user_edit_announcement') && !api_is_anonymous())) {
  108. $cond_user_id = " AND (
  109. ip.lastedit_user_id = '".api_get_user_id()."' OR
  110. ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
  111. ) ";
  112. } else {
  113. $cond_user_id = " AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL) ";
  114. }
  115. $sql = "SELECT
  116. announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  117. FROM $tbl_announcement announcement, $tbl_item_property ip
  118. WHERE
  119. announcement.id = ip.ref AND
  120. ip.tool='announcement' AND
  121. ip.visibility='1'
  122. $announcement_id
  123. $cond_user_id
  124. $condition_session
  125. GROUP BY ip.ref
  126. ORDER BY display_order DESC
  127. LIMIT 0,$maximum";
  128. } else {
  129. if (api_get_course_setting('announcement.allow_user_edit_announcement')) {
  130. $cond_user_id = " AND (
  131. ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL
  132. ) ";
  133. } else {
  134. $cond_user_id = " AND ip.to_group_id='0' ";
  135. }
  136. // the user is not identiefied => show only the general announcements
  137. $sql = "SELECT
  138. announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  139. FROM $tbl_announcement announcement, $tbl_item_property ip
  140. WHERE announcement.id = ip.ref
  141. AND ip.tool='announcement'
  142. AND ip.visibility='1'
  143. AND ip.to_group_id='0'
  144. $announcement_id
  145. $condition_session
  146. GROUP BY ip.ref
  147. ORDER BY display_order DESC
  148. LIMIT 0,$maximum";
  149. }
  150. }
  151. $result = Database::query($sql);
  152. return $result;
  153. }
  154. }
  155. /*
  156. echo "aqui: ";
  157. $aqui = new WSCMAnnouncements();
  158. echo "<pre>";
  159. //print_r($aqui->unreadMessage("aluno", "e695f51fe3dd6b7cf2be3188a614f10f"));
  160. print_r($aqui->get_announcement_data("aluno", "c4ca4238a0b923820dcc509a6f75849b", "P0204", "17", "title"));
  161. echo "</pre>";
  162. */