DiscussionController.php 19 KB

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