WbwSentenceController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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->get('book').'-'.
  32. $request->get('para').'-'.
  33. $request->get('wordStart').'-'.
  34. $request->get('wordEnd');
  35. switch ($request->get('view')) {
  36. case 'course-answer':
  37. if($request->has('course')){
  38. $channelsId[] = Course::where('id',$request->get('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->get('course'));
  46. $channels[] = Course::where('id',$request->get('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->get('exclude')){
  55. $channelsId[] = $id;
  56. }
  57. }
  58. }else if($request->has('channels')){
  59. //仅列出指定的channel
  60. $include = explode(',', $request->get('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->get('book'),
  74. $request->get('para'),
  75. $request->get('wordStart')
  76. );
  77. foreach ($validBlocks as $key => $blockId) {
  78. $channel = WbwBlock::where('uid',$blockId)->first();
  79. $corpus = new CorpusController;
  80. $props = $corpus->getSentTpl($sentId,[$channel->channel_uid],
  81. 'edit',true,
  82. 'react');
  83. $result[] = $props;
  84. }
  85. return $this->ok(['rows'=>$result,'count'=>count($result)]);
  86. }
  87. public static function getBlocksByChannels($channelsId,$book,$para,$wordId){
  88. $wbwBlocksId = WbwBlock::where('book_id',$book)
  89. ->where('paragraph',$para)
  90. ->whereIn('channel_uid',$channelsId)
  91. ->select('uid')
  92. ->get();
  93. $validBlocks = Wbw::whereIn('block_uid',$wbwBlocksId)
  94. ->where('book_id',$book)
  95. ->where('paragraph',$para)
  96. ->where('wid',$wordId)
  97. ->select('block_uid')
  98. ->groupBy('block_uid')
  99. ->get();
  100. $blocksId = [];
  101. foreach ($validBlocks as $key => $block) {
  102. $blocksId[] = $block->block_uid;
  103. }
  104. return $blocksId;
  105. }
  106. public static function getWbwIdByChannels($channelsId,$book,$para,$wordId){
  107. $validBlocks = WbwSentenceController::getBlocksByChannels($channelsId,$book,$para,$wordId);
  108. $wbwId = Wbw::whereIn('block_uid',$validBlocks)
  109. ->where('book_id',$book)
  110. ->where('paragraph',$para)
  111. ->where('wid',$wordId)
  112. ->select('uid')
  113. ->get();
  114. $id = [];
  115. foreach ($wbwId as $key => $value) {
  116. $id[] = $value->uid;
  117. }
  118. return $id;
  119. }
  120. /**
  121. * Store a newly created resource in storage.
  122. *
  123. * @param \Illuminate\Http\Request $request
  124. * @return \Illuminate\Http\Response
  125. */
  126. public function store(Request $request)
  127. {
  128. //
  129. }
  130. /**
  131. * Display the specified resource.
  132. *
  133. * @param \App\Models\Wbw $wbw
  134. * @return \Illuminate\Http\Response
  135. */
  136. public function show(Wbw $wbw)
  137. {
  138. //
  139. }
  140. /**
  141. * Update the specified resource in storage.
  142. *
  143. * @param \Illuminate\Http\Request $request
  144. * @param \App\Models\Wbw $wbw
  145. * @return \Illuminate\Http\Response
  146. */
  147. public function update(Request $request, Wbw $wbw)
  148. {
  149. //
  150. }
  151. /**
  152. * Remove the specified resource from storage.
  153. *
  154. * @param \App\Models\Wbw $wbw
  155. * @return \Illuminate\Http\Response
  156. */
  157. public function destroy(Request $request,string $sentId)
  158. {
  159. //
  160. //鉴权
  161. $user = AuthApi::current($request);
  162. if(!$user ){
  163. //未登录用户
  164. return $this->error(__('auth.failed'),401,401);
  165. }
  166. $channelId = $request->get('channel');
  167. if(!ChannelApi::canManageByUser($channelId,$user['user_uid'])){
  168. return $this->error(__('auth.failed'),403,403);
  169. }
  170. $sent = explode('-',$sentId);
  171. $wbwBlockId = WbwBlock::where('book_id',$sent[0])
  172. ->where('paragraph',$sent[1])
  173. ->where('channel_uid',$channelId)
  174. ->value('uid');
  175. $delete = Wbw::where('block_uid',$wbwBlockId)
  176. ->whereBetween('wid',[$sent[2],$sent[3]])
  177. ->delete();
  178. return $this->ok($delete);
  179. }
  180. }