//setup the array holding the header texts
$headers = array();
$headers[] = '';
$headers[] = '';
$headers[] = '';
$headers[] = '';
$headers[] = '';
$headers[] = '';
$headers[] = '';
$headers[] = '';
//pass the columns to the table
$flextable->define_columns($columns);
//pass the headers to the table
$flextable->define_headers($headers);
//tell the table it is not sortable
$flextable->sortable(false);
//set the attributes of the table
$flextable->set_attribute('id', 'reportfields-table');
$flextable->set_attribute('cellspacing', '0');
$flextable->set_attribute('class', 'reportfieldstable flexible boxaligncenter generaltable');
$flextable->set_attribute('summary', get_string('reportfields', 'block_ilp'));
$flextable->column_class('label', 'leftalign');
// setup the table - now we can use it
$flextable->setup();
//get the data on fields to be used in the table
$reports = $dbc->get_reports_table($flextable);
$totalreportfields = count($reports);
if (!empty($reports)) {
    foreach ($reports as $row) {
        $data = array();
        $data[] = $row->name;
 /**
  *
  * This function creates the register grid that is displayed
  * @param recordset $data recordset containing student register data
  * @param int          $term the term that will be displayed if not supplied all terms are shown
  * @param unknown_type $course
  */
 function term_attendance($data, $term = 0, $course = false)
 {
     global $CFG, $USER;
     $cidfield = get_config('block_ilp', 'mis_plugin_register_courseid');
     $cdatefield = get_config('block_ilp', 'mis_plugin_register_datetime');
     $markfield = get_config('block_ilp', 'mis_plugin_register_mark');
     $timefield = get_config('block_ilp', 'mis_plugin_register_starttime');
     $timefield = !empty($timefield) ? $timefield : 0;
     $cnamefield = get_config('block_ilp', 'mis_plugin_register_coursename');
     $startdate = $this->terms[0]['startts'];
     if (!empty($term)) {
         $yearstart = $this->terms[0]['start'];
         $termstart = $this->terms[$term - 1]['start'];
         $termend = $this->terms[$term - 1]['end'];
     } else {
         $yearstart = $this->terms[0]['start'];
         $termstart = $this->terms[0]['start'];
         $termend = $this->terms[$this->numterms - 1]['end'];
     }
     //these variables define the academic weeks of $termstart and $termend
     $academicstart = $this->academic_week($termstart, $yearstart);
     $academicend = $this->academic_week($termend, $yearstart);
     $weekofseconds = 604800;
     //assign the tables column names
     $tablecolumns = array('class', 'late', 'att', 'date', 'time');
     //set the displayed headers
     $tableheaders = array(get_string('ilp_mis_attendance_plugin_register_disp_class', 'block_ilp'), get_string('ilp_mis_attendance_plugin_register_disp_late', 'block_ilp'), get_string('ilp_mis_attendance_plugin_register_disp_att', 'block_ilp'), get_string('ilp_mis_attendance_plugin_register_disp_day', 'block_ilp'), get_string('ilp_mis_attendance_plugin_register_disp_time', 'block_ilp'));
     //assign the week column names and set the week display header
     for ($z = $academicstart; $z < $academicend + 1; $z++) {
         $tablecolumns[] = 'week' . $z;
         $tableheaders[] = $z;
     }
     require_once $CFG->dirroot . '/blocks/ilp/classes/tables/ilp_tablelib.class.php';
     $table = new ilp_flexible_table('user-attendence', true, 'ilp_mis_attendance_plugin_register');
     $table->define_columns($tablecolumns);
     $table->define_headers($tableheaders);
     $table->define_baseurl($CFG->wwwroot . '/blocks/ilp/attendence.php?&id=1');
     $table->sortable(false);
     $table->collapsible(false);
     $table->initialbars(false);
     $table->set_attribute('cellpadding', '6');
     $table->set_attribute('id', 'ilp-attendance-grid');
     $table->set_attribute('class', 'generalbox');
     $table->set_attribute('width', '100%');
     $table->set_attribute('align', 'left');
     $table->set_attribute('font-size', '0.85em');
     $table->setup();
     //set the row displaying the week dates
     $dates = array('', '', '', '', '<div class="termweeks">Week:</div>');
     for ($z = $academicstart; $z < $academicend + 1; $z++) {
         $weekindays = ($z - 1) * 7;
         $timestampweek = strtotime("+{$weekindays} days", $startdate);
         //$timestampweek = $weekofseconds * ($z - 1);
         //$dates[] = '<div class="termweeks">' . $this->getMonday($startdate + $timestampweek) . '</div>';
         $dates[] = '<div class="termweeks">' . $this->getMonday($timestampweek) . '</div>';
     }
     $table->add_data($dates);
     $registers = array();
     //we will now take the attendance data and construct an array separating class on day and time basis
     foreach ($data as $att) {
         $tempday = $this->courseday($att[$cdatefield]);
         $temptime = isset($att[$timefield]) ? $this->coursetime($att[$timefield]) : 'N/A';
         $registers[$att[$cidfield]][$tempday][$temptime][] = $att;
     }
     $total = array();
     foreach (array_keys($registers) as $groupKey) {
         $class = array('', '', '', '', '');
         $termsweeks = array();
         for ($z = $academicstart; $z < $academicend + 1; $z++) {
             $base_class[] = '';
             $base_termsweeks[] = $z;
         }
         $class = $base_class;
         $termsweeks = $base_termsweeks;
         $total[$groupKey] = array();
         foreach ($registers[$groupKey] as $cday => $timeslot) {
             foreach ($timeslot as $ctime => $classtime) {
                 $total[$groupKey][$cday][$ctime] = array(array(0, 0, 0, 0), array(0, 0, 0, 0), array(0, 0, 0, 0), array(0, 0, 0, 0));
                 foreach ($classtime as $item) {
                     //$item['Week_No'] = $this->weekno($item[$cdatefield]);
                     $marktimestamp = strtotime($this->normalise_date($item[$cdatefield]));
                     $item['Week_No'] = $this->academic_week(date('W', $marktimestamp), $yearstart);
                     if (in_array($item['Week_No'], $termsweeks)) {
                         if (!in_array($item[$markfield], $this->noclasscodes) && strlen($mark[$markfield]) > 0) {
                             $total[$groupKey][$cday][$ctime][0][0]++;
                         }
                         if (in_array($item[$markfield], $this->presentcodes)) {
                             $total[$groupKey][$cday][$ctime][1][0]++;
                         }
                         if (in_array($item[$markfield], $this->absentcodes)) {
                             $total[$groupKey][$cday][$ctime][2][0]++;
                         }
                         if (in_array($item[$markfield], $this->latecodes)) {
                             $total[$groupKey][$cday][$ctime][3][0]++;
                         }
                         $class['class'] = $item[$cidfield] . ': ' . $item[$cnamefield];
                         $startdate = explode('-', $item[$cdatefield]);
                         $class['date'] = $this->courseday($item[$cdatefield]);
                         $class['time'] = $ctime;
                         $att_class = 'amber';
                         if (in_array($item[$markfield], $this->presentcodes)) {
                             $att_class = get_config('block_ilp', 'mis_plugin_register_presentcolour');
                         }
                         if (in_array($item[$markfield], $this->absentcodes)) {
                             $att_class = get_config('block_ilp', 'mis_plugin_register_absentcolour');
                         }
                         if (in_array($item[$markfield], $this->latecodes)) {
                             $att_class = get_config('block_ilp', 'mis_plugin_register_latecolour');
                         }
                         $class['week' . $item['Week_No']] = '<span style="display:block; text-align:center; background-color:' . $att_class . '" title="">' . $item[$markfield] . '</span>';
                     }
                 }
                 //calculate the attendance and late averages
                 if ($total[$groupKey][$cday][$ctime][0][$term] != 0) {
                     $class['late'] = round($total[$groupKey][$cday][$ctime][3][$term] / $total[$groupKey][$cday][$ctime][0][$term] * 100, 0) . '%';
                     $class['att'] = round($total[$groupKey][$cday][$ctime][1][$term] / $total[$groupKey][$cday][$ctime][0][$term] * 100, 0) . '%';
                 }
                 $classdata[] = $class;
                 //reset $class and $termsweeks to their original states
                 $class = $base_class;
                 $termsweeks = $base_termsweeks;
             }
         }
     }
     foreach ($classdata as $class) {
         $table->add_data_keyed($class);
     }
     $table->print_html();
 }