2
0

ExerciseController.php 5.1 KB

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