Merge pull request #5403 from eileenmcnaughton/CRM-16055-task-fixes
[civicrm-core.git] / CRM / Case / BAO / Case.php
index c51b09920316b9444f6e42866e8c0ed79049c706..8aeb7f3b14301625a5775122d71ae5c5f8ca9afb 100644 (file)
@@ -23,7 +23,7 @@
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  */
 
 /**
- * This class contains the functions for Case Management
+ * This class contains the functions for Case Management.
  *
  */
 class CRM_Case_BAO_Case extends CRM_Case_DAO_Case {
 
   /**
-   * Static field for all the case information that we can potentially export
+   * Static field for all the case information that we can potentially export.
    *
    * @var array
    */
   static $_exportableFields = NULL;
 
   /**
+   * Class constructor.
    */
   public function __construct() {
     parent::__construct();
@@ -63,9 +64,9 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case {
   }
 
   /**
-   * Takes an associative array and creates a case object
+   * Create a case object.
    *
-   * the function extract all the params it needs to initialize the create a
+   * The function extracts all the params it needs to initialize the create a
    * case object. the params array could contain additional unused name/value
    * pairs
    *
@@ -82,7 +83,7 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case {
 
   /**
    * Given the list of params in the params array, fetch the object
-   * and store the values in the values array
+   * and store the values in the values array.
    *
    * @param array $params
    *   Input parameters to find object.
@@ -91,7 +92,8 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case {
    * @param array $ids
    *   The array that holds all the db ids.
    *
-   * @return CRM_Case_BAO_Case|null the found object or null
+   * @return CRM_Case_BAO_Case|null
+   *   The found object or null
    */
   public static function &getValues(&$params, &$values, &$ids) {
     $case = new CRM_Case_BAO_Case();
@@ -107,10 +109,10 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case {
   }
 
   /**
-   * Takes an associative array and creates a case object
+   * Takes an associative array and creates a case object.
    *
    * @param array $params
-   *   (reference ) an assoc array of name/value pairs.
+   *   (reference) an assoc array of name/value pairs.
    *
    * @return CRM_Case_BAO_Case
    */
@@ -151,9 +153,10 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case {
   }
 
   /**
-   * Create case contact record
+   * Create case contact record.
    *
-   * @param array case_id, contact_id
+   * @param array $params
+   *   case_id, contact_id
    *
    * @return object
    */
@@ -193,11 +196,9 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case {
   }
 
   /**
-   * Delet case contact record
+   * Delete case contact record.
    *
-   * @param int case_id
-   *
-   * @return Void
+   * @param int $caseID
    */
   public static function deleteCaseContact($caseID) {
     $caseContact = new CRM_Case_DAO_CaseContact();
@@ -213,12 +214,18 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case {
   }
 
   /**
-   * convert associative array names to values
-   * and vice-versa.
+   * Convert associative array names to values and vice-versa.
    *
    * This function is used by both the web form layer and the api. Note that
    * the api needs the name => value conversion, also the view layer typically
    * requires value => name conversion
+   *
+   * @param array $defaults
+   * @param string $property
+   * @param array $lookup
+   * @param bool $reverse
+   *
+   * @return bool
    */
   public static function lookupValue(&$defaults, $property, &$lookup, $reverse) {
     $id = $property . '_id';
@@ -242,10 +249,9 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case {
   }
 
   /**
-   * Takes a bunch of params that are needed to match certain criteria and
-   * retrieves the relevant objects. We'll tweak this function to be more
-   * full featured over a period of time. This is the inverse function of
-   * create.  It also stores all the retrieved values in the default array
+   * Retrieve DB object based on input parameters.
+   *
+   * It also stores all the retrieved values in the default array.
    *
    * @param array $params
    *   (reference ) an assoc array of name/value pairs.
@@ -280,7 +286,7 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case {
   }
 
   /**
-   * Get the case subject for Activity
+   * Get the case subject for Activity.
    *
    * @param int $activityId
    *   Activity id.
@@ -318,7 +324,7 @@ WHERE civicrm_case.id = %1";
   }
 
   /**
-   * Delete the record that are associated with this case
+   * Delete the record that are associated with this case.
    * record are deleted from case
    *
    * @param int $caseId
@@ -373,7 +379,7 @@ WHERE civicrm_case.id = %1";
   }
 
   /**
-   * Enable disable case related relationships
+   * Enable disable case related relationships.
    *
    * @param int $caseId
    *   Case id.
@@ -399,7 +405,7 @@ WHERE civicrm_case.id = %1";
   }
 
   /**
-   * Delete the activities related to case
+   * Delete the activities related to case.
    *
    * @param int $activityId
    *   Id of the activity.
@@ -413,7 +419,7 @@ WHERE civicrm_case.id = %1";
   }
 
   /**
-   * Retrieve contact_id by case_id
+   * Retrieve contact_id by case_id.
    *
    * @param int $caseId
    *   ID of the case.
@@ -439,7 +445,7 @@ WHERE civicrm_case.id = %1";
   }
 
   /**
-   * Look up a case using an activity ID
+   * Look up a case using an activity ID.
    *
    * @param int $activityId
    *
@@ -461,7 +467,7 @@ WHERE civicrm_case.id = %1";
   }
 
   /**
-   * Retrieve contact names by caseId
+   * Retrieve contact names by caseId.
    *
    * @param int $caseId
    *   ID of the case.
@@ -504,7 +510,7 @@ WHERE civicrm_case.id = %1";
   }
 
   /**
-   * Retrieve case_id by contact_id
+   * Retrieve case_id by contact_id.
    *
    * @param int $contactID
    * @param bool $includeDeleted
@@ -701,8 +707,7 @@ LEFT JOIN civicrm_option_group aog ON aog.name='activity_type'
   }
 
   /**
-   * Retrieve cases related to particular contact or whole contact
-   * used in Dashboad and Tab
+   * Retrieve cases related to particular contact or whole contact used in Dashboard and Tab.
    *
    * @param bool $allCases
    *
@@ -953,7 +958,7 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
   }
 
   /**
-   * Get Case roles
+   * Get Case roles.
    *
    * @param int $contactID
    *   Contact id.
@@ -1011,7 +1016,7 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
   }
 
   /**
-   * Get Case Activities
+   * Get Case Activities.
    *
    * @param int $caseID
    *   Case id.
@@ -1348,7 +1353,7 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
       }
 
       if ($allowEdit) {
-        $values[$dao->id]['status'] = '<a class="crm-activity-status crm-activity-status-' . $dao->id . ' ' . $values[$dao->id]['class'] . ' crm-activity-change-status crm-editable-enabled" activity_id=' . $dao->id . ' current_status=' . $dao->status . ' case_id=' . $caseID . ' href="#" title=\'' . $statusTitle . '\'>' . $values[$dao->id]['status'] . '</a>';
+        $values[$dao->id]['status'] = '<div class="crmf-status_id crm-activity-status-' . $dao->id . ' ' . $values[$dao->id]['class'] . ' crm-editable" data-type="select" data-action="create" data-refresh="true">' . $values[$dao->id]['status'] . '</div>';
       }
     }
     $dao->free();
@@ -1357,7 +1362,7 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
   }
 
   /**
-   * Get Case Related Contacts
+   * Get Case Related Contacts.
    *
    * @param int $caseID
    *   Case id.
@@ -1580,7 +1585,7 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
   }
 
   /**
-   * Create an activity for a case via email
+   * Create an activity for a case via email.
    *
    * @param int $file
    *   Email sent.
@@ -1670,7 +1675,7 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
   }
 
   /**
-   * Retrieve the scheduled activity type and date
+   * Retrieve the scheduled activity type and date.
    *
    * @param array $cases
    *   Array of contact and case id.
@@ -1714,7 +1719,7 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
   }
 
   /**
-   * Combine all the exportable fields from the lower levels object
+   * Combine all the exportable fields from the lower levels object.
    *
    * @return array
    *   array of exportable Fields
@@ -1742,7 +1747,7 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
   }
 
   /**
-   * Restore the record that are associated with this case
+   * Restore the record that are associated with this case.
    *
    * @param int $caseId
    *   Id of the case to restore.
@@ -1812,7 +1817,7 @@ 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
+   * Convenience function to get both case contacts and global in one array.
    * @param int $caseId
    *
    * @return array
@@ -1856,7 +1861,7 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
     $where = $groupBy = ' ';
 
     if (!$caseID) {
-      return;
+      return NULL;
     }
 
     if ($latestDate) {
@@ -2000,7 +2005,7 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
    */
   public static function getCaseManagerContact($caseType, $caseId) {
     if (!$caseType || !$caseId) {
-      return;
+      return NULL;
     }
 
     $caseManagerContact = array();
@@ -2033,14 +2038,16 @@ SELECT civicrm_contact.id as casemanager_id,
   }
 
   /**
-   * Get all cases with no end dates
+   * Get all cases with no end dates.
    *
    * @param array $params
    * @param array $excludeCaseIds
    * @param bool $excludeDeleted
    *
+   * @param bool $includeClosed
+   *
    * @return array
-   *   case and related data keyed on case id
+   *   Case and related data keyed on case id
    */
   public static function getUnclosedCases($params = array(), $excludeCaseIds = array(), $excludeDeleted = TRUE, $includeClosed = FALSE) {
     //params from ajax call.
@@ -2318,6 +2325,9 @@ INNER JOIN  civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.cas
    * @see CRM_Dedupe_Merger::cpTables()
    *
    * TODO: use the 3rd $sqls param to append sql statements rather than executing them here
+   *
+   * @param int $mainContactId
+   * @param int $otherContactId
    */
   public static function mergeContacts($mainContactId, $otherContactId) {
     self::mergeCases($mainContactId, NULL, $otherContactId);
@@ -2341,7 +2351,7 @@ INNER JOIN  civicrm_case_contact ON ( civicrm_case.id = civicrm_case_contact.cas
    *
    * @return int|NULL
    */
-  static function mergeCases(
+  public static function mergeCases(
     $mainContactId, $mainCaseId = NULL, $otherContactId = NULL,
     $otherCaseId = NULL, $changeClient = FALSE) {
     $moveToTrash = TRUE;
@@ -2714,13 +2724,13 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
         }
       }
 
-      //create merge activity record.
+      //Create merge activity record. Source for merge activity is the logged in user's contact ID ($currentUserId).
       $activityParams = array(
         'subject' => $mergeActSubject,
         'details' => $mergeActSubjectDetails,
         'status_id' => array_search('Completed', $activityStatuses),
         'activity_type_id' => $mergeActType,
-        'source_contact_id' => $mainContactId,
+        'source_contact_id' => $currentUserId,
         'activity_date_time' => date('YmdHis'),
       );
 
@@ -2800,7 +2810,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
    * @param bool $checkComponent
    *   Do we need to check component enabled.
    *
-   * @return boolean
+   * @return bool
    */
   public static function checkPermission($activityId, $operation, $actTypeId = NULL, $contactId = NULL, $checkComponent = TRUE) {
     $allow = FALSE;
@@ -3057,7 +3067,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
   }
 
   /**
-   * Verify user has permission to access a case
+   * Verify user has permission to access a case.
    *
    * @param int $caseId
    * @param bool $denyClosed
@@ -3106,12 +3116,12 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
   }
 
   /**
-   * Check whether activity is a case Activity
+   * Check whether activity is a case Activity.
    *
    * @param int $activityID
    *   Activity id.
    *
-   * @return boolean
+   * @return bool
    */
   public static function isCaseActivity($activityID) {
     $isCaseActivity = FALSE;
@@ -3127,7 +3137,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
   }
 
   /**
-   * Get all the case type ids currently in use
+   * Get all the case type ids currently in use.
    *
    * @return array
    */
@@ -3151,7 +3161,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
   }
 
   /**
-   * Get all the case status ids currently in use
+   * Get all the case status ids currently in use.
    *
    * @return array
    */
@@ -3172,7 +3182,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
   }
 
   /**
-   * Get all the encounter medium ids currently in use
+   * Get all the encounter medium ids currently in use.
    *
    * @return array
    */
@@ -3243,7 +3253,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
   }
 
   /**
-   * Used during case component enablement and during ugprade
+   * Used during case component enablement and during ugprade.
    *
    * @return bool
    */
@@ -3281,6 +3291,8 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
   /**
    * Helper function, also used by the upgrade in case of error
    *
+   * @param string $section
+   *
    * @return string
    */
   public static function createCaseViewsQuery($section = 'upcoming') {
@@ -3352,7 +3364,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
   }
 
   /**
-   * Get the list of clients for a case
+   * Get the list of clients for a case.
    *
    * @param int $caseId
    *
@@ -3378,11 +3390,11 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
    *
    * @param string $fieldName
    * @param string $context
-   *   @see CRM_Core_DAO::buildOptionsContext.
+   * @see CRM_Core_DAO::buildOptionsContext
    * @param array $props
    *   Whatever is known about this dao object.
    *
-   * @return Array|bool
+   * @return array|bool
    */
   public static function buildOptions($fieldName, $context = NULL, $props = array()) {
     $className = __CLASS__;
@@ -3395,4 +3407,5 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')';
     }
     return CRM_Core_PseudoConstant::get($className, $fieldName, $params, $context);
   }
+
 }