* 4) A manager approving his staff's booking requests
 *   - Manager does not neccesarily have any capabilities in this course
 *   - Show only attendees who are also the manager's staff
 *   - Show only staff awaiting approval
 *   - Show any staff who have cancelled
 *   - Shouldn't throw an error if there are previously declined attendees
 */
// Allowed actions are actions the user has permissions to do
$allowed_actions = array();
$can_view_session =false;

if(is_siteadmin())
$can_view_session = true;
elseif(is_teammanager())
$can_view_session = true;
elseif(is_reportingmanager())
$can_view_session = true;
elseif(is_trainingmanager())
$can_view_session = true;
elseif(is_odmanager())
$can_view_session = true;
else{
if (!$can_view_session) {
    $return = new moodle_url('/mod/facetoface/view.php', array('f' => $facetoface->id));
    redirect($return);
    die();
}
}
// Available actions are actions that have a point. e.g. view the cancellations page whhen there are no cancellations is not an "available" action, but it maybe be an "allowed" action
$available_actions = array();
 function print_session_list_custom_table($table,$tableheader,$facetofacename,$customfields, $sessions, $viewattendees, $editsessions,$tab=0,$userid_url=0) {
     global $CFG,$PAGE,$DB,$USER,$OUTPUT;   
     $output = '';

          $can_view_session =false;

        if(is_siteadmin())
            $can_view_session = true;
        elseif(is_teammanager())
            $can_view_session = true;
        elseif(is_reportingmanager())
            $can_view_session = true;
            $timenow = time();

        $table->data = array();
        foreach ($sessions as $session) {
	
            $isbookedsession = false;
  
            $bookedsession = $session->bookedsession;
            $sessionstarted = false;
            $sessionfull = false;

            $sessionrow = array();

            // Custom fields
            $customdata = $session->customfielddata;
            foreach ($customfields as $field) {
                if (empty($field->showinsummary)) {
                    continue;
                }

                if (empty($customdata[$field->id])) {
                    $sessionrow[] = ' ';
                } else {
                    if (CUSTOMFIELD_TYPE_MULTISELECT == $field->type) {
                        $sessionrow[] = str_replace(CUSTOMFIELD_DELIMITER, html_writer::empty_tag('br'), format_string($customdata[$field->id]->data));
                    } else {
                        $sessionrow[] = format_string($customdata[$field->id]->data);
                    }
                }
            }

            // Dates/times
            $allsessiondates = '';
            $allsessiontimes = '';
            if ($session->datetimeknown) {
                foreach ($session->sessiondates as $date) {
                    if (!empty($allsessiondates)) {
                        $allsessiondates .= html_writer::empty_tag('br');
                        $allsessiontimes .= html_writer::empty_tag('br');
                    }
                    $sessionobj = facetoface_format_session_times($date->timestart, $date->timefinish, $date->sessiontimezone);
                    if ($sessionobj->startdate == $sessionobj->enddate) {
                        $allsessiondates .= $sessionobj->startdate;
                    } else {
                        $allsessiondates .= $sessionobj->startdate . ' - ' . $sessionobj->enddate;
                    }
                    $allsessiontimes .= $sessionobj->starttime . ' - ' . $sessionobj->endtime . ' ' . $sessionobj->timezone;
                }
            } else {
                $allsessiondates = get_string('wait-listed', 'facetoface');
                $allsessiontimes = get_string('wait-listed', 'facetoface');
                $sessionwaitlisted = true;
            }
            $sessionrow[] = $allsessiondates;
            $sessionrow[] = $allsessiontimes;
            /*Showing online sessions */
              /* Onlinesession=1 indeciates we have wiziq session for a session in a batch
               * Onlinesession=0 indicates the classroom session
               *
               */
            if($session->onlinesession==1) {
                 $wiziqid=$DB->get_field('wiziq','id',array('sessionid'=>$session->id,'batchid'=>$session->facetoface));
				 $modulename='wiziq';
				$moduleids= $DB->get_field('modules','id',array('name'=>$modulename));
				$status=$DB->get_field('wiziq','class_status',array('id'=>$wiziqid));
                $moduleid=$DB->get_field('course_modules','id',array('instance'=>$wiziqid,'module'=>$moduleids));
				   $sessions_signups=$DB->get_record_sql("SELECT fss.* FROM {facetoface_signups} as fs ,{facetoface_signups_status} as fss WHERE fs.sessionid=$session->id AND fs.id=fss.signupid AND fss.superceded=0");
                if($sessions_signups->statuscode==10){
                 $link =get_string('join','facetoface');
                 }else{
			     $link = html_writer::link( $CFG->wwwroot.'/mod/wiziq/view.php?id='.$moduleid,get_string('join','facetoface'), array('title' => get_string('joinwebex', 'facetoface')));
                 }
                $sessionrow[]=$link;
            }
            else 
                $sessionrow[]= get_string('classroom', 'facetoface');
            // Room.
            if (isset($session->room)) {
                $roomhtml = '';
                $roomhtml .= isset($session->room->name) ? format_string($session->room->name) . html_writer::empty_tag('br') : '';
                $roomhtml .= isset($session->room->building) ? format_string($session->room->building) . html_writer::empty_tag('br') : '';
                $roomhtml .= isset($session->room->address) ? format_string($session->room->address) . html_writer::empty_tag('br') : '';
                $sessionrow[] = $roomhtml;
            } else {
                $sessionrow[] = '';
            }

            // Capacity.
			if($userid_url!=0){
           $user_createdid=$DB->get_field('facetoface_sessions','usermodified',array('id'=>$session->sessiondates[0]->sessionid));
		   $username=$DB->get_field('user','firstname',array('id'=>$user_createdid));
            $sessionrow[] = $username;
             $sessions_signups=$DB->get_record_sql("SELECT fss.* FROM {facetoface_signups} as fs ,{facetoface_signups_status} as fss WHERE fs.sessionid=$session->id AND fs.id=fss.signupid AND fss.superceded=0");
              switch($sessions_signups->statuscode){
                case 70:
                     $sessionrow[] ="Active";
                 break;
                case 10:
                     $sessionrow[] ="Cancel";
                 break;
                 default:
                   $sessionrow[] ="Complete";  
              }
            
			}
if($userid_url==0){
	if ($session->datetimeknown) {
                $signupcount = facetoface_get_num_attendees($session->id, MDL_F2F_STATUS_BOOKED);
            } else {
                $signupcount = facetoface_get_num_attendees($session->id, MDL_F2F_STATUS_APPROVED);
            }
            if ($viewattendees) {
                if ($session->datetimeknown) {
                    $a = array('current' => $signupcount, 'maximum' => $session->capacity);
                    $stats = get_string('capacitycurrentofmaximum', 'facetoface', $a);
                    if ($signupcount > $session->capacity) {
                        $stats .= get_string('capacityoverbooked', 'facetoface');
                    }
                    $waitlisted = facetoface_get_num_attendees($session->id, MDL_F2F_STATUS_APPROVED) - $signupcount;
                    if ($waitlisted > 0) {
                        $stats .= " (" . $waitlisted . " " . get_string('status_waitlisted', 'facetoface') . ")";
                    }
                } else {
                    $stats = $session->capacity . " (" . $signupcount . " " . get_string('status_waitlisted', 'facetoface') . ")";
                }
            } else {
                $stats = max(0, $session->capacity - $signupcount);
            }
            $sessionrow[] = $stats;
            // Status.
            $allowcancellation = false;
            $status  = get_string('bookingopen', 'facetoface');
            if ($session->datetimeknown && facetoface_has_session_started($session, $timenow) && !facetoface_is_session_in_progress($session, $timenow)) {
                $status = get_string('sessioninprogress', 'facetoface');
                $sessionstarted = true;
                // If user status is wait-listed.
                if ($bookedsession && $bookedsession->statuscode == MDL_F2F_STATUS_WAITLISTED) {
                    $allowcancellation = true;
                }
           } else if ($session->datetimeknown && facetoface_has_session_started($session, $timenow)&&facetoface_is_session_in_progress($session, $timenow)) {
                $status = get_string('sessionover', 'facetoface');
                $sessionstarted = true;
                // If user status is wait-listed.
                if ($bookedsession && $bookedsession->statuscode == MDL_F2F_STATUS_WAITLISTED) {
                    $allowcancellation = true;
                }
            } else if ($bookedsession && $session->id == $bookedsession->sessionid) {
                $signupstatus = facetoface_get_status($bookedsession->statuscode);
                $status = get_string('status_'.$signupstatus, 'facetoface');
                $isbookedsession = true;
            } else if ($signupcount >= $session->capacity) {
                $status = get_string('bookingfull', 'facetoface');
                $sessionfull = true;
            }

            $sessionrow[] = $status;


     $batchstatus=$DB->get_field('facetoface','active',array('id'=>$session->facetoface));
                 
         /* 0 indicates batch status is new
         * 1 indicates batch status is active
         * 2 indicates batch status is canceled
         * 3 indicates batch status is hold
         * 8 indicates batch status is completed
         */
         
    $options = '';
    if($batchstatus==0||$batchstatus==3){
            if ($editsessions) {
               
             
                    $options .= $this->output->action_icon(new moodle_url($CFG->wwwroot.'/mod/facetoface/sessions.php', array('s' => $session->id,'tab'=>$tab)), new pix_icon('t/edit', get_string('edit', 'facetoface')), null, array('title' => get_string('editsession', 'facetoface'))) . ' ';
                    $options .= $this->output->action_icon(new moodle_url($CFG->wwwroot.'/mod/facetoface/sessions.php', array('s' => $session->id, 'c' => 1,'tab'=>$tab)), new pix_icon('t/copy', get_string('copy', 'facetoface')), null, array('title' => get_string('copysession', 'facetoface'))) . ' ';
                    $options .= $this->output->action_icon(new moodle_url($CFG->wwwroot.'/mod/facetoface/sessions.php', array('s' => $session->id, 'd' => 1,'tab'=>$tab)), new pix_icon('t/delete', get_string('delete', 'facetoface')), null, array('title' => get_string('deletesession', 'facetoface'),'id'=>'deleteconfirmsession'.$session->id.'')) . ' ';
                    $PAGE->requires->event_handler('#deleteconfirmsession'.$session->id.'', 'click', 'M.util.tmahendra_show_confirm_dialog', array('message' =>  get_string('deletesession', 'facetoface'), 'callbackargs' => array('id' => '','extraparams'=>"s=$session->id&d=1&confirm=1")));
                    $options .= html_writer::empty_tag('br');
                
                
            }
            if ($viewattendees || $can_view_session) {
                 $batchstatus=$DB->get_field('facetoface','active',array('id'=>$session->id));
                
                    //$options .= html_writer::link( $CFG->wwwroot.'/mod/facetoface/attendees.php?s='.$session->id.'&backtoallsessions='.$session->facetoface, get_string('attendees', 'facetoface'), array('title' => get_string('seeattendees', 'facetoface')));
                    //$options .= html_writer::empty_tag('br');
                
                
				
            }
			if(is_trainers() || is_siteadmin() || is_instructor() || is_ast_nh()){
			$has_attendees = facetoface_get_num_attendees($session->id);	 
			if ($has_attendees && $session->datetimeknown && facetoface_has_session_started($session, time())) {
				$options .=html_writer::link( $CFG->wwwroot.'/mod/facetoface/attendence.php?s='.$session->id, 'Take Attendance', array('title' =>'Take Attendance'));
                $options .= html_writer::empty_tag('br');
			}
			}
            if ($isbookedsession) {
                $options .= html_writer::link($CFG->wwwroot.'/mod/facetoface/signup.php?s='.$session->id.'&backtoallsessions='.$session->facetoface, get_string('moreinfo', 'facetoface'), array('title' => get_string('moreinfo', 'facetoface')));
                $options .= html_writer::empty_tag('br');
                $options .= html_writer::link($CFG->wwwroot.'/mod/facetoface/cancelsignup.php?s='.$session->id.'&backtoallsessions='.$session->facetoface, get_string('cancelbooking', 'facetoface'), array('title' => get_string('cancelbooking', 'facetoface')));
            } else if (!$sessionstarted and !$bookedsession) {
                if (!facetoface_session_has_capacity($session, $this->context, MDL_F2F_STATUS_WAITLISTED) && !$session->allowoverbook) {
                    $options .= get_string('none', 'facetoface');
                } else {
                    $options .= html_writer::link($CFG->wwwroot.'/mod/facetoface/signup.php?s='.$session->id.'&backtoallsessions='.$session->facetoface, get_string('signup', 'facetoface'));
                }
            }
            if (empty($options)) {
                if ($sessionstarted && $allowcancellation) {
                    $options = html_writer::link($CFG->wwwroot.'/mod/facetoface/cancelsignup.php?s='.$session->id.'&backtoallsessions='.$session->facetoface, get_string('cancelbooking', 'facetoface'), array('title' => get_string('cancelbooking', 'facetoface')));
                } else {
                    $options = get_string('none', 'facetoface');
                }
            }
    //*======================================================================================*//        
    }elseif($batchstatus==1||$batchstatus==2||$batchstatus==8){
            if ($editsessions) {
               
        if($batchstatus==1){
                    $sessions_signups=$DB->get_record_sql("SELECT fss.* FROM {facetoface_signups} as fs ,{facetoface_signups_status} as fss WHERE fs.sessionid=$session->id AND fs.id=fss.signupid AND fss.statuscode=10 AND fss.superceded=0");
                    if($sessions_signups){
                        $options .= html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/images'),'width'=>'20','style'=>' margin-left: 17px;')). ' ';
                    }else{
                    $options .=html_writer::link(new moodle_url('/mod/facetoface/confirm_session_cancel.php', array('id' =>$session->id)), html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('t/images'),'width'=>'20','style'=>' margin-left: 17px;',
                    'onmouseover' => "this.src='".$OUTPUT->pix_url('t/images')."'",
                    'onmouseout' => "this.src='".$OUTPUT->pix_url('t/images')."'", 'title' => 'Cancel',
                    'alt' =>'Cancel', 'class' => 'iconsmall')),
                                            array('id' => 'deleteconfirm_session' .$session->id. '')). html_writer::script("Y.on('click', M.util.tmahendra_show_confirm_dialog, '#deleteconfirm_session$session->id', null, {'message':'" . get_string('deleteconfirm_session', 'mod_facetoface',$allsessiondates) . "','callbackargs':{'id':$session->id,'extraparams':'&rem=remove&delete=$session->id'}});");;
				
                    }
                  
                    $options .= html_writer::empty_tag('br');
                
            
           if ($viewattendees || $can_view_session) {
                 $batchstatus=$DB->get_field('facetoface','active',array('id'=>$session->id));
                
                    //$options .= html_writer::link( $CFG->wwwroot.'/mod/facetoface/attendees.php?s='.$session->id.'&backtoallsessions='.$session->facetoface, get_string('attendees', 'facetoface'), array('title' => get_string('seeattendees', 'facetoface')));
                    //$options .= html_writer::empty_tag('br');
                
                
				
            }
			if(is_trainers() || is_siteadmin() || is_instructor() || is_ast_nh()){
			$has_attendees = facetoface_get_num_attendees($session->id);	 
			if ($has_attendees && $session->datetimeknown && facetoface_has_session_started($session, time())) {
				$options .=html_writer::link( $CFG->wwwroot.'/mod/facetoface/attendence.php?s='.$session->id, 'Take Attendance', array('title' =>'Take Attendance'));
               
			}
			}
        }else{
            $options.="N/A";
        }
       
           
            }
    }
           $sessionrow[] = $options;
}
            $row = new html_table_row($sessionrow);

            // Set the CSS class for the row.
            if ($sessionstarted) {
                $row->attributes = array('class' => 'dimmed_text');
            } else if ($isbookedsession) {
                $row->attributes = array('class' => 'highlight');
            } else if ($sessionfull) {
                $row->attributes = array('class' => 'dimmed_text');
            }

            // Add row to table.
            $table->data[] = $row;
        }

        //$output .= html_writer::table($table);

        return $table;
    }