cm_webservice_forum.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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['db_name']);
  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. $course_db = CourseManager::get_course_information($course_code);
  206. $user_id = UserManager::get_user_id_from_username($username);
  207. $table_threads = Database :: get_course_table(TABLE_FORUM_THREAD, $course_db['db_name']);
  208. $forum_table_attachment = Database :: get_course_table(TABLE_FORUM_ATTACHMENT, $course_db['db_name']);
  209. $table_posts = Database :: get_course_table(TABLE_FORUM_POST, $course_db['db_name']);
  210. $post_date=date('Y-m-d H:i:s');
  211. $visible=1;
  212. $has_attachment=false;
  213. $my_post = '';
  214. $post_notification = '';
  215. $content = nl2br($content);
  216. $title = htmlentities($title);
  217. $content = htmlentities($content);
  218. $sql="INSERT INTO $table_posts (post_title, post_text, thread_id, forum_id, poster_id, post_date, post_notification, post_parent_id, visible)
  219. VALUES ('".Database::escape_string($title)."',
  220. '".Database::escape_string(isset($content) ? (api_html_entity_decode($content)) : null)."',
  221. '".Database::escape_string($thread_id)."',
  222. '".Database::escape_string($forum_id)."',
  223. '".Database::escape_string($user_id)."',
  224. '".Database::escape_string($post_date)."',
  225. '".Database::escape_string(isset($post_notification)?$post_notification:null)."',
  226. '".Database::escape_string(isset($my_post)?$my_post:null)."',
  227. '".Database::escape_string($visible)."')";
  228. $result=Database::query($sql);
  229. return "Post enviado!";
  230. //return $sql;
  231. //send_notification_mails($thread_id, $values);
  232. } else
  233. return get_lang('InvalidId');
  234. }
  235. }
  236. /*
  237. echo "aqui: ";
  238. $aqui = new WSCMForum();
  239. echo "<pre>";
  240. //print_r($aqui->unreadMessage("aluno", "e695f51fe3dd6b7cf2be3188a614f10f"));
  241. //print_r($aqui->get_post_data("aluno", "c4ca4238a0b923820dcc509a6f75849b", "95", "sender_name"));
  242. print_r($aqui->send_post("aluno", "c4ca4238a0b923820dcc509a6f75849b", "P0304", "3", "15", "títle", "conteúdo222222"));
  243. echo "</pre>";
  244. */