A bit of help with a script...

Status
Not open for further replies.

ranwilli

Member
Heres a script I "ccoked up" from snippets I found here and other places.
Client has three types of employees:emt medic and staff.

He has memo's (Joomla Articles) that he needs to assign to them to read.

To provide a module of "incomplete" assignments to a user upon Login, We made two lists: assign a memo, and record of memo assignments.

This script is to fill in the records with a row for each user containing fields:
assignment id (from the other list), user_id (of the "assigned user"), user_group_id, assigned_article_id,completed (yes or no).

Here's the code, Where did I go wrong?

Code:
<?php
/**
 * @package Joomla
 * @subpackage Fabrik
 * @copyright Copyright (C) 2005 Rob Clayburn. All rights reserved.
 * Modified by Randy Williams - Harper Vance Web Services.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
$memo = {memo_assignments___assigned_article_id} ;//memo number
$emt = {memo_assignments___emt_assigned};//emt yes or no
$medic = {memo_assignments___medic_assigned};//medic yes or no
$staff = {memo_assignments___staff_assigned};//staff yes or no
$assigner = {memo_assignments___user};//Super User that made the assignment
$emtvalue = "9";
$medicvalue = "10";
$staffvalue = "11";
//test for assignment to emt's
if ($emt != 0) {
    // Get a db connection.
    $db = JFactory::getDbo();
    // Create a new query object.
    $query = $db->getQuery(true);
    $query
        ->select(array('m.user_id'))
        ->from('X__user_usergroup_map AS m')
        ->join('LEFT', '#_users AS u ON (m.user_id=u.id)')
        ->where('X__user_usergroup_map.group_id = $emtvalue')
        ->order('X__user_usergroup_map.group_id ASC');
    $db->setQuery($query);
    $r = $db->loadObjectList();
    // Create a new query object.
    $query = $db->getQuery(true);
    //build query
    foreach($rows as $r) {
    $query = "INSERT INTO assignment_records (date_time, assigment_id, user_id, user_group_id, assigned_article_id, completed) VALUES ( '$now', '$memo', '$r->id', '$emtvalue', '0')";}
    $db->setQuery($query);
    $db->query();    
  }
//test for assignment to medics
if ($medic != 0) {
    // Get a db connection.
    $db = JFactory::getDbo();
    // Create a new query object.
    $query = $db->getQuery(true);
    $query
        ->select(array('m.user_id'))
        ->from('X__user_usergroup_map AS m')
        ->join('LEFT', '#_users AS u ON (m.user_id=u.id)')
        ->where('X__user_usergroup_map.group_id = $medicvalue')
        ->order('X__user_usergroup_map.group_id ASC');
    $db->setQuery($query);
    $r = $db->loadObjectList();
    // Create a new query object.
    $query = $db->getQuery(true);
    //build query
    foreach($rows as $r) {
    $query = "INSERT INTO assignment_records (date_time, assigment_id, user_id, user_group_id, assigned_article_id, completed) VALUES ( '$now', '$memo', '$r->id', '$medicvalue', '0')";}
    $db->setQuery($query);
    $db->query();    
  }  
//test for assignment to staff
if ($staff != 0) {
    // Get a db connection.
    $db = JFactory::getDbo();
    // Create a new query object.
    $query = $db->getQuery(true);
    $query
        ->select(array('m.user_id'))
        ->from('X__user_usergroup_map AS m')
        ->join('LEFT', '#_users AS u ON (m.user_id=u.id)')
        ->where('X__user_usergroup_map.group_id = $staffvalue')
        ->order('X__user_usergroup_map.group_id ASC');
    $db->setQuery($query);
    $r = $db->loadObjectList();
    // Create a new query object.
    $query = $db->getQuery(true);
    //build query
    foreach($rows as $r) {
    $query = "INSERT INTO assignment_records (date_time, assigment_id, user_id, user_group_id, assigned_article_id, completed) VALUES ( '$now', '$memo', '$r->id', '$staffvalue', '0')";}
    $db->setQuery($query);
    $db->query();    
 }
?>
Thanks.
 
Where do you use this code?

In PHP code fields you can't use <?php ?> tags,
you usually can use placeholders but placeholders should always be quoted to ommit PHP errors if the element is empty (you can do type casting if you want to get e.g. an integer:
$i =(int)'{tablename___elementname}';)
Maybe you have to use {tablename___elementname_raw} (e.g. to get values 0/1 instead of labels no/yes)

In PHP files you'll need the <?php ?> tags, but I think you usually can use placeholders.

You can add
var_dump($your-variable);exit;
to your code to see if the code is running at all and to display the values you get.
 
Changed the code thusly:
Code:
<?php
/**
 * @package Joomla
 * @subpackage Fabrik
 * @copyright Copyright (C) 2005 Rob Clayburn. All rights reserved.
 * Modified by Randy Williams - Harper Vance Web Services.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
$memo = (int)'{memo_assignments___id}' ;//memo number
$emt = (int)'{memo_assignments___emt_assigned}';//emt yes or no
$medic = (int)'{memo_assignments___medic_assigned}';//medic yes or no
$staff = (int)'{memo_assignments___staff_assigned}';//staff yes or no
$assigner = (int)'{memo_assignments___user}';//Super User that made the assignment
$emtvalue = '9';
$medicvalue = '10';
$staffvalue = '11';
?>
<?php
//test for assignment to staff
if ($staff != 0) {
    // Get a db connection.
    $db = JFactory::getDbo();
    // Create a new query object.
    $query = $db->getQuery(true);
    $query
        ->select(array('m.user_id'))
        ->from('X__user_usergroup_map AS m')
        ->join('LEFT', '#_users AS u ON (m.user_id=u.id)')
        ->where('X__user_usergroup_map.group_id = $staffvalue')
        ->order('X__user_usergroup_map.group_id ASC');
    $db->setQuery($query);
    $r = $db->loadObjectList();
    // Create a new query object.
    $query = $db->getQuery(true);
    //build query
    foreach($rows as $r) {
    $query = "INSERT INTO assignment_records (date_time, assigment_id, user_id, user_group_id, assigned_article_id, completed) VALUES ( '$now', '$memo', '$r->id', '$staffvalue', '0')";
    $db->setQuery($query);
    $db->query();
    }
?>
<?php
//test for assignment to emt's
if ($emt != 0) {
    // Get a db connection.
    $db = JFactory::getDbo();
    // Create a new query object.
    $query = $db->getQuery(true);
    $query
        ->select(array('m.user_id'))
        ->from('X__user_usergroup_map AS m')
        ->join('LEFT', '#_users AS u ON (m.user_id=u.id)')
        ->where('X__user_usergroup_map.group_id = $emtvalue')
        ->order('X__user_usergroup_map.group_id ASC');
    $db->setQuery($query);
    $r = $db->loadObjectList();
    // Create a new query object.
    $query = $db->getQuery(true);
    //build query
    foreach($rows as $r) {
    $query = "INSERT INTO assignment_records (date_time, assigment_id, user_id, user_group_id, assigned_article_id, completed) VALUES ( '$now', '$memo', '$r->id', '$emtvalue', '0')";
    $db->setQuery($query);
    $db->query();
    }
//test for assignment to medics
?>
<?php
if ($medic != 0) {
    // Get a db connection.
    $db = JFactory::getDbo();
    // Create a new query object.
    $query = $db->getQuery(true);
    $query
        ->select(array('m.user_id'))
        ->from('X__user_usergroup_map AS m')
        ->join('LEFT', '#_users AS u ON (m.user_id=u.id)')
        ->where('X__user_usergroup_map.group_id = $medicvalue')
        ->order('X__user_usergroup_map.group_id ASC');
    $db->setQuery($query);
    $r = $db->loadObjectList();
    // Create a new query object.
    $query = $db->getQuery(true);
    //build query
    foreach($rows as $r) {
    $query = "INSERT INTO assignment_records (date_time, assigment_id, user_id, user_group_id, assigned_article_id, completed) VALUES ( '$now', '$memo', '$r->id', '$medicvalue', '0')";
    $db->setQuery($query);
    $db->query();    
}
?>

Now get this error:

Code:
Parse error: syntax error, unexpected $end in  /home/XXXXX/public_html/plugins/fabrik_form/php/scripts/add-assignments-to-list.php  on line 92
 
Ok, Thanks for that, you've helped immensely...

I'll have to figure out how to call out the vars in pure php, unless someone knows how to do this "the fabrik way".
 
This is the Joomla way (for J! 2.5 and above):

To get to the forms values, put this at the beginning of your plugin:

Code:
$app =  JFactory::getApplication();

Then to retreive your data, use:
Code:
$memo = $app->input->get( 'memo_assignments___id', '0', 'int' );

The '0' above is a default value for this element.
be careful, if the data to retreive is not an integer, you may need to use arrays:
Code:
$app->input->get( 'XXX', array(), 'array' );
 
I think you have a couple of missing closing '}' in there to close the open if statmenet - shouldnt there be one after:

//test for assignment to medics

and another one right at the end
 
In a form plugin script, you should be able to use ...

$whatever = $formModel->getElementData('tablename___elementname');

... which is usually more reliable than grabbing it from the input array, as it'll include any modifications made by things like validations, and (depending on which hook you use to run the plugin on) will have been formatted. So for instance, it'll give you the label rather than value of a dropdown or join, etc.

You can also append three other arguments if needed. First arg is always the element name. Second is "use_raw", a true.false which tells us if you want the raw or formatted value, third arg is an optional default, and fourth arg is an optional repeat count (for elements in a repeated group). So if you wanted to fetch the raw value of the 2nd instance of an element from a repeated group, defaulting to "blue" ...

$color = $formModel->getElementData('yourtable___color', true, 'blue', 2);

If the element is in a repeating group and you don't supply the repeatCount, you'll get an array of values back.

-- hugh
 
Oh, and as Troester said, I'd start small. Just the first steps ... grab the data, make sure it's what you expect. Then start adding the database handling.

And remember, var_dump() is your friend. So for instance, after you've grabbed the data, make sure it's what you expect by putting:

var_dump($memo, $emt, $medic);exit;

... which will dump out the values of those variables on screen and exit the script, so you can see if you've gotten the data you are expecting.

-- hugh
 
Thanks for all the replies, you guys are great!

I've made quite a bit of progress actually (var_dump IS your friend, as some sage told me)

Here's what I have working nicely:

Code:
defined('_JEXEC') or die();
$assignmentid = (int)'{memo_assignments___id_raw}';
$memo = (int)'{memo_assignments___assigned_article_id_raw}';
$groups = '{memo_assignments___assigned_groups}';
$emtGroupId = 9;
$medicGroupId = 10;
$staffGroupId = 11;
//SELECT QUERY
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query
    ->select('user_id')
    ->from('iqgio_user_usergroup_map')
    ->where('iqgio_user_usergroup_map.group_id = 9')// AND ('iqgio_user_usergroup_map.group_id = 10') AND ('iqgio_user_usergroup_map.group_id = 11'))
    ->order('user_id ASC');
$db->setQuery($query);
$rows = $db->loadObjectList();
This yields an array of all user_ID's within a given group...
I also have a Switch worked out to run the proper code for his choice(s) of user groups.

What is giving me trouble now is running an INSERT Query...

Here's one with all the variable values removed, and ignoring the "foreach" loops. even this simple one doesn't work (even though running it in PHPMyAdmin works fine):
Code:
//INSERT Query
$db = JFactory::getDbo();
$sql = $db->getQuery(true);
$sql = "INSERT INTO assignment_records( 'date_time' `assigment_id` , `user_id` , `user_group_id` , `assigned_article_id` , `completed` ) VALUES ( Now(), 156, 65, 9, 183, 0 )";
$db->setQuery($sql);
$db->query();

Any ideas?

I had added this to try to get a clue, but it just throws "ERROR" without even giving me the lame and not very helpful MySQL error message:
Code:
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);

Thanks in advance
 
With a Great deal of your usual help, I got it working...

For those it may help:

Code:
defined('_JEXEC') or die();
$assignmentid = (int)'{memo_assignments___id_raw}';
$memo = (int)'{memo_assignments___assigned_article_id_raw}';
$emt = (int)'{memo_assignments___emt_raw}';
$medic = (int)'{memo_assignments___medic_raw}';
$staff = (int)'{memo_assignments___staff_raw}';
$emtGroupId = 9;
$medicGroupId = 10;
$staffGroupId = 11;
if (($emt == 1) && ($medic == 1) && ($staff == 1)) {
//Select Query
$db = FabrikWorker::getDbo();
$query = $db->getQuery(true);
$query
    ->select('user_id')
    ->from('iqgio_user_usergroup_map')
    ->where('iqgio_user_usergroup_map.group_id = 9' OR 'iqgio_user_usergroup_map.group_id = 10' OR 'iqgio_user_usergroup_map.group_id = 11')
    ->order('user_id ASC');
$db->setQuery($query);
$rows = $db->loadObjectList();
//END SELECT QUERY
} elseif (($emt == 1) && ($medic == 1) && ($staff == 0)) {
//Select Query
$db = FabrikWorker::getDbo();
$query = $db->getQuery(true);
$query
    ->select('user_id')
    ->from('iqgio_user_usergroup_map')
    ->where('iqgio_user_usergroup_map.group_id = 9' OR 'iqgio_user_usergroup_map.group_id = 10')
    ->order('user_id ASC');
$db->setQuery($query);
$rows = $db->loadObjectList();
//END SELECT QUERY
} elseif (($emt == 1) && ($medic == 0) && ($staff == 0)) {
//Select Query
$db = FabrikWorker::getDbo();
$query = $db->getQuery(true);
$query
    ->select('user_id')
    ->from('iqgio_user_usergroup_map')
    ->where('iqgio_user_usergroup_map.group_id = 9')
    ->order('user_id ASC');
$db->setQuery($query);
$rows = $db->loadObjectList();
//END SELECT QUERY
} elseif (($emt == 0) && ($medic == 1) && ($staff == 0)) {
//Select Query
$db = FabrikWorker::getDbo();
$query = $db->getQuery(true);
$query
    ->select('user_id')
    ->from('iqgio_user_usergroup_map')
    ->where('iqgio_user_usergroup_map.group_id = 10')
    ->order('user_id ASC');
$db->setQuery($query);
$rows = $db->loadObjectList();
//END SELECT QUERY
} elseif (($emt == 0) && ($medic == 0) && ($staff == 1)) {
//Select Query
$db = FabrikWorker::getDbo();
$query = $db->getQuery(true);
$query
    ->select('user_id')
    ->from('iqgio_user_usergroup_map')
    ->where('iqgio_user_usergroup_map.group_id = 11')
    ->order('user_id ASC');
$db->setQuery($query);
$rows = $db->loadObjectList();
//END SELECT QUERY
} elseif (($emt == 0) && ($medic == 1) && ($staff == 1)) {
//Select Query
$db = FabrikWorker::getDbo();
$query = $db->getQuery(true);
$query
    ->select('user_id')
    ->from('iqgio_user_usergroup_map')
    ->where('iqgio_user_usergroup_map.group_id = 10' OR 'iqgio_user_usergroup_map.group_id = 11')
    ->order('user_id ASC');
$db->setQuery($query);
$rows = $db->loadObjectList();
//END SELECT QUERY
} elseif (($emt == 1) && ($medic == 0) && ($staff == 1)) {
//Select Query
$db = FabrikWorker::getDbo();
$query = $db->getQuery(true);
$query
    ->select('user_id')
    ->from('iqgio_user_usergroup_map')
    ->where('iqgio_user_usergroup_map.group_id = 9' OR 'iqgio_user_usergroup_map.group_id = 11')
    ->order('user_id ASC');
$db->setQuery($query);
$rows = $db->loadObjectList();
}//END SELECT QUERY
//START INSERT PROCESS
$query = $db->getQuery(true);
foreach($rows as $r) {
    $sql = "INSERT INTO assignment_records ( `assigment_id` , `user_id` , `assigned_article_id` ) VALUES ( $assignmentid, $r->user_id, $memo )";
    $db->setQuery($sql);
    $db->query();
}

Thanks guys, I really appreciate your help.
 
Just as a small point, you don't need to re-fetch the DBO object every time, so just one ...

$db = JFactory::getDBO();

... at the top of the script would be sufficient. Won't hurt doing it multiple times, just not necessary once you have your copy of the main J! DBO.

Also, in this line:

PHP:
    $sql = "INSERT INTO assignment_records ( `assigment_id` , `user_id` , `assigned_article_id` ) VALUES ( $assignmentid, $r->user_id, $memo )";

I'm actually surprised that works, as I wouldn't have expected $r->user_id to evaluate properly inside of quotes. Typically, you either need to do surround references like that with {}, like ...

PHP:
    $sql = "INSERT INTO assignment_records ( `assigment_id` , `user_id` , `assigned_article_id` ) VALUES ( $assignmentid, {$r->user_id}, $memo )";

.... or take them out of the quotes and concat them:

PHP:
    $sql = "INSERT INTO assignment_records ( `assigment_id` , `user_id` , `assigned_article_id` ) VALUES ( $assignmentid, " . $r->user_id . ", $memo )";

And ... unless $memo is an integer, I'm surprised that works without single quotes round it, although by the context of that query, it should be an int.

BUT, and perhaps MOST importantly, $memo is coming direct from one of your form inputs. ANYTHING you use in a query which comes from a form input MUST be sanitized, to prevent Bad People from hacking the form input to do something like "(DROP TABLE xxx_users)" or some such. So ALWAYS run form inputs through $db->quote ....

PHP:
    $sql = "INSERT INTO assignment_records ( `assigment_id` , `user_id` , `assigned_article_id` ) VALUES ( $assignmentid, " . $r->user_id . ", " . $db->quote($memo) . ")";

http://xkcd.com/327/

-- hugh
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top