Example #1
0
/**
 * Delete a game attempt.
 */
function game_delete_attempt($attempt, $game)
{
    global $DB;
    if (is_numeric($attempt)) {
        if (!($attempt = $DB->get_record('game_attempts', 'id', $attempt))) {
            return;
        }
    }
    if ($attempt->gameid != $game->id) {
        debugging("Trying to delete attempt {$attempt->id} which belongs to game {$attempt->gameid} " . "but was passed gameid {$game->id}.");
        return;
    }
    $DB->delete_records('game_attempts', array('id' => $attempt->id));
    delete_attempt($attempt->id);
    // Search game_attempts for other instances by this user.
    // If none, then delete record for this game, this user from game_grades
    // else recalculate best grade
    $userid = $attempt->userid;
    if (!$DB->record_exists('game_attempts', array('userid' => $userid, 'gameid' => $game->id))) {
        $DB->delete_records('game_grades', array('userid' => $userid, 'gameid' => $game->id));
    } else {
        game_save_best_score($game);
    }
    game_update_grades($game, $userid);
}
Example #2
0
/**
 * Delete a quiz attempt.
 * @param mixed $attempt an integer attempt id or an attempt object (row of the quiz_attempts table).
 * @param object $quiz the quiz object.
 */
function quiz_delete_attempt($attempt, $quiz)
{
    if (is_numeric($attempt)) {
        if (!($attempt = get_record('quiz_attempts', 'id', $attempt))) {
            return;
        }
    }
    if ($attempt->quiz != $quiz->id) {
        debugging("Trying to delete attempt {$attempt->id} which belongs to quiz {$attempt->quiz} " . "but was passed quiz {$quiz->id}.");
        return;
    }
    delete_records('quiz_attempts', 'id', $attempt->id);
    delete_attempt($attempt->uniqueid);
    // Search quiz_attempts for other instances by this user.
    // If none, then delete record for this quiz, this user from quiz_grades
    // else recalculate best grade
    $userid = $attempt->userid;
    if (!record_exists('quiz_attempts', 'userid', $userid, 'quiz', $quiz->id)) {
        delete_records('quiz_grades', 'userid', $userid, 'quiz', $quiz->id);
    } else {
        quiz_save_best_grade($quiz, $userid);
    }
    quiz_update_grades($quiz, $userid);
}
Example #3
0
 /**
  * Display the report.
  */
 function display($quiz, $cm, $course)
 {
     global $CFG, $SESSION, $db, $QTYPES;
     // 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, $reportmode = "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) {
                 if ($attemptid && ($todelete = get_record('quiz_attempts', 'id', $attemptid))) {
                     add_to_log($course->id, 'quiz', 'delete attempt', 'report.php?id=' . $cm->id, $attemptid, $cm->id);
                     delete_records('quiz_attempts', 'id', $attemptid);
                     delete_attempt($todelete->uniqueid);
                     // Search quiz_attempts for other instances by this user.
                     // If none, then delete record for this quiz, this user from quiz_grades
                     // else recalculate best grade
                     $userid = $todelete->userid;
                     if (!record_exists('quiz_attempts', 'userid', $userid, 'quiz', $quiz->id)) {
                         delete_records('quiz_grades', 'userid', $userid, 'quiz', $quiz->id);
                     } else {
                         quiz_save_best_grade($quiz, $userid);
                     }
                 }
             }
             break;
     }
     // Set of format options for teacher-created content, for example overall feedback.
     $nocleanformatoptions = new stdClass();
     $nocleanformatoptions->noclean = true;
     // Set table options
     $noattempts = optional_param('noattempts', 0, PARAM_INT);
     $detailedmarks = optional_param('detailedmarks', 0, PARAM_INT);
     $pagesize = optional_param('pagesize', 10, PARAM_INT);
     $reporturl = $CFG->wwwroot . '/mod/quiz/report.php?mode=overview';
     $reporturlwithoptions = $reporturl . '&id=' . $cm->id . '&noattempts=' . $noattempts . '&detailedmarks=' . $detailedmarks . '&pagesize=' . $pagesize;
     // Print information on the number of existing attempts
     if (!$download) {
         //do not print notices when downloading
         if ($attemptnum = count_records('quiz_attempts', 'quiz', $quiz->id, 'preview', 0)) {
             $a = new stdClass();
             $a->attemptnum = $attemptnum;
             $a->studentnum = count_records_select('quiz_attempts', "quiz = '{$quiz->id}' AND preview = '0'", 'COUNT(DISTINCT userid)');
             $a->studentstring = $course->students;
             notify(get_string('numattempts', 'quiz', $a));
         }
     }
     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
     /// find out current groups mode
     if ($groupmode = groupmode($course, $cm)) {
         // Groups are being used
         if (!$download) {
             $currentgroup = setup_and_print_groups($course, $groupmode, $reporturlwithoptions);
         } else {
             $currentgroup = get_and_set_current_group($course, $groupmode);
         }
     } else {
         $currentgroup = get_and_set_current_group($course, $groupmode);
     }
     $hasfeedback = quiz_has_feedback($quiz->id) && $quiz->grade > 1.0E-7 && $quiz->sumgrades > 1.0E-7;
     if ($pagesize < 1) {
         $pagesize = 10;
     }
     // 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('checkbox', 'picture', 'fullname', 'timestart', 'timefinish', 'duration');
     $tableheaders = array(NULL, '', get_string('name'), get_string('startedon', 'quiz'), get_string('timecompleted', 'quiz'), get_string('attemptduration', 'quiz'));
     if ($quiz->grade and $quiz->sumgrades) {
         $tablecolumns[] = 'sumgrades';
         $tableheaders[] = get_string('grade', 'quiz') . '/' . $quiz->grade;
     }
     if ($detailedmarks) {
         // we want to display marks for all questions
         // Start by getting all questions
         $questionlist = quiz_questions_in_quiz($quiz->questions);
         $questionids = explode(',', $questionlist);
         $sql = "SELECT q.*, i.grade AS maxgrade, i.id AS instance" . "  FROM {$CFG->prefix}question q," . "       {$CFG->prefix}quiz_question_instances i" . " WHERE i.quiz = '{$quiz->id}' AND q.id = i.question" . "   AND q.id IN ({$questionlist})";
         if (!($questions = get_records_sql($sql))) {
             error('No questions found');
         }
         $number = 1;
         foreach ($questionids as $key => $id) {
             if ($questions[$id]->length) {
                 // Only print questions of non-zero length
                 $tablecolumns[] = '$' . $id;
                 $tableheaders[] = '#' . $number;
                 $questions[$id]->number = $number;
                 $number += $questions[$id]->length;
             } else {
                 // get rid of zero length questions
                 unset($questions[$id]);
                 unset($questionids[$key]);
             }
         }
     }
     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($reporturlwithoptions);
         $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 ($quiz->grade and $quiz->sumgrades) {
                 $headers[] = get_string('grade', 'quiz') . '/' . $quiz->grade;
             }
             if ($detailedmarks) {
                 foreach ($questionids as $id) {
                     $headers[] = '#' . $questions[$id]->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 ($quiz->grade and $quiz->sumgrades) {
                     $headers[] = get_string('grade', 'quiz') . '/' . $quiz->grade;
                 }
                 if ($detailedmarks) {
                     foreach ($questionids as $id) {
                         $headers[] = '#' . $questions[$id]->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 ($quiz->grade and $quiz->sumgrades) {
                         $headers .= "\t" . get_string('grade', 'quiz') . "/" . $quiz->grade;
                     }
                     if ($detailedmarks) {
                         foreach ($questionids as $id) {
                             $headers .= "\t#" . $questions[$id]->number;
                         }
                     }
                     if ($hasfeedback) {
                         $headers .= "\t" . get_string('feedback', 'quiz');
                     }
                     echo $headers . " \n";
                 }
             }
         }
     }
     $contextlists = get_related_contexts_string(get_context_instance(CONTEXT_COURSE, $course->id));
     // 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 ' . groups_members_join_sql() . 'JOIN ' . $CFG->prefix . 'quiz_attempts qa ON u.id = qa.userid AND qa.quiz = ' . $quiz->id;
             $where = ' WHERE ra.contextid ' . $contextlists . ' AND ' . groups_members_where_sql($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 ' . groups_members_join_sql() . 'LEFT JOIN ' . $CFG->prefix . 'quiz_attempts qa ON u.id = qa.userid AND qa.quiz = ' . $quiz->id;
                 $where = ' WHERE ra.contextid ' . $contextlists . ' AND ' . groups_members_where_sql($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
                 }
             }
         }
         $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()) {
             $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 .= ', grade ';
                         $from .= ' LEFT JOIN ' . $CFG->prefix . 'question_sessions qns ON qns.attemptid = qa.id ' . 'LEFT JOIN ' . $CFG->prefix . 'question_states qs ON qs.id = qns.newgraded ';
                         $where .= ' AND (' . sql_isnull('qns.questionid') . ' OR qns.questionid = ' . $qid . ')';
                         $newsort[] = 'grade ' . (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 (!empty($attempts) || !empty($noattempts)) {
         if ($attempts) {
             foreach ($attempts as $attempt) {
                 $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 = '<a class="dimmed" href="'.$CFG->wwwroot.'/user/view.php?id='.$attempt->userid.'&amp;course='.$course->id.'">'.fullname($attempt).'</a>';
                 //}
                 //else {
                 $userlink = '<a href="' . $CFG->wwwroot . '/user/view.php?id=' . $attempt->userid . '&amp;course=' . $course->id . '">' . fullname($attempt) . '</a>';
                 //}
                 if (!$download) {
                     $row = array('<input type="checkbox" name="attemptid[]" value="' . $attempt->attempt . '" />', $picture, $userlink, empty($attempt->attempt) ? '-' : '<a href="review.php?q=' . $quiz->id . '&amp;attempt=' . $attempt->attempt . '">' . userdate($attempt->timestart, $strtimeformat) . '</a>', empty($attempt->timefinish) ? '-' : '<a href="review.php?q=' . $quiz->id . '&amp;attempt=' . $attempt->attempt . '">' . userdate($attempt->timefinish, $strtimeformat) . '</a>', empty($attempt->attempt) ? '-' : (empty($attempt->timefinish) ? get_string('unfinished', 'quiz') : format_time($attempt->duration)));
                 } else {
                     $row = array(fullname($attempt), empty($attempt->attempt) ? '-' : userdate($attempt->timestart, $strtimeformat), empty($attempt->timefinish) ? '-' : userdate($attempt->timefinish, $strtimeformat), empty($attempt->attempt) ? '-' : (empty($attempt->timefinish) ? get_string('unfinished', 'quiz') : format_time($attempt->duration)));
                 }
                 if ($quiz->grade and $quiz->sumgrades) {
                     if (!$download) {
                         $row[] = $attempt->sumgrades === NULL ? '-' : '<a href="review.php?q=' . $quiz->id . '&amp;attempt=' . $attempt->attempt . '">' . round($attempt->sumgrades / $quiz->sumgrades * $quiz->grade, $quiz->decimalpoints) . '</a>';
                     } else {
                         $row[] = $attempt->sumgrades === NULL ? '-' : round($attempt->sumgrades / $quiz->sumgrades * $quiz->grade, $quiz->decimalpoints);
                     }
                 }
                 if ($detailedmarks) {
                     if (empty($attempt->attempt)) {
                         foreach ($questionids as $questionid) {
                             $row[] = '-';
                         }
                     } else {
                         foreach ($questionids as $questionid) {
                             if ($gradedstateid = get_field('question_sessions', 'newgraded', 'attemptid', $attempt->attemptuniqueid, 'questionid', $questionid)) {
                                 $grade = round(get_field('question_states', 'grade', 'id', $gradedstateid), $quiz->decimalpoints);
                             } else {
                                 $grade = '--';
                             }
                             if (!$download) {
                                 $row[] = link_to_popup_window('/mod/quiz/reviewquestion.php?state=' . $gradedstateid . '&amp;number=' . $questions[$questionid]->number, 'reviewquestion', $grade, 450, 650, $strreviewquestion, 'none', true);
                             } else {
                                 $row[] = $grade;
                             }
                         }
                     }
                 }
                 if ($hasfeedback) {
                     if ($attempt->timefinish) {
                         if (empty($attempt->feedbacktext)) {
                             $attempt->feedbacktext = '';
                         }
                         $row[] = format_text($attempt->feedbacktext, FORMAT_MOODLE, $nocleanformatoptions);
                     } else {
                         $row[] = '-';
                     }
                 }
                 if (!$download) {
                     $table->add_data($row);
                 } else {
                     if ($download == 'Excel' or $download == 'ODS') {
                         $colnum = 0;
                         foreach ($row as $item) {
                             $myxls->write($rownum, $colnum, $item, $format);
                             $colnum++;
                         }
                         $rownum++;
                     } else {
                         if ($download == 'CSV') {
                             $text = implode("\t", $row);
                             echo $text . " \n";
                         }
                     }
                 }
             }
         }
         if (!$download) {
             // Start form
             echo '<div id="tablecontainer">';
             echo '<form id="attemptsform" method="post" action="' . $reporturlwithoptions . '" onsubmit="var menu = document.getElementById(\'menuaction\'); return (menu.options[menu.selectedIndex].value == \'delete\' ? confirm(\'' . $strreallydel . '\') : true);">';
             echo '<div>';
             // Print table
             $table->print_html();
             // Prepare list of available options.
             $options = array();
             if (has_capability('mod/quiz:deleteattempts', $context)) {
                 $options['delete'] = get_string('delete');
             }
             // Print "Select all" etc.
             if (!empty($attempts) && !empty($options)) {
                 echo '<table id="commands">';
                 echo '<tr><td>';
                 echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">' . get_string('selectall', 'quiz') . '</a> / ';
                 echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">' . get_string('selectnone', 'quiz') . '</a> ';
                 echo '&nbsp;&nbsp;';
                 echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
                 echo '<noscript id="noscriptmenuaction" style="display: inline;"><div>';
                 echo '<input type="submit" value="' . get_string('go') . '" /></div></noscript>';
                 echo '<script type="text/javascript">' . "\n<!--\n" . 'document.getElementById("noscriptmenuaction").style.display = "none";' . "\n-->\n" . '</script>';
                 echo '</td></tr></table>';
             }
             // Close form
             echo '</div>';
             echo '</form></div>';
             if (!empty($attempts)) {
                 echo '<table class="boxaligncenter"><tr>';
                 $options = array();
                 $options["id"] = $cm->id;
                 $options["q"] = $quiz->id;
                 $options['sesskey'] = sesskey();
                 $options["noheader"] = "yes";
                 $options['noattempts'] = $noattempts;
                 $options['detailedmarks'] = $detailedmarks;
                 echo '<td>';
                 $options["download"] = "ODS";
                 print_single_button($reporturl, $options, get_string("downloadods"));
                 echo "</td>\n";
                 echo '<td>';
                 $options["download"] = "Excel";
                 print_single_button($reporturl, $options, get_string("downloadexcel"));
                 echo "</td>\n";
                 echo '<td>';
                 $options["download"] = "CSV";
                 print_single_button($reporturl, $options, get_string("downloadtext"));
                 echo "</td>\n";
                 echo "<td>";
                 helpbutton('overviewdownload', get_string('overviewdownload', 'quiz_overview'), 'quiz');
                 echo "</td>\n";
                 echo '</tr></table>';
             }
         } else {
             if ($download == 'Excel' or $download == 'ODS') {
                 $workbook->close();
                 exit;
             } else {
                 if ($download == 'CSV') {
                     exit;
                 }
             }
         }
     } else {
         if (!$download) {
             $table->print_html();
         }
     }
     // Print display options
     echo '<div class="controls">';
     echo '<form id="options" action="' . $reporturl . '" method="get">';
     echo '<div>';
     echo '<p>' . get_string('displayoptions', 'quiz') . ': </p>';
     echo '<input type="hidden" name="id" value="' . $cm->id . '" />';
     echo '<input type="hidden" name="q" value="' . $quiz->id . '" />';
     echo '<input type="hidden" name="noattempts" value="0" />';
     echo '<input type="hidden" name="detailedmarks" value="0" />';
     echo '<table id="overview-options" class="boxaligncenter">';
     echo '<tr align="left">';
     echo '<td><label for="pagesize">' . get_string('pagesize', 'quiz') . '</label></td>';
     echo '<td><input type="text" id="pagesize" name="pagesize" size="3" value="' . $pagesize . '" /></td>';
     echo '</tr>';
     echo '<tr align="left">';
     echo '<td colspan="2">';
     $options = array(0 => get_string('attemptsonly', 'quiz_overview', $course->students));
     if ($course->id != SITEID) {
         $options[1] = get_string('noattemptsonly', 'quiz_overview', $course->students);
         $options[2] = get_string('allstudents', 'quiz_overview', $course->students);
         $options[3] = get_string('allattempts', 'quiz_overview');
     }
     choose_from_menu($options, 'noattempts', $noattempts, '');
     echo '</td></tr>';
     echo '<tr align="left">';
     echo '<td colspan="2"><input type="checkbox" id="checkdetailedmarks" name="detailedmarks" ' . ($detailedmarks ? 'checked="checked" ' : '') . 'value="1" /> <label for="checkdetailedmarks">' . get_string('showdetailedmarks', 'quiz') . '</label> ';
     echo '</td></tr>';
     echo '<tr><td colspan="2" align="center">';
     echo '<input type="submit" value="' . get_string('go') . '" />';
     echo '</td></tr></table>';
     echo '</div>';
     echo '</form>';
     echo '</div>';
     echo "\n";
     return true;
 }
Example #4
0
/**
 * Delete all the attempts belonging to a quiz.
 *
 * @global stdClass
 * @global object
 * @param object $quiz The quiz object.
 */
function quiz_delete_all_attempts($quiz) {
    global $CFG, $DB;
    require_once($CFG->libdir . '/questionlib.php');
    $attempts = $DB->get_records('quiz_attempts', array('quiz' => $quiz->id));
    foreach ($attempts as $attempt) {
        delete_attempt($attempt->uniqueid);
    }
    $DB->delete_records('quiz_attempts', array('quiz' => $quiz->id));
    $DB->delete_records('quiz_grades', array('quiz' => $quiz->id));
}
Example #5
0
 if ($type == 'simple') {
     // Getting old attempt in order to decrees the total score.
     $old_result = $objExercise->manageAnswers($exe_id, $my_question_id, null, 'exercise_show', array(), false, true, false);
     // Removing old score.
     $total_score = $total_score - $old_result['score'];
     if ($debug) {
         error_log("old score = " . $old_result['score']);
         error_log("total_score = " . $total_score . "");
     }
 }
 // Deleting old attempt
 if (isset($attempt_list) && !empty($attempt_list[$my_question_id])) {
     if ($debug) {
         error_log("delete_attempt  exe_id : {$exe_id}, my_question_id: {$my_question_id}");
     }
     delete_attempt($exe_id, api_get_user_id(), $course_id, $session_id, $my_question_id);
     if ($objQuestionTmp->type == HOT_SPOT) {
         delete_attempt_hotspot($exe_id, api_get_user_id(), $course_id, $my_question_id);
     }
     if (isset($attempt_list[$my_question_id]) && isset($attempt_list[$my_question_id]['marks'])) {
         $total_score -= $attempt_list[$my_question_id]['marks'];
     }
 }
 // We're inside *one* question. Go through each possible answer for this question
 $result = $objExercise->manageAnswers($exe_id, $my_question_id, $my_choice, 'exercise_result', $hot_spot_coordinates, true, false, false, $hotspot_delineation_result);
 //Adding the new score
 $total_score += $result['score'];
 if ($debug) {
     error_log("total_score: {$total_score} ");
     error_log("total_weight: {$total_weight} ");
 }
Example #6
0
/**
 * Actual implementation of the rest coures functionality, delete all the
 * quiz attempts for course $data->courseid, if $data->reset_quiz_attempts is
 * set and true.
 *
 * Also, move the quiz open and close dates, if the course start date is changing.
 *
 * @param $data the data submitted from the reset course forum.
 * @param $showfeedback whether to output progress information as the reset
 *      progresses.
 */
function quiz_delete_userdata($data, $showfeedback = true)
{
    global $CFG;
    /// Delete attempts.
    if (!empty($data->reset_quiz_attempts)) {
        $conditiononquizids = 'quiz IN (SELECT id FROM ' . $CFG->prefix . 'quiz q WHERE q.course = ' . $data->courseid . ')';
        $attemptids = get_records_select('quiz_attempts', $conditiononquizids, '', 'id, uniqueid');
        if ($attemptids) {
            if ($showfeedback) {
                echo '<div class="notifysuccess">', get_string('deletingquestionattempts', 'quiz');
                $divider = ': ';
            }
            foreach ($attemptids as $attemptid) {
                delete_attempt($attemptid->uniqueid);
                if ($showfeedback) {
                    echo $divider, $attemptid->uniqueid;
                    $divider = ', ';
                }
            }
            if ($showfeedback) {
                echo "</div><br />\n";
            }
        }
        if (delete_records_select('quiz_grades', $conditiononquizids) && $showfeedback) {
            notify(get_string('gradesdeleted', 'quiz'), 'notifysuccess');
        }
        if (delete_records_select('quiz_attempts', $conditiononquizids) && $showfeedback) {
            notify(get_string('attemptsdeleted', 'quiz'), 'notifysuccess');
        }
    }
    /// Update open and close dates
    if (!empty($data->reset_start_date)) {
        /// Work out offset.
        $olddate = get_field('course', 'startdate', 'id', $data->courseid);
        $olddate = usergetmidnight($olddate);
        // time part of $olddate should be zero
        $newdate = make_timestamp($data->startyear, $data->startmonth, $data->startday);
        $interval = $newdate - $olddate;
        /// Apply it to quizzes with an open or close date.
        $success = true;
        begin_sql();
        $success = $success && execute_sql("UPDATE {$CFG->prefix}quiz\n                    SET timeopen = timeopen + {$interval}\n                    WHERE course = {$data->courseid} AND timeopen <> 0", false);
        $success = $success && execute_sql("UPDATE {$CFG->prefix}quiz\n                    SET timeclose = timeclose + {$interval}\n                    WHERE course = {$data->courseid} AND timeclose <> 0", false);
        if ($success) {
            commit_sql();
            if ($showfeedback) {
                notify(get_string('openclosedatesupdated', 'quiz'), 'notifysuccess');
            }
        } else {
            rollback_sql();
        }
    }
}
Example #7
0
/**
 * Delete a quiz attempt.
 * @param mixed $attempt an integer attempt id or an attempt object (row of the quiz_attempts table).
 * @param object $quiz the quiz object.
 */
function quiz_delete_attempt($attempt, $quiz) {
    global $DB;
    if (is_numeric($attempt)) {
        if (!$attempt = $DB->get_record('quiz_attempts', array('id' => $attempt))) {
            return;
        }
    }

    if ($attempt->quiz != $quiz->id) {
        debugging("Trying to delete attempt $attempt->id which belongs to quiz $attempt->quiz " .
                "but was passed quiz $quiz->id.");
        return;
    }

    $DB->delete_records('quiz_attempts', array('id' => $attempt->id));
    delete_attempt($attempt->uniqueid);

    // Search quiz_attempts for other instances by this user.
    // If none, then delete record for this quiz, this user from quiz_grades
    // else recalculate best grade

    $userid = $attempt->userid;
    if (!$DB->record_exists('quiz_attempts', array('userid' => $userid, 'quiz' => $quiz->id))) {
        $DB->delete_records('quiz_grades', array('userid' => $userid,'quiz' => $quiz->id));
    } else {
        quiz_save_best_grade($quiz, $userid);
    }

    quiz_update_grades($quiz, $userid);
}
Example #8
0
    // so we set a finish time on the current attempt (if any).
    // It will then automatically be deleted below
    set_field('quiz_attempts', 'timefinish', $timestamp, 'quiz', $quiz->id, 'userid', $USER->id);
}
$attempt = quiz_get_user_attempt_unfinished($quiz->id, $USER->id);
$newattempt = false;
if (!$attempt) {
    // Check if this is a preview request from a teacher
    // in which case the previous previews should be deleted
    if (has_capability('mod/quiz:preview', $context)) {
        if ($oldattempts = get_records_select('quiz_attempts', "quiz = '{$quiz->id}'\n             AND userid = '{$USER->id}'")) {
            delete_records('quiz_attempts', 'quiz', $quiz->id, 'userid', $USER->id);
            delete_records('quiz_grades', 'quiz', $quiz->id, 'userid', $USER->id);
            foreach ($oldattempts as $oldattempt) {
                // there should only be one but we loop just in case
                delete_attempt($oldattempt->uniqueid);
            }
        }
    }
    $newattempt = true;
    // Start a new attempt and initialize the question sessions
    $attempt = quiz_create_attempt($quiz, $attemptnumber);
    // If this is an attempt by a teacher mark it as a preview
    if (has_capability('mod/quiz:preview', $context)) {
        $attempt->preview = 1;
    }
    // Save the attempt
    if (!($attempt->id = insert_record('quiz_attempts', $attempt))) {
        error('Could not create new attempt');
    }
    // make log entries
Example #9
0
function reader_delete_attempt($attempt, $reader)
{
    global $DB;
    if (is_numeric($attempt)) {
        if (!($attempt = $DB->get_record('reader_attempts', array('id' => $attempt)))) {
            return;
        }
    }
    if ($attempt->reader != $reader->id) {
        debugging("Trying to delete attempt {$attempt->id} which belongs to reader {$attempt->reader} " . "but was passed reader {$reader->id}.");
        return;
    }
    $DB->delete_records('reader_attempts', array('id' => $attempt->id));
    delete_attempt($attempt->uniqueid);
    // Search reader_attempts for other instances by this user.
    // If none, then delete record for this reader, this user from reader_grades
    // else recalculate best grade
    $userid = $attempt->userid;
    if (!record_exists('reader_attempts', 'userid', $userid, 'reader', $reader->id)) {
        $DB->delete_records('reader_grades', array('userid' => $userid, 'reader' => $reader->id));
    } else {
        reader_save_best_grade($reader, $userid);
    }
    reader_update_grades($reader, $userid);
}