• Resolved elbradey

    (@elbradey)


    Helloo,

    I’ve a form with “Select” field with options inside.
    I need to limit the option selection to be once per user.
    So when the user selects an option and submits. He won’t see that option again but other logged-in users can. (Single time option I mean)

    Could you please guide me how to do so?

    Thanks in advance!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @elbradey

    Is your goal to only allow users to submit a form once?

    If so, this thread has some information and custom code from the developer that’ll get you on the path to accomplishing that.

    Thread Starter elbradey

    (@elbradey)

    Hi @aakash8, thank you for providing the thread!
    Actually the thread is about the one time submission for the entire form.

    My topic is about single option selection for user.
    When the user chose an option once, it won’t appear again (for him) but the rest options will, the selected option will appear for the other users which will also have the same rule for each user.

    I hope I’ve clarified it, I know it’s complex somehow :”

    Thanks in advance for your contribution!

    Hello @elbradey !

    I hope you’re doing great today!

    I’ve asked our SLS team which provided the snippet mentioned by @aakash8 to see if it would be possible to do something like that. We’ll need to check this first as it could require a large amount of custom coding. We’ll get back to you regarding this as soon as we hear back from the SLS team.

    Kind regards,
    Pawel

    Plugin Support Dimitris – WPMU DEV Support

    (@wpmudev-support6)

    Hello there @elbradey

    This will require the addition of a hidden field that stores the user ID. And then need to fetch all the entries for that user ID to find the selected options, to disable/remove that option from the select field. The following MU plugin should help, please install it first in a staging/dev environment for testing, here’s how: https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins

    Please mind changing the form ID 2910 in the code with the form ID of your installation (same as the integer number found in the shortcode).
    Also, we are assuming hidden field ID as hidden-1 and select field ID as select-1, so please also change them if needed.

    <?php
    add_filter( 'forminator_cform_render_fields', function( $wrappers, $model_id ) {
    
    	if( ! is_user_logged_in() ){
            return $wrappers;
        }
    
        if( $model_id != 2910 ){
            return $wrappers;
        }
    
        $select_fields_data = array(
            'select-1' => 'options',
        );
    
        foreach ( $wrappers as $wrapper_key => $wrapper ) {
            if ( ! isset( $wrapper[ 'fields' ] ) ) {
                continue;
            }
    
            if ( isset( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) && ! empty( $select_fields_data[ $wrapper[ 'fields' ][ 0 ][ 'element_id' ] ] ) ) {
                $user_id = get_current_user_id();
    			$last_entry = wpmudev_get_entries_by_user( $model_id, $user_id );
                if ( ! empty( $last_entry ) ) {
    				foreach ( $last_entry as $l_entry ) {
    					if ( ! empty( $l_entry->entry_id ) ) {
    						$entry = Forminator_API::get_entry( $model_id, $l_entry->entry_id );
    						if ( ! empty( $entry ) ) {
    							if ( ! empty( $entry->meta_data['select-1'] ) ) {
    								$selected_value = $entry->meta_data['select-1']['value'];
    								if ( ! empty( $selected_value ) ) {
    									foreach ( $wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ] as $opt_key => $opt_val ) {
    										if ( $opt_val['label'] == $selected_value ) {
    											unset( $wrappers[ $wrapper_key ][ 'fields' ][ 0 ][ 'options' ][$opt_key] );
    										}
    									}
    								}
    							}
    						}
    					}
    				}
                }
            }
        }
    
        return $wrappers;
        
    },10,2);
    
    function wpmudev_get_entries_by_user( $form_id, $user_id ){
        global $wpdb;
        $table_name       = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY_META );
        $entry_table_name = Forminator_Database_Tables::get_table_name( Forminator_Database_Tables::FORM_ENTRY );
        $sql              = "SELECT m.<code>entry_id</code> FROM {$table_name} m LEFT JOIN {$entry_table_name} e ON(e.<code>entry_id</code> = m.<code>entry_id</code>) WHERE e.<code>form_id</code> = %d AND m.<code>meta_key</code> = %s AND m.<code>meta_value</code> = %s order by m.<code>meta_id</code>";
        $entry_id         = $wpdb->get_results( $wpdb->prepare( $sql, $form_id, 'hidden-1', $user_id ) );
        if ( ! empty( $entry_id ) ) {
            return $entry_id;
        }
        return false;
    }

    Thank you,
    Dimitris

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Limit Option Submissions by User’ is closed to new replies.