made a copy
[atutor.git] / enroll.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay & Joel Kronenberg        */
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 define('AT_INCLUDE_PATH', 'include/');
15 require (AT_INCLUDE_PATH.'vitals.inc.php');
16
17 $course = intval($_GET['course']);
18 if ($course == 0) {
19         $course = intval($_POST['form_course_id']);
20 }
21
22 if ($course == 0) {
23         exit;
24 }
25
26 $sql    = "SELECT access, member_id FROM ".TABLE_PREFIX."courses WHERE course_id=$course";
27 $result = mysql_query($sql, $db);
28 $course_info = mysql_fetch_array($result);
29
30 if ($_POST['submit']) {
31         $_SESSION['enroll'] = AT_ENROLL_YES;
32         $_POST['form_course_id'] = intval($_POST['form_course_id']);
33
34         if ($course_info[0] == 'private') {
35                 $sql    = "INSERT INTO ".TABLE_PREFIX."course_enrollment VALUES ($_SESSION[member_id], $_POST[form_course_id], 'n', 0, '"._AT('student')."', 0)";
36                 $result = mysql_query($sql, $db);
37
38                 // send the email thing. if needed
39
40                 $sql    = "SELECT notify, member_id FROM ".TABLE_PREFIX."courses WHERE course_id=$_POST[form_course_id] AND notify=1";
41                 $result = mysql_query($sql, $db);
42                 if ($row = mysql_fetch_array($result)) {
43                         // notify is enabled. get the email
44                         $sql    = "SELECT email, first_name, last_name FROM ".TABLE_PREFIX."members WHERE member_id=$row[member_id]";
45                         $result = mysql_query($sql, $db);
46                         $row    = mysql_fetch_assoc($result);
47
48                         $to_email = $row['email'];
49
50                         $tmp_message  = $row['first_name'].' '.$row['last_name'].",\n\n";
51                         $tmp_message .= _AT('enrol_msg', $system_courses[$_POST['form_course_id']]['title']);
52                         $tmp_message .= _AT('enrol_login');
53                         if ($to_email != '') {
54
55                                 require(AT_INCLUDE_PATH . 'classes/phpmailer/atutormailer.class.php');
56
57                                 $mail = new ATutorMailer;
58
59                                 $mail->From     = $_config['contact_email'];
60                                 $mail->AddAddress($to_email);
61                                 $mail->Subject = _AT('course_enrolment');
62                                 $mail->Body    = $tmp_message;
63
64                                 if(!$mail->Send()) {
65                                         //echo 'There was an error sending the message';
66                                    $msg->printErrors('SENDING_ERROR');
67                                    exit;
68                                 }
69                                 unset($mail);
70                         }
71                 }
72         } else {
73                 // public or protected
74                 $sql    = "INSERT INTO ".TABLE_PREFIX."course_enrollment VALUES ($_SESSION[member_id], $_POST[form_course_id], 'y', 0, '"._AT('student')."', 0)";
75                 $result = mysql_query($sql, $db);
76         }
77 }
78
79 if ($_SESSION['valid_user']) {
80
81         $sql    = "SELECT * FROM ".TABLE_PREFIX."course_enrollment WHERE member_id=$_SESSION[member_id] AND course_id=$course";
82         $result = mysql_query($sql, $db);
83         $row    = mysql_fetch_array($result);
84
85         if (($course_info[0] == 'public') || ($course_info[0] == 'protected')) {
86                 if ($row != '') {
87                 
88                         $feedback = array('NOW_ENROLLED', $system_courses[$course]['title']);
89                         $msg->addFeedback($feedback);
90                         header("Location:index.php");
91                 } else if ($course_info[1] != $_SESSION['member_id']) {
92
93                         require(AT_INCLUDE_PATH.'header.inc.php'); ?>
94
95                         <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
96                         <input type="hidden" name="form_course_id" value="<?php echo $course; ?>">
97                         <div class="input-form">
98                                 <div class="row">
99                                         <p><?php  echo _AT('use_enrol_button'); ?></p>
100                                 </div>
101
102                                 <div class="row buttons">
103                                         <input type="submit" name="submit" value="<?php echo _AT('enroll_me'); ?>" />
104                                 </div>
105                         </div>
106                         </form>
107 <?php
108                 } else {
109                         // we own this course!
110                         $msg->printErrors('ALREADY_OWNED');
111                 }
112         } else { // private
113
114                 require(AT_INCLUDE_PATH.'header.inc.php'); 
115
116                 if ((!$_POST['submit']) && ($row == '')) {
117
118                         $msg->printInfos('PRIVATE_ENROL'); ?>
119
120                         <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
121                         <input type="hidden" name="form_course_id" value="<?php echo $course; ?>">
122                         <input type="submit" name="submit" class="button" value="<?php echo _AT('request_enrollment'); ?>">
123                         </form>
124 <?php
125
126                 } else if ($_POST['submit']) {
127                         $msg->printFeedbacks('APPROVAL_PENDING');
128                 } else if ($course_info[1] != $_SESSION['member_id'] ){
129                          // request has already been made
130                          $msg->printErrors('ALREADY_ENROLED');
131                 } else {
132                         $msg->printErrors('ALREADY_OWNED');
133                 }
134         }
135
136 } else {
137         require(AT_INCLUDE_PATH.'header.inc.php'); 
138         $msg->printErrors('LOGIN_ENROL');       
139         ?>
140         <div class="input-form">
141                 <div class="row">
142                         <h3><?php echo _AT('login'); ?></h3>
143                         <p><a href="login.php?course=<?php echo $_SESSION[course_id]; ?>"><?php echo _AT('login_into_atutor'); ?></a></p>
144                 </div>
145         </div>
146
147         <form action="registration.php" method="get">
148         <div class="input-form">
149                 <div class="row">
150                         <h3><?php echo _AT('new_user');?></h3>
151                         <p><?php echo _AT('registration_text'); ?></p>
152                 </div>
153                 <div class="row buttons">
154                         <input type="submit" name="register" value="<?php echo _AT('register'); ?>" class="button" />
155                 </div>
156         </div>
157         </form>
158
159 <?php
160 }
161
162 require(AT_INCLUDE_PATH.'footer.inc.php');
163 ?>