summaryrefslogtreecommitdiff
path: root/tbc_external_review_eligibility_test.inc
blob: ef816a0d82521268d6c002e6ae5999af5fbd4ded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<?php

function tbc_external_review_eligibility_test_form($form, $form_state, $question_id=0) {
        $form = array();
        global $user;
        
        $query = "
            SELECT * FROM external_review_eligibility_answers
            WHERE uid = :uid AND question_id = :question_id AND reset = 0 
        ";
        $result = db_query($query,array(':uid'=>$user->uid, ':question_id'=> $question_id));
        $row = $result->fetchObject();
        
        $form["wrapper"] = array(
            "#type" => "fieldset",
            "#title" => t("Eligibility Test"),
            "#prefix" => "<div id='eligibility-form-wrapper'>",
            "#suffix" => "</div>",
        );
        $form["wrapper"]["ncf"] = array(
            "#type" => "radios",
            "#title" => t("Naming convention followed? (Checklist point 1, 2 and 4)"),
            "#options" => array(
                t("Yes"),
                t("No"),
            ),
            "#required" => TRUE,
            "#default_value" => $row->ncf
        );
        $form["wrapper"]["eit"] = array(
            "#type" => "radios",
            "#title" => t("Error in textbook mentioned as comment?"),
            "#options" => array(
                t("Yes"),
                t("No"),
                t("Not required and answer matches with the textbook"),
            ),
            "#required" => TRUE,
            "#default_value" => $row->eit
        );
        $form["wrapper"]["sua"] = array(
            "#type" => "radios",
            "#title" => t("Symbols used are appropiate?"),
            "#options" => array(
                t("Yes"),
                t("No"),
            ),
            "#required" => TRUE,
            "#default_value" => $row->sua
        );
        $form["wrapper"]["sfu"] = array(
            "#type" => "radios",
            "#title" => t("Scilab functions used?"),
            "#options" => array(
                t("Yes"),
                t("No"),
            ),
            "#required" => TRUE,
            "#default_value" => $row->sfu
        );
        $form["wrapper"]["aci"] = array(
            "#type" => "radios",
            "#title" => t("Appropriate comments included as mentioned in checklist point 7?"),
            "#options" => array(
                t("Yes"),
                t("No"),
            ),
            "#required" => TRUE,
            "#default_value" => $row->aci
        );
        $form["wrapper"]["auu"] = array(
            "#type" => "radios",
            "#title" => t("Appropriate units used as given in the textbook?"),
            "#options" => array(
                t("Yes"),
                t("No"),
            ),
            "#required" => TRUE,
            "#default_value" => $row->auu
        );
        $form["wrapper"]["teo"] = array(
            "#type" => "radios",
            "#title" => t("Typographical errors in output and/or comments?"),
            "#options" => array(
                t("Yes"),
                t("No"),
            ),
            "#required" => TRUE,
            "#default_value" => $row->teo
        );
        $form["wrapper"]["mit"] = array(
            "#type" => "radios",
            "#title" => t("The code can be approved if the same example is solved using Matlab in the textbook."),
            "#options" => array(
                t("Yes"),
                t("No"),
            ),
            "#required" => TRUE,
            "#default_value" => $row->mit
        );
        $form["wrapper"]["agl"] = array(
            "#type" => "radios",
            "#title" => t("Axes of graph need not be labeled"),
            "#options" => array(
                t("Yes"),
                t("No"),
            ),
            "#required" => TRUE,
            "#default_value" => $row->agl
        );
        $form["wrapper"]["question_id"] = array(
            "#type" => "hidden",
            "#value" => $question_id
        );
        $form["wrapper"]["submit"] = array(
            "#type" => "submit",
            "#value" => "Submit and Continue."
        );
        return $form;
    }

    function tbc_external_review_eligibility_test_form_submit($form, &$form_state) {
        global $user;
        
        $v = $form_state["values"];
        
        /* checking the timer and status of current user */
        $query = "
            SELECT * FROM external_review_eligibility_timings
            WHERE uid = :uid
        ";
        $result = db_query($query,array(':uid'=> $user->uid));
        $num_rows = $result->rowCount(); //either 1 or 0
        if($num_rows){
            /* now checking whether the test is still open */
            $row =$result->fetchObject();
            $start_date = new DateTime($row->start_date);
            $current_date = new DateTime();
            $since_start = $start_date->diff($current_date);
            $minutes_remaining = 24 - $since_start->i;
            $seconds_remaining = 60 - $since_start->s;
            if($minutes_remaining >= 0) {
            } else {
                drupal_goto("tbc_external_review/eligibility_test/end");
            }
        }
        
        $query = "
            SELECT id FROM external_review_eligibility_answers
            WHERE 
            uid =  :uid AND question_id = :question_id
        ";
        $result = db_query($query,array(':uid'=> $user->uid,':question_id'=> $v['question_id'] ));
        $num_rows = $result->rowCount();
        if($num_rows) {
           /* $query = "
                UPDATE external_review_eligibility_answers
                SET
                ncf = {$v['ncf']}, eit = {$v['eit']}, sua = {$v['sua']},
                sfu = {$v['sfu']}, aci = {$v['aci']}, auu = {$v['auu']},
                teo = {$v['teo']}, mit = {$v['mit']}, agl = {$v['agl']}
                WHERE
                uid = {$user->uid} AND question_id = {$v['question_id']}
                ";
            $result = db_query($query);*/
            $query = db_update('external_review_eligibility_answers');
            $query->fields(array(
              'ncf' => $v['ncf'],
              'eit' => $v['eit'],
              'sua' => $v['sua'],
              'sfu' => $v['sfu'],
              'aci' => $v['aci'], 
              'auu' => $v['auu'],
              'teo' => $v['teo'],
              'mit' => $v['mit'],
              'agl' => $v['agl'],
            ));
            $query->condition('uid', $user->uid);          
            $query->condition('question_id', $v['question_id']);
            $result = $query->execute();
            //var_dump('ok');
           // die;            
        } else {
         
            /*$query = "
                INSERT INTO external_review_eligibility_answers
                (
                    uid, question_id, 
                    ncf, eit, sua,
                    sfu, aci, auu,
                    teo, mit, agl
                )
                VALUES
                (
                    {$user->uid}, {$v['question_id']},
                    {$v['ncf']}, {$v['eit']}, {$v['sua']},
                    {$v['sfu']}, {$v['aci']}, {$v['auu']},
                    {$v['teo']}, {$v['mit']}, {$v['agl']}
                )
            ";
            $result = db_query($query);
        }*/
        
        $query = "
                INSERT INTO external_review_eligibility_answers
                (
                    uid, question_id, 
                    ncf, eit, sua,
                    sfu, aci, auu,
                    teo, mit, agl
                )
                VALUES
                (  :uid, :question_id, 
                   :ncf, :eit, :sua,
                   :sfu, :aci, :auu,
                   :teo, :mit, :agl
                   
                )
            ";
           
            $args = array(
                          ':uid' => $user->uid ,
                          ':question_id' => $v['question_id'],
                          ':ncf' => $v['ncf'],
                          ':eit' => $v['eit'],
                          ':sua' => $v['sua'],
                          ':sfu' => $v['sfu'],
                          ':aci' => $v['aci'], 
                          ':auu' => $v['auu'],
                          ':teo' => $v['teo'],
                          ':mit' => $v['mit'],
                          ':agl' => $v['agl'],
                            
                    );
                    $result = db_query($query, $args);
        }
        
        $question_id = $v["question_id"];
        if($question_id == 5) {
            // end the test
            drupal_goto("tbc_external_review/eligibility_test/end");
        } else {
            drupal_goto("tbc_external_review/eligibility_test/question/".($question_id+1));
        }
    }

    function tbc_external_review_eligibility_test_all($action="", $question_id=0){
        global $user;
        $page_content = "";
        $minutes_remaining = 25;
        $seconds_remaining = 0;
        
        /* checking if a valid question */
        if ($question_id > 5 || $question_id < 0) {
            drupal_set_message("Wrong question number.", "error");
            drupal_goto("tbc_external_review/eligibility_test/question/1");
        }
        
        if($action == "question" && $question_id) {
            /* checking the timer and status of current user */
            $query = "
                SELECT * FROM external_review_eligibility_timings
                WHERE uid = :uid
            ";
            $result = db_query($query, array(':uid'=> $user->uid ));
            $num_rows = $result->rowCount(); //either 1 or 0
            if($num_rows){
                /* now checking whether the test is still open */
                $row = $result->fetchObject();
                $start_date = new DateTime($row->start_date);
                $current_date = new DateTime();
                $since_start = $start_date->diff($current_date);
                $minutes_remaining = 24 - $since_start->i;
                $seconds_remaining = 60 - $since_start->s;
                if($minutes_remaining >= 0) {
                } else {
                    drupal_goto("tbc_external_review/eligibility_test/end");
                }
            } else {
                /* inserting a new entry into timings table */
                $current_date = new DateTime();
                $current_date = $current_date->format('Y-m-d H:i:s');
                $query = "
                    INSERT INTO external_review_eligibility_timings
                    (uid, status, start_date)
                    VALUES
                    (:uid, 1, :current_date)
                ";
                $result = db_query($query, array(':uid'=> $user->uid, ':current_date' => $current_date ));
            }
            
            /* getting the questions */
            $query = "
                SELECT * FROM external_review_eligibility_questions
                WHERE id = :question_id
            ";
            $result = db_query($query, array(':question_id'=>$question_id));
            $row = $result->fetchObject();
            $page_content .= "<div id='timer'>";
                $page_content .= "<span id='minutes'></span>m: ";
                $page_content .= "<span id='seconds'></span>s";
            $page_content .= "</div>";
            $page_content .= "<h4>";
                if ($question_id != 1) {
                    $page_content .= l("<Prev - ", "tbc_external_review/eligibility_test/question/" . ($question_id-1));
                }
                $page_content .= "Question {$question_id} of 5";
                if ($question_id != 5) {
                    $page_content .= l(" - Next>", "tbc_external_review/eligibility_test/question/" . ($question_id+1));
                }
            $page_content .= "</h4><br>";
            $page_content .= "<div id='example-name'>Example file name: <strong>{$row->example_name}</strong></div><br>";
            $page_content .= "<div class='well'><pre>{$row->code}</pre></div>";
            
            $tbc_external_review_eligibility_test_form = drupal_get_form("tbc_external_review_eligibility_test_form", $question_id);
            $page_content .= drupal_render($tbc_external_review_eligibility_test_form);
            
            /* hidden fields with minutes_remaining and seconds_remaining */
            $page_content .= "<input id='minutes_remaining' value='{$minutes_remaining}' type='hidden'>";
            $page_content .= "<input id='seconds_remaining' value='{$seconds_remaining}' type='hidden'>";
        } else if($action == "end") {
            $query = "
                UPDATE external_review_eligibility_timings
                SET status = 2
                WHERE uid = :uid
            ";
            $result = db_query($query, array(':uid'=> $user->uid));
            drupal_set_message("Eligibility Test completed successfully. We will get back to you soon.", "status");
        }
         else {
            /* checking if the user has filled the profile form */
            $query = "
                SELECT uid FROM external_review_profile
                WHERE uid = :uid
            ";
            $result = db_query($query, array(':uid'=> $user->uid));
            $num_rows = $result->rowCount();
            if(!$num_rows) {
                drupal_set_message("Please fill  the profile form before you can attend the test.", "error");
                drupal_goto("tbc_external_review/profile");
            }
            
            /* showing the test details before starting the test */
            $page_content .= "<p>You are about to give a test based on which you will be selected as a reviewer for the Scilab Textbook Companions.</p>";
            $page_content .= "<ul>";
                $page_content .= "<li>The test consists of 5 code review questions that are mandatory.</li>";
                $page_content .= "<li>Test duration is 25 minutes, after which the test will automatically close.</li>";
            $page_content .= "</ul>";
            $page_content .= "<u><b>" . l("Click here", "tbc_external_review/eligibility_test/question/1") . "</b></u>";
            $page_content .= " to start the test.";
            // $page_content .= "<strong>The test has been suspended for today due to technical reasons.</strong><br>";
            // $page_content .= "<strong>Please visit this page tomorrow.</strong>";
        }
        return $page_content;
    }

    function tbc_external_review_manage_tests_all($action="", $user_id=0){
        /* 
            end the tests that did not close properly 
            i.e set status=2 before loading this page in the future\
            if the time has expired.
        */
        $page_content = "";
        if($action == "view" && $user_id) {
            /* fetching the correct answers*/
            $query = "
                SELECT * FROM external_review_eligibility_timings
                WHERE uid = :uid
            ";
            $result = db_query($query, array(':uid'=> $user_id));
            if($result->rowCount()) {
                /* fetching correct answers */
                $query = "
                    SELECT id, ncf, eit, sua, sfu, aci, auu, teo, mit, agl
                    FROM external_review_eligibility_questions
                ";
                $result = db_query($query);
                $correct_answers = array();
                while($row = $result->fetchObject()) {
                    $correct_answers[$row->id] = $row;
                }
                
                /* fetching user answers */
                $query = "
                    SELECT * FROM external_review_eligibility_answers
                    WHERE uid = :uid
                ";
                $result = db_query($query, array(':uid'=> $user_id));
                $user_answers = array();
                while($row = $result->fetchObject()) {
                    $user_answers[$row->question_id] = $row;
                }
                
                /* comparing the results */
                $scores = array();
                foreach($user_answers as $user_answer) {
                    $scores [$user_answer->question_id] = 0;
                    ($user_answer->ncf == $correct_answers[$user_answer->question_id]->ncf)?$scores[$user_answer->question_id]++:Null;
                    ($user_answer->eit == $correct_answers[$user_answer->question_id]->eit)?$scores[$user_answer->question_id]++:Null;
                    ($user_answer->sua == $correct_answers[$user_answer->question_id]->sua)?$scores[$user_answer->question_id]++:Null;
                    ($user_answer->sfu == $correct_answers[$user_answer->question_id]->sfu)?$scores[$user_answer->question_id]++:Null;
                    ($user_answer->aci == $correct_answers[$user_answer->question_id]->aci)?$scores[$user_answer->question_id]++:Null;
                    ($user_answer->auu == $correct_answers[$user_answer->question_id]->auu)?$scores[$user_answer->question_id]++:Null;
                    ($user_answer->teo == $correct_answers[$user_answer->question_id]->teo)?$scores[$user_answer->question_id]++:Null;
                    ($user_answer->mit == $correct_answers[$user_answer->question_id]->mit)?$scores[$user_answer->question_id]++:Null;
                    ($user_answer->agl == $correct_answers[$user_answer->question_id]->agl)?$scores[$user_answer->question_id]++:Null;
                }
                $page_content .= "<hr><h1><u>User Score</u></h1>";
                $total_score = 0;
                foreach($scores as $key => $value) {
                    $page_content .= "Question {$key} score = <strong>{$value}</strong> out of 9<br>";
                    $total_score += $value;
                }
                $page_content .= "<h1>Total Score = {$total_score} out of 45";
                /* fetching user data for display */
                $query = "
                    SELECT * FROM external_review_profile erd
                    LEFT JOIN users usr ON usr.uid = erd.uid
                    WHERE erd.uid = :uid
                ";
                $result = db_query($query, array(':uid' => $user_id));
                $row = $result->fetchAssoc();
                $fields = array(
                    "first_name", "last_name", "designation", "university",
                    "phone", "email", "alt_email", "address",
                    "description"
                );
                $page_content .= "<hr><h1><u>User Profile</u></h1>";
                $page_content .= "<table border=1 cellspacing=1 cellpadding=1>";
                foreach($fields as $field) {
                    $page_content .= "<tr>";
                        $page_content .= "<td>{$field}</td>";
                        $page_content .= "<td>{$row[$field]}</td>";
                    $page_content .= "</tr>";
                }
                $page_content .= "</table>";
            }
        }elseif($action == "reset" && $user_id){       
        $query = db_delete('external_review_eligibility_timings');           
            $query->condition('uid', $user_id);            
            $result = $query->execute();
            
        $query = "
        UPDATE external_review_eligibility_answers
                SET reset = 1
                WHERE uid = :uid
       ";
       $result = db_query($query, array(':uid'=> $user_id));
      
            drupal_set_message("Test Reset");
            drupal_goto("tbc_external_review/manage_tests");
        
       
        }
        
         else {
            $query = "
                SELECT * FROM external_review_eligibility_timings tim
                LEFT JOIN users usr ON usr.uid = tim.uid
                WHERE tim.status = 2
                ";
            $result = db_query($query);
            
            $headers = array(
                "User", "Date Attended",
                "Action"
            );
            $rows = array();
            while($row = $result->fetchObject()) {
                $item = array(
                    $row->name,
                    $row->start_date,
                    l("View", "tbc_external_review/manage_tests/view/{$row->uid}") ." | ". l("Reset", "tbc_external_review/manage_tests/reset/{$row->uid}")
                );
                array_push($rows, $item);
            }
            //$page_content .= theme("table", $headers, $rows);
            $page_content .= theme('table', array('header' => $headers, 'rows' => $rows ));
        }
        return $page_content;
    }