comparison new/test_base.php @ 15:853caf8cd74b

Update
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 04 May 2016 17:25:19 +0100
parents
children 01608b20a12d
comparison
equal deleted inserted replaced
14:f63604ce8f21 15:853caf8cd74b
1 <?php
2 function fisherYatesShuffle(&$items, $seed) // http://stackoverflow.com/questions/6557805/randomize-a-php-array-with-a-seed
3 {
4 @mt_srand($seed);
5 for ($i = count($items) - 1; $i > 0; $i--)
6 {
7 $j = @mt_rand(0, $i);
8 $tmp = $items[$i];
9 $items[$i] = $items[$j];
10 $items[$j] = $tmp;
11 }
12 }
13 function url_origin( $s, $use_forwarded_host = false ) //http://stackoverflow.com/questions/6768793/get-the-full-url-in-php
14 {
15 $ssl = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
16 $sp = strtolower( $s['SERVER_PROTOCOL'] );
17 $protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
18 $port = $s['SERVER_PORT'];
19 $port = ( ( ! $ssl && $port=='80' ) || ( $ssl && $port=='443' ) ) ? '' : ':'.$port;
20 $host = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
21 $host = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
22 return $protocol . '://' . $host;
23 }
24
25 function full_url( $s, $use_forwarded_host = false )
26 {
27 return url_origin( $s, $use_forwarded_host ) . $s['REQUEST_URI'];
28 }
29 $toAppendToUrl = '';
30 if(isset($_GET["id"])){
31 $id = $_GET["id"];
32 } else {
33 $max = pow(2, 24);
34 $rand = openssl_random_pseudo_bytes($max);
35 $id = sha1($rand);
36 if(sizeof($_GET) == 0){ // if the query string is empty
37 $toAppendToUrl = '?'; // start the query string
38 } else {
39 $toAppendToUrl = '&'; // otherwise, append to it
40 }
41 $toAppendToUrl .= 'id='.$id;
42 }
43 if(isset($_GET["next"])){
44 $next = $_GET["next"];
45 } else {
46 $next = 0;
47 }
48
49 $absoluteUrl = full_url($_SERVER).$toAppendToUrl;
50 // echo "<br \>".$absoluteUrl."<br \>";
51 // if there is a "next" in the query string, create a version of $absoluteUrl with
52 // next:=next+1
53 $absoluteUrlSplit = explode('?', $absoluteUrl);
54 $absoluteUrlNextPlusOne = $absoluteUrl;
55 if(sizeof($absoluteUrlSplit) === 2){
56 $queryString = $absoluteUrlSplit[1];
57 parse_str($queryString, $queryStringParsed);
58 $queryStringParsed['next'] += 1;
59 $queryString = http_build_query($queryStringParsed);
60 $absoluteUrlNextPlusOne = $absoluteUrlSplit[0]."?".$queryString;
61 }
62 $defaultTestEntry = Array('url' => null, 'string' => null, 'class' => 'disabled', 'a' => false, 'editable' => false, 'alwaysAccessible' => false);
63
64 require_once('test_list.php'); //this returns $tests
65
66 if($next == sizeof($tests)){
67 // we are done
68 $bottomBox = 'The test is complete, thank you for your participation.';
69 } else {
70 $bottomBox = 'If you want to have a break, come back to this page and continue from where you left, just come back to this URL:<br /><div id="currentUrl">'.$absoluteUrl.'</div>';
71 }
72 // until this point, the content of $tests will always be the same for a given $id,
73 // regardless of how many times we visited this page.
74 for($n = 0; $n < sizeof($tests); $n++ ){
75 //TODO: check if the corresponding file exists
76 // meantime, let us just rely on the GET variable 'next'
77 if($n <= $next){
78 $tests[$n]['a'] = true;
79 $tests[$n]['class'] = 'enabled done';
80 // if we are going to re-run a test, return to the same page
81 $tests[$n]['returnUrl'] = urlencode($absoluteUrl);
82 }
83 if($n == $next){
84 $tests[$n]['editable'] = true;
85 $tests[$n]['class'] = 'enabled editable';
86 // if we are going to run a new test, return to the same page with next:=next+1
87 $tests[$n]['returnUrl'] = urlencode($absoluteUrlNextPlusOne);
88 }
89 if($tests[$n]['alwaysAccessible'] === true){
90 $tests[$n]['class'] .= ' alwaysAccessible';
91 }
92 }
93 ?>
94 <html>
95 <head>
96 <style>
97 ul.tests-list li{
98 margin: 10px 0 5px 0;
99
100 }
101 .done {
102 list-style-image: url('assets/images/checkbox-checked.png');
103 }
104 .done a {
105 color: green;
106 }
107 .editable{
108 list-style-image: url('assets/images/arrow-checkbox-unchecked.png');
109 list-style-position: inside;
110 margin-left: -30px;
111 }
112 .alwaysAccessible a{
113 font-weight: bold;
114 }
115 .alwaysAccessible.done a{
116 /*color: fa5858;*/
117 background: rgba(255,200,200, 150);
118 }
119 .disabled{
120 color: grey;
121 text-decoration: line-through;
122 list-style-image: url('assets/images/checkbox-unchecked-disabled.png');
123 }
124 #currentUrl{
125 font-weight: bold;
126 padding-left: 20px;
127 padding-top: 5px;
128 }
129
130 </style>
131 <script src="js/jquery-2.1.4.js"></script>
132 <script>
133 function confirmEditing(e){
134 var message = 'Are you sure you want to edit this item? All previous changes will be lost';
135 return window.confirm(message);
136 }
137 var elements;
138 $(document).ready(function(){
139 lis = $('ul');
140 elements = $('li.done:not(.editable):not(.alwaysAccessible)', lis);
141 for(var n = 0; n < elements.length; n++){
142 elements[n].onclick = confirmEditing;
143 }
144 history.pushState({}, null, location.pathname+location.search+'<?php echo $toAppendToUrl; ?>');
145
146 // elements = $('li:not(.editable.done)', lis);
147 // for(var element in elements){
148 // element.onclick = confirmEditing;
149 // }
150 });
151 </script>
152 </head>
153 <body>
154
155 <ul class = "tests-list">
156 <?php foreach($tests as $n => $test) : ?>
157 <li class="test-element <?php echo $test['class'] ?>">
158 <?php
159 if($test['a'] === true) {
160 // parameters passed to the test are used to keep track of the state and should be returned back to
161 // this page when it is called again.
162 // These parameters are:
163 // id= keeps track of the user and of the sorting of the tests in this page
164 // next= keeps track of the first test not yet undertaken
165
166 echo '<a href="'.$test['url'].'&returnUrl='.$test['returnUrl'].'">'.($n+1).' - '.$test['string'].'</a>';
167 } else {
168 echo ($n+1).' - '.$test['string'];
169 }
170 ?>
171 </li>
172 <?php endforeach; ?>
173 </ul>
174
175 <div id="bottomBox">
176 <?php if($bottomBox!=='') echo $bottomBox ?>
177 </div>
178 </body>
179 </html>