cm_webservice_forum.php 11 KB

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