根据时间的不同,excel报告需要以php下载

时间:2018-07-19 07:08:33

标签: php excel

嗨,我正在尝试通过使用from和date以及用户类型来下载excel工作表中的用户报告,就像我们需要下载活动/不活动的用户一样。但是这里的问题是我无法找到我必须去的地方从我的代码中给出日期。

任何人都可以帮助我如何从我的代码中做到这一点吗?我需要将$ finish_date作为起始和截止日期

这是我的代码:

    <?php
if (isset($_POST['submit'])) {
    $fromdate = $_POST['datepicker'];
    $todate = $_POST['datepicker1'];
    $usertype = $_POST['usertype'];
    echo $fromdate;
    echo $todate;
    echo $usertype;



require_once dirname(__FILE__) . '/Classes/PHPExcel.php';
require('../config.php'); 
require_once('../login/lib.php'); 
global $DB,$USER, $CFG;
require_once($CFG->dirroot . '/mod/scorm/lib.php');
require_once($CFG->dirroot . '/mod/scorm/locallib.php');
require_once($CFG->dirroot . '/course/lib.php');
require_once dirname(__FILE__) . '/Classes/PHPExcel.php';

$companyid= $_SESSION['companyid'];

//course table fetch
$sql1 = "SELECT cm.id as cmid,c.id,c.fullname,c.startdate,instance,c.timecreated FROM {course} c LEFT JOIN {course_modules} cm ON c.id=cm.course where c.category != 0 and requested='0' AND cm.id!=''";
$courses = $DB->get_records_sql($sql1);

if (sizeof($courses)) {
    $exInc = 2;
    $objPHPExcel = new PHPExcel();
    $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Course Title ')
            ->setCellValue('B1', 'Name')
            ->setCellValue('C1', 'Company')
            ->setCellValue('D1', 'Department')
            ->setCellValue('E1', 'Branch')
             ->setCellValue('F1', 'Assigned Date')
           ->setCellValue('G1', 'Start Date')
            ->setCellValue('H1', 'Last Visited')
             ->setCellValue('I1', 'Total Time')
            ->setCellValue('J1', 'Course Score')
            ->setCellValue('K1', 'Course Status');

    foreach ($courses as $row) {

        $scorm = $DB->get_record("scorm", array("id" => $row->instance));
        $scoid = 0;
        $orgidentifier = '';
        if ($sco = scorm_get_sco($scorm->launch, SCO_ONLY)) {
            if (($sco->organization == '') && ($sco->launch == '')) {
                $orgidentifier = $sco->identifier;
            } else {
                $orgidentifier = $sco->organization;
            }
            $scoid = $sco->id;
        }
        $cmid = $row->cmid;
        $coursename = $row->fullname;
         $assigned_date = date('Y-M-d H-i-s', $row->timecreated);

        $useridQry = '';
        $userIds = '';
        $login_id = $_SESSION['USER']->id;

        if ($_SESSION['idnumber'] == 1) {

        } elseif ($_SESSION['idnumber'] == 2) {
            $records = $DB->get_records_sql("select * from {user} where maildigest=$login_id");
            if (count($records)) {
                foreach ($records as $row) {
                    $userIds[] = "'" . $row->id . "'";
                }
                if ($userIds != '') {
                    $userIds = implode(',', $userIds);
                    $useridQry = " and st.userid IN ($userIds)";
                }
            }
        } else {
            $useridQry = "and st.userid = $login_id";
        }


        $scom_id = $row->cmid;
        $cm = get_coursemodule_from_id('scorm', $scom_id, 0, false, MUST_EXIST);
        $course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
        $scorm = $DB->get_record('scorm', array('id' => $cm->instance), '*', MUST_EXIST);

        $query = "SELECT DISTINCT CONCAT(u.id, '#', COALESCE(st.attempt, 0)) AS uniqueid, st.scormid AS scormid, st.attempt AS attempt, u.id AS userid,u.picture,u.firstname,u.lastname,u.username,u.firstnamephonetic,u.lastnamephonetic,u.middlename,u.alternatename,u.imagealt,u.email,u.idnumber,u.icq,u.department,u.skype,pd.department,pd.did,pg.gid,pg.group_name,pc.cid,pc.company FROM mdl_user u LEFT JOIN mdl_scorm_scoes_track st ON st.userid = u.id AND st.scormid = $scorm->id  LEFT JOIN mdl_ppc_department pd ON pd.did=u.department LEFT JOIN mdl_ppc_groups pg On pg.gid=u.icq LEFT JOIN mdl_ppc_company pc On pc.cid=u.skype WHERE (st.userid IS NOT NULL) and u.idnumber != 0 and u.skype ='$companyid' and u.trackforums='$usertype'";
        $attempts = $DB->get_records_sql($query);
        $row = array();
        $statusval = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
                                            $totaltimeval = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
                                            $scoreval = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
                                            $valueu1 = 1;
                                            $valueu2 = 1;
                                            $valueu3 = 1;

        if (isset($attempts)) {
            foreach ($attempts as $scouser) {

                $type = '';
                $status = '';
                $totaltime = '';
                $score = '';

                if (!empty($scouser->attempt)) {
                    $timetracks = scorm_get_sco_runtime($scorm->id, false, $scouser->userid, $scouser->attempt);
                } else {
                    $timetracks = '';
                }


                global $DB;
                $tracks = $DB->get_records_sql("SELECT id, element, value FROM {scorm_scoes_track} WHERE scormid = ? AND userid = ? AND element IN('cmi.core.lesson_status','cmi.core.total_time','cmi.core.score.raw')", array($scorm->id, $scouser->userid));

                foreach ($tracks as $trac) {
                    if ($trac->element == 'cmi.core.lesson_status') {
                        $status = $trac->value;
                    }
                    if ($trac->element == 'cmi.core.total_time') {
                        $totaltime = $trac->value;
                    }
                    if ($trac->element == 'cmi.core.score.raw') {
                        $score = $trac->value;
                        $scoreval[$valueu3] = $score;
                        $valueu3 = $valueu3 + 1;
                    }


                    $scouser->times = $timetracks;
                    $scouser->status = $status;
                    $scouser->totaltime = $totaltime;
                    $scouser->score = $score;
                    $row[] = $scouser;

                    $start_date = $finish_date = '';

                    if (isset($scouser->times)) {
                        if (isset($scouser->times->start))
                            $start_date = date('Y-M-d H-i-s', $scouser->times->start);
                        if (isset($scouser->times->finish))
                            $finish_date = date('Y-M-d H-i-s', $scouser->times->finish);
                    }
                }

                $objPHPExcel->getActiveSheet()->setCellValue('A' . $exInc, $coursename)
                    ->setCellValue('B' . $exInc, $scouser->firstname.''. $scouser->lastname)
                    ->setCellValue('C' . $exInc, $scouser->company)
                    ->setCellValue('D' . $exInc, $scouser->department)
                     ->setCellValue('E' . $exInc, $scouser->group_name)
                    ->setCellValue('F' . $exInc, $assigned_date)
                    ->setCellValue('G' . $exInc, $start_date)
                    ->setCellValue('H' . $exInc, $finish_date)
                    ->setCellValue('I' . $exInc, $totaltime)
                    ->setCellValue('J' . $exInc, $score)
                    ->setCellValue('K' . $exInc, ucfirst($status));

                $exInc++;

            }
        }
    }

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="reports.xlsx"');
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
    //$objWriter->save($filename);
    $objWriter->save('php://output');

    exit();
}
}
?>
<script>
    setTimeout(function () {
        window.location.reload();
    }, 4 * 60 * 1000);
// just show current time stamp to see time of last refresh.
    document.write(new Date());
</script>
?>

我已经提到了用户类型,现在我要根据需要下载excel的用户类型来提起日期和日期。

有人可以帮我怎么做吗

提前谢谢。

1 个答案:

答案 0 :(得分:0)

在这里我已经改变了这种情况,然后开始工作

更改了这一行:

 $tracks = $DB->get_records_sql("SELECT id, element, value FROM {scorm_scoes_track} WHERE scormid = ? AND userid = ? AND element IN('cmi.core.lesson_status','cmi.core.total_time','cmi.core.score.raw'), array($scorm->id, $scouser->userid));

 $tracks = $DB->get_records_sql("SELECT id, element, value FROM {scorm_scoes_track} WHERE scormid = ? AND userid = ? AND element IN('cmi.core.lesson_status','cmi.core.total_time','cmi.core.score.raw') and DATE(FROM_UNIXTIME(mdl_scorm_scoes_track.timemodified))>= '$fromdate' AND DATE(FROM_UNIXTIME(mdl_scorm_scoes_track.timemodified))<= '$todate'" , array($scorm->id, $scouser->userid));