cm_webservice_announcements.php 7.6 KB

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