======Randomize Media Files====== Images, videos and audio files can be randomized using [[en:create:php|PHP code]]. Therefore the files get included with HTML texts. The files must already be stored in **images and media files** (see also [[en:create:images|]] and [[en:create:media|media files in questionnaire]]). **Note:** Since the inclusion of the stimuli is already the 2nd step of a randomization, be sure to read the [[en:create:randomization-php|]] chapter first, as it describes exactly how to create the required [[en:create:questions:random|]] (step 1) and where to place your PHP code on the page. **Tip:** Media files can also be randomized using only a [[en:create:questions:random|random generator]] and HTML. Read the chapter [[en:create:randomization-einfaktoriell|]], in most cases this simpler approach is sufficient. How the PHP codes for the individual file types can look like is described in more detail below. In general, however, the PHP codes look the same, because the files are each created as HTML texts (see [[en:create:media|]]) and then displayed. ===== Stimulus: Image===== **Tip:** This option offers a lot of room for design and editing for more complex representations through the HTML text (see below: example of extensive HTML codes). For simpler inclusion of images, the option described in //[[en:create:randomization-einfaktoriell|]]// is more suitable. ====Example: Randomization of 2 Images==== If there are 2 images to randomize in your example, the corresponding PHP code to include them using HTML text could look like this: if (value('RG01') == 1) { text('BT01'); //The shown text is the HTML-Text of the image with the ID BT01 } elseif (value('RG01') == 2) { text('BT02'); //The shown text is the HTML-Text of the image with the ID BT02 } ====Example: Randomization of 4 Images==== Especially in experiments, it is often necessary to randomly distribute participants between experimental and control groups or, in other words, to randomly choose which of several stimuli to display. The following example shows how to randomize one of 4 images using [[en:create:php|PHP code]]. The prerequisite is that -- as described above -- a random number between 1 and 4 is drawn and stored in a variable (random generator). In addition, the 4 images must be uploaded to the survey project in advance under **Images and Media Files**. In the exaple are randomly the images "stimulus1.png", "stimulus2.png", "stimulus3.png" or "stimulus4.png" displayed. Underneath, for example, a question about the rating of the image could be displayed. This would have to be placed under the PHP code when **Compose Questionnaire**, then all participants will get different stimuli, but always the same question about them: $number = value('RG01'); // Reading the drawn random number // Display graphic depending on the number // (therefore we use HTML code) if ($number == 1) { html('

'); } elseif ($number == 2) { html('

'); } elseif ($number == 3) { html('

'); } elseif ($number == 4) { html('

'); }
Instead of using ''html()'' to show a text or a graphic, you can of course use ''[[:en:create:functions:text|text()]]'' to show a longer text, embed a video or use ''[[:en:create:functions:question|question()]]'' to show a question. ==== Randomization Images via Placeholders==== The following PHP code can also be used to include images, but it does not work with complete pieces of HTML code, but with a [[en:create:placeholders|placeholder]] ''%filename%'' and the function ''[[:en:create:functions:replace]]''. $code = value('RG01'); // Reading the drawn random number // Write graphic file into a placeholder depending on the random number if ($code == 1) { replace('%filename%', 'stimulus1.png'); } elseif ($code == 2) { replace('%filename%', 'stimulus2.png'); } elseif ($code == 3) { replace('%filename%', 'stimulus3.png'); } elseif ($code == 4) { replace('%filename%', 'stimulus4.png'); } // You can include the HTML code directly or (not shown here) as a text module html('

');
**Tip:** Placeholders are especially helpful when more HTML code is required to include the stimulus than shown here as an example. **Tip:** The name of the PHP variable (in the examples ''$number'' and ''$code'') is arbitrarily chosen. For more information, see [[https://www.soscisurvey.de/help/doku.php/de:create:variables#php-variablen|PHP variables]]. ==== Texts in the Random Generator and Placeholders ==== The PHP codes above have only used the (numerical) code drawn by the random generator. However, you can also use the content of the slips of paper in the random generator. If you are working with images, it is a good idea to store the image names in the random generator. For example 1 = stimulus1.png 2 = stimulus2.png 3 = stimulus3.png 4 = stimulus4.png 5 = stimulus5.png 6 = stimulus6.png While you use ''value('RG01')'' to access the numeric code, you can use ''value('RG01', 'label')'' to access the content of the code that was drawn. Using ''replace('%image%', 'RG01', 'response')'' you can also write the content directly into a placeholder. The previous PHP code can thus be written as follows. question('RG01'); replace('%filename%', 'RG01', 'response'); html('

');
If you draw several codes during an interview, you only have to adjust the name of the variable according to the **Variables Listing**, e.g. question('RG01'); // Show the image according to the first code drawn replace('%filename1%', 'RG01x1', 'response'); html('

'); // Show the image according to the second code drawn replace('%filename2%', 'RG01x2', 'response'); html('

');
=====Stimulus: Video===== Videos can also be embedded using a text() function. Therefore you need the same code as for the texts and embed the video via HTML text. The videos have to be saved in **Images and Mediafiles** and written in a [[en:create:texts|Text]]. A code in which one control group is shown no stimulus and the other two groups are randomized to two videos might look like this: if (value('RG01') == 2) { text('VT01'); //HTML text of the video with the ID VT01 } elseif (value('RG01') == 3) { text('VT02'); //HTML text of the video with the ID VT02 } =====Stimulus: Audio===== Audio files can also be embedded using a text() function. To do this, you need the same code as for the texts and embed the audio via [[en:create:php|PHP-Code]] on the corresponding questionnaire page (see step2). The HTML text of the individual audios already specifies how they are to be displayed and played. For a more detailed explanation, be sure to read the chapters [[en:create:media|Include audio files]] and [[en:create:texts|]] and again save the audio files beforehand under **Images and Media Files**. The PHP code to include the audio files on the questionnaire page could look like this if there are 3 groups and only 2 groups should have audio played to them (a control group without audio). if (value('RG01') == 2) { text('AT01'); //HTML text ot the audio with the ID AT01 } elseif (value('RG01') == 3) { text('AT02'); //HTML text ot the audio with the ID AT02 } ===== Extensive HTML codes ===== If stimuli require extensive HTML code (e.g., videos), if the HTML code differs greatly between experimental groups, or if working with a [[:en:create:multilang|multilingual questionnaire]], four texts are created in the questionnaire (e.g., "RG02" to "RG05", //presentation// each HTML code) and included depending on the random draw: $code = value('RG01'); // Reading the drawn random number // Display text respectively HTML code as text if ($code == 1) { text('RG02'); } elseif ($code == 2) { text('RG03'); } elseif ($code == 3) { text('RG04'); } elseif ($code == 4) { text('RG05'); }