1<?php
2/* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3require_once 'Modules/Test/classes/class.ilTestSession.php';
4/**
5 * Class ilTestPassFinishTasks
6 * @author Guido Vollbach <gvollbach@databay.de>
7 */
8class ilTestPassFinishTasks
9{
10    protected $testSession;
11
12    protected $obj_id;
13
14    protected $active_id;
15
16    /**
17     * ilTestPassFinishTasks constructor.
18     * @param $active_id
19     * @param $obj_id
20     */
21    public function __construct($active_id, $obj_id)
22    {
23        $this->testSession = new ilTestSession();
24        $this->testSession->loadFromDb($active_id);
25        $this->obj_id = $obj_id;
26        $this->active_id = $active_id;
27    }
28
29    public function performFinishTasks(ilTestProcessLocker $processLocker)
30    {
31        $testSession = $this->testSession;
32
33        $processLocker->executeTestFinishOperation(function () use ($testSession) {
34            if (!$testSession->isSubmitted()) {
35                $testSession->setSubmitted();
36                $testSession->setSubmittedTimestamp();
37                $testSession->saveToDb();
38            }
39
40            $lastStartedPass = (
41                $testSession->getLastStartedPass() === null ? -1 : $testSession->getLastStartedPass()
42            );
43
44            $lastFinishedPass = (
45                $testSession->getLastFinishedPass() === null ? -1 : $testSession->getLastFinishedPass()
46            );
47
48            if ($lastStartedPass > -1 && $lastFinishedPass < $lastStartedPass) {
49                $testSession->setLastFinishedPass($testSession->getPass());
50                $testSession->increaseTestPass(); // saves to db
51            }
52        });
53
54        $this->updateLearningProgressAfterPassFinishedIsWritten();
55    }
56
57    protected function updateLearningProgressAfterPassFinishedIsWritten()
58    {
59        require_once './Modules/Test/classes/class.ilObjTestAccess.php';
60        require_once './Services/Tracking/classes/class.ilLPStatusWrapper.php';
61        ilLPStatusWrapper::_updateStatus(
62            $this->obj_id,
63            ilObjTestAccess::_getParticipantId($this->active_id)
64        );
65
66        $caller = $this->getCaller();
67        $lp = ilLPStatus::_lookupStatus($this->obj_id, $this->testSession->getUserId());
68        $debug = "finPass={$this->testSession->getLastFinishedPass()} / Lp={$lp}";
69
70        ilObjAssessmentFolder::_addLog(
71            $this->testSession->getUserId(),
72            $this->obj_id,
73            "updateLearningProgressAfterPassFinishedIsWritten has been called from {$caller} ({$debug})",
74            true
75        );
76    }
77
78    protected function getCaller()
79    {
80        try {
81            throw new Exception();
82        } catch (Exception $e) {
83            $trace = $e->getTrace();
84        }
85
86        return $trace[3]['class'];
87    }
88}
89