made a copy
[atutor.git] / tools / enrollment / export_course_list.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto                  */
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 define('AT_INCLUDE_PATH', '../../include/');
16 require (AT_INCLUDE_PATH.'vitals.inc.php');
17 authenticate(AT_PRIV_ENROLLMENT);
18
19 $completed = 0;
20
21 /*EXPORT LIST OF STUDENTS*/
22 if(isset($_POST['export'])) {
23         //if not list was selected
24         if (!$_POST['enrolled'] && !$_POST['pending_enrollment'] && !$_POST['alumni']) {
25                 $msg->addError('NO_STUDENT_SELECTED');
26         }
27         //retrieve info from database based on selection (make sure that instructor is not exported!)
28         else {
29                 if ($_POST['enrolled'] && $_POST['pending_enrollment'] && $_POST['alumni']) {
30                         $condition = "";
31                 } else if ($_POST['enrolled'] && $_POST['pending_enrollment']) {
32                         $condition = "AND approved <> 'a'";
33                 } else if ($_POST['enrolled'] && $_POST['alumni']) {
34                         $condition = "AND approved <> 'n'";
35                 } else if ($_POST['pending_enrollment'] && $_POST['alumni']) {
36                         $condition = "AND approved <> 'y'";
37                 } else if ($_POST['pending_enrollment']) {
38                         $condition = "AND approved = 'n'";                              
39                 } else if ($_POST['enrolled']) {
40                         $condition = "AND approved = 'y'";
41                 } else if ($_POST['alumni']) {
42                         $condition = "AND approved = 'a'";
43                 } 
44
45                 $sql = "SELECT m.first_name, m.last_name, m.email 
46                                 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";
47
48                 $result =  mysql_query($sql,$db);
49                 while ($row = mysql_fetch_assoc($result)){
50                         $this_row .= quote_csv($row['first_name']).",";
51                         $this_row .= quote_csv($row['last_name']).",";
52                         $this_row .= quote_csv($row['email'])."\n";
53                 }
54
55                 if ($this_row) {
56                         header('Content-Type: text/csv');
57                         header('Content-transfer-encoding: binary');
58                         header('Content-Disposition: attachment; filename="course_list_'.$_SESSION['course_id'].'.csv"');
59                         header('Expires: 0');
60                         header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
61                         header('Pragma: public');
62
63                         echo $this_row;
64                 } else {
65                         // nothing to send. empty file
66                         $msg->addError('ENROLLMENT_NONE_FOUND');
67                         header('Location: export_course_list.php');
68                 }
69                 exit;
70         }
71 }
72 if(isset($_POST['cancel'])) {
73         $msg->addFeedback('CANCELLED');
74         header('Location: index.php');
75         exit;
76 }
77
78 if(isset($_POST['done'])) {
79         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
80         header('Location: index.php');
81         exit;
82 }
83 require(AT_INCLUDE_PATH.'header.inc.php');
84
85
86 ?>
87
88 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="selectform">
89 <div class="input-form">
90         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('export'); ?></legend>
91         <div class="row">
92                 <label><input type="checkbox" name="enrolled" value="1" id="enrolled" /><?php echo _AT('enrolled_list_includes_assistants'); ?></label><br />
93                 <label><input type="checkbox" name="pending_enrollment" value="1" id="pending_enrollment" /><?php echo _AT('pending_enrollment'); ?></label><br />
94                 <label><input type="checkbox" name="alumni" value="1" id="alumni" /><?php echo _AT('alumni'); ?></label>
95         </div>
96
97         <div class="row buttons">
98                 <input type="submit" name="export" value="<?php echo _AT('export'); ?>" /> 
99                 <input type="submit" name="cancel" value="<?php echo _AT('cancel'); ?>" />
100         </div>
101         </fieldset>
102 </div>
103 </form>
104
105 <?php 
106
107 /**
108 * Creates csv file to be exported
109 * @access  private
110 * @param   string $line         The line ot be converted to csv
111 * @return  string                       The line after conversion to csv
112 * @author  Shozub Qureshi
113 */
114 function quote_csv($line) {
115         $line = str_replace('"', '""', $line);
116
117         $line = str_replace("\n", '\n', $line);
118         $line = str_replace("\r", '\r', $line);
119         $line = str_replace("\x00", '\0', $line);
120
121         return '"'.$line.'"';
122 }
123
124 require(AT_INCLUDE_PATH.'footer.inc.php'); 
125 ?>