Commit f70e483d authored by mathieso's avatar mathieso

New views plugins.

parent af17fa4d
......@@ -38,6 +38,16 @@ function skilling_views_data_alter(&$data) {
],
];
// Really a filter.
$data['views']['instructor_of_node_owner'] = [
'title' => t('Current user is instructor of node owner'),
'help' => t('Current user is an instructor of the node\'s owner.'),
'group' => 'Skilling',
'field' => [
'id' => 'instructor_of_node_owner',
],
];
$data['node__field_when_read']['unread_filter'] = array(
// $data['node_field_data']['unread_filter'] = array(
'filter' => [
......@@ -49,6 +59,17 @@ function skilling_views_data_alter(&$data) {
],
);
$data['node_field_data']['instructor_of_node_owner_filter'] = array(
'filter' => [
'title' => t('Current user instructor of node owner'),
'help' => t('Is the current user an instructor of the node owner?'),
'field' => 'instructor_of_node_owner',
'id' => 'instructor_of_node_owner_filter',
'group' => 'Skilling',
],
);
// For query plugin giving data on one student's exercise submissions.
$data['student_submissions']['table']['group'] = t("Student's submissions");
$data['student_submissions']['table']['base'] = [
......
<?php
namespace Drupal\skilling\Plugin\views\field;
use DateInterval;
use DateTime;
use Drupal\skilling\SkillingConstants;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
/**
* Show whether owner of the current node (intended as submission)
* is in a class where the current user is an instructor.
*
* @ingroup views_field_handlers
*
* @ViewsField("instructor_of_node_owner")
*/
class InstructorOfNodeOwner extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function usesGroupBy() {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function query() {
// Do nothing -- to override the parent query.
}
/**
* {@inheritdoc}
*/
// protected function defineOptions() {
// $options = parent::defineOptions();
//
// $options['hide_alter_empty'] = ['default' => FALSE];
// return $options;
// }
/**
* {@inheritdoc}
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\skilling\Exception\SkillingValueMissingException
* @throws \Drupal\skilling\Exception\SkillingWrongTypeException
* @throws \Drupal\skilling\Exception\SkillingException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Exception
*/
public function render(ResultRow $values) {
/** @var \Drupal\node\NodeInterface $node */
$node = $values->_entity;
/** @var \Drupal\user\UserInterface $nodeOwner */
$nodeOwner = $node->getOwner();
/** @var \Drupal\skilling\SkillingUserFactory $skillingUserFactory */
$skillingUserFactory = \Drupal::service('skilling.skilling_user_factory');
$skillingOwner = $skillingUserFactory->makeSkillingUser($nodeOwner->id());
/** @var \Drupal\skilling\SkillingCurrentUser $currentUserService */
$currentUserService = \Drupal::service('skilling.skilling_current_user');
/** @var \Drupal\skilling\Access\SkillingCheckUserRelationships $relationshipService */
$relationshipService = \Drupal::service('skilling.check_user_relationships');
$isInstructor = $relationshipService->isUserInstructorOfUser(
$currentUserService, $skillingOwner
);
$result = ($isInstructor || $currentUserService->isAdministrator())
? 'yes' : 'no';
return $result;
$values = NULL;
return 'Rosie';
$result = '';
/** @var \Drupal\node\Entity\Node $notice */
$notice = $values->_entity;
$whenRead = $notice->get('field_when_read')->value;
if (is_null($whenRead)) {
$result = $this->t('Unread');
}
return $result;
}
}
<?php
namespace Drupal\skilling\Plugin\views\filter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\BooleanOperator;
use Drupal\views\Plugin\views\filter\FilterPluginBase;
use Drupal\views\Plugin\views\filter\Standard;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Is the current user an instructor of the node owner?
* @ViewsFilter("instructor_of_node_owner_filter")
*/
class FilterInstructorOfNodeOwner extends FilterPluginBase {
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition
);
}
/**
* {@inheritdoc}
*/
protected function valueForm(&$form, FormStateInterface $form_state) {
$form['value'] = [
'#type' => 'checkbox',
'#title' => $this->t('Check for instructor of node owner (or admin)?'),
'#default_value' => $this->value,
];
// $form['value'] = array(
// '#type' => 'radios',
// '#title' => t('Notices to show'),
// '#options' => ['all' => 'Show all notices', 'unread' => 'Only show unread notices'],
// '#description' => t('Which notices would you like to see?'),
// '#default_value' => 'unread',
// );
}
/**
* Override query so no filtering if the user doesn't select any options.
*/
// Couldn't get this to work.
public function query() {
// parent::query();
// if (!empty($this->value)) {
// $this->query->addWhere(
// 0, //Default group
// 'node__field_when_read.field_when_read_value',
// 'NULL',
// 'IS'
// );
// }
}
//(node__field_when_read.field_when_read_value IS NULL)
/**
* @param $values
*
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
* @throws \Drupal\skilling\Exception\SkillingException
*/
public function postExecute(&$values) {
if ($this->value) {
/** @var \Drupal\skilling\SkillingCurrentUser $currentUserService */
$currentUserService = \Drupal::service('skilling.skilling_current_user');
/** @var \Drupal\skilling\Access\SkillingCheckUserRelationships $relationshipService */
$relationshipService = \Drupal::service('skilling.check_user_relationships');
foreach ($values as $key => $value) {
/** @var \Drupal\node\NodeInterface $node */
$node = $value->_entity;
$nodeOwner = $node->getOwner();
/** @var \Drupal\skilling\SkillingUserFactory $skillingUserFactory */
$skillingUserFactory = \Drupal::service('skilling.skilling_user_factory');
$skillingOwner = $skillingUserFactory->makeSkillingUser($nodeOwner->id());
$isInstructor = $relationshipService->isUserInstructorOfUser(
$currentUserService, $skillingOwner
);
$keepRow = ($isInstructor || $currentUserService->isAdministrator());
if (!$keepRow) {
unset($values[$key]);
continue;
}
}
$values = array_values($values);
}
parent::postExecute($values);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment