libdir.'/tablelib.php');
require_once($CFG->dirroot.'/mod/quiz/report/overview/overviewsettings_form.php');
class quiz_report extends quiz_default_report {
/**
* Display the report.
*/
function display($quiz, $cm, $course) {
global $CFG, $db;
// Define some strings
$strreallydel = addslashes(get_string('deleteattemptcheck','quiz'));
$strtimeformat = get_string('strftimedatetime');
$strreviewquestion = get_string('reviewresponse', 'quiz');
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
// Only print headers if not asked to download data
if (!$download = optional_param('download', NULL)) {
$this->print_header_and_tabs($cm, $course, $quiz, "overview");
}
// Deal with actions
$action = optional_param('action', '', PARAM_ACTION);
switch($action) {
case 'delete': // Some attempts need to be deleted
require_capability('mod/quiz:deleteattempts', $context);
$attemptids = optional_param('attemptid', array(), PARAM_INT);
foreach($attemptids as $attemptid) {
add_to_log($course->id, 'quiz', 'delete attempt', 'report.php?id=' . $cm->id,
$attemptid, $cm->id);
quiz_delete_attempt($attemptid, $quiz);
}
break;
}
// Set of format options for teacher-created content, for example overall feedback.
$nocleanformatoptions = new stdClass;
$nocleanformatoptions->noclean = true;
// Prepare list of available actions to perform on attempts - we only want to show the checkbox.
// Column on the table if there are options.
$attemptactions = array();
if (has_capability('mod/quiz:deleteattempts', $context)) {
$attemptactions['delete'] = get_string('delete');
}
// Work out some display options - whether there is feedback, and whether scores should be shown.
$hasfeedback = quiz_has_feedback($quiz->id) && $quiz->grade > 1.e-7 && $quiz->sumgrades > 1.e-7;
$fakeattempt = new stdClass();
$fakeattempt->preview = false;
$fakeattempt->timefinish = $quiz->timeopen;
$reviewoptions = quiz_get_reviewoptions($quiz, $fakeattempt, $context);
$showgrades = $quiz->grade && $quiz->sumgrades && $reviewoptions->scores;
if (!$reviewoptions->scores) {
$detailedmarks = 0;
}
$pageoptions = array();
$pageoptions['id'] = $cm->id;
$pageoptions['q'] = $quiz->id;
$pageoptions['mode'] = 'overview';
$reporturl = new moodle_url($CFG->wwwroot.'/mod/quiz/report.php', $pageoptions);
$qmsubselect = quiz_report_qm_filter_subselect($quiz->grademethod);
$mform = new mod_quiz_report_overview_settings($reporturl, compact('qmsubselect'));
if ($fromform = $mform->get_data()){
$attemptsmode = $fromform->attemptsmode;
if ($qmsubselect){
//control is not on the form if
//the grading method is not set
//to grade one attempt per user eg. for average attempt grade.
$qmfilter = $fromform->qmfilter;
} else {
$qmfilter = 0;
}
set_user_preference('quiz_report_overview_detailedmarks', $fromform->detailedmarks);
set_user_preference('quiz_report_pagesize', $fromform->pagesize);
$detailedmarks = $fromform->detailedmarks;
$pagesize = $fromform->pagesize;
} else {
$qmfilter = optional_param('qmfilter', 0, PARAM_INT);
$attemptsmode = optional_param('attemptsmode', QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH, PARAM_INT);
$detailedmarks = get_user_preferences('quiz_report_overview_detailedmarks', 1);
$pagesize = get_user_preferences('quiz_report_pagesize', 0);
}
if ($pagesize < 1) {
$pagesize = QUIZ_REPORT_DEFAULT_PAGE_SIZE;
}
$displayoptions = array();
$displayoptions['attemptsmode'] = $attemptsmode;
$displayoptions['qmfilter'] = $qmfilter;
$reporturlwithdisplayoptions = new moodle_url($CFG->wwwroot.'/mod/quiz/report.php', $pageoptions + $displayoptions);
/// find out current groups mode
$currentgroup = groups_get_activity_group($cm, true);
if ($groupmode = groups_get_activity_groupmode($cm)) { // Groups are being used
if (!$download) {
groups_print_activity_menu($cm, $reporturlwithdisplayoptions->out());
}
}
// Print information on the number of existing attempts
if (!$download) { //do not print notices when downloading
if ($strattemptnum = quiz_num_attempt_summary($quiz, $cm, false, $currentgroup)) {
echo '
' . $strattemptnum . '
';
}
}
// Now check if asked download of data
if ($download) {
$filename = clean_filename("$course->shortname ".format_string($quiz->name,true));
$sort = '';
}
// Define table columns
$tablecolumns = array('picture', 'fullname', 'timestart', 'timefinish', 'duration');
$tableheaders = array('', get_string('name'), get_string('startedon', 'quiz'),
get_string('timecompleted','quiz'), get_string('attemptduration', 'quiz'));
if (!empty($attemptactions)) {
array_unshift($tablecolumns, 'checkbox');
array_unshift($tableheaders, NULL);
}
if ($showgrades) {
$tablecolumns[] = 'sumgrades';
$tableheaders[] = get_string('grade', 'quiz').'/'.$quiz->grade;
}
if ($detailedmarks) {
// we want to display marks for all questions
$questions = quiz_report_load_questions($quiz);
foreach ($questions as $id => $question) {
// Ignore questions of zero length
$tablecolumns[] = '$'.$id;
$tableheaders[] = '#'.$question->number;
}
}
if ($hasfeedback) {
$tablecolumns[] = 'feedbacktext';
$tableheaders[] = get_string('feedback', 'quiz');
}
if (!$download) {
// Set up the table
$table = new flexible_table('mod-quiz-report-overview-report');
$table->define_columns($tablecolumns);
$table->define_headers($tableheaders);
$table->define_baseurl($reporturlwithdisplayoptions->out());
$table->sortable(true);
$table->collapsible(true);
$table->column_suppress('picture');
$table->column_suppress('fullname');
$table->column_class('picture', 'picture');
$table->set_attribute('cellspacing', '0');
$table->set_attribute('id', 'attempts');
$table->set_attribute('class', 'generaltable generalbox');
// Start working -- this is necessary as soon as the niceties are over
$table->setup();
} else if ($download =='ODS') {
require_once("$CFG->libdir/odslib.class.php");
$filename .= ".ods";
// Creating a workbook
$workbook = new MoodleODSWorkbook("-");
// Sending HTTP headers
$workbook->send($filename);
// Creating the first worksheet
$sheettitle = get_string('reportoverview','quiz');
$myxls =& $workbook->add_worksheet($sheettitle);
// format types
$format =& $workbook->add_format();
$format->set_bold(0);
$formatbc =& $workbook->add_format();
$formatbc->set_bold(1);
$formatbc->set_align('center');
$formatb =& $workbook->add_format();
$formatb->set_bold(1);
$formaty =& $workbook->add_format();
$formaty->set_bg_color('yellow');
$formatc =& $workbook->add_format();
$formatc->set_align('center');
$formatr =& $workbook->add_format();
$formatr->set_bold(1);
$formatr->set_color('red');
$formatr->set_align('center');
$formatg =& $workbook->add_format();
$formatg->set_bold(1);
$formatg->set_color('green');
$formatg->set_align('center');
// Here starts workshhet headers
$headers = array(get_string('name'), get_string('startedon', 'quiz'),
get_string('timecompleted', 'quiz'), get_string('attemptduration', 'quiz'));
if ($showgrades) {
$headers[] = get_string('grade', 'quiz').'/'.$quiz->grade;
}
if($detailedmarks) {
foreach ($questions as $question) {
$headers[] = '#'.$question->number;
}
}
if ($hasfeedback) {
$headers[] = get_string('feedback', 'quiz');
}
$colnum = 0;
foreach ($headers as $item) {
$myxls->write(0,$colnum,$item,$formatbc);
$colnum++;
}
$rownum=1;
} else if ($download =='Excel') {
require_once("$CFG->libdir/excellib.class.php");
$filename .= ".xls";
// Creating a workbook
$workbook = new MoodleExcelWorkbook("-");
// Sending HTTP headers
$workbook->send($filename);
// Creating the first worksheet
$sheettitle = get_string('reportoverview','quiz');
$myxls =& $workbook->add_worksheet($sheettitle);
// format types
$format =& $workbook->add_format();
$format->set_bold(0);
$formatbc =& $workbook->add_format();
$formatbc->set_bold(1);
$formatbc->set_align('center');
$formatb =& $workbook->add_format();
$formatb->set_bold(1);
$formaty =& $workbook->add_format();
$formaty->set_bg_color('yellow');
$formatc =& $workbook->add_format();
$formatc->set_align('center');
$formatr =& $workbook->add_format();
$formatr->set_bold(1);
$formatr->set_color('red');
$formatr->set_align('center');
$formatg =& $workbook->add_format();
$formatg->set_bold(1);
$formatg->set_color('green');
$formatg->set_align('center');
// Here starts workshhet headers
$headers = array(get_string('name'), get_string('startedon', 'quiz'),
get_string('timecompleted', 'quiz'), get_string('attemptduration', 'quiz'));
if ($showgrades) {
$headers[] = get_string('grade', 'quiz').'/'.$quiz->grade;
}
if($detailedmarks) {
foreach ($questions as $question) {
$headers[] = '#'.$question->number;
}
}
if ($hasfeedback) {
$headers[] = get_string('feedback', 'quiz');
}
$colnum = 0;
foreach ($headers as $item) {
$myxls->write(0,$colnum,$item,$formatbc);
$colnum++;
}
$rownum=1;
} else if ($download=='CSV') {
$filename .= ".txt";
header("Content-Type: application/download\n");
header("Content-Disposition: attachment; filename=\"$filename\"");
header("Expires: 0");
header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
header("Pragma: public");
$headers = get_string('name')."\t".get_string('startedon', 'quiz')."\t".
get_string('timecompleted', 'quiz')."\t".get_string('attemptduration', 'quiz');
if ($showgrades) {
$headers .= "\t".get_string('grade', 'quiz')."/".$quiz->grade;
}
if($detailedmarks) {
foreach ($questions as $question) {
$headers .= "\t#".$question->number;
}
}
if ($hasfeedback) {
$headers .= "\t" . get_string('feedback', 'quiz');
}
echo $headers." \n";
}
// Get users with quiz attempt capability 'students'.
// don't need to do this expensive call if we are listing all attempts though.
if ( $attemptsmode != QUIZ_REPORT_ATTEMPTS_ALL ) {
if (empty($currentgroup)) {
// all users who can attempt quizzes
$allowed = join(',',array_keys(get_users_by_capability($context, 'mod/quiz:attempt','','','','','','',false)));
} else {
// all users who can attempt quizzes and who are in the currently selected group
$allowed = join(',',array_keys(get_users_by_capability($context, 'mod/quiz:attempt','','','','',$currentgroup,'',false)));
}
}
// Construct the SQL
$select = 'SELECT '.sql_concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).' AS uniqueid, '.
($qmsubselect?$qmsubselect.' AS gradedattempt, ':'').
'qa.uniqueid AS attemptuniqueid, qa.id AS attempt, u.id AS userid, u.firstname, u.lastname, u.picture, '.
'qa.sumgrades, qa.timefinish, qa.timestart, qa.timefinish - qa.timestart AS duration ';
// This part is the same for all cases - join users and quiz_attempts tables
$from = 'FROM '.$CFG->prefix.'user u ';
$from .= 'LEFT JOIN '.$CFG->prefix.'quiz_attempts qa ON qa.userid = u.id AND qa.quiz = '.$quiz->id;
if ($qmsubselect && $qmfilter){
$from .= ' AND '.$qmsubselect;
}
switch ($attemptsmode){
case QUIZ_REPORT_ATTEMPTS_ALL:
// Show all attempts, including students who are no longer in the course
$where = ' WHERE qa.id IS NOT NULL AND qa.preview = 0';
break;
case QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH:
// Show only students with attempts
$where = ' WHERE u.id IN (' .$allowed. ') AND qa.preview = 0 AND qa.id IS NOT NULL';
break;
case QUIZ_REPORT_ATTEMPTS_STUDENTS_WITH_NO:
// Show only students without attempts
$where = ' WHERE u.id IN (' .$allowed. ') AND qa.id IS NULL';
break;
case QUIZ_REPORT_ATTEMPTS_ALL_STUDENTS:
// Show all students with or without attempts
$where = ' WHERE u.id IN (' .$allowed. ') AND (qa.preview = 0 OR qa.preview IS NULL)';
break;
}
$countsql = 'SELECT COUNT(DISTINCT('.sql_concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).')) '.$from.$where;
if (!$download) {
// Add extra limits due to initials bar
if($table->get_sql_where()) {
$where .= ' AND '.$table->get_sql_where();
}
// Count the records NOW, before funky question grade sorting messes up $from
if (!empty($countsql)) {
$totalinitials = count_records_sql($countsql);
if ($table->get_sql_where()) {
$countsql .= ' AND '.$table->get_sql_where();
}
$total = count_records_sql($countsql);
}
// Add extra limits due to sorting by question grade
if($sort = $table->get_sql_sort()) {
$sortparts = explode(',', $sort);
$newsort = array();
$questionsort = false;
foreach($sortparts as $sortpart) {
$sortpart = trim($sortpart);
if(substr($sortpart, 0, 1) == '$') {
if(!$questionsort) {
$qid = intval(substr($sortpart, 1));
$select .= ', qs.grade AS qgrade ';
$from .= ' LEFT JOIN '.$CFG->prefix.'question_sessions qns ON qns.attemptid = qa.uniqueid '.
'LEFT JOIN '.$CFG->prefix.'question_states qs ON qs.id = qns.newgraded ';
$where .= ' AND (qns.questionid IS NULL OR qns.questionid = '.$qid.')';
$newsort[] = 'qgrade '.(strpos($sortpart, 'ASC')? 'ASC' : 'DESC');
$questionsort = true;
}
} else {
$newsort[] = $sortpart;
}
}
// Reconstruct the sort string
$sort = ' ORDER BY '.implode(', ', $newsort);
}
// Fix some wired sorting
if (empty($sort)) {
$sort = ' ORDER BY uniqueid';
}
$table->pagesize($pagesize, $total);
}
// If there is feedback, include it in the query.
if ($hasfeedback) {
$factor = $quiz->grade/$quiz->sumgrades;
$select .= ', qf.feedbacktext ';
$from .= " LEFT JOIN {$CFG->prefix}quiz_feedback qf ON " .
"qf.quizid = $quiz->id AND qf.mingrade <= qa.sumgrades * $factor AND qa.sumgrades * $factor < qf.maxgrade";
}
// Fetch the attempts
if (!empty($from)) { // if we're in the site course and displaying no attempts, it makes no sense to do the query.
if (!$download) {
$attempts = get_records_sql($select.$from.$where.$sort,
$table->get_page_start(), $table->get_page_size());
} else {
$attempts = get_records_sql($select.$from.$where.$sort);
}
} else {
$attempts = array();
}
// Build table rows
if (!$download) {
$table->initialbars($totalinitials>20);
}
if ($attempts) {
if($detailedmarks) {
//get all the attempt ids we want to display on this page
//or to export for download.
$attemptids = array();
foreach ($attempts as $attempt){
if ($attempt->attemptuniqueid > 0){
$attemptids[] = $attempt->attemptuniqueid;
}
}
$gradedstatesbyattempt = quiz_get_newgraded_states($attemptids);
}
foreach ($attempts as $attempt) {
$picture = print_user_picture($attempt->userid, $course->id, $attempt->picture, false, true);
$userlink = ''.fullname($attempt).'';
// Username columns.
$row = array();
if (!$download) {
if (!empty($attemptactions)) {
$row[] = '';
}
$row[] = $picture;
$row[] = $userlink;
} else {
$row[] = fullname($attempt);
}
// Timing columns.
if ($attempt->attempt) {
$startdate = userdate($attempt->timestart, $strtimeformat);
if (!$download) {
$row[] = ''.$startdate.'';
} else {
$row[] = $startdate;
}
if ($attempt->timefinish) {
$timefinish = userdate($attempt->timefinish, $strtimeformat);
$duration = format_time($attempt->duration);
if (!$download) {
$row[] = ''.$timefinish.'';
} else {
$row[] = $timefinish;
}
$row[] = $duration;
} else {
$row[] = '-';
$row[] = get_string('unfinished', 'quiz');
}
} else {
$row[] = '-';
$row[] = '-';
$row[] = '-';
}
// Grades columns.
if ($showgrades) {
if ($attempt->timefinish) {
$grade = quiz_rescale_grade($attempt->sumgrades, $quiz);
if (!$download) {
$gradehtml = ''.$grade.'';
if ($qmsubselect && $attempt->gradedattempt){
$gradehtml = '