DiscussionController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Api\ChannelApi;
  4. use App\Http\Api\CourseApi;
  5. use App\Http\Api\MdRender;
  6. use App\Http\Api\Mq;
  7. use App\Http\Api\UserApi;
  8. use App\Http\Resources\DiscussionResource;
  9. use App\Models\Channel;
  10. use App\Models\Discussion;
  11. use App\Models\PaliSentence;
  12. use App\Models\Sentence;
  13. use App\Models\Wbw;
  14. use App\Models\WbwBlock;
  15. use App\Services\AuthService;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Http\Response;
  18. class DiscussionController extends Controller
  19. {
  20. /**
  21. * Display a listing of the resource.
  22. *
  23. * @return Response
  24. */
  25. public function index(Request $request)
  26. {
  27. //
  28. $user = AuthService::current($request);
  29. if ($user) {
  30. $userInfo = UserApi::getByUuid($user['user_uid']);
  31. }
  32. switch ($request->input('view')) {
  33. case 'question-by-topic':
  34. $topic = Discussion::where('id', $request->input('id'));
  35. $topic->where('status', $request->input('status', 'active'))
  36. ->select('res_id')->first();
  37. if (! $topic) {
  38. return $this->error('无效的id');
  39. }
  40. $table = Discussion::where('res_id', $topic->res_id);
  41. $activeNumber = Discussion::where('res_id', $topic->res_id)
  42. ->where('status', 'active')->count();
  43. $closeNumber = Discussion::where('res_id', $topic->res_id)
  44. ->where('status', 'close')->count();
  45. $table->where('status', $request->input('status', 'active'))
  46. ->where('parent', null);
  47. break;
  48. case 'question':
  49. /**
  50. * 禁止:
  51. * 未注册用户看到任何人发表的discussion
  52. * basic用户看到别人在别人channel发表的discussion
  53. */
  54. if (! $user && $request->input('type') === 'discussion') {
  55. return $this->ok([
  56. 'rows' => [],
  57. 'count' => 0,
  58. 'active' => 0,
  59. 'close' => 0,
  60. 'can_create' => false,
  61. 'can_reply' => false,
  62. ]);
  63. }
  64. $resType = $request->input('res_type');
  65. if ($user) {
  66. switch ($resType) {
  67. case 'sentence':
  68. // code...
  69. break;
  70. case 'wbw':
  71. $block_uid = Wbw::where('uid', $request->input('id'))->value('block_uid');
  72. if ($block_uid) {
  73. $channelId = WbwBlock::where('uid', $block_uid)->value('channel_uid');
  74. if ($channelId) {
  75. $canEdit = ChannelApi::userCanEdit($user['user_uid'], $channelId);
  76. }
  77. }
  78. break;
  79. default:
  80. // code...
  81. break;
  82. }
  83. }
  84. $resId = [$request->input('id')];
  85. if (! empty($request->input('course'))) {
  86. //
  87. /**
  88. * 如果res id 是答案,获取学员提问
  89. * 如果是学员
  90. */
  91. // 获取学员提问
  92. // 获取学员channel
  93. if ($request->input('show_student') === 'true') {
  94. $channelsId = CourseApi::getStudentChannels($request->input('course'));
  95. switch ($resType) {
  96. case 'wbw':
  97. // 获取答案单词编号
  98. $wbwWord = Wbw::where('uid', $request->input('id'))
  99. ->first();
  100. $wbwId = WbwSentenceController::getWbwIdByChannels(
  101. $channelsId,
  102. $wbwWord->book_id,
  103. $wbwWord->paragraph,
  104. $wbwWord->wid
  105. );
  106. $resId = array_merge($resId, $wbwId);
  107. break;
  108. case 'sentence':
  109. break;
  110. }
  111. }
  112. }
  113. $table = Discussion::whereIn('res_id', $resId)
  114. ->where('type', $request->input('type', 'discussion'))
  115. ->where('status', $request->input('status', 'active'))
  116. ->where('parent', null);
  117. if ($request->input('type') === 'discussion') {
  118. if (
  119. isset($userInfo) &&
  120. isset($userInfo['roles']) &&
  121. in_array('basic', $userInfo['roles'])
  122. ) {
  123. if (isset($canEdit) && $canEdit === true) {
  124. } else {
  125. $table = $table->where('editor_uid', $userInfo['id']);
  126. }
  127. }
  128. }
  129. $activeNumber = Discussion::whereIn('res_id', $resId)
  130. ->where('parent', null)
  131. ->where('type', $request->input('type', 'discussion'))
  132. ->where('status', 'active')->count();
  133. $closeNumber = Discussion::whereIn('res_id', $resId)
  134. ->where('parent', null)
  135. ->where('type', $request->input('type', 'discussion'))
  136. ->where('status', 'close')->count();
  137. break;
  138. case 'answer':
  139. $table = Discussion::where('parent', $request->input('id'));
  140. $activeNumber = Discussion::where('parent', $request->input('id'))
  141. ->where('status', 'active')->count();
  142. $closeNumber = Discussion::where('parent', $request->input('id'))
  143. ->where('status', 'close')->count();
  144. break;
  145. case 'res_id':
  146. /**
  147. * 先获取顶级节点
  148. * 需要确定用户身份,manager查看全部topic 普通用户只显示自己提交的topic
  149. */
  150. $roots = Discussion::where('res_id', $request->input('id'))
  151. ->where('type', $request->input('type', 'discussion'))
  152. ->whereIn('status', explode(',', $request->input('status', 'active')))
  153. ->where('parent', null)
  154. ->select('id')
  155. ->get();
  156. $table = Discussion::where(function ($query) use ($roots) {
  157. $query->whereIn('id', $roots)
  158. ->orWhereIn('parent', $roots);
  159. });
  160. $activeNumber = Discussion::where('res_id', $request->input('id'))
  161. ->where('type', $request->input('type', 'discussion'))
  162. ->where('status', 'active')->count();
  163. $closeNumber = Discussion::where('res_id', $request->input('id'))
  164. ->where('type', $request->input('type', 'discussion'))
  165. ->where('status', 'close')->count();
  166. break;
  167. case 'topic-by-user':
  168. /**
  169. * 某用户发表的全部topic
  170. */
  171. if (! $user) {
  172. return $this->error('', 403, 403);
  173. }
  174. $table = Discussion::where('editor_uid', $user['user_uid'])
  175. ->where('type', $request->input('type', 'discussion'))
  176. ->whereIn('status', explode(',', $request->input('status', 'active')))
  177. ->where('parent', null);
  178. $activeNumber = Discussion::where('editor_uid', $user['user_uid'])
  179. ->where('parent', null)
  180. ->where('type', $request->input('type', 'discussion'))
  181. ->where('status', 'active')->count();
  182. $closeNumber = Discussion::where('editor_uid', $user['user_uid'])
  183. ->where('parent', null)
  184. ->where('type', $request->input('type', 'discussion'))
  185. ->where('status', 'close')->count();
  186. break;
  187. case 'all':
  188. $table = Discussion::where('parent', null);
  189. $activeNumber = Discussion::where('parent', null)
  190. ->where('status', 'active')->count();
  191. $closeNumber = Discussion::where('parent', null)
  192. ->where('status', 'close')->count();
  193. break;
  194. }
  195. if (! empty($search)) {
  196. $table = $table->where('title', 'like', $search.'%');
  197. }
  198. $count = $table->count();
  199. $table = $table->orderBy($request->input('order', 'created_at'), $request->input('dir', 'desc'));
  200. $table = $table->skip($request->input('offset', 0))
  201. ->take($request->input('limit', 100));
  202. $result = $table->get();
  203. $can_create = false;
  204. $can_reply = false;
  205. $user = AuthService::current($request);
  206. switch ($request->input('type', 'discussion')) {
  207. case 'qa':
  208. switch ($request->input('res_type')) {
  209. case 'article':
  210. if ($user && ArticleController::userCanEditId($user['user_uid'], $request->input('id'))) {
  211. $can_create = true;
  212. $can_reply = true;
  213. }
  214. break;
  215. }
  216. break;
  217. case 'help':
  218. switch ($request->input('res_type')) {
  219. case 'article':
  220. if ($user) {
  221. $can_reply = true;
  222. if (ArticleController::userCanEditId($user['user_uid'], $request->input('id'))) {
  223. $can_create = true;
  224. }
  225. }
  226. break;
  227. }
  228. break;
  229. case 'discussion':
  230. if ($user) {
  231. $can_create = true;
  232. $can_reply = true;
  233. }
  234. break;
  235. }
  236. return $this->ok([
  237. 'rows' => DiscussionResource::collection($result),
  238. 'count' => $count,
  239. 'active' => $activeNumber,
  240. 'close' => $closeNumber,
  241. 'can_create' => $can_create,
  242. 'can_reply' => $can_reply,
  243. ]);
  244. }
  245. public function discussion_tree(Request $request)
  246. {
  247. $output = [];
  248. $sentences = $request->input('data');
  249. foreach ($sentences as $key => $sentence) {
  250. // 先查句子信息
  251. $sentInfo = Sentence::where('book_id', $sentence['book'])
  252. ->where('paragraph', $sentence['paragraph'])
  253. ->where('word_start', $sentence['word_start'])
  254. ->where('word_end', $sentence['word_end'])
  255. ->where('channel_uid', $sentence['channel_id'])
  256. ->first();
  257. if ($sentInfo) {
  258. $sentPr = Discussion::where('res_id', $sentInfo['uid'])
  259. ->whereNull('parent')
  260. ->select('title', 'children_count', 'editor_uid')
  261. ->orderBy('created_at', 'desc')->get();
  262. if (count($sentPr) > 0) {
  263. $output[] = [
  264. 'sentence' => [
  265. 'book' => $sentInfo->book_id,
  266. 'paragraph' => $sentInfo->paragraph,
  267. 'word_start' => $sentInfo->word_start,
  268. 'word_end' => $sentInfo->word_end,
  269. 'channel_id' => $sentInfo->channel_uid,
  270. 'content' => $sentInfo->content,
  271. 'pr_count' => count($sentPr),
  272. ],
  273. 'pr' => $sentPr,
  274. ];
  275. }
  276. }
  277. }
  278. return $this->ok(['rows' => $output, 'count' => count($output)]);
  279. }
  280. /**
  281. * Store a newly created resource in storage.
  282. *
  283. * @return Response
  284. */
  285. public function store(Request $request)
  286. {
  287. $user = AuthService::current($request);
  288. if (! $user) {
  289. return $this->error(__('auth.failed'), [401], 401);
  290. }
  291. //
  292. // validate
  293. // read more on validation at http://laravel.com/docs/validation
  294. if ($request->has('parent')) {
  295. $rules = [];
  296. $parentInfo = Discussion::find($request->input('parent'));
  297. if (! $parentInfo) {
  298. return $this->error('no record');
  299. }
  300. } else {
  301. $rules = [
  302. 'res_id' => 'required',
  303. 'res_type' => 'required',
  304. 'title' => 'required',
  305. ];
  306. }
  307. $validated = $request->validate($rules);
  308. $discussion = new Discussion;
  309. if ($request->has('parent')) {
  310. $discussion->res_id = $parentInfo->res_id;
  311. $discussion->res_type = $parentInfo->res_type;
  312. } else {
  313. $discussion->res_id = $request->input('res_id');
  314. $discussion->res_type = $request->input('res_type');
  315. }
  316. $discussion->type = $request->input('type', 'discussion');
  317. $discussion->tpl_id = $request->input('tpl_id');
  318. $discussion->title = $request->input('title', null);
  319. $discussion->content = $request->input('content', null);
  320. $discussion->content_type = $request->input('content_type', 'markdown');
  321. $discussion->parent = $request->input('parent', null);
  322. $discussion->editor_uid = $user['user_uid'];
  323. $discussion->save();
  324. // 更新parent children_count
  325. if ($request->has('parent')) {
  326. $parentInfo->increment('children_count', 1);
  327. $parentInfo->save();
  328. }
  329. if ($request->input('notification', true)) {
  330. Mq::publish('discussion', new DiscussionResource($discussion));
  331. }
  332. return $this->ok(new DiscussionResource($discussion));
  333. }
  334. /**
  335. * Display the specified resource.
  336. *
  337. * @return Response
  338. */
  339. public function show(Discussion $discussion)
  340. {
  341. //
  342. return $this->ok(new DiscussionResource($discussion));
  343. }
  344. /**
  345. * 获取discussion 锚点的数据。以句子为最小单位,逐词解析也要显示单词所在的句子
  346. *
  347. * @param string $id
  348. * @return Response
  349. */
  350. public function anchor($id)
  351. {
  352. //
  353. $discussion = Discussion::find($id);
  354. $content = '';
  355. switch ($discussion->res_type) {
  356. case 'wbw':
  357. // 从逐词解析表获取逐词解析数据
  358. $wbw = Wbw::where('uid', $discussion->res_id)->first();
  359. if (! $wbw) {
  360. return $this->error('no wbw data');
  361. }
  362. $wbwBlock = WbwBlock::where('uid', $wbw->block_uid)->first();
  363. if (! $wbwBlock) {
  364. return $this->error('no wbwBlock data');
  365. }
  366. $sent = PaliSentence::where('book', $wbw->book_id)
  367. ->where('paragraph', $wbw->paragraph)
  368. ->where('word_begin', '<=', $wbw->wid)
  369. ->where('word_end', '>=', $wbw->wid)
  370. ->first();
  371. if (! $sent) {
  372. return $this->error('no sent data');
  373. }
  374. $sentId = "{$sent['book']}-{$sent['paragraph']}-{$sent['word_begin']}-{$sent['word_end']}";
  375. $channel = $wbwBlock->channel_uid;
  376. $content = MdRender::render('{{'.$sentId.'}}', [$channel]);
  377. break;
  378. default:
  379. // code...
  380. break;
  381. }
  382. return $this->ok($content);
  383. }
  384. /**
  385. * Update the specified resource in storage.
  386. *
  387. * @return Response
  388. */
  389. public function update(Request $request, Discussion $discussion)
  390. {
  391. //
  392. $user = AuthService::current($request);
  393. if (! $user) {
  394. return $this->error(__('auth.failed'), [403], 403);
  395. }
  396. //
  397. $isManager = false;
  398. $isResManager = false;
  399. if ($discussion->editor_uid === $user['user_uid']) {
  400. $isManager = true;
  401. } else {
  402. // 查看是否是资源拥有者
  403. if ($discussion->res_type === 'sentence') {
  404. $res = Sentence::find($discussion->res_id);
  405. if ($res) {
  406. $channelId = $res->channel_uid;
  407. }
  408. } elseif ($discussion->res_type === 'wbw') {
  409. $res = Wbw::where('uid', $discussion->res_id)->first();
  410. if ($res) {
  411. $block = WbwBlock::where('uid', $res->block_uid)->first();
  412. if ($block) {
  413. $channelId = $block->channel_uid;
  414. }
  415. }
  416. }
  417. if (isset($channelId)) {
  418. $channel = Channel::find($channelId);
  419. if ($channel) {
  420. $isResManager = ChannelApi::userCanEdit($user['user_uid'], $channelId);
  421. }
  422. }
  423. }
  424. if (! $isManager && ! $isResManager) {
  425. return $this->error(__('auth.failed'), [403], 403);
  426. }
  427. $discussion->title = $request->input('title', null);
  428. $discussion->content = $request->input('content', null);
  429. $discussion->status = $request->input('status', 'active');
  430. if ($request->has('type')) {
  431. $discussion->type = $request->input('type');
  432. }
  433. // $discussion->editor_uid = $user['user_uid'];
  434. $discussion->save();
  435. return $this->ok(new DiscussionResource($discussion));
  436. }
  437. /**
  438. * Remove the specified resource from storage.
  439. *
  440. * @return Response
  441. */
  442. public function destroy(Request $request, Discussion $discussion)
  443. {
  444. //
  445. $user = AuthService::current($request);
  446. if (! $user) {
  447. return $this->error(__('auth.failed'), [401], 401);
  448. }
  449. // TODO 其他有权限的人也可以删除
  450. if ($discussion->editor_uid !== $user['user_uid']) {
  451. return $this->error(__('auth.failed'), [403], 403);
  452. }
  453. $delete = $discussion->delete();
  454. return $this->ok($delete);
  455. }
  456. }