Welcome to the LimeSurvey Community Forum

Ask the community, share ideas, and connect with other LimeSurvey users!

Create PDF with token and QR code

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 5 days ago #242417 by DenisChenu
Replied by DenisChenu on topic Create PDF with token and QR code

Is there a way to connect to the database in the plugin-code without entering the database user and password?

Want to use something like:
$sql = 'SELECT token FROM lime_tokens_[surveyId]';
 
Plugin or API ?

Plugin : www.yiiframework.com/doc/api/1.1/CActiveRecord#find-detail
Or : github.com/LimeSurvey/LimeSurvey/blob/fb...odels/Token.php#L237

There are no API function currently

BUT : be more clear please !

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.
The following user(s) said Thank You: evaluation2023

Please Log in to join the conversation.

  • evaluation2023
  • evaluation2023's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
1 year 4 days ago #242465 by evaluation2023
Replied by evaluation2023 on topic Create PDF with token and QR code
Now I made slight progress:

I created the actions:
Code:
$this->subscribe('beforeToolsMenuRender');
        $this->subscribe('pTsIndex');
        $this->subscribe('pTsCreate');

When I click the Menu Entry, the action "pTsIndex" works fine. This is for getting a form to enter the wanted number of new created tokens.
Code:
    public function pTsIndex($surveyId) 
    {
        if (!Permission::model()->hasSurveyPermission($surveyId, 'tokens', 'read')) {
            throw new CHttpException(403, gT("You do not have permission to access this page."));
        }
        
        $surveyId = (int) $surveyId;
        $survey = Survey::model()->findByPk($surveyId);        
        $tkcount = Token::model($surveyId)->count();    
        
        if (!$survey->hasTokensTable) {
            // If no tokens table exists
            $this->newtokentable($iSurveyId);
        }
        Yii::app()->loadHelper("surveytranslator");
        
 
        
        if ($survey->hasTokensTable)  {
            $content = "There are a lot of  Tokens in the database for this survey<br/>
 
                                    <label for='ssid' class='control-label'>Umfrage-ID</label>
                                    <input type='text' class='form-control' id='ssid' name='ssid' placeholder='$surveyId' readonly>
    
                                    <label for='stitle' class='control-label'>Titel</label>
                                    <input type='text' class='form-control' id='stitle' name='stitle' readonly>
    
                                    <label for='count_tokens' class='control-label'>Anzahl TANs</label>
                                    <input type='text' class='form-control' id='scount_tokens' name='scount_tokens' placeholder='$tkcount' readonly>
    
                                    <label for='count_tokens' class='control-label'>Anfang</label>
                                    <input type='text' class='form-control' id='sstartdate' name='sstartdate' readonly>
 
                                    <label for='count_tokens' class='control-label'>Ende</label>
                                    <input type='text' class='form-control' id='sexpires' name='sexpires' readonly>
    
                                <form id='tokenForm' action='pluginhelper?sa=printAction&amp;surveyId=$surveyId'>
                                        <input type='text' class='form-control' id='stokencount' name='stokencount' placeholder='Anzahl'>
                                        <input type='submit' id='stokenButton' class='btn btn-primary'>Create Tokens</button>
                                </form>";
 
        } if( isset($_POST['button']) )
        { $content = "ELSE TEST";
        }
        
        
        return $content;        
        
    }

The action "pTsCreate" works fine at the moment, it creates the given Number of Tokens. The next step is to create the QR codes in the PDF.
Code:
    public function pTsCreate($surveyId) 
    {
        if (!Permission::model()->hasSurveyPermission($surveyId, 'tokens', 'read')) {
            throw new CHttpException(403, gT("You do not have permission to access this page."));
        }
        
        $surveyId = (int) $surveyId;
        $stokencount = $_GET["stokencount"];
        for ($i = 0; $i < $stokencount; $i++) {
            $oToken = Token::create($surveyId);
            $oToken->setAttributes(Yii::app()->getRequest()->getParam('tokenattribute'));
            //$resultToken = $oToken->save();        
            $oToken->generateToken();
            $oToken->save();
        }
 
        
        return "It works:
        $surveyId
        $stokencount
        $oToken->token";
        
        
        
        
    }


Now the question:

I use "admin/pluginhelper" for menu creation. So it works to get on the "public function pTsIndex"-part but when clicking the <form>-Button I cant get it work to send the $surveyId & $stokencount  to the function "pTsCreate" - any ideas?

PHP Version 8.0.28
Version 5.6.4+230206

Please Log in to join the conversation.

  • Joffm
  • Joffm's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
1 year 1 day ago #242526 by Joffm
Replied by Joffm on topic Create PDF with token and QR code
Hi, I tried a different approach
using the API and TCPDF.

At first I added the button to the "Help" menu.


Now a php file is called with this rough form


1. The tokens are created
 

2. Clicking "Generate QR" creates a pdf with one QR per page.


Just a really rough idea.

Joffm

 

Volunteers are not paid.
Not because they are worthless, but because they are priceless
The following user(s) said Thank You: DenisChenu, evaluation2023

Please Log in to join the conversation.

  • evaluation2023
  • evaluation2023's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
11 months 4 weeks ago #242646 by evaluation2023
Replied by evaluation2023 on topic Create PDF with token and QR code
That looks pretty good - I got it work to create individual qr codes with token and save it temporary on the server and put 5 codes per page (with a "for"-function). Unfortunately "NewDirectRequest" does not work - I open a bug report...

400: Falsche Anfrage Der CSRF-Token konnte nicht verifiziert werden.

PHP Version 8.0.28
Version 5.6.4+230206

Please Log in to join the conversation.

  • evaluation2023
  • evaluation2023's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
11 months 4 weeks ago #242653 by evaluation2023
Replied by evaluation2023 on topic Create PDF with token and QR code
And could you post the code of your solution? Would be very helpful ;-)

PHP Version 8.0.28
Version 5.6.4+230206

Please Log in to join the conversation.

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 months 4 weeks ago #242654 by DenisChenu
Replied by DenisChenu on topic Create PDF with token and QR code

That looks pretty good - I got it work to create individual qr codes with token and save it temporary on the server and put 5 codes per page (with a "for"-function). Unfortunately "NewDirectRequest" does not work - I open a bug report...

400: Falsche Anfrage Der CSRF-Token konnte nicht verifiziert werden.
 
No : it's not an issue
See manual.limesurvey.org/NewUnsecureRequest
 

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.

Please Log in to join the conversation.

  • evaluation2023
  • evaluation2023's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
11 months 4 weeks ago #242672 by evaluation2023
Replied by evaluation2023 on topic Create PDF with token and QR code
Many thanks for your help, Dennis and Joffm! - unfortunately I tried to use "NewUnsecureRequest" - so in the function which shows the <form>- with an number-input and a send button i added the url which sends then to /plugins/unsecure ... but the viewed page then is blank with no code :-(

So in my class now I have two functions, one is the "index-Function" which shows the <form> with <input number> and <button> and the other is the "create"-function which gets the <input number> and the survey id and then successfull generates the PDF with QR-Codes.... but just, when I access the url manually ...

My question: How can I get it work to call the create-function from the index-function without getting the CSRF-Errors ... I tried to understand your examples, but didn't get it to work...

Maybe I could get a look at Joffm's solution ;-)

PHP Version 8.0.28
Version 5.6.4+230206

Please Log in to join the conversation.

  • DenisChenu
  • DenisChenu's Avatar
  • Offline
  • LimeSurvey Community Team
  • LimeSurvey Community Team
More
11 months 4 weeks ago #242679 by DenisChenu
Replied by DenisChenu on topic Create PDF with token and QR code
I don't really understand the purpose of using NewDirectRequest , but if it work with NewDirectRequest : it work with NewUnsecureRequest : it's exactly the same fuinctoion except unsecure are ,not CRSF protected

BUT : since it's a plugin for admin : why not pluginhelper

manual.limesurvey.org/Add_new_menu_and_view_by_a_plugin

Assistance on LimeSurvey forum and LimeSurvey core development are on my free time.
I'm not a LimeSurvey GmbH member, professional service on demand , plugin development .
I don't answer to private message.

Please Log in to join the conversation.

  • evaluation2023
  • evaluation2023's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
11 months 4 weeks ago #242680 by evaluation2023
Replied by evaluation2023 on topic Create PDF with token and QR code
I tried for 4 hours to solve the problem. Now I just renamed the previous function to "public function NewUnsecureRequest" on a trial basis thanks to your hint and it works. Great.
Is it also "secure" to use this function or is the system then vulnerable?

PHP Version 8.0.28
Version 5.6.4+230206

Please Log in to join the conversation.

  • evaluation2023
  • evaluation2023's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
11 months 4 weeks ago #242681 by evaluation2023
Replied by evaluation2023 on topic Create PDF with token and QR code
One stupid question more (sorry...)

I want to add a "valid from" and "valid until" - Date Picker in the form of my plugin (using /admin/pluginhelper)

In my form I added the code: 
Code:
$dateFrom = Yii::app()->getController()->widget('ext.DateTimePickerWidget.DateTimePicker', [
                                'name' => 'validfrom',
                                'value' => $validfrom ?? '',
                                'pluginOptions' => [
                                    'format' => "DD.MM.YYYY HH:mm",
                                    'allowInputToggle' => true,
                                    'showClear' => true,
                                    'locale' => 'de'
                                ]
                            ]);

Then I get a 500-message:
"500: Interner Serverfehler Object of class DateTimePicker could not be converted to string"

Any ideas?

 

PHP Version 8.0.28
Version 5.6.4+230206
Attachments:

Please Log in to join the conversation.

  • evaluation2023
  • evaluation2023's Avatar Topic Author
  • Offline
  • Junior Member
  • Junior Member
More
11 months 3 weeks ago #242877 by evaluation2023
Replied by evaluation2023 on topic Create PDF with token and QR code
Update: Got it work now... 
When I click on the button, PDF is created with individuale QR-Codes and sent to browser for download

Many thanks for the help!

PHP Version 8.0.28
Version 5.6.4+230206

Please Log in to join the conversation.

Lime-years ahead

Online-surveys for every purse and purpose