You are here

function course_outline_overview_form in Course 7

Same name and namespace in other branches
  1. 6 includes/course.outline.inc \course_outline_overview_form()
  2. 7.2 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, &$form_state) {
  $form = array();

  // 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('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 object" was clicked.
  if (isset($form_state['values']['op']) && $form_state['values']['op'] == t('Add object') && !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 {
    if ($objects = $course
      ->getObjects()) {

      // Great.
    }
    else {
      $objects = array();
    }
  }

  // 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'] = t('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', FALSE);
  }
  $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) {
      if ($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.
        // @kludge 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'),
        );
        if (empty($handlers[$courseObject
          ->getOption('module')][$courseObject
          ->getOption('object_type')])) {
          $show_object_name = t('Missing CourseObject handler for <br/><i>@m/@t</i>', array(
            '@m' => $courseObject
              ->getOption('module'),
            '@t' => $courseObject
              ->getOption('object_type'),
          ));
        }
        else {
          $show_object_name = $handlers[$courseObject
            ->getOption('module')][$courseObject
            ->getOption('object_type')]['name'] . '<br/><small><i>' . ucwords(str_replace('_', ' ', $courseObject
            ->getOption('module'))) . '</i></small>';
        }
        $rform['object_type_show'] = array(
          '#type' => 'markup',
          '#markup' => filter_xss_admin($show_object_name),
        );
        $cform['objects'][$uniqid] = $rform;
      }
    }
  }

  // Add object button and select box for new objects.
  $object_types = array(
    '' => '- ' . t('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 = call_user_func(array(
          $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'])) {
          $object_types[$module . '-' . $key] = $object_info['name'];
        }
      }
    }
  }
  $form['more'] = array(
    '#type' => 'markup',
    '#prefix' => '<div class="container-inline">',
    '#suffix' => '</div>',
  );
  $form['more']['add_another'] = array(
    '#type' => 'button',
    '#value' => t('Add object'),
    '#ajax' => array(
      'method' => 'replace',
      'wrapper' => 'course-outline-wrapper',
      'callback' => 'course_outline_overview_form_rebuild',
    ),
    '#weight' => 20,
  );

  // Sort course object types
  asort($object_types);
  $form['more']['object_type'] = array(
    '#type' => 'select',
    '#options' => $object_types,
    '#weight' => 10,
  );
  $form['actions']['#type'] = 'actions';

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

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