| 121 | | |
| 122 | | |
| 123 | | == Create jobs == |
| | 122 | == Displaying jobs == |
| | 123 | |
| | 124 | Next we'll need a script that displays a job and handles user input. |
| | 125 | {{{ |
| | 126 | function show_job($bj, $bji) { |
| | 127 | $info = json_decode($bj->info); |
| | 128 | $img_url = $info.url; |
| | 129 | echo " |
| | 130 | <form method=get action=bossa_example.php> |
| | 131 | Click on the center of the ellipse. |
| | 132 | If you don't see one, click here: |
| | 133 | <br><br><input type=submit name=submit value=None> |
| | 134 | <input type=hidden name=bji value=$bji->id> |
| | 135 | <input type=hidden name=completion value=1> |
| | 136 | <input type=image name=pic src=$img_url> |
| | 137 | </form> |
| | 138 | "; |
| | 139 | } |
| | 140 | |
| | 141 | function handle_job_completion($bj, $bji) { |
| | 142 | $response = null; |
| | 143 | if (get_str('submit', true)) { |
| | 144 | $response->have_ellipse = 0; |
| | 145 | } else { |
| | 146 | $response->have_ellipse = 1; |
| | 147 | $response->cx = get_int('pic.x'); |
| | 148 | $response->cy = get_int('pic.y'); |
| | 149 | } |
| | 150 | $bji->info = json_encode($response); |
| | 151 | $bji->completed($bj); |
| | 152 | Bossa::show_next_job($bj); // show another job immediately |
| | 153 | } |
| | 154 | |
| | 155 | Bossa::script_init($user, $bj, $bji); |
| | 156 | |
| | 157 | if (isset($_GET['completion'])) { |
| | 158 | handle_job_completion($bj, $bji); |
| | 159 | } else { |
| | 160 | show_job($bj, $bji); |
| | 161 | } |
| | 162 | }}} |
| 127 | | == View results == |
| | 166 | == Handling completed results == |
| | 167 | |
| | 168 | Finally, we need to specify how results are handled. |
| | 169 | This is specified in "html/inc/bossa_example.inc". |
| | 170 | |
| | 171 | This defines two functions. |
| | 172 | The first defines a comparison of two results: |
| | 173 | {{{ |
| | 174 | function compare($r1, $r2) { |
| | 175 | if ($r1->have_ellipse) { |
| | 176 | if ($r2->have_ellipse) { |
| | 177 | $dx = ($r1->cx - $r2->cx); |
| | 178 | $dy = ($r1->cy - $r2->cy); |
| | 179 | $dsq = $dx*$dx + $dy*$dy; |
| | 180 | return ($dsq < 400); |
| | 181 | } else return false; |
| | 182 | } else { |
| | 183 | return !$r2->have_ellipse; |
| | 184 | } |
| | 185 | } |
| | 186 | }}} |
| | 187 | |
| | 188 | The second function specifies what happens when a |
| | 189 | job has been completed, i.e. a consensus set has been found: |
| | 190 | {{{ |
| | 191 | function handle_consensus($bj, $c) { |
| | 192 | $res = $c[0]; |
| | 193 | if ($res->have_ellipse) { |
| | 194 | $res->cx = 0; |
| | 195 | $res->cy = 0; |
| | 196 | foreach ($c as $r) { |
| | 197 | $res->cx += $r->cx; |
| | 198 | $res->cy += $r->cy; |
| | 199 | } |
| | 200 | $res->cx /= count($c); |
| | 201 | $res->cy /= count($c); |
| | 202 | } |
| | 203 | |
| | 204 | $info = json_decode[$bj->info); |
| | 205 | $info->result = $res; |
| | 206 | $i = json_encode($info); |
| | 207 | $bj->update("info='$i'"); |
| | 208 | } |
| | 209 | }}} |
| | 210 | |