DiscussionCountController.php 7.8 KB

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