cm_webservice_forum.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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__.'/../forum/forumfunction.inc.php';
  8. require_once __DIR__.'/cm_webservice.php';
  9. /**
  10. * Description of cm_soap_inbox.
  11. *
  12. * @author marcosousa
  13. */
  14. class WSCMForum extends WSCM
  15. {
  16. public function get_foruns_id($username, $password, $course_code)
  17. {
  18. if ($this->verifyUserPass($username, $password) == "valid") {
  19. $course_db = api_get_course_info($course_code);
  20. $foruns_info = get_forums($id = '', $course_db['code']);
  21. $foruns_id = '#';
  22. foreach ($foruns_info as $forum) {
  23. if (isset($forum['forum_id'])) {
  24. $foruns_id .= $forum['forum_id']."#";
  25. }
  26. }
  27. return $foruns_id;
  28. } else {
  29. return get_lang('Login failed - incorrect login or password.');
  30. }
  31. }
  32. public function get_forum_title(
  33. $username,
  34. $password,
  35. $course_code,
  36. $forum_id
  37. ) {
  38. if ($this->verifyUserPass($username, $password) == "valid") {
  39. $course_db = api_get_course_info($course_code);
  40. $table_forums = Database::get_course_table(TABLE_FORUM, $course_db['db_name']);
  41. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_db['db_name']);
  42. $sql = "SELECT * FROM ".$table_forums." forums, ".$table_item_property." item_properties
  43. WHERE item_properties.tool='".TOOL_FORUM."'
  44. AND item_properties.ref='".Database::escape_string($forum_id)."'
  45. AND forums.forum_id='".Database::escape_string($forum_id)."'";
  46. $result = Database::query($sql);
  47. $forum_info = Database::fetch_array($result);
  48. $forum_info['approval_direct_post'] = 0; // we can't anymore change this option, so it should always be activated
  49. $forum_title = utf8_decode($forum_info['forum_title']);
  50. return $forum_title;
  51. } else {
  52. return get_lang('Login failed - incorrect login or password.');
  53. }
  54. }
  55. public function get_forum_threads_id(
  56. $username,
  57. $password,
  58. $course_code,
  59. $forum_id
  60. ) {
  61. if ($this->verifyUserPass($username, $password) == "valid") {
  62. $threads_info = get_threads($forum_id);
  63. $threads_id = '#';
  64. foreach ($threads_info as $thread) {
  65. if (isset($thread['thread_id'])) {
  66. $threads_id .= $thread['thread_id']."#";
  67. }
  68. }
  69. return $threads_id;
  70. } else {
  71. return get_lang('Login failed - incorrect login or password.');
  72. }
  73. }
  74. public function get_forum_thread_data(
  75. $username,
  76. $password,
  77. $course_code,
  78. $thread_id,
  79. $field
  80. ) {
  81. if ($this->verifyUserPass($username, $password) == "valid") {
  82. $course_db = api_get_course_info($course_code);
  83. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_db['db_name']);
  84. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD, $course_db['db_name']);
  85. $sql = "SELECT * FROM ".$table_threads." threads, ".$table_item_property." item_properties
  86. WHERE item_properties.tool='".TOOL_FORUM_THREAD."'
  87. AND item_properties.ref='".Database::escape_string($thread_id)."'
  88. AND threads.thread_id='".Database::escape_string($thread_id)."'";
  89. $result = Database::query($sql);
  90. $thread_info = Database::fetch_array($result);
  91. switch ($field) {
  92. case 'title':
  93. $htmlcode = true;
  94. $field_table = "thread_title";
  95. break;
  96. case 'date':
  97. $field_table = "thread_date";
  98. break;
  99. case 'sender':
  100. $field_table = "insert_user_id";
  101. break;
  102. case 'sender_name':
  103. $user_id = $thread_info['insert_user_id'];
  104. $user_info = api_get_user_info($user_id);
  105. return $user_info['firstname'];
  106. break;
  107. default:
  108. $field_table = "title";
  109. }
  110. return $thread_info[$field_table];
  111. } else {
  112. return get_lang('Login failed - incorrect login or password.');
  113. }
  114. }
  115. public function get_forum_thread_title(
  116. $username,
  117. $password,
  118. $course_code,
  119. $thread_id
  120. ) {
  121. if ($this->verifyUserPass($username, $password) == "valid") {
  122. $course_db = api_get_course_info($course_code);
  123. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_db['db_name']);
  124. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD, $course_db['db_name']);
  125. $sql = "SELECT * FROM ".$table_threads." threads, ".$table_item_property." item_properties
  126. WHERE item_properties.tool='".TOOL_FORUM_THREAD."'
  127. AND item_properties.ref='".Database::escape_string($thread_id)."'
  128. AND threads.thread_id='".Database::escape_string($thread_id)."'";
  129. $result = Database::query($sql);
  130. $thread_info = Database::fetch_array($result);
  131. $htmlcode = true;
  132. $field_table = "thread_title";
  133. return $thread_info[$field_table];
  134. } else {
  135. return get_lang('Login failed - incorrect login or password.');
  136. }
  137. }
  138. public function get_posts_id($username, $password, $course_code, $thread_id)
  139. {
  140. if ($this->verifyUserPass($username, $password) == "valid") {
  141. $course_db = api_get_course_info($course_code);
  142. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  143. $table_posts = Database::get_course_table(TABLE_FORUM_POST, $course_db['db_name']);
  144. // note: change these SQL so that only the relevant fields of the user table are used
  145. if (api_is_allowed_to_edit(null, true)) {
  146. $sql = "SELECT * FROM $table_posts posts
  147. LEFT JOIN $table_users users
  148. ON posts.poster_id=users.user_id
  149. WHERE posts.thread_id='".Database::escape_string($thread_id)."'
  150. ORDER BY posts.post_id ASC";
  151. } else {
  152. // students can only se the posts that are approved (posts.visible='1')
  153. $sql = "SELECT * FROM $table_posts posts
  154. LEFT JOIN $table_users users
  155. ON posts.poster_id=users.user_id
  156. WHERE posts.thread_id='".Database::escape_string($thread_id)."'
  157. AND posts.visible='1'
  158. ORDER BY posts.post_id ASC";
  159. }
  160. $result = Database::query($sql);
  161. while ($row = Database::fetch_array($result)) {
  162. $posts_info[] = $row;
  163. }
  164. $posts_id = '#';
  165. foreach ($posts_info as $post) {
  166. if (isset($post['post_id'])) {
  167. $posts_id .= $post['post_id']."#";
  168. }
  169. }
  170. return $posts_id;
  171. } else {
  172. return get_lang('Login failed - incorrect login or password.');
  173. }
  174. }
  175. public function get_post_data(
  176. $username,
  177. $password,
  178. $course_code,
  179. $post_id,
  180. $field
  181. ) {
  182. if ($this->verifyUserPass($username, $password) == "valid") {
  183. $table_posts = Database::get_course_table(TABLE_FORUM_POST);
  184. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  185. $sql = "SELECT * FROM ".$table_posts."posts, ".$table_users." users
  186. WHERE posts.poster_id=users.user_id AND posts.post_id='".Database::escape_string($post_id)."'";
  187. $result = Database::query($sql);
  188. $post_info = Database::fetch_array($result);
  189. $htmlcode = false;
  190. switch ($field) {
  191. case 'title':
  192. $htmlcode = true;
  193. $field_table = "post_title";
  194. break;
  195. case 'text':
  196. $htmlcode = true;
  197. $field_table = "post_text";
  198. break;
  199. case 'date':
  200. $field_table = "post_date";
  201. break;
  202. case 'sender':
  203. $field_table = "user_id";
  204. break;
  205. case 'sender_name':
  206. $field_table = "firstname";
  207. break;
  208. default:
  209. $htmlcode = true;
  210. $field_table = "title";
  211. }
  212. return ($htmlcode) ? html_entity_decode($post_info[$field_table]) : $post_info[$field_table];
  213. } else {
  214. return get_lang('Login failed - incorrect login or password.');
  215. }
  216. }
  217. public function send_post(
  218. $username,
  219. $password,
  220. $course_code,
  221. $forum_id,
  222. $thread_id,
  223. $title,
  224. $content
  225. ) {
  226. if ($this->verifyUserPass($username, $password) == "valid") {
  227. $em = Database::getManager();
  228. $course_db = api_get_course_info($course_code);
  229. $user_id = UserManager::get_user_id_from_username($username);
  230. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD, $course_db['db_name']);
  231. $forum_table_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT, $course_db['db_name']);
  232. $table_posts = Database::get_course_table(TABLE_FORUM_POST, $course_db['db_name']);
  233. $post_date = date('Y-m-d H:i:s');
  234. $visible = 1;
  235. $has_attachment = false;
  236. $my_post = '';
  237. $post_notification = '';
  238. $content = nl2br($content);
  239. $title = htmlentities($title);
  240. $content = htmlentities($content);
  241. $postDate = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC'));
  242. $post = new \Chamilo\CourseBundle\Entity\CForumPost();
  243. $post
  244. ->setPostTitle($title)
  245. ->setPostText(isset($content) ? (api_html_entity_decode($content)) : null)
  246. ->setThread($thread_id)
  247. ->setForumId($forum_id)
  248. ->setPosterId($user_id)
  249. ->setPostDate($postDate)
  250. ->setPostNotification(isset($post_notification) ? $post_notification : null)
  251. ->setPostParentId(isset($my_post) ? $my_post : null)
  252. ->setVisible($visible);
  253. $em->persist($post);
  254. $em->flush();
  255. return "Post enviado!";
  256. } else {
  257. return get_lang('Login failed - incorrect login or password.');
  258. }
  259. }
  260. }
  261. /*
  262. echo "aqui: ";
  263. $aqui = new WSCMForum();
  264. echo "<pre>";
  265. //print_r($aqui->unreadMessage("aluno", "e695f51fe3dd6b7cf2be3188a614f10f"));
  266. //print_r($aqui->get_post_data("aluno", "c4ca4238a0b923820dcc509a6f75849b", "95", "sender_name"));
  267. print_r($aqui->send_post("aluno", "c4ca4238a0b923820dcc509a6f75849b", "P0304", "3", "15", "títle", "conteúdo222222"));
  268. echo "</pre>";
  269. */