function _printResultSet($congid, $groupid)
    {
        echo '<h3>';
        if ($congid) {
            $cong = $GLOBALS['system']->getDBObject('congregation', $congid);
            $cong->printFieldValue('name');
            echo ' Congregation';
        } else {
            $group = $GLOBALS['system']->getDBObject('person_group', $groupid);
            $group->printFieldValue('name');
            echo ' Group';
        }
        echo '</h3>';
        list($dates, $attendances, $totals) = Attendance_Record_Set::getAttendances((array) $congid, $groupid, $this->age_bracket, $this->start_date, $this->end_date);
        if (empty($attendances)) {
            ?>
			<p><i>No attendance records found.  Try adjusting your criteria.</i></p>
			<?php 
            return;
        }
        $headcounts = Headcount::fetchRange($congid ? 'congregation' : 'person_group', $congid ? $congid : $groupid, $this->start_date, $this->end_date);
        $letters = array(0 => 'A', 1 => 'P', '' => '?');
        $classes = array(0 => 'absent', 1 => 'present', '' => 'unknown');
        $dummy = new Person();
        ?>
		<form method="post" action="" class="bulk-person-action">
		<table class="table table-hover table-auto-width nowrap table-bordered table-condensed">
			<thead>
				<tr>
					<th>Last Name</th>
					<th>First Name</th>
					<th><?php 
        echo $this->groupid ? 'Membership Status' : 'Status';
        ?>
</th>
				<?php 
        foreach ($dates as $date) {
            ?>
					<th><?php 
            echo date('j M', strtotime($date));
            ?>
</th>
					<?php 
        }
        ?>
					<th>Actions</th>
					<th class="narrow selector form-inline"><input type="checkbox" class="select-all" title="Select all" /></th>
				</tr>
			</thead>
			<tbody>
			<?php 
        foreach ($attendances as $personid => $record) {
            ?>
				<tr <?php 
            if ($record['status'] == 'archived') {
                echo 'class="archived"';
            }
            ?>
>
					<td><?php 
            echo ents($record['last_name']);
            ?>
</td>
					<td><?php 
            echo ents($record['first_name']);
            ?>
</td>
					<td>
						<?php 
            if ($this->groupid) {
                echo ents($record['membership_status']);
            } else {
                $dummy->printFieldValue('status', $record['status']);
            }
            ?>
					</td>
				<?php 
            foreach ($dates as $date) {
                $letter = $letters[array_get($record, $date, '')];
                $class = $classes[array_get($record, $date, '')];
                echo '<td class="' . $class . '">' . $letter . '</td>';
            }
            ?>
					<td class="narrow action-cell">
						<a class="med-popup" href="?view=persons&personid=<?php 
            echo $personid;
            ?>
"><i class="icon-user"></i>View</a> &nbsp;
					<?php 
            if ($GLOBALS['user_system']->havePerm(PERM_EDITPERSON)) {
                ?>
						<a class="med-popup" href="?view=_edit_person&personid=<?php 
                echo $personid;
                ?>
"><i class="icon-wrench"></i>Edit</a> &nbsp;
						<?php 
            }
            if ($GLOBALS['user_system']->havePerm(PERM_EDITNOTE)) {
                ?>
						<a class="med-popup" href="?view=_add_note_to_person&personid=<?php 
                echo $personid;
                ?>
"><i class="icon-pencil"></i>Add Note</a>
						<?php 
            }
            ?>
					</td>
					<td class="selector"><input name="personid[]" type="checkbox" value="<?php 
            echo $personid;
            ?>
" /></td>
				</tr>
				<?php 
        }
        ?>
			</tbody>
			<tfoot class="attendance-stats">
				<tr class="headcount">
					<th colspan="3">Total Headcount</th>
				<?php 
        foreach ($dates as $date) {
            ?>
					<td>
						<?php 
            echo array_get($headcounts, $date);
            ?>
					</td>
					<?php 
        }
        ?>
					<td colspan="2">&nbsp;</td>
				</tr>
				<tr class="present">
					<th colspan="3">Total Present</th>
				<?php 
        foreach ($dates as $date) {
            ?>
					<td>
						<?php 
            echo array_get($totals[$date], 1, 0);
            ?>
					</td>
					<?php 
        }
        ?>
					<td colspan="2">&nbsp;</td>
				</tr>
				<tr class="absent">
					<th colspan="3">Total Absent</th>
				<?php 
        foreach ($dates as $date) {
            ?>
					<td>
						<?php 
            echo array_get($totals[$date], 0, 0);
            ?>
					</td>
					<?php 
        }
        ?>
					<td colspan="2">&nbsp;</td>
				</tr>
				<tr class="extras">
					<th colspan="3">Extras</th>
				<?php 
        foreach ($dates as $date) {
            ?>
					<td>
						<?php 
            if (isset($headcounts[$date])) {
                echo $headcounts[$date] - array_get($totals[$date], 1, 0);
            }
            ?>
					</td>
					<?php 
        }
        ?>
					<td colspan="2">&nbsp;</td>
				</tr>
			</tfoot>
		</table>
		<?php 
        include 'templates/bulk_actions.template.php';
        ?>
		</form>
		<?php 
    }
 function processView()
 {
     if (isset($_REQUEST['cohortids'])) {
         foreach ($_REQUEST['cohortids'] as $id) {
             if ($id) {
                 $this->_cohortids[] = $id;
             }
         }
         $_SESSION['attendance']['cohortids'] = $this->_cohortids;
     }
     $this->_attendance_date = process_widget('attendance_date', array('type' => 'date'));
     if (empty($this->_attendance_date)) {
         // Default to last Sunday, unless today is Sunday
         $default_day = defined('ATTENDANCE_DEFAULT_DAY') ? ATTENDANCE_DEFAULT_DAY : 'Sunday';
         $this->_attendance_date = date('Y-m-d', date('l') == $default_day ? time() : strtotime('last ' . $default_day));
     }
     if (empty($_REQUEST['params_submitted']) && empty($_REQUEST['attendances_submitted'])) {
         if (!empty($_SESSION['attendance'])) {
             $this->_age_brackets = array_get($_SESSION['attendance'], 'age_brackets');
             $this->_statuses = array_get($_SESSION['attendance'], 'statuses');
             $this->_cohortids = array_get($_SESSION['attendance'], 'cohortids');
             $this->_show_photos = array_get($_SESSION['attendance'], 'show_photos', FALSE);
             $this->_parallel_mode = array_get($_SESSION['attendance'], 'parallel_mode', FALSE);
         }
     }
     if (!empty($_REQUEST['params_submitted']) || !empty($_REQUEST['attendances_submitted'])) {
         if (!empty($_REQUEST['age_brackets_all'])) {
             unset($_REQUEST['age_brackets']);
         }
         if (!empty($_REQUEST['statuses_all'])) {
             unset($_REQUEST['statuses']);
         }
         $this->_age_brackets = $_SESSION['attendance']['age_brackets'] = array_get($_REQUEST, 'age_brackets');
         $this->_statuses = $_SESSION['attendance']['statuses'] = array_get($_REQUEST, 'statuses');
         $this->_show_photos = $_SESSION['attendance']['show_photos'] = array_get($_REQUEST, 'show_photos', FALSE);
         $this->_parallel_mode = $_SESSION['attendance']['parallel_mode'] = array_get($_REQUEST, 'parallel_mode', FALSE);
     }
     foreach ($this->_cohortids as $id) {
         $this->_record_sets[$id] = new Attendance_Record_set($this->_attendance_date, $id, $this->_age_brackets, $this->_statuses);
         if ($this->_show_photos) {
             $this->_record_sets[$id]->show_photos = TRUE;
         }
     }
     if (!empty($_REQUEST['release'])) {
         foreach ($this->_record_sets as $set) {
             $set->releaseLock();
         }
     } else {
         if (!empty($_REQUEST['params_submitted'])) {
             foreach ($this->_record_sets as $cohortid => $set) {
                 if (!$set->checkAllowedDate()) {
                     add_message(_('"Attendance for "') . $set->getCohortName() . _('" cannot be recorded on a "') . date('l', strtotime($this->_attendance_date)), 'error');
                     unset($this->_record_sets[$cohortid]);
                     $this->_cohortids = array_diff($this->_cohortids, array($cohortid));
                     continue;
                 }
                 if (!$set->acquireLock()) {
                     add_message(_('"Another user is currently recording attendance for "') . $set->getCohortName() . _('".  Please wait until they finish then try again."'), 'error');
                     unset($this->_record_sets[$cohortid]);
                     $this->_cohortids = array_diff($this->_cohortids, array($cohortid));
                 }
             }
         }
     }
     if (!empty($_REQUEST['attendances_submitted'])) {
         // Process step 2
         if ($_SESSION['enter_attendance_token'] == $_REQUEST['enter_attendance_token']) {
             // Clear the token from the session on disk
             $_SESSION['enter_attendance_token'] = NULL;
             session_write_close();
             session_start();
             // Process the form
             foreach ($this->_record_sets as $i => $set) {
                 if (!$set->haveLock() && !$set->acquireLock()) {
                     add_message("Unfortunately your lock on '" . $set->getCohortName() . "' has expired and been acquired by another user.  Please wait until they finish and try again.", 'error');
                 } else {
                     if ($set->processForm($i)) {
                         $set->save();
                         if ((int) $set->congregationid) {
                             Headcount::save('congregation', $this->_attendance_date, $set->congregationid, $_REQUEST['headcount']['congregation'][$set->congregationid]);
                         } else {
                             Headcount::save('person_group', $this->_attendance_date, $set->groupid, $_REQUEST['headcount']['group'][$set->groupid]);
                         }
                         $set->releaseLock();
                     }
                 }
             }
         } else {
             trigger_error('Could not save attendances - synchronizer token does not match.  This probably means the request was duplicated somewhere along the line.  If you see your changes below, they have been saved by the other request');
             sleep(3);
             // Give the other one time to finish before we load again
             // Pretend we are back in step 2
             $_POST['attendances_submitted'] = FALSE;
             $_SESSION['enter_attendance_token'] = md5(time());
         }
     }
 }
    public function printStats()
    {
        list($totals, $breakdowns) = $this->getStats();
        if ((int) $this->congregationid) {
            $headcount = Headcount::fetch('congregation', $this->date, $this->congregationid);
        } else {
            $headcount = Headcount::fetch('person_group', $this->date, $this->groupid);
        }
        ?>
		<table class="table valign-middle attendance-stats table-bordered" style="width: 40ex">
		<?php 
        if ($headcount) {
            ?>
			<tr class="headcount">
				<th><?php 
            echo _('Total Headcount');
            ?>
</th>
				<td colspan="3">
					<b>
					<?php 
            echo $headcount;
            ?>
					</b>
				</td>
			</tr>
			<?php 
        }
        foreach (array(1 => 'Present', 0 => 'Absent') as $present => $label) {
            ?>
			<tr class="<?php 
            echo strtolower($label);
            ?>
">
				<th rowspan="<?php 
            echo count($breakdowns[$present]);
            ?>
">Marked <?php 
            echo $label;
            ?>
</th>
				<td rowspan="<?php 
            echo count($breakdowns[$present]);
            ?>
">
					<b><?php 
            echo $totals[$present];
            ?>
</b>
				</td>
			<?php 
            if (!empty($breakdowns[$present])) {
                ?>
				<td><?php 
                echo $breakdowns[$present][0]['status'];
                ?>
</td>
				<td><?php 
                echo $breakdowns[$present][0]['total'];
                ?>
</td>
				<?php 
            } else {
                ?>
				<td>&nbsp;</td>
				<td>&nbsp;</td>
				<?php 
            }
            ?>
			</tr>
			<?php 
            for ($i = 1; $i < count($breakdowns[$present]); $i++) {
                ?>
				<tr class="<?php 
                echo strtolower($label);
                ?>
">
					<td><?php 
                echo $breakdowns[$present][$i]['status'];
                ?>
</td>
					<td><?php 
                echo $breakdowns[$present][$i]['total'];
                ?>
</td>
				</tr>
				<?php 
            }
        }
        if ($headcount) {
            ?>
			<tr class="extras">
				<th>Extras</th>
				<td colspan="3"><b><?php 
            echo $headcount - $totals[1];
            ?>
</b></td>
			</tr>
			<?php 
        }
        ?>
		</table>
		<?php 
    }
    private function printSet($cohortid, $cohortname)
    {
        $stats = Attendance_Record_Set::getStatsForPeriod($this->_start_date, $this->_end_date, $cohortid);
        if (empty($stats) || $stats[NULL]['rate'] == 0) {
            return FALSE;
        }
        ?>
		<div class="span4">
		<table class="table table-bordered attendance-stats">
			<thead>
				<tr>
					<th colspan="4"><?php 
        echo ents($cohortname);
        ?>
</th>
				</tr>
				<tr>
					<th><?php 
        echo _('Segment');
        ?>
</th>
					<th title=<?php 
        echo _('"Percentage of dates marked present rather than absent"');
        ?>
><?php 
        echo _('Rate');
        ?>
</th>
					<th class="present" title=<?php 
        echo _('"Average number marked present per date"');
        ?>
><?php 
        echo _('Avg&nbsp;P');
        ?>
</th>
					<th class="absent" title="<?php 
        echo _('Average number marked absent per date"');
        ?>
><?php 
        echo _('Avg&nbsp;A');
        ?>
</th>
			</thead>
			<tbody>
		<?php 
        $map['age_bracket'] = explode(',', AGE_BRACKET_OPTIONS);
        if ($cohortid[0] == 'g') {
            list($map['status'], $default) = Person_Group::getMembershipStatusOptionsAndDefault();
        } else {
            $map['status'] = $this->status_map;
        }
        foreach (array('status', 'age_bracket') as $grouping) {
            foreach ($map[$grouping] as $k => $v) {
                if (!isset($stats[$grouping][$k])) {
                    continue;
                }
                ?>
				<tr <?php 
                if ($k == 0 && $grouping == 'age_bracket') {
                    echo 'class="thick-top-border"';
                }
                ?>
>
					<th><?php 
                echo ents($v);
                ?>
</th>
				<?php 
                if (isset($stats[$grouping][$k])) {
                    ?>
					<td><?php 
                    echo $stats[$grouping][$k]['rate'];
                    ?>
%</td>
					<td><?php 
                    echo number_format($stats[$grouping][$k]['avg_present'], 1);
                    ?>
</td>
					<td><?php 
                    echo number_format($stats[$grouping][$k]['avg_absent'], 1);
                    ?>
</td>
					<?php 
                } else {
                    ?>
					<td>-</td>
					<td>-</td>
					<td>-</td>
					<?php 
                }
                ?>
				</tr>
				<?php 
            }
        }
        ?>
				<tr class="thick-top-border">
					<th><?php 
        echo _('Overall');
        ?>
</th>
					<td><?php 
        echo $stats[NULL]['rate'];
        ?>
%</td>
					<td><?php 
        echo number_format($stats[NULL]['avg_present'], 1);
        ?>
</td>
					<td><?php 
        echo number_format($stats[NULL]['avg_absent'], 1);
        ?>
</td>
				</tr>
				<tr class="headcount">
					<th colspan="2">
						<?php 
        echo _('Avg&nbsp;Headcount');
        ?>
					</th>
					<td class="right">
						<?php 
        $bits = explode('-', $cohortid);
        $hc = Headcount::fetchAverage($bits[0], $bits[1], $this->_start_date, $this->_end_date);
        if ($hc) {
            echo number_format($hc, 1);
        } else {
            echo 'N/A';
        }
        ?>
					</td>
					<td colspan="2"></td>
				</tr>
			</tbody>
		</table>
		</div>
		<?php 
        return TRUE;
    }
    private function _printResultsTabular()
    {
        $GLOBALS['system']->includeDBClass('attendance_record_set');
        $GLOBALS['system']->includeDBClass('person');
        $dummy = new Person();
        $all_persons = Attendance_Record_Set::getPersonIDsForCohorts($this->cohortids);
        $all_dates = $all_attendances = $all_totals = $all_headcounts = array();
        if (!empty($this->cohortids)) {
            foreach ($this->cohortids as $cohortid) {
                $congid = $groupid = NULL;
                list($type, $id) = explode('-', $cohortid);
                if ($type == 'c') {
                    $congid = $id;
                }
                if ($type == 'g') {
                    $groupid = $id;
                }
                list($cdates, $cattendances, $ctotals) = Attendance_Record_Set::getAttendances((array) $congid, $groupid, $this->age_bracket, $this->start_date, $this->end_date);
                $hc = Headcount::fetchRange($congid ? 'congregation' : 'person_group', $congid ? $congid : $groupid, $this->start_date, $this->end_date);
                foreach ($hc as $date => $c) {
                    $all_headcounts[$date][$cohortid] = $c;
                }
                $all_dates = array_merge($all_dates, $cdates);
                foreach ($ctotals as $date => $t) {
                    $all_totals[$date][$cohortid] = $t;
                }
                foreach ($cattendances as $personid => $cat) {
                    $all_persons[$personid]['cohortids'][] = $cohortid;
                    foreach ($cat as $k => $v) {
                        if (!in_array($k, array('first_name', 'last_name', 'membership_status', 'status'))) {
                            $all_attendances[$personid][$cohortid][$k] = $v;
                        }
                    }
                }
            }
        }
        $all_dates = array_unique($all_dates);
        ?>
		<table class="table table-hover table-condensed table-auto-width valign-middle table-bordered parallel-attendance-report">
			<thead>
				<tr>
					<th <?php 
        if ($this->format != 'totals') {
            echo 'rowspan="2"';
        }
        ?>
>Name</th>
				<?php 
        if (SizeDetector::isWide()) {
            ?>
					<th <?php 
            if ($this->format != 'totals') {
                echo 'rowspan="2"';
            }
            ?>
>Status</th>
					<?php 
        }
        if ($this->format == 'totals') {
            $colspan = 1;
        } else {
            $colspan = count($this->cohortids);
        }
        foreach ($all_dates as $date) {
            $classes = 'center nowrap';
            if ($this->format != 'totals') {
                $classes .= ' new-cohort';
            }
            ?>
					<th class="<?php 
            echo $classes;
            ?>
" colspan="<?php 
            echo $colspan;
            ?>
"><?php 
            echo format_date($date);
            ?>
</th>
					<?php 
        }
        ?>
					<th <?php 
        if ($this->format != 'totals') {
            echo 'rowspan="2"';
        }
        ?>
></th>
					<th class="narrow selector form-inline" rowspan="2"><input type="checkbox" class="select-all" title="Select all" /></th>					
				</tr>

			<?php 
        if ($this->format != 'totals') {
            ?>
				<tr>
				<?php 
            foreach ($all_dates as $date) {
                $first = TRUE;
                foreach ($this->cohortids as $cohortid) {
                    $name = '';
                    list($type, $id) = explode('-', $cohortid);
                    if ($type == 'c') {
                        $congregation = $GLOBALS['system']->getDBObject('congregation', $id);
                        $name = $congregation->getValue('name');
                    } else {
                        if ($type == 'g') {
                            $group =& $GLOBALS['system']->getDBObject('person_group', $id);
                            $name = $group->getValue('name');
                        }
                    }
                    $short = reset(explode(' ', $name));
                    if (strlen($short) > 5 && !preg_match('/[0-9]/', $short)) {
                        $short = substr($short, 0, 3) . '…';
                    }
                    $class = $first ? 'new-cohort' : '';
                    ?>
						<th class="nowrap <?php 
                    echo $class;
                    ?>
" title="<?php 
                    echo ents($name);
                    ?>
"><?php 
                    echo ents($short);
                    ?>
</th>
						<?php 
                    $first = FALSE;
                }
            }
            ?>
				</tr>
				<?php 
        }
        ?>
			</thead>
			<tbody>
			<?php 
        foreach ($all_persons as $personid => $details) {
            if (!isset($all_attendances[$personid])) {
                continue;
            }
            ?>
				<tr <?php 
            if ($details['status'] == 'archived') {
                echo 'class="archived"';
            }
            ?>
>
					<td class="nowrap">
						<?php 
            echo ents($details['first_name'] . ' ' . $details['last_name']);
            ?>
					</td>
				<?php 
            if (SizeDetector::isWide()) {
                ?>
					<td><?php 
                $dummy->printFieldValue('status', $details['status']);
                ?>
</th>
					<?php 
            }
            foreach ($all_dates as $date) {
                $first = TRUE;
                if ($this->format == 'totals') {
                    $score = '';
                    foreach ($this->cohortids as $cohortid) {
                        $catt = array_get($all_attendances[$personid], $cohortid, array());
                        $x = array_get($catt, $date, '');
                        if (strlen($x)) {
                            $score = (int) $score + $x;
                        }
                    }
                    $class = $this->classes[$score > 0 ? 1 : $score];
                    if ($score === '') {
                        $score = '?';
                    }
                    echo '<td class="center ' . $class . '">' . $score . '</td>';
                } else {
                    foreach ($this->cohortids as $cohortid) {
                        if (!in_array($cohortid, array_get($all_persons[$personid], 'cohortids', array()))) {
                            $class = 'disabled';
                            $letter = '';
                        } else {
                            $catt = array_get($all_attendances[$personid], $cohortid, array());
                            $v = array_get($catt, $date, '');
                            $letter = $this->letters[$v];
                            $class = $this->classes[$v];
                        }
                        if ($first) {
                            $class .= ' new-cohort';
                        }
                        echo '<td class="' . $class . '">' . $letter . '</td>';
                        $first = FALSE;
                    }
                }
            }
            $this->_printActionsAndSelector($personid);
            ?>
	
				</tr>
				<?php 
        }
        ?>
			</tbody>
			<?php 
        if ($this->format != 'totals') {
            // headcounts don't make sense when we collapse groups down into totals
            $colspan = SizeDetector::isWide() ? 'colspan="2"' : '';
            ?>
			<tfoot class="attendance-stats">
				<tr class="headcount">
					<th <?php 
            echo $colspan;
            ?>
>Total Headcount</th>
				<?php 
            foreach ($all_dates as $date) {
                $hc = array_get($all_headcounts, $date, array());
                foreach ($this->cohortids as $cohortid) {
                    ?>
						<td>
							<?php 
                    echo array_get($hc, $cohortid, '');
                    ?>
						</td>
						<?php 
                }
            }
            ?>
					<td colspan="2">&nbsp;</td>
				</tr>
				<tr class="present">
					<th <?php 
            echo $colspan;
            ?>
>Total Present</th>
				<?php 
            foreach ($all_dates as $date) {
                $tots = array_get($all_totals, $date, array());
                foreach ($this->cohortids as $cohortid) {
                    ?>
						<td>
							<?php 
                    echo array_get(array_get($tots, $cohortid, array()), 1, 0);
                    ?>
						</td>
						<?php 
                }
            }
            ?>
					<td colspan="2">&nbsp;</td>
				</tr>
				<tr class="absent">
					<th <?php 
            echo $colspan;
            ?>
>Total Absent</th>
				<?php 
            foreach ($all_dates as $date) {
                $tots = array_get($all_totals, $date, array());
                foreach ($this->cohortids as $cohortid) {
                    ?>
						<td>
							<?php 
                    echo array_get(array_get($tots, $cohortid, array()), 0, 0);
                    ?>
						</td>
						<?php 
                }
            }
            ?>
					<td colspan="2">&nbsp;</td>
				</tr>
				<tr class="extras">
					<th <?php 
            echo $colspan;
            ?>
>Extras</th>
				<?php 
            foreach ($all_dates as $date) {
                $tots = array_get($all_totals, $date, array());
                $hc = array_get($all_headcounts, $date, array());
                foreach ($this->cohortids as $cohortid) {
                    $present = array_get(array_get($tots, $cohortid, array()), 1, 0);
                    $absent = array_get(array_get($tots, $cohortid, array()), 0, 0);
                    $headcount = array_get($hc, $cohortid, NULL);
                    ?>
						<td>
							<?php 
                    if ($headcount) {
                        echo $headcount - $present - $absent;
                    }
                    ?>
						</td>
						<?php 
                }
            }
            ?>
					<td colspan="2">&nbsp;</td>
				</tr>
			</tfoot>
			<?php 
        }
        ?>
		</table>
		<?php 
        include 'templates/bulk_actions.template.php';
    }
    function printView()
    {
        if (empty($this->_record_sets)) {
            // STEP 1 - choose congregation and date
            ?>
			<form method="get" class="well well-small clearfix">
				<input type="hidden" name="view" value="<?php 
            echo $_REQUEST['view'];
            ?>
" />
				<table class="attendance-config-table valign-middle">
					<tr>
						<th>For</th>
						<td colspan="2" class="fill-me">
							<?php 
            print_widget('age_bracket', array('type' => 'select', 'options' => array('' => 'All age brackets') + explode(',', AGE_BRACKET_OPTIONS), 'default' => '', 'allow_empty' => false), $this->_age_bracket);
            ?>
						</td>
					</tr>
					<tr>
						<th rowspan="2">In</th>
						<td class="valign-top">
							<label class="radio">
								<input type="radio" name="for_type" 
									value="congregationid" id="for_type_congregation" 
									data-toggle="enable" data-target="#congregationchooser select"
									<?php 
            if (empty($this->_groupid)) {
                echo 'checked="checked"';
            }
            ?>
								>
								Congregation(s) &nbsp;
							</label>
						</td>
						<td class="fill-me">
							<table id="congregationchooser" class="expandable table-condensed no-padding" cellspacing="0">
						<?php 
            $congs = $this->_congregationids ? $this->_congregationids : array(0);
            foreach ($congs as $congid) {
                ?>
							<tr>
								<td>
								<?php 
                print_widget('congregationid[]', array('type' => 'reference', 'references' => 'congregation', 'order_by' => 'name', 'allow_empty' => true, 'empty_text' => '-- Choose --'), $congid);
                ?>
								</td>
							</tr>
							<?php 
            }
            ?>
							</table>
						</td>
					</tr>
					<tr>
						<td>
							<label class="radio">
								<input type="radio" name="for_type" 
									value="groupid" id="for_type_group"
									data-toggle="enable" data-target="#groupchooser select" 
									<?php 
            if (!empty($this->_groupid)) {
                echo 'checked="checked"';
            }
            ?>
								>
								Group
							</label>
						</td>
						<td class="fill-me" id="groupchooser">
							<?php 
            print_widget('groupid', array('type' => 'reference', 'references' => 'person_group', 'filter' => array('can_record_attendance' => '1', 'is_archived' => 0)), $this->_groupid);
            ?>
						</td>
					</tr>
					<tr>
						<th>On</th>
						<td colspan="2">
							<?php 
            print_widget('attendance_date', array('type' => 'date'), $this->_attendance_date);
            ?>
						</td>
					</tr>
				<?php 
            if ($GLOBALS['system']->featureEnabled('PHOTOS')) {
                ?>
					<tr>
						<th></th>
						<td>
							<label class="checkbox">
								<input type="checkbox" name="show_photos" value="1" <?php 
                if ($this->_show_photos) {
                    echo 'checked="checked"';
                }
                ?>
 />
								Show photos
							</label>
						</td>
					</tr>
					<?php 
            }
            ?>
				</table>
				<button type="submit" class="btn attendance-config-submit">Continue <i class="icon-chevron-right"></i></button>
				<input type="hidden" name="params_submitted" value="1" />
			</form>
			<?php 
        } else {
            if (empty($_POST['attendances_submitted'])) {
                $_SESSION['enter_attendance_token'] = md5(time());
                // STEP 2 - enter attendances
                ob_start();
                ?>
			<form method="post" class="attendance warn-unsaved" action="?view=attendance__record">
				<input type="hidden" name="for_type" value="<?php 
                echo ents($_REQUEST['for_type']);
                ?>
" />
				<input type="hidden" name="attendance_date" value="<?php 
                echo $this->_attendance_date;
                ?>
" />
				<input type="hidden" name="age_bracket" value="<?php 
                echo $this->_age_bracket;
                ?>
" />
				<input type="hidden" name="show_photos" value="<?php 
                echo $this->_show_photos;
                ?>
" />
				<input type="hidden" name="enter_attendance_token" value="<?php 
                echo $_SESSION['enter_attendance_token'];
                ?>
" />
				<input type="hidden" name="attendances_submitted" value="1" />

				<p class="visible-desktop smallprint">For greatest speed, press P for present and A for absent.  The cursor will automatically progress to the next person.  To go back, use the arrow keys.</p>
				<?php 
                $totalPrinted = 0;
                foreach ($this->_record_sets as $i => $set) {
                    if ((int) $set->congregationid) {
                        ?>
						<input type="hidden" name="congregationid[]" value="<?php 
                        echo $set->congregationid;
                        ?>
" />
						<?php 
                        $congregation = $GLOBALS['system']->getDBObject('congregation', (int) $set->congregationid);
                        $title = $congregation->getValue('name') . ' Congregation';
                    } else {
                        if ($set->groupid) {
                            $group =& $GLOBALS['system']->getDBObject('person_group', $set->groupid);
                            $title = $group->getValue('name') . ' Group';
                            ?>
						<input type="hidden" name="groupid" value="<?php 
                            echo $set->groupid;
                            ?>
" />
						<?php 
                        } else {
                            return;
                        }
                    }
                    if (strlen($set->age_bracket)) {
                        $GLOBALS['system']->includeDBClass('person');
                        $p = new Person();
                        $p->setValue('age_bracket', $set->age_bracket);
                        $title = $p->getFormattedValue('age_bracket') . ' in ' . $title;
                    }
                    ?>
					<h3><?php 
                    echo ents($title);
                    ?>
</h3>
					<div class="width-really-auto form-inline">
						<?php 
                    $setPrinted = $set->printForm($i);
                    if ($setPrinted > 0) {
                        $totalPrinted += $setPrinted;
                        if ((int) $set->congregationid) {
                            $headcountFieldName = 'headcount[congregation][' . $set->congregationid . ']';
                            $headcountValue = Headcount::fetch('congregation', $this->_attendance_date, $set->congregationid);
                        } else {
                            $headcountFieldName = 'headcount[group][' . $set->groupid . ']';
                            $headcountValue = Headcount::fetch('person_group', $this->_attendance_date, $set->groupid);
                        }
                        ?>
							<div class="container row-fluid control-group">
								<p class="span6">
									Total headcount:
									<input type="text" class="int-box" name="<?php 
                        echo $headcountFieldName;
                        ?>
" value="<?php 
                        echo $headcountValue;
                        ?>
" size="5" />
								</p>
								<p class="span6 align-right nowrap">
									<input type="submit" class="btn" value="Save All Attendances" />
									<a href="?view=attendance__record" class="btn">Cancel</a>
								</p>
							</div>
							<?php 
                    } else {
                        ?>
							<i>(No persons in this listing)</i>
							<?php 
                    }
                    ?>
					</div>
					<?php 
                }
                ?>
			</form>
			<?php 
                if (ini_get('max_input_vars') && $totalPrinted > ini_get('max_input_vars')) {
                    ob_end_clean();
                    print_message("The parameters you have selected will list more persons " . "than your server can process.  Please narrow down your parameters, " . "or ask your server administrator to increase the PHP max_input_vars setting" . " (currently " . ini_get('max_input_vars') . ')', 'error');
                } else {
                    ob_flush();
                }
            } else {
                // STEP 3 - confirmation
                foreach ($this->_record_sets as $set) {
                    if ($set->congregationid) {
                        $congregation = $GLOBALS['system']->getDBObject('congregation', (int) $set->congregationid);
                        $title = $congregation->getValue('name') . ' congregation';
                    } else {
                        $group =& $GLOBALS['system']->getDBObject('person_group', $set->groupid);
                        $title = $group->getValue('name') . ' group';
                    }
                    echo '<h3>' . $title . '</h3>';
                    $set->printStats();
                }
                ?>
			<p><a href="?view=<?php 
                echo $_REQUEST['view'];
                ?>
"><i class="icon-pencil"></i>Record more attendances</a></p>
			<p><a href="?view=persons__reports"><i class="icon-list"></i>Analyse attendance using a person report</a></p>
			<?php 
            }
        }
    }
    private function printSet($cohortid, $cohortname)
    {
        $stats = Attendance_Record_Set::getStatsForPeriod($this->_start_date, $this->_end_date, $cohortid);
        if (empty($stats)) {
            return FALSE;
        }
        ?>
		<div class="span4">
		<table class="table table-bordered attendance-stats">
			<thead>
				<tr>
					<th colspan="4"><?php 
        echo ents($cohortname);
        ?>
</th>
				</tr>
				<tr>
					<th>Status</th>
					<th title="Percentage of dates marked present rather than absent">Rate</th>
					<th class="present" title="Average number marked present per date">Avg&nbsp;P</th>
					<th class="absent" title="Average number marked absent per date">Avg&nbsp;A</th>
			</thead>
			<tbody>
		<?php 
        if ($cohortid[0] == 'g') {
            list($map, $default) = Person_Group::getMembershipStatusOptionsAndDefault();
        } else {
            $map = $this->status_map;
        }
        foreach ($map as $k => $v) {
            if (isset($stats[$k])) {
                ?>
				<tr>
					<th><?php 
                echo ents($v);
                ?>
</th>
					<td style="width: 6ex; text-align: right"><?php 
                echo $stats[$k]['rate'];
                ?>
%</td>
					<td style="width: 6ex; text-align: right"><?php 
                echo number_format($stats[$k]['avg_present'], 1);
                ?>
</td>
					<td style="width: 6ex; text-align: right"><?php 
                echo number_format($stats[$k]['avg_absent'], 1);
                ?>
</td>
				</tr>
				<?php 
            }
        }
        ?>
				<tr class="thick-top-border">
					<th>Overall</th>
					<td style="width: 6ex; text-align: right"><?php 
        echo $stats[NULL]['rate'];
        ?>
%</td>
					<td style="width: 6ex; text-align: right"><?php 
        echo number_format($stats[NULL]['avg_present'], 1);
        ?>
</td>
					<td style="width: 6ex; text-align: right"><?php 
        echo number_format($stats[NULL]['avg_absent'], 1);
        ?>
</td>
				</tr>
				<tr class="headcount">
					<th colspan="2">
						Avg&nbsp;Headcount
					</th>
					<td class="right">
						<?php 
        $bits = explode('-', $cohortid);
        $hc = Headcount::fetchAverage($bits[0], $bits[1], $this->_start_date, $this->_end_date);
        if ($hc) {
            echo number_format($hc, 1);
        } else {
            echo 'N/A';
        }
        ?>
					</td>
					<td colspan="2"></td>
				</tr>
			</tbody>
		</table>
		</div>
		<?php 
        return TRUE;
    }