I am trying to query my database and return a number to store in a twig template called node--course.html.twig
So I started by writing the function completionCheck() in class FormaRegis.php
<?php
namespace Drupal\docebo_login;
use Drupal\docebo_login\FormaEntity;
class FormaRegis extends FormaEntity {
public $ID;
public function setID($ID) {
$this->ID = $ID;
}
public function getID() {
return $this->ID;
}
public function __construct() {
parent::__construct();
}
public function rowBase() {
if (parent::accessCheck()) {
$sql = "SELECT * FROM learning_courseuser
WHERE idUser = " . $_SESSION['public_area_idst'] .
" AND idCourse = " . $this->ID. " ";
return $sql;
} else {
return "";
}
}
public function completionCheck() {
$check = false;
if (parent::accessCheck()) {
$sql = "SELECT count(*) FROM learning_courseuser
WHERE DATE_FORMAT(CURDATE(),'%d/%m/%Y') = DATE_FORMAT(date_complete,'%d/%m/%Y')
AND idUser = " . $_SESSION['public_area_idst'];
return $sql;
}
else {
return "";
}
}
}
and in a page called FormaEntity.php I wrote this function
public function getCCresult() {
if ($this->completionCheck() == "") {
\Drupal\Core\Database\Database::setActiveConnection();
return false;
}
$result = $this->connection->query($this->completionCheck())->fetch();
\Drupal\Core\Database\Database::setActiveConnection();
if ($result > 5 ) {
$check = "fail";
}
else {
$check = "pass";
}
return $check;
}
Then in FormaNotification.php I wrote function completionCheck()
<?php
namespace Drupal\docebo_login;
use Drupal\docebo_login\FormaEntity;
class FormaNotification extends FormaEntity {
public function __construct() {
parent::__construct();
}
public function getResult() {
return parent::accessCheck();
}
public function completionCheck() {
return parent::getCCresult();
}
}
Then finally, in my .theme file I added the function mytheme_preprocess_node_course(&$variables)
function mytheme_preprocess_node_course (&$variables) {
$noti = new FormaNotification();
if ($noti->completionCheck() == "fail") {
$variables['creditCheck'] = "yes";
}
else {
$variables['creditCheck'] = "no";
}
}
But when I visit the any page that uses the node--course.html.twig and use {{ kint(creditCheck) }}, it says that the variable in NULL. I am not sure what I am doing wrong. If anyone can point me in the right direction that would be great.