You are here

function course_outline_overview_form in Course 6

Same name and namespace in other branches
  1. 7.2 includes/course.outline.inc \course_outline_overview_form()
  2. 7 includes/course.outline.inc \course_outline_overview_form()

Form constructor for course outline form.

See also

course_menu()

_course_outline_object_form()

theme_course_outline_overview_form()

1 string reference to 'course_outline_overview_form'
course_menu in ./course.module
Implements hook_menu().

File

includes/course.outline.inc, line 16
course_outline.inc

Code

function course_outline_overview_form(&$form_state) {
  $form = array();

  // Get around ahah_helper PHP notices.
  // @see http://drupal.org/node/1371792
  $form['#action'] = '';
  $form['#id'] = '';

  // Register the form with ahah_helper so we can use it. Also updates
  // $form_state['storage'] to ensure it contains the latest values that have
  // been entered, even when the form item has temporarily been removed from
  // the form. So if a form item *once* had a value, you *always* can retrieve
  // it.
  ahah_helper_register($form, $form_state);

  // Determine the default value of the 'usage' select. When nothing is stored
  // in $form_state['storage'] yet, it's the form hasn't been submitted yet,
  // thus it's the first time the form is being displayed.
  // Load the modal library and add the modal javascript.
  ctools_include('ajax');
  ctools_include('modal');
  ctools_include('dependent');
  ctools_include('object-cache');
  ctools_modal_add_js();

  // Wrapper for objects and more button.
  $form['#tree'] = TRUE;
  $form['#prefix'] = '<div class="clear-block" id="course-outline-wrapper">';
  $form['#suffix'] = '</div>';

  // Shortcut.
  $cform =& $form['course_outline'];
  if (isset($form_state['values']['nid'])) {
    $node = node_load($form_state['values']['nid']);
  }
  else {
    $node = node_load(arg(1));
  }
  $course = course_get_course($node);

  // Check if "Add another" was clicked.
  if (isset($form_state['values']['op']) && $form_state['values']['op'] == 'Add another' && !empty($form_state['values']['more']['object_type'])) {

    // Ensure that we cached the course.
    course_editing_start($course);

    // Create a new course object in the session, and let the rest of the form
    // builder handle it.
    $obj_uniqid = uniqid('course_object_');
    $_SESSION['course'][$node->nid]['editing'][$obj_uniqid] = array();

    // Populate temporary course object, save it in the session.
    $new = array();
    $new['weight'] = 0;

    // Get highest weight and add to it.
    if (isset($form_state['values']['course_outline']['objects'])) {
      foreach ($form_state['values']['course_outline']['objects'] as $key => $object) {
        if ($object['weight'] >= $new['weight']) {
          $new['weight'] = $object['weight'] + 1;
        }
      }
    }
    $new['nid'] = $node->nid;
    $new['coid'] = $obj_uniqid;
    list($new['module'], $new['object_type']) = explode('-', $form_state['values']['more']['object_type']);
    $_SESSION['course'][$node->nid]['editing'][$obj_uniqid] = $new;
  }
  $form['nid']['#type'] = 'hidden';
  $form['nid']['#value'] = $node->nid;

  // Grab initial list of objects from DB or session.
  if (!empty($_SESSION['course'][$node->nid]['editing'])) {
    $objects = $_SESSION['course'][$node->nid]['editing'];
  }
  else {
    $objects = $node->course['objects'];
  }

  // Sort list of objects we pulled from session or DB by weight for proper
  // display.
  uasort($objects, '_course_outline_overview_form_cmp_function');
  $cform['#title'] = 'Course objects';
  $form['#theme'] = 'course_outline_overview_form';
  if (!empty($_SESSION['course'][$node->nid]['editing'])) {
    drupal_set_message('Changes to this course have not yet been saved.', 'warning');
  }
  $handlers = course_get_handlers('object');

  // Wrapper for just the objects.
  $cform['objects']['#tree'] = TRUE;
  $object_counts = array();
  if (count($objects)) {
    foreach (array_keys($objects) as $uniqid) {
      $courseObject = course_get_course_object_by_id($uniqid);
      $rform = _course_outline_object_form($courseObject);

      // Keep track of how many of each course object we have.
      // @todo probably some simpler way to do this effectively
      if (!isset($object_counts[$courseObject
        ->getModule()][$courseObject
        ->getComponent()])) {
        $object_counts[$courseObject
          ->getModule()][$courseObject
          ->getComponent()] = 1;
      }
      else {
        $object_counts[$courseObject
          ->getModule()][$courseObject
          ->getComponent()]++;
      }

      // Don't allow user to change type of object.
      $rform['object_type'] = array(
        '#type' => 'hidden',
        '#value' => $courseObject
          ->getOption('object_type'),
      );
      $rform['object_type_show'] = array(
        '#type' => 'markup',
        '#value' => filter_xss_admin($handlers[$courseObject
          ->getOption('module')][$courseObject
          ->getOption('object_type')]['name'] . '<br/><small><i>' . ucwords(str_replace('_', ' ', $courseObject
          ->getOption('module'))) . '</i></small>'),
      );
      $cform['objects'][$uniqid] = $rform;
    }
  }

  // Add another button and select box for new objects.
  $object_types = array(
    '' => '- select object -',
  );
  foreach ($handlers as $module => $object_definitions) {
    if ($object_definitions) {
      foreach ($object_definitions as $key => $object_info) {
        $class = $object_info['class'];
        $max_object_count = $class::getMaxOccurences();
        $under_limit = !$max_object_count || !(isset($object_counts[$module][$key]) && $object_counts[$module][$key] >= $max_object_count);
        if ($under_limit && empty($object_info['legacy'])) {
          $module_string = ucwords(str_replace('_', ' ', $module));
          $object_types[$module . '-' . $key] = $module_string . ': ' . $object_info['name'];
        }
      }
    }
  }
  $form['more'] = array(
    '#type' => 'markup',
    '#prefix' => '<div class="container-inline">',
    '#suffix' => '</div>',
  );
  $form['more']['add_another'] = array(
    '#type' => 'submit',
    '#value' => 'Add another',
    '#ahah' => array(
      'event' => 'click',
      'method' => 'replace',
      'path' => ahah_helper_path(AHAH_HELPER_PARENTS_ENTIRE_FORM),
      'wrapper' => 'course-outline-wrapper',
    ),
    '#submit' => array(
      'ahah_helper_submit',
    ),
  );
  $form['more']['object_type'] = array(
    '#type' => 'select',
    '#options' => $object_types,
  );

  // Submit and reset buttons.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Save',
    '#submit' => array(
      'course_outline_overview_form_submit',
    ),
  );
  if (!empty($_SESSION['course'][$node->nid]['editing'])) {
    $form['reset'] = array(
      '#type' => 'submit',
      '#value' => 'Reset',
      '#submit' => array(
        'course_outline_overview_form_reset',
      ),
    );
  }
  $cform['objects']['#element_validate'] = array(
    '_course_outline_overview_validate_objects',
  );

  //return for form on tab
  return $form;
}