1<?php
2/* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Settings for LO courses
6 *
7 * @author Stefan Meyer <smeyer.ilias@gmx.de>
8 * @version $Id$
9 */
10class ilLOUtils
11{
12
13    /**
14     * Check if objective is completed
15     */
16    public static function isCompleted($a_cont_oid, $a_test_rid, $a_objective_id, $max_points, $reached, $limit_perc)
17    {
18        include_once './Modules/Course/classes/Objectives/class.ilLOSettings.php';
19        $settings = ilLOSettings::getInstanceByObjId($a_cont_oid);
20
21        if (self::lookupRandomTest(ilObject::_lookupObjId($a_test_rid))) {
22            if (!$max_points) {
23                return true;
24            } else {
25                return ($reached / $max_points * 100) >= $limit_perc;
26            }
27        } else {
28            $required_perc = self::lookupObjectiveRequiredPercentage($a_cont_oid, $a_objective_id, $a_test_rid, $max_points);
29
30            if (!$max_points) {
31                return true;
32            } else {
33                return ($reached / $max_points * 100) >= $required_perc;
34            }
35        }
36    }
37
38    /**
39     *
40     * @param type $a_container_id
41     * @param type $a_objective_id
42     * @param type $a_test_type
43     */
44    public static function lookupObjectiveRequiredPercentage($a_container_id, $a_objective_id, $a_test_ref_id, $a_max_points)
45    {
46        include_once './Modules/Course/classes/Objectives/class.ilLOSettings.php';
47        $settings = ilLOSettings::getInstanceByObjId($a_container_id);
48
49        include_once './Modules/Course/classes/Objectives/class.ilLOTestAssignments.php';
50        $assignments = ilLOTestAssignments::getInstance($a_container_id);
51        $a_test_type = $assignments->getTypeByTest($a_test_ref_id);
52
53        if ($assignments->isSeparateTest($a_test_ref_id)) {
54            include_once './Services/Object/classes/class.ilObjectFactory.php';
55            $factory = new ilObjectFactory();
56            $tst = $factory->getInstanceByRefId($a_test_ref_id, false);
57            if ($tst instanceof ilObjTest) {
58                $schema = $tst->getMarkSchema();
59                foreach ($schema->getMarkSteps() as $mark) {
60                    if ($mark->getPassed()) {
61                        return (int) $mark->getMinimumLevel();
62                    }
63                }
64            }
65        }
66
67
68
69        $tst_ref_id = $a_test_ref_id;
70        if (self::lookupRandomTest(ilObject::_lookupObjId($tst_ref_id))) {
71            include_once './Modules/Course/classes/Objectives/class.ilLORandomTestQuestionPools.php';
72            return (int) ilLORandomTestQuestionPools::lookupLimit($a_container_id, $a_objective_id, $a_test_type);
73        } else {
74            include_once './Modules/Course/classes/class.ilCourseObjectiveQuestion.php';
75            $limit = ilCourseObjectiveQuestion::loookupTestLimit(ilObject::_lookupObjId($tst_ref_id), $a_objective_id);
76            return $limit;
77        }
78    }
79
80    /**
81     *
82     * @param int $a_container_id
83     * @param int $a_objective_id
84     * @param int $a_ref_id
85     * @return  int $a_passes
86     */
87    public static function lookupMaxAttempts($a_container_id, $a_objective_id, $a_test_ref_id)
88    {
89        global $DIC;
90
91        $ilDB = $DIC['ilDB'];
92
93        include_once './Modules/Course/classes/Objectives/class.ilLOTestAssignments.php';
94        /**
95         * @var ilLOTestAssignments
96         */
97        $assignments = ilLOTestAssignments::getInstance($a_container_id);
98        if (!$assignments->isSeparateTest($a_test_ref_id)) {
99            // no limit of tries for tests assigned to multiple objectives.
100            return 0;
101        }
102
103        $query = 'SELECT nr_of_tries FROM tst_tests ' .
104            'WHERE obj_fi = ' . $ilDB->quote(ilObject::_lookupObjId($a_test_ref_id), 'integer');
105        $res = $ilDB->query($query);
106        while ($row = $res->fetchRow(ilDBConstants::FETCHMODE_OBJECT)) {
107            return (int) $row->nr_of_tries;
108        }
109        return 0;
110    }
111
112
113    /**
114     * Check if test is a random test
115     * @param type $a_test_obj_id
116     * @return bool
117     */
118    public static function lookupRandomTest($a_test_obj_id)
119    {
120        include_once './Modules/Test/classes/class.ilObjTest.php';
121        return ilObjTest::_lookupRandomTest($a_test_obj_id);
122    }
123
124    /**
125     * Lookup assigned qpl name (including taxonomy) by sequence
126     * @param type $a_test_ref_id
127     * @param type $a_sequence_id
128     * @return string
129     */
130    public static function lookupQplBySequence($a_test_ref_id, $a_sequence_id)
131    {
132        if (!$a_sequence_id) {
133            return '';
134        }
135        $tst = ilObjectFactory::getInstanceByRefId($a_test_ref_id, false);
136        if (!$tst instanceof ilObjTest) {
137            return '';
138        }
139        include_once './Modules/Test/classes/class.ilTestRandomQuestionSetSourcePoolDefinitionList.php';
140        include_once './Modules/Test/classes/class.ilTestRandomQuestionSetSourcePoolDefinitionFactory.php';
141        $list = new ilTestRandomQuestionSetSourcePoolDefinitionList(
142            $GLOBALS['DIC']['ilDB'],
143            $tst,
144            new ilTestRandomQuestionSetSourcePoolDefinitionFactory(
145                $GLOBALS['DIC']['ilDB'],
146                $tst
147            )
148        );
149
150        $list->loadDefinitions();
151
152        include_once './Modules/Test/classes/class.ilTestTaxonomyFilterLabelTranslater.php';
153        $translator = new ilTestTaxonomyFilterLabelTranslater($GLOBALS['DIC']['ilDB']);
154        $translator->loadLabels($list);
155
156        $title = '';
157        foreach ($list as $definition) {
158            if ($definition->getId() != $a_sequence_id) {
159                continue;
160            }
161            $title = self::buildQplTitleByDefinition($definition, $translator);
162        }
163        return $title;
164    }
165
166    /**
167     * build title by definition
168     * @param ilTestRandomQuestionSetSourcePoolDefinition $def
169     */
170    protected static function buildQplTitleByDefinition(ilTestRandomQuestionSetSourcePoolDefinition $def, ilTestTaxonomyFilterLabelTranslater $trans)
171    {
172        $title = $def->getPoolTitle();
173        // fau: taxFilter/typeFilter - get title for extended filter conditions
174        $filterTitle = array();
175        $filterTitle[] = $trans->getTaxonomyFilterLabel($def->getMappedTaxonomyFilter());
176        $filterTitle[] = $trans->getTypeFilterLabel($def->getTypeFilter());
177        if (!empty($filterTitle)) {
178            $title .= ' -> ' . implode(' / ', $filterTitle);
179        }
180        #$tax_id = $def->getMappedFilterTaxId();
181        #if($tax_id)
182        #{
183        #	$title .= (' -> '. $trans->getTaxonomyTreeLabel($tax_id));
184        #}
185        #$tax_node = $def->getMappedFilterTaxNodeId();
186        #if($tax_node)
187        #{
188        #	$title .= (' -> ' .$trans->getTaxonomyNodeLabel($tax_node));
189        #}
190        // fau.
191        return $title;
192    }
193
194    public static function hasActiveRun($a_container_id, $a_test_ref_id, $a_objective_id)
195    {
196        return false;
197
198        // check if pass exists
199        include_once './Modules/Test/classes/class.ilObjTest.php';
200        if (
201            !ilObjTest::isParticipantsLastPassActive(
202                $a_test_ref_id,
203                $GLOBALS['DIC']['ilUser']->getId()
204            )
205        ) {
206            return false;
207        }
208
209        // check if multiple pass exists
210        include_once './Modules/Course/classes/Objectives/class.ilLOTestRun.php';
211        $last_objectives = ilLOTestRun::lookupObjectives(
212            $a_container_id,
213            $GLOBALS['DIC']['ilUser']->getId(),
214            ilObject::_lookupObjId($a_test_ref_id)
215        );
216
217        if (count((array) $last_objectives) and in_array((int) $a_objective_id, (array) $last_objectives)) {
218            return true;
219        }
220        return false;
221    }
222
223    public static function getTestResultLinkForUser($a_test_ref_id, $a_user_id)
224    {
225        global $DIC;
226
227        $ilCtrl = $DIC['ilCtrl'];
228        $ilUser = $DIC['ilUser'];
229        $ilAccess = $DIC['ilAccess'];
230
231        if ($ilUser->getId() == ANONYMOUS_USER_ID) {
232            return;
233        }
234
235        $valid = $tutor = false;
236        if ($a_user_id == $ilUser->getId()) {
237            $valid = $ilAccess->checkAccess('read', '', $a_test_ref_id);
238        }
239        if (!$valid) {
240            $valid = $ilAccess->checkAccess('write', '', $a_test_ref_id);
241            $tutor = true;
242        }
243        if ($valid) {
244            $testObjId = ilObject::_lookupObjId($a_test_ref_id);
245            if (!$tutor) {
246                require_once 'Modules/Test/classes/class.ilObjTestAccess.php';
247                if (ilObjTestAccess::visibleUserResultExists($testObjId, $a_user_id)) {
248                    $ilCtrl->setParameterByClass('ilObjTestGUI', 'ref_id', $a_test_ref_id);
249                    $ctrlClasses = array('ilRepositoryGUI', 'ilObjTestGUI', 'ilTestResultsGUI');
250                    $link = $ilCtrl->getLinkTargetByClass($ctrlClasses);
251                    $ilCtrl->setParameterByClass('ilObjTestGUI', 'ref_id', '');
252                    return $link;
253                }
254            } else {
255                include_once 'Modules/Test/classes/class.ilObjTest.php';
256                $testId = ilObjTest::_getTestIDFromObjectID($testObjId);
257                if ($testId) {
258                    $userActiveId = ilObjTest::_getActiveIdOfUser($a_user_id, $testId);
259                    if ($userActiveId) {
260                        $ilCtrl->setParameterByClass('ilTestEvaluationGUI', 'ref_id', $a_test_ref_id);
261                        $ilCtrl->setParameterByClass('ilTestEvaluationGUI', 'active_id', $userActiveId);
262                        $link = $ilCtrl->getLinkTargetByClass(array('ilRepositoryGUI', 'ilObjTestGUI', 'ilTestEvaluationGUI'), 'outParticipantsResultsOverview');
263                        $ilCtrl->setParameterByClass('ilTestEvaluationGUI', 'ref_id', '');
264                        $ilCtrl->setParameterByClass('ilTestEvaluationGUI', 'active_id', '');
265                        return $link;
266                    }
267                }
268            }
269        }
270    }
271}
272