-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathh5p_xapi.module
More file actions
93 lines (79 loc) · 2.45 KB
/
Copy pathh5p_xapi.module
File metadata and controls
93 lines (79 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
/**
* @file
* Saves the H5P XAPI Event Statements.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\views\ViewExecutable;
use Drupal\user\Entity\User;
use Drupal\node\Entity\Node;
/**
* Implements hook_page_attachments().
*
* Add H5P XAPI libraries to H5P Resource nodes only.
*/
function h5p_xapi_page_attachments(&$page) {
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
if ($node->bundle() == "h5p") {
$page['#attached']['library'][] = 'h5p_xapi/listener';
}
}
}
/**
* Implements template_preprocess_page().
*
* Creates a few JS Settings to use in the h5p-xapi file
*/
function h5p_xapi_preprocess_page(&$variables) {
// Create a JS Setting for the User ID
$user = \Drupal::currentUser();
$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
if ($node->bundle() == "h5p") {
$variables['#attached']['drupalSettings']['h5pxapi']['userId'] = $user->id();
$variables['#attached']['drupalSettings']['h5pxapi']['nodeId'] = $node->id();
}
}
$variables['page']['#cache']['contexts'][] = 'h5pxapi';
$variables['page']['#cache']['contexts'][] = 'url';
}
/**
* Implements hook_views_pre_build().
*/
function h5p_xapi_views_pre_build(ViewExecutable $view) {
$user = \Drupal::currentUser();
$node = \Drupal::routeMatch()->getParameter('node');
if ($view->id() == 'h5p_xapi_event_actor' && $view->getDisplay()->display['id'] == 'page_1') {
if ($user) {
$view->args[0] = $user->id();
}
}
if ($view->id() == 'h5p_xapi_event_object_data' && $view->getDisplay()->display['id'] == 'page_1') {
if ($user) {
$view->args[0] = $user->id();
}
}
if ($view->id() == 'h5p_xapi_event_result_data' && $view->getDisplay()->display['id'] == 'page_1') {
if ($user) {
$view->args[0] = $user->id();
}
}
}
/**
* Implements hook_preprocess_views_view_field().
*/
function h5p_xapi_preprocess_views_view_field(&$variables) {
$view = $variables['view'];
$field = $variables['field'];
if ($view->storage->id() == 'h5p_xapi_event_result_data' && $view->current_display == 'page_1') {
if ($field->field == 'uid') {
$user = User::load($field->original_value);
$variables['output'] = $user->getAccountName();
}
if ($field->field == 'nid') {
$node = Node::load($field->original_value);
$variables['output'] = $node->label();
}
}
}