how to update custom profile field data in buddypress - buddypress

I want to update custom profile field data in buddypress. I've been searching on google but still no luck. is there anyway to update it with a custom code?

You can use the default buddypress function
xprofile_set_field_data( $field, $user_id, $value, $is_required = false )
$fields = field id
$user_id = current login user id
$value = inserted value
$is_required = boolean
Here you have passed the specific insert fields data.
OR
You can insert your custom field data in usermeta table.
Like this:
update_user_meta($user_id, '$key', $_POST['post_value']);
$key = unique name of the fields

Related

Get user data from database codeignter

I have two tables in one database, table one called: ci_admin, and table two called active_ingredient.
I want to get user data from the active_ingredient table where user_id equals admin_id from ci_admin table using the current user session.
this code, get data for all user, I need the data inserted by the user only:
$wh =array();
$SQL ='SELECT * FROM active_ingredient';
$wh[] = "SELECT 1
FROM ci_admin
WHERE ci_admin.admin_id = active_ingredient.user_id";
if(count($wh)>0)
{ $WHERE = implode(' and ',$wh);
return $this->datatable->LoadJson($SQL,$WHERE);
}
else
{
return $this->datatable->LoadJson($SQL);
}
Try this, it will help you.
$this->db->select('ai.*,ca.*');
$this->db->from('active_ingredient ai');
$this->db->join('ci_admin ca', 'ca.admin_id = ai.user_id', 'left');
$this->db->where($data); //pass the session data for exact filter
$query = $this->db->get();
return $query->result();

Prestashop - Which SQL called after actionProductUpdate?

I try to update a name from ps_product when I update my product's stock. I added a SQL request on the hook actionProductUpdate.
The hook works, because when I set a die; after the SQL, the database is updated.
But I don't know where, another SQL is called and set back to the old value. I use PROFILING and I saw that it's called after....
How can I set this call as the last thing to do after a product update save ?
This is the simplified code :
public function hookActionProductUpdate($params)
{
$product = $params['product'];
$sql = 'UPDATE ' . _DB_PREFIX_ . 'product mp
SET mp.name = ''
WHERE mp.id_product = ' . $product->id;
Db::getInstance()->execute($sql);
}
PS : If I change a another parameter like reference, this time it works !
It is better to use object product to achieve your goal. Something like
public function hookActionObjectProductUpdateAfter($params)
{
$product = new Product($params['object']->id);
foreach(Language::getLanguages(true) as $language) {
$product->name[$language['id_lang']] = 'new name';
}
$product->update();
}
in this case, you don't care where the name is persisted in the DB, and you will never damage it
PS : Take care of params if you want to get 'product'. In actionProductUpdate, it's params['product'] but in hookActionObjectProductUpdateAfter it's params['object'] (as Product). I lost so much time on this...
"name" field is in the "ps_product_lang" table.
You have to rename product for each languages.

get user role by user id in moodle

I want to get user role from user id. I am using loop in my code where i want to show all user except admin. i used below code but its not working.
$context = get_context_instance (CONTEXT_SYSTEM);
$roles = get_user_roles($context, $USER->id, false);
$role = key($roles);
$roleid = $roles[$role]->roleid;
Its provide me blank array as like screenshot. Also below my all code.
https://prnt.sc/gq8p12
$allUsers = $DB->get_records('user');
$SQL = "SELECT * FROM `".$CFG->prefix."config` WHERE `name` LIKE 'siteadmins'";
$getSiteAdmins = $DB->get_record_sql($SQL);
$explodeAdminIds = explode(',', $getSiteAdmins->value);
$context = get_context_instance (CONTEXT_SYSTEM);
if(!empty($allUsers))
{
foreach ($allUsers as $allUser)
{
if(!in_array($allUser->id, $explodeAdminIds))
{
$roles = get_user_roles($context, $allUser->id, false);
$role = key($roles);
$roleid = $roles[$role]->roleid;
echo 'USER ID -- '.$allUser->id.' >>> ';
print_r($roles); echo '<br>';
$name = ''.$allUser->id.'_'.$allUser->firstname.' '.$allUser->lastname.'';
$confirmed = ($allUser->confirmed == 1) ? 'Active' : 'In-active';
$table->data[] = array(
$i,
$name,
'Team Name',
$allUser->email,
$allUser->phone1,
'Role',
$confirmed,
//empty($coachusrarr)?'--':implode(',',$coachusrarr),
//empty($tmpleaderarr)?'--':implode(',',$tmpleaderarr),
//$coach,
);
$i++;
}
}
}
The basic problem is that get_user_roles($context, $userid) will only get you a list of roles assigned at that particular context level. Very few users have roles assigned at the system context, it is much more usual for roles to be assigned at a course level. This allows users to have different roles in different courses (a teacher on one course, might be enrolled as a student on another course).
If you want to get all the roles for a user, then you're going to need to do something like this:
$roleassignments = $DB->get_records('role_assignments', ['userid' => $user->id]);
You can then loop through all the $roleassignments and extract the 'roleid' from them (alternatively, you could use the $DB->get_fieldset command, to extract the roleids directly).
Note also that you should be using context_system::instance() instead of the old get_context_instance(CONTEXT_SYSTEM) (unless you are using a very old and insecure version of Moodle).
For getting the site admins, use get_admins() (or, if you really want to access the config value, use $CFG->siteadmins).
If you want to get a user role for a course by user id then this script will help you.
$context = context_course::instance($COURSE->id);
$roles = get_user_roles($context, $USER->id, true);
$role = key($roles);
$rolename = $roles[$role]->shortname;

How to join two table id and validate a particular field?

Sorry for the fuorviant title but I'll trying to explain what I'm try to do. So, I've two table ea_users and ea_user_settings.
ea_users - STRUCTURE
id | data|
5 | 0
ea_user_settings - STRUCTURE
|id | username|
5 Sandokan
Now what I'm trying to do is validate if certain username already exists in the system. The table ea_users contain the id, that rapresents the id of a single user, and the data field (0 user not removed, also 1 user deleted).
In the ea_user_settings table I've all settings of my users. Now a possible idea for check if a certain username already exist is this:
public function validate_username($username, $user_id)
{
$num_rows = $this->db->get_where('ea_user_settings',
array('username' => $username, 'id_users <> ' => $user_id))->num_rows();
return ($num_rows > 0) ? FALSE : TRUE;
}
This function have two parameters: username and user_id, the username that's going to be inserted and the user_id (if this is valorized means that I'm actually stay update the user so I don't need to check on it row if the username already exists).
Actually all working good but I want to check if the username exists for only the user with the data equal to 0, if the data is 1 the username is available 'cause it's deleted. I don't know if the situation is more clear. How I can achieve that in a single query?
You can try something like this
public function validate_username($username, $user_id)
{
$num_rows =
$this->db->select('*')
->from('ea_user_settings')
->join('ea_users', 'ea_user_settings.id_users = ea_users.id', 'left')
->where(array('ea_user_settings.username' => $username, 'ea_user_settings.id_users <> ' => $user_id, 'ea_users.data' => 1))
->get();
return ($num_rows->num_rows() > 0) ? 'We have this username and it was deleted!' : 'This username was not deleted';
}

Codeigniter populate nested associations

this is the results i need from the database
{comment: 'hello there', user: [{name: 'sahan', id: 1}], id: 2}
function used to get comments
public function get_comments(){
$query = $this->db->get('comments');
return $query->result_array();
}
I have a comments table and a users table, When a user comments on something the
comment is saved as follows
comment > Comment text
user: userid
So when the data is shown I need codeigniter to populate the user field with the user data found from the users table
Does anyone know how to do this ?
I used this functionality in SailsJS but dont know how to do it here in CodeIG
Sails.js populate nested associations
Codeigniter 's active record is not as advanced as SailJS active record, but you can achieve what you are asking for with two queries within the same method.
I'm assuming the comments table has a foreign key to the users table through a user_id field.
public function get_comments() {
/* Get all the comments */
$query = $this->db->get('comments');
$comments = $query->result_array();
/* Loop through each comment, pulling the associated user from the db */
foreach $comments as &$comment {
$query = $this->db->get_where('users', array('id' => $comment['user_id']));
$user = $query->result_array();
/* Put the user's data in a key called 'user' within the comment array */
$comment['user'] = $user;
/* Remove the unnecessary user_id and $user variable */
unset($comment['user_id']);
unset($user);
}
return $comments;
}