为什么我从此脚本中获得重复结果?

时间:2016-06-01 10:31:12

标签: php mysql moodle

有些行正在多次打印出来,变量$ foo表明它是foreach($ student作为$ student)循环,但我无法弄清楚它为什么会发生。

这是Moodle中的自定义报告

这是我的代码

require_once('../../config.php');
require_once($CFG->libdir . '/adminlib.php');

admin_externalpage_setup('reportlink_critic', '', null, '',  array('pagelayout'=>'report'));

echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('pluginname', 'report_link_critic'));

$table = new html_table();

$table->head = array('Student', 'Links Posted', 'Votes Received', 'Cumalative Score', 'Rating', 'Foo');
$table->headspan = array(1, 1, 1, 1, 1, 1);

$query_students = "SELECT id, firstname, lastname FROM mdl_user WHERE confirmed = '1' AND deleted = '0'";

$students = $DB->get_recordset_sql($query_students);

$foo = 1;

foreach( $students as $student ) {

    $query_links = "SELECT count(id), user_id AS links_posted FROM mdl_link_critic_links WHERE user_id = '" . $student->id . "'";
    $links = $DB->get_recordset_sql($query_links);

    foreach($links as $link) {
        $links_posted = $link->links_posted;
    }

    $query_votes = "SELECT count(v.id) AS votes FROM mdl_link_critic_votes v JOIN mdl_link_critic_links l ON l.id = v.link_id WHERE l.user_id = '" . $student->id . "' ";
    $votes = $DB->get_recordset_sql($query_votes);

    foreach($votes as $vote) {
        $total_votes = $vote->votes;
    }

    $query_scores = "SELECT sum(v.vote_score) as score FROM mdl_link_critic_votes v WHERE user_id = '" . $student->id . "' ";
    $scores = $DB->get_recordset_sql($query_scores);

    foreach($scores as $score) {
        $cumalative_score = $score->score;
    }

    $foo = $foo + 1;

    if($links_posted > 0) {
        $rating = round(($total_votes * $cumalative_score) / $links_posted, 2);
        $cells = array( $student->firstname . ' ' . $student->lastname, $links_posted, $total_votes, $cumalative_score, $rating, $foo );
    }

    if(!empty($cells)) {    
        $table->data[] = new html_table_row($cells);
    }


}

echo html_writer::table($table);

$students->close();
$links->close();
$votes->close();
$scores->close();

echo $OUTPUT->footer();

报告打印出每一行作为学生姓名,收到的票数,cumalative评分,评分和$ foo的计数,但有些学生打印出几次?非常困惑

0 个答案:

没有答案