move code up one directory
[atutor.git] / mods / _core / enrolment / export_course_list.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12 // $Id$
13
14 define('AT_INCLUDE_PATH', '../../../include/');
15 require (AT_INCLUDE_PATH.'vitals.inc.php');
16 authenticate(AT_PRIV_ENROLLMENT);
17
18 $completed = 0;
19
20 /*EXPORT LIST OF STUDENTS*/
21 if(isset($_POST['export'])) {
22         //if not list was selected
23         if (!$_POST['enrolled'] && !$_POST['pending_enrollment'] && !$_POST['alumni']) {
24                 $msg->addError('NO_STUDENT_SELECTED');
25         }
26         //retrieve info from database based on selection (make sure that instructor is not exported!)
27         else {
28                 if ($_POST['enrolled'] && $_POST['pending_enrollment'] && $_POST['alumni']) {
29                         $condition = "";
30                 } else if ($_POST['enrolled'] && $_POST['pending_enrollment']) {
31                         $condition = "AND approved <> 'a'";
32                 } else if ($_POST['enrolled'] && $_POST['alumni']) {
33                         $condition = "AND approved <> 'n'";
34                 } else if ($_POST['pending_enrollment'] && $_POST['alumni']) {
35                         $condition = "AND approved <> 'y'";
36                 } else if ($_POST['pending_enrollment']) {
37                         $condition = "AND approved = 'n'";                              
38                 } else if ($_POST['enrolled']) {
39                         $condition = "AND approved = 'y'";
40                 } else if ($_POST['alumni']) {
41                         $condition = "AND approved = 'a'";
42                 } 
43
44                 $sql = "SELECT m.first_name, m.last_name, m.email 
45                                 FROM ".TABLE_PREFIX."course_enrollment cm JOIN ".TABLE_PREFIX."members m ON cm.member_id = m.member_id JOIN ".TABLE_PREFIX."courses c ON (cm.course_id = c.course_id AND cm.member_id <> c.member_id)   WHERE cm.course_id = $_SESSION[course_id] " . $condition . "ORDER BY m.last_name";
46
47                 $result =  mysql_query($sql,$db);
48                 while ($row = mysql_fetch_assoc($result)){
49                         $this_row .= quote_csv($row['first_name']).",";
50                         $this_row .= quote_csv($row['last_name']).",";
51                         $this_row .= quote_csv($row['email'])."\n";
52                 }
53
54                 if ($this_row) {
55                         header('Content-Type: text/csv');
56                         header('Content-transfer-encoding: binary');
57                         header('Content-Disposition: attachment; filename="course_list_'.$_SESSION['course_id'].'.csv"');
58                         header('Expires: 0');
59                         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
60                         header('Pragma: public');
61
62                         echo $this_row;
63                 } else {
64                         // nothing to send. empty file
65                         $msg->addError('ENROLLMENT_NONE_FOUND');
66                         header('Location: export_course_list.php');
67                 }
68                 exit;
69         }
70 }
71 if(isset($_POST['cancel'])) {
72         $msg->addFeedback('CANCELLED');
73         header('Location: index.php');
74         exit;
75 }
76
77 if(isset($_POST['done'])) {
78         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
79         header('Location: index.php');
80         exit;
81 }
82 require(AT_INCLUDE_PATH.'header.inc.php');
83
84
85
86 /**
87 * Creates csv file to be exported
88 * @access  private
89 * @param   string $line         The line ot be converted to csv
90 * @return  string                       The line after conversion to csv
91 * @author  Shozub Qureshi
92 */
93 function quote_csv($line) {
94         $line = str_replace('"', '""', $line);
95
96         $line = str_replace("\n", '\n', $line);
97         $line = str_replace("\r", '\r', $line);
98         $line = str_replace("\x00", '\0', $line);
99
100         return '"'.$line.'"';
101 }
102 $savant->display('instructor/enrolment/export_course_list.tmpl.php');
103 require(AT_INCLUDE_PATH.'footer.inc.php'); 
104 ?>