DiscussionCountController.php 7.2 KB

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