ATutor 2.0
[atutor.git] / mods / _core / enrolment / html / enroll_edit.inc.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2010                                              */
6 /* Inclusive Design Institute                                           */
7 /* http://atutor.ca                                                     */
8 /* This program is free software. You can redistribute it and/or        */
9 /* modify it under the terms of the GNU General Public License          */
10 /* as published by the Free Software Foundation.                        */
11 /************************************************************************/
12 // $Id: enroll_edit.php 6662 2006-11-20 15:52:49Z joel $
13
14 if (!defined('AT_INCLUDE_PATH')) { exit; }
15
16 /**
17 * Generates the list of login ids of the selected user
18 * @access  private
19 * @param   string $member_ids   the list of members to be checked
20 * @return  string                               The list of login IDs
21 * @author  Shozub Qureshi
22 */
23 function get_usernames ($member_ids) {
24         global $db;
25
26         $sql    = "SELECT login FROM ".TABLE_PREFIX."members WHERE `member_id` IN ($member_ids)";
27
28         $result = mysql_query($sql, $db);
29
30         while ($row = mysql_fetch_assoc($result)) {
31                 $str .= '<li>' . $row['login'] . '</li>';
32         }
33         return $str;
34 }
35
36 /**
37 * Checks if any of the selected users have non-zero roles or privileges
38 * @access  private
39 * @param   string $member_ids   the list of members to be checked
40 * @return  int                                  whether the role/priv is empty or not (0 = if empty, 1 = if ok)
41 * @author  Shozub Qureshi
42 */
43 function check_roles ($member_ids) {
44         global $db;
45
46         $sql    = "SELECT * FROM ".TABLE_PREFIX."course_enrollment WHERE `member_id` IN ($member_ids)";
47         $result = mysql_query($sql, $db);
48
49         while ($row = mysql_fetch_assoc($result)) {
50                 if ($row['role'] != 'Student' || $row['privileges'] != 0) {
51                         return 1;
52                 }
53         }
54         return 0;
55 }
56
57 /**
58 * Removes students from course enrollement
59 * @access  private
60 * @param   array $list                  the IDs of the members to be removed
61 * @author  Shozub Qureshi
62 */
63 /*
64 // no longer used. Unenroll does this job AND removes groups too.
65 function remove ($list) {
66         global $db;
67
68         $members = '(member_id='.$list[0].')';
69         for ($i=1; $i < count($list); $i++) {
70                 $members .= ' OR (member_id='.$list[$i].')';
71         }
72         $sql    = "DELETE FROM ".TABLE_PREFIX."course_enrollment WHERE course_id = $_SESSION[course_id] AND ($members)";        
73         $result = mysql_query($sql, $db);
74 }*/
75
76 /**
77 * Unenrolls students from course enrollement
78 * @access  private
79 * @param   array $list                  the IDs of the members to be removed
80 * @author  Shozub Qureshi
81 * @author  Greg Gay  added Unsubscribe when unenrolling
82 */
83 function unenroll ($list) {
84         global $db, $system_courses, $course_id;
85         $members = implode(',', $list);
86
87         if ($members) {
88                 $members = addslashes($members);
89
90                 $sql    = "DELETE FROM ".TABLE_PREFIX."course_enrollment WHERE course_id=$course_id AND member_id IN ($members)";
91                 $result = mysql_query($sql, $db);
92
93                 $sql    = "DELETE FROM ".TABLE_PREFIX."groups_members WHERE member_id IN ($members)";
94                 $result = mysql_query($sql, $db);
95                 // $groupModule->unenroll(course_id, user_id);
96                 // $forumModule->unenroll(course_id, user_id);
97                 
98                 // remove forum subscriptions as admin else instructor 
99                 if($_SESSION['course_id'] == "-1"){
100                         $this_course_id = $_REQUEST['course_id'];
101                 } else {
102                         $this_course_id = $_SESSION['course_id'];
103                 }
104                 
105                 // get a list for forums in this course
106                 $sql = "SELECT forum_id from ".TABLE_PREFIX."forums_courses WHERE course_id = '$this_course_id'";
107                 $result = mysql_query($sql, $db);
108
109                 if($result && mysql_num_rows($result)>0){
110                         while($row = mysql_fetch_assoc($result)){
111                                 $this_course_forums[] = $row['forum_id'];
112                         }
113                         $this_forum_list = implode(',', $this_course_forums);
114
115                         // delete from forum_subscription any member in $members (being unenrolled)
116                         // with posts to forums in this course. 
117                         foreach ($this_course_forums as $this_course_forum){
118                                 $sql1 = "DELETE FROM ".TABLE_PREFIX."forums_subscriptions WHERE forum_id = '$this_course_forum' AND member_id IN ($members)";
119                                 $result_unsub = mysql_query($sql1, $db);
120                         }
121                 }
122
123                 // get a list of posts for forums in the current course
124                 $sql = "SELECT post_id FROM ".TABLE_PREFIX."forums_threads WHERE forum_id IN ($this_forum_list)";
125                 $result = mysql_query($sql, $db);
126                 if($result && mysql_num_rows($result)>0){
127                         while($row = mysql_fetch_assoc($result)){
128                                 $this_course_posts[] = $row['post_id'];
129                         }
130                         $this_post_list = implode(',', $this_course_posts);
131
132                         // delete from forums_accessed any post with member_id in $members being unenrolled, 
133                         // and post_id in 
134                         foreach($this_course_posts as $this_course_post){
135
136                                 $sql2   = "DELETE FROM ".TABLE_PREFIX."forums_accessed WHERE post_id = '$this_course_post' AND member_id IN ($members)";
137                                 $result_unsub2 = mysql_query($sql2, $db);
138                         }
139                 }
140         }
141 }
142
143 /**
144 * Enrolls students into course enrollement
145 * @access  private
146 * @param   array $list                  the IDs of the members to be added
147 * @author  Shozub Qureshi
148 */
149 function enroll ($list) {
150         global $db, $msg, $_config, $course_id, $owner;
151         require(AT_INCLUDE_PATH . 'classes/phpmailer/atutormailer.class.php');
152
153         $num_list = count($list);
154         $members = '(member_id='.$list[0].')';
155         for ($i=0; $i < $num_list; $i++)        {
156                 $id = intval($list[$i]);
157                 $members .= ' OR (member_id='.$id.')';
158                 $sql = "INSERT INTO ".TABLE_PREFIX."course_enrollment VALUES ($id, $course_id, 'y', 0, '', 0)";
159                 $result = mysql_query($sql, $db);
160                 if (mysql_affected_rows($db) != 1) {
161                         $sql = "UPDATE ".TABLE_PREFIX."course_enrollment SET approved='y' WHERE course_id=$course_id AND member_id=$id";
162                         $result = mysql_query($sql, $db);
163                 }
164         }
165
166         //get First_name, Last_name of course Instructor
167         $sql_from    = "SELECT first_name, last_name, email FROM ".TABLE_PREFIX."members WHERE member_id = $owner";
168         $result_from = mysql_query($sql_from, $db);
169         $row_from    = mysql_fetch_assoc($result_from);
170
171         $email_from_name  = $row_from['first_name'] . ' ' . $row_from['last_name'];
172         $email_from = $row_from['email'];
173
174         //get email addresses of users:
175         $sql_to    = "SELECT email FROM ".TABLE_PREFIX."members WHERE ($members)";
176         $result_to = mysql_query($sql_to, $db);
177
178         while ($row_to = mysql_fetch_assoc($result_to)) {
179                 // send email here.
180                 $login_link = AT_BASE_HREF . 'login.php?course=' . $course_id;
181                 $subject = SITE_NAME.': '._AT('enrol_message_subject');
182                 $body = SITE_NAME.': '._AT('enrol_message_approved', $_SESSION['course_title'], $login_link)."\n\n";
183
184                 $mail = new ATutorMailer;
185                 $mail->From     = $_config['contact_email'];
186                 $mail->FromName = $_config['site_name'];
187                 $mail->AddAddress($row_to['email']);
188                 $mail->Subject  = $subject;
189                 $mail->Body     = $body;
190                         
191                 if (!$mail->Send()) {
192                         $msg->addError('SENDING_ERROR');
193                 }
194
195                 unset($mail);
196         }
197 }
198
199
200 function group ($list, $gid) {
201         global $db,$msg;
202         $sql = "REPLACE INTO ".TABLE_PREFIX."groups_members VALUES ";
203         $gid=intval($gid);
204         for ($i=0; $i < count($list); $i++)     {
205                 $student_id = intval($list[$i]);
206                 $sql .= "($gid, $student_id),";
207         }
208         $sql = substr($sql, 0, -1);
209         mysql_query($sql, $db);
210
211         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
212         header('Location: index.php');
213         exit;
214 }
215
216 function group_remove ($ids, $gid) {
217         global $db,$msg;
218         $gid=intval($gid);
219
220         $ids=implode(',', $ids);
221
222         if ($ids) {
223                 $sql = "DELETE FROM ".TABLE_PREFIX."groups_members WHERE group_id=$gid AND member_id IN ($ids)";
224                 mysql_query($sql, $db);
225                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
226         }
227
228         header('Location: index.php');
229         exit;
230 }
231
232 /**
233 * Marks a student as an alumni of the course (not enrolled, but can view course material and participate in forums)
234 * @access  private
235 * @param   array $list                  the IDs of the members to be alumni
236 * @author  Heidi Hazelton
237 */
238 function alumni ($list) {
239         global $db, $course_id;
240         $members = '(member_id='.$list[0].')';
241         for ($i=1; $i < count($list); $i++)     {
242                 $members .= ' OR (member_id='.$list[$i].')';
243         }
244         
245         $sql    = "UPDATE ".TABLE_PREFIX."course_enrollment SET approved = 'a' WHERE course_id=$course_id AND ($members)";
246         $result = mysql_query($sql, $db);
247 }
248
249
250 //course_owner
251 $owner = $system_courses[$course_id]['member_id'];
252
253 if (isset($_POST['submit_no'])) {
254         //if user decides to forgo option
255         $msg->addFeedback('CANCELLED');
256         header('Location: index.php?current_tab='.$_POST['curr_tab'].SEP.'course_id='.$course_id);
257         exit;
258 } /*
259 // No longer used. Unenroll does the same job and removes from groups too.
260 else if (isset($_POST['submit_yes']) && $_POST['func'] =='remove' ) {
261         //Remove student from list (unenrolls automatically)
262
263         //you cannot remove anyone unless you are the course owner
264         authenticate(AT_PRIV_ADMIN);
265
266         //echo 'atleast this worked';
267         remove($_POST['id']);
268
269         $msg->addFeedback('MEMBERS_REMOVED');
270         header('Location: index.php?current_tab=4');
271         exit;
272 }*/
273 else if (isset($_POST['submit_yes']) && $_POST['func'] =='unenroll' ) {
274         //Unenroll student from course
275         unenroll($_POST['id']);
276
277 //      $msg->addFeedback('MEMBERS_UNENROLLED');
278         $msg->addFeedback('MEMBERS_REMOVED');
279         header('Location: index.php?current_tab=4'.SEP.'course_id='.$course_id);
280         exit;
281 } else if (isset($_POST['submit_yes']) && $_POST['func'] =='enroll' ) {
282         //Enroll student in course
283         enroll($_POST['id']);
284
285         $msg->addFeedback('MEMBERS_ENROLLED');
286         header('Location: index.php?current_tab=0'.SEP.'course_id='.$course_id);
287         exit;
288 } else if (isset($_POST['submit_yes']) && $_POST['func'] =='alumni' ) {
289         //Mark student as course alumnus
290         alumni($_POST['id']);
291         
292         $msg->addFeedback('MEMBERS_ALUMNI');
293         header('Location: index.php?current_tab=2'.SEP.'course_id='.$course_id);
294         exit;
295 } else if (isset($_POST['submit_yes']) && $_POST['func'] =='group' ) {
296         //Mark student as a member of the group
297         group($_POST['id'],$_POST['gid']);
298         
299         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
300         header('Location: index.php?current_tab='.$_POST['current_tab'].SEP.'course_id='.$course_id);
301         exit;
302 } else if (isset($_POST['submit_yes']) && $_POST['func'] =='group_remove' ) {
303         // Remove student as a member of the group
304         group_remove($_POST['id'],$_POST['gid']);
305         
306         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
307         header('Location: index.php?current_tab='.$_POST['current_tab'].SEP.'course_id='.$course_id);
308         exit;
309 }
310 require(AT_INCLUDE_PATH.'header.inc.php');
311
312 //Store id's into a hidden element for use by functions
313 $j = 0;
314 while ($_GET['id'.$j]) {
315         $_GET['id'.$j] = abs($_GET['id'.$j]);
316         if ($_GET['id'.$j] == $owner) {
317                 //do nothing
318         } else {
319                 $hidden_vars['id['.$j.']'] = $_GET['id'.$j];
320                 $member_ids .= $_GET['id'.$j].', ';
321         }       
322         $j++;
323 }
324 $member_ids = substr($member_ids, 0, -2);
325
326 $hidden_vars['func']     = $_GET['func'];
327 $hidden_vars['current_tab'] = $_GET['current_tab'];
328 $hidden_vars['gid']              = abs($_GET['gid']);
329 $hidden_vars['course_id'] = $course_id;
330 //get usernames of users about to be edited
331 $str = get_usernames($member_ids);
332
333 //Print appropriate confirm msg for action
334 if ($_GET['func'] == 'remove') {
335         $confirm = array('REMOVE_STUDENT',   $str);
336         $msg->addConfirm($confirm, $hidden_vars);
337 } else if ($_GET['func'] == 'enroll') {
338         $confirm = array('ENROLL_STUDENT',   $str);
339         $msg->addconfirm($confirm, $hidden_vars);
340 } else if ($_GET['func'] == 'unenroll') {
341         if (check_roles($member_ids) == 1) {
342                 $confirm = array('UNENROLL_PRIV', $str);
343                 $msg->addConfirm($confirm, $hidden_vars);
344         } else {
345                 $confirm = array('UNENROLL_STUDENT', $str);
346                 $msg->addConfirm($confirm, $hidden_vars);
347         }
348 } else if ($_GET['func'] == 'alumni') {
349         $confirm = array('ALUMNI',   $str);
350         $msg->addConfirm($confirm, $hidden_vars);
351 } else if ($_GET['func'] == 'group') {
352         $sql = "SELECT title FROM ".TABLE_PREFIX."groups WHERE group_id=".$hidden_vars['gid'];
353         $result = mysql_query($sql, $db);
354         $row = mysql_fetch_assoc($result);
355
356         $confirm = array('STUDENT_GROUP', $row['title'], $str);
357         $msg->addConfirm($confirm, $hidden_vars);
358 } else if ($_GET['func'] == 'group_remove') {
359         $sql = "SELECT title FROM ".TABLE_PREFIX."groups WHERE group_id=".$hidden_vars['gid'];
360         $result = mysql_query($sql, $db);
361         $row = mysql_fetch_assoc($result);
362
363         $confirm = array('STUDENT_REMOVE_GROUP', $row['title'], $str);
364         $msg->addConfirm($confirm, $hidden_vars);
365 }
366                 
367 $msg->printConfirm();
368
369 require(AT_INCLUDE_PATH.'footer.inc.php');
370
371 ?>