Merge pull request #9633 from jaapjansma/CRM-19831
[civicrm-core.git] / Civi / CCase / SequenceListener.php
index b885f406dc3281c760ea170b6ebcf208f6301d24..1bceea8c8f2e670f869d4c9df8839475fb09d548 100644 (file)
@@ -14,8 +14,9 @@ class SequenceListener implements CaseChangeListener {
   private static $singleton;
 
   /**
-   * @param bool $reset whether to forcibly rebuild the entire container
-   * @return \Symfony\Component\DependencyInjection\TaggedContainerInterface
+   * @param bool $reset
+   *   Whether to forcibly rebuild the entire container.
+   * @return SequenceListener
    */
   public static function singleton($reset = FALSE) {
     if ($reset || self::$singleton === NULL) {
@@ -24,10 +25,19 @@ class SequenceListener implements CaseChangeListener {
     return self::$singleton;
   }
 
+  /**
+   * @param \Civi\CCase\Event\CaseChangeEvent $event
+   */
   public static function onCaseChange_static(\Civi\CCase\Event\CaseChangeEvent $event) {
     self::singleton()->onCaseChange($event);
   }
 
+  /**
+   * @param \Civi\CCase\Event\CaseChangeEvent $event
+   *
+   * @throws \CiviCRM_API3_Exception
+   * @return void
+   */
   public function onCaseChange(\Civi\CCase\Event\CaseChangeEvent $event) {
     /** @var \Civi\CCase\Analyzer $analyzer */
     $analyzer = $event->analyzer;
@@ -55,9 +65,17 @@ class SequenceListener implements CaseChangeListener {
       }
     }
 
-    // OK, the activity has completed every step in the sequence!
+    //CRM-17452 - Close the case only if all the activities are complete
+    $activities = $analyzer->getActivities();
+    foreach ($activities as $activity) {
+      if ($activity['status_id'] != $actStatuses['Completed']) {
+        return;
+      }
+    }
+
+    // OK, the all activities have completed
     civicrm_api3('Case', 'create', array(
-      'id'  => $analyzer->getCaseId(),
+      'id' => $analyzer->getCaseId(),
       'status_id' => 'Closed',
     ));
     $analyzer->flush();
@@ -87,7 +105,8 @@ class SequenceListener implements CaseChangeListener {
   }
 
   /**
-   * @param Analyzer $analyzer the case being analyzed -- to which we want to add an activity
+   * @param Analyzer $analyzer
+   *   The case being analyzed -- to which we want to add an activity.
    * @param \SimpleXMLElement $actXML the <ActivityType> tag which describes the new activity
    */
   public function createActivity(Analyzer $analyzer, \SimpleXMLElement $actXML) {
@@ -100,4 +119,5 @@ class SequenceListener implements CaseChangeListener {
     $r = civicrm_api3('Activity', 'create', $params);
     $analyzer->flush();
   }
+
 }