remove old readme
[atutor.git] / docs / 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$
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
94                 $sql    = "DELETE FROM ".TABLE_PREFIX."groups_members 
95                             WHERE member_id IN ($members) 
96                               AND group_id IN (SELECT group_id from ".TABLE_PREFIX."groups G, ".TABLE_PREFIX."groups_types GT
97                                                 WHERE G.type_id = GT.type_id AND GT.course_id = ".$course_id.")";
98                 $result = mysql_query($sql, $db);
99
100                 // remove forum subscriptions as admin else instructor 
101                 if($_SESSION['course_id'] == "-1"){
102                         $this_course_id = $_REQUEST['course_id'];
103                 } else {
104                         $this_course_id = $_SESSION['course_id'];
105                 }
106                 
107                 // get a list for forums in this course
108                 $sql = "SELECT forum_id from ".TABLE_PREFIX."forums_courses WHERE course_id = '$this_course_id'";
109                 $result = mysql_query($sql, $db);
110
111                 if($result && mysql_num_rows($result)>0){
112                         while($row = mysql_fetch_assoc($result)){
113                                 $this_course_forums[] = $row['forum_id'];
114                         }
115                         $this_forum_list = implode(',', $this_course_forums);
116
117                         // delete from forum_subscription any member in $members (being unenrolled)
118                         // with posts to forums in this course. 
119                         foreach ($this_course_forums as $this_course_forum){
120                                 $sql1 = "DELETE FROM ".TABLE_PREFIX."forums_subscriptions WHERE forum_id = '$this_course_forum' AND member_id IN ($members)";
121                                 $result_unsub = mysql_query($sql1, $db);
122                         }
123                 }
124
125                 // get a list of posts for forums in the current course
126                 $sql = "SELECT post_id FROM ".TABLE_PREFIX."forums_threads WHERE forum_id IN ($this_forum_list)";
127                 $result = mysql_query($sql, $db);
128                 if($result && mysql_num_rows($result)>0){
129                         while($row = mysql_fetch_assoc($result)){
130                                 $this_course_posts[] = $row['post_id'];
131                         }
132                         $this_post_list = implode(',', $this_course_posts);
133
134                         // delete from forums_accessed any post with member_id in $members being unenrolled, 
135                         // and post_id in 
136                         foreach($this_course_posts as $this_course_post){
137
138                                 $sql2   = "DELETE FROM ".TABLE_PREFIX."forums_accessed WHERE post_id = '$this_course_post' AND member_id IN ($members)";
139                                 $result_unsub2 = mysql_query($sql2, $db);
140                         }
141                 }
142         }
143 }
144
145 /**
146 * Enrolls students into course enrollement
147 * @access  private
148 * @param   array $list                  the IDs of the members to be added
149 * @author  Shozub Qureshi
150 */
151 function enroll ($list) {
152         global $db, $msg, $_config, $course_id, $owner;
153         require(AT_INCLUDE_PATH . 'classes/phpmailer/atutormailer.class.php');
154
155         $num_list = count($list);
156         $members = '(member_id='.$list[0].')';
157         for ($i=0; $i < $num_list; $i++)        {
158                 $id = intval($list[$i]);
159                 $members .= ' OR (member_id='.$id.')';
160                 $sql = "INSERT INTO ".TABLE_PREFIX."course_enrollment VALUES ($id, $course_id, 'y', 0, '', 0)";
161                 $result = mysql_query($sql, $db);
162                 if (mysql_affected_rows($db) != 1) {
163                         $sql = "UPDATE ".TABLE_PREFIX."course_enrollment SET approved='y' WHERE course_id=$course_id AND member_id=$id";
164                         $result = mysql_query($sql, $db);
165                 }
166         }
167
168         //get First_name, Last_name of course Instructor
169         $sql_from    = "SELECT first_name, last_name, email FROM ".TABLE_PREFIX."members WHERE member_id = $owner";
170         $result_from = mysql_query($sql_from, $db);
171         $row_from    = mysql_fetch_assoc($result_from);
172
173         $email_from_name  = $row_from['first_name'] . ' ' . $row_from['last_name'];
174         $email_from = $row_from['email'];
175
176         //get email addresses of users:
177         $sql_to    = "SELECT email FROM ".TABLE_PREFIX."members WHERE ($members)";
178         $result_to = mysql_query($sql_to, $db);
179
180         while ($row_to = mysql_fetch_assoc($result_to)) {
181                 // send email here.
182                 $login_link = AT_BASE_HREF . 'login.php?course=' . $course_id;
183                 $subject = SITE_NAME.': '._AT('enrol_message_subject');
184                 $body = SITE_NAME.': '._AT('enrol_message_approved', $_SESSION['course_title'], $login_link)."\n\n";
185
186                 $mail = new ATutorMailer;
187                 $mail->From     = $_config['contact_email'];
188                 $mail->FromName = $_config['site_name'];
189                 $mail->AddAddress($row_to['email']);
190                 $mail->Subject  = $subject;
191                 $mail->Body     = $body;
192                         
193                 if (!$mail->Send()) {
194                         $msg->addError('SENDING_ERROR');
195                 }
196
197                 unset($mail);
198         }
199
200 }
201
202
203 function group ($list, $gid) {
204         global $db,$msg;
205
206         $sql = "REPLACE INTO ".TABLE_PREFIX."groups_members VALUES ";
207         $gid=intval($gid);
208         for ($i=0; $i < count($list); $i++)     {
209                 $student_id = intval($list[$i]);
210                 $sql .= "($gid, $student_id),";
211         }
212         $sql = substr($sql, 0, -1);
213         mysql_query($sql, $db);
214
215         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
216         header('Location: index.php');
217         exit;
218 }
219
220 function group_remove ($ids, $gid) {
221         global $db,$msg;
222         $gid=intval($gid);
223
224         $ids=implode(',', $ids);
225
226         if ($ids) {
227                 $sql = "DELETE FROM ".TABLE_PREFIX."groups_members WHERE group_id=$gid AND member_id IN ($ids)";
228                 mysql_query($sql, $db);
229                 $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
230         }
231
232         header('Location: index.php');
233         exit;
234 }
235
236 /**
237 * Marks a student as an alumni of the course (not enrolled, but can view course material and participate in forums)
238 * @access  private
239 * @param   array $list                  the IDs of the members to be alumni
240 * @author  Heidi Hazelton
241 */
242 function alumni ($list) {
243         global $db, $course_id;
244         $members = '(member_id='.$list[0].')';
245         for ($i=1; $i < count($list); $i++)     {
246                 $members .= ' OR (member_id='.$list[$i].')';
247         }
248         
249         $sql    = "UPDATE ".TABLE_PREFIX."course_enrollment SET approved = 'a' WHERE course_id=$course_id AND ($members)";
250         $result = mysql_query($sql, $db);
251 }
252
253
254 //course_owner
255 $owner = $system_courses[$course_id]['member_id'];
256
257 if (isset($_POST['submit_no'])) {
258         //if user decides to forgo option
259         $msg->addFeedback('CANCELLED');
260         header('Location: index.php?current_tab='.$_POST['curr_tab'].SEP.'course_id='.$course_id);
261         exit;
262 } /*
263 // No longer used. Unenroll does the same job and removes from groups too.
264 else if (isset($_POST['submit_yes']) && $_POST['func'] =='remove' ) {
265         //Remove student from list (unenrolls automatically)
266
267         //you cannot remove anyone unless you are the course owner
268         authenticate(AT_PRIV_ADMIN);
269
270         //echo 'atleast this worked';
271         remove($_POST['id']);
272
273         $msg->addFeedback('MEMBERS_REMOVED');
274         header('Location: index.php?current_tab=4');
275         exit;
276 }*/
277 else if (isset($_POST['submit_yes']) && $_POST['func'] =='unenroll' ) {
278         //Unenroll student from course
279         unenroll($_POST['id']);
280
281 //      $msg->addFeedback('MEMBERS_UNENROLLED');
282         $msg->addFeedback('MEMBERS_REMOVED');
283         header('Location: index.php?current_tab=4'.SEP.'course_id='.$course_id);
284         exit;
285 } else if (isset($_POST['submit_yes']) && $_POST['func'] =='enroll' ) {
286         //Enroll student in course
287         enroll($_POST['id']);
288
289         $msg->addFeedback('MEMBERS_ENROLLED');
290         header('Location: index.php?current_tab=0'.SEP.'course_id='.$course_id);
291         exit;
292 } else if (isset($_POST['submit_yes']) && $_POST['func'] =='alumni' ) {
293         //Mark student as course alumnus
294         alumni($_POST['id']);
295         
296         $msg->addFeedback('MEMBERS_ALUMNI');
297         header('Location: index.php?current_tab=2'.SEP.'course_id='.$course_id);
298         exit;
299 } else if (isset($_POST['submit_yes']) && $_POST['func'] =='group' ) {
300         //Mark student as a member of the group
301         group($_POST['id'],$_POST['gid']);
302         
303         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
304         header('Location: index.php?current_tab='.$_POST['current_tab'].SEP.'course_id='.$course_id);
305         exit;
306 } else if (isset($_POST['submit_yes']) && $_POST['func'] =='group_remove' ) {
307         // Remove student as a member of the group
308         group_remove($_POST['id'],$_POST['gid']);
309         
310         $msg->addFeedback('ACTION_COMPLETED_SUCCESSFULLY');
311         header('Location: index.php?current_tab='.$_POST['current_tab'].SEP.'course_id='.$course_id);
312         exit;
313 }
314 require(AT_INCLUDE_PATH.'header.inc.php');
315
316 //Store id's into a hidden element for use by functions
317 $j = 0;
318 while ($_GET['id'.$j]) {
319         $_GET['id'.$j] = abs($_GET['id'.$j]);
320         if ($_GET['id'.$j] == $owner) {
321                 //do nothing
322         } else {
323                 $hidden_vars['id['.$j.']'] = $_GET['id'.$j];
324                 $member_ids .= $_GET['id'.$j].', ';
325         }       
326         $j++;
327 }
328 $member_ids = substr($member_ids, 0, -2);
329
330 $hidden_vars['func']     = $_GET['func'];
331 $hidden_vars['current_tab'] = $_GET['current_tab'];
332 $hidden_vars['gid']              = abs($_GET['gid']);
333 $hidden_vars['course_id'] = $course_id;
334 //get usernames of users about to be edited
335 $str = get_usernames($member_ids);
336
337 //Print appropriate confirm msg for action
338 if ($_GET['func'] == 'remove') {
339         $confirm = array('REMOVE_STUDENT',   $str);
340         $msg->addConfirm($confirm, $hidden_vars);
341 } else if ($_GET['func'] == 'enroll') {
342         $confirm = array('ENROLL_STUDENT',   $str);
343         $msg->addConfirm($confirm, $hidden_vars);
344 } else if ($_GET['func'] == 'unenroll') {
345         if (check_roles($member_ids) == 1) {
346                 $confirm = array('UNENROLL_PRIV', $str);
347                 $msg->addConfirm($confirm, $hidden_vars);
348         } else {
349                 $confirm = array('UNENROLL_STUDENT', $str);
350                 $msg->addConfirm($confirm, $hidden_vars);
351         }
352 } else if ($_GET['func'] == 'alumni') {
353         $confirm = array('ALUMNI',   $str);
354         $msg->addConfirm($confirm, $hidden_vars);
355 } else if ($_GET['func'] == 'group') {
356         $sql = "SELECT title FROM ".TABLE_PREFIX."groups WHERE group_id=".$hidden_vars['gid'];
357         $result = mysql_query($sql, $db);
358         $row = mysql_fetch_assoc($result);
359
360         $confirm = array('STUDENT_GROUP', $row['title'], $str);
361         $msg->addConfirm($confirm, $hidden_vars);
362 } else if ($_GET['func'] == 'group_remove') {
363         $sql = "SELECT title FROM ".TABLE_PREFIX."groups WHERE group_id=".$hidden_vars['gid'];
364         $result = mysql_query($sql, $db);
365         $row = mysql_fetch_assoc($result);
366
367         $confirm = array('STUDENT_REMOVE_GROUP', $row['title'], $str);
368         $msg->addConfirm($confirm, $hidden_vars);
369 }
370                 
371 $msg->printConfirm();
372
373 require(AT_INCLUDE_PATH.'footer.inc.php');
374
375 ?>