Пример #1
0
function printscore($sc, $qsetid, $seed)
{
    $poss = 1;
    if (strpos($sc, '~') === false) {
        $sc = str_replace('-1', 'N/A', $sc);
        $out = "{$sc} out of {$poss}";
        $pts = $sc;
        if (!is_numeric($pts)) {
            $pts = 0;
        }
    } else {
        $query = "SELECT control FROM imas_questionset WHERE id='{$qsetid}'";
        $result = mysql_query($query) or die("Query failed: {$query}: " . mysql_error());
        $control = mysql_result($result, 0, 0);
        $ptposs = getansweights($control, $seed);
        $weightsum = array_sum($ptposs);
        if ($weightsum > 1.1) {
            $poss = $weightsum;
        } else {
            $poss = count($ptposs);
        }
        for ($i = 0; $i < count($ptposs) - 1; $i++) {
            $ptposs[$i] = round($ptposs[$i] / $weightsum * $poss, 2);
        }
        //adjust for rounding
        $diff = $poss - array_sum($ptposs);
        $ptposs[count($ptposs) - 1] += $diff;
        $pts = getpts($sc, $poss);
        $sc = str_replace('-1', 'N/A', $sc);
        //$sc = str_replace('~',', ',$sc);
        $scarr = explode('~', $sc);
        foreach ($scarr as $k => $v) {
            $v = round($v * $poss, 2);
            if ($ptposs[$k] == 0) {
                $pm = 'gchk';
            } else {
                if (!is_numeric($v) || $v == 0) {
                    $pm = 'redx';
                } else {
                    if (abs($v - $ptposs[$k]) < 0.011) {
                        $pm = 'gchk';
                    } else {
                        $pm = 'ychk';
                    }
                }
            }
            $bar = "<img src=\"{$imasroot}/img/{$pm}.gif\" />";
            $scarr[$k] = "{$bar} {$v}/{$ptposs[$k]}";
        }
        $sc = implode(', ', $scarr);
        //$ptposs = implode(', ',$ptposs);
        $out = "{$pts} out of {$poss} (parts: {$sc})";
    }
    $bar = '<span class="scorebarholder">';
    if ($poss == 0) {
        $w = 30;
    } else {
        $w = round(30 * $pts / $poss);
    }
    if ($w == 0) {
        $w = 1;
    }
    if ($w < 15) {
        $color = "#f" . dechex(floor(16 * $w / 15)) . "0";
    } else {
        if ($w == 15) {
            $color = '#ff0';
        } else {
            $color = "#" . dechex(floor(16 * (2 - $w / 15))) . "f0";
        }
    }
    $bar .= '<span class="scorebarinner" style="background-color:' . $color . ';width:' . $w . 'px;">&nbsp;</span></span> ';
    return $bar . $out;
}
Пример #2
0
function gbtable()
{
    global $cid, $isteacher, $istutor, $tutorid, $userid, $catfilter, $secfilter, $timefilter, $lnfilter, $isdiag, $sel1name, $sel2name, $canviewall, $lastlogin, $logincnt, $hidelocked, $latepasshrs;
    if ($canviewall && func_num_args() > 0) {
        $limuser = func_get_arg(0);
    } else {
        if (!$canviewall) {
            $limuser = $userid;
        } else {
            $limuser = 0;
        }
    }
    if (!isset($lastlogin)) {
        $lastlogin = 0;
    }
    if (!isset($logincnt)) {
        $logincnt = 0;
    }
    $category = array();
    $gb = array();
    $ln = 0;
    //Pull Gradebook Scheme info
    $query = "SELECT useweights,orderby,defaultcat,usersort FROM imas_gbscheme WHERE courseid='{$cid}'";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    list($useweights, $orderby, $defaultcat, $usersort) = mysql_fetch_row($result);
    if ($useweights == 2) {
        $useweights = 0;
    }
    //use 0 mode for calculation of totals
    if (isset($GLOBALS['setorderby'])) {
        $orderby = $GLOBALS['setorderby'];
    }
    //Build user ID headers
    $gb[0][0][0] = "Name";
    if ($isdiag) {
        $gb[0][0][1] = "ID";
        $gb[0][0][2] = "Term";
        $gb[0][0][3] = ucfirst($sel1name);
        $gb[0][0][4] = ucfirst($sel2name);
    } else {
        $gb[0][0][1] = "Username";
    }
    $query = "SELECT count(id) FROM imas_students WHERE imas_students.courseid='{$cid}' AND imas_students.section IS NOT NULL";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    if (mysql_result($result, 0, 0) > 0) {
        $hassection = true;
    } else {
        $hassection = false;
    }
    $query = "SELECT count(id) FROM imas_students WHERE imas_students.courseid='{$cid}' AND imas_students.code IS NOT NULL";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    if (mysql_result($result, 0, 0) > 0) {
        $hascode = true;
    } else {
        $hascode = false;
    }
    if ($hassection && !$isdiag) {
        $gb[0][0][] = "Section";
    }
    if ($hascode) {
        $gb[0][0][] = "Code";
    }
    if ($lastlogin) {
        $gb[0][0][] = "Last Login";
    }
    if ($logincnt) {
        $gb[0][0][] = "Login Count";
    }
    //orderby 10: course order (11 cat first), 12: course order rev (13 cat first)
    if ($orderby >= 10 && $orderby <= 13) {
        $query = "SELECT itemorder FROM imas_courses WHERE id='{$cid}'";
        $result = mysql_query($query) or die("Query failed : {$query}" . mysql_error());
        $courseitemorder = unserialize(mysql_result($result, 0, 0));
        $courseitemsimporder = array();
        function flattenitems($items, &$addto)
        {
            foreach ($items as $item) {
                if (is_array($item)) {
                    flattenitems($item['items'], $addto);
                } else {
                    $addto[] = $item;
                }
            }
        }
        flattenitems($courseitemorder, $courseitemsimporder);
        $courseitemsimporder = array_flip($courseitemsimporder);
        $courseitemsassoc = array();
        $query = "SELECT id,itemtype,typeid FROM imas_items WHERE courseid='{$cid}'";
        $result = mysql_query($query) or die("Query failed : {$query}" . mysql_error());
        while ($row = mysql_fetch_row($result)) {
            if (!isset($courseitemsimporder[$row[0]])) {
                //error catch items not in course.itemorder
                $courseitemsassoc[$row[1] . $row[2]] = 999 + count($courseitemsassoc);
            } else {
                $courseitemsassoc[$row[1] . $row[2]] = $courseitemsimporder[$row[0]];
            }
        }
    }
    //Pull Assessment Info
    $now = time();
    $query = "SELECT id,name,defpoints,deffeedback,timelimit,minscore,startdate,enddate,itemorder,gbcategory,cntingb,avail,groupsetid,allowlate FROM imas_assessments WHERE courseid='{$cid}' AND avail>0 ";
    if (!$canviewall) {
        $query .= "AND cntingb>0 ";
    }
    if ($istutor) {
        $query .= "AND tutoredit<2 ";
    }
    if (!$isteacher) {
        //$query .= "AND startdate<$now ";
    }
    if ($catfilter > -1) {
        $query .= "AND gbcategory='{$catfilter}' ";
    }
    $query .= "ORDER BY enddate,name";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    $overallpts = 0;
    $now = time();
    $kcnt = 0;
    $assessments = array();
    $grades = array();
    $discuss = array();
    $exttools = array();
    $timelimits = array();
    $minscores = array();
    $assessmenttype = array();
    $startdate = array();
    $enddate = array();
    $tutoredit = array();
    $isgroup = array();
    $avail = array();
    $sa = array();
    $category = array();
    $name = array();
    $possible = array();
    $courseorder = array();
    $allowlate = array();
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $assessments[$kcnt] = $line['id'];
        if (isset($courseitemsassoc)) {
            $courseorder[$kcnt] = $courseitemsassoc['Assessment' . $line['id']];
        }
        $timelimits[$kcnt] = $line['timelimit'];
        $minscores[$kcnt] = $line['minscore'];
        $deffeedback = explode('-', $line['deffeedback']);
        $assessmenttype[$kcnt] = $deffeedback[0];
        $sa[$kcnt] = $deffeedback[1];
        if ($line['avail'] == 2) {
            $line['startdate'] = 0;
            $line['enddate'] = 2000000000;
        }
        $enddate[$kcnt] = $line['enddate'];
        $startdate[$kcnt] = $line['startdate'];
        if ($now < $line['startdate']) {
            $avail[$kcnt] = 2;
        } else {
            if ($now < $line['enddate']) {
                $avail[$kcnt] = 1;
            } else {
                $avail[$kcnt] = 0;
            }
        }
        $category[$kcnt] = $line['gbcategory'];
        $isgroup[$kcnt] = $line['groupsetid'] != 0;
        $name[$kcnt] = $line['name'];
        $cntingb[$kcnt] = $line['cntingb'];
        //0: ignore, 1: count, 2: extra credit, 3: no count but show
        if ($deffeedback[0] == 'Practice') {
            //set practice as no count in gb
            $cntingb[$kcnt] = 3;
        }
        $aitems = explode(',', $line['itemorder']);
        if ($line['allowlate'] > 0) {
            $allowlate[$kcnt] = $line['allowlate'];
        }
        $k = 0;
        $atofind = array();
        foreach ($aitems as $v) {
            if (strpos($v, '~') !== FALSE) {
                $sub = explode('~', $v);
                if (strpos($sub[0], '|') === false) {
                    //backwards compat
                    $atofind[$k] = $sub[0];
                    $aitemcnt[$k] = 1;
                    $k++;
                } else {
                    $grpparts = explode('|', $sub[0]);
                    if ($grpparts[0] == count($sub) - 1) {
                        //handle diff point values in group if n=count of group
                        for ($i = 1; $i < count($sub); $i++) {
                            $atofind[$k] = $sub[$i];
                            $aitemcnt[$k] = 1;
                            $k++;
                        }
                    } else {
                        $atofind[$k] = $sub[1];
                        $aitemcnt[$k] = $grpparts[0];
                        $k++;
                    }
                }
            } else {
                $atofind[$k] = $v;
                $aitemcnt[$k] = 1;
                $k++;
            }
        }
        $query = "SELECT points,id FROM imas_questions WHERE assessmentid='{$line['id']}'";
        $result2 = mysql_query($query) or die("Query failed : {$query}: " . mysql_error());
        $totalpossible = 0;
        while ($r = mysql_fetch_row($result2)) {
            if (($k = array_search($r[1], $atofind)) !== false) {
                //only use first item from grouped questions for total pts
                if ($r[0] == 9999) {
                    $totalpossible += $aitemcnt[$k] * $line['defpoints'];
                    //use defpoints
                } else {
                    $totalpossible += $aitemcnt[$k] * $r[0];
                    //use points from question
                }
            }
        }
        $possible[$kcnt] = $totalpossible;
        $kcnt++;
    }
    //Pull Offline Grade item info
    $query = "SELECT * from imas_gbitems WHERE courseid='{$cid}' ";
    if (!$canviewall) {
        $query .= "AND showdate<{$now} ";
    }
    if (!$canviewall) {
        $query .= "AND cntingb>0 ";
    }
    if ($istutor) {
        $query .= "AND tutoredit<2 ";
    }
    if ($catfilter > -1) {
        $query .= "AND gbcategory='{$catfilter}' ";
    }
    $query .= "ORDER BY showdate";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $grades[$kcnt] = $line['id'];
        $assessmenttype[$kcnt] = "Offline";
        $category[$kcnt] = $line['gbcategory'];
        $enddate[$kcnt] = $line['showdate'];
        $startdate[$kcnt] = $line['showdate'];
        if ($now < $line['showdate']) {
            $avail[$kcnt] = 2;
        } else {
            $avail[$kcnt] = 0;
        }
        $possible[$kcnt] = $line['points'];
        $name[$kcnt] = $line['name'];
        $cntingb[$kcnt] = $line['cntingb'];
        $tutoredit[$kcnt] = $line['tutoredit'];
        if (isset($courseitemsassoc)) {
            $courseorder[$kcnt] = 2000 + $kcnt;
        }
        $kcnt++;
    }
    //Pull Discussion Grade info
    $query = "SELECT id,name,gbcategory,startdate,enddate,replyby,postby,points,cntingb,avail FROM imas_forums WHERE courseid='{$cid}' AND points>0 AND avail>0 ";
    if (!$canviewall) {
        $query .= "AND startdate<{$now} ";
    }
    if ($istutor) {
        $query .= "AND tutoredit<2 ";
    }
    if ($catfilter > -1) {
        $query .= "AND gbcategory='{$catfilter}' ";
    }
    $query .= "ORDER BY enddate,postby,replyby,startdate";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $discuss[$kcnt] = $line['id'];
        $assessmenttype[$kcnt] = "Discussion";
        $category[$kcnt] = $line['gbcategory'];
        if ($line['avail'] == 2) {
            $line['startdate'] = 0;
            $line['enddate'] = 2000000000;
        }
        $enddate[$kcnt] = $line['enddate'];
        $startdate[$kcnt] = $line['startdate'];
        if ($now < $line['startdate']) {
            $avail[$kcnt] = 2;
        } else {
            if ($now < $line['enddate']) {
                $avail[$kcnt] = 1;
                if ($line['replyby'] > 0 && $line['replyby'] < 2000000000) {
                    if ($line['postby'] > 0 && $line['postby'] < 2000000000) {
                        if ($now > $line['replyby'] && $now > $line['postby']) {
                            $avail[$kcnt] = 0;
                            $enddate[$kcnt] = max($line['replyby'], $line['postby']);
                        }
                    } else {
                        if ($now > $line['replyby']) {
                            $avail[$kcnt] = 0;
                            $enddate[$kcnt] = $line['replyby'];
                        }
                    }
                } else {
                    if ($line['postby'] > 0 && $line['postby'] < 2000000000) {
                        if ($now > $line['postby']) {
                            $avail[$kcnt] = 0;
                            $enddate[$kcnt] = $line['postby'];
                        }
                    }
                }
            } else {
                $avail[$kcnt] = 0;
            }
        }
        $possible[$kcnt] = $line['points'];
        $name[$kcnt] = $line['name'];
        $cntingb[$kcnt] = $line['cntingb'];
        if (isset($courseitemsassoc)) {
            $courseorder[$kcnt] = $courseitemsassoc['Forum' . $line['id']];
        }
        $kcnt++;
    }
    //Pull External Tools info
    $query = "SELECT id,title,text,startdate,enddate,points,avail FROM imas_linkedtext WHERE courseid='{$cid}' AND points>0 AND avail>0 ";
    if (!$canviewall) {
        $query .= "AND startdate<{$now} ";
    }
    /*if ($istutor) {
    		$query .= "AND tutoredit<2 ";
    	}
    	if ($catfilter>-1) {
    		$query .= "AND gbcategory='$catfilter' ";
    	}*/
    $query .= "ORDER BY enddate,startdate";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        if (substr($line['text'], 0, 8) != 'exttool:') {
            continue;
        }
        $toolparts = explode('~~', substr($line['text'], 8));
        if (isset($toolparts[3])) {
            $thisgbcat = $toolparts[3];
            $thiscntingb = $toolparts[4];
            $thistutoredit = $toolparts[5];
        } else {
            continue;
        }
        if ($istutor && $thistutoredit == 2) {
            continue;
        }
        if ($catfilter > -1 && $thisgbcat != $catfilter) {
            continue;
        }
        $exttools[$kcnt] = $line['id'];
        $assessmenttype[$kcnt] = "External Tool";
        $category[$kcnt] = $thisgbcat;
        if ($line['avail'] == 2) {
            $line['startdate'] = 0;
            $line['enddate'] = 2000000000;
        }
        $enddate[$kcnt] = $line['enddate'];
        $startdate[$kcnt] = $line['startdate'];
        if ($now < $line['startdate']) {
            $avail[$kcnt] = 2;
        } else {
            if ($now < $line['enddate']) {
                $avail[$kcnt] = 1;
            } else {
                $avail[$kcnt] = 0;
            }
        }
        $possible[$kcnt] = $line['points'];
        $name[$kcnt] = $line['title'];
        $cntingb[$kcnt] = $thiscntingb;
        if (isset($courseitemsassoc)) {
            $courseorder[$kcnt] = $courseitemsassoc['LinkedText' . $line['id']];
        }
        $kcnt++;
    }
    $cats = array();
    $catcolcnt = 0;
    //Pull Categories:  Name, scale, scaletype, chop, drop, weight, calctype
    if (in_array(0, $category)) {
        //define default category, if used
        $cats[0] = explode(',', $defaultcat);
        if (!isset($cats[6])) {
            $cats[6] = $cats[4] == 0 ? 0 : 1;
        }
        array_unshift($cats[0], "Default");
        array_push($cats[0], $catcolcnt);
        $catcolcnt++;
    }
    $query = "SELECT id,name,scale,scaletype,chop,dropn,weight,hidden,calctype FROM imas_gbcats WHERE courseid='{$cid}' ";
    $query .= "ORDER BY name";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($row = mysql_fetch_row($result)) {
        if (in_array($row[0], $category)) {
            //define category if used
            if ($row[1][0] >= '1' && $row[1][0] <= '9') {
                $row[1] = substr($row[1], 1);
            }
            $cats[$row[0]] = array_slice($row, 1);
            array_push($cats[$row[0]], $catcolcnt);
            $catcolcnt++;
        }
    }
    //create item headers
    $pos = 0;
    $catposspast = array();
    $catposspastec = array();
    $catposscur = array();
    $catposscurec = array();
    $catpossfuture = array();
    $catpossfutureec = array();
    $cattotpast = array();
    $cattotpastec = array();
    $cattotcur = array();
    $cattotcurec = array();
    $cattotattempted = array();
    $cattotfuture = array();
    $cattotfutureec = array();
    $itemorder = array();
    $assesscol = array();
    $gradecol = array();
    $discusscol = array();
    $exttoolcol = array();
    if ($orderby == 1) {
        //order $category by enddate
        asort($enddate, SORT_NUMERIC);
        $newcategory = array();
        foreach ($enddate as $k => $v) {
            $newcategory[$k] = $category[$k];
        }
        $category = $newcategory;
    } else {
        if ($orderby == 5) {
            //order $category by enddate reverse
            arsort($enddate, SORT_NUMERIC);
            $newcategory = array();
            foreach ($enddate as $k => $v) {
                $newcategory[$k] = $category[$k];
            }
            $category = $newcategory;
        } else {
            if ($orderby == 7) {
                //order $category by startdate
                asort($startdate, SORT_NUMERIC);
                $newcategory = array();
                foreach ($startdate as $k => $v) {
                    $newcategory[$k] = $category[$k];
                }
                $category = $newcategory;
            } else {
                if ($orderby == 9) {
                    //order $category by startdate reverse
                    arsort($startdate, SORT_NUMERIC);
                    $newcategory = array();
                    foreach ($startdate as $k => $v) {
                        $newcategory[$k] = $category[$k];
                    }
                    $category = $newcategory;
                } else {
                    if ($orderby == 3) {
                        //order $category alpha
                        natcasesort($name);
                        //asort($name);
                        $newcategory = array();
                        foreach ($name as $k => $v) {
                            $newcategory[$k] = $category[$k];
                        }
                        $category = $newcategory;
                    } else {
                        if ($orderby == 11) {
                            //order $category courseorder
                            asort($courseorder, SORT_NUMERIC);
                            $newcategory = array();
                            foreach ($courseorder as $k => $v) {
                                $newcategory[$k] = $category[$k];
                            }
                            $category = $newcategory;
                        } else {
                            if ($orderby == 13) {
                                //order $category courseorder rev
                                arsort($courseorder, SORT_NUMERIC);
                                $newcategory = array();
                                foreach ($courseorder as $k => $v) {
                                    $newcategory[$k] = $category[$k];
                                }
                                $category = $newcategory;
                            }
                        }
                    }
                }
            }
        }
    }
    foreach (array_keys($cats) as $cat) {
        //foreach category
        $catposspast[$cat] = array();
        $catposscur[$cat] = array();
        $catpossfuture[$cat] = array();
        $catkeys = array_keys($category, $cat);
        //pull items in that category
        if (($orderby & 1) == 1) {
            //order by category
            array_splice($itemorder, count($itemorder), 0, $catkeys);
        }
        foreach ($catkeys as $k) {
            if (isset($cats[$cat][6]) && $cats[$cat][6] == 1) {
                //hidden
                $cntingb[$k] = 0;
            }
            if ($avail[$k] < 1) {
                //is past
                if ($assessmenttype[$k] != "Practice" && $cntingb[$k] == 1) {
                    $catposspast[$cat][] = $possible[$k];
                    //create category totals
                } else {
                    if ($cntingb[$k] == 2) {
                        $catposspastec[$cat][] = 0;
                    }
                }
            }
            if ($avail[$k] < 2) {
                //is past or current
                if ($assessmenttype[$k] != "Practice" && $cntingb[$k] == 1) {
                    $catposscur[$cat][] = $possible[$k];
                    //create category totals
                } else {
                    if ($cntingb[$k] == 2) {
                        $catposscurec[$cat][] = 0;
                    }
                }
            }
            //is anytime
            if ($assessmenttype[$k] != "Practice" && $cntingb[$k] == 1) {
                $catpossfuture[$cat][] = $possible[$k];
                //create category totals
            } else {
                if ($cntingb[$k] == 2) {
                    $catpossfutureec[$cat][] = 0;
                }
            }
            if (($orderby & 1) == 1) {
                //display item header if displaying by category
                //$cathdr[$pos] = $cats[$cat][6];
                $gb[0][1][$pos][0] = $name[$k];
                //item name
                $gb[0][1][$pos][1] = $cats[$cat][8];
                //item category number
                $gb[0][1][$pos][2] = $possible[$k];
                //points possible
                $gb[0][1][$pos][3] = $avail[$k];
                //0 past, 1 current, 2 future
                $gb[0][1][$pos][4] = $cntingb[$k];
                //0 no count and hide, 1 count, 2 EC, 3 no count
                if ($assessmenttype[$k] == "Practice") {
                    $gb[0][1][$pos][5] = 1;
                    //0 regular, 1 practice test
                } else {
                    $gb[0][1][$pos][5] = 0;
                }
                if (isset($assessments[$k])) {
                    $gb[0][1][$pos][6] = 0;
                    //0 online, 1 offline
                    $gb[0][1][$pos][7] = $assessments[$k];
                    $gb[0][1][$pos][10] = $isgroup[$k];
                    $assesscol[$assessments[$k]] = $pos;
                } else {
                    if (isset($grades[$k])) {
                        $gb[0][1][$pos][6] = 1;
                        //0 online, 1 offline
                        $gb[0][1][$pos][8] = $tutoredit[$k];
                        //tutoredit
                        $gb[0][1][$pos][7] = $grades[$k];
                        $gradecol[$grades[$k]] = $pos;
                    } else {
                        if (isset($discuss[$k])) {
                            $gb[0][1][$pos][6] = 2;
                            //0 online, 1 offline, 2 discuss
                            $gb[0][1][$pos][7] = $discuss[$k];
                            $discusscol[$discuss[$k]] = $pos;
                        } else {
                            if (isset($exttools[$k])) {
                                $gb[0][1][$pos][6] = 3;
                                //0 online, 1 offline, 2 discuss, 3 exttool
                                $gb[0][1][$pos][7] = $exttools[$k];
                                $exttoolcol[$exttools[$k]] = $pos;
                            }
                        }
                    }
                }
                if (isset($GLOBALS['includeduedate']) && $GLOBALS['includeduedate'] == true || isset($allowlate[$k])) {
                    $gb[0][1][$pos][11] = $enddate[$k];
                }
                if (isset($allowlate[$k])) {
                    $gb[0][1][$pos][12] = $allowlate[$k];
                }
                $pos++;
            }
        }
    }
    if (($orderby & 1) == 0) {
        //if not grouped by category
        if ($orderby == 0) {
            //enddate
            asort($enddate, SORT_NUMERIC);
            $itemorder = array_keys($enddate);
        } else {
            if ($orderby == 2) {
                //alpha
                natcasesort($name);
                //asort($name);
                $itemorder = array_keys($name);
            } else {
                if ($orderby == 4) {
                    //enddate reverse
                    arsort($enddate, SORT_NUMERIC);
                    $itemorder = array_keys($enddate);
                } else {
                    if ($orderby == 6) {
                        //startdate
                        asort($startdate, SORT_NUMERIC);
                        $itemorder = array_keys($startdate);
                    } else {
                        if ($orderby == 8) {
                            //startdate reverse
                            arsort($startdate, SORT_NUMERIC);
                            $itemorder = array_keys($startdate);
                        } else {
                            if ($orderby == 10) {
                                //courseorder
                                asort($courseorder, SORT_NUMERIC);
                                $itemorder = array_keys($courseorder);
                            } else {
                                if ($orderby == 12) {
                                    //courseorder rev
                                    arsort($courseorder, SORT_NUMERIC);
                                    $itemorder = array_keys($courseorder);
                                }
                            }
                        }
                    }
                }
            }
        }
        foreach ($itemorder as $k) {
            $gb[0][1][$pos][0] = $name[$k];
            //item name
            $gb[0][1][$pos][1] = $cats[$category[$k]][7];
            //item category name
            $gb[0][1][$pos][2] = $possible[$k];
            //points possible
            $gb[0][1][$pos][3] = $avail[$k];
            //0 past, 1 current, 2 future
            $gb[0][1][$pos][4] = $cntingb[$k];
            //0 no count and hide, 1 count, 2 EC, 3 no count
            $gb[0][1][$pos][5] = $assessmenttype[$k] == "Practice";
            //0 regular, 1 practice test
            if (isset($assessments[$k])) {
                $gb[0][1][$pos][6] = 0;
                //0 online, 1 offline
                $gb[0][1][$pos][7] = $assessments[$k];
                $gb[0][1][$pos][10] = $isgroup[$k];
                $assesscol[$assessments[$k]] = $pos;
            } else {
                if (isset($grades[$k])) {
                    $gb[0][1][$pos][6] = 1;
                    //0 online, 1 offline
                    $gb[0][1][$pos][8] = $tutoredit[$k];
                    //tutoredit
                    $gb[0][1][$pos][7] = $grades[$k];
                    $gradecol[$grades[$k]] = $pos;
                } else {
                    if (isset($discuss[$k])) {
                        $gb[0][1][$pos][6] = 2;
                        //0 online, 1 offline, 2 discuss
                        $gb[0][1][$pos][7] = $discuss[$k];
                        $discusscol[$discuss[$k]] = $pos;
                    } else {
                        if (isset($exttools[$k])) {
                            $gb[0][1][$pos][6] = 3;
                            //0 online, 1 offline, 2 discuss, 3 exttool
                            $gb[0][1][$pos][7] = $exttools[$k];
                            $exttoolcol[$exttools[$k]] = $pos;
                        }
                    }
                }
            }
            if (isset($GLOBALS['includeduedate']) && $GLOBALS['includeduedate'] == true || isset($allowlate[$k])) {
                $gb[0][1][$pos][11] = $enddate[$k];
            }
            if (isset($allowlate[$k])) {
                $gb[0][1][$pos][12] = $allowlate[$k];
            }
            $pos++;
        }
    }
    $totalspos = $pos;
    //create category headers
    $catorder = array_keys($cats);
    $overallptspast = 0;
    $overallptscur = 0;
    $overallptsfuture = 0;
    $overallptsattempted = 0;
    $cattotweightpast = 0;
    $cattotweightcur = 0;
    $cattotweightfuture = 0;
    $pos = 0;
    $catpossattempted = array();
    $catpossattemptedec = array();
    foreach ($catorder as $cat) {
        //foreach category
        //cats: name,scale,scaletype,chop,drop,weight
        $catitemcntpast[$cat] = count($catposspast[$cat]);
        // + count($catposspastec[$cat]);
        $catitemcntcur[$cat] = count($catposscur[$cat]);
        // + count($catposscurec[$cat]);
        $catitemcntfuture[$cat] = count($catpossfuture[$cat]);
        // + count($catpossfutureec[$cat]);
        $catpossattempted[$cat] = $catposscur[$cat];
        //a copy of the current for later use with attempted
        $catpossattemptedec[$cat] = $catposscurec[$cat];
        if ($cats[$cat][4] != 0 && abs($cats[$cat][4]) < count($catposspast[$cat])) {
            //if drop is set and have enough items
            asort($catposspast[$cat], SORT_NUMERIC);
            $catposspast[$cat] = array_slice($catposspast[$cat], $cats[$cat][4]);
        }
        if ($cats[$cat][4] != 0 && abs($cats[$cat][4]) < count($catposscur[$cat])) {
            //same for past&current
            asort($catposscur[$cat], SORT_NUMERIC);
            $catposscur[$cat] = array_slice($catposscur[$cat], $cats[$cat][4]);
        }
        if ($cats[$cat][4] != 0 && abs($cats[$cat][4]) < count($catpossfuture[$cat])) {
            //same for all items
            asort($catpossfuture[$cat], SORT_NUMERIC);
            $catpossfuture[$cat] = array_slice($catpossfuture[$cat], $cats[$cat][4]);
        }
        $catposspast[$cat] = array_sum($catposspast[$cat]);
        $catposscur[$cat] = array_sum($catposscur[$cat]);
        $catpossfuture[$cat] = array_sum($catpossfuture[$cat]);
        $gb[0][2][$pos][0] = $cats[$cat][0];
        $gb[0][2][$pos][1] = $cats[$cat][8];
        $gb[0][2][$pos][10] = $cat;
        $gb[0][2][$pos][12] = $cats[$cat][6];
        $gb[0][2][$pos][13] = $cats[$cat][7];
        if ($catposspast[$cat] > 0 || count($catposspastec[$cat]) > 0) {
            $gb[0][2][$pos][2] = 0;
            //scores in past
            $cattotweightpast += $cats[$cat][5];
            $cattotweightcur += $cats[$cat][5];
            $cattotweightfuture += $cats[$cat][5];
        } else {
            if ($catposscur[$cat] > 0 || count($catposscurec[$cat]) > 0) {
                $gb[0][2][$pos][2] = 1;
                //scores in cur
                $cattotweightcur += $cats[$cat][5];
                $cattotweightfuture += $cats[$cat][5];
            } else {
                if ($catpossfuture[$cat] > 0 || count($catpossfutureec[$cat]) > 0) {
                    $gb[0][2][$pos][2] = 2;
                    //scores in future
                    $cattotweightfuture += $cats[$cat][5];
                } else {
                    $gb[0][2][$pos][2] = 3;
                    //no items
                }
            }
        }
        if ($useweights == 0 && $cats[$cat][5] > -1) {
            //if scaling cat total to point value
            if ($catposspast[$cat] > 0) {
                $gb[0][2][$pos][3] = $cats[$cat][5];
                //score for past
            } else {
                $gb[0][2][$pos][3] = 0;
                //fix to 0 if no scores in past yet
            }
            if ($catposscur[$cat] > 0) {
                $gb[0][2][$pos][4] = $cats[$cat][5];
                //score for cur
            } else {
                $gb[0][2][$pos][4] = 0;
                //fix to 0 if no scores in cur/past yet
            }
            if ($catpossfuture[$cat] > 0) {
                $gb[0][2][$pos][5] = $cats[$cat][5];
                //score for future
            } else {
                $gb[0][2][$pos][5] = 0;
                //fix to 0 if no scores in future yet
            }
        } else {
            $gb[0][2][$pos][3] = $catposspast[$cat];
            $gb[0][2][$pos][4] = $catposscur[$cat];
            $gb[0][2][$pos][5] = $catpossfuture[$cat];
        }
        if ($useweights == 1) {
            $gb[0][2][$pos][11] = $cats[$cat][5];
        }
        $overallptspast += $gb[0][2][$pos][3];
        $overallptscur += $gb[0][2][$pos][4];
        $overallptsfuture += $gb[0][2][$pos][5];
        $pos++;
    }
    //find total possible points
    if ($useweights == 0) {
        //use points grading method
        $gb[0][3][0] = $overallptspast;
        $gb[0][3][1] = $overallptscur;
        $gb[0][3][2] = $overallptsfuture;
    }
    //Pull student data
    $ln = 1;
    $query = "SELECT imas_users.id,imas_users.SID,imas_users.FirstName,imas_users.LastName,imas_users.SID,imas_users.email,imas_students.section,imas_students.code,imas_students.locked,imas_students.timelimitmult,imas_students.lastaccess,imas_users.hasuserimg,imas_students.gbcomment ";
    $query .= "FROM imas_users,imas_students WHERE imas_users.id=imas_students.userid AND imas_students.courseid='{$cid}' ";
    //$query .= "FROM imas_users,imas_teachers WHERE imas_users.id=imas_teachers.userid AND imas_teachers.courseid='$cid' ";
    //if (!$isteacher && !isset($tutorid)) {$query .= "AND imas_users.id='$userid' ";}
    if ($limuser > 0) {
        $query .= "AND imas_users.id='{$limuser}' ";
    }
    if ($secfilter != -1 && $limuser <= 0) {
        $query .= "AND imas_students.section='{$secfilter}' ";
    }
    if ($hidelocked) {
        $query .= "AND imas_students.locked=0 ";
    }
    if (isset($timefilter)) {
        $tf = time() - 60 * 60 * $timefilter;
        $query .= "AND imas_users.lastaccess>{$tf} ";
    }
    if (isset($lnfilter) && $lnfilter != '') {
        $query .= "AND imas_users.LastName LIKE '{$lnfilter}%' ";
    }
    if ($isdiag) {
        $query .= "ORDER BY imas_users.email,imas_users.LastName,imas_users.FirstName";
    } else {
        if ($hassection && $usersort == 0) {
            $query .= "ORDER BY imas_students.section,imas_users.LastName,imas_users.FirstName";
        } else {
            $query .= "ORDER BY imas_users.LastName,imas_users.FirstName";
        }
    }
    $result = mysql_query($query) or die("Query failed : {$query}: " . mysql_error());
    $alt = 0;
    $sturow = array();
    $timelimitmult = array();
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        //foreach student
        unset($asid);
        unset($pts);
        unset($IP);
        unset($timeused);
        $cattotpast[$ln] = array();
        $cattotpastec[$ln] = array();
        $cattotcur[$ln] = array();
        $cattotfuture[$ln] = array();
        $cattotcurec[$ln] = array();
        $cattotfutureec[$ln] = array();
        //Student ID info
        $gb[$ln][0][0] = "{$line['LastName']},&nbsp;{$line['FirstName']}";
        $gb[$ln][4][0] = $line['id'];
        $gb[$ln][4][1] = $line['locked'];
        $gb[$ln][4][2] = $line['hasuserimg'];
        $gb[$ln][4][3] = !empty($line['gbcomment']);
        if ($isdiag) {
            $selparts = explode('~', $line['SID']);
            $gb[$ln][0][1] = $selparts[0];
            $gb[$ln][0][2] = $selparts[1];
            $selparts = explode('@', $line['email']);
            $gb[$ln][0][3] = $selparts[0];
            $gb[$ln][0][4] = $selparts[1];
        } else {
            $gb[$ln][0][1] = $line['SID'];
        }
        if ($hassection && !$isdiag) {
            $gb[$ln][0][] = $line['section'] == null ? '' : $line['section'];
        }
        if ($hascode) {
            $gb[$ln][0][] = $line['code'];
        }
        if ($lastlogin) {
            $gb[$ln][0][] = date("n/j/y", $line['lastaccess']);
        }
        $sturow[$line['id']] = $ln;
        $timelimitmult[$line['id']] = $line['timelimitmult'];
        $ln++;
    }
    //pull logincnt if needed
    if ($logincnt == 1) {
        $query = "SELECT userid,count(*) FROM imas_login_log WHERE courseid='{$cid}' GROUP BY userid";
        $result2 = mysql_query($query) or die("Query failed : " . mysql_error());
        while ($r = mysql_fetch_row($result2)) {
            $gb[$sturow[$r[0]]][0][] = $r[1];
        }
    }
    //pull exceptions
    $exceptions = array();
    $query = "SELECT imas_exceptions.assessmentid,imas_exceptions.userid,imas_exceptions.enddate,imas_exceptions.islatepass FROM imas_exceptions,imas_assessments WHERE ";
    $query .= "imas_exceptions.assessmentid=imas_assessments.id AND imas_assessments.courseid='{$cid}'";
    $result2 = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($r = mysql_fetch_row($result2)) {
        if (!isset($sturow[$r[1]])) {
            continue;
        }
        $exceptions[$r[0]][$r[1]] = array($r[2], $r[3]);
        $gb[$sturow[$r[1]]][1][$assesscol[$r[0]]][6] = $r[3] > 0 ? 1 + $r[3] : 1;
        $gb[$sturow[$r[1]]][1][$assesscol[$r[0]]][3] = 10;
        //will get overwritten later if assessment session exists
    }
    //Get assessment scores
    $assessidx = array_flip($assessments);
    $query = "SELECT ias.id,ias.assessmentid,ias.bestscores,ias.starttime,ias.endtime,ias.timeontask,ias.feedback,ias.userid,ia.timelimit FROM imas_assessment_sessions AS ias,imas_assessments AS ia ";
    $query .= "WHERE ia.id=ias.assessmentid AND ia.courseid='{$cid}' ";
    if ($limuser > 0) {
        $query .= " AND ias.userid='{$limuser}' ";
    }
    $result2 = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($l = mysql_fetch_array($result2, MYSQL_ASSOC)) {
        if (!isset($assessidx[$l['assessmentid']]) || !isset($sturow[$l['userid']]) || !isset($assesscol[$l['assessmentid']])) {
            continue;
        }
        $i = $assessidx[$l['assessmentid']];
        $row = $sturow[$l['userid']];
        $col = $assesscol[$l['assessmentid']];
        //if two asids for same stu/assess, skip or overright one with higher ID. Shouldn't happen
        if (isset($gb[$row][1][$col][4]) && $gb[$row][1][$col][4] < $l['id']) {
            continue;
        }
        $gb[$row][1][$col][4] = $l['id'];
        //assessment session id
        $sp = explode(';', $l['bestscores']);
        $scores = explode(',', $sp[0]);
        $pts = 0;
        for ($j = 0; $j < count($scores); $j++) {
            $pts += getpts($scores[$j]);
            //if ($scores[$i]>0) {$total += $scores[$i];}
        }
        $timeused = $l['endtime'] - $l['starttime'];
        if ($l['endtime'] == 0 || $l['starttime'] == 0) {
            $gb[$row][1][$col][7] = -1;
        } else {
            $gb[$row][1][$col][7] = round($timeused / 60);
        }
        $timeontask = array_sum(explode(',', str_replace('~', ',', $l['timeontask'])));
        if ($timeontask == 0) {
            $gb[$row][1][$col][8] = "N/A";
        } else {
            $gb[$row][1][$col][8] = round($timeontask / 60, 1);
        }
        if (isset($GLOBALS['includelastchange']) && $GLOBALS['includelastchange'] == true) {
            $gb[$row][1][$col][9] = $l['endtime'];
        }
        if (in_array(-1, $scores)) {
            $IP = 1;
        } else {
            $IP = 0;
        }
        /*
        Moved up to exception finding so LP mark will show on unstarted assessments
        if (isset($exceptions[$l['assessmentid']][$l['userid']])) {
        	$gb[$row][1][$col][6] = ($exceptions[$l['assessmentid']][$l['userid']][1]>0)?2:1; //had exception
        }
        */
        $latepasscnt = 0;
        if (isset($exceptions[$l['assessmentid']][$l['userid']])) {
            // && $now>$enddate[$i] && $now<$exceptions[$l['assessmentid']][$l['userid']]) {
            if ($enddate[$i] > $exceptions[$l['assessmentid']][$l['userid']][0] && $assessmenttype[$i] == "NoScores") {
                //if exception set for earlier, and NoScores is set, use later date to hide score until later
                $thised = $enddate[$i];
            } else {
                $thised = $exceptions[$l['assessmentid']][$l['userid']][0];
                if ($limuser > 0 && $gb[0][1][$col][3] == 2) {
                    //change $avail past/cur/future
                    if ($now < $thised) {
                        $gb[0][1][$col][3] = 1;
                    } else {
                        $gb[0][1][$col][3] = 0;
                    }
                }
            }
            $inexception = true;
            if ($enddate[$i] < $exceptions[$l['assessmentid']][$l['userid']][0] && $latepasshrs > 0) {
                $latepasscnt = round(($exceptions[$l['assessmentid']][$l['userid']][0] - $enddate[$i]) / ($latepasshrs * 3600));
            }
        } else {
            $thised = $enddate[$i];
            $inexception = false;
        }
        $allowlatethis = false;
        if (isset($allowlate[$i]) && ($allowlate[$i] % 10 == 1 || $latepasscnt < $allowlate[$i] % 10 - 1)) {
            if ($now < $thised) {
                $allowlatethis = true;
            } else {
                if ($allowlate[$i] > 10 && $now - $thised < $latepasshrs * 3600) {
                    $allowlatethis = true;
                }
            }
        }
        $gb[$row][1][$col][10] = $allowlatethis;
        if ($canviewall || $sa[$i] == "I" || $sa[$i] != "N" && $now > $thised) {
            //|| $assessmenttype[$i]=="Practice"
            $gb[$row][1][$col][2] = 1;
            //show link
        } else {
            $gb[$row][1][$col][2] = 0;
            //don't show link
        }
        $countthisone = false;
        if ($assessmenttype[$i] == "NoScores" && $sa[$i] != "I" && $now < $thised && !$canviewall) {
            $gb[$row][1][$col][0] = 'N/A';
            //score is not available
            $gb[$row][1][$col][3] = 0;
            //no other info
        } else {
            if ($minscores[$i] < 10000 && $pts < $minscores[$i] || $minscores[$i] > 10000 && $pts < ($minscores[$i] - 10000) / 100 * $possible[$i]) {
                //else if ($pts<$minscores[$i]) {
                if ($canviewall) {
                    $gb[$row][1][$col][0] = $pts;
                    //the score
                    $gb[$row][1][$col][3] = 1;
                    //no credit
                } else {
                    $gb[$row][1][$col][0] = 'NC';
                    //score is No credit
                    $gb[$row][1][$col][3] = 1;
                    //no credit
                }
            } else {
                if ($IP == 1 && $thised > $now && ($timelimits[$i] == 0 || $timeused < $timelimits[$i] * $timelimitmult[$l['userid']])) {
                    $gb[$row][1][$col][0] = $pts;
                    //the score
                    $gb[$row][1][$col][3] = 2;
                    //in progress
                    $countthisone = true;
                } else {
                    if ($timelimits[$i] > 0 && $timeused > $timelimits[$i] * $timelimitmult[$l['userid']]) {
                        $gb[$row][1][$col][0] = $pts;
                        //the score
                        $gb[$row][1][$col][3] = 3;
                        //over time
                    } else {
                        if ($assessmenttype[$i] == "Practice") {
                            $gb[$row][1][$col][0] = $pts;
                            //the score
                            $gb[$row][1][$col][3] = 4;
                            //practice test
                        } else {
                            //regular score available to students
                            $gb[$row][1][$col][0] = $pts;
                            //the score
                            $gb[$row][1][$col][3] = 0;
                            //no other info
                            $countthisone = true;
                        }
                    }
                }
            }
        }
        if ($now < $thised) {
            //still active
            $gb[$row][1][$col][3] += 10;
        }
        if ($countthisone) {
            if ($cntingb[$i] == 1) {
                if ($gb[0][1][$col][3] < 1) {
                    //past
                    $cattotpast[$row][$category[$i]][$col] = $pts;
                }
                if ($gb[0][1][$col][3] < 2) {
                    //past or cur
                    $cattotcur[$row][$category[$i]][$col] = $pts;
                }
                $cattotfuture[$row][$category[$i]][$col] = $pts;
            } else {
                if ($cntingb[$i] == 2) {
                    if ($gb[0][1][$col][3] < 1) {
                        //past
                        $cattotpastec[$row][$category[$i]][$col] = $pts;
                    }
                    if ($gb[0][1][$col][3] < 2) {
                        //past or cur
                        $cattotcurec[$row][$category[$i]][$col] = $pts;
                    }
                    $cattotfutureec[$row][$category[$i]][$col] = $pts;
                }
            }
        }
        if ($limuser > 0 || isset($GLOBALS['includecomments']) && $GLOBALS['includecomments']) {
            $gb[$row][1][$col][1] = $l['feedback'];
            //the feedback
        } else {
            if ($limuser == 0 && $l['feedback'] != '') {
                $gb[$row][1][$col][1] = 1;
                //has comment
            } else {
                $gb[$row][1][$col][1] = 0;
                //no comment
            }
        }
    }
    //Get other grades
    $gradeidx = array_flip($grades);
    unset($gradeid);
    unset($opts);
    unset($discusspts);
    $discussidx = array_flip($discuss);
    $exttoolidx = array_flip($exttools);
    $gradetypeselects = array();
    if (count($grades) > 0) {
        $gradeidlist = implode(',', $grades);
        $gradetypeselects[] = "(gradetype='offline' AND gradetypeid IN ({$gradeidlist}))";
    }
    if (count($discuss) > 0) {
        $forumidlist = implode(',', $discuss);
        $gradetypeselects[] = "(gradetype='forum' AND gradetypeid IN ({$forumidlist}))";
    }
    if (count($exttools) > 0) {
        $linkedlist = implode(',', $exttools);
        $gradetypeselects[] = "(gradetype='exttool' AND gradetypeid IN ({$linkedlist}))";
    }
    if (count($gradetypeselects) > 0) {
        $sel = implode(' OR ', $gradetypeselects);
        $query = "SELECT * FROM imas_grades WHERE ({$sel})";
        //$query = "SELECT imas_grades.gradetypeid,imas_grades.gradetype,imas_grades.refid,imas_grades.id,imas_grades.score,imas_grades.feedback,imas_grades.userid FROM imas_grades,imas_gbitems WHERE ";
        //$query .= "imas_grades.gradetypeid=imas_gbitems.id AND imas_gbitems.courseid='$cid'";
        if ($limuser > 0) {
            $query .= " AND userid='{$limuser}' ";
        }
        $result2 = mysql_query($query) or die("Query failed : {$query} " . mysql_error());
        while ($l = mysql_fetch_array($result2, MYSQL_ASSOC)) {
            if ($l['gradetype'] == 'offline') {
                if (!isset($gradeidx[$l['gradetypeid']]) || !isset($sturow[$l['userid']]) || !isset($gradecol[$l['gradetypeid']])) {
                    continue;
                }
                $i = $gradeidx[$l['gradetypeid']];
                $row = $sturow[$l['userid']];
                $col = $gradecol[$l['gradetypeid']];
                $gb[$row][1][$col][2] = $l['id'];
                if ($l['score'] != null) {
                    $gb[$row][1][$col][0] = 1 * $l['score'];
                }
                if ($limuser > 0 || isset($GLOBALS['includecomments']) && $GLOBALS['includecomments']) {
                    $gb[$row][1][$col][1] = $l['feedback'];
                    //the feedback (for students)
                } else {
                    if ($limuser == 0 && $l['feedback'] != '') {
                        //feedback
                        $gb[$row][1][$col][1] = 1;
                        //yes it has it (for teachers)
                    } else {
                        $gb[$row][1][$col][1] = 0;
                        //no feedback
                    }
                }
                if ($cntingb[$i] == 1) {
                    if ($gb[0][1][$col][3] < 1) {
                        //past
                        $cattotpast[$row][$category[$i]][$col] = 1 * $l['score'];
                    }
                    if ($gb[0][1][$col][3] < 2) {
                        //past or cur
                        $cattotcur[$row][$category[$i]][$col] = 1 * $l['score'];
                    }
                    $cattotfuture[$row][$category[$i]][$col] = 1 * $l['score'];
                } else {
                    if ($cntingb[$i] == 2) {
                        if ($gb[0][1][$col][3] < 1) {
                            //past
                            $cattotpastec[$row][$category[$i]][$col] = 1 * $l['score'];
                        }
                        if ($gb[0][1][$col][3] < 2) {
                            //past or cur
                            $cattotcurec[$row][$category[$i]][$col] = 1 * $l['score'];
                        }
                        $cattotfutureec[$row][$category[$i]][$col] = 1 * $l['score'];
                    }
                }
            } else {
                if ($l['gradetype'] == 'forum') {
                    if (!isset($discussidx[$l['gradetypeid']]) || !isset($sturow[$l['userid']]) || !isset($discusscol[$l['gradetypeid']])) {
                        continue;
                    }
                    $i = $discussidx[$l['gradetypeid']];
                    $row = $sturow[$l['userid']];
                    $col = $discusscol[$l['gradetypeid']];
                    if ($l['score'] != null) {
                        if (isset($gb[$row][1][$col][0])) {
                            $gb[$row][1][$col][0] += 1 * $l['score'];
                            //adding up all forum scores
                        } else {
                            $gb[$row][1][$col][0] = 1 * $l['score'];
                        }
                    }
                    if ($limuser == 0 && !isset($gb[$row][1][$col][1])) {
                        $gb[$row][1][$col][1] = 0;
                        //no feedback
                    }
                    if (trim($l['feedback']) != '') {
                        if ($limuser > 0 || isset($GLOBALS['includecomments']) && $GLOBALS['includecomments']) {
                            if (isset($gb[$row][1][$col][1])) {
                                $gb[$row][1][$col][1] .= "<br/>" . $l['feedback'];
                            } else {
                                $gb[$row][1][$col][1] = $l['feedback'];
                            }
                            //the feedback (for students)
                        } else {
                            if ($limuser == 0) {
                                //feedback
                                $gb[$row][1][$col][1] = 1;
                                //yes it has it (for teachers)
                            }
                        }
                    }
                    $gb[$row][1][$col][2] = 1;
                    //show link
                    $gb[$row][1][$col][3] = 0;
                    //is counted
                    if ($gb[0][1][$col][3] < 1) {
                        //past
                        $cattotpast[$row][$category[$i]][$col] = $gb[$row][1][$col][0];
                    }
                    if ($gb[0][1][$col][3] < 2) {
                        //past or cur
                        $cattotcur[$row][$category[$i]][$col] = $gb[$row][1][$col][0];
                    }
                    $cattotfuture[$row][$category[$i]][$col] = $gb[$row][1][$col][0];
                } else {
                    if ($l['gradetype'] == 'exttool') {
                        if (!isset($exttoolidx[$l['gradetypeid']]) || !isset($sturow[$l['userid']]) || !isset($exttoolcol[$l['gradetypeid']])) {
                            continue;
                        }
                        $i = $exttoolidx[$l['gradetypeid']];
                        $row = $sturow[$l['userid']];
                        $col = $exttoolcol[$l['gradetypeid']];
                        $gb[$row][1][$col][2] = $l['id'];
                        if ($l['score'] != null) {
                            $gb[$row][1][$col][0] = 1 * $l['score'];
                        }
                        if ($limuser > 0 || isset($GLOBALS['includecomments']) && $GLOBALS['includecomments']) {
                            $gb[$row][1][$col][1] = $l['feedback'];
                            //the feedback (for students)
                        } else {
                            if ($limuser == 0 && $l['feedback'] != '') {
                                //feedback
                                $gb[$row][1][$col][1] = 1;
                                //yes it has it (for teachers)
                            } else {
                                $gb[$row][1][$col][1] = 0;
                                //no feedback
                            }
                        }
                        if ($cntingb[$i] == 1) {
                            if ($gb[0][1][$col][3] < 1) {
                                //past
                                $cattotpast[$row][$category[$i]][$col] = 1 * $l['score'];
                            }
                            if ($gb[0][1][$col][3] < 2) {
                                //past or cur
                                $cattotcur[$row][$category[$i]][$col] = 1 * $l['score'];
                            }
                            $cattotfuture[$row][$category[$i]][$col] = 1 * $l['score'];
                        } else {
                            if ($cntingb[$i] == 2) {
                                if ($gb[0][1][$col][3] < 1) {
                                    //past
                                    $cattotpastec[$row][$category[$i]][$col] = 1 * $l['score'];
                                }
                                if ($gb[0][1][$col][3] < 2) {
                                    //past or cur
                                    $cattotcurec[$row][$category[$i]][$col] = 1 * $l['score'];
                                }
                                $cattotfutureec[$row][$category[$i]][$col] = 1 * $l['score'];
                            }
                        }
                    }
                }
            }
        }
    }
    /*
    //Get discussion grades
    unset($discusspts);
    $discussidx = array_flip($discuss);
    $query = "SELECT imas_forum_posts.userid,imas_forum_posts.forumid,SUM(imas_forum_posts.points) FROM imas_forum_posts,imas_forums WHERE imas_forum_posts.forumid=imas_forums.id AND imas_forums.courseid='$cid' ";
    if ($limuser>0) { $query .= " AND imas_forum_posts.userid='$limuser' ";}
    $query .= "GROUP BY imas_forum_posts.forumid,imas_forum_posts.userid ";
    
    $result2 = mysql_query($query) or die("Query failed : $query " . mysql_error());
    while ($r = mysql_fetch_row($result2)) {
    	if (!isset($discussidx[$r[1]]) || !isset($sturow[$r[0]]) || !isset($discusscol[$r[1]])) {
    		continue;
    	}
    	$i = $discussidx[$r[1]];
    	$row = $sturow[$r[0]];
    	$col = $discusscol[$r[1]];
    	if ($r[2]!=null) {
    		$gb[$row][1][$col][0] = 1*$r[2];
    	}
    	$gb[$row][1][$col][3] = 0; //is counted
    	if ($gb[0][1][$col][3]<1) { //past
    		$cattotpast[$row][$category[$i]][$col] = $r[2];
    	} 
    	if ($gb[0][1][$col][3]<2) { //past or cur
    		$cattotcur[$row][$category[$i]][$col] = $r[2];
    	}
    	$cattotfuture[$row][$category[$i]][$col] = $r[2];
    }
    */
    //fill out cattot's with zeros
    for ($ln = 1; $ln < count($sturow) + 1; $ln++) {
        $cattotattempted[$ln] = $cattotcur[$ln];
        //copy current to attempted - we will fill in zeros for past due stuff
        $cattotattemptedec[$ln] = $cattotcurec[$ln];
        foreach ($assessidx as $aid => $i) {
            $col = $assesscol[$aid];
            if (!isset($gb[$ln][1][$col][0]) || $gb[$ln][1][$col][3] % 10 == 1) {
                if ($cntingb[$i] == 1) {
                    if ($gb[0][1][$col][3] < 1) {
                        //past
                        $cattotpast[$ln][$category[$i]][$col] = 0;
                        $cattotattempted[$ln][$category[$i]][$col] = 0;
                    }
                    if ($gb[0][1][$col][3] < 2) {
                        //past or cur
                        $cattotcur[$ln][$category[$i]][$col] = 0;
                    }
                    $cattotfuture[$ln][$category[$i]][$col] = 0;
                } else {
                    if ($cntingb[$i] == 2) {
                        if ($gb[0][1][$col][3] < 1) {
                            //past
                            $cattotpastec[$ln][$category[$i]][$col] = 0;
                            $cattotattemptedec[$ln][$category[$i]][$col] = 0;
                        }
                        if ($gb[0][1][$col][3] < 2) {
                            //past or cur
                            $cattotcurec[$ln][$category[$i]][$col] = 0;
                        }
                        $cattotfutureec[$ln][$category[$i]][$col] = 0;
                    }
                }
            }
        }
        foreach ($gradeidx as $aid => $i) {
            $col = $gradecol[$aid];
            if (!isset($gb[$ln][1][$col][0])) {
                if ($cntingb[$i] == 1) {
                    if ($gb[0][1][$col][3] < 1) {
                        //past
                        $cattotpast[$ln][$category[$i]][$col] = 0;
                        $cattotattempted[$ln][$category[$i]][$col] = 0;
                    }
                    if ($gb[0][1][$col][3] < 2) {
                        //past or cur
                        $cattotcur[$ln][$category[$i]][$col] = 0;
                    }
                    $cattotfuture[$ln][$category[$i]][$col] = 0;
                } else {
                    if ($cntingb[$i] == 2) {
                        if ($gb[0][1][$col][3] < 1) {
                            //past
                            $cattotpastec[$ln][$category[$i]][$col] = 0;
                            $cattotattemptedec[$ln][$category[$i]][$col] = 0;
                        }
                        if ($gb[0][1][$col][3] < 2) {
                            //past or cur
                            $cattotcurec[$ln][$category[$i]][$col] = 0;
                        }
                        $cattotfutureec[$ln][$category[$i]][$col] = 0;
                    }
                }
            }
        }
        foreach ($discussidx as $aid => $i) {
            $col = $discusscol[$aid];
            if (!isset($gb[$ln][1][$col][0])) {
                if ($cntingb[$i] == 1) {
                    if ($gb[0][1][$col][3] < 1) {
                        //past
                        $cattotpast[$ln][$category[$i]][$col] = 0;
                        $cattotattempted[$ln][$category[$i]][$col] = 0;
                    }
                    if ($gb[0][1][$col][3] < 2) {
                        //past or cur
                        $cattotcur[$ln][$category[$i]][$col] = 0;
                    }
                    $cattotfuture[$ln][$category[$i]][$col] = 0;
                } else {
                    if ($cntingb[$i] == 2) {
                        if ($gb[0][1][$col][3] < 1) {
                            //past
                            $cattotpastec[$ln][$category[$i]][$col] = 0;
                            $cattotattemptedec[$ln][$category[$i]][$col] = 0;
                        }
                        if ($gb[0][1][$col][3] < 2) {
                            //past or cur
                            $cattotcurec[$ln][$category[$i]][$col] = 0;
                        }
                        $cattotfutureec[$ln][$category[$i]][$col] = 0;
                    }
                }
            }
        }
    }
    //create category totals
    for ($ln = 1; $ln < count($sturow) + 1; $ln++) {
        //foreach student calculate category totals and total totals
        $totpast = 0;
        $totcur = 0;
        $totfuture = 0;
        $totattempted = 0;
        $cattotweightattempted = 0;
        $pos = 0;
        //reset position for category totals
        //update attempted for this student
        unset($catpossattemptedstu);
        unset($catpossattemptedecstu);
        $catpossattemptedstu = $catpossattempted;
        //copy attempted array for each stu
        $catpossattemptedecstu = $catpossattemptedec;
        foreach ($assessidx as $aid => $i) {
            $col = $assesscol[$aid];
            if (!isset($gb[$ln][1][$col][0])) {
                if ($gb[0][1][$col][3] == 1) {
                    //if cur , clear out of cattotattempted
                    if ($gb[0][1][$col][4] == 1) {
                        $atloc = array_search($gb[0][1][$col][2], $catpossattemptedstu[$category[$i]]);
                        if ($atloc !== false) {
                            unset($catpossattemptedstu[$category[$i]][$atloc]);
                        }
                    } else {
                        if ($gb[0][1][$col][4] == 2) {
                            $atloc = array_search($gb[0][1][$col][2], $catpossattemptedecstu[$category[$i]]);
                            if ($atloc !== false) {
                                unset($catpossattemptedecstu[$category[$i]][$atloc]);
                            }
                        }
                    }
                }
            }
        }
        foreach ($catorder as $cat) {
            //foreach category
            if (isset($cattotpast[$ln][$cat])) {
                //past items
                //cats: name,scale,scaletype,chop,drop,weight,calctype
                //if ($cats[$cat][4]!=0 && abs($cats[$cat][4])<count($cattotpast[$ln][$cat])) { //if drop is set and have enough items
                if ($cats[$cat][7] == 1) {
                    foreach ($cattotpast[$ln][$cat] as $col => $v) {
                        if ($gb[0][1][$col][2] == 0) {
                            $cattotpast[$ln][$cat][$col] = 0;
                        } else {
                            $cattotpast[$ln][$cat][$col] = $v / $gb[0][1][$col][2];
                            //convert to percents
                        }
                    }
                    if ($cats[$cat][4] != 0 && abs($cats[$cat][4]) < count($cattotpast[$ln][$cat])) {
                        asort($cattotpast[$ln][$cat], SORT_NUMERIC);
                        if ($cats[$cat][4] < 0) {
                            //doing keep n
                            $ntodrop = count($cattotpast[$ln][$cat]) + $cats[$cat][4];
                        } else {
                            //doing drop n
                            $ntodrop = $cats[$cat][4] - ($catitemcntpast[$cat] - count($cattotpast[$ln][$cat]));
                        }
                        if ($ntodrop > 0) {
                            $ndropcnt = 0;
                            foreach ($cattotpast[$ln][$cat] as $col => $v) {
                                $gb[$ln][1][$col][5] = 1;
                                //mark as dropped
                                $ndropcnt++;
                                if ($ndropcnt == $ntodrop) {
                                    break;
                                }
                            }
                        }
                        while (count($cattotpast[$ln][$cat]) < $catitemcntpast[$cat]) {
                            array_unshift($cattotpast[$ln][$cat], 0);
                        }
                        $cattotpast[$ln][$cat] = array_slice($cattotpast[$ln][$cat], $cats[$cat][4]);
                        $tokeep = $cats[$cat][4] < 0 ? abs($cats[$cat][4]) : $catitemcntpast[$cat] - $cats[$cat][4];
                        $cattotpast[$ln][$cat] = round($catposspast[$cat] * array_sum($cattotpast[$ln][$cat]) / $tokeep, 1);
                    } else {
                        $cattotpast[$ln][$cat] = round($catposspast[$cat] * array_sum($cattotpast[$ln][$cat]) / count($cattotpast[$ln][$cat]), 2);
                    }
                } else {
                    $cattotpast[$ln][$cat] = array_sum($cattotpast[$ln][$cat]);
                }
                if ($cats[$cat][1] != 0) {
                    //scale is set
                    if ($cats[$cat][2] == 0) {
                        //pts scale
                        $cattotpast[$ln][$cat] = round($catposspast[$cat] * ($cattotpast[$ln][$cat] / $cats[$cat][1]), 1);
                    } else {
                        if ($cats[$cat][2] == 1) {
                            //percent scale
                            $cattotpast[$ln][$cat] = round($cattotpast[$ln][$cat] * (100 / $cats[$cat][1]), 1);
                        }
                    }
                }
                if (isset($cattotpastec[$ln][$cat])) {
                    //add in EC
                    $cattotpast[$ln][$cat] += array_sum($cattotpastec[$ln][$cat]);
                }
                if ($useweights == 0 && $cats[$cat][5] > -1) {
                    //use fixed pt value for cat
                    $cattotpast[$ln][$cat] = $catposspast[$cat] == 0 ? 0 : round($cats[$cat][5] * ($cattotpast[$ln][$cat] / $catposspast[$cat]), 1);
                }
                if ($cats[$cat][3] > 0) {
                    //chop score - no over 100%
                    if ($useweights == 0 && $cats[$cat][5] > -1) {
                        //set cat pts
                        $cattotpast[$ln][$cat] = min($cats[$cat][5] * $cats[$cat][3], $cattotpast[$ln][$cat]);
                    } else {
                        $cattotpast[$ln][$cat] = min($catposspast[$cat] * $cats[$cat][3], $cattotpast[$ln][$cat]);
                    }
                }
                $gb[$ln][2][$pos][0] = $cattotpast[$ln][$cat];
                if ($useweights == 1) {
                    if ($cattotpast[$ln][$cat] > 0 && $catposspast[$cat] > 0) {
                        $totpast += $cattotpast[$ln][$cat] * $cats[$cat][5] / (100 * $catposspast[$cat]);
                        //weight total
                    }
                }
            } else {
                if (isset($cattotpastec[$ln][$cat])) {
                    $cattotpast[$ln][$cat] = array_sum($cattotpastec[$ln][$cat]);
                    $gb[$ln][2][$pos][0] = $cattotpast[$ln][$cat];
                } else {
                    //no items in category yet?
                    $gb[$ln][2][$pos][0] = 0;
                }
            }
            if (isset($cattotcur[$ln][$cat])) {
                //cur items
                //cats: name,scale,scaletype,chop,drop,weight,calctype
                //if ($cats[$cat][4]!=0 && abs($cats[$cat][4])<count($cattotcur[$ln][$cat])) { //if drop is set and have enough items
                if ($cats[$cat][7] == 1) {
                    foreach ($cattotcur[$ln][$cat] as $col => $v) {
                        if ($gb[0][1][$col][2] == 0) {
                            $cattotcur[$ln][$cat][$col] = 0;
                        } else {
                            $cattotcur[$ln][$cat][$col] = $v / $gb[0][1][$col][2];
                            //convert to percents
                        }
                    }
                    if ($cats[$cat][4] != 0 && abs($cats[$cat][4]) < count($cattotcur[$ln][$cat])) {
                        asort($cattotcur[$ln][$cat], SORT_NUMERIC);
                        if ($cats[$cat][4] < 0) {
                            //doing keep n
                            $ntodrop = count($cattotcur[$ln][$cat]) + $cats[$cat][4];
                        } else {
                            //doing drop n
                            $ntodrop = $cats[$cat][4] - ($catitemcntcur[$cat] - count($cattotcur[$ln][$cat]));
                        }
                        if ($ntodrop > 0) {
                            $ndropcnt = 0;
                            foreach ($cattotcur[$ln][$cat] as $col => $v) {
                                $gb[$ln][1][$col][5] += 2;
                                //mark as dropped
                                $ndropcnt++;
                                if ($ndropcnt == $ntodrop) {
                                    break;
                                }
                            }
                        }
                        while (count($cattotcur[$ln][$cat]) < $catitemcntcur[$cat]) {
                            array_unshift($cattotcur[$ln][$cat], 0);
                        }
                        $cattotcur[$ln][$cat] = array_slice($cattotcur[$ln][$cat], $cats[$cat][4]);
                        $tokeep = $cats[$cat][4] < 0 ? abs($cats[$cat][4]) : $catitemcntcur[$cat] - $cats[$cat][4];
                        $cattotcur[$ln][$cat] = round($catposscur[$cat] * array_sum($cattotcur[$ln][$cat]) / $tokeep, 1);
                    } else {
                        $cattotcur[$ln][$cat] = round($catposscur[$cat] * array_sum($cattotcur[$ln][$cat]) / count($cattotcur[$ln][$cat]), 2);
                    }
                } else {
                    $cattotcur[$ln][$cat] = array_sum($cattotcur[$ln][$cat]);
                }
                if ($cats[$cat][1] != 0) {
                    //scale is set
                    if ($cats[$cat][2] == 0) {
                        //pts scale
                        $cattotcur[$ln][$cat] = round($catposscur[$cat] * ($cattotcur[$ln][$cat] / $cats[$cat][1]), 1);
                    } else {
                        if ($cats[$cat][2] == 1) {
                            //percent scale
                            $cattotcur[$ln][$cat] = round($cattotcur[$ln][$cat] * (100 / $cats[$cat][1]), 1);
                        }
                    }
                }
                if (isset($cattotcurec[$ln][$cat])) {
                    $cattotcur[$ln][$cat] += array_sum($cattotcurec[$ln][$cat]);
                }
                if ($useweights == 0 && $cats[$cat][5] > -1) {
                    //use fixed pt value for cat
                    $cattotcur[$ln][$cat] = $catposscur[$cat] == 0 ? 0 : round($cats[$cat][5] * ($cattotcur[$ln][$cat] / $catposscur[$cat]), 1);
                }
                if ($cats[$cat][3] > 0) {
                    if ($useweights == 0 && $cats[$cat][5] > -1) {
                        //set cat pts
                        $cattotcur[$ln][$cat] = min($cats[$cat][5] * $cats[$cat][3], $cattotcur[$ln][$cat]);
                    } else {
                        $cattotcur[$ln][$cat] = min($catposscur[$cat] * $cats[$cat][3], $cattotcur[$ln][$cat]);
                    }
                }
                $gb[$ln][2][$pos][1] = $cattotcur[$ln][$cat];
                if ($useweights == 1) {
                    if ($cattotcur[$ln][$cat] > 0 && $catposscur[$cat] > 0) {
                        $totcur += $cattotcur[$ln][$cat] * $cats[$cat][5] / (100 * $catposscur[$cat]);
                        //weight total
                    }
                }
            } else {
                if (isset($cattotcurec[$ln][$cat])) {
                    $cattotcur[$ln][$cat] = array_sum($cattotcurec[$ln][$cat]);
                    $gb[$ln][2][$pos][1] = $cattotcur[$ln][$cat];
                } else {
                    //no items in category yet?
                    $gb[$ln][2][$pos][1] = 0;
                }
            }
            if (isset($cattotfuture[$ln][$cat])) {
                //future items
                //cats: name,scale,scaletype,chop,drop,weight,calctype
                //if ($cats[$cat][4]!=0 && abs($cats[$cat][4])<count($cattotfuture[$ln][$cat])) { //if drop is set and have enough items
                if ($cats[$cat][7] == 1) {
                    foreach ($cattotfuture[$ln][$cat] as $col => $v) {
                        if ($gb[0][1][$col][2] == 0) {
                            $cattotfuture[$ln][$cat][$col] = 0;
                        } else {
                            $cattotfuture[$ln][$cat][$col] = $v / $gb[0][1][$col][2];
                            //convert to percents
                        }
                    }
                    if ($cats[$cat][4] != 0 && abs($cats[$cat][4]) < count($cattotfuture[$ln][$cat])) {
                        asort($cattotfuture[$ln][$cat], SORT_NUMERIC);
                        if ($cats[$cat][4] < 0) {
                            //doing keep n
                            $ntodrop = count($cattotfuture[$ln][$cat]) + $cats[$cat][4];
                        } else {
                            //doing drop n
                            $ntodrop = $cats[$cat][4] - ($catitemcntfuture[$cat] - count($cattotfuture[$ln][$cat]));
                        }
                        if ($ntodrop > 0) {
                            $ndropcnt = 0;
                            foreach ($cattotfuture[$ln][$cat] as $col => $v) {
                                $gb[$ln][1][$col][5] += 4;
                                //mark as dropped
                                $ndropcnt++;
                                if ($ndropcnt == $ntodrop) {
                                    break;
                                }
                            }
                        }
                        while (count($cattotfuture[$ln][$cat]) < $catitemcntfuture[$cat]) {
                            array_unshift($cattotfuture[$ln][$cat], 0);
                        }
                        $cattotfuture[$ln][$cat] = array_slice($cattotfuture[$ln][$cat], $cats[$cat][4]);
                        $tokeep = $cats[$cat][4] < 0 ? abs($cats[$cat][4]) : $catitemcntfuture[$cat] - $cats[$cat][4];
                        $cattotfuture[$ln][$cat] = round($catpossfuture[$cat] * array_sum($cattotfuture[$ln][$cat]) / $tokeep, 1);
                    } else {
                        $cattotfuture[$ln][$cat] = round($catpossfuture[$cat] * array_sum($cattotfuture[$ln][$cat]) / count($cattotfuture[$ln][$cat]), 2);
                    }
                } else {
                    $cattotfuture[$ln][$cat] = array_sum($cattotfuture[$ln][$cat]);
                }
                if ($cats[$cat][1] != 0) {
                    //scale is set
                    if ($cats[$cat][2] == 0) {
                        //pts scale
                        $cattotfuture[$ln][$cat] = round($catpossfuture[$cat] * ($cattotfuture[$ln][$cat] / $cats[$cat][1]), 1);
                    } else {
                        if ($cats[$cat][2] == 1) {
                            //percent scale
                            $cattotfuture[$ln][$cat] = round($cattotfuture[$ln][$cat] * (100 / $cats[$cat][1]), 1);
                        }
                    }
                }
                if (isset($cattotfutureec[$ln][$cat])) {
                    $cattotfuture[$ln][$cat] += array_sum($cattotfutureec[$ln][$cat]);
                }
                if ($useweights == 0 && $cats[$cat][5] > -1) {
                    //use fixed pt value for cat
                    $cattotfuture[$ln][$cat] = round($cats[$cat][5] * ($cattotfuture[$ln][$cat] / $catpossfuture[$cat]), 1);
                }
                if ($cats[$cat][3] > 0) {
                    if ($useweights == 0 && $cats[$cat][5] > -1) {
                        //set cat pts
                        $cattotfuture[$ln][$cat] = min($cats[$cat][5] * $cats[$cat][3], $cattotfuture[$ln][$cat]);
                    } else {
                        $cattotfuture[$ln][$cat] = min($catpossfuture[$cat] * $cats[$cat][3], $cattotfuture[$ln][$cat]);
                    }
                }
                $gb[$ln][2][$pos][2] = $cattotfuture[$ln][$cat];
                if ($useweights == 1) {
                    if ($cattotfuture[$ln][$cat] > 0 && $catpossfuture[$cat] > 0) {
                        $totfuture += $cattotfuture[$ln][$cat] * $cats[$cat][5] / (100 * $catpossfuture[$cat]);
                        //weight total
                    }
                }
            } else {
                if (isset($cattotfutureec[$ln][$cat])) {
                    $cattotfuture[$ln][$cat] = array_sum($cattotfutureec[$ln][$cat]);
                    $gb[$ln][2][$pos][2] = $cattotfuture[$ln][$cat];
                } else {
                    //no items in category yet?
                    $gb[$ln][2][$pos][2] = 0;
                }
            }
            //update attempted for this student; adjust for drops
            if ($cats[$cat][4] != 0 && abs($cats[$cat][4]) < count($catpossattemptedstu[$cat])) {
                //same for past&current
                asort($catpossattemptedstu[$cat], SORT_NUMERIC);
                $catpossattemptedstu[$cat] = array_slice($catpossattemptedstu[$cat], $cats[$cat][4]);
            }
            if (isset($cattotattempted[$ln][$cat])) {
                //attempted and attempted items
                $catitemcntattempted[$cat] = count($catpossattemptedstu[$cat]);
                $catpossattemptedstu[$cat] = array_sum($catpossattemptedstu[$cat]);
                //cats: name,scale,scaletype,chop,drop,weight
                if ($cats[$cat][4] != 0 && abs($cats[$cat][4]) < count($cattotattempted[$ln][$cat])) {
                    //if drop is set and have enough items
                    foreach ($cattotattempted[$ln][$cat] as $col => $v) {
                        if ($gb[0][1][$col][2] == 0) {
                            $cattotattempted[$ln][$cat][$col] = 0;
                        } else {
                            $cattotattempted[$ln][$cat][$col] = $v / $gb[0][1][$col][2];
                            //convert to percents
                        }
                    }
                    asort($cattotattempted[$ln][$cat], SORT_NUMERIC);
                    if ($cats[$cat][4] < 0) {
                        //doing keep n
                        $ntodrop = count($cattotattempted[$ln][$cat]) + $cats[$cat][4];
                    } else {
                        //doing drop n
                        $ntodrop = $cats[$cat][4];
                        // - ($catitemcntattempted[$cat]-count($cattotattempted[$ln][$cat]));
                    }
                    if ($ntodrop > 0) {
                        $ndropcnt = 0;
                        foreach ($cattotattempted[$ln][$cat] as $col => $v) {
                            $gb[$ln][1][$col][5] += 8;
                            //mark as dropped
                            $ndropcnt++;
                            if ($ndropcnt == $ntodrop) {
                                break;
                            }
                        }
                    }
                    while (count($cattotattempted[$ln][$cat]) < $catitemcntattempted[$cat]) {
                        array_unshift($cattotattempted[$ln][$cat], 0);
                    }
                    $cattotattempted[$ln][$cat] = array_slice($cattotattempted[$ln][$cat], $cats[$cat][4]);
                    //$tokeep = ($cats[$cat][4]<0)? abs($cats[$cat][4]) : ($catitemcntattempted[$cat] - $cats[$cat][4]);
                    $tokeep = $catitemcntattempted[$cat];
                    $cattotattempted[$ln][$cat] = round($catpossattemptedstu[$cat] * array_sum($cattotattempted[$ln][$cat]) / $tokeep, 1);
                } else {
                    $cattotattempted[$ln][$cat] = array_sum($cattotattempted[$ln][$cat]);
                }
                if ($cats[$cat][1] != 0) {
                    //scale is set
                    if ($cats[$cat][2] == 0) {
                        //pts scale
                        $cattotattempted[$ln][$cat] = round($catpossattemptedstu[$cat] * ($cattotattempted[$ln][$cat] / $cats[$cat][1]), 1);
                    } else {
                        if ($cats[$cat][2] == 1) {
                            //percent scale
                            $cattotattempted[$ln][$cat] = round($cattotattempted[$ln][$cat] * (100 / $cats[$cat][1]), 1);
                        }
                    }
                }
                if (isset($cattotattemptedec[$ln][$cat])) {
                    //add in EC
                    $cattotattempted[$ln][$cat] += array_sum($cattotattemptedec[$ln][$cat]);
                }
                if ($useweights == 0 && $cats[$cat][5] > -1) {
                    //use fixed pt value for cat
                    $cattotattempted[$ln][$cat] = $catpossattemptedstu[$cat] == 0 ? 0 : round($cats[$cat][5] * ($cattotattempted[$ln][$cat] / $catpossattemptedstu[$cat]), 1);
                    $catpossattemptedstu[$cat] = $catpossattemptedstu[$cat] == 0 ? 0 : $cats[$cat][5];
                }
                if ($cats[$cat][3] > 0) {
                    //chop score - no over 100%
                    if ($useweights == 0 && $cats[$cat][5] > -1) {
                        //set cat pts
                        $cattotattempted[$ln][$cat] = min($cats[$cat][5] * $cats[$cat][3], $cattotattempted[$ln][$cat]);
                    } else {
                        $cattotattempted[$ln][$cat] = min($catpossattemptedstu[$cat] * $cats[$cat][3], $cattotattempted[$ln][$cat]);
                    }
                }
                $gb[$ln][2][$pos][3] = $cattotattempted[$ln][$cat];
                if ($useweights == 1) {
                    if ($cattotattempted[$ln][$cat] > 0 && $catpossattemptedstu[$cat] > 0) {
                        $totattempted += $cattotattempted[$ln][$cat] * $cats[$cat][5] / (100 * $catpossattemptedstu[$cat]);
                        //weight total
                    }
                }
                $gb[$ln][2][$pos][4] = $catpossattemptedstu[$cat];
            } else {
                if (isset($cattotattemptedec[$ln][$cat])) {
                    $cattotattempted[$ln][$cat] = array_sum($cattotattemptedec[$ln][$cat]);
                    $catpossattemptedstu[$cat] = 0;
                    $gb[$ln][2][$pos][3] = $cattotattempted[$ln][$cat];
                    $gb[$ln][2][$pos][4] = 0;
                } else {
                    //no items in category yet?
                    $gb[$ln][2][$pos][3] = 0;
                    $gb[$ln][2][$pos][4] = 0;
                    $catpossattemptedstu[$cat] = 0;
                }
            }
            if ($catpossattemptedstu[$cat] > 0 || count($catpossattemptedecstu[$cat]) > 0) {
                $cattotweightattempted += $cats[$cat][5];
            }
            $pos++;
        }
        $overallptsattempted = array_sum($catpossattemptedstu);
        if ($useweights == 0) {
            //use points grading method
            if (!isset($cattotpast)) {
                $totpast = 0;
            } else {
                $totpast = array_sum($cattotpast[$ln]);
            }
            if (!isset($cattotcur)) {
                $totcur = 0;
            } else {
                $totcur = array_sum($cattotcur[$ln]);
            }
            if (!isset($cattotfuture)) {
                $totfuture = 0;
            } else {
                $totfuture = array_sum($cattotfuture[$ln]);
            }
            if (!isset($cattotattempted)) {
                $totattempted = 0;
            } else {
                $totattempted = array_sum($cattotattempted[$ln]);
            }
            $gb[$ln][3][0] = $totpast;
            $gb[$ln][3][1] = $totcur;
            $gb[$ln][3][2] = $totfuture;
            $gb[$ln][3][6] = $totattempted;
            $gb[$ln][3][7] = $overallptsattempted;
            if ($overallptspast > 0) {
                $gb[$ln][3][3] = sprintf("%01.1f", 100 * $totpast / $overallptspast);
            } else {
                $gb[$ln][3][3] = '0.0';
            }
            if ($overallptscur > 0) {
                $gb[$ln][3][4] = sprintf("%01.1f", 100 * $totcur / $overallptscur);
            } else {
                $gb[$ln][3][4] = '0.0';
            }
            if ($overallptsfuture > 0) {
                $gb[$ln][3][5] = sprintf("%01.1f", 100 * $totfuture / $overallptsfuture);
            } else {
                $gb[$ln][3][5] = '0.0';
            }
            if ($overallptsattempted > 0) {
                $gb[$ln][3][8] = sprintf("%01.1f", 100 * $totattempted / $overallptsattempted);
            } else {
                $gb[$ln][3][8] = '0.0';
            }
        } else {
            if ($useweights == 1) {
                //use weights (%) grading method
                //already calculated $tot
                //if ($overallptspast>0) {
                //	$totpast = 100*($totpast/$overallptspast);
                //} else {
                //	$totpast = 0;
                //}
                if ($cattotweightpast == 0) {
                    $gb[$ln][3][0] = '0.0';
                } else {
                    $gb[$ln][3][0] = sprintf("%01.1f", 10000 * $totpast / $cattotweightpast);
                }
                $gb[$ln][3][3] = null;
                //if ($overallptscur>0) {
                //	$totcur = 100*($totcur/$overallptscur);
                //} else {
                //	$totcur = 0;
                //}
                if ($cattotweightcur == 0) {
                    $gb[$ln][3][1] = '0.0';
                } else {
                    $gb[$ln][3][1] = sprintf("%01.1f", 10000 * $totcur / $cattotweightcur);
                }
                $gb[$ln][3][4] = null;
                //if ($overallptsfuture>0) {
                //	$totfuture = 100*($totfuture/$overallptsfuture);
                //} else {
                //	$totfuture = 0;
                //}
                if ($cattotweightfuture == 0) {
                    $gb[$ln][3][2] = '0.0';
                } else {
                    $gb[$ln][3][2] = sprintf("%01.1f", 10000 * $totfuture / $cattotweightfuture);
                }
                $gb[$ln][3][5] = null;
                if ($cattotweightattempted == 0) {
                    $gb[$ln][3][6] = '0.0';
                } else {
                    //$gb[$ln][3][6] = $totattempted.'/'.$cattotweightattempted;
                    $gb[$ln][3][6] = sprintf("%01.1f", 10000 * $totattempted / $cattotweightattempted);
                }
                $gb[$ln][3][7] = null;
                $gb[$ln][3][8] = null;
            }
        }
    }
    if ($limuser < 1) {
        //create averages
        $gb[$ln][0][0] = "Averages";
        $avgs = array();
        for ($j = 0; $j < count($gb[0][1]); $j++) {
            //foreach assessment
            $avgs[$j] = array();
            for ($i = 1; $i < $ln; $i++) {
                //foreach student
                if (isset($gb[$i][1][$j][0]) && $gb[$i][4][1] == 0) {
                    //score exists and student is not locked
                    if ($gb[$i][1][$j][3] % 10 == 0 && is_numeric($gb[$i][1][$j][0])) {
                        $avgs[$j][] = $gb[$i][1][$j][0];
                    }
                }
            }
            if (count($avgs[$j]) > 0) {
                sort($avgs[$j], SORT_NUMERIC);
                $fivenum = array();
                for ($k = 0; $k < 5; $k++) {
                    $fivenum[] = gbpercentile($avgs[$j], $k * 25);
                }
                $fivenumsum = 'n = ' . count($avgs[$j]) . '<br/>';
                $fivenumsum .= implode(',&nbsp;', $fivenum);
                if ($gb[0][1][$j][2] > 0) {
                    for ($k = 0; $k < 5; $k++) {
                        $fivenum[$k] = round(100 * $fivenum[$k] / $gb[0][1][$j][2], 1);
                    }
                    $fivenumsum .= '<br/>' . implode('%,&nbsp;', $fivenum) . '%';
                }
            } else {
                $fivenumsum = '';
            }
            $gb[0][1][$j][9] = $fivenumsum;
            //$gb[0][1][$j][9] = gbpercentile($avgs[$j],0).',&nbsp;'.gbpercentile($avgs[$j],25).',&nbsp;'.gbpercentile($avgs[$j],50).',&nbsp;'.gbpercentile($avgs[$j],75).',&nbsp;'.gbpercentile($avgs[$j],100);
        }
        //cat avgs
        $catavgs = array();
        for ($j = 0; $j < count($gb[0][2]); $j++) {
            //category headers
            $catavgs[$j][0] = array();
            $catavgs[$j][1] = array();
            $catavgs[$j][2] = array();
            $catavgs[$j][3] = array();
            for ($i = 1; $i < $ln; $i++) {
                //foreach student
                if ($gb[$i][4][1] == 0) {
                    $catavgs[$j][0][] = $gb[$i][2][$j][0];
                    $catavgs[$j][1][] = $gb[$i][2][$j][1];
                    $catavgs[$j][2][] = $gb[$i][2][$j][2];
                    if ($gb[$i][2][$j][4] > 0) {
                        $catavgs[$j][3][] = round(100 * $gb[$i][2][$j][3] / $gb[$i][2][$j][4], 1);
                    } else {
                        //$catavgs[$j][3][] = 0;
                    }
                }
            }
            for ($i = 0; $i < 4; $i++) {
                if (count($catavgs[$j][$i]) > 0) {
                    sort($catavgs[$j][$i], SORT_NUMERIC);
                    $fivenum = array();
                    for ($k = 0; $k < 5; $k++) {
                        $fivenum[] = gbpercentile($catavgs[$j][$i], $k * 25);
                    }
                    if ($i == 3) {
                        $fivenumsum = implode('%,&nbsp;', $fivenum) . '%';
                    } else {
                        $fivenumsum = implode(',&nbsp;', $fivenum);
                    }
                    if ($i < 3 && $gb[0][2][$j][3 + $i] > 0) {
                        for ($k = 0; $k < 5; $k++) {
                            $fivenum[$k] = round(100 * $fivenum[$k] / $gb[0][2][$j][3 + $i], 1);
                        }
                        $fivenumsum .= '<br/>' . implode('%,&nbsp;', $fivenum) . '%';
                    }
                } else {
                    $fivenumsum = '';
                }
                $gb[0][2][$j][6 + $i] = $fivenumsum;
            }
        }
        //tot avgs
        $totavgs = array();
        for ($j = 0; $j < count($gb[1][3]); $j++) {
            if ($gb[1][3][$j] === null) {
                continue;
            }
            $totavgs[$j] = array();
            for ($i = 1; $i < $ln; $i++) {
                //foreach student
                if ($gb[$i][4][1] == 0) {
                    $totavgs[$j][] = $gb[$i][3][$j];
                }
            }
        }
        foreach ($avgs as $j => $avg) {
            if (count($avg) > 0) {
                $gb[$ln][1][$j][0] = round(array_sum($avg) / count($avg), 1);
                $gb[$ln][1][$j][4] = 'average';
            }
        }
        foreach ($catavgs as $j => $avg) {
            for ($m = 0; $m < 4; $m++) {
                if (count($avg[$m]) > 0) {
                    $gb[$ln][2][$j][$m] = round(array_sum($avg[$m]) / count($avg[$m]), 1);
                } else {
                    $gb[$ln][2][$j][$m] = 0;
                }
            }
        }
        foreach ($totavgs as $j => $avg) {
            if (count($avg) > 0) {
                $gb[$ln][3][$j] = round(array_sum($avg) / count($avg), 1);
            }
        }
        $gb[$ln][4][0] = -1;
    }
    if ($limuser == -1) {
        $gb[1] = $gb[$ln];
    }
    return $gb;
}
         list($questions, $bestquestions) = explode(";", $line['questions']);
         $questions = explode(",", $bestquestions);
     }
     $sp = explode(';', $line['bestscores']);
     $scores = explode(',', $sp[0]);
     $attp = explode(',', $line['bestattempts']);
     $bla = explode('~', $line['bestlastanswers']);
     $timeot = explode(',', $line['timeontask']);
     $k = array_search($qid, $questions);
     if ($k === false) {
         continue;
     }
     if (strpos($scores[$k], '-1') !== false) {
         $stuincomp[$line['userid']] = 1;
     } else {
         $stuscores[$line['userid']] = getpts($scores[$k]);
         $stuatt[$line['userid']] = $attp[$k];
         $sturegens[$line['userid']] = substr_count($bla[$k], 'ReGen');
         $timeot[$k] = explode('~', $timeot[$k]);
         $stutimes[$line['userid']] = array_sum($timeot[$k]);
     }
 }
 if ($type == 'incomp') {
     $stunames = getstunames(array_keys($stuincomp));
     natsort($stunames);
     echo '<h3>Students who have started the assignment, but have not completed this question</h3><ul>';
     foreach ($stunames as $name) {
         echo '<li>' . $name . '</li>';
     }
     echo '</ul>';
 } else {
Пример #4
0
function printscore($sc)
{
    if (strpos($sc, '~') === false) {
        return array($sc, '');
    } else {
        $pts = getpts($sc);
        $sc = str_replace('-1', 'N/A', $sc);
        $sc = str_replace('~', ', ', $sc);
        return array($pts, $sc);
    }
}
Пример #5
0
        if (!isset($tcnt[$ques])) {
            $tcnt[$ques] = 0;
        }
        if (!isset($attempts[$ques])) {
            $attempts[$ques] = 0;
        }
        if (!isset($regens[$ques])) {
            $regens[$ques] = 0;
        }
        if (!isset($timeontask[$ques])) {
            $timeontask[$ques] = 0;
        }
        if (strpos($scores[$k], '-1') !== false) {
            $qincomplete[$ques] += 1;
        }
        $qtotal[$ques] += getpts($scores[$k]);
        $attempts[$ques] += $attp[$k];
        $regens[$ques] += substr_count($bla[$k], 'ReGen');
        $qcnt[$ques] += 1;
        $timeot[$k] = explode('~', $timeot[$k]);
        $tcnt[$ques] += count($timeot[$k]);
        $timeontask[$ques] += array_sum($timeot[$k]);
    }
    if ($line['endtime'] > 0 && $line['starttime'] > 0) {
        $timetaken[] = $line['endtime'] - $line['starttime'];
    } else {
        $timetaken[] = 0;
    }
}
$vidcnt = array();
if (count($qcnt) > 0) {
Пример #6
0
     $adata[$row[0]]['la'] = explode('~', $row[4]);
 }
 $query = "SELECT userid,bestseeds,bestscores,bestattempts,bestlastanswers FROM imas_assessment_sessions WHERE assessmentid IN (" . implode(',', $source) . ")";
 $result = mysql_query($query) or die("Query failed : " . mysql_error());
 while ($row = mysql_fetch_row($result)) {
     $seeds = explode(',', $row[1]);
     $sp = explode(';', $row[2]);
     $scores = explode(',', $sp[0]);
     if (count($sp) > 1) {
         $rawscore = explode(',', $sp[1]);
         $firstscore = explode(',', $sp[1]);
     }
     $att = explode(',', $row[3]);
     $la = explode(',', $row[4]);
     foreach ($scores as $k => $v) {
         if (getpts($v) > getpts($adata[$row[0]]['scores'][$k])) {
             $adata[$row[0]]['scores'][$k] = $scores[$k];
             if (isset($rawscore) && isset($rawscore[$k])) {
                 $adata[$row[0]]['rawscore'][$k] = $rawscore[$k];
                 $adata[$row[0]]['firstscore'][$k] = $firstscore[$k];
             }
             $adata[$row[0]]['seeds'][$k] = $seeds[$k];
             $adata[$row[0]]['attempts'][$k] = $attempts[$k];
             $adata[$row[0]]['la'][$k] = $la[$k];
         }
     }
 }
 foreach ($adata as $uid => $val) {
     $bestscorelist = implode(',', $val['scores']);
     if (isset($val['rawscore'])) {
         $bestscorelist .= ';' . implode(',', $val['rawscore']) . ';' . implode(',', $val['firstscore']);
Пример #7
0
function gbtable()
{
    global $cid, $isteacher, $istutor, $tutorid, $userid;
    if ($isteacher && func_num_args() > 1) {
        $limuser = func_get_arg(1);
    } else {
        if (!$isteacher && !$istutor) {
            $limuser = $userid;
        } else {
            $limuser = 0;
        }
    }
    $isdiag = false;
    $category = array();
    if ($isteacher || $istutor) {
        $query = "SELECT sel1name,sel2name FROM imas_diags WHERE cid='{$cid}'";
        $result = mysql_query($query) or die("Query failed : " . mysql_error());
        if (mysql_num_rows($result) > 0) {
            $isdiag = true;
            $sel1name = mysql_result($result, 0, 0);
            $sel2name = mysql_result($result, 0, 1);
        }
    }
    $gb = array();
    $ln = 0;
    //Build user ID headers
    $gb[0][0][0] = "Name";
    if ($isdiag) {
        $gb[0][0][1] = "ID";
        $gb[0][0][2] = "Term";
        $gb[0][0][3] = ucfirst($sel1name);
        $gb[0][0][4] = ucfirst($sel2name);
    } else {
        $gb[0][0][1] = "Username";
    }
    $query = "SELECT count(id) FROM imas_students WHERE imas_students.courseid='{$cid}' AND imas_students.section IS NOT NULL";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    if (mysql_result($result, 0, 0) > 0) {
        $hassection = true;
    } else {
        $hassection = false;
    }
    $query = "SELECT count(id) FROM imas_students WHERE imas_students.courseid='{$cid}' AND imas_students.code IS NOT NULL";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    if (mysql_result($result, 0, 0) > 0) {
        $hascode = true;
    } else {
        $hascode = false;
    }
    if ($hassection) {
        $gb[0][0][] = "Section";
    }
    if ($hascode) {
        $gb[0][0][] = "Code";
    }
    //Pull Assessment Info
    $now = time();
    $query = "SELECT id,name,defpoints,deffeedback,timelimit,minscore,startdate,enddate,itemorder,gbcategory,cntingb FROM imas_assessments WHERE courseid='{$cid}' ";
    if (!$isteacher) {
        $query .= "AND cntingb>0 ";
    }
    if (!$isteacher) {
        $query .= "AND startdate<{$now} ";
    }
    $query .= "ORDER BY enddate";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    $overallpts = 0;
    $now = time();
    $kcnt = 0;
    $assessments = array();
    $grades = array();
    $timelimits = array();
    $minscores = array();
    $assessmenttype = array();
    $enddate = array();
    $avail = array();
    $sa = array();
    $category = array();
    $name = array();
    $possible = array();
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $assessments[$kcnt] = $line['id'];
        $timelimits[$kcnt] = $line['timelimit'];
        $minscores[$kcnt] = $line['minscore'];
        $deffeedback = explode('-', $line['deffeedback']);
        $assessmenttype[$kcnt] = $deffeedback[0];
        $sa[$kcnt] = $deffeedback[1];
        $enddate[$kcnt] = $line['enddate'];
        if ($now < $line['startdate']) {
            $avail[$kcnt] = 2;
        } else {
            if ($now < $line['enddate']) {
                $avail[$kcnt] = 1;
            } else {
                $avail[$kcnt] = 0;
            }
        }
        $category[$kcnt] = $line['gbcategory'];
        $name[$kcnt] = $line['name'];
        $cntingb[$kcnt] = $line['cntingb'];
        //0: ignore, 1: count, 2: extra credit, 3: no count but show
        $aitems = explode(',', $line['itemorder']);
        foreach ($aitems as $k => $v) {
            if (strpos($v, '~') !== FALSE) {
                $sub = explode('~', $v);
                if (strpos($sub[0], '|') === false) {
                    //backwards compat
                    $aitems[$k] = $sub[0];
                    $aitemcnt[$k] = 1;
                } else {
                    $grpparts = explode('|', $sub[0]);
                    $aitems[$k] = $sub[1];
                    $aitemcnt[$k] = $grpparts[0];
                }
            } else {
                $aitemcnt[$k] = 1;
            }
        }
        $query = "SELECT points,id FROM imas_questions WHERE assessmentid='{$line['id']}'";
        $result2 = mysql_query($query) or die("Query failed : {$query}: " . mysql_error());
        $totalpossible = 0;
        while ($r = mysql_fetch_row($result2)) {
            if (in_array($r[1], $aitems)) {
                //only use first item from grouped questions for total pts
                if ($r[0] == 9999) {
                    $totalpossible += $aitemcnt[$k] * $line['defpoints'];
                    //use defpoints
                } else {
                    $totalpossible += $aitemcnt[$k] * $r[0];
                    //use points from question
                }
            }
        }
        $possible[$kcnt] = $totalpossible;
        $kcnt++;
    }
    //Pull Offline Grade item info
    $query = "SELECT * from imas_gbitems WHERE courseid='{$cid}' ";
    if (!$isteacher) {
        $query .= "AND showdate<{$now} ";
    }
    if (!$isteacher) {
        $query .= "AND cntingb>0 ";
    }
    $query .= "ORDER BY showdate";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $grades[$kcnt] = $line['id'];
        $assessmenttype[$kcnt] = "Offline";
        $category[$kcnt] = $line['gbcategory'];
        $enddate[$kcnt] = $line['showdate'];
        if ($now < $line['showdate']) {
            $avail[$kcnt] = 2;
        } else {
            $avail[$kcnt] = 0;
        }
        $possible[$kcnt] = $line['points'];
        $name[$kcnt] = $line['name'];
        $cntingb[$kcnt] = $line['cntingb'];
        $kcnt++;
    }
    //Pull Discussion Grade info
    $query = "SELECT id,name,gbcategory,enddate,points FROM imas_forums WHERE courseid='{$cid}' AND points>0 ";
    if (!$isteacher) {
        $query .= "AND startdate<{$now} ";
    }
    $query .= "ORDER BY enddate";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $discuss[$kcnt] = $line['id'];
        $assessmenttype[$kcnt] = "Discussion";
        $category[$kcnt] = $line['gbcategory'];
        $enddate[$kcnt] = $line['enddate'];
        if ($now < $line['showdate']) {
            $avail[$kcnt] = 2;
        } else {
            $avail[$kcnt] = 0;
        }
        $possible[$kcnt] = $line['points'];
        $name[$kcnt] = $line['name'];
        $cntingb[$kcnt] = 1;
        $kcnt++;
    }
    //Pull Gradebook Scheme info
    $query = "SELECT useweights,orderby,defaultcat,usersort FROM imas_gbscheme WHERE courseid='{$cid}'";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    list($useweights, $orderby, $defaultcat, $usersort) = mysql_fetch_row($result);
    $cats = array();
    $catcolcnt = 0;
    //Pull Categories:  Name, scale, scaletype, chop, drop, weight
    if (in_array(0, $category)) {
        //define default category, if used
        $cats[0] = explode(',', $defaultcat);
        array_unshift($cats[0], "Default");
        array_push($cats[0], $catcolcnt);
        $catcolcnt++;
    }
    $query = "SELECT id,name,scale,scaletype,chop,dropn,weight FROM imas_gbcats WHERE courseid='{$cid}' ";
    $query .= "ORDER BY name";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($row = mysql_fetch_row($result)) {
        if (in_array($row[0], $category)) {
            //define category if used
            $cats[$row[0]] = array_slice($row, 1);
            array_push($cats[$row[0]], $catcolcnt);
            $catcolcnt++;
        }
    }
    //create item headers
    $pos = 0;
    $catposs = array();
    $catpossec = array();
    $catpossfuture = array();
    $catpossfutureec = array();
    $itemorder = array();
    if ($orderby == 1) {
        //order $category by enddate
        asort($enddate, SORT_NUMERIC);
        $newcategory = array();
        foreach ($enddate as $k => $v) {
            $newcategory[$k] = $category[$k];
        }
        $category = $newcategory;
    } else {
        if ($orderby == 3) {
            //order $category alpha
            asort($name);
            $newcategory = array();
            foreach ($name as $k => $v) {
                $newcategory[$k] = $category[$k];
            }
            $category = $newcategory;
        }
    }
    foreach (array_keys($cats) as $cat) {
        //foreach category
        $catposs[$cat] = array();
        $catpossfuture[$cat] = array();
        $catkeys = array_keys($category, $cat);
        //pull items in that category
        if (($orderby & 1) == 1) {
            //order by category
            array_splice($itemorder, count($itemorder), 0, $catkeys);
        }
        foreach ($catkeys as $k) {
            if ($avail[$k] < 2) {
                if ($assessmenttype[$k] != "Practice" && $cntingb[$k] == 1) {
                    $catposs[$cat][] = $possible[$k];
                    //create category totals
                } else {
                    if ($cntingb[$k] == 2) {
                        $catpossec[$cat][] = 0;
                    }
                }
            } else {
                if ($assessmenttype[$k] != "Practice" && $cntingb[$k] == 1) {
                    $catpossfuture[$cat][] = $possible[$k];
                    //create category totals
                } else {
                    if ($cntingb[$k] == 2) {
                        $catpossfutureec[$cat][] = 0;
                    }
                }
            }
            if (($orderby & 1) == 1) {
                //display item header if displaying by category
                //$cathdr[$pos] = $cats[$cat][6];
                $gb[0][1][$pos][0] = $name[$k];
                //item name
                $gb[0][1][$pos][1] = $cats[$cat][0];
                //item category name
                $gb[0][1][$pos][2] = $possible[$k];
                //points possible
                $gb[0][1][$pos][3] = $avail[$k];
                //0 past, 1 current, 2 future
                $gb[0][1][$pos][4] = $cntingb[$k];
                //0 no count and hide, 1 count, 2 EC, 3 no count
                if ($assessmenttype[$k] == "Practice") {
                    $gb[0][1][$pos][5] = 1;
                    //0 regular, 1 practice test
                } else {
                    $gb[0][1][$pos][5] = 0;
                }
                if (isset($assessments[$k])) {
                    $gb[0][1][$pos][6] = 0;
                    //0 online, 1 offline
                    $gb[0][1][$pos][7] = $cid;
                    //courseid
                    $gb[0][1][$pos][8] = $assessments[$k];
                } else {
                    if (isset($grades[$k])) {
                        $gb[0][1][$pos][6] = 1;
                        //0 online, 1 offline
                        $gb[0][1][$pos][7] = $cid;
                        //courseid
                        $gb[0][1][$pos][8] = $grades[$k];
                    } else {
                        if (isset($discuss[$k])) {
                            $gb[0][1][$pos][6] = 2;
                            //0 online, 1 offline, 2 discuss
                            $gb[0][1][$pos][7] = $cid;
                            //courseid
                            $gb[0][1][$pos][8] = $discuss[$k];
                        }
                    }
                }
                $pos++;
            }
        }
    }
    if (($orderby & 1) == 0) {
        //if not grouped by category
        if ($orderby == 0) {
            asort($enddate, SORT_NUMERIC);
            $itemorder = array_keys($enddate);
        } else {
            if ($orderby == 2) {
                asort($name);
                $itemorder = array_keys($name);
            }
        }
        foreach ($itemorder as $k) {
            $gb[0][1][$pos][0] = $name[$k];
            //item name
            $gb[0][1][$pos][1] = $cats[$cat][0];
            //item category name
            $gb[0][1][$pos][2] = $possible[$k];
            //points possible
            $gb[0][1][$pos][3] = $avail[$k];
            //0 past, 1 current, 2 future
            $gb[0][1][$pos][4] = $cntingb[$k];
            //0 no count and hide, 1 count, 2 EC, 3 no count
            $gb[0][1][$pos][5] = $assessmenttype[$k] == "Practice";
            //0 regular, 1 practice test
            if (isset($assessments[$k])) {
                $gb[0][1][$pos][6] = 0;
                //0 online, 1 offline
                $gb[0][1][$pos][7] = $cid;
                //courseid
                $gb[0][1][$pos][8] = $assessments[$k];
            } else {
                if (isset($grades[$k])) {
                    $gb[0][1][$pos][6] = 1;
                    //0 online, 1 offline
                    $gb[0][1][$pos][7] = $cid;
                    //courseid
                    $gb[0][1][$pos][8] = $grades[$k];
                } else {
                    if (isset($discuss[$k])) {
                        $gb[0][1][$pos][6] = 2;
                        //0 online, 1 offline, 2 discuss
                        $gb[0][1][$pos][7] = $cid;
                        //courseid
                        $gb[0][1][$pos][8] = $discuss[$k];
                    }
                }
            }
            $pos++;
        }
    }
    $totalspos = $pos;
    //create category headers
    $catorder = array_keys($cats);
    $overallpts = 0;
    $overallptsfuture = 0;
    $pos = 0;
    foreach ($catorder as $cat) {
        //foreach category
        //cats: name,scale,scaletype,chop,drop,weight
        $catitemcnt[$cat] = count($catposs[$cat]) + count($catpossec[$cat]);
        $catitemcntfuture[$cat] = count($catpossfuture[$cat]) + count($catpossfutureec[$cat]);
        if ($cats[$cat][4] != 0 && abs($cats[$cat][4]) < count($catposs[$cat])) {
            //if drop is set and have enough items
            asort($catposs, SORT_NUMERIC);
            $catposs[$cat] = array_slice($catposs[$cat], $cats[$cat][4]);
        }
        if ($cats[$cat][4] != 0 && abs($cats[$cat][4]) < count($catpossfuture[$cat])) {
            //same for all items
            asort($catpossfuture, SORT_NUMERIC);
            $catpossfuture[$cat] = array_slice($catpossfuture[$cat], $cats[$cat][4]);
        }
        $catposs[$cat] = array_sum($catposs[$cat]);
        $catpossfuture[$cat] = array_sum($catpossfuture[$cat]) + $catposs[$cat];
        $gb[0][2][$pos][0] = $cats[$cat][0];
        $gb[0][2][$pos][1] = $cats[$cat][0];
        if ($catposs[$cat] > 0) {
            $gb[0][2][$pos][2] = 0;
            //scores in past
        } else {
            if ($catposs[$cat] > 0) {
                $gb[0][2][$pos][2] = 1;
                //scores only in future
            } else {
                $gb[0][2][$pos][2] = 2;
                //no items
            }
        }
        if ($useweights == 0 && $cats[$cat][5] > -1) {
            //if scaling cat total to point value
            if ($catposs[$cat] > 0) {
                $gb[0][2][$pos][3] = $cats[$cat][5];
                //score for past
            } else {
                $gb[0][2][$pos][3] = 0;
                //fix to 0 if no scores in past yet
            }
            $gb[0][2][$pos][4] = $cats[$cat][5];
            //score for future
        } else {
            $gb[0][2][$pos][3] = $catposs[$cat];
            $gb[0][2][$pos][4] = $catpossfuture[$cat];
        }
        $overallpts += $gb[0][2][$pos][3];
        $overallptsfuture += $gb[0][2][$pos][4];
        $pos++;
    }
    //find total possible points
    if ($useweights == 0) {
        //use points grading method
        $gb[0][3][0] = $overallpts;
        $gb[0][3][1] = $overallptsfuture;
    }
    //Pull student data
    $ln = 1;
    $query = "SELECT imas_users.id,imas_users.SID,imas_users.FirstName,imas_users.LastName,imas_users.SID,imas_users.email,imas_students.section,imas_students.code ";
    $query .= "FROM imas_users,imas_students WHERE imas_users.id=imas_students.userid AND imas_students.courseid='{$cid}' ";
    //$query .= "FROM imas_users,imas_teachers WHERE imas_users.id=imas_teachers.userid AND imas_teachers.courseid='$cid' ";
    //if (!$isteacher && !isset($tutorid)) {$query .= "AND imas_users.id='$userid' ";}
    if ($limuser > 0) {
        $query .= "AND imas_users.id='{$limuser}' ";
    }
    if ($isdiag) {
        $query .= "ORDER BY imas_users.email,imas_users.LastName,imas_users.FirstName";
    } else {
        if ($hassection && $usersort == 0) {
            $query .= "ORDER BY imas_students.section,imas_users.LastName,imas_users.FirstName";
        } else {
            $query .= "ORDER BY imas_users.LastName,imas_users.FirstName";
        }
    }
    $result = mysql_query($query) or die("Query failed : {$query}: " . mysql_error());
    $alt = 0;
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        //foreach student
        unset($asid);
        unset($pts);
        unset($IP);
        unset($timeused);
        //Student ID info
        $gb[$ln][0][0] = "{$line['LastName']},&nbsp;{$line['FirstName']}";
        $gb[$ln][4][0] = $line['id'];
        $gb[$ln][4][1] = $cid;
        if ($isdiag) {
            $selparts = explode('~', $line['SID']);
            $gb[$ln][0][1] = $selparts[0];
            $gb[$ln][0][2] = $selparts[1];
            $selparts = explode('@', $line['email']);
            $gb[$ln][0][3] = $selparts[0];
            $gb[$ln][0][4] = $selparts[1];
        } else {
            $gb[$ln][0][1] = $line['SID'];
        }
        if ($hassection) {
            $gb[$ln][0][] = $line['section'];
        }
        if ($hascode) {
            $gb[$ln][0][] = $line['code'];
        }
        //Get assessment scores
        $query = "SELECT id,assessmentid,bestscores,starttime,endtime,feedback FROM imas_assessment_sessions WHERE userid='{$line['id']}'";
        $result2 = mysql_query($query) or die("Query failed : " . mysql_error());
        while ($l = mysql_fetch_array($result2, MYSQL_ASSOC)) {
            $asid[$l['assessmentid']] = $l['id'];
            $sp = explode(';', $l['bestscores']);
            $scores = explode(',', $sp[0]);
            $total = 0;
            for ($i = 0; $i < count($scores); $i++) {
                $total += getpts($scores[$i]);
                //if ($scores[$i]>0) {$total += $scores[$i];}
            }
            $timeused[$l['assessmentid']] = $l['endtime'] - $l['starttime'];
            $afeedback[$l['assessmentid']] = $l['feedback'];
            if (in_array(-1, $scores)) {
                $IP[$l['assessmentid']] = 1;
            }
            $pts[$l['assessmentid']] = $total;
        }
        //Get other grades
        unset($gradeid);
        unset($opts);
        $query = "SELECT imas_gbitems.id,imas_grades.id,imas_grades.score,imas_grades.feedback FROM imas_grades,imas_gbitems WHERE ";
        $query .= "imas_grades.gradetypeid=imas_gbitems.id AND imas_grades.gradetype='offline' AND imas_grades.userid='{$line['id']}'";
        $result2 = mysql_query($query) or die("Query failed : " . mysql_error());
        while ($r = mysql_fetch_row($result2)) {
            $gradeid[$r[0]] = $r[1];
            $opts[$r[0]] = $r[2];
            $gfeedback[$r[0]] = $r[3];
        }
        //Get discussion grades
        unset($discusspts);
        $query = "SELECT forumid,SUM(points) FROM imas_forum_posts WHERE userid='{$line['id']}' GROUP BY forumid ";
        $result2 = mysql_query($query) or die("Query failed : {$query} " . mysql_error());
        while ($r = mysql_fetch_row($result2)) {
            $discusspts[$r[0]] = $r[1];
        }
        //Create student GB row
        unset($cattot);
        unset($cattotfuture);
        $pos = 0;
        foreach ($itemorder as $i) {
            if ($assessmenttype[$i] == 'Offline') {
                //is other grade
                if (isset($gradeid[$grades[$i]])) {
                    $gb[$ln][1][$pos][0] = 1 * $opts[$grades[$i]];
                    //score
                    if ($isteacher && $gfeedback[$grades[$i]] != '') {
                        //feedback
                        $gb[$ln][1][$pos][4] = 1;
                        //yes it has it (for teachers)
                    } else {
                        if ($limuser > 0) {
                            $gb[$ln][1][$pos][4] = $gfeedback[$grades[$i]];
                            //the feedback (for students)
                        } else {
                            $gb[$ln][1][$pos][4] = 0;
                            //no feedback
                        }
                    }
                    if ($cntingb[$i] == 1 || $cntingb[$i] == 2) {
                        if ($gb[0][1][$pos][3] < 2) {
                            $cattot[$category[$i]][] = $opts[$grades[$i]];
                        }
                        $cattotfuture[$category[$i]][] = $opts[$grades[$i]];
                    }
                } else {
                    $gb[$ln][1][$pos][0] = '-';
                    $gb[$ln][1][$pos][4] = 0;
                    //no feedback
                }
                $gb[$ln][1][$pos][1] = $gb[0][1][$pos][1];
                //copy category
                $gb[$ln][1][$pos][2] = 1;
                //1 offline
                $gb[$ln][1][$pos][3] = $gb[0][1][$pos][3];
                //copy past/future
                $gb[$ln][1][$pos][5] = $grades[$i];
                //gbitems.id
                $gb[$ln][1][$pos][6] = $line['id'];
                //userid
            } else {
                if ($assessmenttype[$i] == 'Discussion') {
                    if (isset($discusspts[$discuss[$i]])) {
                        $gb[$ln][1][$pos][0] = $discusspts[$discuss[$i]];
                        $atots[$pos][] = $discusspts[$discuss[$i]];
                        $cattot[$category[$i]][] = $discusspts[$discuss[$i]];
                    } else {
                        $gb[$ln][1][$pos][0];
                    }
                    $gb[$ln][1][$pos][1] = $gb[0][1][$pos][1];
                    //copy category
                    $gb[$ln][1][$pos][2] = 2;
                    //2 discuss
                    $gb[$ln][1][$pos][3] = $gb[0][1][$pos][3];
                    //copy past/future
                    $gb[$ln][1][$pos][4] = $discuss[$i];
                    //forumid
                    $gb[$ln][1][$pos][5] = $line['id'];
                    //userid
                } else {
                    if (isset($asid[$assessments[$i]])) {
                        $thised = $enddate[$i];
                        if (!$isteacher) {
                            $query = "SELECT enddate FROM imas_exceptions WHERE userid='{$line['id']}' AND assessmentid='{$assessments[$i]}'";
                            $r2 = mysql_query($query) or die("Query failed : " . mysql_error());
                            if (mysql_num_rows($r2) > 0) {
                                $exped = mysql_result($r2, 0, 0);
                                if ($exped > $enddate[$i]) {
                                    $thised = $exped;
                                }
                            }
                        }
                        if ($isdisp && ($isteacher || $assessmenttype[$i] == "Practice" || $sa[$i] == "I" || $sa[$i] != "N" && $now > $thised)) {
                            $gb[$ln][1][$pos][5] = 1;
                            //show link
                        } else {
                            $gb[$ln][1][$pos][5] = 0;
                            //don't show link
                        }
                        if ($assessmenttype[$i] == "NoScores" && $sa[$i] != "I" && $now < $thised && !$isteacher) {
                            $gb[$ln][1][$pos][0] = 'N/A';
                            //score is not available
                            $gb[$ln][1][$pos][6] = 0;
                            //no other info
                        } else {
                            if ($pts[$assessments[$i]] < $minscores[$i]) {
                                if ($isteacher) {
                                    $gb[$ln][1][$pos][0] = $pts[$assessments[$i]];
                                    //the score
                                    $gb[$ln][1][$pos][6] = 1;
                                    //no credit
                                } else {
                                    $gb[$ln][1][$pos][0] = 'NC';
                                    //score is No credit
                                    $gb[$ln][1][$pos][6] = 1;
                                    //no credit
                                }
                            } else {
                                if ($IP[$assessments[$i]] == 1 && $thised > $now) {
                                    $gb[$ln][1][$pos][0] = $pts[$assessments[$i]];
                                    //the score
                                    $gb[$ln][1][$pos][6] = 2;
                                    //in progress
                                } else {
                                    if ($timelimits[$i] > 0 && $timeused[$assessments[$i]] > $timelimits[$i]) {
                                        $gb[$ln][1][$pos][0] = $pts[$assessments[$i]];
                                        //the score
                                        $gb[$ln][1][$pos][6] = 3;
                                        //over time
                                    } else {
                                        if ($assessmenttype[$i] == "Practice") {
                                            $gb[$ln][1][$pos][0] = $pts[$assessments[$i]];
                                            //the score
                                            $gb[$ln][1][$pos][6] = 4;
                                            //practice test
                                        } else {
                                            //regular score available to students
                                            $gb[$ln][1][$pos][0] = $pts[$assessments[$i]];
                                            //the score
                                            $gb[$ln][1][$pos][6] = 0;
                                            //no other info
                                            if ($cntingb[$i] == 1 || $cntingb[$i] == 2) {
                                                if ($gb[0][1][$pos][3] < 2) {
                                                    //curent or past
                                                    $cattot[$category[$i]][] = $pts[$assessments[$i]];
                                                }
                                                $cattotfuture[$category[$i]][] = $pts[$assessments[$i]];
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if ($isteacher && $afeedback[$assessments[$i]] != '') {
                            $gb[$ln][1][$pos][4] = 1;
                            //has comment
                        } else {
                            if ($limuser > 0) {
                                $gb[$ln][1][$pos][4] = $afeedback[$assessments[$i]];
                            } else {
                                $gb[$ln][1][$pos][4] = 0;
                                //no comment
                            }
                        }
                        $gb[$ln][1][$pos][7] = $assessments[$i];
                        //assessment id
                        $gb[$ln][1][$pos][8] = $asid[$assessments[$i]];
                        //assessment session id
                    } else {
                        $gb[$ln][1][$pos][0] = '-';
                        $gb[$ln][1][$pos][7] = $assessments[$i];
                        //assessment id
                        $gb[$ln][1][$pos][8] = 'new';
                        //assessment session id - don't have one
                    }
                }
            }
            $gb[$ln][1][$pos][1] = $gb[0][1][$pos][1];
            //copy category
            $gb[$ln][1][$pos][2] = 0;
            //0 online assessment
            $gb[$ln][1][$pos][3] = $gb[0][1][$pos][3];
            //copy past/future
            $gb[$ln][1][$pos][9] = $line['id'];
            //userid
            $pos++;
        }
        $tot = 0;
        $totfuture = 0;
        //create category totals
        $pos = 0;
        //reset position for category totals
        foreach ($catorder as $cat) {
            //foreach category
            $gb[$ln][2][$pos][1] = $gb[0][2][$pos][0];
            //copy category name
            if (isset($cattot[$cat])) {
                $gb[$ln][2][$pos][3] = 0;
                //there are past/current items
            }
            if (isset($cattotfuture[$cat])) {
                $gb[$ln][2][$pos][3] = 1;
                //only future items
            } else {
                $gb[$ln][2][$pos][3] = 0;
                //no items at all
                $gb[$ln][2][$pos][0] = 0;
                $gb[$ln][2][$pos][2] = 0;
                $pos++;
                continue;
            }
            if (isset($cattot[$cat])) {
                //cats: name,scale,scaletype,chop,drop,weight
                if ($cats[$cat][4] != 0 && abs($cats[$cat][4]) < count($cattot[$cat])) {
                    //if drop is set and have enough items
                    asort($cattot[$cat], SORT_NUMERIC);
                    while (count($cattot[$cat]) < $catitemcnt[$cat]) {
                        array_unshift($cattot[$cat], 0);
                    }
                    $cattot[$cat] = array_slice($cattot[$cat], $cats[$cat][4]);
                }
                $cattot[$cat] = array_sum($cattot[$cat]);
                //**adjust for drop, scale, etc
                if ($cats[$cat][1] != 0) {
                    //scale is set
                    if ($cats[$cat][2] == 0) {
                        //pts scale
                        $cattot[$cat] = round($catposs[$cat] * ($cattot[$cat] / $cats[$cat][1]), 1);
                    } else {
                        if ($cats[$cat][2] == 1) {
                            //percent scale
                            $cattot[$cat] = round($cattot[$cat] * (100 / $cats[$cat][1]), 1);
                        }
                    }
                }
                if ($useweights == 0 && $cats[$cat][5] > -1) {
                    //use fixed pt value for cat
                    $cattot[$cat] = round($cats[$cat][5] * ($cattot[$cat] / $catposs[$cat]), 1);
                }
                if ($cats[$cat][3] == 1) {
                    if ($useweights == 0 && $cats[$cat][5] > -1) {
                        //set cat pts
                        $cattot[$cat] = min($cats[$cat][5], $cattot[$cat]);
                    } else {
                        $cattot[$cat] = min($catposs[$cat], $cattot[$cat]);
                    }
                }
                $gb[$ln][2][$pos][0] = $cattot[$cat];
                if ($useweights == 1) {
                    if ($catposs[$cat] > 0) {
                        $tot += $cattot[$cat] * $cats[$cat][5] / (100 * $catposs[$cat]);
                        //weight total
                    }
                }
            } else {
                //no items in category yet?
                $gb[$ln][2][$pos][0] = 0;
            }
            if (isset($cattotfuture[$cat])) {
                //cats: name,scale,scaletype,chop,drop,weight
                if ($cats[$cat][4] != 0 && abs($cats[$cat][4]) < count($cattotfuture[$cat])) {
                    //if drop is set and have enough items
                    asort($cattotfuture[$cat], SORT_NUMERIC);
                    while (count($cattotfuture[$cat]) < $catitemcntfuture[$cat]) {
                        array_unshift($cattotfuture[$cat], 0);
                    }
                    $cattotfuture[$cat] = array_slice($cattotfuture[$cat], $cats[$cat][4]);
                }
                $cattotfuture[$cat] = array_sum($cattotfuture[$cat]);
                //**adjust for drop, scale, etc
                if ($cats[$cat][1] != 0) {
                    //scale is set
                    if ($cats[$cat][2] == 0) {
                        //pts scale
                        $cattotfuture[$cat] = round($catpossfuture[$cat] * ($cattotfuture[$cat] / $cats[$cat][1]), 1);
                    } else {
                        if ($cats[$cat][2] == 1) {
                            //percent scale
                            $cattotfuture[$cat] = round($cattotfuture[$cat] * (100 / $cats[$cat][1]), 1);
                        }
                    }
                }
                if ($useweights == 0 && $cats[$cat][5] > -1) {
                    //use fixed pt value for cat
                    $cattotfuture[$cat] = round($cats[$cat][5] * ($cattotfuture[$cat] / $catpossfuture[$cat]), 1);
                }
                if ($cats[$cat][3] == 1) {
                    if ($useweights == 0 && $cats[$cat][5] > -1) {
                        //set cat pts
                        $cattotfuture[$cat] = min($cats[$cat][5], $cattotfuture[$cat]);
                    } else {
                        $cattotfuture[$cat] = min($catpossfuture[$cat], $cattotfuture[$cat]);
                    }
                }
                $gb[$ln][2][$pos][2] = $cattotfuture[$cat];
                if ($useweights == 1) {
                    if ($catpossfuture[$cat] > 0) {
                        $totfuture += $cattotfuture[$cat] * $cats[$cat][5] / (100 * $catpossfuture[$cat]);
                        //weight total
                    }
                }
            } else {
                //no items in category yet?
                $gb[$ln][2][$pos][2] = 0;
            }
            $pos++;
        }
        if ($useweights == 0) {
            //use points grading method
            if (!isset($cattot)) {
                $tot = 0;
            } else {
                $tot = array_sum($cattot);
            }
            $gb[$ln][3][0] = $tot;
            if ($overallpts > 0) {
                $gb[$ln][3][1] = round(100 * $tot / $overallpts, 1) . '%';
            } else {
                $gb[$ln][3][1] = '0%';
            }
            if (!isset($cattotfuture)) {
                $totfuture = 0;
            } else {
                $totfuture = array_sum($cattotfuture);
            }
            $gb[$ln][3][2] = $totfuture;
            if ($overallptsfuture > 0) {
                $gb[$ln][3][3] = round(100 * $totfuture / $overallptsfuture, 1) . '%';
            } else {
                $gb[$ln][3][3] = '0%';
            }
        } else {
            if ($useweights == 1) {
                //use weights (%) grading method
                //already calculated $tot
                if ($overallpts > 0) {
                    $tot = 100 * ($tot / $overallpts);
                } else {
                    $tot = 0;
                }
                $gb[$ln][3][0] = round(100 * $tot, 1);
                $gb[$ln][3][1] = null;
                if ($overallptsfuture > 0) {
                    $totfuture = 100 * ($totfuture / $overallptsfuture);
                } else {
                    $totfuture = 0;
                }
                $gb[$ln][3][2] = round(100 * $totfuture, 1);
                $gb[$ln][3][3] = null;
            }
        }
        $ln++;
    }
    return $gb;
}
Пример #8
0
function showcalendar($refpage)
{
    global $imasroot, $cid, $userid, $teacherid, $previewshift, $latepasses, $urlmode, $latepasshrs, $myrights, $tzoffset, $tzname, $havecalcedviewedassess, $viewedassess;
    $now = time();
    if ($previewshift != -1) {
        $now = $now + $previewshift;
    }
    if (!isset($_COOKIE['calstart' . $cid]) || $_COOKIE['calstart' . $cid] == 0) {
        $today = $now;
    } else {
        $today = $_COOKIE['calstart' . $cid];
    }
    if (isset($_GET['calpageshift'])) {
        $pageshift = $_GET['calpageshift'];
    } else {
        $pageshift = 0;
    }
    if (!isset($_COOKIE['callength' . $cid])) {
        $callength = 4;
    } else {
        $callength = $_COOKIE['callength' . $cid];
    }
    $today = $today + $pageshift * 7 * $callength * 24 * 60 * 60;
    $dayofweek = tzdate('w', $today);
    $curmonum = tzdate('n', $today);
    $dayofmo = tzdate('j', $today);
    $curyr = tzdate('Y', $today);
    if ($tzname == '') {
        $serveroffset = date('Z') + $tzoffset * 60;
    } else {
        $serveroffset = 0;
        //don't need this if user's timezone has been set
    }
    $midtoday = mktime(12, 0, 0, $curmonum, $dayofmo, $curyr) + $serveroffset;
    $hdrs = array();
    $ids = array();
    $lastmo = '';
    for ($i = 0; $i < 7 * $callength; $i++) {
        $row = floor($i / 7);
        $col = $i % 7;
        list($thismo, $thisday, $thismonum, $datestr) = explode('|', tzdate('M|j|n|l F j, Y', $midtoday - ($dayofweek - $i) * 24 * 60 * 60));
        if ($thismo == $lastmo) {
            $hdrs[$row][$col] = $thisday;
        } else {
            $hdrs[$row][$col] = "{$thismo} {$thisday}";
            $lastmo = $thismo;
        }
        $ids[$row][$col] = "{$thismonum}-{$thisday}";
        $dates[$ids[$row][$col]] = $datestr;
    }
    ?>


<?php 
    //echo '<div class="floatleft">Jump to <a href="'.$refpage.'.php?calpageshift=0&cid='.$cid.'">Now</a></div>';
    $address = $urlmode . $_SERVER['HTTP_HOST'] . rtrim(dirname($_SERVER['PHP_SELF']), '/\\') . "/{$refpage}.php?cid={$cid}";
    echo '<script type="text/javascript">var calcallback = "' . $address . '";</script>';
    echo '<div class="floatright">Show <select id="callength" onchange="changecallength(this)">';
    for ($i = 2; $i < 26; $i++) {
        echo '<option value="' . $i . '" ';
        if ($i == $callength) {
            echo 'selected="selected"';
        }
        echo '>' . $i . '</option>';
    }
    echo '</select> weeks </div>';
    echo '<div class=center><a href="' . $refpage . '.php?calpageshift=' . ($pageshift - 1) . '&cid=' . $cid . '">&lt; &lt;</a> ';
    //echo $longcurmo.' ';
    if ($pageshift == 0 && (!isset($_COOKIE['calstart' . $cid]) || $_COOKIE['calstart' . $cid] == 0)) {
        echo "Now ";
    } else {
        echo '<a href="' . $refpage . '.php?calpageshift=0&calstart=0&cid=' . $cid . '">Now</a> ';
    }
    echo '<a href="' . $refpage . '.php?calpageshift=' . ($pageshift + 1) . '&cid=' . $cid . '">&gt; &gt;</a> ';
    echo '</div> ';
    echo "<table class=\"cal\" >";
    //onmouseout=\"makenorm()\"
    $exlowertime = mktime(0, 0, 0, $curmonum, $dayofmo - $dayofweek, $curyr) + $serveroffset;
    $lowertime = max($now, $exlowertime);
    $uppertime = mktime(0, 0, 0, $curmonum, $dayofmo - $dayofweek + 7 * $callength, $curyr) + $serveroffset;
    $exceptions = array();
    if (!isset($teacherid)) {
        $query = "SELECT assessmentid,startdate,enddate,islatepass,waivereqscore FROM imas_exceptions WHERE userid='{$userid}'";
        $result = mysql_query($query) or die("Query failed : {$query}" . mysql_error());
        while ($row = mysql_fetch_row($result)) {
            $exceptions[$row[0]] = array($row[1], $row[2], $row[3], $row[4]);
        }
    }
    $byid = array();
    $k = 0;
    $query = "SELECT id,name,startdate,enddate,reviewdate,gbcategory,reqscore,reqscoreaid,timelimit,allowlate,caltag,calrtag FROM imas_assessments WHERE avail=1 AND courseid='{$cid}' AND enddate<2000000000 ORDER BY name";
    $result = mysql_query($query) or die("Query failed : {$query}" . mysql_error());
    while ($row = mysql_fetch_row($result)) {
        $canundolatepass = false;
        $latepasscnt = 0;
        if (isset($exceptions[$row[0]])) {
            if ($exceptions[$row[0]][2] > 0 && ($now < $row[3] || $exceptions[$row[0]][1] > $now + $latepasshrs * 60 * 60)) {
                $canundolatepass = true;
            }
            $latepasscnt = round(($exceptions[$row[0]][1] - $row[3]) / ($latepasshrs * 3600));
            $row[2] = $exceptions[$row[0]][0];
            $row[3] = $exceptions[$row[0]][1];
        }
        //2: start, 3: end, 4: review
        //if enddate past end of calendar
        if ($row[3] > $uppertime && ($row[4] == 0 || $row[4] > $uppertime || $row[4] < $row[3])) {
            continue;
        }
        //if enddate is past, and reviewdate is past end of calendar
        if ($row[3] < $now && $row[4] > $uppertime) {
            //continue;
        }
        //echo "{$row[1]}, {$row[3]}, $uppertime, {$row[4]}<br/>";
        //if startdate is past now
        if ($row[2] > $now && !isset($teacherid)) {
            continue;
        }
        //if past reviewdate
        if ($row[4] > 0 && $now > $row[4] && !isset($teacherid)) {
            //if has reviewdate and we're past it   //|| ($now>$row[3] && $row[4]==0)
            //continue;
        }
        if (!isset($teacherid) && $row[6] > 0 && $row[7] > 0 && (!isset($exceptions[$row[0]]) || $exceptions[$row[0]][3] == 0)) {
            $query = "SELECT bestscores FROM imas_assessment_sessions WHERE assessmentid='{$row[7]}' AND userid='{$userid}'";
            $r2 = mysql_query($query) or die("Query failed : " . mysql_error());
            if (mysql_num_rows($r2) == 0) {
                continue;
            } else {
                $scores = explode(';', mysql_result($r2, 0, 0));
                if (getpts($scores[0]) < $row[6]) {
                    continue;
                }
            }
        }
        if ($row[4] < $uppertime && $row[4] > 0 && $now > $row[3]) {
            //has review, and we're past enddate
            list($moday, $time) = explode('~', tzdate('n-j~g:i a', $row[4]));
            $row[1] = htmlentities($row[1], ENT_COMPAT | ENT_HTML401, "UTF-8", false);
            $tag = htmlentities($row[11], ENT_COMPAT | ENT_HTML401, "UTF-8", false);
            if ($now < $row[4]) {
                $colors = '#99f';
            } else {
                $colors = '#ccc';
            }
            $json = "{type:\"AR\", time:\"{$time}\", tag:\"{$tag}\", ";
            if ($now < $row[4] || isset($teacherid)) {
                $json .= "id:\"{$row['0']}\",";
            }
            $json .= "color:\"" . $colors . "\",name:\"{$row['1']}\"" . (isset($teacherid) ? ", editlink:true" : "") . "}";
            if ($row[3] < $uppertime && $row[3] > $exlowertime) {
                //if going to do a second tag, need to increment.
                $byid['AR' . $row[0]] = array($moday, $tag, $colors, $json);
            }
        }
        if ($row[3] < $uppertime && $row[3] > $exlowertime) {
            // taking out "hide if past due" && ($now<$row[3] || isset($teacherid))) {
            /*if (isset($gbcats[$row[5]])) {
            			$tag = $gbcats[$row[5]];
            		} else {
            			$tag = '?';
            		}*/
            $tag = htmlentities($row[10], ENT_COMPAT | ENT_HTML401, "UTF-8", false);
            if (!$havecalcedviewedassess && $now > $row[3] && $row[9] > 10) {
                $havecalcedviewedassess = true;
                $viewedassess = array();
                $query = "SELECT typeid FROM imas_content_track WHERE courseid='{$cid}' AND userid='{$userid}' AND type='gbviewasid'";
                $r2 = mysql_query($query) or die("Query failed : " . mysql_error());
                while ($r = mysql_fetch_row($r2)) {
                    $viewedassess[] = $r[0];
                }
            }
            if (($row[9] % 10 == 1 || $row[9] % 10 - 1 > $latepasscnt) && $latepasses > 0 && ($now < $row[3] || $row[9] > 10 && $now - $row[3] < $latepasshrs * 3600 && !in_array($row[0], $viewedassess))) {
                $lp = 1;
            } else {
                $lp = 0;
            }
            if ($canundolatepass) {
                $ulp = 1;
            } else {
                $ulp = 0;
            }
            list($moday, $time) = explode('~', tzdate('n-j~g:i a', $row[3]));
            $row[1] = htmlentities($row[1], ENT_COMPAT | ENT_HTML401, "UTF-8", false);
            $colors = makecolor2($row[2], $row[3], $now);
            $json = "{type:\"A\", time:\"{$time}\", ";
            if ($now < $row[3] || $row[4] > $now || isset($teacherid) || $lp == 1) {
                $json .= "id:\"{$row['0']}\",";
            }
            if ($now > $row[3] && $now > $row[4] && !isset($teacherid)) {
                $json .= 'inactive:true,';
            }
            $json .= "name:\"{$row['1']}\", color:\"" . $colors . "\", allowlate:\"{$lp}\", undolate:\"{$ulp}\", tag:\"{$tag}\"" . ($row[8] != 0 ? ", timelimit:true" : "") . (isset($teacherid) ? ", editlink:true" : "") . "}";
            //"<span class=icon style=\"background-color:#f66\">?</span> <a href=\"../assessment/showtest.php?id={$row[0]}&cid=$cid\">{$row[1]}</a> Due $time<br/>";
            $byid['A' . $row[0]] = array($moday, $tag, $colors, $json);
        }
    }
    // 4/4/2011, changing tthis to code block below.  Not sure why change on 10/23 was made :/
    //if (isset($teacherid)) {
    //$query = "SELECT id,title,enddate,text,startdate,oncal,caltag,avail FROM imas_inlinetext WHERE ((oncal=2 AND enddate>$exlowertime AND enddate<$uppertime) OR (oncal=1 AND startdate<$uppertime AND startdate>$exlowertime)) AND (avail=1 OR (avail=2 AND startdate>0)) AND courseid='$cid'";
    //} else {
    //	$query = "SELECT id,title,enddate,text,startdate,oncal,caltag FROM imas_inlinetext WHERE ((oncal=2 AND enddate>$lowertime AND enddate<$uppertime AND startdate<$now) OR (oncal=1 AND startdate<$now AND startdate>$exlowertime)) AND avail=1 AND courseid='$cid'";  //chg 10/23/09: replace $now with $uppertime
    //}
    if (isset($teacherid)) {
        $query = "SELECT id,title,enddate,text,startdate,oncal,caltag,avail FROM imas_inlinetext WHERE ((oncal=2 AND enddate>{$exlowertime} AND enddate<{$uppertime}) OR (oncal=1 AND startdate<{$uppertime} AND startdate>{$exlowertime})) AND (avail=1 OR (avail=2 AND startdate>0)) AND courseid='{$cid}'";
    } else {
        $query = "SELECT id,title,enddate,text,startdate,oncal,caltag,avail FROM imas_inlinetext WHERE ";
        $query .= "((avail=1 AND ((oncal=2 AND enddate>{$exlowertime} AND enddate<{$uppertime} AND startdate<{$now}) OR (oncal=1 AND startdate<{$now} AND startdate>{$exlowertime}))) OR ";
        $query .= "(avail=2 AND oncal=1 AND startdate<{$uppertime} AND startdate>{$exlowertime})) AND courseid='{$cid}'";
    }
    $result = mysql_query($query) or die("Query failed : {$query}" . mysql_error());
    while ($row = mysql_fetch_row($result)) {
        if ($row[1] == '##hidden##') {
            $row[1] = strip_tags($row[3]);
        }
        if ($row[5] == 1) {
            list($moday, $time) = explode('~', tzdate('n-j~g:i a', $row[4]));
        } else {
            list($moday, $time) = explode('~', tzdate('n-j~g:i a', $row[2]));
        }
        $row[1] = htmlentities($row[1], ENT_COMPAT | ENT_HTML401, "UTF-8", false);
        $colors = makecolor2($row[4], $row[2], $now);
        if ($row[7] == 2) {
            $colors = "#0f0";
        }
        $tag = htmlentities($row[6], ENT_COMPAT | ENT_HTML401, "UTF-8", false);
        $json = "{type:\"I\", folder:\"@@@\", time:\"{$time}\", id:\"{$row['0']}\", name:\"{$row['1']}\", color:\"" . $colors . "\", tag:\"{$tag}\"" . (isset($teacherid) ? ", editlink:true" : "") . "}";
        //"<span class=icon style=\"background-color:#f66\">?</span> <a href=\"../assessment/showtest.php?id={$row[0]}&cid=$cid\">{$row[1]}</a> Due $time<br/>";
        $byid['I' . $row[0]] = array($moday, $tag, $colors, $json);
    }
    //$query = "SELECT id,title,enddate,text,startdate,oncal,caltag FROM imas_linkedtext WHERE ((oncal=2 AND enddate>$lowertime AND enddate<$uppertime AND startdate<$now) OR (oncal=1 AND startdate<$now AND startdate>$exlowertime)) AND avail=1 AND courseid='$cid'";
    if (isset($teacherid)) {
        $query = "SELECT id,title,enddate,text,startdate,oncal,caltag,avail FROM imas_linkedtext WHERE ((oncal=2 AND enddate>{$exlowertime} AND enddate<{$uppertime}) OR (oncal=1 AND startdate<{$uppertime} AND startdate>{$exlowertime})) AND (avail=1 OR (avail=2 AND startdate>0)) AND courseid='{$cid}' ORDER BY title";
    } else {
        $query = "SELECT id,title,enddate,text,startdate,oncal,caltag,avail FROM imas_linkedtext WHERE ";
        $query .= "((avail=1 AND ((oncal=2 AND enddate>{$exlowertime} AND enddate<{$uppertime} AND startdate<{$now}) OR (oncal=1 AND startdate<{$now} AND startdate>{$exlowertime}))) OR ";
        $query .= "(avail=2 AND oncal=1 AND startdate<{$uppertime} AND startdate>{$exlowertime})) AND courseid='{$cid}' ORDER BY title";
    }
    $result = mysql_query($query) or die("Query failed : {$query}" . mysql_error());
    while ($row = mysql_fetch_row($result)) {
        if ($row[5] == 1) {
            list($moday, $time) = explode('~', tzdate('n-j~g:i a', $row[4]));
        } else {
            list($moday, $time) = explode('~', tzdate('n-j~g:i a', $row[2]));
        }
        $row[1] = htmlentities($row[1], ENT_COMPAT | ENT_HTML401, "UTF-8", false);
        if (substr($row[3], 0, 4) == "http" && strpos($row[3], " ") === false) {
            //is a web link
            $alink = trim($row[3]);
        } else {
            if (substr($row[3], 0, 5) == "file:") {
                $filename = substr(strip_tags($row[3]), 5);
                $alink = getcoursefileurl($filename);
                //$alink = $imasroot . "/course/files/".$filename;
            } else {
                $alink = '';
            }
        }
        $colors = makecolor2($row[4], $row[2], $now);
        if ($row[7] == 2) {
            $colors = "#0f0";
        }
        $json = "{type:\"L\", time:\"{$time}\", ";
        if (isset($teacherid) || $now < $row[2] && $now > $row[4] || $row[7] == 2) {
            $json .= "id:\"{$row['0']}\", ";
        }
        $tag = htmlentities($row[6], ENT_COMPAT | ENT_HTML401, "UTF-8", false);
        $alink = htmlentities($alink, ENT_COMPAT | ENT_HTML401, "UTF-8", false);
        $json .= "name:\"{$row['1']}\", link:\"{$alink}\", color:\"" . $colors . "\", tag:\"{$tag}\"" . (isset($teacherid) ? ", editlink:true" : "") . "}";
        //"<span class=icon style=\"background-color:#f66\">?</span> <a href=\"../assessment/showtest.php?id={$row[0]}&cid=$cid\">{$row[1]}</a> Due $time<br/>";
        $byid['L' . $row[0]] = array($moday, $tag, $colors, $json);
    }
    $query = "SELECT id,name,postby,replyby,startdate,caltag FROM imas_forums WHERE enddate>{$exlowertime} AND ((postby>{$exlowertime} AND postby<{$uppertime}) OR (replyby>{$exlowertime} AND replyby<{$uppertime})) AND avail>0 AND courseid='{$cid}' ORDER BY name";
    $result = mysql_query($query) or die("Query failed : {$query}" . mysql_error());
    while ($row = mysql_fetch_row($result)) {
        if ($row[4] > $now && !isset($teacherid)) {
            continue;
        }
        list($posttag, $replytag) = explode('--', $row[5]);
        $posttag = htmlentities($posttag, ENT_COMPAT | ENT_HTML401, "UTF-8", false);
        $replytag = htmlentities($replytag, ENT_COMPAT | ENT_HTML401, "UTF-8", false);
        if ($row[2] != 2000000000) {
            //($row[2]>$now || isset($teacherid))
            list($moday, $time) = explode('~', tzdate('n-j~g:i a', $row[2]));
            $row[1] = htmlentities($row[1], ENT_COMPAT | ENT_HTML401, "UTF-8", false);
            $colors = makecolor2($row[4], $row[2], $now);
            $json = "{type:\"FP\", time:\"{$time}\", ";
            if ($row[2] > $now || isset($teacherid)) {
                $json .= "id:\"{$row['0']}\",";
            }
            $json .= "name:\"{$row['1']}\", color:\"" . $colors . "\", tag:\"{$posttag}\"" . (isset($teacherid) ? ", editlink:true" : "") . "}";
            $byid['FP' . $row[0]] = array($moday, $posttag, $colors, $json);
        }
        if ($row[3] != 2000000000) {
            //($row[3]>$now || isset($teacherid))
            list($moday, $time) = explode('~', tzdate('n-j~g:i a', $row[3]));
            $colors = makecolor2($row[4], $row[3], $now);
            $json = "{type:\"FR\", time:\"{$time}\",";
            if ($row[3] > $now || isset($teacherid)) {
                $json .= "id:\"{$row['0']}\",";
            }
            $json .= "name:\"{$row['1']}\", color:\"" . $colors . "\", tag:\"{$replytag}\"" . (isset($teacherid) ? ", editlink:true" : "") . "}";
            $byid['FR' . $row[0]] = array($moday, $replytag, $colors, $json);
        }
    }
    $query = "SELECT itemorder FROM imas_courses WHERE id='{$cid}'";
    $result = mysql_query($query) or die("Query failed : {$query}" . mysql_error());
    $itemorder = unserialize(mysql_result($result, 0, 0));
    $itemsimporder = array();
    $itemfolder = array();
    flattenitems($itemorder, $itemsimporder, $itemfolder, '0');
    $itemsassoc = array();
    $query = "SELECT id,itemtype,typeid FROM imas_items WHERE courseid='{$cid}'";
    $result = mysql_query($query) or die("Query failed : {$query}" . mysql_error());
    while ($row = mysql_fetch_row($result)) {
        $itemsassoc[$row[0]] = array($row[1], $row[2]);
    }
    $assess = array();
    $colors = array();
    $tags = array();
    $k = 0;
    foreach ($itemsimporder as $item) {
        if ($itemsassoc[$item][0] == 'Assessment') {
            if (isset($byid['A' . $itemsassoc[$item][1]])) {
                $moday = $byid['A' . $itemsassoc[$item][1]][0];
                $tags[$k] = $byid['A' . $itemsassoc[$item][1]][1];
                $colors[$k] = $byid['A' . $itemsassoc[$item][1]][2];
                $assess[$moday][$k] = $byid['A' . $itemsassoc[$item][1]][3];
                $k++;
            }
            if (isset($byid['AR' . $itemsassoc[$item][1]])) {
                $moday = $byid['AR' . $itemsassoc[$item][1]][0];
                $tags[$k] = $byid['AR' . $itemsassoc[$item][1]][1];
                $colors[$k] = $byid['AR' . $itemsassoc[$item][1]][2];
                $assess[$moday][$k] = $byid['AR' . $itemsassoc[$item][1]][3];
                $k++;
            }
        } else {
            if ($itemsassoc[$item][0] == 'Forum') {
                if (isset($byid['FP' . $itemsassoc[$item][1]])) {
                    $moday = $byid['FP' . $itemsassoc[$item][1]][0];
                    $tags[$k] = $byid['FP' . $itemsassoc[$item][1]][1];
                    $colors[$k] = $byid['FP' . $itemsassoc[$item][1]][2];
                    $assess[$moday][$k] = $byid['FP' . $itemsassoc[$item][1]][3];
                    $k++;
                }
                if (isset($byid['FR' . $itemsassoc[$item][1]])) {
                    $moday = $byid['FR' . $itemsassoc[$item][1]][0];
                    $tags[$k] = $byid['FR' . $itemsassoc[$item][1]][1];
                    $colors[$k] = $byid['FR' . $itemsassoc[$item][1]][2];
                    $assess[$moday][$k] = $byid['FR' . $itemsassoc[$item][1]][3];
                    $k++;
                }
            } else {
                if ($itemsassoc[$item][0] == 'InlineText') {
                    if (isset($byid['I' . $itemsassoc[$item][1]])) {
                        $moday = $byid['I' . $itemsassoc[$item][1]][0];
                        $tags[$k] = $byid['I' . $itemsassoc[$item][1]][1];
                        $colors[$k] = $byid['I' . $itemsassoc[$item][1]][2];
                        if (isset($itemfolder[$item])) {
                            $assess[$moday][$k] = str_replace('@@@', $itemfolder[$item], $byid['I' . $itemsassoc[$item][1]][3]);
                        } else {
                            $assess[$moday][$k] = str_replace('"@@@"', 'null', $byid['I' . $itemsassoc[$item][1]][3]);
                        }
                        $k++;
                    }
                } else {
                    if ($itemsassoc[$item][0] == 'LinkedText') {
                        if (isset($byid['L' . $itemsassoc[$item][1]])) {
                            $moday = $byid['L' . $itemsassoc[$item][1]][0];
                            $tags[$k] = $byid['L' . $itemsassoc[$item][1]][1];
                            $colors[$k] = $byid['L' . $itemsassoc[$item][1]][2];
                            $assess[$moday][$k] = $byid['L' . $itemsassoc[$item][1]][3];
                            $k++;
                        }
                    }
                }
            }
        }
    }
    $query = "SELECT title,tag,date FROM imas_calitems WHERE date>{$exlowertime} AND date<{$uppertime} and courseid='{$cid}' ORDER BY title";
    $result = mysql_query($query) or die("Query failed : {$query}" . mysql_error());
    while ($row = mysql_fetch_row($result)) {
        list($moday, $time) = explode('~', tzdate('n-j~g:i a', $row[2]));
        $row[0] = htmlentities($row[0], ENT_COMPAT | ENT_HTML401, "UTF-8", false);
        $row[1] = htmlentities($row[1], ENT_COMPAT | ENT_HTML401, "UTF-8", false);
        $assess[$moday][$k] = "{type:\"C\", time:\"{$time}\", tag:\"{$row['1']}\", name:\"{$row['0']}\"}";
        $tags[$k] = $row[1];
        $k++;
    }
    $jsarr = '{';
    foreach ($dates as $moday => $val) {
        if ($jsarr != '{') {
            $jsarr .= ',';
        }
        if (isset($assess[$moday])) {
            $jsarr .= '"' . $moday . '":{date:"' . $dates[$moday] . '",data:[' . implode(',', $assess[$moday]) . ']}';
        } else {
            $jsarr .= '"' . $moday . '":{date:"' . $dates[$moday] . '"}';
        }
    }
    $jsarr .= '}';
    echo '<script type="text/javascript">';
    echo "cid = {$cid};";
    echo "caleventsarr = {$jsarr};";
    echo '</script>';
    echo "<thead><tr><th>Sunday</th> <th>Monday</th> <th>Tuesday</th> <th>Wednesday</th> <th>Thursday</th> <th>Friday</th> <th>Saturday</th></tr></thead>";
    echo "<tbody>";
    for ($i = 0; $i < count($hdrs); $i++) {
        echo "<tr>";
        for ($j = 0; $j < count($hdrs[$i]); $j++) {
            if ($i == 0 && $j == $dayofweek && $pageshift == 0) {
                //onmouseover="makebig(this)"
                echo '<td id="' . $ids[$i][$j] . '" onclick="showcalcontents(this)" class="today"><div class="td"><span class=day>' . $hdrs[$i][$j] . "</span><div class=center>";
            } else {
                $addr = $refpage . ".php?cid={$cid}&calstart=" . ($midtoday + $i * 7 * 24 * 60 * 60 + ($j - $dayofweek) * 24 * 60 * 60);
                echo '<td id="' . $ids[$i][$j] . '" onclick="showcalcontents(this)" ><div class="td"><span class=day><a href="' . $addr . '" class="caldl">' . $hdrs[$i][$j] . "</a></span><div class=center>";
            }
            if (isset($assess[$ids[$i][$j]])) {
                foreach ($assess[$ids[$i][$j]] as $k => $info) {
                    //echo $assess[$ids[$i][$j]][$k];
                    if (strpos($info, 'type:"AR"') !== false) {
                        echo "<span class=\"calitem\" style=\"background-color:" . $colors[$k] . ";\">{$tags[$k]}</span> ";
                    } else {
                        if (strpos($info, 'type:"A"') !== false) {
                            echo "<span class=\"calitem\" style=\"background-color:" . $colors[$k] . ";\">{$tags[$k]}</span> ";
                        } else {
                            if (strpos($info, 'type:"F') !== false) {
                                echo "<span class=\"calitem\" style=\"background-color:" . $colors[$k] . ";\">{$tags[$k]}</span> ";
                            } else {
                                if (strpos($info, 'type:"C') !== false) {
                                    echo "<span class=\"calitem\" style=\"background-color: #0ff;\">{$tags[$k]}</span> ";
                                } else {
                                    //textitems
                                    if (isset($tags[$k])) {
                                        echo "<span class=\"calitem\" style=\"background-color:" . $colors[$k] . ";\">{$tags[$k]}</span> ";
                                    } else {
                                        echo "<span class=\"calitem\" style=\"background-color:" . $colors[$k] . ";\">!</span> ";
                                    }
                                }
                            }
                        }
                    }
                }
            }
            echo "</div></div></td>";
        }
        echo "</tr>";
    }
    echo "</tbody></table>";
    echo "<div style=\"margin-top: 10px; padding:10px; border:1px solid #000;\">";
    echo '<span class=right><a href="#" onclick="showcalcontents(' . 1000 * ($midtoday - $dayofweek * 24 * 60 * 60) . '); return false;"/>Show all</a></span>';
    echo "<div id=\"caleventslist\"></div><div class=\"clear\"></div></div>";
    if ($pageshift == 0) {
        echo "<script>showcalcontents(document.getElementById('{$ids[0][$dayofweek]}'));</script>";
    }
}
Пример #9
0
function showscores($questions, $attempts, $testsettings)
{
    global $isdiag, $allowregen, $isreview, $noindivscores, $scores, $bestscores, $qi, $superdone, $timelimitkickout, $reviewatend;
    $total = 0;
    $lastattempttotal = 0;
    for ($i = 0; $i < count($bestscores); $i++) {
        if (getpts($bestscores[$i]) > 0) {
            $total += getpts($bestscores[$i]);
        }
        if (getpts($scores[$i]) > 0) {
            $lastattempttotal += getpts($scores[$i]);
        }
    }
    $totpossible = totalpointspossible($qi);
    $average = round(100 * (double) $total / (double) $totpossible, 1);
    $doendredirect = false;
    $outmsg = '';
    if ($testsettings['endmsg'] != '') {
        $endmsg = unserialize($testsettings['endmsg']);
        $redirecturl = '';
        if (isset($endmsg['msgs'])) {
            foreach ($endmsg['msgs'] as $sc => $msg) {
                //array must be reverse sorted
                if ($endmsg['type'] == 0 && $total >= $sc || $endmsg['type'] == 1 && $average >= $sc) {
                    $outmsg = $msg;
                    break;
                }
            }
            if ($outmsg == '') {
                $outmsg = $endmsg['def'];
            }
            if (!isset($endmsg['commonmsg'])) {
                $endmsg['commonmsg'] = '';
            }
            if (strpos($outmsg, 'redirectto:') !== false) {
                $redirecturl = trim(substr($outmsg, 11));
                echo "<input type=\"button\" value=\"", _('Continue'), "\" onclick=\"window.location.href='{$redirecturl}'\"/>";
                return false;
            }
        }
    }
    if ($isdiag) {
        global $userid;
        $query = "SELECT * from imas_users WHERE id='{$userid}'";
        $result = mysql_query($query) or die("Query failed : " . mysql_error());
        $userinfo = mysql_fetch_array($result, MYSQL_ASSOC);
        echo "<h3>{$userinfo['LastName']}, {$userinfo['FirstName']}: ";
        echo substr($userinfo['SID'], 0, strpos($userinfo['SID'], '~'));
        echo "</h3>\n";
    }
    echo "<h3>", _('Scores:'), "</h3>\n";
    if (!$noindivscores && !$reviewatend) {
        echo "<table class=scores>";
        for ($i = 0; $i < count($scores); $i++) {
            echo "<tr><td>";
            if ($bestscores[$i] == -1) {
                $bestscores[$i] = 0;
            }
            if ($scores[$i] == -1) {
                $scores[$i] = 0;
                echo _('Question') . ' ' . ($i + 1) . ': </td><td>';
                echo _('Last attempt: ');
                echo _('Not answered');
                echo "</td>";
                echo "<td>  ", _('Score in Gradebook: ');
                echo printscore($bestscores[$i], $i);
                echo "</td>";
                echo "</tr>\n";
            } else {
                echo _('Question') . ' ' . ($i + 1) . ': </td><td>';
                echo _('Last attempt: ');
                echo printscore($scores[$i], $i);
                echo "</td>";
                echo "<td>  ", _('Score in Gradebook: ');
                echo printscore($bestscores[$i], $i);
                echo "</td>";
                echo "</tr>\n";
            }
        }
        echo "</table>";
    }
    global $testid;
    recordtestdata();
    if ($testsettings['testtype'] != "NoScores") {
        echo "<p>", sprintf(_('Total Points on Last Attempts:  %d out of %d possible'), $lastattempttotal, $totpossible), "</p>\n";
        //if ($total<$testsettings['minscore']) {
        if ($testsettings['minscore'] < 10000 && $total < $testsettings['minscore'] || $testsettings['minscore'] > 10000 && $total < ($testsettings['minscore'] - 10000) / 100 * $totpossible) {
            echo "<p><b>", sprintf(_('Total Points Earned:  %d out of %d possible: '), $total, $totpossible);
        } else {
            echo "<p><b>", sprintf(_('Total Points in Gradebook: %d out of %d possible: '), $total, $totpossible);
        }
        echo "{$average} % </b></p>\n";
        if ($outmsg != '') {
            echo "<p style=\"color:red;font-weight: bold;\">{$outmsg}</p>";
            if ($endmsg['commonmsg'] != '' && $endmsg['commonmsg'] != '<p></p>') {
                echo $endmsg['commonmsg'];
            }
        }
        //if ($total<$testsettings['minscore']) {
        if ($testsettings['minscore'] < 10000 && $total < $testsettings['minscore'] || $testsettings['minscore'] > 10000 && $total < ($testsettings['minscore'] - 10000) / 100 * $totpossible) {
            if ($testsettings['minscore'] < 10000) {
                $reqscore = $testsettings['minscore'];
            } else {
                $reqscore = $testsettings['minscore'] - 10000 . '%';
            }
            echo "<p><span style=\"color:red;\"><b>", sprintf(_('A score of %s is required to receive credit for this assessment'), $reqscore), "<br/>", _('Grade in Gradebook: No Credit (NC)'), "</span></p> ";
        }
    } else {
        echo "<p><b>", _('Your scores have been recorded for this assessment.'), "</b></p>";
    }
    //if timelimit is exceeded
    $now = time();
    if (!$timelimitkickout && $testsettings['timelimit'] > 0 && $now - $GLOBALS['starttime'] > $testsettings['timelimit']) {
        $over = $now - $GLOBALS['starttime'] - $testsettings['timelimit'];
        echo "<p>", _('Time limit exceeded by'), " ";
        if ($over > 60) {
            $overmin = floor($over / 60);
            echo "{$overmin} ", _('minutes'), ", ";
            $over = $over - $overmin * 60;
        }
        echo "{$over} ", _('seconds'), ".<br/>\n";
        echo _('Grade is subject to acceptance by the instructor'), "</p>\n";
    }
    if (!$superdone) {
        // $total < $totpossible &&
        if ($noindivscores) {
            echo "<p>", _('<a href="showtest.php?reattempt=all">Reattempt assessment</a> on questions allowed (note: where reattempts are allowed, all scores, correct and incorrect, will be cleared)'), "</p>";
        } else {
            if (canimproveany()) {
                echo "<p>", _('<a href="showtest.php?reattempt=canimprove">Reattempt assessment</a> on questions that can be improved where allowed'), "</p>";
            }
            if (hasreattemptsany()) {
                echo "<p>", _('<a href="showtest.php?reattempt=all">Reattempt assessment</a> on all questions where allowed'), "</p>";
            }
        }
        if ($allowregen) {
            echo "<p>", _('<a href="showtest.php?regenall=missed">Try similar problems</a> for all questions with less than perfect scores where allowed.'), "</p>";
            echo "<p>", _('<a href="showtest.php?regenall=all">Try similar problems</a> for all questions where allowed.'), "</p>";
        }
    }
    if ($testsettings['testtype'] != "NoScores") {
        $hascatset = false;
        foreach ($qi as $qii) {
            if ($qii['category'] != '0') {
                $hascatset = true;
                break;
            }
        }
        if ($hascatset) {
            include "../assessment/catscores.php";
            catscores($questions, $bestscores, $testsettings['defpoints'], $testsettings['defoutcome'], $testsettings['courseid']);
        }
    }
    if ($reviewatend) {
        global $testtype, $scores, $saenddate, $isteacher, $istutor, $seeds, $attempts, $rawscores, $noraw;
        $showa = false;
        for ($i = 0; $i < count($questions); $i++) {
            echo '<div>';
            if (!$noraw) {
                if (strpos($rawscores[$i], '~') !== false) {
                    $col = explode('~', $rawscores[$i]);
                } else {
                    $col = array($rawscores[$i]);
                }
            } else {
                $col = scorestocolors($noraw ? $scores[$i] : $rawscores[$i], $qi[$questions[$i]]['points'], $qi[$questions[$i]]['answeights'], $noraw);
            }
            displayq($i, $qi[$questions[$i]]['questionsetid'], $seeds[$i], $showa, false, $attempts[$i], false, false, false, $col);
            echo "<div class=review>", _('Question') . " " . ($i + 1) . ". ", _('Last Attempt:');
            echo printscore($scores[$i], $i);
            echo '<br/>', _('Score in Gradebook: ');
            echo printscore($bestscores[$i], $i);
            echo '</div>';
        }
    }
    return true;
}
Пример #10
0
 if (!isset($scoredata[$gid])) {
     echo "<td>-</td><td>-</td><td></td>";
     continue;
 } else {
     $line = $scoredata[$gid];
 }
 $total = 0;
 $sp = explode(';', $line['bestscores']);
 $scores = explode(",", $sp[0]);
 if (in_array(-1, $scores)) {
     $IP = 1;
 } else {
     $IP = 0;
 }
 for ($i = 0; $i < count($scores); $i++) {
     $total += getpts($scores[$i]);
 }
 $timeused = $line['endtime'] - $line['starttime'];
 if ($line['id'] == null) {
     echo "<td><a href=\"gb-viewasid.php?gbmode={$gbmode}&cid={$cid}&asid=new&uid={$line['userid']}&from=gisolate&aid={$aid}\">-</a></td><td>-</td><td></td>";
 } else {
     echo "<td><a href=\"gb-viewasid.php?gbmode={$gbmode}&cid={$cid}&asid={$line['id']}&uid={$line['userid']}&from=gisolate&aid={$aid}\">";
     //if ($total<$minscore) {
     if ($minscore < 10000 && $total < $minscore || $minscore > 10000 && $total < ($minscore - 10000) / 100 * $totalpossible) {
         echo "{$total}&nbsp;(NC)";
     } else {
         if ($IP == 1 && $enddate > $now) {
             echo "{$total}&nbsp;(IP)";
         } else {
             if ($timelimit > 0 && $timeused > $timelimit * $line['timelimitmult']) {
                 echo "{$total}&nbsp;(OT)";
Пример #11
0
function embedshowicon($qn)
{
    global $qi, $questions, $attempts, $testsettings, $scores, $bestscores, $noindivscores, $showeachscore, $imasroot, $CFG, $sessiondata, $seeds, $isreview;
    $reattemptsremain = hasreattempts($qn);
    $pointsremaining = getremainingpossible($qn, $qi[$questions[$qn]], $testsettings, $attempts[$qn]);
    $qavail = false;
    if ($isreview) {
        $thisscore = getpts($scores[$qn]);
    } else {
        $thisscore = getpts($bestscores[$qn]);
    }
    if (unans($scores[$qn]) && $attempts[$qn] == 0 || $noindivscores && amreattempting($qn)) {
        if (isset($CFG['TE']['navicons'])) {
            echo "<img class=\"embedicon\" src=\"{$imasroot}/img/{$CFG['TE']['navicons']['untried']}\"/> ";
        } else {
            echo "<img class=\"embedicon\" src=\"{$imasroot}/img/q_fullbox.gif\"/> ";
        }
    } else {
        if (canimprove($qn) && !$noindivscores) {
            if (isset($CFG['TE']['navicons'])) {
                if ($thisscore == 0 || $noindivscores) {
                    echo "<img class=\"embedicon\" src=\"{$imasroot}/img/{$CFG['TE']['navicons']['canretrywrong']}\"/> ";
                } else {
                    echo "<img class=\"embedicon\" src=\"{$imasroot}/img/{$CFG['TE']['navicons']['canretrypartial']}\"/> ";
                }
            } else {
                echo "<img class=\"embedicon\" src=\"{$imasroot}/img/q_halfbox.gif\"/> ";
            }
        } else {
            if ($reattemptsremain) {
                if (isset($CFG['TE']['navicons'])) {
                    if (!$showeachscore) {
                        echo "<img class=\"embedicon\" src=\"{$imasroot}/img/{$CFG['TE']['navicons']['noretry']}\"/> ";
                    } else {
                        if ($thisscore == $qi[$questions[$qn]]['points']) {
                            echo "<img class=\"embedicon\" src=\"{$imasroot}/img/{$CFG['TE']['navicons']['correct']}\"/> ";
                        } else {
                            if ($thisscore == 0) {
                                echo "<img class=\"embedicon\" src=\"{$imasroot}/img/{$CFG['TE']['navicons']['wrong']}\"/> ";
                            } else {
                                echo "<img class=\"embedicon\" src=\"{$imasroot}/img/{$CFG['TE']['navicons']['partial']}\"/> ";
                            }
                        }
                    }
                } else {
                    echo "<img class=\"embedicon\" src=\"{$imasroot}/img/q_emptybox.gif\"/> ";
                }
            } else {
                if (isset($CFG['TE']['navicons'])) {
                    if (!$showeachscore) {
                        echo "<img class=\"embedicon\" src=\"{$imasroot}/img/{$CFG['TE']['navicons']['noretry']}\"/> ";
                    } else {
                        if ($thisscore == $qi[$questions[$qn]]['points']) {
                            echo "<img class=\"embedicon\" src=\"{$imasroot}/img/{$CFG['TE']['navicons']['correct']}\"/> ";
                        } else {
                            if ($thisscore == 0) {
                                echo "<img class=\"embedicon\" src=\"{$imasroot}/img/{$CFG['TE']['navicons']['wrong']}\"/> ";
                            } else {
                                echo "<img class=\"embedicon\" src=\"{$imasroot}/img/{$CFG['TE']['navicons']['partial']}\"/> ";
                            }
                        }
                    }
                } else {
                    echo "<img class=\"embedicon\" src=\"{$imasroot}/img/q_emptybox.gif\"/> ";
                }
            }
        }
    }
}
Пример #12
0
    }
}
if (isset($_POST['assesssnap'])) {
    //doing assessment snapshot
    $query = "SELECT userid,bestscores FROM imas_assessment_sessions WHERE assessmentid='{$_POST['assesssnapaid']}'";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($row = mysql_fetch_row($result)) {
        $sp = explode(';', $row[1]);
        $sc = explode(',', $sp[0]);
        $tot = 0;
        $att = 0;
        foreach ($sc as $v) {
            if (strpos($v, '-1') === false) {
                $att++;
            }
            $tot += getpts($v);
        }
        if ($_POST['assesssnaptype'] == 0) {
            $score = $tot;
        } else {
            $attper = $att / count($sc);
            if ($attper >= $_POST['assesssnapatt'] / 100 - 0.001 && $tot >= $_POST['assesssnappts'] - 1.0E-5) {
                $score = $_POST['points'];
            } else {
                $score = 0;
            }
        }
        $query = "INSERT INTO imas_grades (gradetype,gradetypeid,userid,score,feedback) VALUES ";
        $query .= "('offline','{$_GET['gbitem']}','{$row[0]}','{$score}','')";
        mysql_query($query) or die("Query failed : " . mysql_error());
    }
Пример #13
0
     $questions = explode(",", $bestquestions);
 }
 $sp = explode(';', $line['bestscores']);
 $scores = explode(',', $sp[0]);
 $seeds = explode(',', $line['bestseeds']);
 $bla = explode('~', $line['bestlastanswers']);
 $la = explode('~', $line['lastanswers']);
 if (!isset($sturow[$line['userid']])) {
     continue;
 }
 $r = $sturow[$line['userid']];
 foreach ($questions as $k => $ques) {
     $c = $qcol[$ques];
     $offset = 0;
     if ($dopts) {
         $gb[$r][$c + $offset] = getpts($scores[$k]);
         $offset++;
     }
     if ($doptpts) {
         $gb[$r][$c + $offset] = $scores[$k];
         $offset++;
     }
     if ($doba) {
         $laarr = explode('##', $bla[$k]);
         $gb[$r][$c + $offset] = $laarr[count($laarr) - 1];
         if (strpos($gb[$r][$c + $offset], '$f$')) {
             if (strpos($gb[$r][$c + $offset], '&')) {
                 //is multipart q
                 $laparr = explode('&', $gb[$r][$c + $offset]);
                 foreach ($laparr as $lk => $v) {
                     if (strpos($v, '$f$')) {
Пример #14
0
function calcandupdateLTIgrade($sourcedid, $aid, $scores)
{
    global $aidtotalpossible;
    if (!isset($aidtotalpossible[$aid])) {
        $query = "SELECT itemorder,defpoints FROM imas_assessments WHERE id='{$aid}'";
        $res = mysql_query($query) or die("Query failed : {$query}" . mysql_error());
        $aitems = explode(',', mysql_result($res, 0, 0));
        $defpoints = mysql_result($res, 0, 1);
        foreach ($aitems as $k => $v) {
            if (strpos($v, '~') !== FALSE) {
                $sub = explode('~', $v);
                if (strpos($sub[0], '|') === false) {
                    //backwards compat
                    $aitems[$k] = $sub[0];
                    $aitemcnt[$k] = 1;
                } else {
                    $grpparts = explode('|', $sub[0]);
                    $aitems[$k] = $sub[1];
                    $aitemcnt[$k] = $grpparts[0];
                }
            } else {
                $aitemcnt[$k] = 1;
            }
        }
        $query = "SELECT points,id FROM imas_questions WHERE assessmentid='{$aid}'";
        $result2 = mysql_query($query) or die("Query failed : {$query}: " . mysql_error());
        $totalpossible = 0;
        while ($r = mysql_fetch_row($result2)) {
            if (($k = array_search($r[1], $aitems)) !== false) {
                //only use first item from grouped questions for total pts
                if ($r[0] == 9999) {
                    $totalpossible += $aitemcnt[$k] * $defpoints;
                    //use defpoints
                } else {
                    $totalpossible += $aitemcnt[$k] * $r[0];
                    //use points from question
                }
            }
        }
        $aidtotalpossible[$aid] = $totalpossible;
    }
    $total = 0;
    for ($i = 0; $i < count($scores); $i++) {
        if (getpts($scores[$i]) > 0) {
            $total += getpts($scores[$i]);
        }
    }
    $grade = number_format($total / $aidtotalpossible[$aid], 4);
    return updateLTIgrade('update', $sourcedid, $aid, $grade);
}
Пример #15
0
function catscores($quests, $scores, $defptsposs, $defoutcome = 0, $cid)
{
    $qlist = "'" . implode("','", $quests) . "'";
    $query = "SELECT id,category,points FROM imas_questions WHERE id IN ({$qlist})";
    $result = mysql_query($query) or die("Query failed : {$query}; " . mysql_error());
    $cat = array();
    $pospts = array();
    $tolookup = array($defoutcome);
    while ($row = mysql_fetch_row($result)) {
        if (is_numeric($row[1]) && $row[1] == 0 && $defoutcome != 0) {
            $cat[$row[0]] = $defoutcome;
        } else {
            $cat[$row[0]] = $row[1];
        }
        if (is_numeric($row[1]) && $row[1] > 0) {
            $tolookup[] = $row[1];
        }
        if ($row[2] == 9999) {
            $pospts[$row[0]] = $defptsposs;
        } else {
            $pospts[$row[0]] = $row[2];
        }
    }
    $outcomenames = array();
    $outcomenames[0] = "Uncategorized";
    if (count($tolookup) > 0) {
        $lulist = "'" . implode("','", $tolookup) . "'";
        $query = "SELECT id,name FROM imas_outcomes WHERE id IN ({$lulist})";
        $result = mysql_query($query) or die("Query failed : {$query}; " . mysql_error());
        while ($row = mysql_fetch_row($result)) {
            $outcomenames[$row[0]] = $row[1];
        }
        $query = "SELECT outcomes FROM imas_courses WHERE id='{$cid}'";
        $result = mysql_query($query) or die("Query failed : " . mysql_error());
        $row = mysql_fetch_row($result);
        if ($row[0] == '') {
            $outcomes = array();
        } else {
            $outcomes = unserialize($row[0]);
        }
    }
    $catscore = array();
    $catposs = array();
    for ($i = 0; $i < count($quests); $i++) {
        $pts = getpts($scores[$i]);
        if ($pts < 0) {
            $pts = 0;
        }
        $catscore[$cat[$quests[$i]]] += $pts;
        $catposs[$cat[$quests[$i]]] += $pospts[$quests[$i]];
    }
    echo "<h4>", _('Categorized Score Breakdown'), "</h4>\n";
    echo "<table cellpadding=5 class=gb><thead><tr><th>", _('Category'), "</th><th>", _('Points Earned / Possible (Percent)'), "</th></tr></thead><tbody>\n";
    $alt = 0;
    function printoutcomes($arr, $ind, &$outcomenames, &$catscore, &$catposs)
    {
        $out = '';
        foreach ($arr as $oi) {
            if (is_array($oi)) {
                $outc = printoutcomes($oi['outcomes'], $ind + 1, $outcomenames, $catscore, $catposs);
                if ($outc != '') {
                    $out .= '<tr><td colspan="2"><span class="ind' . $ind . '"><b>' . $oi['name'] . '</b></span></td></tr>';
                    $out .= $outc;
                }
            } else {
                if (isset($catscore[$oi])) {
                    $out .= '<tr><td><span class="ind' . $ind . '">' . $outcomenames[$oi] . '</span></td>';
                    $pc = round(100 * $catscore[$oi] / $catposs[$oi], 1);
                    $out .= "<td>{$catscore[$oi]} / {$catposs[$oi]} ({$pc} %)</td></tr>\n";
                }
            }
        }
        return $out;
    }
    if (count($tolookup) > 0) {
        $outc = preg_split('/<tr/', printoutcomes($outcomes, 0, $outcomenames, $catscore, $catposs));
        for ($i = 1; $i < count($outc); $i++) {
            if ($alt == 0) {
                echo '<tr class="even"';
                $alt = 1;
            } else {
                echo '<tr class="odd"';
                $alt = 0;
            }
            echo $outc[$i];
        }
    }
    foreach (array_keys($catscore) as $category) {
        if (is_numeric($category)) {
            continue;
        } else {
            $catname = $category;
        }
        if ($alt == 0) {
            echo "<tr class=even>";
            $alt = 1;
        } else {
            echo "<tr class=odd>";
            $alt = 0;
        }
        $pc = round(100 * $catscore[$category] / $catposs[$category], 1);
        echo "<td>{$catname}</td><td>{$catscore[$category]} / {$catposs[$category]} ({$pc} %)</td></tr>\n";
    }
    echo "</tbody></table>\n";
}
Пример #16
0
function outcometable()
{
    global $cid, $isteacher, $istutor, $tutorid, $userid, $catfilter, $secfilter, $timefilter, $lnfilter, $isdiag, $sel1name, $sel2name, $canviewall, $hidelocked;
    if ($canviewall && func_num_args() > 0) {
        $limuser = func_get_arg(0);
    } else {
        if (!$canviewall) {
            $limuser = $userid;
        } else {
            $limuser = 0;
        }
    }
    $category = array();
    $outc = array();
    $gb = array();
    $ln = 0;
    //Pull Gradebook Scheme info
    $query = "SELECT useweights,orderby,defaultcat,usersort FROM imas_gbscheme WHERE courseid='{$cid}'";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    list($useweights, $orderby, $defaultcat, $usersort) = mysql_fetch_row($result);
    if ($useweights == 2) {
        $useweights = 0;
    }
    //use 0 mode for calculation of totals
    if (isset($GLOBALS['setorderby'])) {
        $orderby = $GLOBALS['setorderby'];
    }
    //Build user ID headers
    $gb[0][0][0] = "Name";
    $query = "SELECT count(id) FROM imas_students WHERE imas_students.courseid='{$cid}' AND imas_students.section IS NOT NULL";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    if (mysql_result($result, 0, 0) > 0) {
        $hassection = true;
    } else {
        $hassection = false;
    }
    //Pull Assessment Info
    $now = time();
    $query = "SELECT id,name,defpoints,deffeedback,timelimit,minscore,startdate,enddate,itemorder,gbcategory,cntingb,avail,groupsetid,defoutcome FROM imas_assessments WHERE courseid='{$cid}' AND avail>0 ";
    $query .= "AND cntingb>0 AND cntingb<3 ";
    if ($istutor) {
        $query .= "AND tutoredit<2 ";
    }
    if ($catfilter > -1) {
        $query .= "AND gbcategory='{$catfilter}' ";
    }
    $query .= "ORDER BY enddate,name";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    $overallpts = 0;
    $now = time();
    $kcnt = 0;
    $assessments = array();
    $grades = array();
    $discuss = array();
    $startdate = array();
    $enddate = array();
    $avail = array();
    $category = array();
    $name = array();
    $possible = array();
    $courseorder = array();
    $qposs = array();
    $qoutcome = array();
    $itemoutcome = array();
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        if (substr($line['deffeedback'], 0, 8) == 'Practice') {
            continue;
        }
        if ($line['avail'] == 2) {
            $line['startdate'] = 0;
            $line['enddate'] = 2000000000;
        }
        if ($now < $line['startdate']) {
            continue;
            //we don't want future items
        } else {
            if ($now < $line['enddate']) {
                $avail[$kcnt] = 1;
            } else {
                $avail[$kcnt] = 0;
            }
        }
        $enddate[$kcnt] = $line['enddate'];
        $startdate[$kcnt] = $line['startdate'];
        $assessments[$kcnt] = $line['id'];
        $category[$kcnt] = $line['gbcategory'];
        $name[$kcnt] = $line['name'];
        $cntingb[$kcnt] = $line['cntingb'];
        //1: count, 2: extra credit
        $assessoutcomes[$kcnt] = array();
        $aitems = explode(',', $line['itemorder']);
        foreach ($aitems as $k => $v) {
            if (strpos($v, '~') !== FALSE) {
                $sub = explode('~', $v);
                if (strpos($sub[0], '|') === false) {
                    //backwards compat
                    $aitems[$k] = $sub[0];
                    $aitemcnt[$k] = 1;
                } else {
                    $grpparts = explode('|', $sub[0]);
                    $aitems[$k] = $sub[1];
                    $aitemcnt[$k] = $grpparts[0];
                }
            } else {
                $aitemcnt[$k] = 1;
            }
        }
        $query = "SELECT points,id,category FROM imas_questions WHERE assessmentid='{$line['id']}'";
        $result2 = mysql_query($query) or die("Query failed : {$query}: " . mysql_error());
        $totalpossible = 0;
        while ($r = mysql_fetch_row($result2)) {
            if ($r[0] == 9999) {
                $qposs[$r[1]] = $line['defpoints'];
            } else {
                $qposs[$r[1]] = $r[0];
            }
            if (is_numeric($r[2]) && $r[2] > 0) {
                $qoutcome[$r[1]] = $r[2];
            } else {
                if ($line['defoutcome'] > 0) {
                    $qoutcome[$r[1]] = $line['defoutcome'];
                }
            }
        }
        $possible[$kcnt] = array();
        foreach ($aitems as $k => $q) {
            if (!isset($qoutcome[$q])) {
                continue;
            }
            if (!isset($possible[$kcnt][$qoutcome[$q]])) {
                $possible[$kcnt][$qoutcome[$q]] = 0;
            }
            $possible[$kcnt][$qoutcome[$q]] += $aitemcnt[$k] * $qposs[$q];
        }
        $kcnt++;
    }
    //Pull Offline Grade item info
    $query = "SELECT * from imas_gbitems WHERE courseid='{$cid}' AND outcomes<>'' ";
    $query .= "AND showdate<{$now} ";
    $query .= "AND cntingb>0 AND cntingb<3 ";
    if ($istutor) {
        $query .= "AND tutoredit<2 ";
    }
    if ($catfilter > -1) {
        $query .= "AND gbcategory='{$catfilter}' ";
    }
    $query .= "ORDER BY showdate";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $avail[$kcnt] = 0;
        $grades[$kcnt] = $line['id'];
        $assessmenttype[$kcnt] = "Offline";
        $category[$kcnt] = $line['gbcategory'];
        $enddate[$kcnt] = $line['showdate'];
        $startdate[$kcnt] = $line['showdate'];
        $possible[$kcnt] = $line['points'];
        $name[$kcnt] = $line['name'];
        $cntingb[$kcnt] = $line['cntingb'];
        $itemoutcome[$kcnt] = explode(',', $line['outcomes']);
        $kcnt++;
    }
    //Pull Discussion Grade info
    $query = "SELECT id,name,gbcategory,startdate,enddate,replyby,postby,points,cntingb,avail FROM imas_forums WHERE courseid='{$cid}' AND points>0 AND avail>0 ";
    $query .= "AND startdate<{$now} AND outcomes<>'' ";
    if ($catfilter > -1) {
        $query .= "AND gbcategory='{$catfilter}' ";
    }
    $query .= "ORDER BY enddate,postby,replyby,startdate";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $discuss[$kcnt] = $line['id'];
        $assessmenttype[$kcnt] = "Discussion";
        $category[$kcnt] = $line['gbcategory'];
        if ($line['avail'] == 2) {
            $line['startdate'] = 0;
            $line['enddate'] = 2000000000;
        }
        $enddate[$kcnt] = $line['enddate'];
        $startdate[$kcnt] = $line['startdate'];
        if ($now < $line['enddate']) {
            $avail[$kcnt] = 1;
            if ($line['replyby'] > 0 && $line['replyby'] < 2000000000) {
                if ($line['postby'] > 0 && $line['postby'] < 2000000000) {
                    if ($now > $line['replyby'] && $now > $line['postby']) {
                        $avail[$kcnt] = 0;
                    }
                } else {
                    if ($now > $line['replyby']) {
                        $avail[$kcnt] = 0;
                    }
                }
            } else {
                if ($line['postby'] > 0 && $line['postby'] < 2000000000) {
                    if ($now > $line['postby']) {
                        $avail[$kcnt] = 0;
                    }
                }
            }
        } else {
            $avail[$kcnt] = 0;
        }
        $possible[$kcnt] = $line['points'];
        $name[$kcnt] = $line['name'];
        $cntingb[$kcnt] = $line['cntingb'];
        $itemoutcome[$kcnt] = explode(',', $line['outcomes']);
        $kcnt++;
    }
    $cats = array();
    $catcolcnt = 0;
    //Pull Categories:  Name, scale, scaletype, chop, drop, weight
    if (in_array(0, $category)) {
        //define default category, if used
        $cats[0] = explode(',', $defaultcat);
        array_unshift($cats[0], "Default");
        array_push($cats[0], $catcolcnt);
        $catcolcnt++;
    }
    $query = "SELECT id,name,scale,scaletype,chop,dropn,weight,hidden FROM imas_gbcats WHERE courseid='{$cid}' ";
    $query .= "ORDER BY name";
    $result = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($row = mysql_fetch_row($result)) {
        if (in_array($row[0], $category)) {
            //define category if used
            if ($row[1][0] >= '1' && $row[1][0] <= '9') {
                $row[1] = substr($row[1], 1);
            }
            $cats[$row[0]] = array_slice($row, 1);
            array_push($cats[$row[0]], $catcolcnt);
            $catcolcnt++;
        }
    }
    //create item headers
    $pos = 0;
    $itemorder = array();
    $assesscol = array();
    $gradecol = array();
    $discusscol = array();
    if ($orderby == 1) {
        //order $category by enddate
        asort($enddate, SORT_NUMERIC);
        $newcategory = array();
        foreach ($enddate as $k => $v) {
            $newcategory[$k] = $category[$k];
        }
        $category = $newcategory;
    } else {
        if ($orderby == 5) {
            //order $category by enddate reverse
            arsort($enddate, SORT_NUMERIC);
            $newcategory = array();
            foreach ($enddate as $k => $v) {
                $newcategory[$k] = $category[$k];
            }
            $category = $newcategory;
        } else {
            if ($orderby == 7) {
                //order $category by startdate
                asort($startdate, SORT_NUMERIC);
                $newcategory = array();
                foreach ($startdate as $k => $v) {
                    $newcategory[$k] = $category[$k];
                }
                $category = $newcategory;
            } else {
                if ($orderby == 9) {
                    //order $category by startdate reverse
                    arsort($startdate, SORT_NUMERIC);
                    $newcategory = array();
                    foreach ($startdate as $k => $v) {
                        $newcategory[$k] = $category[$k];
                    }
                    $category = $newcategory;
                } else {
                    if ($orderby == 3) {
                        //order $category alpha
                        natcasesort($name);
                        //asort($name);
                        $newcategory = array();
                        foreach ($name as $k => $v) {
                            $newcategory[$k] = $category[$k];
                        }
                        $category = $newcategory;
                    }
                }
            }
        }
    }
    foreach (array_keys($cats) as $cat) {
        //foreach category
        $catkeys = array_keys($category, $cat);
        //pull items in that category
        if (($orderby & 1) == 1) {
            //order by category
            array_splice($itemorder, count($itemorder), 0, $catkeys);
        }
        foreach ($catkeys as $k) {
            if (isset($cats[$cat][6]) && $cats[$cat][6] == 1) {
                //hidden
                $cntingb[$k] = 0;
            }
            if (($orderby & 1) == 1) {
                //display item header if displaying by category
                //$cathdr[$pos] = $cats[$cat][6];
                $gb[0][1][$pos][0] = $name[$k];
                //item name
                $gb[0][1][$pos][1] = $cats[$cat][7];
                //item category number
                $gb[0][1][$pos][2] = $avail[$k];
                //0 past, 1 current, 2 future
                $gb[0][1][$pos][3] = $cntingb[$k];
                //0 no count and hide, 1 count, 2 EC, 3 no count
                if (isset($assessments[$k])) {
                    $gb[0][1][$pos][4] = 0;
                    //0 online, 1 offline
                    $gb[0][1][$pos][5] = $assessments[$k];
                    $assesscol[$assessments[$k]] = $pos;
                } else {
                    if (isset($grades[$k])) {
                        $gb[0][1][$pos][4] = 1;
                        //0 online, 1 offline
                        $gb[0][1][$pos][5] = $grades[$k];
                        $gradecol[$grades[$k]] = $pos;
                    } else {
                        if (isset($discuss[$k])) {
                            $gb[0][1][$pos][4] = 2;
                            //0 online, 1 offline, 2 discuss
                            $gb[0][1][$pos][5] = $discuss[$k];
                            $discusscol[$discuss[$k]] = $pos;
                        }
                    }
                }
                $gb[0][1][$pos][6] = array();
                $pos++;
            }
        }
    }
    if (($orderby & 1) == 0) {
        //if not grouped by category
        if ($orderby == 0) {
            //enddate
            asort($enddate, SORT_NUMERIC);
            $itemorder = array_keys($enddate);
        } else {
            if ($orderby == 2) {
                //alpha
                natcasesort($name);
                //asort($name);
                $itemorder = array_keys($name);
            } else {
                if ($orderby == 4) {
                    //enddate reverse
                    arsort($enddate, SORT_NUMERIC);
                    $itemorder = array_keys($enddate);
                } else {
                    if ($orderby == 6) {
                        //startdate
                        asort($startdate, SORT_NUMERIC);
                        $itemorder = array_keys($startdate);
                    } else {
                        if ($orderby == 8) {
                            //startdate reverse
                            arsort($startdate, SORT_NUMERIC);
                            $itemorder = array_keys($startdate);
                        }
                    }
                }
            }
        }
        foreach ($itemorder as $k) {
            $gb[0][1][$pos][0] = $name[$k];
            //item name
            $gb[0][1][$pos][1] = $cats[$category[$k]][7];
            //item category name
            $gb[0][1][$pos][2] = $avail[$k];
            //0 past, 1 current, 2 future
            $gb[0][1][$pos][3] = $cntingb[$k];
            //0 no count and hide, 1 count, 2 EC, 3 no count
            if (isset($assessments[$k])) {
                $gb[0][1][$pos][4] = 0;
                //0 online, 1 offline
                $gb[0][1][$pos][5] = $assessments[$k];
                $assesscol[$assessments[$k]] = $pos;
            } else {
                if (isset($grades[$k])) {
                    $gb[0][1][$pos][4] = 1;
                    //0 online, 1 offline
                    $gb[0][1][$pos][5] = $grades[$k];
                    $gradecol[$grades[$k]] = $pos;
                } else {
                    if (isset($discuss[$k])) {
                        $gb[0][1][$pos][4] = 2;
                        //0 online, 1 offline, 2 discuss
                        $gb[0][1][$pos][5] = $discuss[$k];
                        $discusscol[$discuss[$k]] = $pos;
                    }
                }
            }
            $pos++;
        }
    }
    //create category headers
    $pos = 0;
    $catorder = array_keys($cats);
    foreach ($catorder as $cat) {
        //foreach category
        $gb[0][2][$pos][0] = $cats[$cat][0];
        $gb[0][2][$pos][1] = $cats[$cat][7];
        $pos++;
    }
    //Pull student data
    $ln = 1;
    $query = "SELECT imas_users.id,imas_users.SID,imas_users.FirstName,imas_users.LastName,imas_users.SID,imas_users.email,imas_students.section,imas_students.code,imas_students.locked,imas_students.timelimitmult,imas_students.lastaccess,imas_users.hasuserimg ";
    $query .= "FROM imas_users,imas_students WHERE imas_users.id=imas_students.userid AND imas_students.courseid='{$cid}' ";
    //$query .= "FROM imas_users,imas_teachers WHERE imas_users.id=imas_teachers.userid AND imas_teachers.courseid='$cid' ";
    //if (!$isteacher && !isset($tutorid)) {$query .= "AND imas_users.id='$userid' ";}
    if ($limuser > 0) {
        $query .= "AND imas_users.id='{$limuser}' ";
    }
    if ($secfilter != -1) {
        $query .= "AND imas_students.section='{$secfilter}' ";
    }
    if ($hidelocked) {
        $query .= "AND imas_students.locked=0 ";
    }
    if (isset($timefilter)) {
        $tf = time() - 60 * 60 * $timefilter;
        $query .= "AND imas_users.lastaccess>{$tf} ";
    }
    if (isset($lnfilter) && $lnfilter != '') {
        $query .= "AND imas_users.LastName LIKE '{$lnfilter}%' ";
    }
    if ($isdiag) {
        $query .= "ORDER BY imas_users.email,imas_users.LastName,imas_users.FirstName";
    } else {
        if ($hassection && $usersort == 0) {
            $query .= "ORDER BY imas_students.section,imas_users.LastName,imas_users.FirstName";
        } else {
            $query .= "ORDER BY imas_users.LastName,imas_users.FirstName";
        }
    }
    $result = mysql_query($query) or die("Query failed : {$query}: " . mysql_error());
    $alt = 0;
    $sturow = array();
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        //foreach student
        unset($asid);
        unset($pts);
        unset($IP);
        unset($timeused);
        $cattotpast[$ln] = array();
        $cattotpastec[$ln] = array();
        $catposspast[$ln] = array();
        $cattotcur[$ln] = array();
        $cattotcurec[$ln] = array();
        $catposscur[$ln] = array();
        //Student ID info
        $gb[$ln][0][0] = "{$line['LastName']},&nbsp;{$line['FirstName']}";
        $gb[$ln][0][1] = $line['id'];
        $gb[$ln][0][2] = $line['locked'];
        $sturow[$line['id']] = $ln;
        $ln++;
    }
    //pull exceptions
    $exceptions = array();
    $query = "SELECT imas_exceptions.assessmentid,imas_exceptions.userid,imas_exceptions.enddate,imas_exceptions.islatepass FROM imas_exceptions,imas_assessments WHERE ";
    $query .= "imas_exceptions.assessmentid=imas_assessments.id AND imas_assessments.courseid='{$cid}'";
    $result2 = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($r = mysql_fetch_row($result2)) {
        if (!isset($sturow[$r[1]])) {
            continue;
        }
        $exceptions[$r[0]][$r[1]] = array($r[2], $r[3]);
        $gb[$sturow[$r[1]]][1][$assesscol[$r[0]]][2] = 10;
        //will get overwritten later if assessment session exists
    }
    //Get assessment scores
    $assessidx = array_flip($assessments);
    $query = "SELECT ias.id,ias.assessmentid,ias.questions,ias.bestscores,ias.starttime,ias.endtime,ias.timeontask,ias.feedback,ias.userid,ia.timelimit FROM imas_assessment_sessions AS ias,imas_assessments AS ia ";
    $query .= "WHERE ia.id=ias.assessmentid AND ia.courseid='{$cid}'";
    if ($limuser > 0) {
        $query .= " AND ias.userid='{$limuser}' ";
    }
    $result2 = mysql_query($query) or die("Query failed : " . mysql_error());
    while ($l = mysql_fetch_array($result2, MYSQL_ASSOC)) {
        if (!isset($assessidx[$l['assessmentid']]) || !isset($sturow[$l['userid']]) || !isset($assesscol[$l['assessmentid']])) {
            continue;
        }
        $i = $assessidx[$l['assessmentid']];
        $row = $sturow[$l['userid']];
        $col = $assesscol[$l['assessmentid']];
        $gb[$row][1][$col][3] = $l['id'];
        //assessment session id
        if (strpos($l['questions'], ';') === false) {
            $questions = explode(",", $l['questions']);
        } else {
            list($questions, $bestquestions) = explode(";", $l['questions']);
            $questions = explode(",", $bestquestions);
        }
        $sp = explode(';', $l['bestscores']);
        $scores = explode(",", $sp[0]);
        $pts = array();
        $ptsposs = array();
        for ($j = 0; $j < count($scores); $j++) {
            if (!isset($qoutcome[$questions[$j]])) {
                continue;
            }
            //no outcome set - skip it
            if (!isset($pts[$qoutcome[$questions[$j]]])) {
                $pts[$qoutcome[$questions[$j]]] = 0;
            }
            if (!isset($ptsposs[$qoutcome[$questions[$j]]])) {
                $ptsposs[$qoutcome[$questions[$j]]] = 0;
            }
            $pts[$qoutcome[$questions[$j]]] += getpts($scores[$j]);
            $ptsposs[$qoutcome[$questions[$j]]] += $qposs[$questions[$j]];
        }
        if (in_array(-1, $scores)) {
            $IP = 1;
        } else {
            $IP = 0;
        }
        if (isset($exceptions[$l['assessmentid']][$l['userid']])) {
            // && $now>$enddate[$i] && $now<$exceptions[$l['assessmentid']][$l['userid']]) {
            if ($enddate[$i] > $exceptions[$l['assessmentid']][$l['userid']][0] && $assessmenttype[$i] == "NoScores") {
                //if exception set for earlier, and NoScores is set, use later date to hide score until later
                $thised = $enddate[$i];
            } else {
                $thised = $exceptions[$l['assessmentid']][$l['userid']][0];
                if ($limuser > 0 && $gb[0][1][$col][2] == 2) {
                    //change $avail past/cur/future
                    if ($now < $thised) {
                        $gb[0][1][$col][2] = 1;
                    } else {
                        $gb[0][1][$col][2] = 0;
                    }
                }
            }
            $inexception = true;
        } else {
            $thised = $enddate[$i];
            $inexception = false;
        }
        $countthisone = false;
        $gb[$row][1][$col][1] = $ptsposs;
        if ($assessmenttype[$i] == "NoScores" && $sa[$i] != "I" && $now < $thised && !$canviewall) {
            $gb[$row][1][$col][0] = 'N/A';
            //score is not available
            $gb[$row][1][$col][2] = 0;
            //no other info
        } else {
            if ($minscores[$i] < 10000 && $pts < $minscores[$i] || $minscores[$i] > 10000 && $pts < ($minscores[$i] - 10000) / 100 * $possible[$i]) {
                //else if ($pts<$minscores[$i]) {
                if ($canviewall) {
                    $gb[$row][1][$col][0] = $pts;
                    //the score
                    $gb[$row][1][$col][2] = 1;
                    //no credit
                } else {
                    $gb[$row][1][$col][0] = 'NC';
                    //score is No credit
                    $gb[$row][1][$col][2] = 1;
                    //no credit
                }
            } else {
                if ($IP == 1 && $thised > $now && ($timelimits[$i] == 0 || $timeused < $timelimits[$i] * $timelimitmult[$l['userid']])) {
                    $gb[$row][1][$col][0] = $pts;
                    //the score
                    $gb[$row][1][$col][2] = 2;
                    //in progress
                    $countthisone = true;
                } else {
                    if ($timelimits[$i] > 0 && $timeused > $timelimits[$i] * $timelimitmult[$l['userid']]) {
                        $gb[$row][1][$col][0] = $pts;
                        //the score
                        $gb[$row][1][$col][2] = 3;
                        //over time
                    } else {
                        if ($assessmenttype[$i] == "Practice") {
                            $gb[$row][1][$col][0] = $pts;
                            //the score
                            $gb[$row][1][$col][2] = 4;
                            //practice test
                        } else {
                            //regular score available to students
                            $gb[$row][1][$col][0] = $pts;
                            //the score
                            $gb[$row][1][$col][2] = 0;
                            //no other info
                            $countthisone = true;
                        }
                    }
                }
            }
        }
        if ($now < $thised) {
            //still active
            $gb[$row][1][$col][2] += 10;
        }
        if ($countthisone) {
            foreach ($pts as $oc => $pv) {
                if ($cntingb[$i] == 1) {
                    if ($gb[0][1][$col][2] < 1) {
                        //past
                        $cattotpast[$row][$category[$i]][$oc][$col] = $pv;
                        $catposspast[$row][$category[$i]][$oc][$col] = $ptsposs[$oc];
                    }
                    if ($gb[0][1][$col][2] < 2) {
                        //past or cur
                        $cattotcur[$row][$category[$i]][$oc][$col] = $pv;
                        $catposscur[$row][$category[$i]][$oc][$col] = $ptsposs[$oc];
                    }
                } else {
                    if ($cntingb[$i] == 2) {
                        if ($gb[0][1][$col][2] < 1) {
                            //past
                            $cattotpastec[$row][$category[$i]][$oc][$col] = $pv;
                        }
                        if ($gb[0][1][$col][2] < 2) {
                            //past or cur
                            $cattotcurec[$row][$category[$i]][$oc][$col] = $pv;
                        }
                    }
                }
            }
        }
    }
    //Get other grades
    $gradeidx = array_flip($grades);
    unset($gradeid);
    unset($opts);
    unset($discusspts);
    $discussidx = array_flip($discuss);
    $gradetypeselects = array();
    if (count($grades) > 0) {
        $gradeidlist = implode(',', $grades);
        $gradetypeselects[] = "(gradetype='offline' AND gradetypeid IN ({$gradeidlist}))";
    }
    if (count($discuss) > 0) {
        $forumidlist = implode(',', $discuss);
        $gradetypeselects[] = "(gradetype='forum' AND gradetypeid IN ({$forumidlist}))";
    }
    if (count($gradetypeselects) > 0) {
        $sel = implode(' OR ', $gradetypeselects);
        $query = "SELECT * FROM imas_grades WHERE ({$sel})";
        if ($limuser > 0) {
            $query .= " AND userid='{$limuser}' ";
        }
        $result2 = mysql_query($query) or die("Query failed : {$query} " . mysql_error());
        while ($l = mysql_fetch_array($result2, MYSQL_ASSOC)) {
            if ($l['gradetype'] == 'offline') {
                if (!isset($gradeidx[$l['gradetypeid']]) || !isset($sturow[$l['userid']]) || !isset($gradecol[$l['gradetypeid']])) {
                    continue;
                }
                $i = $gradeidx[$l['gradetypeid']];
                $row = $sturow[$l['userid']];
                $col = $gradecol[$l['gradetypeid']];
                foreach ($itemoutcome[$i] as $oc) {
                    $gb[$row][1][$col][3] = $l['id'];
                    if ($l['score'] != null) {
                        $gb[$row][1][$col][0][$oc] = 1 * $l['score'];
                        $gb[$row][1][$col][1][$oc] = $possible[$i];
                    }
                    if ($cntingb[$i] == 1) {
                        if ($gb[0][1][$col][2] < 1) {
                            //past
                            $cattotpast[$row][$category[$i]][$oc][$col] = 1 * $l['score'];
                            $catposspast[$row][$category[$i]][$oc][$col] = $possible[$i];
                        }
                        if ($gb[0][1][$col][2] < 2) {
                            //past or cur
                            $cattotcur[$row][$category[$i]][$oc][$col] = 1 * $l['score'];
                            $catposscur[$row][$category[$i]][$oc][$col] = $possible[$i];
                        }
                    } else {
                        if ($cntingb[$i] == 2) {
                            if ($gb[0][1][$col][2] < 1) {
                                //past
                                $cattotpastec[$row][$category[$i]][$oc][$col] = 1 * $l['score'];
                            }
                            if ($gb[0][1][$col][2] < 2) {
                                //past or cur
                                $cattotcurec[$row][$category[$i]][$oc][$col] = 1 * $l['score'];
                            }
                        }
                    }
                }
            } else {
                if ($l['gradetype'] == 'forum') {
                    if (!isset($discussidx[$l['gradetypeid']]) || !isset($sturow[$l['userid']]) || !isset($discusscol[$l['gradetypeid']])) {
                        continue;
                    }
                    $i = $discussidx[$l['gradetypeid']];
                    $row = $sturow[$l['userid']];
                    $col = $discusscol[$l['gradetypeid']];
                    foreach ($itemoutcome[$i] as $oc) {
                        if ($l['score'] != null) {
                            if (isset($gb[$row][1][$col][0])) {
                                $gb[$row][1][$col][0][$oc] += 1 * $l['score'];
                                //adding up all forum scores
                            } else {
                                $gb[$row][1][$col][0][$oc] = 1 * $l['score'];
                            }
                        }
                        if ($gb[0][1][$col][2] < 1) {
                            //past
                            $cattotpast[$row][$category[$i]][$oc][$col] = $gb[$row][1][$col][0];
                            $catposspast[$row][$category[$i]][$oc][$col] = $possible[$i];
                        }
                        if ($gb[0][1][$col][3] < 2) {
                            //past or cur
                            $cattotcur[$row][$category[$i]][$oc][$col] = $gb[$row][1][$col][0];
                            $catposscur[$row][$category[$i]][$oc][$col] = $possible[$i];
                        }
                    }
                }
            }
        }
    }
    //create category totals
    for ($ln = 1; $ln < count($sturow) + 1; $ln++) {
        //foreach student calculate category totals and total totals
        //zero out past due items
        foreach ($gb[0][1] as $col => $inf) {
            if ($gb[0][1][$col][2] > 0 || count($gb[$ln][1][$col][1]) > 0) {
                continue;
            }
            //skip if current, or if already set
            if ($inf[4] == 0 && count($possible[$assessidx[$inf[5]]]) == 0) {
                continue;
            }
            //assess has no outcomes
            $gb[$ln][1][$col] = array();
            $gb[$ln][1][$col][0] = array();
            $gb[$ln][1][$col][1] = array();
            if ($inf[4] == 0) {
                //online item
                $i = $assessidx[$inf[5]];
                foreach ($possible[$i] as $oc => $p) {
                    $gb[$ln][1][$col][0][$oc] = 0;
                    $gb[$ln][1][$col][1][$oc] = $p;
                    $cattotpast[$ln][$category[$i]][$oc][$col] = 0;
                    $catposspast[$ln][$category[$i]][$oc][$col] = $p;
                    $cattotcur[$ln][$category[$i]][$oc][$col] = 0;
                    $catposscur[$ln][$category[$i]][$oc][$col] = $p;
                }
                $gb[$ln][1][$col][3] = 0;
                $gb[$ln][1][$col][4] = 'new';
            } else {
                //offline or discussion
                if ($inf[4] == 1) {
                    $i = $gradeidx[$inf[5]];
                } else {
                    if ($inf[4] == 2) {
                        $i = $discussidx[$inf[5]];
                    }
                }
                foreach ($itemoutcome[$i] as $oc) {
                    $gb[$ln][1][$col][0][$oc] = 0;
                    $gb[$ln][1][$col][1][$oc] = $possible[$i];
                    $cattotpast[$ln][$category[$i]][$oc][$col] = 0;
                    $catposspast[$ln][$category[$i]][$oc][$col] = $possible[$i];
                    $cattotcur[$ln][$category[$i]][$oc][$col] = 0;
                    $catposscur[$ln][$category[$i]][$oc][$col] = $possible[$i];
                }
            }
        }
        $totpast = array();
        $totposspast = array();
        $totcur = array();
        $totposscur = array();
        $pos = 0;
        //reset position for category totals
        foreach ($catorder as $cat) {
            //foreach category
            //add up scores for each outcome
            if (isset($cattotpast[$ln][$cat])) {
                foreach ($cattotpast[$ln][$cat] as $oc => $scs) {
                    $cattotpast[$ln][$cat][$oc] = array_sum($scs);
                    if (isset($cattotpastec[$ln][$cat][$oc])) {
                        $cattotpast[$ln][$cat][$oc] += array_sum($cattotpastec[$ln][$cat][$oc]);
                    }
                    $catposspast[$ln][$cat][$oc] = array_sum($catposspast[$ln][$cat][$oc]);
                    $gb[$ln][2][$pos][0][$oc] = $cattotpast[$ln][$cat][$oc];
                    $gb[$ln][2][$pos][1][$oc] = $catposspast[$ln][$cat][$oc];
                    if (!isset($totpast[$oc])) {
                        $totpast[$oc] = 0;
                        $totposspast[$oc] = 0;
                    }
                    if ($useweights == 1 && $catposspast[$ln][$cat][$oc] > 0) {
                        $totposspast[$oc] += $cats[$cat][5] / 100;
                        $totpast[$oc] += $cattotpast[$ln][$cat][$oc] * $cats[$cat][5] / (100 * $catposspast[$ln][$cat][$oc]);
                    } else {
                        if ($useweights == 0) {
                            $totposspast[$oc] += $catposspast[$ln][$cat][$oc];
                            $totpast[$oc] += $cattotpast[$ln][$cat][$oc];
                        }
                    }
                }
            }
            if (isset($cattotcur[$ln][$cat])) {
                foreach ($cattotcur[$ln][$cat] as $oc => $scs) {
                    $cattotcur[$ln][$cat][$oc] = array_sum($scs);
                    if (isset($cattotcurec[$ln][$cat][$oc])) {
                        $cattotcur[$ln][$cat][$oc] += array_sum($cattotcurec[$ln][$cat][$oc]);
                    }
                    $catposscur[$ln][$cat][$oc] = array_sum($catposscur[$ln][$cat][$oc]);
                    $gb[$ln][2][$pos][2][$oc] = $cattotcur[$ln][$cat][$oc];
                    $gb[$ln][2][$pos][3][$oc] = $catposscur[$ln][$cat][$oc];
                    if (!isset($totcur[$oc])) {
                        $totcur[$oc] = 0;
                        $totposscur[$oc] = 0;
                    }
                    if ($useweights == 1 && $catposscur[$ln][$cat][$oc] > 0) {
                        $totposscur[$oc] += $cats[$cat][5] / 100;
                        $totcur[$oc] += $cattotcur[$ln][$cat][$oc] * $cats[$cat][5] / (100 * $catposscur[$ln][$cat][$oc]);
                    } else {
                        if ($useweights == 0) {
                            $totposscur[$oc] += $catposscur[$ln][$cat][$oc];
                            $totcur[$oc] += $cattotcur[$ln][$cat][$oc];
                        }
                    }
                }
            }
            $pos++;
        }
        foreach ($totpast as $oc => $v) {
            if ($totposspast[$oc] > 0) {
                $gb[$ln][3][0][$oc] = $totpast[$oc] / $totposspast[$oc];
            }
        }
        foreach ($totcur as $oc => $v) {
            if ($totposscur[$oc] > 0) {
                $gb[$ln][3][1][$oc] = $totcur[$oc] / $totposscur[$oc];
            }
        }
    }
    if ($limuser < 1) {
        $gb[$ln][0][0] = "Averages";
        $gb[$ln][0][1] = -1;
        foreach ($gb[0][1] as $i => $inf) {
            $avg = array();
            $avgposs = array();
            for ($j = 1; $j < $ln; $j++) {
                if (isset($gb[$j][1][$i]) && isset($gb[$j][1][$i][0])) {
                    foreach ($gb[$j][1][$i][0] as $oc => $sc) {
                        if (!isset($avg[$oc])) {
                            $avg[$oc] = array();
                            $avgposs[$oc] = array();
                        }
                        $avg[$oc][] = $sc;
                        $avgposs[$oc][] = $gb[$j][1][$i][1][$oc];
                    }
                }
            }
            foreach ($avg as $oc => $scs) {
                $gb[$ln][1][$i][0][$oc] = array_sum($avg[$oc]) / count($avg[$oc]);
                $gb[$ln][1][$i][1][$oc] = array_sum($avgposs[$oc]) / count($avg[$oc]);
            }
        }
        foreach ($gb[0][2] as $i => $inf) {
            $avg = array();
            $avgposs = array();
            $avgatt = array();
            $avgattposs = array();
            for ($j = 1; $j < $ln; $j++) {
                if (isset($gb[$j][2][$i]) && isset($gb[$j][2][$i][0])) {
                    foreach ($gb[$j][2][$i][0] as $oc => $sc) {
                        if (!isset($avg[$oc])) {
                            $avg[$oc] = array();
                            $avgposs[$oc] = array();
                        }
                        $avg[$oc][] = $sc;
                        $avgposs[$oc][] = $gb[$j][2][$i][1][$oc];
                    }
                }
                if (isset($gb[$j][2][$i]) && isset($gb[$j][2][$i][2])) {
                    foreach ($gb[$j][2][$i][2] as $oc => $sc) {
                        if (!isset($avgatt[$oc])) {
                            $avgatt[$oc] = array();
                            $avgpossatt[$oc] = array();
                        }
                        $avgatt[$oc][] = $sc;
                        $avgattposs[$oc][] = $gb[$j][2][$i][3][$oc];
                    }
                }
            }
            foreach ($avg as $oc => $scs) {
                $gb[$ln][2][$i][0][$oc] = array_sum($avg[$oc]) / count($avg[$oc]);
                $gb[$ln][2][$i][1][$oc] = array_sum($avgposs[$oc]) / count($avg[$oc]);
            }
            foreach ($avgatt as $oc => $scs) {
                $gb[$ln][2][$i][2][$oc] = array_sum($avgatt[$oc]) / count($avgatt[$oc]);
                $gb[$ln][2][$i][3][$oc] = array_sum($avgattposs[$oc]) / count($avgatt[$oc]);
            }
        }
        $avg = array();
        $avgatt = array();
        for ($j = 1; $j < $ln; $j++) {
            if (isset($gb[$j][3][0])) {
                foreach ($gb[$j][3][0] as $oc => $sc) {
                    if (!isset($avg[$oc])) {
                        $avg[$oc] = array();
                    }
                    $avg[$oc][] = $sc;
                }
            }
            if (isset($gb[$j][3][1])) {
                foreach ($gb[$j][3][1] as $oc => $sc) {
                    if (!isset($avgatt[$oc])) {
                        $avgatt[$oc] = array();
                    }
                    $avgatt[$oc][] = $sc;
                }
            }
        }
        foreach ($avg as $oc => $scs) {
            $gb[$ln][3][0][$oc] = array_sum($avg[$oc]) / count($avg[$oc]);
        }
        foreach ($avgatt as $oc => $scs) {
            $gb[$ln][3][1][$oc] = array_sum($avgatt[$oc]) / count($avgatt[$oc]);
        }
    }
    if ($limuser == -1) {
        $gb[1] = $gb[$ln];
    }
    return $gb;
}