cm_webservice_announcements.php 7.4 KB

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