cm_webservice_forum.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. {
  68. if (isset($thread['thread_id']))
  69. {
  70. $threads_id .= $thread['thread_id']."#";
  71. }
  72. }
  73. return $threads_id;
  74. } else {
  75. return get_lang('InvalidId');
  76. }
  77. }
  78. public function get_forum_thread_data(
  79. $username,
  80. $password,
  81. $course_code,
  82. $thread_id,
  83. $field
  84. ) {
  85. if ($this->verifyUserPass($username, $password) == "valid") {
  86. $course_db = api_get_course_info($course_code);
  87. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_db['db_name']);
  88. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD, $course_db['db_name']);
  89. $sql = "SELECT * FROM ".$table_threads." threads, ".$table_item_property." item_properties
  90. WHERE item_properties.tool='".TOOL_FORUM_THREAD."'
  91. AND item_properties.ref='".Database::escape_string($thread_id)."'
  92. AND threads.thread_id='".Database::escape_string($thread_id)."'";
  93. $result = Database::query($sql);
  94. $thread_info = Database::fetch_array($result);
  95. switch ($field) {
  96. case 'title':
  97. $htmlcode = true;
  98. $field_table = "thread_title";
  99. break;
  100. case 'date':
  101. $field_table = "thread_date";
  102. break;
  103. case 'sender':
  104. $field_table = "insert_user_id";
  105. break;
  106. case 'sender_name':
  107. $user_id = $thread_info['insert_user_id'];
  108. $user_info = api_get_user_info($user_id);
  109. return $user_info['firstname'];
  110. break;
  111. default:
  112. $field_table = "title";
  113. }
  114. return $thread_info[$field_table];
  115. } else {
  116. return get_lang('InvalidId');
  117. }
  118. }
  119. public function get_forum_thread_title(
  120. $username,
  121. $password,
  122. $course_code,
  123. $thread_id
  124. ) {
  125. if ($this->verifyUserPass($username, $password) == "valid") {
  126. $course_db = api_get_course_info($course_code);
  127. $table_item_property = Database::get_course_table(TABLE_ITEM_PROPERTY, $course_db['db_name']);
  128. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD, $course_db['db_name']);
  129. $sql = "SELECT * FROM ".$table_threads." threads, ".$table_item_property." item_properties
  130. WHERE item_properties.tool='".TOOL_FORUM_THREAD."'
  131. AND item_properties.ref='".Database::escape_string($thread_id)."'
  132. AND threads.thread_id='".Database::escape_string($thread_id)."'";
  133. $result = Database::query($sql);
  134. $thread_info = Database::fetch_array($result);
  135. $htmlcode = true;
  136. $field_table = "thread_title";
  137. return $thread_info[$field_table];
  138. } else {
  139. return get_lang('InvalidId');
  140. }
  141. }
  142. public function get_posts_id($username, $password, $course_code, $thread_id)
  143. {
  144. if ($this->verifyUserPass($username, $password) == "valid") {
  145. $course_db = api_get_course_info($course_code);
  146. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  147. $table_posts = Database::get_course_table(TABLE_FORUM_POST, $course_db['db_name']);
  148. // note: change these SQL so that only the relevant fields of the user table are used
  149. if (api_is_allowed_to_edit(null, true)) {
  150. $sql = "SELECT * FROM $table_posts posts
  151. LEFT JOIN $table_users users
  152. ON posts.poster_id=users.user_id
  153. WHERE posts.thread_id='".Database::escape_string($thread_id)."'
  154. ORDER BY posts.post_id ASC";
  155. } else {
  156. // students can only se the posts that are approved (posts.visible='1')
  157. $sql = "SELECT * FROM $table_posts posts
  158. LEFT JOIN $table_users users
  159. ON posts.poster_id=users.user_id
  160. WHERE posts.thread_id='".Database::escape_string($thread_id)."'
  161. AND posts.visible='1'
  162. ORDER BY posts.post_id ASC";
  163. }
  164. $result = Database::query($sql);
  165. while ($row = Database::fetch_array($result)) {
  166. $posts_info[] = $row;
  167. }
  168. $posts_id = '#';
  169. foreach ($posts_info as $post) {
  170. if (isset($post['post_id'])) {
  171. $posts_id .= $post['post_id']."#";
  172. }
  173. }
  174. return $posts_id;
  175. } else {
  176. return get_lang('InvalidId');
  177. }
  178. }
  179. public function get_post_data(
  180. $username,
  181. $password,
  182. $course_code,
  183. $post_id,
  184. $field
  185. ) {
  186. if ($this->verifyUserPass($username, $password) == "valid") {
  187. $course_db = api_get_course_info($course_code);
  188. $table_posts = Database::get_course_table(TABLE_FORUM_POST, $course_db['db_name']);
  189. $table_users = Database::get_main_table(TABLE_MAIN_USER);
  190. $sql = "SELECT * FROM ".$table_posts."posts, ".$table_users." users
  191. WHERE posts.poster_id=users.user_id AND posts.post_id='".Database::escape_string($post_id)."'";
  192. $result = Database::query($sql);
  193. $post_info = Database::fetch_array($result);
  194. $htmlcode = false;
  195. switch ($field) {
  196. case 'title':
  197. $htmlcode = true;
  198. $field_table = "post_title";
  199. break;
  200. case 'text':
  201. $htmlcode = true;
  202. $field_table = "post_text";
  203. break;
  204. case 'date':
  205. $field_table = "post_date";
  206. break;
  207. case 'sender':
  208. $field_table = "user_id";
  209. break;
  210. case 'sender_name':
  211. $field_table = "firstname";
  212. break;
  213. default:
  214. $htmlcode = true;
  215. $field_table = "title";
  216. }
  217. return (htmlcode) ? html_entity_decode($post_info[$field_table]) : $post_info[$field_table];
  218. } else
  219. return get_lang('InvalidId');
  220. }
  221. public function send_post(
  222. $username,
  223. $password,
  224. $course_code,
  225. $forum_id,
  226. $thread_id,
  227. $title,
  228. $content
  229. ) {
  230. if ($this->verifyUserPass($username, $password) == "valid") {
  231. $em = Database::getManager();
  232. $course_db = api_get_course_info($course_code);
  233. $user_id = UserManager::get_user_id_from_username($username);
  234. $table_threads = Database::get_course_table(TABLE_FORUM_THREAD, $course_db['db_name']);
  235. $forum_table_attachment = Database::get_course_table(TABLE_FORUM_ATTACHMENT, $course_db['db_name']);
  236. $table_posts = Database::get_course_table(TABLE_FORUM_POST, $course_db['db_name']);
  237. $post_date = date('Y-m-d H:i:s');
  238. $visible = 1;
  239. $has_attachment = false;
  240. $my_post = '';
  241. $post_notification = '';
  242. $content = nl2br($content);
  243. $title = htmlentities($title);
  244. $content = htmlentities($content);
  245. $postDate = new DateTime(api_get_utc_datetime(), new DateTimeZone('UTC'));
  246. $post = new \Chamilo\CourseBundle\Entity\CForumPost();
  247. $post
  248. ->setPostTitle($title)
  249. ->setPostText(isset($content) ? (api_html_entity_decode($content)) : null)
  250. ->setThreadId($thread_id)
  251. ->setForumId($forum_id)
  252. ->setPosterId($user_id)
  253. ->setPostDate($postDate)
  254. ->setPostNotification(isset($post_notification) ? $post_notification : null)
  255. ->setPostParentId(isset($my_post) ? $my_post : null)
  256. ->setVisible($visible);
  257. $em->persist($post);
  258. $em->flush();
  259. return "Post enviado!";
  260. } else
  261. return get_lang('InvalidId');
  262. }
  263. }
  264. /*
  265. echo "aqui: ";
  266. $aqui = new WSCMForum();
  267. echo "<pre>";
  268. //print_r($aqui->unreadMessage("aluno", "e695f51fe3dd6b7cf2be3188a614f10f"));
  269. //print_r($aqui->get_post_data("aluno", "c4ca4238a0b923820dcc509a6f75849b", "95", "sender_name"));
  270. print_r($aqui->send_post("aluno", "c4ca4238a0b923820dcc509a6f75849b", "P0304", "3", "15", "títle", "conteúdo222222"));
  271. echo "</pre>";
  272. */