I am working on a custom joomla component that simply adds a points table to the database with two fields (id,points)... id is the primary key and I am trying to set it to be the same as the user joomla user id...
I need one function that returns the number of points for a user & another that updates the users points to a given value... I successfully created the first one called:
getUserPoints()
It returns the number of points for a given user... Then I started the second update function called:
updateUserPoints()
This is the part that is being "inconsistent" at the moment and I think I have wrong... I am just looking to make a simple function that will update the points value for the current user... and also I was trying to handle the case of if there is no record for them... then create one!.... I did this by making a function within the updateUserPoints() function called:
newUserCheck()
It's supposed to check if there are points for the user and then reply true or false:
$db->setQuery("SELECT points FROM #__mytable WHERE id = '".$user->id."'");
$db->query();
$points = $db->loadResult();
if($points){
return false;
} else {
return true;
}
Then either update create a new record OR update the existing one... This code is almost working!... It worked for me in three tests... however when I add a second user it seems to get a little inconsistent... It would create a new user successfully and give them 5 points.... then when I run it again to update it says "Points Updated" but it doesn't seem to actually update the points value in the database?!
With one or no existing users it seems to work fine! Anyway... Open to suggestions... I am sure there is a bunch I am doing wrong here but I would like to get it working properly! I am new to MVC programming as you can probably tell :P
Here is the joomla controller in its entirety:
<?php defined('_JEXEC') or die('');
// userprofilepoints CONTROLLER
jimport('joomla.application.component.controller');
jimport('joomla.user.helper');
class userprofilepointsController extends JController {
function getUserPoints(){
$values = JRequest::get("post");
$userIdNumber = $values['userIdNumber'];
$user = & JFactory::getUser();
$db = JFactory::getDBO();
$db->setQuery("SELECT points FROM #__mytable WHERE id = '".$user->id."'");
$db->query();
$points = $db->loadResult();
echo $points;
}
function updateUserPoints(){
function newUserCheck(){
$values = JRequest::get("post");
$userIdNumber = $values['userIdNumber'];
$user = & JFactory::getUser();
$db = JFactory::getDBO();
$db->setQuery("SELECT points FROM #__mytable WHERE id = '".$user->id."'");
$db->query();
$points = $db->loadResult();
if($points){
return false;
} else {
return true;
}
}
if(newUserCheck() == false){
$values = JRequest::get("post");
$userIdNumber = $values['userIdNumber'];
$newPointsAmount = $values['newPointsAmount'];
$user = & JFactory::getUser();
$db = JFactory::getDBO();
$db->setQuery("UPDATE #__mytable SET points = '".$newPointsAmount."' WHERE id = '".$user->id."'");
$db->query();
//echo getUserPoints();
//$points = $db->loadResult();
echo "Points Updated";
} else if(newUserCheck() == true) {
$values = JRequest::get("post");
$userIdNumber = $values['userIdNumber'];
$user = & JFactory::getUser();
$db = JFactory::getDBO();
$db->setQuery("INSERT INTO #__mytable SET id = $user->id, points = 5");
$db->query();
echo "New User Created!";
}
}
}
And finally my html and javascript from the view!.... Thank you!!! :)
<?php defined('_JEXEC') or die();
// MAIN PAGE TEMPLATE
jimport('joomla.user.helper');
$user = & JFactory::getUser();
$profile = JUserHelper::getProfile($user->id);
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$('#updateBTN').click(function(){
updateUserPoints();
});
function getUserPoints(){
//Post the following to the controller
$.post('http://www.mysite.com/index.php?format=raw&option=com_userprofilepoints&task=getUserPoints', {
userIdNumber: <?php echo $user->id; ?>
}, function(data) {
alert(data);
});
}
function updateUserPoints(){
//Post the following to the controller
$.post('http://www.mysite.com/index.php?format=raw&option=com_userprofilepoints&task=updateUserPoints', {
userIdNumber: <?php echo $user->id; ?>,
newPointsAmount: 555444 //random hardcoded points amount to test with
}, function(data) {
alert(data);
});
}
});
</script>
<div id="main_block">
<p>Points Page:</p>
<br />
<button id="updateBTN">Trigger Update</button>
</div>