function course_nodeapi in Course 6
Implements hook_nodeapi().
When a course is saved, handles changes to the course outline and the creation of external courses.
Renders the "take course" button on view.
File
- ./
course.module, line 795 - course.module Core functionality for Courses.
Code
function course_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
switch ($op) {
case 'view':
if (course_node_is_course($node)) {
// Render take course button.
$show = variable_get('course_take_course_button_show', array());
if ($teaser && !empty($show['teaser']) || $page && !empty($show['page'])) {
$node->content['course']['#value'] = course_render_button($node);
}
}
break;
case 'insert':
if (course_node_is_course($node)) {
if (!isset($node->course)) {
$node->course = array();
}
}
case 'update':
if (course_node_is_course($node)) {
$record = $node->course;
$record['nid'] = $node->nid;
// Support built-in non-cck date fields.
$builtin_dates = array(
'open',
'close',
);
// Check whether each element has a submitted value, and convert to a
// Unix timestamp before saving to the database.
foreach ($builtin_dates as $element) {
if (!empty($node->course[$element])) {
$record[$element] = !empty($node->validated) ? strtotime($node->course[$element]) : $node->course[$element];
}
}
// Add configurable dates to the node object for easy retrieval.
// Support both configurable cck and built-in non-cck date fields.
$cck_dates = array(
'open' => 'course_start_date_' . $node->type,
'close' => 'course_expiration_date_' . $node->type,
'live_from_date' => 'course_live_from_date_' . $node->type,
'live_to_date' => 'course_live_to_date_' . $node->type,
);
// Check whether each variable is set and the field exists on the
// content type. If so, load that field's value to the course object,
// overriding the coresponding column from the database.
foreach ($cck_dates as $key => $variable) {
$settings = @unserialize(variable_get($variable, array()));
$field_exists = module_exists('content') && isset($settings['field']) && content_fields($settings['field'], $node->type) != FALSE;
if ($field_exists) {
$value = $node->{$settings['field']}[0][$settings['value']];
if (!empty($value)) {
$date = new DateTime("{$value} UTC");
$value = $date
->format('U');
}
$record[$key] = $value;
}
}
$existing = db_result(db_query('SELECT 1 FROM {course_node} WHERE nid = %d', $node->nid));
$update = $existing ? array(
'nid',
) : array();
drupal_write_record('course_node', $record, $update);
// Support cloning.
course_handle_clone($node);
// Save the course objects - necessary for programmatic course creation.
if (isset($node->course['objects'])) {
$course = course_get_course($node);
course_save_objects($node->course['objects'], $course);
}
}
break;
case 'load':
if (course_node_is_course($node)) {
if ($course = db_fetch_array(db_query('SELECT * FROM {course_node} WHERE nid = %d', $node->nid))) {
// Attach additional module provided info to $node->course.
$hooks = module_invoke_all('course_nodeapi_extra', $node, 'load');
$load = array_merge($course, $hooks);
foreach ($load as $key => $data) {
$node->course[$key] = $data;
}
}
// Load the course outline to the node.
$sql = "SELECT * FROM {course_outline}\n WHERE nid = %d\n ORDER BY weight ASC";
$result = db_query($sql, $node->nid);
$objects = array();
while ($object = db_fetch_object($result)) {
foreach ($object as $key => $value) {
$objects[$object->coid]->{$key} = $value;
}
}
$node->course['objects'] = $objects;
}
break;
case 'delete':
if (course_node_is_course($node)) {
// Clean up course specific settings and enrollments when a course is
// deleted.
db_query("DELETE FROM {course_node} WHERE nid = %d", $node->nid);
db_query("DELETE FROM {course_enrolment} WHERE nid = %d", $node->nid);
}
break;
}
}