| Conditions | 12 |
| Paths | 18 |
| Total Lines | 90 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 78 | #[Route("/open_answer_grade", name:"chamilo_core_ai_open_answer_grade", methods:["POST"])] |
||
| 79 | public function openAnswerGrade(Request $request): JsonResponse |
||
| 80 | { |
||
| 81 | $exeId = $request->request->getInt('exeId', 0); |
||
| 82 | $questionId = $request->request->getInt('questionId', 0); |
||
| 83 | $courseId = $request->request->getInt('courseId', 0); |
||
| 84 | |||
| 85 | if (0 === $exeId || 0 === $questionId || 0 === $courseId) { |
||
| 86 | return $this->json(['error' => 'Missing parameters'], 400); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** @var TrackEExercise $trackExercise */ |
||
| 90 | $trackExercise = $this->em |
||
| 91 | ->getRepository(TrackEExercise::class) |
||
| 92 | ->find($exeId); |
||
| 93 | if (null === $trackExercise) { |
||
| 94 | return $this->json(['error' => 'Exercise attempt not found'], 404); |
||
| 95 | } |
||
| 96 | |||
| 97 | $attempt = $this->attemptRepo->findOneBy([ |
||
| 98 | 'trackExercise' => $trackExercise, |
||
| 99 | 'questionId' => $questionId, |
||
| 100 | 'user' => $trackExercise->getUser() |
||
| 101 | ]); |
||
| 102 | if (null === $attempt) { |
||
| 103 | return $this->json(['error' => 'Attempt not found'], 404); |
||
| 104 | } |
||
| 105 | |||
| 106 | $answerText = $attempt->getAnswer(); |
||
| 107 | |||
| 108 | if (ctype_digit($answerText)) { |
||
| 109 | $cqa = $this->em |
||
| 110 | ->getRepository(CQuizAnswer::class) |
||
| 111 | ->find((int) $answerText); |
||
| 112 | if ($cqa) { |
||
| 113 | $answerText = $cqa->getAnswer(); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | $courseInfo = api_get_course_info_by_id($courseId); |
||
| 118 | if (empty($courseInfo['real_id'])) { |
||
| 119 | return $this->json(['error' => 'Course info not found'], 500); |
||
| 120 | } |
||
| 121 | |||
| 122 | $question = Question::read($questionId, $courseInfo); |
||
| 123 | if (false === $question) { |
||
| 124 | return $this->json(['error' => 'Question not found'], 404); |
||
| 125 | } |
||
| 126 | |||
| 127 | $language = $courseInfo['language'] ?? 'en'; |
||
| 128 | $courseTitle = $courseInfo['title'] ?? ''; |
||
| 129 | $maxScore = $question->selectWeighting(); |
||
| 130 | $questionText = $question->selectTitle(); |
||
| 131 | $prompt = sprintf( |
||
| 132 | "In language %s, for the question: '%s', in the context of %s, provide a score between 0 and %d on one line, then feedback on the next line for the following answer: '%s'.", |
||
| 133 | $language, $questionText, $courseTitle, $maxScore, $answerText |
||
| 134 | ); |
||
| 135 | |||
| 136 | $raw = trim((string) $this->aiProviderFactory |
||
| 137 | ->getProvider() |
||
| 138 | ->gradeOpenAnswer($prompt, 'open_answer_grade')); |
||
| 139 | |||
| 140 | if ('' === $raw) { |
||
| 141 | return $this->json(['error' => 'AI request failed'], 500); |
||
| 142 | } |
||
| 143 | |||
| 144 | if (false !== strpos($raw, "\n")) { |
||
| 145 | [$scoreLine, $feedback] = explode("\n", $raw, 2); |
||
| 146 | } else { |
||
| 147 | $scoreLine = (string) $maxScore; |
||
| 148 | $feedback = $raw; |
||
| 149 | } |
||
| 150 | |||
| 151 | $score = (int) filter_var($scoreLine, FILTER_SANITIZE_NUMBER_INT); |
||
| 152 | |||
| 153 | $track = new TrackEDefault(); |
||
| 154 | $track |
||
| 155 | ->setDefaultUserId($this->getUser()->getId()) |
||
| 156 | ->setDefaultEventType('ai_use_question_feedback') |
||
| 157 | ->setDefaultValueType('attempt_id') |
||
| 158 | ->setDefaultValue((string)$attempt->getId()) |
||
| 159 | ->setDefaultDate(new \DateTime()) |
||
| 160 | ->setCId($courseId) |
||
| 161 | ->setSessionId(api_get_session_id()); |
||
| 162 | $this->em->persist($track); |
||
| 163 | $this->em->flush(); |
||
| 164 | |||
| 165 | return $this->json([ |
||
| 166 | 'score' => $score, |
||
| 167 | 'feedback' => $feedback |
||
| 168 | ]); |
||
| 171 |