<commit_before><?php

class CRM_SmsConversation_BAO_Contact extends CRM_SmsConversation_DAO_Contact {

  /**
   * Create a new SmsConversationContact based on array-data
   *
   * @param array $params key-value pairs
   *
   * @return CRM_SmsConversation_DAO_Contact|NULL
   */
  public static function create($params) {
    $className = 'CRM_SmsConversation_DAO_Contact';
    $entityName = 'SmsConversationContact';
    $hook = empty($params['id']) ? 'create' : 'edit';

    CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
    $instance = new $className();
    $instance->copyValues($params);
    $instance->save();
    CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);

    return $instance;
  }

  /**
   * Get the current conversation for the contact
   *
   * @param $contactId
   *
   * @return array|bool
   */
  static function getCurrentConversation($contactId) {
    // Get "In Progress" conversation for contact
    // FIXME: There should not be more than one, if there is we are getting the oldest one.  This behaviour may need to change.
    $inProgressId = CRM_Core_PseudoConstant::getKey('CRM_SmsConversation_BAO_Contact', 'status_id', 'In Progress');
    $convContact = civicrm_api3('SmsConversationContact', 'get', [
      'contact_id' => $contactId,
      'status_id' => $inProgressId,
      'options' => ['limit' => 1, 'sort' => "id ASC"],
    ]);

    if (empty($convContact['is_error']) && !empty($convContact['count'])) {
      return $convContact['values'];
    }
    else {
      return FALSE;
    }
  }

  /**
   * Get the next scheduled conversation for a contact
   *
   * @param $contactId
   *
   * @return array|bool
   */
  static function getNextScheduledConversation($contactId) {
    // Get next "Scheduled" conversation for contact
    // We select the earliest by scheduled date, then earliest by Id and select the first one
    $scheduledId = CRM_Core_PseudoConstant::getKey('CRM_SmsConversation_BAO_Contact', 'status_id', 'Scheduled');
    $convContact = civicrm_api3('SmsConversationContact', 'get', [
      'sequential' => 1,
      'contact_id' => $contactId,
      'status_id' => $scheduledId,
      'options' => ['limit' => 1, 'sort' => "scheduled_date ASC,id ASC"],
      'scheduled_date' => array('<=' => "now"),
    ]);

    if (empty($convContact['is_error']) && !empty($convContact['count'])) {
      return $convContact['values'][0];
    }
    else {
      return FALSE;
    }
  }

  /**
   * Start a conversation with contact
   *
   * @param $contactId
   * @param $conversationId
   *
   * @return SmsConversation.contact_id or FALSE
   */
  static function startConversation($contactId, $id = NULL) {
    // Don't allow another conversation to start
    if (CRM_SmsConversation_BAO_Contact::getCurrentConversation($contactId)) {
      throw new CRM_Core_Exception('This contact already has a conversation in progress');
    }

    // Get conversation contact
    if (!empty($id)) {
      // Use passed in conversation contact id
      $convContact = civicrm_api3('SmsConversationContact', 'get', [
        'sequential' => 1,
        'id' => $id,
      ]);
      if (empty($convContact['is_error']) && !empty($convContact['count'])) {
        $convContact =  $convContact['values'][0];
      }
    }
    if (empty($convContact)) {
      // If we don't already have a conversation to work with, get one
      $convContact = CRM_SmsConversation_BAO_Contact::getNextScheduledConversation($contactId);
    }

    if (empty($convContact)) {
      throw new CRM_Core_Exception('No scheduled conversations found');
    }

    if (!empty($convContact['is_error'])) {
      return FALSE;
    }

    // Get the conversation
    $conversation = CRM_SmsConversation_BAO_Conversation::getConversation($convContact['conversation_id']);
    if (!$conversation['is_active']) {
      return FALSE;
    }

    // Get the question
    $question = CRM_SmsConversation_BAO_Question::getQuestion($conversation['start_question_id']);
    // Update conversation contact status
    CRM_SmsConversation_BAO_Contact::updateStatus($convContact['id'], 'In Progress');

    $convContact = CRM_SmsConversation_BAO_Contact::getCurrentConversation($contactId);
    // Ask the question
    if (CRM_SmsConversation_BAO_Question::ask($question['id'], $contactId, reset($convContact))) {
      return $convContact['id'];
    }
    else {
      return FALSE;<commit_msg>::getCurrentConversation should return the conversation, not an array of conversations<commit_after><?php

class CRM_SmsConversation_BAO_Contact extends CRM_SmsConversation_DAO_Contact {

  /**
   * Create a new SmsConversationContact based on array-data
   *
   * @param array $params key-value pairs
   *
   * @return CRM_SmsConversation_DAO_Contact|NULL
   */
  public static function create($params) {
    $className = 'CRM_SmsConversation_DAO_Contact';
    $entityName = 'SmsConversationContact';
    $hook = empty($params['id']) ? 'create' : 'edit';

    CRM_Utils_Hook::pre($hook, $entityName, CRM_Utils_Array::value('id', $params), $params);
    $instance = new $className();
    $instance->copyValues($params);
    $instance->save();
    CRM_Utils_Hook::post($hook, $entityName, $instance->id, $instance);

    return $instance;
  }

  /**
   * Get the current conversation for the contact
   *
   * @param $contactId
   *
   * @return array|bool
   */
  static function getCurrentConversation($contactId) {
    // Get "In Progress" conversation for contact
    // FIXME: There should not be more than one, if there is we are getting the oldest one.  This behaviour may need to change.
    $inProgressId = CRM_Core_PseudoConstant::getKey('CRM_SmsConversation_BAO_Contact', 'status_id', 'In Progress');
    $convContact = civicrm_api3('SmsConversationContact', 'get', [
      'contact_id' => $contactId,
      'status_id' => $inProgressId,
      'options' => ['limit' => 1, 'sort' => "id ASC"],
    ]);

    if (empty($convContact['is_error']) && !empty($convContact['count'])) {
      return $convContact['values'][$convContact['id']];
    }
    else {
      return FALSE;
    }
  }

  /**
   * Get the next scheduled conversation for a contact
   *
   * @param $contactId
   *
   * @return array|bool
   */
  static function getNextScheduledConversation($contactId) {
    // Get next "Scheduled" conversation for contact
    // We select the earliest by scheduled date, then earliest by Id and select the first one
    $scheduledId = CRM_Core_PseudoConstant::getKey('CRM_SmsConversation_BAO_Contact', 'status_id', 'Scheduled');
    $convContact = civicrm_api3('SmsConversationContact', 'get', [
      'sequential' => 1,
      'contact_id' => $contactId,
      'status_id' => $scheduledId,
      'options' => ['limit' => 1, 'sort' => "scheduled_date ASC,id ASC"],
      'scheduled_date' => array('<=' => "now"),
    ]);

    if (empty($convContact['is_error']) && !empty($convContact['count'])) {
      return $convContact['values'][0];
    }
    else {
      return FALSE;
    }
  }

  /**
   * Start a conversation with contact
   *
   * @param $contactId
   * @param $conversationId
   *
   * @return SmsConversation.contact_id or FALSE
   */
  static function startConversation($contactId, $id = NULL) {
    // Don't allow another conversation to start
    if (CRM_SmsConversation_BAO_Contact::getCurrentConversation($contactId)) {
      throw new CRM_Core_Exception('This contact already has a conversation in progress');
    }

    // Get conversation contact
    if (!empty($id)) {
      // Use passed in conversation contact id
      $convContact = civicrm_api3('SmsConversationContact', 'get', [
        'sequential' => 1,
        'id' => $id,
      ]);
      if (empty($convContact['is_error']) && !empty($convContact['count'])) {
        $convContact =  $convContact['values'][0];
      }
    }
    if (empty($convContact)) {
      // If we don't already have a conversation to work with, get one
      $convContact = CRM_SmsConversation_BAO_Contact::getNextScheduledConversation($contactId);
    }

    if (empty($convContact)) {
      throw new CRM_Core_Exception('No scheduled conversations found');
    }

    if (!empty($convContact['is_error'])) {
      return FALSE;
    }

    // Get the conversation
    $conversation = CRM_SmsConversation_BAO_Conversation::getConversation($convContact['conversation_id']);
    if (!$conversation['is_active']) {
      return FALSE;
    }

    // Get the question
    $question = CRM_SmsConversation_BAO_Question::getQuestion($conversation['start_question_id']);
    // Update conversation contact status
    CRM_SmsConversation_BAO_Contact::updateStatus($convContact['id'], 'In Progress');

    $convContact = CRM_SmsConversation_BAO_Contact::getCurrentConversation($contactId);
    // Ask the question
    if (CRM_SmsConversation_BAO_Question::ask($question['id'], $contactId, $convContact)) {
      return $convContact['id'];
    }
    else {
      return FALSE;
{'fraction_non_alphanumeric': 0.1355629877369008, 'fraction_numerical': 0.002006688963210702, 'mean_word_length': 3.6675338189386055, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4133236', 'n_tokens_mistral': 2763, 'n_tokens_neox': 2708, 'n_words': 620}