5 * The sequence-listener looks for CiviCase XML tags with "<sequence>". If
6 * a change is made to any record in case-type which uses "<sequence>", then
7 * it attempts to add the next step in the sequence.
9 class SequenceListener
implements CaseChangeListener
{
12 * @var SequenceListener
14 private static $singleton;
18 * Whether to forcibly rebuild the entire container.
19 * @return SequenceListener
21 public static function singleton($reset = FALSE) {
22 if ($reset || self
::$singleton === NULL) {
23 self
::$singleton = new SequenceListener();
25 return self
::$singleton;
29 * @param \Civi\CCase\Event\CaseChangeEvent $event
31 public static function onCaseChange_static(\Civi\CCase\Event\CaseChangeEvent
$event) {
32 self
::singleton()->onCaseChange($event);
36 * Triggers next case activity in sequence if current activity status is updated
37 * to type=COMPLETED(See CRM-21598). The adjoining activity is created according
38 * to the sequence configured in case type.
40 * @param \Civi\CCase\Event\CaseChangeEvent $event
42 * @throws \CiviCRM_API3_Exception
45 public function onCaseChange(\Civi\CCase\Event\CaseChangeEvent
$event) {
46 /** @var \Civi\CCase\Analyzer $analyzer */
47 $analyzer = $event->analyzer
;
49 $activitySetXML = $this->getSequenceXml($analyzer->getXml());
50 if (!$activitySetXML) {
54 $actTypes = array_flip(\CRM_Core_PseudoConstant
::activityType(TRUE, TRUE, FALSE, 'name'));
55 $actStatuses = array_flip(\CRM_Activity_BAO_Activity
::getStatusesByType(\CRM_Activity_BAO_Activity
::COMPLETED
));
57 $actIndex = $analyzer->getActivityIndex(array('activity_type_id', 'status_id'));
59 foreach ($activitySetXML->ActivityTypes
->ActivityType
as $actTypeXML) {
60 $actTypeId = $actTypes[(string) $actTypeXML->name
];
61 if (empty($actIndex[$actTypeId])) {
62 // Haven't tried this step yet!
63 $this->createActivity($analyzer, $actTypeXML);
66 elseif (!in_array(key($actIndex[$actTypeId]), $actStatuses)) {
67 // Haven't gotten past this step yet!
72 //CRM-17452 - Close the case only if all the activities are complete
73 $activities = $analyzer->getActivities();
74 foreach ($activities as $activity) {
75 if (!in_array($activity['status_id'], $actStatuses)) {
80 // OK, the all activities have completed
81 civicrm_api3('Case', 'create', array(
82 'id' => $analyzer->getCaseId(),
83 'status_id' => 'Closed',
89 * Find the ActivitySet which defines the pipeline.
91 * @param \SimpleXMLElement $xml
92 * @return \SimpleXMLElement|NULL
94 public function getSequenceXml($xml) {
95 if ($xml->ActivitySets
&& $xml->ActivitySets
->ActivitySet
) {
96 foreach ($xml->ActivitySets
->ActivitySet
as $activitySetXML) {
97 $seq = (string) $activitySetXML->sequence
;
98 if ($seq && strtolower($seq) == 'true') {
99 if ($activitySetXML->ActivityTypes
&& $activitySetXML->ActivityTypes
->ActivityType
) {
100 return $activitySetXML;
112 * @param Analyzer $analyzer
113 * The case being analyzed -- to which we want to add an activity.
114 * @param \SimpleXMLElement $actXML the <ActivityType> tag which describes the new activity
116 public function createActivity(Analyzer
$analyzer, \SimpleXMLElement
$actXML) {
118 'activity_type_id' => (string) $actXML->name
,
119 'status_id' => 'Scheduled',
120 'activity_date_time' => \CRM_Utils_Time
::getTime('YmdHis'),
121 'case_id' => $analyzer->getCaseId(),
123 $r = civicrm_api3('Activity', 'create', $params);