2956c3ce7980042ff49b5c666cd8a49f434b5147
[atutor.git] / docs / mods / _standard / gradebook / import_export_external_marks.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id$
14
15 $page = "gradebook";
16
17 define('AT_INCLUDE_PATH', '../../../include/');
18 require(AT_INCLUDE_PATH.'vitals.inc.php');
19 authenticate(AT_PRIV_GRADEBOOK);
20
21 // initialize relationship between gradebook_test_id and external test name
22 $tests = array();
23
24 // atutor tests
25 $sql = "SELECT * FROM ".TABLE_PREFIX."gradebook_tests WHERE course_id=".$_SESSION["course_id"]." ORDER BY title";
26
27 $sql_at = "SELECT gradebook_test_id, t.title FROM ".TABLE_PREFIX."gradebook_tests g, ".TABLE_PREFIX."tests t".
28                                 " WHERE g.id=t.test_id " .
29                                 " AND g.type='ATutor Test' ".
30                                 " AND t.course_id=".$_SESSION["course_id"].
31                                 " ORDER BY t.title";
32 $result_at = mysql_query($sql_at, $db) or die(mysql_error());
33
34 // atutor assignments
35 $sql_aa = "SELECT gradebook_test_id, a.title FROM ".TABLE_PREFIX."gradebook_tests g, ".TABLE_PREFIX."assignments a".
36                                 " WHERE g.id=a.assignment_id " .
37                                 " AND g.type='ATutor Assignment' ".
38                                 " AND a.course_id=".$_SESSION["course_id"].
39                                 " ORDER BY a.title";
40 $result_aa = mysql_query($sql_aa, $db) or die(mysql_error());
41
42 // external
43 $sql_e = "SELECT gradebook_test_id, title FROM ".TABLE_PREFIX."gradebook_tests".
44                                 " WHERE type='External' ".
45                                 " AND course_id=".$_SESSION["course_id"].
46                                 " ORDER BY title";
47 $result_e = mysql_query($sql_e, $db) or die(mysql_error());
48
49 // end of initialization
50
51 if (isset($_POST['cancel'])) 
52 {
53         $msg->addFeedback('CANCELLED');
54         header('Location: gradebook_tests.php');
55         exit;
56
57 else if (isset($_POST['export'])) 
58 {
59         // generate students array
60         $sql = "SELECT m.first_name, m.last_name, m.email, e.member_id FROM ".TABLE_PREFIX."members m, ".TABLE_PREFIX."course_enrollment e WHERE m.member_id = e.member_id AND e.course_id=".$_SESSION["course_id"]." AND e.approved='y' AND e.role<>'Instructor' ORDER BY m.first_name,m.last_name,m.email";
61         $result = mysql_query($sql, $db) or die(mysql_error());
62         
63         if (mysql_num_rows($result)==0)
64         {
65                 // nothing to send. empty file
66                 $msg->addError('ENROLLMENT_NONE_FOUND');
67         }
68         else
69         {
70                 $this_row = "First Name, Last Name, Email, Grade\n";
71                 while ($row = mysql_fetch_assoc($result))
72                 {
73                         // retrieve title
74                         $sql_title = "(SELECT g.gradebook_test_id, t.title".
75                                                         " FROM ".TABLE_PREFIX."gradebook_tests g, ".TABLE_PREFIX."tests t".
76                                                         " WHERE g.type='ATutor Test'".
77                                                         " AND g.id = t.test_id".
78                                                         " AND g.gradebook_test_id=".$_POST['gradebook_test_id'].")".
79                                                         " UNION (SELECT g.gradebook_test_id, a.title".
80                                                         " FROM ".TABLE_PREFIX."gradebook_tests g, ".TABLE_PREFIX."assignments a".
81                                                         " WHERE g.type='ATutor Assignment'".
82                                                         " AND g.id = a.assignment_id".
83                                                         " AND g.gradebook_test_id=".$_POST['gradebook_test_id'].")".
84                                                         " UNION (SELECT gradebook_test_id, title ".
85                                                         " FROM ".TABLE_PREFIX."gradebook_tests".
86                                                         " WHERE type='External'".
87                                                         " AND gradebook_test_id=".$_POST['gradebook_test_id'].")";
88                         $result_title   = mysql_query($sql_title, $db) or die(mysql_error());
89                         $row_title = mysql_fetch_assoc($result_title);
90                         
91                         // retrieve grade
92                         $sql_grade = "SELECT grade FROM ".TABLE_PREFIX."gradebook_detail WHERE gradebook_test_id=".$_POST["gradebook_test_id"]." AND member_id=".$row["member_id"];
93                         $result_grade   = mysql_query($sql_grade, $db) or die(mysql_error());
94                         $row_grade = mysql_fetch_assoc($result_grade);
95                         $grade=$row_grade["grade"];
96                         
97                         $this_row .= quote_csv($row['first_name']).",";
98                         $this_row .= quote_csv($row['last_name']).",";
99                         $this_row .= quote_csv($row['email']).",";
100                         $this_row .= quote_csv($grade)."\n";
101                 }
102                 header('Content-Type: text/csv');
103                 header('Content-transfer-encoding: binary');
104                 header('Content-Disposition: attachment; filename="grade_'.$row_title["title"].'.csv"');
105                 header('Expires: 0');
106                 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
107                 header('Pragma: public');
108
109                 echo $this_row;
110                 exit;
111         }
112 }
113
114 require(AT_INCLUDE_PATH.'header.inc.php');
115
116 ?>
117 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
118 <div class="input-form">
119         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('export'); ?></legend>
120         <div class="row">
121                 <p><?php echo _AT('export_marks_info'); ?></p>
122         </div>
123 <?php 
124 if (mysql_num_rows($result_aa) == 0 && mysql_num_rows($result_at) == 0 && mysql_num_rows($result_e) == 0)
125 {
126 ?>
127         <div class="row">
128                 <strong><?php echo _AT('none_found'); ?></strong>
129         </div>
130 <?php 
131 }
132 else
133 {
134 ?>
135         <div class="row">
136                 <label for="select_gid"><?php echo _AT('export_content_package_what'); ?></label><br />
137                 <select name="gradebook_test_id" id="select_gid">
138 <?php
139         if (mysql_num_rows($result_aa) > 0)
140         {
141                 echo '                  <optgroup label="'. _AT('assignments') .'">'."\n\r";
142         
143                 while ($row_aa = mysql_fetch_assoc($result_aa))
144                 {
145                         echo '                  <option value="'.$row_aa[gradebook_test_id].'">'.$row_aa[title].'</option>'."\n\r";
146                 }
147                 echo '                  </optgroup>'."\n\r";
148         }
149
150         if (mysql_num_rows($result_at) > 0)
151         {
152                 echo '                  <optgroup label="'. _AT('tests') .'">'."\n\r";
153         
154                 while ($row_at = mysql_fetch_assoc($result_at))
155                 {
156                         echo '                  <option value="'.$row_at[gradebook_test_id].'">'.$row_at[title].'</option>'."\n\r";
157                 }
158                 echo '                  </optgroup>'."\n\r";
159         }
160
161         if (mysql_num_rows($result_e) > 0)
162         {
163                 echo '                  <optgroup label="'. _AT('external_tests') .'">'."\n\r";
164         
165                 while ($row_e = mysql_fetch_assoc($result_e))
166                 {
167                         echo '                  <option value="'.$row_e[gradebook_test_id].'">'.$row_e[title].'</option>'."\n\r";
168                 }
169                 echo '                  </optgroup>'."\n\r";
170         }
171
172 ?>
173 </select>
174         </div>
175
176         <div class="row buttons">
177                 <input type="submit" name="export" value="<?php echo _AT('export'); ?>" />
178                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
179         </div>
180 <?php 
181 }
182 ?>
183         </fieldset>
184 </div>
185 </form>
186
187 <br /><br />
188
189 <form name="form1" method="post" action="mods/_standard/gradebook/verify_list.php" enctype="multipart/form-data">
190 <div class="input-form">
191         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('import'); ?></legend>
192         <div class="row">
193                 <p><?php echo _AT('import_marks_info'); ?></p>
194         </div>
195
196 <?php 
197 if (mysql_num_rows($result_aa) == 0 && mysql_num_rows($result_e) == 0)
198 {
199 ?>
200         <div class="row">
201                 <strong><?php echo _AT('none_found'); ?></strong>
202         </div>
203 <?php 
204 }
205 else
206 {
207 ?>
208         <div class="row">
209                 <label for="select_gid2"><?php echo _AT('import_content_package_where'); ?></label><br />
210                 <select name="gradebook_test_id" id="select_gid2">
211 <?php
212         mysql_data_seek($result_aa, 0);
213         mysql_data_seek($result_e, 0);
214
215         if (mysql_num_rows($result_aa) > 0)
216         {
217                 echo '                  <optgroup label="'. _AT('assignments') .'">'."\n\r";
218         
219                 while ($row_aa = mysql_fetch_assoc($result_aa))
220                 {
221                         echo '                  <option value="'.$row_aa[gradebook_test_id].'">'.$row_aa[title].'</option>'."\n\r";
222                 }
223                 echo '                  </optgroup>'."\n\r";
224         }
225
226         if (mysql_num_rows($result_e) > 0)
227         {
228                 echo '                  <optgroup label="'. _AT('external_tests') .'">'."\n\r";
229         
230                 while ($row_e = mysql_fetch_assoc($result_e))
231                 {
232                         echo '                  <option value="'.$row_e[gradebook_test_id].'">'.$row_e[title].'</option>'."\n\r";
233                 }
234                 echo '                  </optgroup>'."\n\r";
235         }
236
237 ?>
238                 </select>
239         </div>
240         
241         <div class="row">
242                 <label for="to_file"><?php echo _AT('upload'); ?></label><br />
243                 <input type="file" name="file" id="to_file" />
244         </div>
245
246         <div class="row buttons">
247                 <input type="submit" name="import" value="<?php echo _AT('import'); ?>" />
248                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
249         </div>
250 <?php 
251 }
252 ?>
253         </fieldset>
254 </div>
255 </form>
256
257 <?php 
258
259 /**
260 * Creates csv file to be exported
261 * @access  private
262 * @param   string $line         The line ot be converted to csv
263 * @return  string                       The line after conversion to csv
264 * @author  Shozub Qureshi
265 */
266 function quote_csv($line) {
267         $line = str_replace('"', '""', $line);
268
269         $line = str_replace("\n", '\n', $line);
270         $line = str_replace("\r", '\r', $line);
271         $line = str_replace("\x00", '\0', $line);
272
273         return '"'.$line.'"';
274 }
275
276 require (AT_INCLUDE_PATH.'footer.inc.php'); 
277 ?>