cm_webservice_forum.php 11 KB

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