Updating a user role when a class (group) is flagged as finished
function mymodule_training_class_node_form_submit($form, &$form_state) {
if ($form_state['input']['field_class_is_finished']['und'] == 1) {
$nid = $form_state['values']['nid'];
$query = db_select('og_membership', 'ogm')
->condition('ogm.gid', $nid, '=')
->fields('ogm', array('etid'));
$result = $query->execute();
foreach ($result as $record) {
$uid = $record->etid;
_mymodule_training_class_assign_alumni_role($uid);
}
}
}
function _mymodule_training_class_assign_alumni_role($uid){
$rid = db_select("users_roles", "ur")
->fields("ur", array("rid"))
->condition('ur.uid', $uid, '=')
->execute()
->fetchField();
if (empty($rid)) {
db_insert('users_roles')
->fields(array(
'uid' => $uid,
'rid' => ALUMNI,
))
->execute();
}
}
mysql> describe field_data_field_class_is_finished
-> ;
+-------------------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------------+------------------+------+-----+---------+-------+
| entity_type | varchar(128) | NO | PRI | | |
| bundle | varchar(128) | NO | MUL | | |
| deleted | tinyint(4) | NO | PRI | 0 | |
| entity_id | int(10) unsigned | NO | PRI | NULL | |
| revision_id | int(10) unsigned | YES | MUL | NULL | |
| language | varchar(32) | NO | PRI | | |
| delta | int(10) unsigned | NO | PRI | NULL | |
| field_class_is_finished_value | int(11) | YES | MUL | NULL | |
+-------------------------------+------------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
mysql> mysql> describe og_membership;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| type | varchar(255) | NO | | | |
| etid | int(10) unsigned | NO | MUL | 0 | |
| entity_type | varchar(32) | NO | | | |
| gid | int(11) | NO | MUL | NULL | |
| group_type | varchar(32) | NO | MUL | | |
| state | varchar(255) | YES | | | |
| created | int(11) | NO | | 0 | |
| field_name | varchar(255) | NO | | | |
| language | varchar(12) | NO | | | |
+-------------+------------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)
mysql> select * from og_membership where gid=1304;
+------+----------------------------+-------+-------------+------+------------+-------+------------+--------------+----------+
| id | type | etid | entity_type | gid | group_type | state | created | field_name | language |
+------+----------------------------+-------+-------------+------+------------+-------+------------+--------------+----------+
| 8275 | og_membership_type_default | 1 | user | 1304 | node | 1 | 1402485115 | og_user_node | en |
| 8276 | og_membership_type_default | 10106 | user | 1304 | node | 1 | 1402485280 | og_user_node | en |
| 8277 | og_membership_type_default | 10113 | user | 1304 | node | 1 | 1402485286 | og_user_node | en |
| 8278 | og_membership_type_default | 10114 | user | 1304 | node | 1 | 1402485292 | og_user_node | en |
+------+----------------------------+-------+-------------+------+------------+-------+------------+--------------+----------+
4 rows in set (0.00 sec)
mysql> describe users;
+------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| uid | int(10) unsigned | NO | PRI | 0 | |
| name | varchar(60) | NO | UNI | | |
| pass | varchar(128) | NO | | | |
| mail | varchar(254) | YES | MUL | | |
| theme | varchar(255) | NO | | | |
| signature | varchar(255) | NO | | | |
| signature_format | varchar(255) | YES | | NULL | |
| created | int(11) | NO | MUL | 0 | |
| access | int(11) | NO | MUL | 0 | |
| login | int(11) | NO | | 0 | |
| status | tinyint(4) | NO | | 0 | |
| timezone | varchar(32) | YES | | NULL | |
| language | varchar(12) | NO | | | |
| picture | int(11) | NO | MUL | 0 | |
| init | varchar(254) | YES | | | |
| data | longblob | YES | | NULL | |
+------------------+------------------+------+-----+---------+-------+
16 rows in set (0.01 sec)
mysql> select * from users where uid=10106;
+-------+----------------------+---------------------------------------------------------+------------------------------------------------+-------+-----------+------------------+------------+------------+-------+--------+------------------+----------+---------+------------------------------------------------+------+
| uid | name | pass | mail | theme | signature | signature_format | created | access | login | status | timezone | language | picture | init | data |
+-------+----------------------+---------------------------------------------------------+------------------------------------------------+-------+-----------+------------------+------------+------------+-------+--------+------------------+----------+---------+------------------------------------------------+------+
| 10106 | user_authenticated_1 | $S$DBGDqh770IDr09aztKD8Ey8aNGxwx8iiCaYo/rGCcBpa5XzNKnDF | identity+user_authenticated_1@paulbooker.co.uk | | | full_html | 1401792983 | 1401794533 | 0 | 1 | America/New_York | | 0 | identity+user_authenticated_1@paulbooker.co.uk | NULL |
+-------+----------------------+---------------------------------------------------------+------------------------------------------------+-------+-----------+------------------+------------+------------+-------+--------+------------------+----------+---------+------------------------------------------------+------+
1 row in set (0.00 sec)
mysql> select * from users where uid=10113;
+-------+----------------------+---------------------------------------------------------+------------------------------------------------+-------+-----------+------------------+------------+--------+-------+--------+------------------+----------+---------+------------------------------------------------+------+
| uid | name | pass | mail | theme | signature | signature_format | created | access | login | status | timezone | language | picture | init | data |
+-------+----------------------+---------------------------------------------------------+------------------------------------------------+-------+-----------+------------------+------------+--------+-------+--------+------------------+----------+---------+------------------------------------------------+------+
| 10113 | user_authenticated_2 | $S$DeG84e0QP/H2h2rGv6cw93krL3CDoQ6CZOzhiSQCZa4OpZOAeP21 | identity+user_authenticated_2@paulbooker.co.uk | | | full_html | 1402485227 | 0 | 0 | 1 | America/New_York | | 0 | identity+user_authenticated_2@paulbooker.co.uk | NULL |
+-------+----------------------+---------------------------------------------------------+------------------------------------------------+-------+-----------+------------------+------------+--------+-------+--------+------------------+----------+---------+------------------------------------------------+------+
1 row in set (0.00 sec)
mysql> select * from users where uid=10114;
+-------+----------------------+---------------------------------------------------------+------------------------------------------------+-------+-----------+------------------+------------+--------+-------+--------+------------------+----------+---------+------------------------------------------------+------+
| uid | name | pass | mail | theme | signature | signature_format | created | access | login | status | timezone | language | picture | init | data |
+-------+----------------------+---------------------------------------------------------+------------------------------------------------+-------+-----------+------------------+------------+--------+-------+--------+------------------+----------+---------+------------------------------------------------+------+
| 10114 | user_authenticated_3 | $S$D4xWR53hWUcyoZmIuZOLv7K8oasOsPCmqWaQGT.kpMQiX9k7XpfD | identity+user_authenticated_3@paulbooker.co.uk | | | full_html | 1402485256 | 0 | 0 | 1 | America/New_York | | 0 | identity+user_authenticated_3@paulbooker.co.uk | NULL |
+-------+----------------------+---------------------------------------------------------+------------------------------------------------+-------+-----------+------------------+------------+--------+-------+--------+------------------+----------+---------+------------------------------------------------+------+
1 row in set (0.00 sec)
mysql> select rid from users_roles where uid=10106;
+-----+
| rid |
+-----+
| 7 |
+-----+
1 row in set (0.00 sec)
Tags: