WbwSentenceController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. class WbwSentenceController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return \Illuminate\Http\Response
  19. */
  20. public function index(Request $request)
  21. {
  22. //
  23. switch ($request->get('view')) {
  24. case 'sent-can-read':
  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. $channels = [];
  36. if($request->has('course')){
  37. $channels = CourseApi::getStudentChannels($request->get('course'));
  38. $channels[] = Course::where('id',$request->get('course'))
  39. ->value('channel_id');
  40. }else{
  41. $channels = ChannelApi::getCanReadByUser($user_uid);
  42. }
  43. $channelsId = [];
  44. if($request->has('exclude')){
  45. //移除无需查询的channel
  46. foreach ($channels as $key => $id) {
  47. if($id !== $request->get('exclude')){
  48. $channelsId[] = $id;
  49. }
  50. }
  51. }else if($request->has('channels')){
  52. //仅列出指定的channel
  53. $include = explode(',', $request->get('channels'));
  54. foreach ($channels as $key => $id) {
  55. if(in_array($id, $include)){
  56. $channelsId[] = $id;
  57. }
  58. }
  59. }else{
  60. $channelsId = $channels;
  61. }
  62. $validBlocks = WbwSentenceController::getBlocksByChannels($channelsId,
  63. $request->get('book'),
  64. $request->get('para'),
  65. $request->get('wordStart')
  66. );
  67. foreach ($validBlocks as $key => $blockId) {
  68. $channel = WbwBlock::where('uid',$blockId)->first();
  69. $corpus = new CorpusController;
  70. $props = $corpus->getSentTpl($sentId,[$channel->channel_uid],
  71. 'edit',true,
  72. 'react');
  73. $result[] = $props;
  74. }
  75. return $this->ok(['rows'=>$result,'count'=>count($result)]);
  76. break;
  77. }
  78. }
  79. public static function getBlocksByChannels($channelsId,$book,$para,$wordId){
  80. $wbwBlocksId = WbwBlock::where('book_id',$book)
  81. ->where('paragraph',$para)
  82. ->whereIn('channel_uid',$channelsId)
  83. ->select('uid')
  84. ->get();
  85. $validBlocks = Wbw::whereIn('block_uid',$wbwBlocksId)
  86. ->where('book_id',$book)
  87. ->where('paragraph',$para)
  88. ->where('wid',$wordId)
  89. ->select('block_uid')
  90. ->groupBy('block_uid')
  91. ->get();
  92. $blocksId = [];
  93. foreach ($validBlocks as $key => $block) {
  94. $blocksId[] = $block->block_uid;
  95. }
  96. return $blocksId;
  97. }
  98. public static function getWbwIdByChannels($channelsId,$book,$para,$wordId){
  99. $validBlocks = WbwSentenceController::getBlocksByChannels($channelsId,$book,$para,$wordId);
  100. $wbwId = Wbw::whereIn('block_uid',$validBlocks)
  101. ->where('book_id',$book)
  102. ->where('paragraph',$para)
  103. ->where('wid',$wordId)
  104. ->select('uid')
  105. ->get();
  106. $id = [];
  107. foreach ($wbwId as $key => $value) {
  108. $id[] = $value->uid;
  109. }
  110. return $id;
  111. }
  112. /**
  113. * Store a newly created resource in storage.
  114. *
  115. * @param \Illuminate\Http\Request $request
  116. * @return \Illuminate\Http\Response
  117. */
  118. public function store(Request $request)
  119. {
  120. //
  121. }
  122. /**
  123. * Display the specified resource.
  124. *
  125. * @param \App\Models\Wbw $wbw
  126. * @return \Illuminate\Http\Response
  127. */
  128. public function show(Wbw $wbw)
  129. {
  130. //
  131. }
  132. /**
  133. * Update the specified resource in storage.
  134. *
  135. * @param \Illuminate\Http\Request $request
  136. * @param \App\Models\Wbw $wbw
  137. * @return \Illuminate\Http\Response
  138. */
  139. public function update(Request $request, Wbw $wbw)
  140. {
  141. //
  142. }
  143. /**
  144. * Remove the specified resource from storage.
  145. *
  146. * @param \App\Models\Wbw $wbw
  147. * @return \Illuminate\Http\Response
  148. */
  149. public function destroy(Wbw $wbw)
  150. {
  151. //
  152. }
  153. }