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