View source
<?php
namespace Drupal\course_book\Plugin\course\CourseObject;
use Drupal\node\Entity\Node;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal;
use Drupal\course_content\Course\Object\CourseObjectNode;
use function content_access_get_per_node_settings;
use function content_access_save_per_node_settings;
use function course_book_count;
use function course_book_override_outline_list_item;
use function node_type_get_names;
class CourseObjectBook extends CourseObjectNode {
public static function getNodeInstances($node) {
if (!empty($node->book['bid'])) {
return array(
$node->book['bid'],
);
}
}
function getNodeTypes() {
if (Drupal::currentUser()
->hasPermission('administer book outlines')) {
return array_keys(node_type_get_names());
}
else {
$config = \Drupal::config('book.settings');
return $config
->get('allowed_types');
}
}
public function createInstance($node = NULL) {
$node = Node::create([
'type' => $this
->getOption('node_type'),
]);
$node->book['bid'] = 'new';
parent::createInstance($node);
}
function optionsDefinition() {
$defaults = parent::optionsDefinition();
$defaults['node_type'] = \Drupal::config('course_book.settings')
->get('default_node_type', 'book');
$defaults['book_tracking'] = 'all';
$defaults['outline_list_item_type'] = 'active_tree';
return $defaults;
}
function optionsForm(&$form, FormStateInterface $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()
->get('outline')
->getString() == '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'],
);
}
}
function grade($user) {
$book_manager = \Drupal::service('book.manager');
$toc = $book_manager
->getTableOfContents($this
->getInstanceId(), \Drupal::config('course_book.settings')
->get('depth', 100));
if (course_book_count($this
->getInstanceId()) == 0) {
$this
->getFulfillment($user)
->setComplete(1)
->save();
return;
}
if ($this
->getOption('book_tracking') == 'all') {
$nids = array_keys($toc);
$fulfillment = $this
->getFulfillment($user)
->getOption('book_fulfillment');
$viewed = $fulfillment ? array_keys(array_filter($fulfillment)) : array();
if (!array_diff($nids, $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();
$route_match = Drupal::routeMatch();
if ($route_match
->getRouteName() == 'entity.node.canonical') {
$node = $route_match
->getParameter('node');
$book_outline = \Drupal::service('book.outline');
if (isset($node->book)) {
$book_link = $node->book;
if ($prev = $book_outline
->prevLink($book_link)) {
$links['prev'] = Link::createFromRoute(t('Previous'), 'entity.node.canonical', [
'node' => $prev['nid'],
]);
}
if ($next = $book_outline
->nextLink($book_link)) {
$links['next'] = Link::createFromRoute(t('Next'), 'entity.node.canonical', [
'node' => $next['nid'],
]);
}
}
return $links;
}
}
public function overrideOutlineListItem(&$item) {
if ($this
->getCourse()
->get('outline')
->getString() == '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') && Drupal::moduleHandler()
->moduleExists('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;
}
}