tagging as ATutor 1.5.4-release
[atutor.git] / include / html / enrollment.inc.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2006 by Greg Gay & Joel Kronenberg        */
6 /* Adaptive Technology Resource Centre / University of Toronto  */
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: index.php 6751 2007-02-12 18:56:44Z joel $
14 if (!defined('AT_INCLUDE_PATH')) { exit; }
15
16 if (isset($_POST['enroll'])) {
17         if (!$_POST['id'])      {
18                 $msg->addError('NO_STUDENT_SELECTED');
19                 $_GET['tab'] = $_POST['tab'];
20         } else {
21                 $i=0;
22                 foreach ($_POST['id'] as $elem) {
23                         $text .= 'id'.$i.'='.$elem.SEP;
24                         $i++;
25                 }
26                 header('Location: enroll_edit.php?'.$text.'func=enroll'.SEP.'tab=0'.SEP.'course_id='.$course_id);
27                 exit;
28         }
29 } else if (isset($_POST['unenroll'])) {
30         // different from a plain delete. This removes from groups as well.
31         if (!$_POST['id'])      {
32                 $msg->addError('NO_STUDENT_SELECTED');
33                 $_GET['tab'] = $_POST['tab'];
34         } else {
35                 $i=0;
36                 foreach ($_POST['id'] as $elem) {
37                         $text .= 'id'.$i.'='.$elem.SEP;
38                         $i++;
39                 }
40                 header('Location: enroll_edit.php?'.$text.'func=unenroll'.SEP.'tab=1'.SEP.'course_id='.$course_id);
41                 exit;   
42         }
43 } else if (isset($_POST['role'])) {
44         if (!$_POST['id'])      {
45                 $msg->addError('NO_STUDENT_SELECTED');
46                 $_GET['tab'] = $_POST['tab'];
47         } else {
48                 $i=0;
49                 foreach ($_POST['id'] as $elem) {
50                         $text .= 'mid'.$i.'='.$elem.SEP;
51                         $i++;
52                 }
53                 header('Location: privileges.php?'.$text.SEP.'course_id='.$course_id);
54                 exit;
55         }
56 } else if (isset($_POST['alumni'])) {
57         if (!$_POST['id'])      {
58                 $msg->addError('NO_STUDENT_SELECTED');
59                 $_GET['tab'] = $_POST['tab'];
60         } else {
61                 $i=0;
62                 foreach ($_POST['id'] as $elem) {
63                         $text .= 'id'.$i.'='.$elem.SEP;
64                         $i++;
65                 }
66                 header('Location: enroll_edit.php?'.$text.'func=alumni'.SEP.'tab=2'.SEP.'course_id='.$course_id);
67                 exit;
68         }
69 }
70
71 //filter stuff:
72
73 if ($_GET['reset_filter']) {
74         unset($_GET);
75 }
76
77 $filter=array();
78
79 if (isset($_GET['role']) && ($_GET['role'] != '')) {
80         $filter['role'] = intval($_GET['role']);
81
82
83 if (isset($_GET['status']) && ($_GET['status'] != '')) {
84         $filter['status'] = intval($_GET['status']);
85
86
87 if (isset($_GET['group']) && ($_GET['group'] != '')) {
88         $filter['group'] = intval($_GET['group']);
89
90
91 require(AT_INCLUDE_PATH.'html/enroll_tab_functions.inc.php');
92 $tabs = get_tabs();     
93 $num_tabs = count($tabs);
94
95 for ($i=0; $i < $num_tabs; $i++) {
96         if (isset($_POST['button_'.$i]) && ($_POST['button_'.$i] != -1)) { 
97                 $current_tab = $i;
98                 $_POST['current_tab'] = $i;
99                 break;
100         }
101 }
102
103 //get present tab if specified
104 if ($_GET['current_tab']) {
105         $current_tab = $_GET['current_tab'];
106         $_POST['current_tab'] = $_GET['current_tab'];
107 }
108
109 $orders = array('asc' => 'desc', 'desc' => 'asc');
110 $cols   = array('login' => 1, 'first_name' => 1, 'second_name' => 1, 'last_name' => 1, 'email' => 1);
111
112 if (isset($_GET['asc'])) {
113         $order = 'asc';
114         $col   = isset($cols[$_GET['asc']]) ? $_GET['asc'] : 'login';
115 } else if (isset($_GET['desc'])) {
116         $order = 'desc';
117         $col   = isset($cols[$_GET['desc']]) ? $_GET['desc'] : 'login';
118 } else {
119         // no order set
120         $order = 'asc';
121         $col   = 'login';
122 }
123 $view_select = intval($_POST['view_select']);
124
125 // the possible tabs. order matters.
126 $tabs = array('enrolled', 'assistants', 'alumni', 'pending_enrollment', 'not_enrolled');
127
128 $num_tabs = count($tabs);
129 if (isset($_REQUEST['tab'])) {
130         $current_tab = intval($_REQUEST['tab']);
131 }
132
133 if (!isset($current_tab)) {
134         $current_tab = 0;
135 }
136
137 if (isset($_GET['match']) && $_GET['match'] == 'one') {
138         $checked_match_one = ' checked="checked"';
139         $page_string .= SEP.'match=one';
140 } else {
141         $_GET['match'] = 'all';
142         $checked_match_all = ' checked="checked"';
143         $page_string .= SEP.'match=all';
144 }
145
146 if (admin_authenticate(AT_ADMIN_PRIV_ENROLLMENT, TRUE)) {
147         $page_string .= SEP.'course_id='.$course_id;
148 }
149
150 if ($_GET['search']) {
151         $page_string .= SEP.'search='.urlencode($_GET['search']);
152         $search = $addslashes($_GET['search']);
153         $search = explode(' ', $search);
154
155         if ($_GET['match'] == 'all') {
156                 $predicate = 'AND ';
157         } else {
158                 $predicate = 'OR ';
159         }
160
161         $sql = '';
162         foreach ($search as $term) {
163                 $term = trim($term);
164                 $term = str_replace(array('%','_'), array('\%', '\_'), $term);
165                 if ($term) {
166                         $term = '%'.$term.'%';
167                         $sql .= "((M.first_name LIKE '$term') OR (M.second_name LIKE '$term') OR (M.last_name LIKE '$term') OR (M.email LIKE '$term') OR (M.login LIKE '$term')) $predicate";
168                 }
169         }
170         $sql = '('.substr($sql, 0, -strlen($predicate)).')';
171         $search = $sql;
172 } else {
173         $search = '1';
174 }
175
176
177 $instructor_id = $system_courses[$course_id]['member_id'];
178 if ($current_tab == 0) {
179         // enrolled
180         $sql_cnt = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."course_enrollment CE, ".TABLE_PREFIX."members M 
181                                 WHERE CE.course_id=$course_id AND CE.member_id=M.member_id AND approved='y' AND M.member_id<>$instructor_id AND CE.privileges=0 AND $search";
182         $sql    =  "SELECT CE.member_id, CE.privileges, CE.approved, M.login, M.first_name, M.second_name, M.last_name, M.email 
183                                 FROM ".TABLE_PREFIX."course_enrollment CE, ".TABLE_PREFIX."members M 
184                                 WHERE CE.course_id=$course_id AND CE.member_id=M.member_id AND approved='y' AND M.member_id<>$instructor_id AND CE.privileges=0 AND $search
185                                 ORDER BY $col $order";
186 } else if ($current_tab == 1) {
187         // assistants
188         $sql_cnt=  "SELECT COUNT(*) AS cnt
189                                 FROM ".TABLE_PREFIX."course_enrollment CE, ".TABLE_PREFIX."members M 
190                                 WHERE CE.course_id=$course_id AND CE.member_id=M.member_id AND CE.approved='y' AND CE.privileges>0 AND $search";
191         $sql    =  "SELECT CE.member_id, CE.approved, CE.privileges, M.login, M.first_name, M.second_name, M.last_name, M.email 
192                                 FROM ".TABLE_PREFIX."course_enrollment CE, ".TABLE_PREFIX."members M 
193                                 WHERE CE.course_id=$course_id AND CE.member_id=M.member_id AND CE.approved='y' AND CE.privileges>0 AND $search
194                                 ORDER BY $col $order";
195
196 } else if ($current_tab == 3) {
197         // pending
198         if ($system_courses[$course_id]['access'] == 'private') {
199                 $sql_cnt = "SELECT COUNT(*) AS cnt 
200                                 FROM ".TABLE_PREFIX."course_enrollment CE, ".TABLE_PREFIX."members M 
201                                 WHERE CE.course_id=$course_id AND CE.member_id=M.member_id AND approved='n' AND $search";
202
203                 $sql    =  "SELECT CE.member_id, CE.approved, CE.privileges, M.login, M.first_name, M.second_name, M.last_name, M.email 
204                                 FROM ".TABLE_PREFIX."course_enrollment CE, ".TABLE_PREFIX."members M 
205                                 WHERE CE.course_id=$course_id AND CE.member_id=M.member_id AND approved='n' AND $search
206                                 ORDER BY $col $order";
207         } else {
208                 // not sure what this is about
209                 $sql_cnt = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."members WHERE 0";
210                 $sql = "SELECT login FROM ".TABLE_PREFIX."members WHERE 0";
211         }
212 } else if ($current_tab == 2) {
213         // alumni
214         $sql_cnt=  "SELECT COUNT(*) AS cnt
215                                 FROM ".TABLE_PREFIX."course_enrollment CE, ".TABLE_PREFIX."members M 
216                                 WHERE CE.course_id=$course_id AND CE.member_id=M.member_id AND approved='a' AND $search";
217         $sql    =  "SELECT CE.member_id, CE.approved, CE.privileges, M.login, M.first_name, M.second_name, M.last_name, M.email 
218                                 FROM ".TABLE_PREFIX."course_enrollment CE, ".TABLE_PREFIX."members M 
219                                 WHERE CE.course_id=$course_id AND CE.member_id=M.member_id AND approved='a' AND $search
220                                 ORDER BY $col $order";
221 } else {
222         // not sure what this is about
223         $tmp_sql        =  "SELECT member_id FROM ".TABLE_PREFIX."course_enrollment WHERE course_id=$course_id AND member_id<>$instructor_id ";
224         $tmp_result = mysql_query($tmp_sql, $db);
225         $course_enrollment = '';
226         while ($row = mysql_fetch_assoc($tmp_result)) {
227                 $course_enrollment .= $row['member_id'] .',';
228         }
229         $course_enrollment .= $instructor_id;
230
231         $sql_cnt=  "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."members M WHERE M.status>1 AND M.member_id NOT IN ($course_enrollment) AND $search";
232
233         $sql    =  "SELECT M.member_id, M.login, M.first_name, M.second_name, M.last_name, M.email FROM ".TABLE_PREFIX."members M WHERE M.member_id NOT IN ($course_enrollment) AND M.status>1 AND $search ORDER BY $col $order";
234 }
235
236 $results_per_page = 50;
237
238 $result = mysql_query($sql_cnt, $db);
239 $row = mysql_fetch_assoc($result);
240 $num_results = $row['cnt'];
241
242 $num_pages = max(ceil($num_results / $results_per_page), 1);
243 $page = intval($_GET['p']);
244 if (!$page) {
245         $page = 1;
246 }       
247 $count  = (($page-1) * $results_per_page) + 1;
248 $offset = ($page-1)*$results_per_page;
249 $sql .= " LIMIT $offset, $results_per_page";
250
251 $enrollment_result = mysql_query($sql, $db);
252 $page_string .= SEP . 'tab='.$current_tab;
253 require(AT_INCLUDE_PATH.'header.inc.php');
254
255 ?>
256
257 <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
258         <input type="hidden" name="tab" value="<?php echo $current_tab; ?>"/>
259         <input type="hidden" name="course_id" value="<?php echo $course_id; ?>"/>
260         <div class="input-form">
261                 <div class="row">
262                         <h3><?php echo _AT('results_found', $num_results); ?></h3>
263                 </div>
264
265                 <?php if (admin_authenticate(AT_ADMIN_PRIV_ENROLLMENT, TRUE)): ?>
266                         <div class="row">
267                                 <label for="course"><?php echo _AT('course'); ?></label><br/>
268                                 <select name="course_id">
269                                 <?php
270                                 $sql = "SELECT course_id, title FROM ".TABLE_PREFIX."courses ORDER BY title";
271                                 $result = mysql_query($sql, $db);
272                                 while ($courses_row = mysql_fetch_assoc($result)) {
273                                         if ($courses_row['course_id'] == $course_id) {
274                                                 echo '<option value="'.$courses_row['course_id'].'" selected="selected">'.$courses_row['title'].'</option>';
275                                         } else {
276                                                 echo '<option value="'.$courses_row['course_id'].'">'.$courses_row['title'].'</option>';
277                                         }
278                                 }
279                                 ?></select>
280                         </div>
281                 <?php endif; ?>
282
283                 <div class="row">
284                         <label for="search"><?php echo _AT('search'); ?> (<?php echo _AT('login_name').', '._AT('first_name').', '._AT('second_name').', '._AT('last_name') .', '._AT('email'); ?>)</label><br />
285                         <input type="text" name="search" id="search" size="40" value="<?php echo htmlspecialchars($_GET['search']); ?>" />
286                         <br/>
287                         <?php echo _AT('search_match'); ?>:
288                         <input type="radio" name="match" value="all" id="match_all" <?php echo $checked_match_all; ?> /><label for="match_all"><?php echo _AT('search_all_words'); ?></label> <input type="radio" name="match" value="one" id="match_one" <?php echo $checked_match_one; ?> /><label for="match_one"><?php echo _AT('search_any_word'); ?></label>
289                 </div>
290
291                 <div class="row buttons">
292                         <input type="submit" name="filter" value="<?php echo _AT('filter'); ?>" />
293                         <input type="submit" name="reset_filter" value="<?php echo _AT('reset_filter'); ?>" />
294                 </div>
295         </div>
296 </form>
297
298 <?php print_paginator($page, $num_results, $page_string . SEP . $order .'='. $col, $results_per_page); ?>
299
300 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="selectform">
301 <input type="hidden" name="tab" value="<?php echo $current_tab; ?>" />
302
303 <div style="width: 95%; margin-right: auto; margin-left: auto;">
304 <ul id="navlist">
305         <?php for ($i = 0; $i< $num_tabs; $i++): ?>
306                 <?php if ($current_tab == $i): ?>
307                         <li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?tab=<?php echo $i.SEP; ?>course_id=<?php echo $course_id; ?>" class="active"><strong><?php echo _AT($tabs[$i]); ?></strong></a></li>
308                 <?php else: ?>
309                         <li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?tab=<?php echo $i.SEP; ?>course_id=<?php echo $course_id; ?>"><?php echo _AT($tabs[$i]); ?></a></li>
310                 <?php endif; ?>
311         <?php endfor; ?>
312 </ul>
313 </div>
314
315 <table class="data" style="width:95%;" summary="" rules="cols">
316 <colgroup>
317         <?php if ($col == 'login'): ?>
318                 <col />
319                 <col class="sort" />
320                 <col span="4" />
321         <?php elseif($col == 'first_name'): ?>
322                 <col span="2" />
323                 <col class="sort" />
324                 <col span="3" />
325         <?php elseif($col == 'second_name'): ?>
326                 <col span="3" />
327                 <col class="sort" />
328                 <col span="2" />
329         <?php elseif($col == 'last_name'): ?>
330                 <col span="4" />
331                 <col class="sort" />
332                 <col />
333         <?php elseif($col == 'email'): ?>
334                 <col span="5" />
335                 <col class="sort" />
336         <?php endif; ?>
337 </colgroup>
338 <thead>
339 <tr>
340         <th scope="col" align="left"><input type="checkbox" value="<?php echo _AT('select_all'); ?>" id="all" title="<?php echo _AT('select_all'); ?>" name="selectall" onclick="CheckAll();" /></th>
341
342         <th scope="col"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $orders[$order]; ?>=login<?php echo $page_string;?>"><?php echo _AT('login_name'); ?></a></th>
343
344         <th scope="col"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $orders[$order]; ?>=first_name<?php echo $page_string;?>"><?php echo _AT('first_name'); ?></a></th>
345
346         <th scope="col"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $orders[$order]; ?>=second_name<?php echo $page_string;?>"><?php echo _AT('second_name'); ?></a></th>
347
348         <th scope="col"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $orders[$order]; ?>=last_name<?php echo $page_string;?>"><?php echo _AT('last_name'); ?></a></th>
349
350         <th scope="col"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $orders[$order]; ?>=email<?php echo $page_string;?>"><?php echo _AT('email'); ?></a></th>
351 </tr>
352 </thead>
353 <tfoot>
354 <tr>
355         <td colspan="6">
356                 <?php if ($current_tab == 0): ?>
357                         <input type="submit" name="role"     value="<?php echo _AT('privileges');  ?>" /> 
358                         <input type="submit" name="unenroll" value="<?php echo _AT('remove');    ?>" /> 
359                         <input type="submit" name="alumni"   value="<?php echo _AT('mark_alumni'); ?>" />
360                 <?php elseif ($current_tab == 1): ?>
361                         <input type="submit" name="role" value="<?php echo _AT('privileges'); ?>" /> 
362                         <input type="submit" name="unenroll" value="<?php echo _AT('remove'); ?>" /> 
363
364                 <?php elseif ($current_tab == 2): ?>
365                         <input type="submit" name="enroll"   value="<?php echo _AT('enroll'); ?>" /> 
366                         <input type="submit" name="unenroll" value="<?php echo _AT('remove'); ?>" />
367                 
368                 <?php elseif ($current_tab == 3): ?>
369                         <input type="submit" name="enroll" value="<?php echo _AT('enroll'); ?>" /> 
370                         <input type="submit" name="unenroll" value="<?php echo _AT('remove'); ?>" />
371
372                 <?php elseif ($current_tab == 4): ?>
373                         <input type="submit" name="enroll"   value="<?php echo _AT('enroll'); ?>" /> 
374
375                 <?php endif; ?></td>
376 </tr>
377 </tfoot>
378 <tbody>
379 <?php if ($num_results): ?>
380         <?php while ($row = mysql_fetch_assoc($enrollment_result)): ?>
381                 <tr onmousedown="document.selectform['m<?php echo $row['member_id']; ?>'].checked = !document.selectform['m<?php echo $row['member_id']; ?>'].checked;">
382                         <td><input type="checkbox" name="id[]" value="<?php echo $row['member_id']; ?>" id="m<?php echo $row['member_id']; ?>" onmouseup="this.checked=!this.checked" title="<?php echo AT_print($row['login'], 'members.login'); ?>" /></td>
383                         <td><?php echo AT_print($row['login'], 'members.login'); ?></td>
384                         <td><?php echo AT_print($row['first_name'], 'members.name'); ?></td>
385                         <td><?php echo AT_print($row['second_name'], 'members.name'); ?></td>
386                         <td><?php echo AT_print($row['last_name'], 'members.name'); ?></td>
387                         <td><?php echo AT_print($row['email'], 'members.email'); ?></td>
388                 </tr>
389         <?php endwhile; ?>
390 <?php else: ?>
391         <tr>
392                 <td colspan="6"><?php echo _AT('none_found'); ?></td>
393         </tr>
394 <?php endif; ?>
395 </tbody>
396 </table>
397 </form>
398
399 <script language="JavaScript" type="text/javascript">
400 //<!--
401 function CheckAll() {
402         for (var i=0;i<document.selectform.elements.length;i++) {
403                 var e = document.selectform.elements[i];
404                 if ((e.name == 'id[]') && (e.type=='checkbox'))
405                         e.checked = document.selectform.selectall.checked;
406         }
407 }
408 //-->
409 </script>
410 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>