cm_webservice_forum.php 11 KB

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