Merge remote-tracking branch 'origin/abtest' into master-abtest
[civicrm-core.git] / CRM / Case / BAO / Case.php
index a51ba1513e406782b23897fbcbabaeb4f3d38b9c..39823568ebe26d6af7fca940f8e40d62e7e05bed 100644 (file)
@@ -47,6 +47,9 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case {
    */
   static $_exportableFields = NULL;
 
+  /**
+   *
+   */
   function __construct() {
     parent::__construct();
   }
@@ -59,7 +62,8 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case {
    * pairs
    *
    * @param array $params (reference ) an assoc array of name/value pairs
-   * @param array $ids    the array that holds all the db ids
+   *
+   * @internal param array $ids the array that holds all the db ids
    *
    * @return object CRM_Case_BAO_Case object
    * @access public
@@ -100,7 +104,8 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case {
    * takes an associative array and creates a case object
    *
    * @param array $params (reference ) an assoc array of name/value pairs
-   * @param array $ids    the array that holds all the db ids
+   *
+   * @internal param array $ids the array that holds all the db ids
    *
    * @return object CRM_Case_BAO_Case object
    * @access public
@@ -296,6 +301,8 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case {
    *
    * @param int $caseId
    *
+   * @param string $colName
+   *
    * @return  case type
    * @access public
    * @static
@@ -318,7 +325,9 @@ WHERE civicrm_case.id = %1";
    *
    * @param  int $caseId id of the case to delete
    *
-   * @return void
+   * @param bool $moveToTrash
+   *
+   * @return bool is successful
    * @access public
    * @static
    */
@@ -409,11 +418,12 @@ WHERE civicrm_case.id = %1";
   /**
    * Retrieve contact_id by case_id
    *
-   * @param int $caseId  ID of the case
+   * @param int $caseId ID of the case
+   *
+   * @param null $contactID
    *
    * @return array
    * @access public
-   *
    */
   static function retrieveContactIdsByCaseId($caseId, $contactID = NULL) {
     $caseContact = new CRM_Case_DAO_CaseContact();
@@ -434,7 +444,9 @@ WHERE civicrm_case.id = %1";
   /**
    * Look up a case using an activity ID
    *
-   * @param $activity_id
+   * @param $activityId
+   *
+   * @internal param $activity_id
    *
    * @return int, case ID
    */
@@ -501,13 +513,15 @@ WHERE civicrm_case.id = %1";
   /**
    * Retrieve case_id by contact_id
    *
-   * @param int $contactId      ID of the contact
+   * @param $contactID
    * @param boolean $includeDeleted include the deleted cases in result
    *
+   * @param null $caseType
+   *
+   * @internal param int $contactId ID of the contact
    * @return array
    *
    * @access public
-   *
    */
   static function retrieveCaseIdsByContactId($contactID, $includeDeleted = FALSE, $caseType = NULL) {
     $query = "
@@ -539,6 +553,14 @@ WHERE cc.contact_id = %1 AND civicrm_case_type.name = '{$caseType}'";
     return $caseArray;
   }
 
+  /**
+   * @param string $type
+   * @param null $userID
+   * @param null $condition
+   * @param int $isDeleted
+   *
+   * @return string
+   */
   static function getCaseActivityQuery($type = 'upcoming', $userID = NULL, $condition = NULL, $isDeleted = 0) {
     if (!$userID) {
       $session = CRM_Core_Session::singleton();
@@ -698,10 +720,11 @@ LEFT JOIN civicrm_option_group aog ON aog.name='activity_type'
    *
    * @param String $type /upcoming,recent,all/
    *
+   * @param string $context
+   *
    * @return array     Array of Cases
    *
    * @access public
-   *
    */
   static function getCases($allCases = TRUE, $userID = NULL, $type = 'upcoming', $context = 'dashboard') {
     $condition = NULL;
@@ -838,7 +861,8 @@ AND civicrm_case.status_id != $closedId";
         }
       }
       //CRM-4510.
-      $caseManagerContact = self::getCaseManagerContact($result->case_type_name, $result->case_id);
+      $caseTypes = CRM_Case_PseudoConstant::caseType('name');
+      $caseManagerContact = self::getCaseManagerContact($caseTypes[$result->case_type_id], $result->case_id);
       if (!empty($caseManagerContact)) {
         $casesList[$result->case_id]['casemanager_id'] = CRM_Utils_Array::value('casemanager_id', $caseManagerContact);
         $casesList[$result->case_id]['casemanager'] = CRM_Utils_Array::value('casemanager', $caseManagerContact);
@@ -942,6 +966,8 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
    *
    * @param int $contactID contact id
    * @param int $caseID case id
+   * @param null $relationshipID
+   *
    * @return returns case role / relationships
    *
    * @static
@@ -999,6 +1025,10 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
    * @param array $params posted params
    * @param int $contactID contact id
    *
+   * @param null $context
+   * @param null $userID
+   * @param null $type
+   *
    * @return returns case activities
    *
    * @static
@@ -1277,10 +1307,10 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
 
       //check for operations.
       if (self::checkPermission($dao->id, 'Move To Case', $dao->activity_type_id)) {
-        $url .= ' <a ' . $css . ' href="#" onClick="Javascript:fileOnCase( \'move\',' . $dao->id . ', ' . $caseID . ' ); return false;">' . ts('Move To Case') . '</a> ';
+        $url .= ' <a ' . $css . ' href="#" onClick="Javascript:fileOnCase( \'move\',' . $dao->id . ', ' . $caseID . ', this ); return false;">' . ts('Move To Case') . '</a> ';
       }
       if (self::checkPermission($dao->id, 'Copy To Case', $dao->activity_type_id)) {
-        $url .= ' <a ' . $css . ' href="#" onClick="Javascript:fileOnCase( \'copy\',' . $dao->id . ',' . $caseID . ' ); return false;">' . ts('Copy To Case') . '</a> ';
+        $url .= ' <a ' . $css . ' href="#" onClick="Javascript:fileOnCase( \'copy\',' . $dao->id . ',' . $caseID . ', this ); return false;">' . ts('Copy To Case') . '</a> ';
       }
       // if there are file attachments we will return how many and, if only one, add a link to it
       if (!empty($dao->attachment_ids)) {
@@ -1339,19 +1369,24 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
    * @param int $caseID case id
    * @param boolean $skipDetails if true include details of contacts
    *
-   * @return returns $searchRows array of returnproperties
+   * @return array $searchRows array of return properties
    *
    * @static
    */
   static function getRelatedContacts($caseID, $skipDetails = FALSE) {
     $values = array();
-    $query = 'SELECT cc.display_name as name, cc.sort_name as sort_name, cc.id, crt.label_b_a as role, ce.email
- FROM civicrm_relationship cr
- LEFT JOIN civicrm_relationship_type crt ON crt.id = cr.relationship_type_id
- LEFT JOIN civicrm_contact cc ON cc.id = cr.contact_id_b
- LEFT JOIN civicrm_email   ce ON ce.contact_id = cc.id
- WHERE cr.case_id =  %1 AND ce.is_primary= 1
- GROUP BY cc.id';
+    $query = '
+      SELECT cc.display_name as name, cc.sort_name as sort_name, cc.id, crt.label_b_a as role, ce.email
+      FROM civicrm_relationship cr
+      LEFT JOIN civicrm_relationship_type crt
+        ON crt.id = cr.relationship_type_id
+      LEFT JOIN civicrm_contact cc
+        ON cc.id = cr.contact_id_b
+      LEFT JOIN civicrm_email ce
+        ON ce.contact_id = cc.id
+        AND ce.is_primary= 1
+      WHERE cr.case_id =  %1
+      GROUP BY cc.id';
 
     $params = array(1 => array($caseID, 'Integer'));
     $dao = CRM_Core_DAO::executeQuery($query, $params);
@@ -1378,9 +1413,13 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
   /**
    * Function that sends e-mail copy of activity
    *
+   * @param $clientId
    * @param int $activityId activity Id
    * @param array $contacts array of related contact
    *
+   * @param null $attachments
+   * @param $caseId
+   *
    * @return void
    * @access public
    */
@@ -1548,12 +1587,9 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
   /**
    * Create an activity for a case via email
    *
-   * @param int $file   email sent
-   *
-   * @return $activity object of newly creted activity via email
-   *
-   * @access public
+   * @param int $file email sent
    *
+   * @return array|void $activity object of newly creted activity via email@access public
    */
   static function recordActivityViaEmail($file) {
     if (!file_exists($file) ||
@@ -1639,7 +1675,9 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
   /**
    * Function to retrieve the scheduled activity type and date
    *
-   * @param  array $cases  Array of contact and case id
+   * @param  array $cases Array of contact and case id
+   *
+   * @param string $type
    *
    * @return array  $activityInfo Array of scheduled activity type and date
    *
@@ -1735,6 +1773,16 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
     return TRUE;
   }
 
+  /**
+   * @param $groupInfo
+   * @param null $sort
+   * @param null $showLinks
+   * @param bool $returnOnlyCount
+   * @param int $offset
+   * @param int $rowCount
+   *
+   * @return array
+   */
   static function getGlobalContacts(&$groupInfo, $sort = NULL, $showLinks = NULL, $returnOnlyCount = FALSE, $offset = 0, $rowCount = 25) {
     $globalContacts = array();
 
@@ -1771,6 +1819,11 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
   /*
    * Convenience function to get both case contacts and global in one array
    */
+  /**
+   * @param $caseId
+   *
+   * @return array
+   */
   static function getRelatedAndGlobalContacts($caseId) {
     $relatedContacts = self::getRelatedContacts($caseId);
 
@@ -1844,10 +1897,13 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
   /**
    * Function to create activities when Case or Other roles assigned/modified/deleted.
    *
-   * @param int $caseID case id
+   * @param $caseId
    * @param int $relationshipId relationship id
    * @param int $relContactId case role assignee contactId.
    *
+   * @param null $contactId
+   *
+   * @internal param int $caseID case id
    * @return void on success creates activity and case activity
    *
    * @static
@@ -1981,11 +2037,15 @@ SELECT civicrm_contact.id as casemanager_id,
   /**
    * Get all cases with no end dates
    *
+   * @param array $params
+   * @param array $excludeCaseIds
+   * @param bool $excludeDeleted
+   *
    * @return array of case and related data keyed on case id
    */
-  static function getUnclosedCases($params = array(), $excludeCaseIds = array(), $excludeDeleted = TRUE) {
+  static function getUnclosedCases($params = array(), $excludeCaseIds = array(), $excludeDeleted = TRUE, $includeClosed = FALSE) {
     //params from ajax call.
-    $where = array('( ca.end_date is null )');
+    $where = array($includeClosed ? '(1)' : '(ca.end_date is null)');
     if ($caseType = CRM_Utils_Array::value('case_type', $params)) {
       $where[] = "( civicrm_case_type.title LIKE '%$caseType%' )";
     }
@@ -1994,6 +2054,9 @@ SELECT civicrm_contact.id as casemanager_id,
       $search = ($config->includeWildCardInName) ? "%$sortName%" : "$sortName%";
       $where[] = "( sort_name LIKE '$search' )";
     }
+    if ($cid = CRM_Utils_Array::value('contact_id', $params)) {
+      $where[] = " c.id = $cid ";
+    }
     if (is_array($excludeCaseIds) &&
       !CRM_Utils_System::isNull($excludeCaseIds)
     ) {
@@ -2024,15 +2087,19 @@ SELECT civicrm_contact.id as casemanager_id,
             ca.id,
             ca.subject as case_subject,
             civicrm_case_type.title as case_type,
-            ca.start_date as start_date
+            ca.start_date as start_date,
+            ca.end_date as end_date,
+            ca.status_id
       FROM  civicrm_case ca INNER JOIN civicrm_case_contact cc ON ca.id=cc.case_id
  INNER JOIN  civicrm_contact c ON cc.contact_id=c.id
  INNER JOIN  civicrm_case_type ON ca.case_type_id = civicrm_case_type.id
      WHERE  {$whereClause}
-  ORDER BY  c.sort_name
+  ORDER BY  c.sort_name, ca.end_date
             {$limitClause}
 ";
     $dao = CRM_Core_DAO::executeQuery($query);
+    $statuses = CRM_Case_BAO_Case::buildOptions('status_id', 'create');
+
     $unclosedCases = array();
     while ($dao->fetch()) {
       if ($doFilterCases && !array_key_exists($dao->id, $filterCases)) {
@@ -2043,7 +2110,9 @@ SELECT civicrm_contact.id as casemanager_id,
         'case_type' => $dao->case_type,
         'contact_id' => $dao->contact_id,
         'start_date' => $dao->start_date,
+        'end_date' => $dao->end_date,
         'case_subject' => $dao->case_subject,
+        'case_status' => $statuses[$dao->status_id],
       );
     }
     $dao->free();
@@ -2051,6 +2120,12 @@ SELECT civicrm_contact.id as casemanager_id,
     return $unclosedCases;
   }
 
+  /**
+   * @param null $contactId
+   * @param bool $excludeDeleted
+   *
+   * @return null|string
+   */
   static function caseCount($contactId = NULL, $excludeDeleted = TRUE) {
     $whereConditions = array();
     if ($excludeDeleted) {
@@ -2253,12 +2328,14 @@ INNER JOIN  civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.cas
    * 1. Merge two duplicate contacts cases - follow CRM-5758 rules.
    * 2. Merge two cases of same contact - follow CRM-5598 rules.
    *
-   * @param int $mainContactId    contact id of main contact record.
-   * @param int $mainCaseId       case id of main case record.
-   * @param int $otherContactId   contact id of record which is going to merge.
-   * @param int $otherCaseId      case id of record which is going to merge.
+   * @param int $mainContactId contact id of main contact record.
+   * @param int $mainCaseId case id of main case record.
+   * @param int $otherContactId contact id of record which is going to merge.
+   * @param int $otherCaseId case id of record which is going to merge.
    *
-   * @return void.
+   * @param bool $changeClient
+   *
+   * @return integer|NULL
    * @static
    */
   static function mergeCases($mainContactId, $mainCaseId = NULL, $otherContactId = NULL,
@@ -3078,6 +3155,8 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
   /**
    * Function to check case configuration.
    *
+   * @param null $contactId
+   *
    * @return array $configured
    */
   static function isCaseConfigured($contactId = NULL) {
@@ -3252,8 +3331,10 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
    * @see CRM_Core_DAO::buildOptions
    *
    * @param String $fieldName
-   * @param String $context: @see CRM_Core_DAO::buildOptionsContext
-   * @param Array  $props: whatever is known about this dao object
+   * @param String $context : @see CRM_Core_DAO::buildOptionsContext
+   * @param Array $props : whatever is known about this dao object
+   *
+   * @return Array|bool
    */
   public static function buildOptions($fieldName, $context = NULL, $props = array()) {
     $className = __CLASS__;