ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilCronFinishUnfinishedTestPasses.php
Go to the documentation of this file.
1<?php
2require_once 'Services/Cron/classes/class.ilCronJob.php';
3require_once 'Services/Cron/classes/class.ilCronJobResult.php';
4require_once 'Modules/Test/classes/class.ilObjTest.php';
5require_once 'Modules/Test/classes/class.ilTestPassFinishTasks.php';
6require_once 'Services/Logging/classes/public/class.ilLoggerFactory.php';
7
8/* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
9
15{
16
20 protected $log;
21
25 protected $lng;
26
30 protected $db;
31
35 protected $obj_data_cache;
36
40 protected $now;
41
43
44 protected $test_ids;
45
47
52
56 function __construct()
57 {
63 global $ilObjDataCache, $lng, $ilDB;
64
65 global $DIC; /* @var ILIAS\DI\Container $DIC */
66
67 $this->log = ilLoggerFactory::getLogger('tst');
68 $this->lng = $lng;
69 $this->lng->loadLanguageModule('assessment');
70 $this->db = $ilDB;
71 $this->obj_data_cache = $ilObjDataCache;
72 $this->now = time();
73 $this->unfinished_passes= array();
74 $this->test_ids = array();
75 $this->test_ending_times= array();
76
77 require_once 'Modules/Test/classes/class.ilTestProcessLockerFactory.php';
78 $this->processLockerFactory = new ilTestProcessLockerFactory(
79 new ilSetting('assessment'), $DIC->database()
80 );
81 }
82
83 public function getId()
84 {
85 return 'finish_unfinished_passes';
86 }
87
88 public function getTitle()
89 {
90 global $lng;
91
92 return $lng->txt("finish_unfinished_passes");
93 }
94
95 public function getDescription()
96 {
97 global $lng;
98
99 return $lng->txt("finish_unfinished_passes_desc");
100 }
101
102 public function getDefaultScheduleType()
103 {
105 }
106
107 public function getDefaultScheduleValue()
108 {
109 return;
110 }
111
112 public function hasAutoActivation()
113 {
114 return false;
115 }
116
117 public function hasFlexibleSchedule()
118 {
119 return true;
120 }
121
122 public function hasCustomSettings()
123 {
124 return true;
125 }
126
127 public function run()
128 {
129 $this->log->info('start inf cronjob...');
130
131 $result = new ilCronJobResult();
132
134 if(count($this->unfinished_passes) > 0)
135 {
136 $this->log->info('found ' . count($this->unfinished_passes). ' unfinished passes starting analyses.');
138 $this->processPasses();
139 }
140 else
141 {
142 $this->log->info('No unfinished passes found.');
143 }
144
146
147 $this->log->info(' ...finishing cronjob.');
148
149 return $result;
150 }
151
153 {
154 $query = "SELECT tst_active.active_id,
155 tst_active.tries,
156 tst_active.user_fi usr_id,
157 tst_active.test_fi test_fi,
158 usr_data.login,
159 usr_data.lastname,
160 usr_data.firstname,
161 tst_active.submitted test_finished,
162 usr_data.matriculation,
163 usr_data.active,
164 tst_active.lastindex,
165 tst_active.last_started_pass last_started
166 FROM tst_active
167 LEFT JOIN usr_data
168 ON tst_active.user_fi = usr_data.usr_id
169 WHERE IFNULL(tst_active.last_finished_pass, -1) <> tst_active.last_started_pass
170 ";
171 $result = $this->db->query($query);
172 while ($row = $this->db->fetchAssoc($result))
173 {
174 $this->unfinished_passes[] = $row;
175 $this->test_ids[] = $row['test_fi'];
176 }
177 }
178
180 {
181 $query = 'SELECT test_id, obj_fi, ending_time, ending_time_enabled, processing_time, enable_processing_time FROM tst_tests WHERE '.
182 $this->db->in('test_id', $this->test_ids, false, 'integer');
183 $result = $this->db->query($query);
184 while ($row = $this->db->fetchAssoc($result))
185 {
186 $this->test_ending_times[$row['test_id']] = $row;
187 }
188 $this->log->info('Gathered data for ' . count($this->test_ids) . ' test id(s) => (' . implode(',', $this->test_ids) .')');
189 }
190
191 protected function processPasses()
192 {
193 $now = time();
194 foreach($this->unfinished_passes as $key => $data)
195 {
196 $test_id = $data['test_fi'];
197 $can_not_be_finished = true;
198 if(array_key_exists($test_id, $this->test_ending_times))
199 {
200 if($this->test_ending_times[$test_id]['ending_time_enabled'] == 1)
201 {
202 $this->log->info('Test (' . $test_id . ') has ending time ('.$this->test_ending_times[$test_id]['ending_time'].')' );
203 $ending_time = $this->test_ending_times[$test_id]['ending_time'];
204 if($ending_time < $now)
205 {
206 $this->finishPassForUser($data['active_id'], $this->test_ending_times[$test_id]['obj_fi']);
207 $can_not_be_finished = false;
208 }
209 else
210 {
211 $this->log->info('Test (' . $test_id . ') ending time ('.$this->test_ending_times[$test_id]['ending_time'].') > now ('.$now.') is not reached.' );
212 }
213
214 }
215 else
216 {
217 $this->log->info('Test (' . $test_id . ') has no ending time.' );
218 }
219 if($this->test_ending_times[$test_id]['enable_processing_time'] == 1)
220 {
221 $this->log->info('Test (' . $test_id . ') has processing time ('.$this->test_ending_times[$test_id]['processing_time'].')' );
222 $obj_id = $this->test_ending_times[$test_id]['obj_fi'];
223 $test_obj = new ilObjTest($obj_id, false);
224 $startingTime = $test_obj->getStartingTimeOfUser($data['active_id'], $data['last_started_pass']);
225 $max_processing_time = $test_obj->isMaxProcessingTimeReached($startingTime, $data['active_id']);
226 if($max_processing_time)
227 {
228 $this->log->info('Max Processing time reached for user id ('.$data['usr_id'].') so test with active id ('.$data['active_id'].') will be finished.');
229 $this->finishPassForUser($data['active_id'], $this->test_ending_times[$test_id]['obj_fi']);
230 $can_not_be_finished = false;
231 }
232 }
233 else
234 {
235 $this->log->info('Test (' . $test_id . ') has no processing time.');
236 }
237
238 if($can_not_be_finished)
239 {
240 $this->log->info('Test session with active id ('.$data['active_id'].') can not be finished by this cron job.');
241 }
242 }
243 }
244 }
245
246 protected function finishPassForUser($active_id, $obj_id)
247 {
248 $this->processLockerFactory->setActiveId($active_id);
249 $processLocker = $this->processLockerFactory->getLocker();
250
251 $pass_finisher = new ilTestPassFinishTasks($active_id, $obj_id);
252 $pass_finisher->performFinishTasks($processLocker);
253
254 $this->log->info('Test session with active id ('.$active_id .') and obj_id (' .$obj_id .') is now finished.');
255 }
256}
$result
An exception for terminatinating execution or to throw for unit testing.
hasCustomSettings()
Has cron job any custom setting which can be edited?
hasAutoActivation()
Is to be activated on "installation".
Cron job result data container.
Cron job application base class.
const SCHEDULE_TYPE_DAILY
static getLogger($a_component_id)
Get component logger.
ILIAS Setting Class.
Class ilTestPassFinishTasks.
global $ilDB
global $DIC