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