include('open_db.php'); mysql_set_charset('utf8'); extract ($_GET); // id = stem item ID // type = item stem type // '' = random (default) // 0 = A is caused by ... // 1 = A is caused by all EXCEPT ... // 2 = B can cause which one ... // 3 = B causes all EXCEPT ... // num = number of options (3 - 5; default = 4) global $item; if ($type == '') $type = time() % 2; // (set $type to 0 or 1) $num = 4; if ($type < 2) $result = quiz_gamut ($id, $type, $num - 1); else $result = quiz_cause ($id, $type, $num - 1); // Question types 0 and 1 function quiz_gamut ($id, $type, $numDistractors) { $gamutSQL = ( $id == '' ? "1" : mysql_real_escape_string ("entity2 = $id")); // Select a gamut with at least 5 causes (using $id if given) $result = mysql_query ( "SELECT entity2 AS gamut, Entity.name AS gamutName, topic, count(*) AS numCauses FROM List, Entity LEFT JOIN Source ON Entity.sourceID = Source.id WHERE $gamutSQL AND rel = 'C' AND entity2 = Entity.id AND Entity.ok = 1 AND Entity.sourceID = Source.id AND (Entity.sourceID IS NULL OR Source.approved IS NOT NULL) GROUP BY entity2 HAVING numCauses > 4 ORDER BY rand() LIMIT 1"); $row = mysql_fetch_assoc ($result); extract ($row); $GamutName = ucfirst ($gamutName); //get specialties for selected gamut $result = mysql_query ("SELECT specialtyCode FROM SpecialtyRef WHERE entityID = $gamut"); //output specialties for selected gamut to a variable while ($row = mysql_fetch_assoc ($result)) { extract ($row); $listSpecialties .= "'$specialtyCode', "; } $listSpecialties=rtrim($listSpecialties, ", "); // TYPE 0 -- Identify the single cause of gamut if ($type == 0) { // Specify the question stem $question = "Which of the following can cause $gamutName?"; // Specify the SQL queries for the answer and distractors $distractors = "SELECT DISTINCT Candidate.cause, Candidate.name, 0 AS answer FROM (SELECT DISTINCT List.entity1 AS cause, E1.name FROM List, Entity AS E1, Entity AS E2, Source WHERE rel = 'C' AND E1.id = entity1 AND E2.id = entity2 AND E2.sourceID = Source.id AND topic = '$topic') AS Candidate LEFT JOIN (SELECT cause FROM ExpandedCause WHERE effect = $gamut) AS ExpCause USING ( cause ) WHERE ExpCause.cause IS NULL ORDER BY rand() LIMIT $numDistractors"; $answer = "SELECT DISTINCT entity1 AS cause, E1.name, 1 AS answer FROM List, Entity as E1, Entity as E2 LEFT JOIN Source ON E2.sourceID = Source.id WHERE rel = 'C' AND E1.id = entity1 AND E2.id = entity2 AND E2.sourceID = Source.id AND (E2.sourceID IS NULL OR Source.approved IS NOT NULL) AND entity2 = $gamut ORDER BY rand() LIMIT 1"; } // TYPE 1 -- Identify the one NON-cause of the gamut else { // type == 1 // Specify the question stem $question = "Which of the following does NOT cause $gamutName?"; // Specify the SQL queries for the answer and distractors $distractors = "SELECT DISTINCT entity1 AS cause, E1.name, 0 AS answer FROM List, Entity as E1, Entity as E2, Source WHERE rel = 'C' AND E1.id = entity1 AND E2.id = entity2 AND E2.sourceID = Source.id AND entity2 = $gamut ORDER BY rand() LIMIT $numDistractors"; $answer = "SELECT DISTINCT Candidate.cause, Candidate.name, 1 AS answer FROM (SELECT DISTINCT List.entity1 AS cause, E1.name FROM List, Entity AS E1, Entity AS E2, Source WHERE rel = 'C' AND E1.id = entity1 AND E2.id = entity2 AND E2.sourceID = Source.id AND (E2.sourceID IS NULL OR Source.approved IS NOT NULL) AND topic = '$topic') AS Candidate LEFT JOIN (SELECT cause FROM ExpandedCause WHERE effect = $gamut) AS ExpCause USING ( cause ) WHERE ExpCause.cause IS NULL ORDER BY rand() LIMIT 1"; } // Compose overall query $result = mysql_query ( "SELECT cause, name, answer FROM ( ($distractors) UNION ($answer) ) AS Options ORDER BY rand() "); // Display the stem stem ($gamut, $gamutName, $question, $type, $topic); // Display the options while ($row = mysql_fetch_assoc ($result)) { extract ($row); $name = ucfirst ($name); option ($cause, $name, $answer); } } function quiz_cause ($id, $type, $numDistractors) { $gamutSQL = ( $id == '' ? "1" : mysql_real_escape_string ("entity2 = $id")); // Randomly select an entity that appears in at least 5 gamuts $result = mysql_query ( "SELECT entity1 AS cause, Entity.name AS causeName, topic, count(*) AS numCauses FROM List, Entity, Source WHERE $causeSQL AND rel = 'C' AND entity1 = Entity.id AND Entity.sourceID = Source.id GROUP BY entity1 HAVING numCauses > 4 ORDER BY rand() LIMIT 1"); $row = mysql_fetch_assoc ($result); extract ($row); $CauseName = ucfirst ($causeName); if ($type == 2) { $question = "$CauseName does NOT cause:"; } else { $question = "Which of the following is caused by $causeName?"; } stem ($cause, $causeName, $question, $stemType, $topic); // Select answer and distractors $result = mysql_query ( "SELECT gamut, name, answer FROM ((SELECT DISTINCT entity2 AS gamut, E2.name, 0 AS answer FROM List, Entity as E1, Entity as E2, Source WHERE rel = 'C' AND E1.id = entity1 AND E2.id = entity2 AND E2.sourceID = Source.id AND $wrong ORDER BY rand() LIMIT $numDistractors) UNION (SELECT entity2 AS gamut, E2.name, 1 AS answer FROM List, Entity as E1, Entity as E2, Source WHERE rel = 'C' AND E1.id = entity1 AND E2.id = entity2 AND E2.sourceID = Source.id AND $right GROUP BY entity2 ORDER BY rand() LIMIT 1)) AS Options ORDER BY rand() "); //$countRecords = mysql_num_rows($result); //echo "
count: $countRecords
"; while ($row = mysql_fetch_assoc ($result)) { extract ($row); option ($gamut, ucfirst ($name), $answer); //echo "$gamut :: $name :: $answer
"; } } function stem ($id, $name, $question, $stemType, $topic) { global $item; $item ['stem'] = array ( 'id' => $id, 'name' => $name, 'question' => $question, 'type' => $stemType, 'topic' => $topic ); } function option ($id, $name, $answer) { global $item; $item ['option'] [] = array ( 'id' => $id, 'name' => $name, 'answer' => $answer ); //echo "$id :: $name :: $answer
"; } ?>
Gamuts Quiz Questionecho $item[stem][question]; ?> |