changed git call from https to git readonly
[atutor.git] / mods / certify / certify_tests.php
1 <?php
2
3 define('AT_INCLUDE_PATH', '../../include/');
4 require (AT_INCLUDE_PATH.'vitals.inc.php');
5 authenticate(AT_PRIV_CERTIFY);
6
7 $certify_id = '';
8 if (isset($_POST['certify_id'])) {
9     $certify_id = $addslashes($_POST['certify_id']);
10 } else if (isset($_GET['certify_id'])) {
11     $certify_id = $addslashes($_GET['certify_id']);
12 }
13
14 if (isset($_POST['edit'])) { // Commit edit
15
16         // STORE EDITS
17
18         $certify_selected = $_POST['selected'];
19
20         $update_remove = array();
21         $update_add = array();
22
23         $certify_tests = fetchTestList($certify_id);
24         foreach ($certify_tests as $test_id => $test) {
25                 if ($test['selected'] and !isset($certify_selected[$test_id])) {
26                         $update_remove[] = $test_id;
27                 } else if (!$test['selected'] and isset($certify_selected[$test_id])) {
28                         $update_add[] = $test_id;
29                 }
30
31         }
32
33         if (count($update_add) > 0) {
34                 $sqlrows = array();
35                 foreach ($update_add as $testid) {
36                         $sqlrows[] = '('.$certify_id.",".$testid.')';
37                 }
38                 $sql = "INSERT INTO ".TABLE_PREFIX."certify_tests
39                                 (certify_id, 
40                                  test_id) 
41                                         VALUES ".implode(',',$sqlrows);
42                                                         
43                 $result = mysql_query($sql, $db) or die(mysql_error());
44                 write_to_log(AT_ADMIN_LOG_INSERT, 'certify', mysql_affected_rows($db), $sql);
45         
46         }
47
48         if (count($update_remove) > 0) {
49                 $sql = "DELETE FROM ".TABLE_PREFIX."certify_tests
50                                 WHERE certify_id = $certify_id
51                                 AND test_id IN (".implode(",",$update_remove).")";
52                                                         
53                 $result = mysql_query($sql, $db) or die(mysql_error());
54                 write_to_log(AT_ADMIN_LOG_DELETE, 'certify', mysql_affected_rows($db), $sql);
55         
56         }
57
58         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
59
60
61 } else if (isset($_POST['cancel']) || strlen($certify_id) == 0) {
62         
63         // CANCEL
64         
65         // FIXME: Is really a "return" function - need a new text for the button?
66
67         $msg->addFeedback('CANCELLED');
68         header('Location: index_instructor.php');
69         exit;
70
71 }
72
73
74 // FETCH INFO FOR VIEW  
75
76 $certify_tests = fetchTestList($certify_id);
77
78 require(AT_INCLUDE_PATH.'header.inc.php'); 
79 $msg->printAll();
80
81 ?>
82
83 <fieldset>
84 <legend>
85 For instructor to edit certificate
86 </legend>
87 <form name="certifydetails" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
88 <input type="hidden" name="certify_id" value="<?php echo $certify_id; ?>">
89
90 <table class="data" summary="" rules="cols">
91
92 <thead>
93 <tr>
94         <th scope="col"></th>
95         <th scope="col"><?php echo _AT('certify_title'); ?></a></th>
96 </tr>
97 </thead>
98
99 <tfoot>
100 <tr>
101         <td colspan="2">
102                 <input type="submit" name="edit" value="<?php echo _AT('save'); ?>" /> 
103                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
104         </td>
105 </tr>
106 </tfoot>
107
108 <tbody>
109         <?php foreach ($certify_tests as $id => $test) { ?>
110                 <tr>
111                         <td><input type="checkbox" <?php if ($test['selected']) { echo 'checked="checked" '; } ?> name="selected[<?= $id ?>]" value="1"></td>
112                         
113                         <td><label for=""><?php echo $test['title']; ?></label></td>
114                         
115                 </tr>
116         
117         <?php } ?>
118         <?php if (count($certify_tests)==0) { ?>
119         
120                 <tr>
121                         <td colspan="2"><?php echo _AT('none_found'); ?></td>
122                 </tr>
123         
124         <?php } ?>
125
126 </tbody>
127
128 </table>
129
130 </form>
131 </fieldset>
132
133
134 <?php
135
136 require (AT_INCLUDE_PATH.'footer.inc.php');
137
138 function fetchTestList($certify_id) {
139         global $db, $_SESSION;
140
141         // Fetch all tests for course
142         // FIXME: Need to filter out tests that doesn't have a pass criteria
143         $sql =  "SELECT test_id, title FROM ".TABLE_PREFIX."tests WHERE course_id=".$_SESSION['course_id'];
144         $result = mysql_query($sql, $db) or die(mysql_error() . $sql);
145         
146         $certify_tests = array();
147         
148         while( $row = mysql_fetch_assoc($result) ) {
149                 $this_test = array();
150                 $this_test['title'] = $row['title'];
151                 $this_test['selected'] = false;
152                 $certify_tests[$row['test_id']] = $this_test;
153         }
154         
155         // Fetch associated tests
156         $sql =  "SELECT test_id FROM ".TABLE_PREFIX."certify_tests ";
157         $sql .= "WHERE ".TABLE_PREFIX."certify_tests.certify_id=".$certify_id;
158         $result = mysql_query($sql, $db) or die(mysql_error() . $sql);
159         
160         while( $row = mysql_fetch_assoc($result) ) {
161                 $certify_tests[$row['test_id']]['selected'] = true;
162         }
163         return $certify_tests;
164
165 }
166
167 ?>