signed_nodes-8.x-1.x-dev/signed_nodes.module
signed_nodes.module
<?php
/**
* @file
* The main signed_nodes control module file,
* which controls the creation of node agreements and user signatures to those agreements.
*
*/
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
/**
* Get a single agreement.
*
* @param $snid
* The agreement id called signed_node id
*
* @return
* returns an agreement with all its elements as object.
*/
function signed_nodes_load_all($snid) {
$result = db_query("SELECT * FROM {signed_nodes} where snid = :snid", array(':snid' => $snid));
$items = array();
foreach ($result as $row) {
$items[] = $row;
}
return $items[0];
}
/**
* Get agreements for all nodes.
*
* @return
* returns an array of all agreements created by admin with all its elements as object.
*/
function signed_nodes_get_all() {
$signed_nodes = array();
$result = db_query("SELECT snid, nid, year FROM {signed_nodes}");
$items = array();
foreach ($result as $row) {
$items[$row->snid] = $row;
}
return $items;
}
/**
* Get node-id of an agreement.
*
* @param $snid
* The agreement id called signed_node id
*
* @return
* returns a node id of an agreement.
*/
function signed_node_get_nid($snid) {
return db_query("SELECT nid FROM {signed_nodes} WHERE snid = :snid", array(':snid' => $snid))->fetchField();
}
/**
* Get agreement year set up for a node.
*
* @param $nid
* The node id of an agreement
*
* @return
* returns an agreement year of a particular node.
*/
function signed_nodes_get_year($nid) {
$signed_nodes = array();
$result = db_query("SELECT snid, year FROM {signed_nodes} WHERE nid = :nid", array(':nid' => $nid));
$items = array();
foreach ($result as $row) {
$items[$row->snid] = $row->year;
}
return $items;
}
/**
* Deletes an agreement and its related signed user data.
*
* @param $snid
* The agreement id called signed_node id
*/
function signed_nodes_delete_agreement($snid) {
db_delete('signed_nodes')->condition('snid', $snid)->execute();
db_delete('signed_nodes_user')->condition('snid', $snid)->execute();
}
function signed_nodes_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
if ($entity->getType() == 'article' && $view_mode == 'full') {
$build['#cache']['max-age'] = 0;
$build['#cache']['no-cache'] = TRUE;
\Drupal::service('page_cache_kill_switch')->trigger();
}
}
/**
* Implements hook_preprocess_HOOK() for block.html.twig.
*/
function signed_nodes_preprocess_block(&$vars) {
//kint($vars);
if($vars['base_plugin_id'] == 'signed_nodes_user_agreement_form_1') {
//-- This stops the block being cache in drupal 8
//$vars['#cache']['max-age'] = 0;
//kint($vars);
}
}
/**
* Helper function to provide similar listing user functionality based on
* different query and agreementid.
*
* @param $snid
* The agreement id called signed_node id
* @param $query
* SQL query to get pending or signed status for all users
*
* @return
* List of users who have signed an agreement, detailed report will be available
* if profile module is enabled, and configuration for the profile reporting is done in signed_nodes admin form .
*
* @see signed_nodes_report_signed()
* @see signed_nodes_report_pending()
*/
function signed_nodes_report_all($snid, $query) {
$results = db_query($query, array(':snid' => $snid));
//$signed_nodes = signed_nodes_get_all();
$rows = array();
foreach ($results as $result) {
$row['data'] = [
$result->uid,
$result->mail,
];
$rows[] = $row;
}
if ($rows) {
$results = [
'#theme' => 'table',
'#header' => [t('UID'), t('Email')],
'#rows' => $rows,
];
return $results;
}
else {
return [
'#markup' => "No agreement Signed",
];
}
}
/**
* Implementation of hook_user_delete($account).
* @todo Have to come up with logic if a user is deleted should we keep the signature or delete it.
function signed_nodes_user_delete($account) {
// add some logic here
}*/