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