| Total Complexity | 40 |
| Total Lines | 321 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like QuestionOptionsEvaluationPlugin often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use QuestionOptionsEvaluationPlugin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class QuestionOptionsEvaluationPlugin extends Plugin |
||
| 11 | { |
||
| 12 | public const SETTING_ENABLE = 'enable'; |
||
| 13 | public const SETTING_MAX_SCORE = 'exercise_max_score'; |
||
| 14 | public const EXTRAFIELD_FORMULA = 'quiz_evaluation_formula'; |
||
| 15 | |||
| 16 | /** Use C2 handler only. Do NOT use "quiz" to avoid warnings. */ |
||
| 17 | private const EF_HANDLER = 'exercise'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * QuestionValuationPlugin constructor. |
||
| 21 | */ |
||
| 22 | protected function __construct() |
||
| 23 | { |
||
| 24 | $version = '1.0'; |
||
| 25 | $author = 'Angel Fernando Quiroz Campos'; |
||
| 26 | |||
| 27 | parent::__construct( |
||
| 28 | $version, |
||
| 29 | $author, |
||
| 30 | [ |
||
| 31 | self::SETTING_ENABLE => 'boolean', |
||
| 32 | self::SETTING_MAX_SCORE => 'text', |
||
| 33 | ] |
||
| 34 | ); |
||
| 35 | } |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @return QuestionOptionsEvaluationPlugin|null |
||
| 39 | */ |
||
| 40 | public static function create() |
||
| 41 | { |
||
| 42 | static $result = null; |
||
| 43 | |||
| 44 | return $result ? $result : $result = new self(); |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param int $exerciseId |
||
| 49 | * @param int $iconSize |
||
| 50 | * |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | public static function filterModify($exerciseId, $iconSize = ICON_SIZE_SMALL) |
||
| 70 | ] |
||
| 71 | ); |
||
| 72 | } |
||
| 73 | |||
| 74 | public function install() |
||
| 75 | { |
||
| 76 | $this->createExtraField(); |
||
| 77 | } |
||
| 78 | |||
| 79 | public function uninstall() |
||
| 80 | { |
||
| 81 | $this->removeExtraField(); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return Plugin |
||
| 86 | */ |
||
| 87 | public function performActionsAfterConfigure() |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Persist the selected formula for a given Exercise. |
||
| 94 | * |
||
| 95 | * @param int $formula |
||
| 96 | * @param Exercise $exercise |
||
| 97 | */ |
||
| 98 | public function saveFormulaForExercise($formula, Exercise $exercise) |
||
| 99 | { |
||
| 100 | $formula = (int) $formula; |
||
| 101 | |||
| 102 | $this->recalculateQuestionScore($formula, $exercise); |
||
| 103 | |||
| 104 | // Write using the C2 handler to avoid "Undefined array key 'quiz'" warnings |
||
| 105 | $extraFieldValue = new ExtraFieldValue(self::EF_HANDLER); |
||
| 106 | $extraFieldValue->save( |
||
| 107 | [ |
||
| 108 | 'item_id' => (int) $exercise->iid, |
||
|
|
|||
| 109 | 'variable' => self::EXTRAFIELD_FORMULA, |
||
| 110 | 'value' => $formula, |
||
| 111 | ] |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Read formula for an Exercise |
||
| 117 | * |
||
| 118 | * @param int $exerciseId |
||
| 119 | * |
||
| 120 | * @return int |
||
| 121 | */ |
||
| 122 | public function getFormulaForExercise($exerciseId) |
||
| 123 | { |
||
| 124 | $efv = new ExtraFieldValue(self::EF_HANDLER); |
||
| 125 | $value = $efv->get_values_by_handler_and_field_variable((int) $exerciseId, self::EXTRAFIELD_FORMULA); |
||
| 126 | |||
| 127 | if (empty($value)) { |
||
| 128 | return 0; |
||
| 129 | } |
||
| 130 | |||
| 131 | return (int) $value['value']; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return int |
||
| 136 | */ |
||
| 137 | public function getMaxScore() |
||
| 138 | { |
||
| 139 | $max = $this->get(self::SETTING_MAX_SCORE); |
||
| 140 | |||
| 141 | if (!empty($max)) { |
||
| 142 | return (int) $max; |
||
| 143 | } |
||
| 144 | |||
| 145 | return 10; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Compute result using negative marking formulas. |
||
| 150 | * |
||
| 151 | * @param int $trackId |
||
| 152 | * @param int $formula |
||
| 153 | * |
||
| 154 | * @throws \Doctrine\ORM\ORMException |
||
| 155 | * @throws \Doctrine\ORM\OptimisticLockException |
||
| 156 | * @throws \Doctrine\ORM\TransactionRequiredException |
||
| 157 | * |
||
| 158 | * @return float|int |
||
| 159 | */ |
||
| 160 | public function getResultWithFormula($trackId, $formula) |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Recalculate question scores according to the selected formula. |
||
| 227 | * |
||
| 228 | * @param int $formula |
||
| 229 | * @param Exercise $exercise |
||
| 230 | */ |
||
| 231 | private function recalculateQuestionScore($formula, Exercise $exercise) |
||
| 232 | { |
||
| 233 | $tblQuestion = Database::get_course_table(TABLE_QUIZ_QUESTION); |
||
| 234 | $tblAnswer = Database::get_course_table(TABLE_QUIZ_ANSWER); |
||
| 235 | |||
| 236 | foreach ($exercise->questionList as $questionId) { |
||
| 237 | $question = Question::read($questionId, $exercise->course, false); |
||
| 238 | if (!in_array($question->selectType(), [UNIQUE_ANSWER, MULTIPLE_ANSWER], true)) { |
||
| 239 | continue; |
||
| 240 | } |
||
| 241 | |||
| 242 | $questionAnswers = new Answer($questionId, $exercise->course_id, $exercise); |
||
| 243 | $counts = array_count_values($questionAnswers->correct); |
||
| 244 | |||
| 245 | // Ensure keys exist to avoid "Undefined index" and division by zero |
||
| 246 | $totalCorrect = (int) ($counts[1] ?? 0); |
||
| 247 | $totalIncorrect = (int) ($counts[0] ?? 0); |
||
| 248 | |||
| 249 | $questionPonderation = 0.0; |
||
| 250 | |||
| 251 | foreach ($questionAnswers->correct as $i => $isCorrect) { |
||
| 252 | if (!isset($questionAnswers->iid[$i])) { |
||
| 253 | continue; |
||
| 254 | } |
||
| 255 | |||
| 256 | $iid = (int) $questionAnswers->iid[$i]; |
||
| 257 | |||
| 258 | if ($question->selectType() === MULTIPLE_ANSWER || 0 === (int) $formula) { |
||
| 259 | // Multiple-answer or formula 0 distributes weights across options. |
||
| 260 | // Use max(1, totalX) to avoid division by zero. |
||
| 261 | $ponderation = (1 == $isCorrect) |
||
| 262 | ? 1 / max(1, $totalCorrect) |
||
| 263 | : -1 / max(1, $totalIncorrect); |
||
| 264 | } else { |
||
| 265 | // Single-answer: correct=1; wrong=-1/formula |
||
| 266 | $ponderation = (1 == $isCorrect) ? 1.0 : -1.0 / (int) $formula; |
||
| 267 | } |
||
| 268 | |||
| 269 | if ($ponderation > 0) { |
||
| 270 | $questionPonderation += $ponderation; |
||
| 271 | } |
||
| 272 | |||
| 273 | Database::query("UPDATE $tblAnswer SET ponderation = ".(float)$ponderation." WHERE iid = $iid"); |
||
| 274 | } |
||
| 275 | |||
| 276 | Database::query( |
||
| 277 | "UPDATE $tblQuestion SET ponderation = ".(float)$questionPonderation." WHERE iid = ".(int)$question->iid |
||
| 278 | ); |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Creates the ExtraField for storing the evaluation formula. |
||
| 284 | * We force integer 0/1 for boolean flags to satisfy strict MySQL. |
||
| 285 | */ |
||
| 286 | private function createExtraField() |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Removes the ExtraField (C2 handler only). |
||
| 323 | */ |
||
| 324 | private function removeExtraField() |
||
| 334 |