Merge pull request #1387 from deepak-srivastava/logrev
[civicrm-core.git] / CRM / Activity / BAO / Activity.php
index be6f8be691be7e7ec3921061061373f62166aa59..e9e5e5ad9fd6036d9057093f4b36cc0141981da9 100644 (file)
@@ -473,7 +473,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
       }
     }
 
-    // write to changelog before transation is committed/rolled
+    // write to changelog before transaction is committed/rolled
     // back (and prepare status to display)
     if (CRM_Utils_Array::value('id', $params)) {
       $logMsg = "Activity (id: {$result->id} ) updated with ";
@@ -642,11 +642,11 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
   }
 
   /**
-   * function to get the list Actvities
+   * function to get the list Activities
    *
    * @param array   $input            array of parameters
    *    Keys include
-   *    - contact_id  int            contact_id whose activties we want to retrieve
+   *    - contact_id  int            contact_id whose activities we want to retrieve
    *    - offset      int            which row to start from ?
    *    - rowCount    int            how many rows to fetch
    *    - sort        object|array   object or array describing sort order for sql query.
@@ -655,7 +655,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
    *    - context     string         page on which selector is build
    *    - activity_type_id int|string the activitiy types we want to restrict by
    *
-   * @return array (reference)      $values the relevant data object values of open activitie
+   * @return array (reference)      $values the relevant data object values of open activities
    *
    * @access public
    * @static
@@ -688,13 +688,20 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
 
     $sql = "CREATE TEMPORARY TABLE {$activityTempTable} ( ";
     $insertValueSQL = array();
+    // The activityTempTable contains the sorted rows
+    // so in order to maintain the sort order as-is we add an auto_increment
+    // field; we can sort by this later to ensure the sort order stays correct.
+    $sql .= " fixed_sort_order INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,";
     foreach ($tableFields as $name => $desc) {
       $sql .= "$name $desc,\n";
       $insertValueSQL[] = $name;
     }
 
+    // add unique key on activity_id just to be sure
+    // this cannot be primary key because we need that for the auto_increment
+    // fixed_sort_order field
     $sql .= "
-          PRIMARY KEY ( activity_id )
+          UNIQUE KEY ( activity_id ) 
         ) ENGINE=HEAP DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci
         ";
 
@@ -717,6 +724,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
     }
 
     if (empty($order)) {
+      // context = 'activity' in Activities tab.
       $order = (CRM_Utils_Array::value('context', $input) == 'activity') ? " ORDER BY tbl.activity_date_time desc " : " ORDER BY tbl.status_id asc, tbl.activity_date_time asc ";
     }
 
@@ -770,6 +778,7 @@ WHERE      c.is_deleted = 0
     CRM_Core_DAO::executeQuery($query);
 
     // step 3: Combine all temp tables to get final query for activity selector
+    // sort by the original sort order, stored in fixed_sort_order
     $query = "
 SELECT     {$activityTempTable}.*,
            {$activityContactTempTable}.contact_id,
@@ -777,6 +786,7 @@ SELECT     {$activityTempTable}.*,
            {$activityContactTempTable}.contact_name
 FROM       {$activityTempTable}
 INNER JOIN {$activityContactTempTable} on {$activityTempTable}.activity_id = {$activityContactTempTable}.activity_id
+ORDER BY    fixed_sort_order
         ";
 
 
@@ -921,11 +931,11 @@ INNER JOIN {$activityContactTempTable} on {$activityTempTable}.activity_id = {$a
    *
    * @param array   $input            array of parameters
    *    Keys include
-   *    - contact_id  int            contact_id whose activties we want to retrieve
+   *    - contact_id  int            contact_id whose activities we want to retrieve
    *    - admin       boolean        if contact is admin
    *    - caseId      int            case ID
    *    - context     string         page on which selector is build
-   *    - activity_type_id int|string the activitiy types we want to restrict by
+   *    - activity_type_id int|string the activity types we want to restrict by
    *
    * @return int   count of activities
    *
@@ -957,12 +967,12 @@ LEFT JOIN   civicrm_case_activity ON ( civicrm_case_activity.activity_id = tbl.a
    *
    * @param array   $input            array of parameters
    *    Keys include
-   *    - contact_id  int            contact_id whose activties we want to retrieve
+   *    - contact_id  int            contact_id whose activities we want to retrieve
    *    - admin       boolean        if contact is admin
    *    - caseId      int            case ID
    *    - context     string         page on which selector is build
    *    - count       boolean        are we interested in the count clause only?
-   *    - activity_type_id int|string the activitiy types we want to restrict by
+   *    - activity_type_id int|string the activity types we want to restrict by
    *
    * @return int   count of activities
    *
@@ -1136,8 +1146,8 @@ INNER JOIN civicrm_contact contact ON ac.contact_id = contact.id
    * @param int    $userID       use this userID if set
    * @param string $from
    * @param array  $attachments  the array of attachments if any
-   * @param string $cc           cc recepient
-   * @param string $bcc          bcc recepient
+   * @param string $cc           cc recipient
+   * @param string $bcc          bcc recipient
    * @param array $contactIds    contact ids
    *
    * @return array               ( sent, activityId) if any email is sent and activityId
@@ -1431,7 +1441,8 @@ INNER JOIN civicrm_contact contact ON ac.contact_id = contact.id
         $tokenText,
         $tokenHtml,
         $smsParams,
-        $activityID
+        $activityID,
+        $userID
       );
 
       if (PEAR::isError($sendResult)) {
@@ -1471,7 +1482,8 @@ INNER JOIN civicrm_contact contact ON ac.contact_id = contact.id
     &$tokenText,
     &$tokenHtml,
     $smsParams = array(),
-    $activityID
+    $activityID,
+    $userID = null
   ) {
     $toDoNotSms = "";
     $toPhoneNumber = "";
@@ -1507,7 +1519,7 @@ INNER JOIN civicrm_contact contact ON ac.contact_id = contact.id
     $smsParams['parent_activity_id'] = $activityID;
 
     $providerObj = CRM_SMS_Provider::singleton(array('provider_id' => $smsParams['provider_id']));
-    $sendResult = $providerObj->send($recipient, $smsParams, $message, NULL);
+    $sendResult = $providerObj->send($recipient, $smsParams, $message, NULL, $userID);
     if (PEAR::isError($sendResult)) {
       return $sendResult;
     }
@@ -1735,7 +1747,7 @@ WHERE      activity.id IN ($activityIds)";
   /**
    * Function to add activity for Membership/Event/Contribution
    *
-   * @param object  $activity   (reference) perticular component object
+   * @param object  $activity   (reference) particular component object
    * @param string  $activityType for Membership Signup or Renewal
    *
    *
@@ -1776,7 +1788,7 @@ SELECT  display_name
       $component = 'Membership';
     }
     elseif ($activity->__table == 'civicrm_participant') {
-      $event = CRM_Event_BAO_Event::getEvents(TRUE, $activity->event_id, TRUE, FALSE);
+      $event = CRM_Event_BAO_Event::getEvents(1, $activity->event_id, TRUE, FALSE);
 
       $roles = CRM_Event_PseudoConstant::participantRole();
       $status = CRM_Event_PseudoConstant::participantStatus();
@@ -1846,11 +1858,11 @@ SELECT  display_name
   }
 
   /**
-   * Function to get Parent activity for currently viewd activity
+   * Function to get Parent activity for currently viewed activity
    *
    * @param int  $activityId   current activity id
    *
-   * @return int $parentId  Id of parent acyivity otherwise false.
+   * @return int $parentId  Id of parent activity otherwise false.
    * @access public
    */
   static function getParentActivity($activityId) {
@@ -1877,7 +1889,7 @@ SELECT  display_name
    *
    * @param int  $activityId   current activity id
    *
-   * @return int $params  count of prior acyivities otherwise false.
+   * @return int $params  count of prior activities otherwise false.
    * @access public
    */
   static function getPriorCount($activityID) {
@@ -1910,11 +1922,12 @@ AND id < {$activityID}
   }
 
   /**
-   * Function to get all prior activities of currently viewd activity
+   * Function to get all prior activities of currently viewe
+   * d activity
    *
    * @param int  $activityId   current activity id
    *
-   * @return array $result  prior acyivities info.
+   * @return array $result  prior activities info.
    * @access public
    */
   static function getPriorAcitivities($activityID, $onlyPriorRevisions = FALSE) {