You are here

function course_uc_nodeapi in Course 6

Implements hook_nodeapi().

Add a course object to the course when a sell price is added. Remove fields we don't need for selling courses.

File

modules/course_uc/course_uc.module, line 37

Code

function course_uc_nodeapi($node, $op, $a3, $a4) {
  switch ($op) {
    case 'view':
      if (course_node_is_course($node) && isset($node->sell_price)) {
        $hides = array(
          'sell_price',
          'model',
          'list_price',
          'cost',
          'weight',
          'dimensions',
        );
        if (!($node->sell_price > 0)) {
          $hides[] = 'display_price';
        }
        foreach ($hides as $hide) {
          unset($node->content[$hide]);
        }
      }
      break;
    case 'insert':
    case 'update':
      if (course_node_is_course($node) && isset($node->sell_price) && $node->sell_price > 0) {

        // Course has a sell price.
        $course = course_get_course($node);
        foreach ($course
          ->getObjects() as $courseObject) {
          if ($courseObject
            ->getComponent() == 'payment') {
            return;
          }
        }

        // If we are here, the course did not have a payment object.
        $newObject = course_get_course_object('course_uc', 'payment');
        $newObject
          ->setCourse($course);
        $newObject
          ->setCourse($node->nid);
        $newObject
          ->setModule('course_uc');
        $newObject
          ->setComponent('payment');
        $newObject
          ->setInstanceId($node->nid);
        $newObject
          ->setOption('title', 'Payment required');
        $newObject
          ->setOption('required', TRUE);
        $newObject
          ->setOption('enabled', TRUE);
        $newObject
          ->setOption('hidden', TRUE);
        $newObject
          ->save();
      }
  }
}