To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / test_base.php

History | View | Annotate | Download (5.38 KB)

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);
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
  }
90
?>
91
<html>
92
<head>
93
  <style>
94
ul.tests-list li{
95
  margin: 10px 0 5px 0;
96

97
}
98
.done {
99
  list-style-image: url('assets/images/checkbox-checked.png');
100
}
101
.done a {
102
  color: green;
103
}
104
.editable{
105
  list-style-image: url('assets/images/arrow-checkbox-unchecked.png');
106
  list-style-position: inside;
107
  margin-left: -30px;
108
}
109
.disabled{
110
  color: grey;
111
  text-decoration: line-through;
112
  list-style-image: url('assets/images/checkbox-unchecked-disabled.png');
113
}
114
#currentUrl{
115
  font-weight: bold;
116
  padding-left: 20px;
117
  padding-top: 5px;
118
}
119

    
120
  </style>
121
  <script src="jquery-2.1.4.js"></script>
122
  <script>
123
  function confirmEditing(e){
124
    var message = 'Are you sure you want to edit this item? All previous changes will be lost';
125
    return window.confirm(message);
126
  }
127
  var elements;
128
  $(document).ready(function(){
129
    lis = $('ul');
130
    elements = $('li.done:not(.editable)', lis);
131
    for(var n = 0; n < elements.length; n++){
132
      elements[n].onclick = confirmEditing;
133
    }
134
    history.pushState({}, null,  location.pathname+location.search+'<?php echo $toAppendToUrl; ?>');
135

    
136
    // elements = $('li:not(.editable.done)', lis);
137
    // for(var element in elements){
138
    //   element.onclick = confirmEditing;
139
    // }
140
  });
141
  </script>
142
</head>
143
<body>
144

    
145
<ul class = "tests-list">
146
<?php foreach($tests as $n => $test) : ?> 
147
  <li class="test-element <?php echo $test['class'] ?>">
148
<?php 
149
if($test['a'] === true) {
150
  // parameters passed to the test are used to keep track of the state and should be returned back to
151
  // this page when it is called again.
152
  // These parameters are:
153
  // id= keeps track of the user and of the sorting of the tests in this page
154
  // next= keeps track of the first test not yet undertaken
155
  
156
  echo '<a href="'.$test['url'].'&returnUrl='.$test['returnUrl'].'">'.($n+1).' - '.$test['string'].'</a>';
157
} else {
158
  echo ($n+1).' - '.$test['string'];
159
}
160
?>
161
  </li>
162
<?php endforeach; ?>
163
</ul>
164

    
165
  <div id="bottomBox">
166
  <?php if($bottomBox!=='') echo $bottomBox ?>
167
  </div>
168
</body>
169
</html>