WbwSentenceController.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Wbw;
  4. use App\Models\WbwBlock;
  5. use App\Models\Channel;
  6. use App\Models\CourseMember;
  7. use App\Models\Course;
  8. use Illuminate\Http\Request;
  9. use App\Http\Api\AuthApi;
  10. use App\Http\Api\ShareApi;
  11. use App\Http\Api\ChannelApi;
  12. use App\Http\Api\CourseApi;
  13. use App\Models\Sentence;
  14. class WbwSentenceController extends Controller
  15. {
  16. /**
  17. * Display a listing of the resource.
  18. *
  19. * @return \Illuminate\Http\Response
  20. */
  21. public function index(Request $request)
  22. {
  23. //
  24. $channelsId = [];
  25. $result = [];
  26. $user = AuthApi::current($request);
  27. $user_uid = null;
  28. if ($user) {
  29. $user_uid = $user['user_uid'];
  30. }
  31. $sentId = $request->input('book') . '-' .
  32. $request->input('para') . '-' .
  33. $request->input('wordStart') . '-' .
  34. $request->input('wordEnd');
  35. switch ($request->input('view')) {
  36. case 'course-answer':
  37. if ($request->has('course')) {
  38. $channelsId[] = Course::where('id', $request->input('course'))
  39. ->value('channel_id');
  40. }
  41. break;
  42. case 'sent-can-read':
  43. $channels = [];
  44. if ($request->has('course')) {
  45. $channels = CourseApi::getStudentChannels($request->input('course'));
  46. $channels[] = Course::where('id', $request->input('course'))
  47. ->value('channel_id');
  48. } else {
  49. $channels = ChannelApi::getCanReadByUser($user_uid);
  50. }
  51. if ($request->has('exclude')) {
  52. //移除无需查询的channel
  53. foreach ($channels as $key => $id) {
  54. if ($id !== $request->input('exclude')) {
  55. $channelsId[] = $id;
  56. }
  57. }
  58. } else if ($request->has('channels')) {
  59. //仅列出指定的channel
  60. $include = explode(',', $request->input('channels'));
  61. foreach ($channels as $key => $id) {
  62. if (in_array($id, $include)) {
  63. $channelsId[] = $id;
  64. }
  65. }
  66. } else {
  67. $channelsId = $channels;
  68. }
  69. break;
  70. }
  71. $validBlocks = WbwSentenceController::getBlocksByChannels(
  72. $channelsId,
  73. $request->input('book'),
  74. $request->input('para'),
  75. $request->input('wordStart')
  76. );
  77. foreach ($validBlocks as $key => $blockId) {
  78. $channel = WbwBlock::where('uid', $blockId)->first();
  79. $corpus = new CorpusController;
  80. $props = $corpus->getSentTpl(
  81. $sentId,
  82. [$channel->channel_uid],
  83. 'edit',
  84. true,
  85. 'react'
  86. );
  87. $result[] = $props;
  88. }
  89. return $this->ok(['rows' => $result, 'count' => count($result)]);
  90. }
  91. public static function getBlocksByChannels($channelsId, $book, $para, $wordId)
  92. {
  93. $wbwBlocksId = WbwBlock::where('book_id', $book)
  94. ->where('paragraph', $para)
  95. ->whereIn('channel_uid', $channelsId)
  96. ->select('uid')
  97. ->get();
  98. $validBlocks = Wbw::whereIn('block_uid', $wbwBlocksId)
  99. ->where('book_id', $book)
  100. ->where('paragraph', $para)
  101. ->where('wid', $wordId)
  102. ->select('block_uid')
  103. ->groupBy('block_uid')
  104. ->get();
  105. $blocksId = [];
  106. foreach ($validBlocks as $key => $block) {
  107. $blocksId[] = $block->block_uid;
  108. }
  109. return $blocksId;
  110. }
  111. public static function getWbwIdByChannels($channelsId, $book, $para, $wordId)
  112. {
  113. $validBlocks = WbwSentenceController::getBlocksByChannels($channelsId, $book, $para, $wordId);
  114. $wbwId = Wbw::whereIn('block_uid', $validBlocks)
  115. ->where('book_id', $book)
  116. ->where('paragraph', $para)
  117. ->where('wid', $wordId)
  118. ->select('uid')
  119. ->get();
  120. $id = [];
  121. foreach ($wbwId as $key => $value) {
  122. $id[] = $value->uid;
  123. }
  124. return $id;
  125. }
  126. /**
  127. * Store a newly created resource in storage.
  128. *
  129. * @param \Illuminate\Http\Request $request
  130. * @return \Illuminate\Http\Response
  131. */
  132. public function store(Request $request)
  133. {
  134. //
  135. }
  136. /**
  137. * Display the specified resource.
  138. *
  139. * @param \App\Models\Wbw $wbw
  140. * @return \Illuminate\Http\Response
  141. */
  142. public function show(Wbw $wbw)
  143. {
  144. //
  145. }
  146. /**
  147. * Update the specified resource in storage.
  148. *
  149. * @param \Illuminate\Http\Request $request
  150. * @param \App\Models\Wbw $wbw
  151. * @return \Illuminate\Http\Response
  152. */
  153. public function update(Request $request, Wbw $wbw)
  154. {
  155. //
  156. }
  157. /**
  158. * Remove the specified resource from storage.
  159. *
  160. * @param \App\Models\Wbw $wbw
  161. * @return \Illuminate\Http\Response
  162. */
  163. public function destroy(Request $request, string $sentId)
  164. {
  165. //
  166. //鉴权
  167. $user = AuthApi::current($request);
  168. if (!$user) {
  169. //未登录用户
  170. return $this->error(__('auth.failed'), 401, 401);
  171. }
  172. $channelId = $request->input('channel');
  173. if (!ChannelApi::canManageByUser($channelId, $user['user_uid'])) {
  174. return $this->error(__('auth.failed'), 403, 403);
  175. }
  176. $sent = explode('-', $sentId);
  177. $wbwBlockId = WbwBlock::where('book_id', $sent[0])
  178. ->where('paragraph', $sent[1])
  179. ->where('channel_uid', $channelId)
  180. ->value('uid');
  181. $delete = Wbw::where('block_uid', $wbwBlockId)
  182. ->whereBetween('wid', [$sent[2], $sent[3]])
  183. ->delete();
  184. return $this->ok($delete);
  185. }
  186. }