DiscussionCountController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Log;
  5. use App\Models\Discussion;
  6. use App\Models\CourseMember;
  7. use App\Models\Course;
  8. use App\Models\Sentence;
  9. use App\Models\WbwBlock;
  10. use App\Models\Wbw;
  11. use App\Http\Api\AuthApi;
  12. use App\Http\Resources\DiscussionCountResource;
  13. class DiscussionCountController extends Controller
  14. {
  15. /**
  16. * Display a listing of the resource.
  17. *
  18. * @return \Illuminate\Http\Response
  19. */
  20. public function index()
  21. {
  22. //
  23. }
  24. /**
  25. * 课程模式业务逻辑
  26. * 标准答案channel:学生提问
  27. * 学生channel: 老师批改作业
  28. * 老师:
  29. * 标准答案channel: 本期学生,老师(区分已经回复,未回复)
  30. * 学生channel: 本期学生,老师
  31. * 学生:
  32. * 标准答案channel:我自己topic(区分已经回复,未回复)
  33. * 学生自己channel: 我自己,本期老师
  34. *
  35. * 输入:
  36. * 句子列表
  37. * courseId
  38. * 返回数据
  39. * resId
  40. * type:'discussion':
  41. * my:number
  42. * myReplied:number
  43. * all:number
  44. * allReplied:number
  45. * @param \Illuminate\Http\Request $request
  46. * @return \Illuminate\Http\Response
  47. */
  48. public function store(Request $request)
  49. {
  50. /**
  51. * 课程
  52. * 1. 获取用户角色
  53. * 2. 获取成员列表
  54. * 3. 计算答案channel的结果
  55. * 4. 计算作业channel的结果
  56. */
  57. $user = AuthApi::current($request);
  58. if(!$user){
  59. return $this->error('auth.failed',401,401);
  60. }
  61. //判断我的角色
  62. $my = CourseMember::where('user_id',$user["user_uid"])
  63. ->where('is_current',true)
  64. ->where('course_id',$request->get('course_id'))
  65. ->first();
  66. if(!$my){
  67. return $this->error('auth.failed',403,403);
  68. }
  69. //获取全部成员列表
  70. $allMembers = CourseMember::where('is_current',true)
  71. ->where('course_id',$request->get('course_id'))
  72. ->select('user_id')
  73. ->get();
  74. //获取答案channel
  75. $answerChannel = Course::where('id',$request->get('course_id'))
  76. ->value('channel_id');
  77. $exerciseChannels = CourseMember::where('is_current',true)
  78. ->where('course_id',$request->get('course_id'))
  79. ->select('channel_id')
  80. ->get();
  81. $channels = array();
  82. if($answerChannel){
  83. array_push($channels,$answerChannel);
  84. }
  85. $users = array();
  86. if($my->role === 'student'){
  87. //自己的channel + 答案
  88. if($my->channel_id){
  89. array_push($channels,$my->channel_id);
  90. }
  91. }else{
  92. //找到全部学员channel + 答案
  93. foreach ($exerciseChannels as $key => $value) {
  94. array_push($channels,$value->channel_id);
  95. }
  96. }
  97. //获取全部学员对应的资源列表
  98. $querySentId = $request->get('sentences');
  99. $resId = array();
  100. //译文
  101. $sentUid = Sentence::whereIns(['book_id','paragraph','word_start','word_end'],$querySentId)
  102. ->whereIn('channel_uid',$channels)
  103. ->select('uid')->get();
  104. foreach ($sentUid as $key => $value) {
  105. $resId[] = $value->uid;
  106. }
  107. //wbw
  108. $wbwBlockParagraphs = [];
  109. foreach ($querySentId as $key => $value) {
  110. $wbwBlockParagraphs[] = [$value[0],$value[1]];
  111. }
  112. $wbwBlock = WbwBlock::whereIn('channel_uid',$channels)
  113. ->whereIns(['book_id','paragraph'],$wbwBlockParagraphs)
  114. ->select('uid')
  115. ->get();
  116. if($wbwBlock){
  117. //找到逐词解析数据
  118. foreach ($querySentId as $key => $value) {
  119. $wbwData = Wbw::whereIn('block_uid',$wbwBlock)
  120. ->whereBetween('wid',[$value[2],$value[3]])
  121. ->select('uid')
  122. ->get();
  123. foreach ($wbwData as $key => $value) {
  124. $resId[] = $value->uid;
  125. }
  126. }
  127. }
  128. Log::debug('res id',['res'=>$resId,'members'=>$allMembers]);
  129. //全部资源id获取完毕
  130. $allDiscussions = Discussion::where('status','active')
  131. ->whereNull('parent')
  132. ->whereIn('res_id',$resId)
  133. ->whereIn('editor_uid',$allMembers)
  134. ->select(['id','res_id','type','editor_uid'])
  135. ->get();
  136. $result = DiscussionCountResource::collection($allDiscussions);
  137. Log::debug('response',['data'=>$result]);
  138. return $this->ok($result);
  139. }
  140. /**
  141. * Display the specified resource.
  142. *
  143. * @param \App\Models\Discussion $discussion
  144. * @return \Illuminate\Http\Response
  145. */
  146. public function show(Discussion $discussion)
  147. {
  148. //
  149. }
  150. /**
  151. * Update the specified resource in storage.
  152. *
  153. * @param \Illuminate\Http\Request $request
  154. * @param \App\Models\Discussion $discussion
  155. * @return \Illuminate\Http\Response
  156. */
  157. public function update(Request $request, Discussion $discussion)
  158. {
  159. //
  160. }
  161. /**
  162. * Remove the specified resource from storage.
  163. *
  164. * @param \App\Models\Discussion $discussion
  165. * @return \Illuminate\Http\Response
  166. */
  167. public function destroy(Discussion $discussion)
  168. {
  169. //
  170. }
  171. }