ExerciseController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Course;
  4. use App\Models\CourseMember;
  5. use App\Models\Article;
  6. use App\Models\WbwBlock;
  7. use App\Models\Wbw;
  8. use App\Models\Discussion;
  9. use App\Models\Sentence;
  10. use Illuminate\Http\Request;
  11. use App\Http\Api\MDRender;
  12. use App\Http\Api\UserApi;
  13. class ExerciseController 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. * 列出某个练习所有人的提交情况
  24. * 情况包括
  25. * 1.作业填充百分比
  26. * 2.问题数量
  27. */
  28. $validated = $request->validate([
  29. 'course_id' => 'required',
  30. 'article_id' => 'required',
  31. 'exercise_id' => 'required',
  32. ]);
  33. $output = [];
  34. //课程信息
  35. $course = Course::findOrFail($validated['course_id']);
  36. //查询练习句子编号
  37. $article = Article::where('uid',$validated['article_id'])->value('content');
  38. $wiki = MdRender::markdown2wiki($article);
  39. $xml = MdRender::wiki2xml($wiki);
  40. $html = MdRender::xmlQueryId($xml, $validated['exercise_id']);
  41. $sentences = MdRender::take_sentence($html);
  42. //获取课程答案逐词解析列表
  43. $answerWbw = [];
  44. foreach ($sentences as $sent) {
  45. # code...wbw
  46. $sentId = explode('-',$sent);
  47. if(count($sentId)<4){
  48. break;
  49. }
  50. $courseWb = WbwBlock::where('book_id',$sentId[0])
  51. ->where('paragraph',$sentId[1])
  52. ->where('channel_uid',$course->channel_id)
  53. ->value('uid');
  54. if($courseWb){
  55. $wbwId = Wbw::where('block_uid',$courseWb)
  56. ->whereBetween('wid',[$sentId[2],$sentId[3]])
  57. ->select('uid')->get();
  58. foreach ($wbwId as $id) {
  59. # code...
  60. $answerWbw[] = $id->uid;
  61. }
  62. }
  63. }
  64. $members = CourseMember::where('course_id',$validated['course_id'])
  65. ->where('role','student')
  66. ->select(['user_id','channel_id'])
  67. ->get();
  68. foreach ($members as $member) {
  69. # code...
  70. $data = [
  71. 'user' => UserApi::getByUuid($member->user_id),
  72. 'wbw' => 0,
  73. 'translation' => 0,
  74. 'question' => 0,
  75. 'html' => ""
  76. ];
  77. if(!empty($member->channel_id)){
  78. //
  79. foreach ($sentences as $sent) {
  80. # code...wbw
  81. $sentId = explode('-',$sent);
  82. if(count($sentId)<4){
  83. break;
  84. }
  85. $wb = WbwBlock::where('book_id',$sentId[0])
  86. ->where('paragraph',$sentId[1])
  87. ->where('channel_uid',$member->channel_id)
  88. ->value('uid');
  89. if($wb){
  90. $wbwCount = Wbw::where('block_uid',$wb)
  91. ->whereBetween('wid',[$sentId[2],$sentId[3]])
  92. ->where('status','>',4)
  93. ->count();
  94. $data['wbw'] += $wbwCount;
  95. }
  96. //translation
  97. $sentCount = Sentence::where('book_id',$sentId[0])
  98. ->where('paragraph',$sentId[1])
  99. ->where('word_start',$sentId[2])
  100. ->where('word_end',$sentId[3])
  101. ->where('channel_uid',$member->channel_id)
  102. ->count();
  103. $data['translation'] += $sentCount;
  104. //discussion
  105. //查找答案的wbw 对应的discussion
  106. $discussionCount = Discussion::whereIn('res_id',$answerWbw)
  107. ->where('editor_uid',$member->user_id)
  108. ->whereNull('parent')
  109. ->count();
  110. $data['question'] += $discussionCount;
  111. $tpl = MdRender::xml2tpl($html,$member->channel_id);
  112. $data['html'] .= $tpl;
  113. }
  114. }
  115. $output[] = $data;
  116. }
  117. return $this->ok(["rows"=>$output,"count"=>count($output)]);
  118. }
  119. /**
  120. * Store a newly created resource in storage.
  121. *
  122. * @param \Illuminate\Http\Request $request
  123. * @return \Illuminate\Http\Response
  124. */
  125. public function store(Request $request)
  126. {
  127. //
  128. }
  129. /**
  130. * Display the specified resource.
  131. *
  132. * @param \App\Models\Course $course
  133. * @return \Illuminate\Http\Response
  134. */
  135. public function show(Course $course)
  136. {
  137. //
  138. }
  139. /**
  140. * Update the specified resource in storage.
  141. *
  142. * @param \Illuminate\Http\Request $request
  143. * @param \App\Models\Course $course
  144. * @return \Illuminate\Http\Response
  145. */
  146. public function update(Request $request, Course $course)
  147. {
  148. //
  149. }
  150. /**
  151. * Remove the specified resource from storage.
  152. *
  153. * @param \App\Models\Course $course
  154. * @return \Illuminate\Http\Response
  155. */
  156. public function destroy(Course $course)
  157. {
  158. //
  159. }
  160. }