remove old readme
[atutor.git] / users / private_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 if (!$_SESSION['valid_user']) {
20         require(AT_INCLUDE_PATH.'header.inc.php');
21         $msg->printErrors('LOGIN_ENROL');
22         require(AT_INCLUDE_PATH.'footer.inc.php');
23         exit;
24 }
25
26 $course = intval($_REQUEST['course']);
27 if ($course == 0) {
28         exit;
29 }
30
31 $sql    = "SELECT access, member_id FROM ".TABLE_PREFIX."courses WHERE course_id=$course";
32 $result = mysql_query($sql, $db);
33 $course_info = mysql_fetch_assoc($result);
34
35 if ($_POST['submit']) {
36         $_SESSION['enroll'] = AT_ENROLL_YES;
37
38         if ($course_info['access'] == 'private') {
39                 $sql    = "INSERT INTO ".TABLE_PREFIX."course_enrollment VALUES ($_SESSION[member_id], $course, 'n', 0, '"._AT('student')."', 0)";
40                 $result = mysql_query($sql, $db);
41
42                 // send the email - if needed
43                 if ($system_courses[$course]['notify'] == 1) {
44                         $mail_list = array();   //initialize an array to store all the pending emails
45
46                         //Get the list of students with enrollment privilege
47                         $module =& $moduleFactory->getModule('_core/enrolment');
48                         $sql    = "SELECT email, first_name, last_name, `privileges` FROM ".TABLE_PREFIX."members m INNER JOIN ".TABLE_PREFIX."course_enrollment ce ON m.member_id=ce.member_id WHERE ce.privileges > 0 AND ce.course_id=$course";
49                         $result = mysql_query($sql, $db);
50                         while ($row     = mysql_fetch_assoc($result)){
51                                 if (query_bit($row['privileges'], $module->getPrivilege())){
52                                         unset($row['privileges']);      //we don't need the privilege to flow around
53                                         $mail_list[] = $row;
54                                 }
55                         }
56                         
57                         //Get instructor information
58                         $ins_id = $system_courses[$course]['member_id'];
59                         $sql    = "SELECT email, first_name, last_name FROM ".TABLE_PREFIX."members WHERE member_id=$ins_id";
60                         $result = mysql_query($sql, $db);
61                         $row    = mysql_fetch_assoc($result);
62                         $mail_list[] = $row;
63
64                         //Send email notification to both assistants with privileges & Instructor
65                         foreach ($mail_list as $row){
66                                 $to_email = $row['email'];
67                                 $tmp_message  = $row['first_name']  .' ' . $row['last_name']."\n\n";
68                                 $tmp_message .= _AT('enrol_messagenew', $system_courses[$course]['title'], AT_BASE_HREF );
69                                 if ($to_email != '') {
70                                         require(AT_INCLUDE_PATH . 'classes/phpmailer/atutormailer.class.php');
71
72                                         $mail = new ATutorMailer;
73                                         $mail->From     = $_config['contact_email'];
74                                         $mail->FromName = $_config['site_name'];
75                                         $mail->AddAddress($to_email);
76                                         $mail->Subject = _AT('enrol_message3');
77                                         $mail->Body    = $tmp_message;
78
79                                         if (!$mail->Send()) {
80                                            require(AT_INCLUDE_PATH.'header.inc.php');
81                                            $msg->printErrors('SENDING_ERROR');
82                                            require(AT_INCLUDE_PATH.'footer.inc.php');
83                                            exit;
84                                         }
85                                         unset($mail);
86                                 }
87                         }
88                 }
89                 $msg->addFeedback('APPROVAL_PENDING');
90                 header('Location: index.php');
91                 exit;
92         } else {
93                 $sql    = "INSERT INTO ".TABLE_PREFIX."course_enrollment VALUES ($_SESSION[member_id], $course, 'y', 0, '"._AT('student')."', 0)";
94                 $result = mysql_query($sql, $db);
95         }
96 }
97
98 $sql    = "SELECT * FROM ".TABLE_PREFIX."course_enrollment WHERE member_id=$_SESSION[member_id] AND course_id=$course";
99 $result = mysql_query($sql, $db);
100 $row_in = mysql_fetch_assoc($result);
101
102 // request has already been made
103 if ($row_in['member_id'] == $_SESSION['member_id'] ) {
104         $msg->addFeedback('ALREADY_REQUESTED');
105         header('Location: ./index.php');
106         exit;
107 }
108
109 require(AT_INCLUDE_PATH.'header.inc.php');
110 ?>
111
112 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
113 <input type="hidden" name="course" value="<?php echo $course; ?>">
114 <div class="input-form">
115         <div class="row">
116                 <?php echo _AT('private_enroll'); ?>
117         </div>
118
119         <div class="row buttons">
120                 <input type="submit" name="submit" value="<?php echo _AT('request_enrollment'); ?>" />
121         </div>
122 </div>
123 </form>
124
125 <?php   require(AT_INCLUDE_PATH.'footer.inc.php'); ?>