Multi-level Registration in Drupal - php

How to implement two type of registration like student and teacher?
I need two type of registration one for Teacher and one for Student. Both are different registration and both have different roles. Is it possible in Drupal? And also I need registering Student there is no admin approval but for Teacher registration, admin approval is required. How can I achieve this in Drupal 6?

In the custom user registration form add one more select box field with ROLE TYPE (student, teacher).Then on the submit hook check like shown below.
function add_student_form_submit($form, &$form_state) {
$fields = array();
$fields['is_new'] = true;
$fields['name'] = $form_state['values']['user_name'];
$fields['pass'] = $form_state['values']['pass'];
$role_type = $form_state['values']['role_type'];
//Add the user to the corresponding role
$fields['roles'] = array($role_type)
//here you can achieve the thing which you want.If the role is a teacher then set
//status = 0, else status = 1
if($role_type == 'student')
$fields['status'] = 1;
else
$fields['status'] = 0; // $user = user_save(drupal_anonymous_user(), $fields); //This works in D7
$user = user_save('', $fields); //pretty sure this is what works in D6 }
If the user is the teacher you should go to http://localhost/domain_name/admin/user/user. Here you can filter the Inactive users and activate them.

hey you can use below modules for creating multilevel registration.
http://drupal.org/project/content_profile
http://drupal.org/project/autoassignrole
above modules help you to create multilevel registration form in site. from content profile you can create form and also give auto assign role to student.

As far as i'm aware, drupal doesn't provide any mechanism for having multiple types of registration forms. However you can fairly easily create your own registration form from scratch. All you really need is the user_save function to create a new user. See the sample code below as part of a form_submit hook
function add_student_form_submit($form, &$form_state) {
$fields = array();
$fields['is_new'] = true;
$fields['name'] = $form_state['values']['user_name'];
$fields['pass'] = $form_state['values']['pass'];
$fields['status'] = 1;
// $user = user_save(drupal_anonymous_user(), $fields); //This works in D7
$user = user_save('', $fields); //pretty sure this is what works in D6
}
Using this you can create whatever custom logic you want for each form

Related

Wordpress update user meta doesn't work with strtolower

I've a problem with the wp_update_user in wordpress. The code below works, but not as I want.
add_action('user_register', 'register_role', 10 , 1);
function register_role($user_id) {
$userdata = array();
$userdata['ID'] = $user_id;
$userdata['role'] = $_POST['Newrole']; //value for example 'Goldmember'
wp_update_user($userdata);
}
I have several problems:
the "Goldmember" is the displayname of the userrole. I see the new usermeta in the database (a:2:{s:6:"Goldmember";b:1;s:6:"subscriber";b:1;})
the "Goldmember" doesn't work in wordpress user administration, and the user has the default role activated
if I change "Goldmember" to "goldmember" with strtolower($_POST['Newrole']) the code doesn't work anymore
if I change the "Goldmember" to "goldmember" in the database (a:2:{s:6:"goldmember";b:1;s:6:"subscriber";b:1;}), it is visible in wordpress user administration and works
what do I have to change in my code, I don't see any mistake.
thanks a lot
I've changed my way to solve the problem. I took an other hook to change the userrole on registration.
I took the "uwp_after_process_register", this one will be fired at the whole end of the registration process, so there I can do, whatever I want.
This is now my solution, so every problem I had is solved, you only have to take UsersWP:
add_action('uwp_after_process_register', 'register_role', 10 , 2);
function register_role($data, $user_id) {
$userdata = array();
$userdata['ID'] = $user_id;
$userdata['role'] = $data['FormFieldRole'];
wp_update_user($userdata);
}

Prefill CF7 data from the database using a dynamic field

I am trying to create a method through which I can retrieve the data from the database of contacts saved by contact form 7 and pre-fill the fields after the users have previously selected, from a drop-down menu, the PayPal payment method and filled out the form , without then proceeding to the actual payment.
The flow to follow is this:
If the user has chosen to pay with PayPal and filled out the entire form, the "paid" item will be 0 and without making any redirects to the PayPal page
Then, when the user returns to the event card, we will show all the fields filled in previously and the "pay with paypal" button to complete the paypal payment. So the "paid" item will be 1.
So far I have used the following plugins to save the data of the completed forms Advanced CF7 DB and Contact Form CFDB7.
The use of one of these, or someone else, is indifferent to my goal
Here is the code with which so far I am able to populate the dynamic and hidden field [dynamichidden paid ""] when the user selects PayPal but without too much success:
add_action('wpcf7_posted_data', 'course_registration_actions_paypal', 10, 1);
function course_registration_actions_paypal($stato0){
$paypal["paymentmethod"] = '0';
$stato0[“paid”] = '0';
$stato1[“paid”] = '1';
if (isset($paypal[“paymentmethod]) && $stato0[“paid”] === '0') {
return $stato0;
} else {
return $stato1;
}
};
Eventually I managed to get it to work as I wanted, but there remains a redirect problem using the "redirection for contact form 7" plugin.
Always redirect on the first case (paypal) rather than differentiate.
I think the problem is 'wpcf7_posted_data' as without redirects they work fine.
This is my code:
add_action('wpcf7_posted_data','course_registration_actions_persist_payment_status', 10, 1);
function course_registration_actions_persist_payment_status($record){
$current_user = wp_get_current_user();
$userId = get_field('id__pro', 'user_' . $current_user->ID);
$eventId = get_field('id', false);
$records = WPCF7_ContactForm::find([
'ID-Course' => $eventId, // 2
'ID-User' => $userId, // 82994
]);
if ($record['paymentmethod'][0] == 0) {
// Paypal
$record['paymentmethod'] = 0;
// No Paid
$record['paid'] = 0;
} else {
// Bank
$record['paymentmethod'] = 1;
// Paid
$record['paid'] = 1;
}
return $record;
};

How to update custom user fields on Drupal 8

I try to update/save a custom Field of User Profile on Drupal 8.
I am lucky to get Values but not to save them back.
Here is my Code, any1 knows why this won’t work?
I've tried different variations of these already.
$user = \Drupal\user\Entity\User::load(1); // Load USER of ID=1
$user->set(‚field_user_curpage‘,38); //set my custom field = 38 !!!
$user->save(); // save …
This is how I Load these fields:
$user = \Drupal\user\Entity\User::load(1); // Load user with id = 1
$curpage_load = $user->get(‚field_user_curpage‘); // Load custom field from User Profile
$curpage = preg_replace(‚/[^0-9]/‚, ‚‘, $curpage_load->value); // filter for No. only
return $curpage; // return output this
The Entity Load function returns a static object.
Try loading the user with this instead.
\Drupal::entityTypeManager()->getStorage('user')->load($id);

Saving multiple new relations with validation (Codeigniter/DataMapper)

I am building an application with codeigniter that involves adding a "carer" with multiple telephone numbers to a database. From the UI side of things, there is an add button next to each telephone field that uses some javascript magic to clone itself in order for the user to input another number. When the form is submitted, the telephone numbers are submitted as an array.
I have two models setup: carer and carer_telephone. Each model has it's own respective table.
I have been racking my brains for a way that I can get datamapper to validate all the relations before saving them. For example at the moment, only the validation errors for the carer fields are displayed, none for the carer_telephone fields.
Also, I'm not sure if this is the most memory efficient way of dealing with this i.e. creating a new carer_telephone object for every number.
This must be a common setup for many applications but I can't seem to find any documentation on the subject. I am looking for the most standard way of doing this with regards to DataMapper.
The controller so far
function add() {
//Create carer object
$c = new carer();
//Create carer telephone object
$t = new carer_telephone();
//Form submitted
if($this->input->post('add_carer')) {
//Set carer data
$c->title = $this->input->post('title');
$c->first_name = $this->input->post('first_name');
$c->family_name = $this->input->post('family_name');
$c->display_name = $this->input->post('display_name');
$c->date_of_birth = $this->input->post('date_of_birth');
$c->email_address = $this->input->post('email_address');
$c->street_address = $this->input->post('street_address');
$c->town = $this->input->post('town');
$c->county = $this->input->post('county');
$c->postcode = $this->input->post('postcode');
//Set and save telephones
foreach($this->input->post('telephone') as $tel) {
$t = new carer_telephone();
$t->type = 'test';
$t->number = $tel;
$c->save($t);
}
}
//Store carer object
$this->_data['content']['carer'] = $c;
//Load view
$this->load->view('carers/add',$this->_data);
}
Any help on this would be greatly appreciated. Even just a link to an example where somebody has worked on this situation.
Best regards,
Dan
There is an array extension that comes with DataMapper which might be of use to you: http://datamapper.wanwizard.eu/pages/extensions/array.html - lets you save array data to a database, etc.

Checking whether a user already exists in drupal

When a user enters his login information and hits submit, i want to check if the user already exists or not.
So, i have the following two questions
1. Which hook is needed to be implemented , for the case when user hits the submit button on the login form. I need the username entered by the user.
2. How to check if a user already exists in drupal or not programmatically ?
Some sample code would be really appreciated.
Please help.
Thank You.
Drupal 7 provides a function to get a user object by name :
$user = user_load_by_name($name);
if(!$user){
// User doesn't exist
}
else {
// User exists
}
http://api.drupal.org/api/drupal/modules%21user%21user.module/function/user_load_by_name/7
This can be done with hook_form_alter:
function module_(&$form, &$form_state, $form_id) {
$user_login_forms = array('user_login', 'user_login_block');
if (in_array($form_id, $user_login_forms)) {
$form['#validate'][] = 'my_validate_function';
}
}
function my_validate_function(&$form, &$form_state) {
$name = $form_state['values']['name'];
// Drupal 6:
if (!db_result(db_query("SELECT COUNT(*) FROM {users} WHERE name = '%s';", $name))) {
// User doesn't exist
}
// Drupal 7:
if (!db_query("SELECT COUNT(*) FROM {users} WHERE name = :name;", array(':name' => $name))->fetchField()) {
// User doesn't exist
}
}
It's better to query the DB directly in this case than than using user_load as it hooks into other modules as well.
In Drupal 7, substitute for this in the validation function:
if (!db_query("SELECT COUNT(*) FROM {users} WHERE name = :name", array(':name' => $name))->fetchField()) {
// User doesn't exist
}
I realize this is almost 2 years old, but user_authenticate does this nicely.
$existing_user = user_authenticate($name,$password);
if($existing_user)
// user exists
else
// user doesn't exist
Hope this helps someone else.
You can try to look on these 2 modules for inspiration: friendly_register and username_check.

Categories