DiscussionCountController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. if($request->has('course_id')){
  62. //判断我的角色
  63. $my = CourseMember::where('user_id',$user["user_uid"])
  64. ->where('is_current',true)
  65. ->where('course_id',$request->get('course_id'))
  66. ->first();
  67. if(!$my){
  68. return $this->error('auth.failed',403,403);
  69. }
  70. //获取全部成员列表
  71. $allMembers = CourseMember::where('is_current',true)
  72. ->where('course_id',$request->get('course_id'))
  73. ->select('user_id')
  74. ->get();
  75. Log::debug('allMembers',['members'=>$allMembers]);
  76. //找到全部相关channel
  77. $channels = array();
  78. //获取答案channel
  79. $answerChannel = Course::where('id',$request->get('course_id'))
  80. ->value('channel_id');
  81. $exerciseChannels = CourseMember::where('is_current',true)
  82. ->where('course_id',$request->get('course_id'))
  83. ->select('channel_id')
  84. ->get();
  85. if($answerChannel){
  86. array_push($channels,$answerChannel);
  87. }
  88. $users = array();
  89. if($my->role === 'student'){
  90. //自己的channel + 答案
  91. if($my->channel_id){
  92. array_push($channels,$my->channel_id);
  93. }
  94. }else{
  95. //找到全部学员channel + 答案
  96. foreach ($exerciseChannels as $key => $value) {
  97. array_push($channels,$value->channel_id);
  98. }
  99. }
  100. }
  101. //获取全部资源列表
  102. $resId = array();
  103. $querySentId = $request->get('sentences');
  104. //译文
  105. $table = Sentence::select('uid')
  106. ->whereIns(['book_id','paragraph','word_start','word_end'],$querySentId);
  107. if(isset($channels)){
  108. $table = $table->whereIn('channel_uid',$channels);
  109. }
  110. $sentUid = $table->get();
  111. foreach ($sentUid as $key => $value) {
  112. $resId[] = $value->uid;
  113. }
  114. //wbw
  115. $wbwBlockParagraphs = [];
  116. foreach ($querySentId as $key => $value) {
  117. $wbwBlockParagraphs[] = [$value[0],$value[1]];
  118. }
  119. $table = WbwBlock::select('uid')
  120. ->whereIns(['book_id','paragraph'],$wbwBlockParagraphs);
  121. if(isset($channels)){
  122. $table = $table->whereIn('channel_uid',$channels);
  123. }
  124. $wbwBlock = $table->get();
  125. if($wbwBlock){
  126. //找到逐词解析数据
  127. foreach ($querySentId as $key => $value) {
  128. $wbwData = Wbw::whereIn('block_uid',$wbwBlock)
  129. ->whereBetween('wid',[$value[2],$value[3]])
  130. ->select('uid')
  131. ->get();
  132. foreach ($wbwData as $key => $value) {
  133. $resId[] = $value->uid;
  134. }
  135. }
  136. }
  137. Log::debug('res id',['res'=>$resId]);
  138. //全部资源id获取完毕
  139. $table = Discussion::select(['id','res_id','type','editor_uid'])
  140. ->where('status','active')
  141. ->whereNull('parent')
  142. ->whereIn('res_id',$resId);
  143. if(isset($allMembers)){
  144. $table = $table->whereIn('editor_uid',$allMembers);
  145. }
  146. $allDiscussions = $table->get();
  147. $result = DiscussionCountResource::collection($allDiscussions);
  148. Log::debug('response',['data'=>$result]);
  149. return $this->ok($result);
  150. }
  151. /**
  152. * Display the specified resource.
  153. *
  154. * @param string $resId
  155. * @return \Illuminate\Http\Response
  156. */
  157. public function show(string $resId)
  158. {
  159. //
  160. $allDiscussions = Discussion::where('status','active')
  161. ->whereNull('parent')
  162. ->where('res_id',$resId)
  163. ->select(['id','res_id','type','editor_uid'])
  164. ->get();
  165. $result = DiscussionCountResource::collection($allDiscussions);
  166. Log::debug('response',['data'=>$result]);
  167. return $this->ok($result);
  168. }
  169. /**
  170. * Update the specified resource in storage.
  171. *
  172. * @param \Illuminate\Http\Request $request
  173. * @param \App\Models\Discussion $discussion
  174. * @return \Illuminate\Http\Response
  175. */
  176. public function update(Request $request, Discussion $discussion)
  177. {
  178. //
  179. }
  180. /**
  181. * Remove the specified resource from storage.
  182. *
  183. * @param \App\Models\Discussion $discussion
  184. * @return \Illuminate\Http\Response
  185. */
  186. public function destroy(Discussion $discussion)
  187. {
  188. //
  189. }
  190. }