Search
Other Advanced Module Manager questions
Forum

To not Showing the module once consent validated

Gregory Roussel's Avatar Gregory Roussel
Hi,
I want to not show a module on the user edit form, when the user's already validate the consent
I try with PHP code on the module with the "Exclude" setting with this code :
$query = $db->getQuery(true);
$user = JFactory::getUser();

$query
    ->select($db->quoteName('state'))
    ->from($db->quoteName('#__privacy_consents'))
    ->where($db->quoteName('user_id') . ' = ' . (int) $user->id);

$db->setQuery($query);
$state = $db->loadResult();

if ($state == 1) {
    $state = 1;
} else {
    $state = 0;
}

return $state;
But it doesn't work as the module still appearing

same with
$db->setQuery($query);
return ($db->loadResult() == 1);

Thanks for advice
Joomla 3.10.11
Advanced Module Manager 9.5.6 Pro
Peter van Westen's Avatar Peter van Westen ADMIN
Does this work?
$query = $db->getQuery(true)
    ->select($db->quoteName('state'))
    ->from($db->quoteName('#__privacy_consents'))
    ->where($db->quoteName('user_id') . ' = ' . (int) $user->id);

$db->setQuery($query);
$state = $db->loadResult();

return $state == 1;

Make sure the database query actually returns what you expect. You can do that for instance by adding some temporary testing output:
$query = $db->getQuery(true)
    ->select($db->quoteName('state'))
    ->from($db->quoteName('#__privacy_consents'))
    ->where($db->quoteName('user_id') . ' = ' . (int) $user->id);

$db->setQuery($query);
$state = $db->loadResult();

echo "\n\n<pre>==========================\n";
print_r($user->id);
echo "\n--------------------------\n";
print_r($state);
echo "\n==========================</pre>\n\n";

return $state == 1;
Please post a rating at the Joomla! Extensions Directory
Gregory Roussel's Avatar Gregory Roussel
Thanks
strangely nothing appears from the echo and the print_r.

But I do see the query in the debug as
SELECT `state`

  FROM `m2db5_privacy_consents`

  WHERE `user_id` = 12345 //(myid)
22	JDatabaseDriver->loadResult()	/home/tmp/regularlabs/custom_php/2937_xxxxxx:19
Peter van Westen's Avatar Peter van Westen ADMIN
Ah, try to add an exit, so it prevents the rest of the page from being rendered:
$query = $db->getQuery(true)
    ->select($db->quoteName('state'))
    ->from($db->quoteName('#__privacy_consents'))
    ->where($db->quoteName('user_id') . ' = ' . (int) $user->id);

$db->setQuery($query);
$state = $db->loadResult();

echo "\n\n<pre>==========================\n";
print_r($user->id);
echo "\n--------------------------\n";
print_r($state);
echo "\n==========================</pre>\n\n";
exit;
Please post a rating at the Joomla! Extensions Directory
Gregory Roussel's Avatar Gregory Roussel
ok got it
==========================
12345 (myid)
--------------------------
-1
==========================

and that should not be "-1" because my id is validated so should be "1"
But as several results possible (several unvalidated and validated for each users), I suppose that I need to ORDER BY DESC my query result.
like so :
->order($db->quoteName('created') . ' DESC');

Now, everything is good, now the module only appears when the user has not yet validated the consent.

Thanks a lot 🙂
Peter van Westen's Avatar Peter van Westen ADMIN
Another option, which might be better, is to add this instead:
->where($db->quoteName('state') . ' = 1')
Please post a rating at the Joomla! Extensions Directory
Gregory Roussel's Avatar Gregory Roussel
ok so better like so :
$query = $db->getQuery(true)
    ->select($db->quoteName('state'))
    ->from($db->quoteName('#__privacy_consents'))
    ->where($db->quoteName('user_id') . ' = ' . (int) $user->id . ' AND' . $db->quoteName('state') . ' = 1')
    ->order($db->quoteName('created') . ' DESC');

But it will only return the validated consent, and since a customer would have reject it and become invalidated with "-1"
?
Peter van Westen's Avatar Peter van Westen ADMIN
I really don't know too much about how the privacy_consents thing works and how the database is set up.
But I could find some code in Joomla core that was only searching for the rows with state = 1.

Use whatever works for you...
Please post a rating at the Joomla! Extensions Directory
Gregory Roussel's Avatar Gregory Roussel
thanks, in fact what I need is just the last result for a user.
So as it works like that..it's fine.

Thanks again
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription