function schools_form_alter

6.x schools.module schools_form_alter(&$form, $form_id)

Implements hook_form_alter

File

modules/schools/schools.module, line 761
Schools module.

Code

function schools_form_alter(&$form, $form_id) {


  $forms_to_alter = array(
    'admin_edit_degree_form',
    'admin_edit_group_form',
    'admin_edit_course_form',
    'user_edit_user_form',
    'user_edit_student_user_form',
  );


  // Add "school" dropdown to the degrees, groups, and courses form
  if (in_array($form_id, $forms_to_alter)) {

    fp_add_js(fp_get_module_path("schools") . '/js/schools.js');


    $db = get_global_database_handler();
    $school_id = 0; // default
    $entity_id = 0; // default


    if ($form_id == 'admin_edit_degree_form') {
      $entity_type = 'degree';
      $degree_id = @intval($form ['degree_id']['value']);
      $entity_id = $degree_id;
      if ($degree_id) {
        $school_id = $db->get_school_id_for_degree_id($degree_id, TRUE);
      }
      $weight = 1120;

      @$catalog_year = $form ['de_catalog_year']['value'];
      if ($catalog_year == "") {
        @$catalog_year = $form ['catalog_year']['value'];
      }
    }



    // Is this a "group" we are editing?
    if ($form_id == "admin_edit_group_form") {
      $entity_type = 'group';
      $weight = 45;
      $group_id = @$form ['group_id']['value'];
      $entity_id = $group_id;
      $school_id = $db->get_school_id_for_group_id($group_id, TRUE);

    }


    if ($form_id == 'admin_edit_course_form') {
      $entity_type = 'course';
      $course_id = @intval($form ['course_id']['value']);
      $entity_id = $course_id;
      $weight = 55;
      $school_id = $db->get_school_id_for_course_id($course_id, TRUE);

      // We also want to adjust the description for the "update for all years" checkbox.      
      $form ['all_years']['options']['yes'] = t('Update title, description, hour info, and school selection for all years of this course.');
    }


    if ($form_id == 'user_edit_user_form' || $form_id == 'user_edit_student_user_form') {
      $entity_type = 'user';
      $user_id = intval($form ['user_id']['value']);
      $entity_id = $user_id;
      $school_id = $db->get_school_id_for_user_id($user_id, TRUE);
      $weight = 95;
    } // user edit form





    $options = schools_get_schools_for_fapi(TRUE, TRUE, $entity_type, TRUE);



    $form ['schools_school'] = array(
      'type' => 'select',
      'label' => t('School:'),
      'options' => $options,
      'attributes' => array('onchange' => "return schoolsConfirmChangeSchool($school_id);"),
      'value' => $school_id,
      'hide_please_select' => TRUE,
      'weight' => $weight,
      'popup_description' => t('Please select a school which this @datatype belongs to.  Schools are defined under the Admin Console -> Configure Schools.', array('@datatype' => $entity_type)),
      'description' => t("<strong>Caution:</strong> changing this value could have unexpected consequences. If unsure what to do, set to Default."),
    );



    $form ['schools_entity_type'] = array(
      'type' => 'hidden',
      'value' => $entity_type,
    );

    $form ['schools_entity_id'] = array(
      'type' => 'hidden',
      'value' => $entity_id,
    );


    // We also need to add our custom submit handler so that our selection gets saved correctly.
    // Also, use unshift so it's the first thing we do.
    //$form['#submit_handlers'][] = 'schools_data_form_submit';
    if (!isset($form ['#submit_handlers'])) {
      $form ['#submit_handlers'] = array();
    }
    array_unshift($form ['#submit_handlers'], 'schools_data_form_submit');


    // If this is a NEW group, then don't show them anything!    
    if (($entity_type == 'group' && @$form ['group_id']['value'] === 'new') || ($entity_type == 'course' && @$form ['course_id']['value'] === 'new') || ($entity_type == 'user' && @intval($form ['user_id']['value']) === 0)) {
      $form ['schools_school'] = array(
        'type' => 'markup',
        'label' => t('School:'),
        'value' => t('<b>Please save this @dt at least once (to establish an ID in the database).  Once you have done this,
                          the School selection box will appear.</b>', array('@dt' => $entity_type)),
        'weight' => $weight,
      );


    }

  } // if in_array forms to alter




  // When we copy a degree, make sure we have a chance to interact (so we can
  // indicate the degree as well.
  if ($form_id == "admin_copy_degree_form") {

    $options = schools_get_schools_for_fapi(TRUE, TRUE, 'degree', TRUE);

    $form ['school_id'] = array(
      'type' => 'select',
      'label' => 'Source School:',
      'options' => $options,
      'hide_please_select' => TRUE,
      'weight' => 25,
      'popup_description' => t('Please select a school which this degree belongs to.  Schools are defined under the Admin Console -> Configure Schools.'),
      'description' => t("This must match the source degree.  If you do not know the source degree's school, or are unsure what to select, set to Default."),
    );

    $form ['destination_school_id'] = array(
      'type' => 'select',
      'label' => 'Destination School:',
      'options' => $options,
      'hide_please_select' => TRUE,
      'weight' => 35,
      'popup_description' => t('Please select a school which you wish to save the new degree into.  Schools are defined under the Admin Console -> Configure Schools.'),
      'description' => t("Please select a school which you wish to save the new degree into.  If you are unsure what to select, set to Default."),
    );



    //if (!isset($form['#validate_handlers'])) $form['#validate_handlers'] = array();
    //array_unshift($form['#validate_handlers'], 'schools_admin_copy_degree_form_validate');    
  }


  if ($form_id == 'admin_add_degree_form') {
    $options = schools_get_schools_for_fapi(TRUE, TRUE, 'degree', TRUE);

    $form ['school_id'] = array(
      'type' => 'select',
      'label' => 'School:',
      'options' => $options,
      'value' => $school_id,
      'hide_please_select' => TRUE,
      'weight' => 15,
      'popup_description' => t('Please select a school which this degree belongs to.  Schools are defined under the Admin Console -> Configure Schools.'),
      'description' => t("This must match the source degree.  If you do not know the source degree's school, or are unsure what to select, set to Default."),
    );
  }


  // Alter the "urgent message" form, to include new verbiage about how the main one is "global", and add new
  // boxes for each school.
  if ($form_id == 'admin_urgent_message_form') {

    $form ["mark_school"] = array(
      'value' => '<br><hr><p>' . t('<h2>Schools</h2>The following boxes are provided by the Schools module, so that you may display
                                  urgent messages to <b>students</b> and <b>faculty/staff</b>, based on the school they have been assigned to.') . "</p>",
    );


    $defs = schools_get_school_definitions();

    foreach ($defs as $school_id => $school_desc) {

      if ($school_id == 0) {
        continue;
      }

      $form ["urgent_msg_$school_id"] = array(
        "type" => "textarea",
        "label" => t("Urgent Message for users in %school_desc:", array("%school_desc" => $school_desc)),
        "value" => variable_get("urgent_msg_$school_id", ""),
        'description' => t("To delete this message, simple delete all the text in this box and save."),
      );

    }

    $form ['mark_below_school'] = array(
      'value' => '<br><hr><br>',
    );

  } // form is admin_urgent_message_form


  // Forms for which we should allow override values per school.
  $oforms = array('alerts_settings_form', "system_school_data_form", 'admin_advising_settings_form', "course_search_settings_form", 'audit_settings_form', 'calendar_appointment_settings_form');
  if (in_array($form_id, $oforms)) {
    // We want to add a "use default" checkbox at the top, which, if set, will disable all of the form elements.        
    fp_add_js(fp_get_module_path('schools') . '/js/schools.js');
    fp_add_css(fp_get_module_path('system') . '/css/style.css');
    fp_add_css(fp_get_module_path('schools') . '/css/style.css');

    $school_id = intval($form ['school_id']['value']);

    if ($school_id !== 0) {

      $school_name = schools_get_school_name_for_id($school_id);

      $form ['markup_school_use_default_school_values'] = array(
        'type' => 'markup',
        'value' => "<div>" . t("Since this is not the Default school, you have the option to <em>override</em> the Default school's values for each field.  
                                <br>If <strong>checked</strong>, the value you enter will be used for students, degrees, etc, belonging to this school (%school).  
                                <br>If <strong>unchecked</strong>, the value you enter will be ignored, and the Default school's value will be used instead.
                                <br><br>If unsure what to do, leave these 'Override' boxes unchecked.
                            ", array("%school" => $school_name)) . "</div>",
        'weight' => -100,
      );



      // Add a checkbox to every form element!
      $fields_to_edit = array('textfield', 'textarea', 'select', 'checkboxes');
      $fields_to_check_override = "";
      foreach ($form as $fieldname => $element) {
        if (in_array($element ['type'], $fields_to_edit)) {
          // Add an "override" checkbox for this field.           
          $checked = "";
          if (variable_get("school_override__$fieldname", "") === "yes") {
            $checked = "checked=checked";
          }

          if (!isset($form [$fieldname]['prefix'])) {
            $form [$fieldname]['prefix'] = "";
          }

          $n_fieldname = $fieldname;
          if ($element ['type'] == 'checkboxes') {
            $n_fieldname = 'inner-wrapper-' . $fieldname;
          }

          $css_id = md5($fieldname) . mt_rand(9, 9999);

          $form [$fieldname]['prefix'] .= "<span class='schools-override-cb'>
                              <label><input type='checkbox' name='school_override__$n_fieldname' class='school-override-cb' id='school_override__$css_id' value='yes' $checked>" . t("Override Default school value?") . "</label></span>";
          // Also add this fieldname to our "fields_to_check_override" string, so we can put it in a hidden textarea for later use in the _submit function.
          $fields_to_check_override .= $fieldname . ",";
        }
      } // foreach

      $form ['markup_fields_to_override'] = array(
        'value' => "<textarea name='fields_to_check_override' style='display:none;'>$fields_to_check_override</textarea>",
        'weight' => 999,
      );

      // We also need to add a custom submit handler, so we can save the "override" values.
      $form ['#submit_handlers'][] = 'schools_override_elements_form_submit';

    } // school != 0

  } // system_school_data_form




}