cm_webservice_announcements.php 7.4 KB

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