View source
<?php
class CourseObjectBook extends CourseObjectNode {
public static function getNodeInstances($node) {
if (!empty($node->book['bid'])) {
return array(
$node->book['bid'],
);
}
}
function getNodeTypes() {
if (user_access('administer book outlines')) {
return array_keys(node_type_get_names());
}
else {
return variable_get('book_allowed_types', array(
'book',
));
}
}
public function create($node = NULL) {
$node = new stdClass();
$node->type = $this
->getOption('node_type');
$node->book['bid'] = 'new';
$node->book['mlid'] = NULL;
parent::create($node);
}
function optionsDefinition() {
$defaults = parent::optionsDefinition();
$defaults['node_type'] = variable_get('course_book_default_node_type', 'book');
$defaults['book_tracking'] = 'all';
$defaults['outline_list_item_type'] = 'active_tree';
return $defaults;
}
function optionsForm(&$form, &$form_state) {
$config = $this
->getOptions();
parent::optionsForm($form, $form_state);
$form['book_tracking'] = array(
'#title' => t('Completion criteria'),
'#type' => 'select',
'#options' => array(
'one' => t('View any page'),
'all' => t('View all pages'),
),
'#default_value' => $config['book_tracking'],
);
if ($this
->getCourse()
->getNode()->course['outline'] == 'course') {
$form['outline_list_item_type'] = array(
'#title' => t('Course outline list item type'),
'#type' => 'select',
'#options' => array(
'all_pages' => t('All book pages as an expanded, nested list'),
'active_tree' => t('Only the active book menu tree items, with a count indicator'),
'count' => t('A count indicator only'),
),
'#default_value' => $config['outline_list_item_type'],
);
}
if (!module_exists('node_access_book') && user_access('administer modules')) {
$form['node']['private']['#description'] .= '<br/><span style="color: #d00">If using "private", you should install <a href="http://drupal.org/project/node_access_book">Node access book</a> to ensure that book sub-pages obey the content access on the book parent.';
}
}
function grade($user) {
if (course_book_count($this
->getInstanceId()) == 0) {
$this
->getFulfillment($user)
->setComplete(1)
->save();
return;
}
if ($this
->getOption('book_tracking') == 'all') {
$mlids = array_keys(book_toc($this
->getInstanceId(), 99));
$fulfillment = $this
->getFulfillment($user)
->getOption('book_fulfillment');
$viewed = $fulfillment ? array_keys(array_filter($fulfillment)) : array();
if (!array_diff($mlids, $viewed)) {
$this
->getFulfillment($user)
->setComplete(1)
->save();
}
}
elseif ($this
->getOption('book_tracking') == 'one') {
$this
->getFulfillment($user)
->setComplete(1)
->save();
}
}
public function overrideNavigation() {
$links = parent::overrideNavigation();
$node = node_load(arg(1));
if (isset($node->book)) {
$book_link = $node->book;
if ($prev = book_prev($book_link)) {
$links['prev'] = l(t('Previous'), $prev['href']);
}
if ($next = book_next($book_link)) {
$links['next'] = l(t('Next'), $next['href']);
}
}
return $links;
}
public function overrideOutlineListItem(&$item) {
if ($this
->getCourse()
->getNode()->course['outline'] == 'course') {
$type = $this
->getOption('outline_list_item_type');
course_book_override_outline_list_item($item, $this, $type);
}
}
public function getCloneAbility() {
return t('%title will only clone the first page.', array(
'%title' => $this
->getTitle(),
));
}
public function save() {
parent::save();
if ($this
->hasNodePrivacySupport() && $this
->getOption('private') && module_exists('node_access_book')) {
$flat = array();
$tree = menu_tree_all_data($this
->getNode()->book['menu_name']);
_book_flatten_menu($tree, $flat);
foreach ($flat as $item) {
$nid = str_replace('node/', '', $item['link_path']);
$node = node_load($nid);
$settings = content_access_get_per_node_settings($node);
$settings['view'] = array();
content_access_save_per_node_settings($node, $settings);
node_access_acquire_grants($node);
}
}
}
public function freeze() {
$ice = parent::freeze();
unset($ice->node->book['bid']);
return $ice;
}
}