예제 #1
0
/**
 * Find the activity grade for a given user and attempt
 * @param int $rcontentid
 * @param int $userid
 * @param int $attempt
 * @param int $unitid
 * @param int $activityid
 * @return string grade(mingrade/maxgrade)
 */
function rcontent_grade_user_attempt($rcontentid, $userid, $attempt = 1, $unitid = '', $activityid = '', $idgrade = '', $starttime = '')
{
    global $CFG, $DB;
    // Take the grade of the activity
    if (!empty($idgrade)) {
        $sql = "id = {$idgrade}";
    } else {
        $sql = "userid={$userid} AND rcontentid={$rcontentid} AND attempt={$attempt}";
        $sql .= empty($unitid) ? " AND unitid=0" : " AND unitid={$unitid}";
        $sql .= empty($activityid) ? " AND activityid=0" : " AND activityid={$activityid}";
        $sql .= empty($starttime) ? "" : " AND starttime = {$starttime}";
    }
    $numgrades = $DB->count_records_select('rcontent_grades', $sql);
    $return = new stdClass();
    $return->id = "";
    $return->grade = "";
    $return->justgrade = "";
    $return->range = "";
    $return->maxattempts = "";
    $return->status = rcontent_grade_calculate_status($rcontentid, $userid, $attempt, $unitid, $activityid, $starttime);
    $return->url = "";
    $return->comments = "";
    $return->justcomments = "";
    if ($numgrades == 0) {
        return $return;
    }
    if ($numgrades == 1) {
        $grade = $DB->get_record_select('rcontent_grades', $sql);
    } else {
        if ($whatgrade = $DB->get_field('rcontent', 'whatgrade', array('id' => $rcontentid))) {
            switch ($whatgrade) {
                case RCONTENT_FIRSTATTEMPT:
                    $grade = $DB->get_record_sql("SELECT * FROM {rcontent_grades} WHERE {$sql} AND timecreated = (SELECT MIN(timecreated) FROM {rcontent_grades} WHERE " . $sql . ")", array(), IGNORE_MULTIPLE);
                    break;
                case RCONTENT_LASTATTEMPT:
                    $grade = $DB->get_record_sql("SELECT * FROM {rcontent_grades} WHERE {$sql} AND timecreated = (SELECT MAX(timecreated) FROM {rcontent_grades} WHERE " . $sql . ")", array(), IGNORE_MULTIPLE);
                    break;
                case RCONTENT_HIGHESTATTEMPT:
                    $grade = $DB->get_record_sql("SELECT * FROM {rcontent_grades} WHERE {$sql} AND grade=(SELECT max(grade) as grade FROM {rcontent_grades} WHERE " . $sql . ")", array(), IGNORE_MULTIPLE);
                    break;
                case RCONTENT_AVERAGEATTEMPT:
                    $grade_grade = $DB->get_record_sql("SELECT round(avg(grade)) as grade FROM {rcontent_grades} WHERE {$sql}", array(), IGNORE_MULTIPLE);
                    $grade = $DB->get_records_select('rcontent_grades', $sql);
                    $grade = array_pop($grade);
                    $grade->grade = $grade_grade->grade;
                    break;
            }
        }
    }
    $return->id = $grade->id;
    $return->grade = '<div id="rcontent_grade_' . $userid . '_' . $grade->id . '">' . $grade->grade . '</div>';
    $return->totaltime = $grade->totaltime;
    $return->justgrade = round($grade->grade, 2);
    $return->range = "({$grade->mingrade}-{$grade->maxgrade})";
    $return->maxattempts = '(' . $grade->maxattempts . ')';
    if ($grade->urlviewresults != "") {
        $return->url = rcontent_get_urlviewresults_link($grade->urlviewresults);
    }
    if ($grade->comments != "") {
        $return->comments = '<div id="rcontent_comments_' . $userid . '_' . $grade->id . '">';
        if (strlen($grade->comments) > 30) {
            $return->comments .= '<span title="' . $grade->comments . '">' . substr($grade->comments, 0, 27) . '...</span>';
        } else {
            $return->comments .= $grade->comments;
        }
        $return->comments .= '</div>';
        $return->justcomments = $grade->comments;
    } else {
        $return->comments = '<div id="rcontent_comments_' . $userid . '_' . $grade->id . '"></div>';
    }
    return $return;
}
예제 #2
0
 foreach ($attempts as $at) {
     $row = array();
     if ($at->attempt == $attempt) {
         $row[] = $at->attempt;
     } else {
         $row[] = rcontent_report_get_attempt_link($id, $at);
     }
     if (!empty($at->starttime)) {
         $row[] = userdate($at->starttime, get_string('strftimedaydatetime'));
     } else {
         $row[] = "";
     }
     $row[] = rcontent_get_ellapsed_time($at->totaltime);
     $grade = rcontent_grade_user_attempt($rcontent->id, $user, $at->attempt, $unitid, $activityid);
     // Take status value from the 1st position of the array
     $status = rcontent_grade_calculate_status($rcontent->id, $at->userid, $at->attempt, $at->unitid, $at->activityid);
     if (!empty($status[0])) {
         $row[] = get_string($status[0], 'rcontent');
     } else {
         $row[] = '';
     }
     // Show grade
     $row[] = '<div id="rcontent_grade_' . $at->userid . '_' . $at->id . '">' . round($at->grade, 2) . '</div>';
     // Set grade range
     $range = "({$at->mingrade}-{$at->maxgrade})";
     // Show comments
     $comments = '<div id="rcontent_comments_' . $at->userid . '_' . $at->id . '">';
     if (strlen($at->comments) > 30) {
         $comments .= '<span title="' . $at->comments . '">' . substr($at->comments, 0, 27) . '...</span>';
     } else {
         $comments .= $at->comments;