Last active
March 15, 2026 15:50
-
-
Save rudiedirkx/5f3fed826520414ad70586a7543d22a6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require __DIR__ . '/icares-env.php'; // GRAPHQL_ENDPOINT, GRAPHQL_TOKEN | |
| if (isset($_GET['type'], $_POST['q'])) { | |
| echo '<pre>'; | |
| print_r($_POST['q']); | |
| $answers = []; | |
| foreach ($_POST['q'] as $id => $value) { | |
| $answers[] = [ | |
| 'id' => $id, | |
| 'value' => is_array($value) ? implode('', $value) : $value, | |
| ]; | |
| } | |
| // print_r($answers); exit; | |
| echo "\n"; | |
| echo "Invite:\n"; | |
| $testType = $_GET['type']; | |
| var_dump($testType); | |
| [$data, $time] = graphql( | |
| <<<'GRAPHQL' | |
| mutation InviteTest($type: String!) { | |
| invite(test: $type, amount: 1, readable: true) { | |
| type { | |
| type | |
| } | |
| id | |
| } | |
| } | |
| GRAPHQL, | |
| [ | |
| 'type' => $testType . '_test', | |
| ], | |
| ); | |
| print_r($data); | |
| $userTestId = $data['data']['invite'][0]['id']; | |
| var_dump($userTestId); | |
| echo "\n"; | |
| echo "Submit answers:\n"; | |
| [$data, $time] = graphql( | |
| <<<'GRAPHQL' | |
| mutation SaveAnswers($id: Int!, $answers: [SubmitTestAnswer!]!) { | |
| submit_test(id: $id, answers: $answers) { | |
| type { | |
| type | |
| } | |
| started_at | |
| finished_at | |
| secret_url | |
| } | |
| } | |
| GRAPHQL, | |
| [ | |
| 'id' => $userTestId, | |
| 'answers' => $answers | |
| ], | |
| ); | |
| print_r($data); | |
| echo "\n"; | |
| echo "Result:\n"; | |
| if ($testType == 'education_choice') { | |
| [$data, $time] = graphql( | |
| <<<GRAPHQL | |
| query { | |
| education_choice_test(id: $userTestId) { | |
| educations(level: 5, limit: 2) { | |
| total | |
| level { | |
| name | |
| } | |
| nodes { | |
| score | |
| education { | |
| name | |
| } | |
| } | |
| } | |
| } | |
| } | |
| GRAPHQL | |
| ); | |
| print_r($data); | |
| } | |
| elseif ($testType == 'career_choice') { | |
| [$data, $time] = graphql( | |
| <<<GRAPHQL | |
| query { | |
| career_choice_test(id: $userTestId) { | |
| professions(level: MIDDLE, limit: 2) { | |
| total | |
| country_region { | |
| name | |
| } | |
| nodes { | |
| score | |
| profession { | |
| name | |
| } | |
| } | |
| } | |
| } | |
| } | |
| GRAPHQL | |
| ); | |
| print_r($data); | |
| } | |
| elseif ($testType == 'enneagram') { | |
| [$data, $time] = graphql( | |
| <<<GRAPHQL | |
| query { | |
| enneagram_test(id: $userTestId) { | |
| personality_types { | |
| score | |
| category { | |
| number | |
| name | |
| } | |
| } | |
| professions(level: MIDDLE, limit: 2) { | |
| nodes { | |
| score | |
| profession { | |
| name | |
| } | |
| } | |
| } | |
| } | |
| } | |
| GRAPHQL | |
| ); | |
| print_r($data); | |
| } | |
| else { | |
| echo "!! No result API?\n"; | |
| } | |
| exit; | |
| } | |
| [$form0, $time] = graphql( | |
| <<<GRAPHQL | |
| query { | |
| career_choice_test_form { | |
| ...multiple_choices | |
| } | |
| education_choice_test_form { | |
| ...multiple_choices | |
| } | |
| capaz_study_test_form { | |
| __typename | |
| test_type { | |
| name | |
| } | |
| answers { | |
| __typename | |
| value | |
| answer | |
| } | |
| questions { | |
| __typename | |
| id | |
| question | |
| } | |
| } | |
| enneagram_test_form { | |
| __typename | |
| test_type { | |
| name | |
| } | |
| answers { | |
| __typename | |
| index | |
| answer | |
| } | |
| questions { | |
| __typename | |
| id | |
| options { | |
| __typename | |
| value | |
| label | |
| } | |
| } | |
| } | |
| cube8_blackboard_test_form { | |
| ...cube8 | |
| } | |
| cube8_notice_board_test_form { | |
| ...cube8 | |
| } | |
| cube8_photo_test_form { | |
| test_type { | |
| name | |
| } | |
| questions { | |
| id | |
| value | |
| question | |
| answers { | |
| value | |
| gender | |
| url | |
| title | |
| } | |
| } | |
| } | |
| } | |
| fragment multiple_choices on MultipleStaticChoiceTestForm { | |
| __typename | |
| test_type { | |
| name | |
| } | |
| answers { | |
| __typename | |
| value | |
| answer | |
| } | |
| questions { | |
| __typename | |
| id | |
| question | |
| } | |
| } | |
| fragment cube8 on Cube8OnlySortTestForm { | |
| test_type { | |
| type | |
| name | |
| } | |
| question { | |
| id | |
| question | |
| help | |
| } | |
| answers { | |
| gender | |
| value | |
| title | |
| description | |
| } | |
| } | |
| GRAPHQL | |
| ); | |
| $forms1 = [ | |
| 'career_choice' => $form0['data']['career_choice_test_form'], | |
| 'education_choice' => $form0['data']['education_choice_test_form'], | |
| ]; | |
| $form2 = $form0['data']['enneagram_test_form']; | |
| $form3 = $form0['data']['capaz_study_test_form']; | |
| $forms4 = [ | |
| 'cube8_blackboard' => $form0['data']['cube8_blackboard_test_form'], | |
| 'cube8_notice_board' => $form0['data']['cube8_notice_board_test_form'], | |
| ]; | |
| $form5 = $form0['data']['cube8_photo_test_form']; | |
| ?> | |
| <title>Icares live testdata demo</title> | |
| <h1>Icares live testdata demo</h1> | |
| <? foreach ($forms1 as $type => $form): ?> | |
| <details> | |
| <summary><?= e($form['test_type']['name']) ?> - <?= count($form['questions']) ?> questions</summary> | |
| <form method="post" action="?type=<?= $type ?>"> | |
| <? foreach ($form['questions'] as $question): ?> | |
| <p><?= e($question['question']) ?></p> | |
| <p> | |
| <input type="range" name="q[<?= $question['id'] ?>]" min="1" max="<?= count($form['answers']) ?>" value="1"> | |
| <span><?= e($form['answers'][0]['answer']) ?></span> | |
| </p> | |
| <? endforeach ?> | |
| <p><button>Submit data, get result</button></p> | |
| </form> | |
| </details> | |
| <script> | |
| window.addEventListener('load', function(e) { | |
| const labels = <?= json_encode(array_column($form['answers'], 'answer', 'value')) ?>; | |
| document.querySelectorAll('form[action*="<?= $type ?>"] input[type="range"]').forEach(el => el.addEventListener('input', function(e) { | |
| this.nextElementSibling.textContent = labels[this.value] || '?'; | |
| })); | |
| }); | |
| </script> | |
| <? endforeach ?> | |
| <details> | |
| <summary><?= e($form3['test_type']['name']) ?> - <?= count($form3['questions']) ?> questions</summary> | |
| <form method="post" action="?type=capaz_study"> | |
| <? foreach ($form3['questions'] as $question): ?> | |
| <p><?= e($question['question']) ?></p> | |
| <ul> | |
| <? foreach ($form3['answers'] as $answer): ?> | |
| <li> | |
| <label> | |
| <input type="radio" name="q[<?= $question['id'] ?>]" value="<?= $answer['value'] ?>"> | |
| <?= e($answer['answer']) ?> | |
| </label> | |
| </li> | |
| <? endforeach ?> | |
| </ul> | |
| <? endforeach ?> | |
| <p><button>Submit data, get result</button></p> | |
| </form> | |
| </details> | |
| <details> | |
| <summary><?= e($form2['test_type']['name']) ?> - <?= count($form2['questions']) ?> questions</summary> | |
| <form method="post" action="?type=enneagram"> | |
| <? $answers = array_column($form2['answers'], 'answer', 'index') ?> | |
| <? foreach ($form2['questions'] as $question): ?> | |
| <p>Sort from 'most like me' to 'least like me':</p> | |
| <ul> | |
| <? foreach (['123', '132', '213', '231', '312', '321'] as $order): ?> | |
| <? $options = array_map(fn($n) => $question['options'][$n-1], str_split($order)) ?> | |
| <li> | |
| <label> | |
| <input type="radio" name="q[<?= $question['id'] ?>]" value="<?= strrev(implode('', array_column($options, 'value'))) ?>"> | |
| ๐ | |
| <?= e(implode(' > ', array_map(fn(array $option) => $option['label'] . ' (' . $option['value'] . ')', $options))) ?> | |
| ๐ | |
| </label> | |
| </li> | |
| <? endforeach ?> | |
| </ul> | |
| <!-- <table style="margin-left: 2rem"> | |
| <? foreach ($question['options'] as $i => $option): ?> | |
| <tr class="option" data-rated="<?= 3 - $i ?>"> | |
| <td><?= e($option['label']) ?></td> | |
| <td><input type="number" name="q[<?= $question['id'] ?>][<?= $option['key'] ?>]" min="1" max="3" value="<?= 3 - $i ?>"></td> | |
| <td> | |
| <? foreach ($answers as $value => $label): ?> | |
| <span class="answer rating-<?= $value ?>"><?= e($label) ?></span> | |
| <? endforeach ?> | |
| </td> | |
| </tr> | |
| <? endforeach ?> | |
| </table> --> | |
| <? endforeach ?> | |
| <p><button>Submit data, get result</button></p> | |
| </form> | |
| </details> | |
| <? foreach ($forms4 as $type => $form): ?> | |
| <details> | |
| <? $gender = rand(0, 1) ? 'm' : 'f' ?> | |
| <summary><?= e($form['test_type']['name']) ?> - <?= strtoupper($gender) ?></summary> | |
| <p>Sort from 'like' (top) to 'not like' (bottom). Click to select a row, click again to move it there.</p> | |
| <form method="post" action="?type=<?= $type ?>"> | |
| <? $answers = array_filter($form['answers'], fn(array $answer) => !$answer['gender'] || $answer['gender'] == $gender) ?> | |
| <? $question = $form['question'] ?> | |
| <ol class="click-sort"> | |
| <? foreach ($answers as $answer): ?> | |
| <li> | |
| <input type="hidden" name="q[<?= $question['id'] ?>][]" value="<?= $answer['value'] ?>"> | |
| <b><?= e(str_replace("\n", ' / ', $answer['title'])) ?></b><br> | |
| <?= nl2br(e($answer['description'])) ?> | |
| </li> | |
| <? endforeach ?> | |
| </ol> | |
| <p><button>Submit data, get result</button></p> | |
| </form> | |
| </details> | |
| <? endforeach ?> | |
| <style> | |
| li.sorting { | |
| background-color: yellow; | |
| } | |
| </style> | |
| <script> | |
| window.addEventListener('load', () => document.querySelectorAll('.click-sort > li').forEach(el => el.addEventListener('click', function(e) { | |
| const ol = this.parentNode; | |
| const source = ol.querySelector('.sorting'); | |
| if (source) { | |
| ol.insertBefore(source, this); | |
| setTimeout(() => source.classList.remove('sorting'), 500); | |
| } | |
| else { | |
| this.classList.add('sorting'); | |
| } | |
| }))); | |
| </script> | |
| <details open> | |
| <? $gender = rand(0, 1) ? 'm' : 'f' ?> | |
| <summary><?= e($form5['test_type']['name']) ?> - <?= strtoupper($gender) ?></summary> | |
| <form method="post" action="?type=cube8_photo"> | |
| <? foreach ($form5['questions'] as $question): ?> | |
| <? if ($question['value']): ?> | |
| <p>Which image looks most interesting? (See tooltips)</p> | |
| <p data-copy-photo-from="<?= $question['value'] ?>"> | |
| <? foreach ($question['answers'] as $answer): | |
| if ($answer['gender'] != $gender) continue; | |
| ?> | |
| <label> | |
| <input type="radio" name="q[<?= $question['id'] ?>]" value="<?= $answer['value'] ?>" style="visibility: hidden"> | |
| <img src="<?= e($answer['url']) ?>" width="200" title="<?= e($answer['title']) ?>"> | |
| </label> | |
| <? endforeach ?> | |
| </p> | |
| <? else: ?> | |
| <p>Sort your images from interesting (first) to boring (last). Click to select a row, click again to move it there.</p> | |
| <ol class="click-sort"> | |
| <? foreach (array_filter($form5['questions'], fn($q) => $q['value']) as $q): ?> | |
| <li> | |
| <input type="hidden" name="q[<?= $question['id'] ?>][]" value="<?= $q['value'] ?>"> | |
| <img src="" width="200" data-copy-photo-to="<?= $q['value'] ?>"> | |
| </li> | |
| <? endforeach ?> | |
| </ol> | |
| <? endif ?> | |
| <? endforeach ?> | |
| <p><button>Submit data, get result</button></p> | |
| </form> | |
| </details> | |
| <style> | |
| input:checked + img { | |
| outline: solid 10px lime; | |
| } | |
| img[src=""] { | |
| display: none; | |
| } | |
| </style> | |
| <script> | |
| window.addEventListener('load', () => document.querySelectorAll('[data-copy-photo-from]').forEach(el => el.addEventListener('change', function(e) { | |
| const value = this.dataset.copyPhotoFrom; | |
| // console.log(value); | |
| const img = e.target.nextElementSibling; | |
| const src = img.src; | |
| const title = img.title; | |
| // console.log(src); | |
| const to = document.querySelector(`img[data-copy-photo-to="${value}"]`); | |
| // console.log(to); | |
| to.src = src; | |
| to.title = title; | |
| }))); | |
| </script> | |
| <br> | |
| <details> | |
| <summary>Forms API response (<?= round($time, 1) ?> ms)</summary> | |
| <pre><?= e(json_encode($form0, JSON_PRETTY_PRINT)) ?></pre> | |
| </details> | |
| <?php | |
| function e(?string $text) { | |
| return htmlspecialchars($text ?? '', ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); | |
| } | |
| function graphql(string $query, array $variables = []) : array { | |
| $t = hrtime(true); | |
| $ch = curl_init(GRAPHQL_ENDPOINT); | |
| curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| curl_setopt($ch, CURLOPT_POST, true); | |
| curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([ | |
| 'query' => $query, | |
| 'variables' => (object) $variables, | |
| ])); | |
| curl_setopt($ch, CURLOPT_HTTPHEADER, [ | |
| 'Content-Type: application/json', | |
| 'Accept: application/json', | |
| 'Authorization: Bearer ' . GRAPHQL_TOKEN, | |
| ]); | |
| $json = curl_exec($ch); | |
| if (curl_errno($ch)) { | |
| throw new RuntimeException('HTTP ERROR:' . curl_error($ch)); | |
| } | |
| curl_close($ch); | |
| $t = (hrtime(true) - $t) / 1e6; | |
| $data = json_decode($json, true); | |
| if (isset($data['errors'][0]['message'])) { | |
| throw new RuntimeException('GRAPHQL ERROR: ' . $data['errors'][0]['message']); | |
| // echo '<pre>'; | |
| // print_r($data); | |
| } | |
| if (empty($data['data'])) { | |
| throw new RuntimeException('JSON ERROR:' . $json); | |
| } | |
| return [$data, $t]; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment