ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  {
40  $wf_id = $workflow->getDbId();
41  $mode = self::DB_MODE_UPDATE;
42  }
43  else
44  {
45  $wf_id = $ilDB->nextId('wfe_workflows');
46  $workflow->setDbId($wf_id);
47  $mode = self::DB_MODE_CREATE;
48  }
49 
50  $wf_data = $workflow->getWorkflowData();
51  $wf_subject = $workflow->getWorkflowSubject();
52  $wf_context = $workflow->getWorkflowContext();
53  $active = $workflow->isActive();
54  $instance = serialize($workflow);
55 
56  if ($mode == self::DB_MODE_UPDATE)
57  {
58  $ilDB->update('wfe_workflows',
59  array(
60  'workflow_type' => array ('text', $wf_data['type'] ),
61  'workflow_content' => array ('text', $wf_data['content']),
62  'workflow_class' => array ('text', $workflow->getWorkflowClass()),
63  'workflow_location' => array ('text', $workflow->getWorkflowLocation()),
64  'subject_type' => array ('text', $wf_subject['type']),
65  'subject_id' => array ('integer', $wf_subject['identifier']),
66  'context_type' => array ('text', $wf_context['type']),
67  'context_id' => array ('integer', $wf_context['identifier']),
68  'workflow_instance' => array ('clob', $instance),
69  'active' => array ('integer', (int)$active)
70  ),
71  array(
72  'workflow_id' => array ('integer', $wf_id)
73  )
74  );
75  }
76 
77  if ($mode == self::DB_MODE_CREATE)
78  {
79  $ilDB->insert('wfe_workflows',
80  array(
81  'workflow_id' => array ('integer', $wf_id),
82  'workflow_type' => array ('text', $wf_data['type'] ),
83  'workflow_class' => array ('text', $workflow->getWorkflowClass()),
84  'workflow_location' => array ('text', $workflow->getWorkflowLocation()),
85  'workflow_content' => array ('text', $wf_data['content']),
86  'subject_type' => array ('text', $wf_subject['type']),
87  'subject_id' => array ('integer', $wf_subject['identifier']),
88  'context_type' => array ('text', $wf_context['type']),
89  'context_id' => array ('integer', $wf_context['identifier']),
90  'workflow_instance' => array ('clob', $instance),
91  'active' => array ('integer', (int)$active)
92  )
93  );
94  }
95 
96  if($require_data_persistance)
97  {
98  self::persistWorkflowIOData($workflow);
99  }
100  }
101 
105  public static function persistWorkflowIOData(ilWorkflow $workflow)
106  {
107  global $DIC;
109  $ilDB = $DIC['ilDB'];
110 
111  $workflow_id = $workflow->getId();
112 
113  $input_data = $workflow->getInputVars();
114  foreach($input_data as $name => $value)
115  {
116  $ilDB->replace(
117  'wfe_io_inputs',
118  array('workflow_id' => $workflow_id, 'name' => $name),
119  array('value' => $value)
120  );
121  }
122 
123  $output_data = $workflow->getOutputVars();
124  foreach($output_data as $name => $value)
125  {
126  $ilDB->replace(
127  'wfe_io_outputs',
128  array('workflow_id' => $workflow_id, 'name' => $name),
129  array('value' => $value)
130  );
131  }
132  }
133 
142  public static function deleteWorkflow(ilWorkflow $a_workflow)
143  {
144  global $DIC;
146  $ilDB = $DIC['ilDB'];
147 
148  if ($a_workflow->hasDbId())
149  {
150  $ilDB->manipulate(
151  'DELETE
152  FROM wfe_workflows
153  WHERE workflow_id = ' . $ilDB->quote($a_workflow->getDbId(), 'integer')
154  );
155 
156  // This should not be necessary, actually. Still this call makes sure
157  // that there won't be orphan records polluting the database.
158  $ilDB->manipulate(
159  'DELETE
160  FROM wfe_det_listening
161  WHERE workflow_id = ' . $ilDB->quote($a_workflow->getDbId(), 'integer')
162  );
163 
164  $ilDB->manipulate(
165  'DELETE
166  FROM wfe_io_inputs
167  WHERE workflow_id = ' . $ilDB->quote($a_workflow->getDbId(), 'integer')
168  );
169 
170  $ilDB->manipulate(
171  'DELETE
172  FROM wfe_io_outputs
173  WHERE workflow_id = ' . $ilDB->quote($a_workflow->getDbId(), 'integer')
174  );
175  }
176  else
177  {
178  return;
179  }
180  }
181 
189  public static function writeDetector(ilDetector $a_detector)
190  {
191  global $DIC;
193  $ilDB = $DIC['ilDB'];
194 
195  if ($a_detector->hasDbId())
196  {
197  $det_id = $a_detector->getDbId();
198  $mode = self::DB_MODE_UPDATE;
199  }
200  else
201  {
202  $det_id = $ilDB->nextId('wfe_det_listening');
203  $a_detector->setDbId($det_id);
204  $mode = self::DB_MODE_CREATE;
205  }
206 
207  $node = $a_detector->getContext();
208  $workflow = $node->getContext();
209  if($workflow->hasDbId())
210  {
211  $wf_id = $workflow->getDbId();
212  } else {
213  $wf_id = null;
214  }
215 
216  $det_data = $a_detector->getEvent();
217  $det_subject = $a_detector->getEventSubject();
218  $det_context = $a_detector->getEventContext();
219  $det_listen = $a_detector->getListeningTimeframe();
220 
221  if($det_context['identifier'] === '{{THIS:WFID}}')
222  {
223  $det_context['identifier'] = $wf_id;
224  }
225 
226  if($det_subject['identifier'] === '{{THIS:WFID}}')
227  {
228  $det_subject['identifier'] = $wf_id;
229  }
230 
231  if ($mode == self::DB_MODE_UPDATE)
232  {
233  $ilDB->update('wfe_det_listening',
234  array(
235  'workflow_id' => array ('integer', $wf_id),
236  'type' => array ('text', $det_data['type'] ),
237  'content' => array ('text', $det_data['content']),
238  'subject_type' => array ('text', $det_subject['type']),
239  'subject_id' => array ('integer', $det_subject['identifier']),
240  'context_type' => array ('text', $det_context['type']),
241  'context_id' => array ('integer', $det_context['identifier']),
242  'listening_start' => array ('integer', $det_listen['listening_start']),
243  'listening_end' => array ('integer', $det_listen['listening_end'])
244  ),
245  array(
246  'detector_id' => array ('integer', $det_id)
247  )
248  );
249  }
250 
251  if ($mode == self::DB_MODE_CREATE)
252  {
253  $ilDB->insert('wfe_det_listening',
254  array(
255  'detector_id' => array ('integer', $det_id),
256  'workflow_id' => array ('integer', $wf_id),
257  'type' => array ('text', $det_data['type'] ),
258  'content' => array ('text', $det_data['content']),
259  'subject_type' => array ('text', $det_subject['type']),
260  'subject_id' => array ('integer', $det_subject['identifier']),
261  'context_type' => array ('text', $det_context['type']),
262  'context_id' => array ('integer', $det_context['identifier']),
263  'listening_start' => array ('integer', $det_listen['listening_start']),
264  'listening_end' => array ('integer', $det_listen['listening_end'])
265  )
266  );
267  }
268  }
269 
279  public static function deleteDetector(ilExternalDetector $detector)
280  {
281  global $DIC;
283  $ilDB = $DIC['ilDB'];
284 
285  if ($detector->hasDbId())
286  {
287  $ilDB->manipulate(
288  'DELETE
289  FROM wfe_det_listening
290  WHERE detector_id = ' . $ilDB->quote($detector->getDbId(), 'integer')
291  );
292  $detector->setDbId(null);
293  }
294  else
295  {
296  return;
297  }
298  }
299 
314  public static function getDetectors(
315  $type,
316  $content,
317  $subject_type,
318  $subject_id,
319  $context_type,
320  $context_id
321  )
322  {
323  global $DIC;
325  $ilDB = $DIC['ilDB'];
326 
327  require_once './Services/WorkflowEngine/classes/utils/class.ilWorkflowUtils.php';
328  $now = ilWorkflowUtils::time();
329  $workflows = array();
330 
331  $result = $ilDB->query(
332  'SELECT workflow_id
333  FROM wfe_det_listening
334  WHERE type = ' . $ilDB->quote($type, 'text') . '
335  AND content = ' . $ilDB->quote($content, 'text') . '
336  AND subject_type = ' . $ilDB->quote($subject_type, 'text') . '
337  AND (subject_id = ' . $ilDB->quote($subject_id, 'integer') . ' OR subject_id = ' . $ilDB->quote(0, 'integer') . ')
338  AND context_type = ' . $ilDB->quote($context_type, 'text') . '
339  AND (context_id = ' . $ilDB->quote($context_id, 'integer') . ' OR context_id = ' . $ilDB->quote(0, 'integer') . ')
340  AND (listening_start = ' . $ilDB->quote(0, 'integer') . '
341  OR listening_start <= ' . $ilDB->quote($now, 'integer') . ') AND (listening_end = '. $ilDB->quote(0, 'integer') . '
342  OR listening_end >= ' . $ilDB->quote($now, 'integer') . ')'
343  );
344 
345  while ($row = $ilDB->fetchAssoc($result))
346  {
347  $workflows[] = $row['workflow_id'];
348  }
349 
350  return $workflows;
351  }
352 
363  public static function wakeupWorkflow($id)
364  {
365  global $DIC;
367  $ilDB = $DIC['ilDB'];
368 
369  $result = $ilDB->query(
370  'SELECT workflow_class, workflow_location, workflow_instance
371  FROM wfe_workflows
372  WHERE workflow_id = ' . $ilDB->quote($id, 'integer')
373  );
374 
375  $workflow = $ilDB->fetchAssoc($result);
376 
377  require_once './Services/WorkflowEngine/classes/workflows/class.ilBaseWorkflow.php';
378  $path = rtrim($workflow['workflow_location'], '/') . '/' . $workflow['workflow_class'];
379 
380  if(file_exists($path) && $path != '/')
381  {
382  require_once $path;
383  $instance = unserialize($workflow['workflow_instance']);
384  }
385  else
386  {
387  $instance = null;
388  }
389  return $instance;
390  }
391 
399  public static function writeStartEventData($event, $process_id)
400  {
401  global $DIC;
403  $ilDB = $DIC['ilDB'];
404 
405  $event_id = $ilDB->nextId('wfe_startup_events');
406 
407  $ilDB->insert('wfe_startup_events',
408  array(
409  'event_id' => array ('integer', $event_id),
410  'workflow_id' => array ('text', $process_id),
411  'type' => array ('text', $event['type'] ),
412  'content' => array ('text', $event['content']),
413  'subject_type' => array ('text', $event['subject_type']),
414  'subject_id' => array ('integer', $event['subject_id']),
415  'context_type' => array ('text', $event['context_type']),
416  'context_id' => array ('integer', $event['context_id'])
417  )
418  );
419 
420  return $event_id;
421  }
422 
428  public static function writeStaticInput($key, $value, $start_event)
429  {
430  global $DIC;
432  $ilDB = $DIC['ilDB'];
433 
434  $ilDB->insert(
435  'wfe_static_inputs',
436  array(
437  'input_id' => array ('integer', $ilDB->nextId('wfe_static_inputs')),
438  'event_id' => array ('integer', $start_event),
439  'name' => array ('text', $key),
440  'value' => array ('text', $value)
441  )
442  );
443  }
444 
445  public static function findApplicableWorkflows($component, $event, $params)
446  {
447  $query = "SELECT event_id, workflow_id FROM wfe_startup_events WHERE
448  type = '".$component."' AND content = '".$event."' AND subject_type = '".$params->getSubjectType()."'
449  AND context_type = '".$params->getContextType()."' ";
450 
451  $query .= "AND ( subject_id = '".$params->getSubjectId()."' OR subject_id ='0' ) ";
452  $query .= "AND ( context_id = '".$params->getContextId()."' OR context_id ='0' ) ";
453 
454  global $DIC;
456  $ilDB = $DIC['ilDB'];
457 
458  $workflows = array();
459  $result = $ilDB->query($query);
460  while ($row = $ilDB->fetchAssoc($result))
461  {
462  $workflows[] = array('event' => $row['event_id'], 'workflow' => $row['workflow_id']);
463  }
464  return $workflows;
465  }
466 
467  public static function getStaticInputDataForEvent($event_id)
468  {
469  $query = "SELECT name, value FROM wfe_static_inputs WHERE event_id = '" . $event_id . "'";
470 
471  global $DIC;
473  $ilDB = $DIC['ilDB'];
474 
475  $result = $ilDB->query($query);
476 
477  $retval = array();
478 
479  while($row = $ilDB->fetchAssoc($result))
480  {
481  $retval[$row['name']] = $row['value'];
482  }
483 
484  return $retval;
485  }
486 
487  public static function deleteStartEventData($event_id)
488  {
489  global $DIC;
491  $ilDB = $DIC['ilDB'];
492 
493  $result = $ilDB->query(
494  'SELECT event_id FROM wfe_startup_events
495  WHERE workflow_id = ' . $ilDB->quote($event_id, 'integer')
496  );
497 
498  $events = array();
499  while($row = $ilDB->fetchAssoc($result))
500  {
501  $events = $row['revent_id'];
502  }
503 
504  $ilDB->manipulate(
505  'DELETE
506  FROM wfe_startup_events
507  WHERE workflow_id = ' . $ilDB->quote($event_id, 'integer')
508  );
509 
510  if(count($events))
511  {
512  $ilDB->manipulate(
513  'DELETE
514  FROM wfe_static_inputs
515  WHERE ' . $ilDB->in('event_id', $events, false, 'integer')
516  );
517  }
518  }
519 }
$path
Definition: aliased.php:25
$result
getWorkflowSubject()
getWorkflowContext()
getWorkflowLocation()
isDataPersistenceRequired()
PhpIncludeInspection
getWorkflowClass()
resetDataPersistenceRequirement()
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
global $DIC
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
$params
Definition: example_049.php:96