ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilCronFinishUnfinishedTestPasses.php
Go to the documentation of this file.
1 <?php
2 require_once 'Services/Cron/classes/class.ilCronJob.php';
3 require_once 'Services/Cron/classes/class.ilCronJobResult.php';
4 require_once 'Modules/Test/classes/class.ilObjTest.php';
5 require_once 'Modules/Test/classes/class.ilTestPassFinishTasks.php';
6 require_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 
42  protected $unfinished_passes;
43 
44  protected $test_ids;
45 
46  protected $test_ending_times;
47 
52 
56  public 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'),
80  $DIC->database()
81  );
82  }
83 
84  public function getId()
85  {
86  return 'finish_unfinished_passes';
87  }
88 
89  public function getTitle()
90  {
91  global $lng;
92 
93  return $lng->txt("finish_unfinished_passes");
94  }
95 
96  public function getDescription()
97  {
98  global $lng;
99 
100  return $lng->txt("finish_unfinished_passes_desc");
101  }
102 
103  public function getDefaultScheduleType()
104  {
105  return self::SCHEDULE_TYPE_DAILY;
106  }
107 
108  public function getDefaultScheduleValue()
109  {
110  return;
111  }
112 
113  public function hasAutoActivation()
114  {
115  return false;
116  }
117 
118  public function hasFlexibleSchedule()
119  {
120  return true;
121  }
122 
123  public function hasCustomSettings()
124  {
125  return true;
126  }
127 
128  public function run()
129  {
130  $this->log->info('start inf cronjob...');
131 
132  $result = new ilCronJobResult();
133 
135  if (count($this->unfinished_passes) > 0) {
136  $this->log->info('found ' . count($this->unfinished_passes) . ' unfinished passes starting analyses.');
138  $this->processPasses();
139  } else {
140  $this->log->info('No unfinished passes found.');
141  }
142 
144 
145  $this->log->info(' ...finishing cronjob.');
146 
147  return $result;
148  }
149 
150  protected function gatherUsersWithUnfinishedPasses()
151  {
152  $query = "SELECT tst_active.active_id,
153  tst_active.tries,
154  tst_active.user_fi usr_id,
155  tst_active.test_fi test_fi,
156  usr_data.login,
157  usr_data.lastname,
158  usr_data.firstname,
159  tst_active.submitted test_finished,
160  usr_data.matriculation,
161  usr_data.active,
162  tst_active.lastindex,
163  tst_active.last_started_pass last_started
164  FROM tst_active
165  LEFT JOIN usr_data
166  ON tst_active.user_fi = usr_data.usr_id
167  WHERE IFNULL(tst_active.last_finished_pass, -1) <> tst_active.last_started_pass
168  ";
169  $result = $this->db->query($query);
170  while ($row = $this->db->fetchAssoc($result)) {
171  $this->unfinished_passes[] = $row;
172  $this->test_ids[] = $row['test_fi'];
173  }
174  }
175 
176  protected function getTestsFinishAndProcessingTime()
177  {
178  $query = 'SELECT test_id, obj_fi, ending_time, ending_time_enabled, processing_time, enable_processing_time FROM tst_tests WHERE ' .
179  $this->db->in('test_id', $this->test_ids, false, 'integer');
180  $result = $this->db->query($query);
181  while ($row = $this->db->fetchAssoc($result)) {
182  $this->test_ending_times[$row['test_id']] = $row;
183  }
184  $this->log->info('Gathered data for ' . count($this->test_ids) . ' test id(s) => (' . implode(',', $this->test_ids) . ')');
185  }
186 
187  protected function processPasses()
188  {
189  $now = time();
190  foreach ($this->unfinished_passes as $key => $data) {
191  $test_id = $data['test_fi'];
192  $can_not_be_finished = true;
193  if (array_key_exists($test_id, $this->test_ending_times)) {
194  if ($this->test_ending_times[$test_id]['ending_time_enabled'] == 1) {
195  $this->log->info('Test (' . $test_id . ') has ending time (' . $this->test_ending_times[$test_id]['ending_time'] . ')');
196  $ending_time = $this->test_ending_times[$test_id]['ending_time'];
197  if ($ending_time < $now) {
198  $this->finishPassForUser($data['active_id'], $this->test_ending_times[$test_id]['obj_fi']);
199  $can_not_be_finished = false;
200  } else {
201  $this->log->info('Test (' . $test_id . ') ending time (' . $this->test_ending_times[$test_id]['ending_time'] . ') > now (' . $now . ') is not reached.');
202  }
203  } else {
204  $this->log->info('Test (' . $test_id . ') has no ending time.');
205  }
206  if ($this->test_ending_times[$test_id]['enable_processing_time'] == 1) {
207  $this->log->info('Test (' . $test_id . ') has processing time (' . $this->test_ending_times[$test_id]['processing_time'] . ')');
208  $obj_id = $this->test_ending_times[$test_id]['obj_fi'];
209  $test_obj = new ilObjTest($obj_id, false);
210  $startingTime = $test_obj->getStartingTimeOfUser($data['active_id'], $data['last_started_pass']);
211  $max_processing_time = $test_obj->isMaxProcessingTimeReached($startingTime, $data['active_id']);
212  if ($max_processing_time) {
213  $this->log->info('Max Processing time reached for user id (' . $data['usr_id'] . ') so test with active id (' . $data['active_id'] . ') will be finished.');
214  $this->finishPassForUser($data['active_id'], $this->test_ending_times[$test_id]['obj_fi']);
215  $can_not_be_finished = false;
216  }
217  } else {
218  $this->log->info('Test (' . $test_id . ') has no processing time.');
219  }
220 
221  if ($can_not_be_finished) {
222  $this->log->info('Test session with active id (' . $data['active_id'] . ') can not be finished by this cron job.');
223  }
224  }
225  }
226  }
227 
228  protected function finishPassForUser($active_id, $obj_id)
229  {
230  $this->processLockerFactory->setActiveId($active_id);
231  $processLocker = $this->processLockerFactory->getLocker();
232 
233  $pass_finisher = new ilTestPassFinishTasks($active_id, $obj_id);
234  $pass_finisher->performFinishTasks($processLocker);
235 
236  $this->log->info('Test session with active id (' . $active_id . ') and obj_id (' . $obj_id . ') is now finished.');
237  }
238 }
Class ilTestPassFinishTasks.
$result
global $DIC
Definition: saml.php:7
Cron job application base class.
Class ilCronFinishUnfinishedTestPasses.
$query
Create styles array
The data for the language used.
global $ilDB
static getLogger($a_component_id)
Get component logger.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
Cron job result data container.
$key
Definition: croninfo.php:18