cm_webservice_announcements.php 7.8 KB

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