ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.ilWorkflowDbHelper.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2016 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
18 {
19  const DB_MODE_CREATE = 0;
20  const DB_MODE_UPDATE = 1;
21 
29  public static function writeWorkflow(ilWorkflow $workflow)
30  {
31  global $DIC;
33  $ilDB = $DIC['ilDB'];
34 
35  $require_data_persistance = $workflow->isDataPersistenceRequired();
37 
38  if ($workflow->hasDbId()) {
39  $wf_id = $workflow->getDbId();
40  $mode = self::DB_MODE_UPDATE;
41  } else {
42  $wf_id = $ilDB->nextId('wfe_workflows');
43  $workflow->setDbId($wf_id);
44  $mode = self::DB_MODE_CREATE;
45  }
46 
47  $wf_data = $workflow->getWorkflowData();
48  $wf_subject = $workflow->getWorkflowSubject();
49  $wf_context = $workflow->getWorkflowContext();
50  $active = $workflow->isActive();
51  $instance = serialize($workflow);
52 
53  if ($mode == self::DB_MODE_UPDATE) {
54  $ilDB->update(
55  'wfe_workflows',
56  array(
57  'workflow_type' => array('text', $wf_data['type'] ),
58  'workflow_content' => array('text', $wf_data['content']),
59  'workflow_class' => array('text', $workflow->getWorkflowClass()),
60  'workflow_location' => array('text', $workflow->getWorkflowLocation()),
61  'subject_type' => array('text', $wf_subject['type']),
62  'subject_id' => array('integer', $wf_subject['identifier']),
63  'context_type' => array('text', $wf_context['type']),
64  'context_id' => array('integer', $wf_context['identifier']),
65  'workflow_instance' => array('clob', $instance),
66  'active' => array('integer', (int) $active)
67  ),
68  array(
69  'workflow_id' => array('integer', $wf_id)
70  )
71  );
72  }
73 
74  if ($mode == self::DB_MODE_CREATE) {
75  $ilDB->insert(
76  'wfe_workflows',
77  array(
78  'workflow_id' => array('integer', $wf_id),
79  'workflow_type' => array('text', $wf_data['type'] ),
80  'workflow_class' => array('text', $workflow->getWorkflowClass()),
81  'workflow_location' => array('text', $workflow->getWorkflowLocation()),
82  'workflow_content' => array('text', $wf_data['content']),
83  'subject_type' => array('text', $wf_subject['type']),
84  'subject_id' => array('integer', $wf_subject['identifier']),
85  'context_type' => array('text', $wf_context['type']),
86  'context_id' => array('integer', $wf_context['identifier']),
87  'workflow_instance' => array('clob', $instance),
88  'active' => array('integer', (int) $active)
89  )
90  );
91  }
92 
93  if ($require_data_persistance) {
94  self::persistWorkflowIOData($workflow);
95  }
96  }
97 
101  public static function persistWorkflowIOData(ilWorkflow $workflow)
102  {
103  global $DIC;
105  $ilDB = $DIC['ilDB'];
106 
107  $workflow_id = $workflow->getId();
108 
109  $input_data = $workflow->getInputVars();
110  foreach ($input_data as $name => $value) {
111  $ilDB->replace(
112  'wfe_io_inputs',
113  array('workflow_id' => $workflow_id, 'name' => $name),
114  array('value' => $value)
115  );
116  }
117 
118  $output_data = $workflow->getOutputVars();
119  foreach ($output_data as $name => $value) {
120  $ilDB->replace(
121  'wfe_io_outputs',
122  array('workflow_id' => $workflow_id, 'name' => $name),
123  array('value' => $value)
124  );
125  }
126  }
127 
136  public static function deleteWorkflow(ilWorkflow $a_workflow)
137  {
138  global $DIC;
140  $ilDB = $DIC['ilDB'];
141 
142  if ($a_workflow->hasDbId()) {
143  $ilDB->manipulate(
144  'DELETE
145  FROM wfe_workflows
146  WHERE workflow_id = ' . $ilDB->quote($a_workflow->getDbId(), 'integer')
147  );
148 
149  // This should not be necessary, actually. Still this call makes sure
150  // that there won't be orphan records polluting the database.
151  $ilDB->manipulate(
152  'DELETE
153  FROM wfe_det_listening
154  WHERE workflow_id = ' . $ilDB->quote($a_workflow->getDbId(), 'integer')
155  );
156 
157  $ilDB->manipulate(
158  'DELETE
159  FROM wfe_io_inputs
160  WHERE workflow_id = ' . $ilDB->quote($a_workflow->getDbId(), 'integer')
161  );
162 
163  $ilDB->manipulate(
164  'DELETE
165  FROM wfe_io_outputs
166  WHERE workflow_id = ' . $ilDB->quote($a_workflow->getDbId(), 'integer')
167  );
168  } else {
169  return;
170  }
171  }
172 
180  public static function writeDetector(ilDetector $a_detector)
181  {
182  global $DIC;
184  $ilDB = $DIC['ilDB'];
185 
186  if ($a_detector->hasDbId()) {
187  $det_id = $a_detector->getDbId();
188  $mode = self::DB_MODE_UPDATE;
189  } else {
190  $det_id = $ilDB->nextId('wfe_det_listening');
191  $a_detector->setDbId($det_id);
192  $mode = self::DB_MODE_CREATE;
193  }
194 
195  $node = $a_detector->getContext();
196  $workflow = $node->getContext();
197  if ($workflow->hasDbId()) {
198  $wf_id = $workflow->getDbId();
199  } else {
200  $wf_id = null;
201  }
202 
203  $det_data = $a_detector->getEvent();
204  $det_subject = $a_detector->getEventSubject();
205  $det_context = $a_detector->getEventContext();
206  $det_listen = $a_detector->getListeningTimeframe();
207 
208  if ($det_context['identifier'] === '{{THIS:WFID}}') {
209  $det_context['identifier'] = $wf_id;
210  }
211 
212  if ($det_subject['identifier'] === '{{THIS:WFID}}') {
213  $det_subject['identifier'] = $wf_id;
214  }
215 
216  if ($mode == self::DB_MODE_UPDATE) {
217  $ilDB->update(
218  'wfe_det_listening',
219  array(
220  'workflow_id' => array('integer', $wf_id),
221  'type' => array('text', $det_data['type'] ),
222  'content' => array('text', $det_data['content']),
223  'subject_type' => array('text', $det_subject['type']),
224  'subject_id' => array('integer', $det_subject['identifier']),
225  'context_type' => array('text', $det_context['type']),
226  'context_id' => array('integer', $det_context['identifier']),
227  'listening_start' => array('integer', $det_listen['listening_start']),
228  'listening_end' => array('integer', $det_listen['listening_end'])
229  ),
230  array(
231  'detector_id' => array('integer', $det_id)
232  )
233  );
234  }
235 
236  if ($mode == self::DB_MODE_CREATE) {
237  $ilDB->insert(
238  'wfe_det_listening',
239  array(
240  'detector_id' => array('integer', $det_id),
241  'workflow_id' => array('integer', $wf_id),
242  'type' => array('text', $det_data['type'] ),
243  'content' => array('text', $det_data['content']),
244  'subject_type' => array('text', $det_subject['type']),
245  'subject_id' => array('integer', $det_subject['identifier']),
246  'context_type' => array('text', $det_context['type']),
247  'context_id' => array('integer', $det_context['identifier']),
248  'listening_start' => array('integer', $det_listen['listening_start']),
249  'listening_end' => array('integer', $det_listen['listening_end'])
250  )
251  );
252  }
253  }
254 
264  public static function deleteDetector(ilExternalDetector $detector)
265  {
266  global $DIC;
268  $ilDB = $DIC['ilDB'];
269 
270  if ($detector->hasDbId()) {
271  $ilDB->manipulate(
272  'DELETE
273  FROM wfe_det_listening
274  WHERE detector_id = ' . $ilDB->quote($detector->getDbId(), 'integer')
275  );
276  $detector->setDbId(null);
277  } else {
278  return;
279  }
280  }
281 
296  public static function getDetectors(
297  $type,
298  $content,
299  $subject_type,
300  $subject_id,
301  $context_type,
302  $context_id
303  ) {
304  global $DIC;
306  $ilDB = $DIC['ilDB'];
307 
308  require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowUtils.php';
309  $now = ilWorkflowUtils::time();
310  $workflows = array();
311 
312  $result = $ilDB->query(
313  'SELECT workflow_id
314  FROM wfe_det_listening
315  WHERE type = ' . $ilDB->quote($type, 'text') . '
316  AND content = ' . $ilDB->quote($content, 'text') . '
317  AND subject_type = ' . $ilDB->quote($subject_type, 'text') . '
318  AND (subject_id = ' . $ilDB->quote($subject_id, 'integer') . ' OR subject_id = ' . $ilDB->quote(0, 'integer') . ')
319  AND context_type = ' . $ilDB->quote($context_type, 'text') . '
320  AND (context_id = ' . $ilDB->quote($context_id, 'integer') . ' OR context_id = ' . $ilDB->quote(0, 'integer') . ')
321  AND (listening_start = ' . $ilDB->quote(0, 'integer') . '
322  OR listening_start <= ' . $ilDB->quote($now, 'integer') . ') AND (listening_end = ' . $ilDB->quote(0, 'integer') . '
323  OR listening_end >= ' . $ilDB->quote($now, 'integer') . ')'
324  );
325 
326  while ($row = $ilDB->fetchAssoc($result)) {
327  $workflows[] = $row['workflow_id'];
328  }
329 
330  return $workflows;
331  }
332 
343  public static function wakeupWorkflow($id)
344  {
345  global $DIC;
347  $ilDB = $DIC['ilDB'];
348 
349  $result = $ilDB->query(
350  'SELECT workflow_class, workflow_location, workflow_instance
351  FROM wfe_workflows
352  WHERE workflow_id = ' . $ilDB->quote($id, 'integer')
353  );
354 
355  $workflow = $ilDB->fetchAssoc($result);
356 
357  require_once './Services/WorkflowEngine/classes/workflows/class.ilBaseWorkflow.php';
358  $path = rtrim($workflow['workflow_location'], '/') . '/' . $workflow['workflow_class'];
359 
360  if (file_exists($path) && $path != '/') {
361  require_once $path;
362  $instance = unserialize($workflow['workflow_instance']);
363  } else {
364  $instance = null;
365  }
366  return $instance;
367  }
368 
376  public static function writeStartEventData($event, $process_id)
377  {
378  global $DIC;
380  $ilDB = $DIC['ilDB'];
381 
382  $event_id = $ilDB->nextId('wfe_startup_events');
383 
384  $ilDB->insert(
385  'wfe_startup_events',
386  array(
387  'event_id' => array('integer', $event_id),
388  'workflow_id' => array('text', $process_id),
389  'type' => array('text', $event['type'] ),
390  'content' => array('text', $event['content']),
391  'subject_type' => array('text', $event['subject_type']),
392  'subject_id' => array('integer', $event['subject_id']),
393  'context_type' => array('text', $event['context_type']),
394  'context_id' => array('integer', $event['context_id'])
395  )
396  );
397 
398  return $event_id;
399  }
400 
406  public static function writeStaticInput($key, $value, $start_event)
407  {
408  global $DIC;
410  $ilDB = $DIC['ilDB'];
411 
412  $ilDB->insert(
413  'wfe_static_inputs',
414  array(
415  'input_id' => array('integer', $ilDB->nextId('wfe_static_inputs')),
416  'event_id' => array('integer', $start_event),
417  'name' => array('text', $key),
418  'value' => array('text', $value)
419  )
420  );
421  }
422 
423  public static function findApplicableWorkflows($component, $event, $params)
424  {
425  $query = "SELECT event_id, workflow_id FROM wfe_startup_events WHERE
426  type = '" . $component . "' AND content = '" . $event . "' AND subject_type = '" . $params->getSubjectType() . "'
427  AND context_type = '" . $params->getContextType() . "' ";
428 
429  $query .= "AND ( subject_id = '" . $params->getSubjectId() . "' OR subject_id ='0' ) ";
430  $query .= "AND ( context_id = '" . $params->getContextId() . "' OR context_id ='0' ) ";
431 
432  global $DIC;
434  $ilDB = $DIC['ilDB'];
435 
436  $workflows = array();
437  $result = $ilDB->query($query);
438  while ($row = $ilDB->fetchAssoc($result)) {
439  $workflows[] = array('event' => $row['event_id'], 'workflow' => $row['workflow_id']);
440  }
441  return $workflows;
442  }
443 
444  public static function getStaticInputDataForEvent($event_id)
445  {
446  $query = "SELECT name, value FROM wfe_static_inputs WHERE event_id = '" . $event_id . "'";
447 
448  global $DIC;
450  $ilDB = $DIC['ilDB'];
451 
452  $result = $ilDB->query($query);
453 
454  $retval = array();
455 
456  while ($row = $ilDB->fetchAssoc($result)) {
457  $retval[$row['name']] = $row['value'];
458  }
459 
460  return $retval;
461  }
462 
463  public static function deleteStartEventData($event_id)
464  {
465  global $DIC;
467  $ilDB = $DIC['ilDB'];
468 
469  $result = $ilDB->query(
470  'SELECT event_id FROM wfe_startup_events
471  WHERE workflow_id = ' . $ilDB->quote($event_id, 'integer')
472  );
473 
474  $events = array();
475  while ($row = $ilDB->fetchAssoc($result)) {
476  $events = $row['revent_id'];
477  }
478 
479  $ilDB->manipulate(
480  'DELETE
481  FROM wfe_startup_events
482  WHERE workflow_id = ' . $ilDB->quote($event_id, 'integer')
483  );
484 
485  if (count($events)) {
486  $ilDB->manipulate(
487  'DELETE
488  FROM wfe_static_inputs
489  WHERE ' . $ilDB->in('event_id', $events, false, 'integer')
490  );
491  }
492  }
493 }
$params
Definition: disable.php:11
$result
$type
getWorkflowSubject()
global $DIC
Definition: saml.php:7
getWorkflowContext()
if(!array_key_exists('StateId', $_REQUEST)) $id
getWorkflowLocation()
isDataPersistenceRequired()
PhpIncludeInspection
if($format !==null) $name
Definition: metadata.php:146
getWorkflowClass()
resetDataPersistenceRequirement()
$query
Create styles array
The data for the language used.
ilWorkflowDbHelper is part of the petri net based workflow engine.
PhpIncludeInspection
Definition: ilWorkflow.php:23
global $ilDB
setDbId($id)
ilDetector Interface is part of the petri net based workflow engine.
Definition: ilDetector.php:16
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
$key
Definition: croninfo.php:18