//exit; anchorRedirect($anchor, $tab); } else { if ($action == "addtogroup") { $anchor = rt("anchor"); $tab = rt("tab"); $gid = rt("gid"); $students = $_GET["student"]; $sql = "select count(*) from users where active=1 and teacher_code={$teacher_code} and group_id={$gid}"; $rs = dbExec($sql); $spaceleft = 4 - $rs->fields(0); $numprovided = count($students); $n = $spaceleft; dbg("spaceleft={$spaceleft}"); if ($spaceleft < 1) { msgRedirectRed("No students added - TEAM FULL!", $anchor); } dbg("numprovided={$numprovided}"); dbg("n={$n}"); // Add students to the group $added = 0; foreach ($students as $s) { $sql = "update users set group_id={$gid} where active=1 and id={$s} and teacher_code={$teacher_code} and group_id=0"; $rs = dbExec($sql); logit("student {$id} added to group {$gid} by teacher {$teacher_code}"); $added++; $n--; if ($n == 0) { break; } }
function make_a_group($teacher_code) { global $db; // make sure this teacher is below their quota of groups $sql = "select count(*) from groups where teacher_code={$teacher_code}"; $rs = $db->Execute($sql); if (!$rs) { p_sqlfail($db, $sql); } $maxgrps = $db->GetOne("select val from config where tag='max_groups_per_teacher'"); if ($rs->fields(0) >= $maxgrps) { msgRedirectRed("group quota for teacher-{$teacher_code} maxed out"); } $n = 0; while (true) { if ($n > 10) { dbg("ACK!! {$id} {$tc}"); exit; } // look for a free group slot $sql = "select * from groups where teacher_code=0 and grab=0 order by rand() limit 1"; $rs = $db->Execute($sql); if (!$rs) { p_sqlfail($db, $sql); } if ($rs->RecordCount() == 0) { msgRedirectRed("no available group slots. Please try again later."); } // attempt to increment grab by 1 $id = $rs->fields("id"); $g = $rs->fields("grab"); $sql = "update groups set grab=grab+1 where id={$id}"; $rs = $db->Execute($sql); if (!$rs) { p_sqlfail($db, $sql); } dbg("grab count incremented"); // check to see if grab has in fact only grown by 1 $sql = "select grab from groups where id={$id}"; $rs = $db->Execute($sql); if (!$rs) { p_sqlfail($db, $sql); } if ($rs->fields("grab") == $g + 1) { // we won the grab! dbg("grab successful!"); break; } // someone else tried to grab at the same time as us. // decrement my grab and try again ... dbg("grab failed"); $sql = "update groups set grab=grab-1 where id={$id}"; $rs = $db->Execute($sql); dbg("grab count decremented"); logit("grab failed (id={$id})"); $n++; } // Store my info into the group $name = "DEVGROUP_" . $teacher_code . "_{$id}"; $sql = "update groups set teacher_code={$teacher_code}, name='{$name}' where id={$id}"; $rs = $db->Execute($sql); if (!$rs) { p_sqlfail($db, $sql); } logit("group {$id} acquired by DEV for teacher {$teacher_code}"); dbg("group created: {$id}"); return $id; }