. /** * This page shows the answers of the current game * * @author bdaloukas * @version $Id: showattempts.php,v 1.6 2012/07/25 11:16:04 bdaloukas Exp $ * @package game **/ require_once("../../config.php"); require_once( "headergame.php"); if (!has_capability('mod/game:viewreports', $context)) { print_error( get_string( 'only_teachers', 'game')); } $PAGE->navbar->add(get_string('showattempts', 'game')); $action = optional_param('action', "", PARAM_ALPHANUM); // Action. if ($action == 'delete') { game_ondeleteattempt( $game); } echo get_string( 'group').': '; game_showgroups( $game); echo '   '.get_string('user').': '; game_showusers( $game);echo '

'; game_showattempts( $game); echo $OUTPUT->footer(); function game_showusers($game) { global $CFG, $USER, $DB; $users = array(); $context = game_get_context_course_instance( $game->course); $groupid = optional_param('groupid', 0, PARAM_INT); $sql = "SELECT DISTINCT ra.userid,u.lastname,u.firstname FROM {role_assignments} ra, {user} u ". " WHERE ra.contextid={$context->id} AND ra.userid=u.id"; if ($groupid != 0) { $sql .= " AND ra.userid IN (SELECT gm.userid FROM {groups_members} gm WHERE gm.groupid=$groupid)"; } if (($recs = $DB->get_records_sql( $sql))) { foreach ($recs as $rec) { $users[ $rec->userid] = $rec->lastname.' '.$rec->firstname; } } if ($guest = guest_user()) { $users[$guest->id] = fullname($guest); } $href = $CFG->wwwroot.'/mod/game/showattempts.php?q='.$game->id.'&userid='; ?> ' . "\n"; $output .= ' ' . "\n"; } } } echo $output . '' . "\n"; } function game_showgroups($game) { global $CFG, $USER, $DB; $groups = array(); if (($recs = $DB->get_records_sql( "SELECT id,name FROM {groups} WHERE courseid=$game->course ORDER BY name"))) { foreach ($recs as $rec) { $groups[ $rec->id] = $rec->name; } } $href = $CFG->wwwroot.'/mod/game/showattempts.php?q='.$game->id.'&groupid='; ?> ' . "\n"; $output .= ' ' . "\n"; } } } echo $output . '' . "\n"; } function game_showattempts($game) { global $CFG, $DB, $OUTPUT; $allowdelete = optional_param('allowdelete', 0, PARAM_INT); $userid = optional_param('userid', 0, PARAM_INT); $limitfrom = optional_param('limitfrom', 0, PARAM_INT); $gamekind = $game->gamekind; $update = get_coursemodule_from_instance( 'game', $game->id, $game->course)->id; // Here are user attempts. $table = "{game_attempts} as ga, {user} u, {game} as g"; $select = "ga.userid=u.id AND ga.gameid={$game->id} AND g.id={$game->id}"; $fields = "ga.id, u.lastname, u.firstname, ga.attempts,". "timestart, timefinish, timelastattempt, score, ga.lastip, ga.lastremotehost"; if ($userid != 0) { $select .= ' AND u.id='.$userid; } $sql = "SELECT COUNT(*) AS c FROM $table WHERE $select"; $count = $DB->count_records_sql( $sql); $maxlines = 20; $recslimitfrom = $recslimitnum = ''; if ($count > $maxlines) { $recslimitfrom = ( $limitfrom ? $limitfrom * $maxlines : ''); $recslimitnum = $maxlines; for ($i = 0; $i * $maxlines < $count; $i++) { if ($i == $limitfrom) { echo ($i + 1).' '; } else { echo "wwwroot}/mod/game/showattempts.php?q={$game->id}&limitfrom=$i&\">".($i + 1).""; echo '  '; } } echo "
"; } $sql = "SELECT $fields FROM $table WHERE $select ORDER BY timelastattempt DESC,timestart DESC"; if (($recs = $DB->get_records_sql( $sql, null, $recslimitfrom, $recslimitnum)) != false) { echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo "\r\n"; foreach ($recs as $rec) { echo ''; echo ''; echo ''; echo ''; echo '\r\n"; echo ''; echo ''; echo ''; echo ''; echo ''; // Show the solution. echo ''; echo "\r\n"; } echo "
'.get_string( 'delete').''.get_string('user').''.get_string('lastip', 'game').''.get_string('timestart', 'game').''.get_string('timelastattempt', 'game').''.get_string('timefinish', 'game').''.get_string('score', 'game').''.get_string('attempts', 'game').''.get_string('preview', 'game').''.get_string('showsolution', 'game').'
'; if (($rec->timefinish == 0) or $allowdelete) { echo "\r\nwwwroot}/mod/game/showattempts.php?attemptid={$rec->id}"; echo "&q={$game->id}&action=delete"; if ($allowdelete) { echo '&allowdelete=1'; } echo '">'; echo ''.get_string( 'delete').''; } echo '
'.$rec->firstname. ' '.$rec->lastname.'
'.(strlen( $rec->lastremotehost) > 0 ? $rec->lastremotehost : $rec->lastip).'
'.( $rec->timestart != 0 ? userdate($rec->timestart) : '')."
'.( $rec->timelastattempt != 0 ? userdate($rec->timelastattempt) : '').'
'.( $rec->timefinish != 0 ? userdate($rec->timefinish) : '').'
'.round($rec->score * 100).'
'.$rec->attempts.'
'; // Preview. if (($gamekind == 'cross') or ($gamekind == 'sudoku') or ($gamekind == 'hangman') or ($gamekind == 'cryptex')) { echo "\r\nwwwroot}/mod/game/preview.php?action=preview&"; echo "attemptid={$rec->id}&gamekind=$gamekind"; echo '&update='.$update."&q={$game->id}\">"; echo ''.get_string( 'preview', 'game').''; } echo '
'; if (($gamekind == 'cross') or ($gamekind == 'sudoku') or ($gamekind == 'hangman') or ($gamekind == 'cryptex') ) { echo "\r\nwwwroot}/mod/game/preview.php?action=solution&". "attemptid={$rec->id}&gamekind={$gamekind}&update=$update&&". "q={$game->id}\">"; echo ''.get_string( 'showsolution', 'game').''; } echo '
\r\n"; } } function game_ondeleteattempt( $game) { global $CFG, $DB; $attemptid = required_param('attemptid', PARAM_INT); $attempt = $DB->get_record( 'game_attempts', array( 'id' => $attemptid)); switch( $game->gamekind) { case 'bookquiz': $DB->delete_records( 'game_bookquiz_chapters', array( 'attemptid' => $attemptid)); break; } $DB->delete_records( 'game_queries', array( 'attemptid' => $attemptid)); $DB->delete_records( 'game_attempts', array( 'id' => $attemptid)); }