85 خطوط
3.8 KiB
PHP
85 خطوط
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Evidence\Application;
|
|
|
|
use App\Modules\Assessments\Domain\QuestionResult;
|
|
use App\Modules\Evidence\Domain\Enums\EvidenceType;
|
|
use App\Modules\Evidence\Domain\Events\AssessmentEvidenceCreated;
|
|
use App\Modules\Evidence\Domain\EvidenceRecord;
|
|
use App\Modules\Taxonomy\Domain\ContentTaxonomyMapping;
|
|
use App\Modules\Taxonomy\Domain\Enums\MappableType;
|
|
use App\Modules\Taxonomy\Domain\Enums\MappingConfirmationStatus;
|
|
use App\Modules\Taxonomy\Domain\Enums\MappingType;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
final class EvidenceGenerationService
|
|
{
|
|
/** @return Collection<int, EvidenceRecord> */
|
|
public function fromQuestionResult(QuestionResult $result, EvidenceType $evidenceType = EvidenceType::Assessment): Collection
|
|
{
|
|
$result->loadMissing('attempt', 'question');
|
|
$attempt = $result->attempt;
|
|
$question = $result->question;
|
|
|
|
if (! $attempt || ! $question
|
|
|| $attempt->organization_id !== $result->organization_id
|
|
|| $question->organization_id !== $result->organization_id
|
|
|| $question->course_version_id !== $attempt->course_version_id) {
|
|
throw ValidationException::withMessages(['questionResult' => ['The question result context is inconsistent.']]);
|
|
}
|
|
|
|
$mappings = ContentTaxonomyMapping::query()
|
|
->where('organization_id', $result->organization_id)
|
|
->where('course_version_id', $attempt->course_version_id)
|
|
->where('mappable_type', MappableType::Question)
|
|
->where('mappable_id', $question->getKey())
|
|
->where('mapping_type', MappingType::Assesses)
|
|
->where('confirmation_status', MappingConfirmationStatus::Confirmed)
|
|
->get();
|
|
|
|
$strength = (float) config('capability.evidence_strength.'.$evidenceType->value);
|
|
|
|
return DB::transaction(function () use ($mappings, $result, $attempt, $evidenceType, $strength) {
|
|
return $mappings->map(function (ContentTaxonomyMapping $mapping) use ($result, $attempt, $evidenceType, $strength) {
|
|
$evidence = EvidenceRecord::query()->firstOrCreate(
|
|
[
|
|
'source_type' => 'question_result',
|
|
'source_id' => $result->getKey(),
|
|
'taxonomy_node_id' => $mapping->taxonomy_node_id,
|
|
'content_mapping_id' => $mapping->getKey(),
|
|
],
|
|
[
|
|
'organization_id' => $result->organization_id,
|
|
'learner_id' => $attempt->learner_id,
|
|
'evidence_type' => $evidenceType,
|
|
'raw_value' => $result->raw_value,
|
|
'normalized_value' => $result->normalized_value,
|
|
'strength' => $strength,
|
|
'mapping_weight' => $mapping->weight,
|
|
'occurred_at' => $result->occurred_at,
|
|
'course_version_id' => $attempt->course_version_id,
|
|
'metadata' => [
|
|
'assessmentId' => $attempt->assessment_id,
|
|
'questionId' => $result->question_id,
|
|
'attemptNumber' => $attempt->attempt_number,
|
|
],
|
|
],
|
|
);
|
|
|
|
if ($evidence->wasRecentlyCreated) {
|
|
AssessmentEvidenceCreated::dispatch(
|
|
$evidence->organization_id,
|
|
$evidence->learner_id,
|
|
$evidence->getKey(),
|
|
$evidence->taxonomy_node_id,
|
|
);
|
|
}
|
|
|
|
return $evidence;
|
|
});
|
|
});
|
|
}
|
|
}
|