cm_webservice_announcements.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. $course_db = $course_info['db_name'];
  71. $tbl_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_db);
  72. $tbl_announcement = Database::get_course_table(TABLE_ANNOUNCEMENT, $course_db);
  73. $maximum = '12';
  74. $group_memberships=GroupManager::get_group_ids($course_info['real_id'], $user_id);
  75. if (api_get_group_id() == 0) {
  76. $cond_user_id = " AND (
  77. ip.to_user_id='".$user_id."' OR
  78. ip.to_group_id IN (0, ".implode(", ", $group_memberships).") OR
  79. ip.to_group_id IS NULL
  80. ) ";
  81. } else {
  82. $cond_user_id = " AND (
  83. ip.to_user_id='".$user_id."' OR
  84. ip.to_group_id IN (0, ".api_get_group_id().") OR
  85. ip.to_group_id IS NULL
  86. ) ";
  87. }
  88. // the user is member of several groups => display personal
  89. // announcements AND his group announcements AND the general announcements
  90. if (is_array($group_memberships) && count($group_memberships)>0) {
  91. $sql="SELECT
  92. announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  93. FROM $tbl_announcement announcement, $tbl_item_property ip
  94. WHERE
  95. announcement.id = ip.ref AND
  96. ip.tool='announcement' AND
  97. ip.visibility='1'
  98. $announcement_id
  99. $cond_user_id
  100. $condition_session
  101. GROUP BY ip.ref
  102. ORDER BY display_order DESC
  103. LIMIT 0,$maximum";
  104. } else {
  105. // the user is not member of any group
  106. // this is an identified user => show the general announcements AND his personal announcements
  107. if ($user_id) {
  108. if ((api_get_course_setting('allow_user_edit_announcement') && !api_is_anonymous())) {
  109. $cond_user_id = " AND (
  110. ip.lastedit_user_id = '".api_get_user_id()."' OR
  111. ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL)
  112. ) ";
  113. } else {
  114. $cond_user_id = " AND ( ip.to_user_id='".$user_id."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL) ";
  115. }
  116. $sql = "SELECT
  117. announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  118. FROM $tbl_announcement announcement, $tbl_item_property ip
  119. WHERE
  120. announcement.id = ip.ref AND
  121. ip.tool='announcement' AND
  122. ip.visibility='1'
  123. $announcement_id
  124. $cond_user_id
  125. $condition_session
  126. GROUP BY ip.ref
  127. ORDER BY display_order DESC
  128. LIMIT 0,$maximum";
  129. } else {
  130. if (api_get_course_setting('allow_user_edit_announcement')) {
  131. $cond_user_id = " AND (
  132. ip.lastedit_user_id = '".api_get_user_id()."' OR ip.to_group_id='0' OR ip.to_group_id IS NULL
  133. ) ";
  134. } else {
  135. $cond_user_id = " AND ip.to_group_id='0' ";
  136. }
  137. // the user is not identiefied => show only the general announcements
  138. $sql = "SELECT
  139. announcement.*, ip.visibility, ip.to_group_id, ip.insert_user_id
  140. FROM $tbl_announcement announcement, $tbl_item_property ip
  141. WHERE announcement.id = ip.ref
  142. AND ip.tool='announcement'
  143. AND ip.visibility='1'
  144. AND ip.to_group_id='0'
  145. $announcement_id
  146. $condition_session
  147. GROUP BY ip.ref
  148. ORDER BY display_order DESC
  149. LIMIT 0,$maximum";
  150. }
  151. }
  152. $result = Database::query($sql);
  153. return $result;
  154. }
  155. }
  156. /*
  157. echo "aqui: ";
  158. $aqui = new WSCMAnnouncements();
  159. echo "<pre>";
  160. //print_r($aqui->unreadMessage("aluno", "e695f51fe3dd6b7cf2be3188a614f10f"));
  161. print_r($aqui->get_announcement_data("aluno", "c4ca4238a0b923820dcc509a6f75849b", "P0204", "17", "title"));
  162. echo "</pre>";
  163. */