For the purpose described in the issue title, following this discussion, thedut wrote:

Using Rules, I suggest you :

  1. automatically create a node answer with the connected user as author,
  2. then retrieving the token value,
  3. then build the survey url,
  4. and then redirecting to this url in order to complete the survey.

Forgive me if my Drupal skills are not entirely up to it. Anyway this is what I found while trying to execute your suggestion:

Concerning suggested step 1:

I end up with this:

Notice: Undefined property: stdClass::$ls_status em ls_answ_insert() (linha 584 de domain.com/public_html/sites/all/modules/limesurvey_sync/ls_answ/ls_answ.module).
Notice: Undefined property: stdClass::$ls_sync em ls_answ_insert() (linha 585 de domain.com/public_html/sites/all/modules/limesurvey_sync/ls_answ/ls_answ.module).
Notice: Undefined property: stdClass::$ls_last_import em ls_answ_insert() (linha 586 de domain.com/public_html/sites/all/modules/limesurvey_sync/ls_answ/ls_answ.module).
Notice: Undefined property: stdClass::$ls_sid em ls_answ_insert() (linha 587 de domain.com/public_html/sites/all/modules/limesurvey_sync/ls_answ/ls_answ.module).
Notice: Undefined property: stdClass::$ls_lang em ls_answ_insert() (linha 588 de domain.com/public_html/sites/all/modules/limesurvey_sync/ls_answ/ls_answ.module).
Notice: Undefined property: stdClass::$ls_aid em ls_answ_insert() (linha 589 de domain.com/public_html/sites/all/modules/limesurvey_sync/ls_answ/ls_answ.module).
Notice: Undefined property: stdClass::$ls_token em ls_answ_insert() (linha 590 de domain.com/public_html/sites/all/modules/limesurvey_sync/ls_answ/ls_answ.module).
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'ls_last_import' cannot be null: INSERT INTO {ls_answ} (vid, nid, ls_status, ls_sync, ls_last_import, ls_sid, ls_lang, ls_aid, ls_token) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8); Array ( [:db_insert_placeholder_0] => 1420 [:db_insert_placeholder_1] => 80 [:db_insert_placeholder_2] => [:db_insert_placeholder_3] => [:db_insert_placeholder_4] => [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => [:db_insert_placeholder_7] => [:db_insert_placeholder_8] => ) em ls_answ_insert() (linha 592 de domain.com/public_html/sites/all/modules/limesurvey_sync/ls_answ/ls_answ.module).

While executing this rule:

{ "rules_create_survey" : {
    "LABEL" : "Create Survey",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "facebook_rules" ],
    "ON" : { "facebook_rules_registered_through_fbconnect" : [] },
    "DO" : [
      { "entity_create" : {
          "USING" : {
            "type" : "node",
            "param_type" : "inquerito1",
            "param_title" : "[site:current-user]",
            "param_author" : [ "site:current-user" ]
          },
          "PROVIDE" : { "entity_created" : { "inquerito1" : "Inquerito1" } }
        }
      }
    ]
  }
}

I thought that just creating the node entity would populate the fields by default, but I am guessing I need to set each field value individually!?!? I did try adding actions for "Set a data value" after "Create a new entity" in same the Rule but most of the stuff/fields (most notoriously the ones pertaining synchronization) is/are generated by the module upon creation and does/do not seem to be available.

Note 1: Same happens using events "User logged in using Facebook connect.", "After Saving a New User Account", "User has logged in" or even "Content is viewed" by logged in user.

Note 2: I have switched OFF the "The responses title can not be changed" option (the idea being the node to have the unique username has title). Same happens using user:uid.

Concerning suggested step 2:

How? I do not seem to find a way to retrieve the survey token (for a node survey already created, regardless of the above), neither in Rules nor in Views. In the later the description which goes with the following field title does not match:
"LS answers: Survey language
The invitation and answer token on the LimeSurvey site"

The field indeed pertains the title, shows the language not the token.

I was unable to proceed to steps 3 and 4.

Thanks in advance for any assistance.

EDIT from thedut :
The solution to taking the user directly to start answering is
:

  1. enable the PHP filter core module (/admin/modules)
  2. then import (/admin/config/workflow/rules/reaction/import) this rule :
{ "rules_test_redirect_with_user_authentication" : {
    "LABEL" : "test redirect with user authentication",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "php" ],
    "ON" : { "user_insert" : [] },
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "inquerito1" },
          "PROVIDE" : { "variable_added" : { "ls_type" : "survey type" } }
        }
      },
      { "variable_add" : {
          "USING" : {
            "type" : "text",
            "value" : "\u003C?php \r\n$form_state = array();\r\nmodule_load_include(\u0027inc\u0027, \u0027node\u0027, \u0027node.pages\u0027);\r\n$node = new stdClass(); \/\/ We create a new node object\r\n$node-\u003Etype = $ls_type;\r\n$form_state[\u0027values\u0027][\u0027node\u0027] = $node;\r\n$form_state[\u0027values\u0027][\u0027name\u0027] = $account-\u003Ename;\r\n$form_state[\u0027values\u0027][\u0027op\u0027] = t(\u0027Save\u0027);\r\ndrupal_form_submit($ls_type . \u0027_node_form\u0027, $form_state, $node);\r\n\/\/ Do not display the success message.\r\ndrupal_get_messages(\u0027status\u0027, TRUE);\r\n\/\/ Retrieve the Nid for the created node answer.\r\n$nid = $form_state[\u0027values\u0027][\u0027nid\u0027];\r\nmodule_load_include(\u0027inc\u0027, \u0027limesurvey_sync\u0027, \u0027ls_api\/limesurvey_sync_api_answ\u0027);\r\n\/\/ return the survey survey page url.\r\nreturn str_replace(\u0027 \u0027, \u0027%20\u0027, limesurvey_sync_answ_surveyurl(node_load($form_state[\u0027values\u0027][\u0027nid\u0027])));\r\n?\u003E"
          },
          "PROVIDE" : { "variable_added" : { "ls_answer_url" : "Survey answer URL" } }
        }
      },
      { "redirect" : { "url" : "[ls-answer-url:value]" } }
    ]
  }
}

Then change the event to fit what you want to do.
You may need to edit the rule, replacing "$account" by "$user" depending on the event parameter.

Comments

hvalentim’s picture

Issue summary: View changes
hvalentim’s picture

Issue summary: View changes
hvalentim’s picture

Issue summary: View changes
hvalentim’s picture

Issue summary: View changes
thedut’s picture

Hello hvalentim,

I was wrong proposing such way.
What you want is not so easy. I need to modify the LimeSurvey Sync code.
The main trouble is you don't want to go throught the node/add/your_answer_content_type page.
As a workarround : you can add a javascript code, "clicking" on the submit button on loading the node/add/your_answer_content_type page.

I'm thinking to manage this step in a new way but I don't yet which one.
I'm going to think about your idea : taking the user directly to start answering.

By the way, you may found interest reading this issue :
Limit to 1 answer per survey, proposing to finish an "in progress" answer for connected users.

hvalentim’s picture

OK. I will be looking forward to that.

I am also experimenting with the javascript auto-submit detour. So far tried a couple of variants with no luck. I suspect for it to work one may need to act on <head>. A <script> inside the body is not getting there.

As for the other issue you link to, count me in for whatever beta testing you may need.

Once one starts to reflect on the matter, it is clearer that less steps are a plus. Most of the time people are not that keen on answering to surveys, a redundant page/form in between may very well act as another deterrent.

thedut’s picture

Actually I have created a rule using php code that do what you want in one step :

  • automatically create a node answer with the connected user as author,
  • and then redirecting to the survey url in order to complete the survey.

For trying it, enable the PHP filter core module then import (/admin/config/workflow/rules/reaction/import) this rule :

{ "rules_test_redirect" : {
    "LABEL" : "test redirect",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "php" ],
    "ON" : { "user_insert" : [] },
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "inquerito1" },
          "PROVIDE" : { "variable_added" : { "ls_type" : "survey type" } }
        }
      },
      { "php_eval" : { "code" : "$form_state = array();\r\nglobal $user;\r\nmodule_load_include(\u0027inc\u0027, \u0027node\u0027, \u0027node.pages\u0027);\r\n$node = new stdClass(); \/\/ We create a new node object\r\n$node-\u003Etype = $ls_type;\r\n$form_state[\u0027values\u0027][\u0027node\u0027] = $node;\r\n$form_state[\u0027values\u0027][\u0027author\u0027] = $user-\u003Ename;\r\n$form_state[\u0027values\u0027][\u0027op\u0027] = t(\u0027Save\u0027);  \/\/ this seems to be a required value \r\ndrupal_form_submit($ls_type . \u0027_node_form\u0027, $form_state, $node);\r\n$nid = $form_state[\u0027values\u0027][\u0027nid\u0027];\r\nmodule_load_include(\u0027inc\u0027, \u0027limesurvey_sync\u0027, \u0027ls_api\/limesurvey_sync_api_answ\u0027);\r\ndrupal_goto(limesurvey_sync_answ_surveyurl(node_load($nid)));" } }
    ]
  }
}
hvalentim’s picture

Tried your rule.

The results were:

Notice: Undefined property: stdClass::$name in eval() (line 7 of /srv/www/domain.com/public_html/sites/all/modules/rules/modules/php.eval.inc(146) : eval()'d code).

Status message
Inquérito1 Response #2 966 was created.

Result:
Indeed a new Survey node is created
but it is attributed to user "Anonymous" (as author).

No immediate redirection takes place (perhaps because anonymous users do not permission to answer surveys?)

I tried replacing the event by "User registered using Facebook connect" (in my case users register using Facebook OAuth). The first warning (eval) disappeared. But the node was still created with "Anonymous" as user. No redirection took place. Same thing with the "User has logged in" event.

thedut’s picture

Ok, here is the new rules replacing the one in #7:

  • the author is fixed
  • and the message "Inquérito1 Response #2 966 was created." will not be displayed.

Change the Event to by "User registered using Facebook connect".
In my code, I use the $account variable for the author, but using the "User registered using Facebook connect" event you may need to use another user variable instead, provided by this event.
On my server, the immediate redirection works.

{ "rules_test_redirect" : {
    "LABEL" : "test redirect",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "php" ],
    "ON" : { "user_insert" : [] },
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "inquerito1" },
          "PROVIDE" : { "variable_added" : { "ls_type" : "survey type" } }
        }
      },
      { "php_eval" : { "code" : "$form_state = array();\r\nmodule_load_include(\u0027inc\u0027, \u0027node\u0027, \u0027node.pages\u0027);\r\n$node = new stdClass(); \/\/ We create a new node object\r\n$node-\u003Etype = $ls_type;\r\n$form_state[\u0027values\u0027][\u0027node\u0027] = $node;\r\n$form_state[\u0027values\u0027][\u0027name\u0027] = $account-\u003Ename;\r\n$form_state[\u0027values\u0027][\u0027op\u0027] = t(\u0027Save\u0027);\r\ndrupal_form_submit($ls_type . \u0027_node_form\u0027, $form_state, $node);\r\n\/\/ Retrieve the Nid for the create node answer.\r\n$nid = $form_state[\u0027values\u0027][\u0027nid\u0027];\r\nmodule_load_include(\u0027inc\u0027, \u0027limesurvey_sync\u0027, \u0027ls_api\/limesurvey_sync_api_answ\u0027);\r\n\/\/ Do not display the success message.\r\ndrupal_get_messages(\u0027status\u0027, TRUE);\r\n\/\/ redirect to the survey page.\r\ndrupal_goto(limesurvey_sync_answ_surveyurl(node_load($nid)));" } }
    ]
  }
}
hvalentim’s picture

1 - The event "After saving a new user account" works when the user registers using Facebook as well (after all, a new account is created all the same).

2 - Redirection works both using standard registration form and Facebook Connect button provided tough that the process is initiated in the page domain.com/user

3 - A minor drawback is: because he is immediately redirected to Limesurvey if and when the user returns to the Drupal site he is logged off there (a session was never started).

4 - My problem is: The Facebook OAuth Connect button can be placed anywhere (major advantage) for instance a custom basic page where you explain the purpose of the Survey and invite the user to take part. Under these circumstances even with your Rule applied by default Drupal seems to redirect the user to the initial page (not /user) not to the new node (and LimeSurvey). So it seems some sort of precedence or "force redirection" parameter is needed. Any ideas on this? As it is I replaced the redundant step of going trough /node/add by the need to go trough /user (which I do not wish to expose in the first place, otherwise I may very well get either duplicate answers by the same user using different e-mails or mock submissions by spamming robots).

5 - BTW, converting to the Facebook OAuth event ("User registered using Facebook connect") would nevertheless be easy. Apparently one only needs to replace the "account" variable by "user".

Thanks a bunch.

hvalentim’s picture

Another drawback: whenever an admin custom adds a user in /admin/people/create
the Rule also applies (new survey created and admin redirected to LimeSurvey directly).

But I guess this can be easily changed by adding some sort of role verification.

thedut’s picture

3 - A minor drawback is: because he is immediately redirected to Limesurvey if and when the user returns to the Drupal site he is logged off there (a session was never started).

This new version of the rule fix this issue :

{ "rules_test_redirect_with_user_authentication" : {
    "LABEL" : "test redirect with user authentication",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "php" ],
    "ON" : { "user_insert" : [] },
    "DO" : [
      { "variable_add" : {
          "USING" : { "type" : "text", "value" : "inquerito1" },
          "PROVIDE" : { "variable_added" : { "ls_type" : "survey type" } }
        }
      },
      { "variable_add" : {
          "USING" : {
            "type" : "text",
            "value" : "\u003C?php \r\n$form_state = array();\r\nmodule_load_include(\u0027inc\u0027, \u0027node\u0027, \u0027node.pages\u0027);\r\n$node = new stdClass(); \/\/ We create a new node object\r\n$node-\u003Etype = $ls_type;\r\n$form_state[\u0027values\u0027][\u0027node\u0027] = $node;\r\n$form_state[\u0027values\u0027][\u0027name\u0027] = $account-\u003Ename;\r\n$form_state[\u0027values\u0027][\u0027op\u0027] = t(\u0027Save\u0027);\r\ndrupal_form_submit($ls_type . \u0027_node_form\u0027, $form_state, $node);\r\n\/\/ Do not display the success message.\r\ndrupal_get_messages(\u0027status\u0027, TRUE);\r\n\/\/ Retrieve the Nid for the created node answer.\r\n$nid = $form_state[\u0027values\u0027][\u0027nid\u0027];\r\nmodule_load_include(\u0027inc\u0027, \u0027limesurvey_sync\u0027, \u0027ls_api\/limesurvey_sync_api_answ\u0027);\r\n\/\/ return the survey survey page url.\r\nreturn str_replace(\u0027 \u0027, \u0027%20\u0027, limesurvey_sync_answ_surveyurl(node_load($form_state[\u0027values\u0027][\u0027nid\u0027])));\r\n?\u003E"
          },
          "PROVIDE" : { "variable_added" : { "ls_answer_url" : "Survey answer URL" } }
        }
      },
      { "redirect" : { "url" : "[ls-answer-url:value]" } }
    ]
  }
}
thedut’s picture

Assigned: Unassigned » thedut
Issue summary: View changes
Status: Active » Fixed
thedut’s picture

Issue summary: View changes

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

thedut’s picture

Note : the LimeSurvey Sync 7.x-1.6 release provide a new powerfull support for Rules : 1 new event , 2 new actions and 2 new Rules are availables by default.
No need to import some PHP code anymore, just use the new actions (you can find them into the "LimeSurvey Sync" group).
Just upgrade to 7.x-1.6, install and enable the Rules module, the new rules will be available.