Index: report.php =================================================================== RCS file: /cvsroot/moodle/moodle/mod/quiz/report/overview/report.php,v retrieving revision 1.98.2.7 diff -u -r1.98.2.7 report.php --- report.php 27 Jan 2008 01:26:47 -0000 1.98.2.7 +++ report.php 21 Mar 2008 11:34:29 -0000 @@ -307,57 +307,58 @@ $contextlists = get_related_contexts_string(get_context_instance(CONTEXT_COURSE, $course->id)); + // Get users with quiz attempt capability + 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, '. '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 '; - if ($course->id != SITEID) { // this is too complicated, so just do it for each of the four cases. - if (!empty($currentgroup) && empty($noattempts)) { - // we want a particular group and we only want to see students WITH attempts. - // So join on groups_members and do an inner join on attempts. - $from = 'FROM '.$CFG->prefix.'user u JOIN '.$CFG->prefix.'role_assignments ra ON ra.userid = u.id '. - 'JOIN '.$CFG->prefix.'groups_members gm ON u.id = gm.userid '. - 'JOIN '.$CFG->prefix.'quiz_attempts qa ON u.id = qa.userid AND qa.quiz = '.$quiz->id; - $where = ' WHERE ra.contextid ' . $contextlists . ' AND gm.groupid = '. $currentgroup .' AND qa.preview = 0'; - } else if (!empty($currentgroup) && !empty($noattempts)) { - // We want a particular group and we want to do something funky with attempts - // So join on groups_members and left join on attempts... - $from = 'FROM '.$CFG->prefix.'user u JOIN '.$CFG->prefix.'role_assignments ra ON ra.userid = u.id '. - 'JOIN '.$CFG->prefix.'groups_members gm ON u.id = gm.userid '. - 'LEFT JOIN '.$CFG->prefix.'quiz_attempts qa ON u.id = qa.userid AND qa.quiz = '.$quiz->id; - $where = ' WHERE ra.contextid ' .$contextlists . ' AND gm.groupid = '.$currentgroup; - if ($noattempts == 1) { - // noattempts = 1 means only no attempts, so make the left join ask for only records where the right is null (no attempts) - $where .= ' AND qa.userid IS NULL'; // show ONLY no attempts; - } else { - // We are including attempts, so exclude previews. - $where .= ' AND qa.preview = 0'; - } - } else if (empty($currentgroup)) { - // We don't care about group, and we to do something funky with attempts - // So do a left join on attempts - $from = 'FROM '.$CFG->prefix.'user u JOIN '.$CFG->prefix.'role_assignments ra ON ra.userid = u.id LEFT JOIN '. - $CFG->prefix.'quiz_attempts qa ON u.id = qa.userid AND qa.quiz = '.$quiz->id; - $where = " WHERE ra.contextid $contextlists"; - if (empty($noattempts)) { - $where .= ' AND qa.userid IS NOT NULL AND qa.preview = 0'; // show ONLY students with attempts; - } else if ($noattempts == 1) { - // noattempts = 1 means only no attempts, so make the left join ask for only records where the right is null (no attempts) - $where .= ' AND qa.userid IS NULL'; // show ONLY students without attempts; - } else if ($noattempts == 3) { - // we want all attempts - $from = 'FROM '.$CFG->prefix.'user u JOIN '.$CFG->prefix.'quiz_attempts qa ON u.id = qa.userid '; - $where = ' WHERE qa.quiz = '.$quiz->id.' AND qa.preview = 0'; - } // noattempts = 2 means we want all students, with or without attempts - } + + if ($course->id != SITEID) { + + // 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 ( $noattempts == 3 ) { // Show all attempts, including previews and students who are no longer in the course + + $where = ' WHERE qa.id IS NOT NULL'; + + if (!empty($currentgroup)) { // limit to the current group + $where .= ' AND u.id IN ('. $allowed. ')'; + } + + // Comment out the following line to include preview attempts in the 'show all attempts' filter + $where .= ' AND qa.preview = 0'; + } + + else { // All non-admin users with quiz attempt capabilites - e.g. students + + $where = ' WHERE u.id IN (' .$allowed. ') AND (qa.preview = 0 OR qa.preview IS NULL)'; + + if ( empty( $noattempts )) { // Show only those users with attempts + + $where .= ' AND qa.id IS NOT NULL'; + } + else if ( $noattempts == 1 ) { // Show only those users without attempts + + $where .= ' AND qa.id IS NULL'; + } + } + $countsql = 'SELECT COUNT(DISTINCT('.sql_concat('u.id', '\'#\'', $db->IfNull('qa.attempt', '0')).')) '.$from.$where; - } else { - if (empty($noattempts)) { - $from = 'FROM '.$CFG->prefix.'user u JOIN '.$CFG->prefix.'quiz_attempts qa ON u.id = qa.userid '; - $where = ' WHERE qa.quiz = '.$quiz->id.' AND qa.preview = 0'; - $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()) { @@ -439,16 +440,17 @@ $picture = print_user_picture($attempt->userid, $course->id, $attempt->picture, false, true); - // uncomment the commented lines below if you are choosing to show unenrolled users and - // have uncommented the corresponding lines earlier in this script - //if (in_array($attempt->userid, $unenrolledusers)) { - // $userlink = ''.fullname($attempt).''; - //} - //else { + // If we're showing all attempts, check to see if user still has the quiz attempt capability + if ( $noattempts == 3 && !has_capability('mod/quiz:attempt', $context, $attempt->userid ) ) { + + $userlink = ''.fullname($attempt).''; + } + else { + $userlink = ''.fullname($attempt).''; - //} + } // Username columns. $row = array();