made a copy
[atutor.git] / include / html / enrollment.inc.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2008 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$
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
94
95 //debug( $num_tabs);
96 $num_tabs = count($tabs);
97
98 for ($i=0; $i < $num_tabs; $i++) {
99         if (isset($_POST['button_'.$i]) && ($_POST['button_'.$i] != -1)) { 
100                 $current_tab = $i;
101                 $_POST['current_tab'] = $i;
102                 break;
103         }
104 }
105
106 //get present tab if specified
107 if ($_GET['current_tab']) {
108         $current_tab = $_GET['current_tab'];
109         $_POST['current_tab'] = $_GET['current_tab'];
110 }
111
112 $orders = array('asc' => 'desc', 'desc' => 'asc');
113 $cols   = array('login' => 1, 'first_name' => 1, 'second_name' => 1, 'last_name' => 1, 'email' => 1);
114
115 if (isset($_GET['asc'])) {
116         $order = 'asc';
117         $col   = isset($cols[$_GET['asc']]) ? $_GET['asc'] : 'login';
118 } else if (isset($_GET['desc'])) {
119         $order = 'desc';
120         $col   = isset($cols[$_GET['desc']]) ? $_GET['desc'] : 'login';
121 } else {
122         // no order set
123         $order = 'asc';
124         $col   = 'login';
125 }
126 $view_select = intval($_POST['view_select']);
127
128 // the possible tabs. order matters.
129 $tabs = array('enrolled', 'assistants', 'alumni', 'pending_enrollment', 'not_enrolled');
130
131
132 // Remove Not Enrolled tab if system preference is turned off 1.6.2
133 if($_config['allow_instructor_registration'] != 1){
134         array_pop($tabs);
135 }
136
137 $num_tabs = count($tabs);
138 if (isset($_REQUEST['tab'])) {
139         $current_tab = intval($_REQUEST['tab']);
140 }
141
142 if (!isset($current_tab)) {
143         $current_tab = 0;
144 }
145
146 if (isset($_GET['match']) && $_GET['match'] == 'one') {
147         $checked_match_one = ' checked="checked"';
148         $page_string .= SEP.'match=one';
149 } else {
150         $_GET['match'] = 'all';
151         $checked_match_all = ' checked="checked"';
152         $page_string .= SEP.'match=all';
153 }
154
155 if (admin_authenticate(AT_ADMIN_PRIV_ENROLLMENT, TRUE)) {
156         $page_string .= SEP.'course_id='.$course_id;
157 }
158
159 if ($_GET['search']) {
160         $page_string .= SEP.'search='.urlencode($_GET['search']);
161         $search = $addslashes($_GET['search']);
162         $search = explode(' ', $search);
163
164         if ($_GET['match'] == 'all') {
165                 $predicate = 'AND ';
166         } else {
167                 $predicate = 'OR ';
168         }
169
170         $sql = '';
171         foreach ($search as $term) {
172                 $term = trim($term);
173                 $term = str_replace(array('%','_'), array('\%', '\_'), $term);
174                 if ($term) {
175                         $term = '%'.$term.'%';
176                         $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";
177                 }
178         }
179         $sql = '('.substr($sql, 0, -strlen($predicate)).')';
180         $search = $sql;
181 } else {
182         $search = '1';
183 }
184
185 $instructor_id = $system_courses[$course_id]['member_id'];
186 // retrieve all the members of this course (used later to get all those who aren't in this course)
187 $course_enrollment = get_group_concat('course_enrollment', 'member_id', "course_id=$course_id AND member_id<>$instructor_id");
188 $course_enrollment .= ','.$instructor_id;
189
190 $tab_counts     = array();
191 $tab_sql_counts = array();
192 $tab_sql_counts[0] = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."course_enrollment CE INNER JOIN ".TABLE_PREFIX."members M USING (member_id) WHERE CE.course_id=$course_id 
193                                                 AND CE.approved='y' AND M.member_id<>$instructor_id AND CE.privileges=0 AND $search";
194 $tab_sql_counts[1] = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."course_enrollment CE INNER JOIN ".TABLE_PREFIX."members M USING (member_id) WHERE CE.course_id=$course_id 
195                                                 AND CE.approved='y' AND CE.privileges>0 AND $search";
196 $tab_sql_counts[2] = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."course_enrollment CE INNER JOIN ".TABLE_PREFIX."members M USING (member_id) WHERE CE.course_id=$course_id 
197                                                 AND approved='a' AND $search";
198 $tab_sql_counts[3] = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."course_enrollment CE INNER JOIN ".TABLE_PREFIX."members M USING (member_id) WHERE CE.course_id=$course_id
199                                                 AND approved='n' AND $search";
200 $tab_sql_counts[4] = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."members M WHERE M.status>1 AND M.member_id NOT IN ($course_enrollment) AND $search";
201
202 foreach ($tab_sql_counts as $tab => $sql) {
203         if ($tab == 3 && $system_courses[$course_id]['access'] != 'private') {
204                 $tab_counts[$tab] = 0;
205         } else {
206                 $result = mysql_query($sql);
207                 $row    = mysql_fetch_assoc($result);
208                 $tab_counts[$tab] = $row['cnt'];
209         }
210 }
211
212
213 if ($current_tab == 0) {
214         // enrolled
215         $sql    =  "SELECT CE.member_id, CE.privileges, CE.approved, M.login, M.first_name, M.second_name, M.last_name, M.email 
216                                 FROM ".TABLE_PREFIX."course_enrollment CE INNER JOIN ".TABLE_PREFIX."members M USING (member_id)
217                                 WHERE CE.course_id=$course_id AND approved='y' AND M.member_id<>$instructor_id AND CE.privileges=0 AND $search
218                                 ORDER BY $col $order";
219 } else if ($current_tab == 1) {
220         // assistants
221         $sql    =  "SELECT CE.member_id, CE.approved, CE.privileges, M.login, M.first_name, M.second_name, M.last_name, M.email 
222                                 FROM ".TABLE_PREFIX."course_enrollment CE INNER JOIN ".TABLE_PREFIX."members M USING (member_id)
223                                 WHERE CE.course_id=$course_id AND CE.approved='y' AND CE.privileges>0 AND $search
224                                 ORDER BY $col $order";
225
226 } else if ($current_tab == 3) {
227         // pending
228         if ($system_courses[$course_id]['access'] == 'private') {
229                 $sql    =  "SELECT CE.member_id, CE.approved, CE.privileges, M.login, M.first_name, M.second_name, M.last_name, M.email 
230                                 FROM ".TABLE_PREFIX."course_enrollment CE INNER JOIN ".TABLE_PREFIX."members M USING (member_id)
231                                 WHERE CE.course_id=$course_id AND approved='n' AND $search
232                                 ORDER BY $col $order";
233         } else {
234                 // not sure what this is about
235 //              $sql_cnt = "SELECT COUNT(*) AS cnt FROM ".TABLE_PREFIX."members WHERE 0";
236                 $sql = "SELECT login FROM ".TABLE_PREFIX."members WHERE 0";
237         }
238 } else if ($current_tab == 2) {
239         // alumni
240         $sql    =  "SELECT CE.member_id, CE.approved, CE.privileges, M.login, M.first_name, M.second_name, M.last_name, M.email 
241                                 FROM ".TABLE_PREFIX."course_enrollment CE INNER JOIN ".TABLE_PREFIX."members M USING (member_id)
242                                 WHERE CE.course_id=$course_id AND approved='a' AND $search
243                                 ORDER BY $col $order";
244 } else { // current_tab == 4
245
246 //      $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";
247         
248         $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";
249 }
250
251 $results_per_page = 50;
252
253 $num_pages = max(ceil($tab_counts[$current_tab] / $results_per_page), 1);
254 $page = intval($_GET['p']);
255 if (!$page) {
256         $page = 1;
257 }       
258 $count  = (($page-1) * $results_per_page) + 1;
259 $offset = ($page-1)*$results_per_page;
260 $sql .= " LIMIT $offset, $results_per_page";
261
262 $enrollment_result = mysql_query($sql, $db);
263 $page_string_w_tab = $page_string . SEP . 'tab='.$current_tab;
264 require(AT_INCLUDE_PATH.'header.inc.php');
265
266 ?>
267
268 <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
269         <input type="hidden" name="tab" value="<?php echo $current_tab; ?>"/>
270         <input type="hidden" name="course_id" value="<?php echo $course_id; ?>"/>
271         <div class="input-form">
272         <fieldset class="group_form"><legend class="group_form"><?php echo _AT('search'); ?></legend>
273                 <?php if (admin_authenticate(AT_ADMIN_PRIV_ENROLLMENT, TRUE)): ?>
274                         <div class="row">
275                                 <label for="course"><?php echo _AT('course'); ?></label><br/>
276                                 <select name="course_id" id="course">
277                                 <?php
278                                 $sql = "SELECT course_id, title FROM ".TABLE_PREFIX."courses ORDER BY title";
279                                 $result = mysql_query($sql, $db);
280                                 while ($courses_row = mysql_fetch_assoc($result)) {
281                                         if ($courses_row['course_id'] == $course_id) {
282                                                 echo '<option value="'.$courses_row['course_id'].'" selected="selected">'.validate_length($courses_row['title'], 45,VALIDATE_LENGTH_FOR_DISPLAY).'</option>';
283                                         } else {
284                                                 echo '<option value="'.$courses_row['course_id'].'">'.validate_length($courses_row['title'],45,VALIDATE_LENGTH_FOR_DISPLAY).'</option>';
285                                         }
286                                 }
287                                 ?></select>
288                         </div>
289                 <?php endif; ?>
290
291                 <div class="row">
292                         <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 />
293                         <input type="text" name="search" id="search" size="40" value="<?php echo htmlspecialchars($_GET['search']); ?>" />
294                         <br/>
295                         <?php echo _AT('search_match'); ?>:
296                         <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>
297                 </div>
298
299                 <div class="row buttons">
300                         <input type="submit" name="filter" value="<?php echo _AT('filter'); ?>" />
301                         <input type="submit" name="reset_filter" value="<?php echo _AT('reset_filter'); ?>" />
302                 </div>
303         </fieldset>
304         </div>
305 </form>
306
307 <?php print_paginator($page, $tab_counts[$current_tab], $page_string_w_tab . SEP . $order .'='. $col, $results_per_page); ?>
308
309 <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="selectform">
310 <input type="hidden" name="tab" value="<?php echo $current_tab; ?>" />
311 <input type="hidden" name="course_id" value="<?php echo $course_id; ?>"/>
312 <div style="width: 95%; margin-right: auto; margin-left: auto;">
313 <ul id="navlist">
314         <?php for ($i = 0; $i< $num_tabs; $i++): ?>
315                 <?php if ($current_tab == $i): ?>
316                         <li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?tab=<?php echo $i.$page_string; ?>" class="active"><strong><?php echo _AT($tabs[$i]); ?> - <?php echo $tab_counts[$i]; ?></strong></a></li>
317                 <?php else: ?>
318                         <li><a href="<?php echo $_SERVER['PHP_SELF']; ?>?tab=<?php echo $i.$page_string; ?>"><?php echo _AT($tabs[$i]); ?> - <?php echo $tab_counts[$i]; ?></a></li>
319                 <?php endif; ?>
320         <?php endfor; ?>
321 </ul>
322 </div>
323
324 <table class="data" style="width:95%;" summary="" rules="cols">
325 <colgroup>
326         <?php if ($col == 'login'): ?>
327                 <col />
328                 <col class="sort" />
329                 <col span="4" />
330         <?php elseif($col == 'first_name'): ?>
331                 <col span="2" />
332                 <col class="sort" />
333                 <col span="3" />
334         <?php elseif($col == 'second_name'): ?>
335                 <col span="3" />
336                 <col class="sort" />
337                 <col span="2" />
338         <?php elseif($col == 'last_name'): ?>
339                 <col span="4" />
340                 <col class="sort" />
341                 <col />
342         <?php elseif($col == 'email'): ?>
343                 <col span="5" />
344                 <col class="sort" />
345         <?php endif; ?>
346 </colgroup>
347 <thead>
348 <tr>
349         <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>
350
351         <th scope="col"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $orders[$order]; ?>=login<?php echo $page_string_w_tab;?>"><?php echo _AT('login_name'); ?></a></th>
352
353         <th scope="col"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $orders[$order]; ?>=first_name<?php echo $page_string_w_tab;?>"><?php echo _AT('first_name'); ?></a></th>
354
355         <th scope="col"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $orders[$order]; ?>=second_name<?php echo $page_string_w_tab;?>"><?php echo _AT('second_name'); ?></a></th>
356
357         <th scope="col"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $orders[$order]; ?>=last_name<?php echo $page_string_w_tab;?>"><?php echo _AT('last_name'); ?></a></th>
358
359         <th scope="col"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?<?php echo $orders[$order]; ?>=email<?php echo $page_string_w_tab;?>"><?php echo _AT('email'); ?></a></th>
360 </tr>
361 </thead>
362 <tfoot>
363 <tr>
364         <td colspan="6">
365                 <?php if ($current_tab == 0): ?>
366                         <input type="submit" name="role"     value="<?php echo _AT('privileges');  ?>" /> 
367                         <input type="submit" name="unenroll" value="<?php echo _AT('remove');    ?>" /> 
368                         <input type="submit" name="alumni"   value="<?php echo _AT('mark_alumni'); ?>" />
369                 <?php elseif ($current_tab == 1): ?>
370                         <input type="submit" name="role" value="<?php echo _AT('privileges'); ?>" /> 
371                         <input type="submit" name="unenroll" value="<?php echo _AT('remove'); ?>" /> 
372
373                 <?php elseif ($current_tab == 2): ?>
374                         <input type="submit" name="enroll"   value="<?php echo _AT('enroll'); ?>" /> 
375                         <input type="submit" name="unenroll" value="<?php echo _AT('remove'); ?>" />
376                 
377                 <?php elseif ($current_tab == 3): ?>
378                         <input type="submit" name="enroll" value="<?php echo _AT('enroll'); ?>" /> 
379                         <input type="submit" name="unenroll" value="<?php echo _AT('remove'); ?>" />
380
381                 <?php elseif ($current_tab == 4): ?>
382                         <input type="submit" name="enroll"   value="<?php echo _AT('enroll'); ?>" /> 
383
384                 <?php endif; ?></td>
385 </tr>
386 </tfoot>
387 <tbody>
388 <?php if ($tab_counts[$current_tab]): ?>
389         <?php while ($row = mysql_fetch_assoc($enrollment_result)): ?>
390                 <tr onmousedown="document.selectform['m<?php echo $row['member_id']; ?>'].checked = !document.selectform['m<?php echo $row['member_id']; ?>'].checked; togglerowhighlight(this, 'm<?php echo $row['member_id']; ?>');" id="rm<?php echo $row['member_id']; ?>">
391                         <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>
392                         <td><?php echo AT_print($row['login'], 'members.login'); ?></td>
393                         <td><?php echo AT_print($row['first_name'], 'members.name'); ?></td>
394                         <td><?php echo AT_print($row['second_name'], 'members.name'); ?></td>
395                         <td><?php echo AT_print($row['last_name'], 'members.name'); ?></td>
396                         <td><?php echo AT_print($row['email'], 'members.email'); ?></td>
397                 </tr>
398         <?php endwhile; ?>
399 <?php else: ?>
400         <tr>
401                 <td colspan="6"><?php echo _AT('none_found'); ?></td>
402         </tr>
403 <?php endif; ?>
404 </tbody>
405 </table>
406 </form>
407
408 <script language="JavaScript" type="text/javascript">
409 //<!--
410 function CheckAll() {
411         for (var i=0;i<document.selectform.elements.length;i++) {
412                 var e = document.selectform.elements[i];
413                 if ((e.name == 'id[]') && (e.type=='checkbox')) {
414                         e.checked = document.selectform.selectall.checked;
415                         togglerowhighlight(document.getElementById("r" + e.id), e.id);
416                 }
417         }
418 }
419
420 function togglerowhighlight(obj, boxid) {
421         if (document.getElementById(boxid).checked) {
422                 obj.className = 'selected';
423         } else {
424                 obj.className = '';
425         }
426 }
427 //-->
428 </script>
429 <?php require(AT_INCLUDE_PATH.'footer.inc.php'); ?>