You are here

function lti_tool_provider_og_group_mapping_validate in LTI Tool Provider 7

Validation callback.

Parameters

array $form: The form.

array $form_state: The form state.

File

lti_tool_provider_og/lti_tool_provider_og.admin.inc, line 204
Admin forms and menu page callbacks for LTI Tool Provider OG module.

Code

function lti_tool_provider_og_group_mapping_validate($form, &$form_state) {
  $entity_bundle = $form_state['values']['bundle'];
  list($entity, $bundle) = explode(':', $entity_bundle);
  if ($entity == 'none') {
    return;
  }
  $check_isset_title = FALSE;
  $check_isset_context_id = FALSE;
  $check_repeat = FALSE;
  $fields = lti_tool_provider_og_retrieve_entity_field_types($entity_bundle);
  $counters = array();
  foreach (lti_tool_provider_context_mapping_details() as $key => $detail) {
    if (isset($form_state['values']['mapping'][$key]['attribute'])) {
      if ($detail != 'context_type' && $form_state['input']['mapping'][$key]['attribute'] == 'title') {
        $check_isset_title = TRUE;
      }
      if ($detail == 'context_id' && $form_state['input']['mapping'][$key]['attribute'] != 'none') {
        $check_isset_context_id = TRUE;
      }
      $value = $form_state['input']['mapping'][$key]['attribute'];
      if (isset($counters[$value])) {
        $counters[$value]++;
      }
      else {
        $counters[$value] = 0;
      }
    }
  }
  foreach ($counters as $counter => $count) {
    if ($counter != 'none' && $count > 1) {
      $check_repeat = TRUE;
      break;
    }
  }
  if (!$check_isset_context_id) {
    form_set_error('group_detail', t('You must map context_id variable to a field.'));
  }
  if ($check_repeat) {
    form_set_error('group_detail', t('You may not map more than one variable to the same field.'));
  }
  if ($entity == 'node' && isset($form_state['values']['create_course']) && $form_state['values']['create_course'] == 1 && !$check_isset_title) {
    form_set_error('group_detail', t('Title must be mapped from one of context_id, context_label or context_title.'));
  }
  return;
}