Many new additions - code not quite usable yet, but getting close

This commit is contained in:
moodler 2002-10-14 09:07:13 +00:00
parent 2eaa2e2861
commit 6a952ce7a0
5 changed files with 360 additions and 4 deletions

View file

@ -1,3 +1,4 @@
<?PHP // $Id
<?PHP // $Id$
echo "not done yet";
?>

View file

@ -1,3 +1,166 @@
<?PHP // $Id
<?PHP // $Id$
require("../../config.php");
require("lib.php");
require_login();
if (match_referer($destination) && isset($course) && isset($HTTP_POST_VARS)) { // form submitted from mod.html
$modform = (object)$HTTP_POST_VARS;
if (!$modform->name or !$modform->intro) {
error(get_string("filloutallfields"), $HTTP_REFERER);
}
$SESSION->modform = $modform; // Save the form in the current session
save_session("SESSION");
} else {
if (!isset($SESSION->modform)) {
error("You have used this page incorrectly!");
}
$modform = $SESSION->modform;
}
if (! $course = get_record("course", "id", $modform->course)) {
error("This course doesn't exist");
}
require_login($course->id);
if (!isteacher($course->id)) {
error("You can't modify this course!");
}
// Now, check for commands on this page and modify variables as necessary
if ($up) { //------------------------------------------------------------
$questions = explode(",", $modform->questions);
if ($questions[0] <> $up) {
foreach ($questions as $key => $question) {
if ($up == $question) {
$swap = $questions[$key-1];
$questions[$key-1] = $question;
$questions[$key] = $swap;
break;
}
}
$modform->questions = implode(",", $questions);
}
}
if ($down) { //----------------------------------------------------------
$questions = explode(",", $modform->questions);
if ($questions[count($questions)-1] <> $down) {
foreach ($questions as $key => $question) {
if ($down == $question) {
$swap = $questions[$key+1];
$questions[$key+1] = $question;
$questions[$key] = $swap;
break;
}
}
$modform->questions = implode(",", $questions);
}
}
if ($add) { //-----------------------------------------------------------
$rawquestions = $HTTP_POST_VARS;
$questions = explode(",", $modform->questions);
foreach ($rawquestions as $key => $value) { // Parse input for question ids
if (substr($key, 0, 1) == "q") {
$key = substr($key,1);
foreach ($questions as $question) {
if ($question == $key) {
continue 2;
}
}
$questions[] = $key;
$newgrade->quiz = $quiz->id;
}
}
$modform->questions = implode(",", $questions);
}
if ($delete) { //--------------------------------------------------------
$questions = explode(",", $modform->questions);
foreach ($questions as $key => $question) {
if ($question == $delete) {
unset($questions[$key]);
$db->debug=true;
execute_sql("DELETE FROM quiz_question_grades WHERE quiz='$quiz->id' and question='$question'");
$db->debug=false;
}
}
$modform->questions = implode(",", $questions);
}
if ($grade) { //---------------------------------------------------------
$rawgrades = $HTTP_POST_VARS;
foreach ($rawgrades as $key => $value) { // Parse input for question -> grades
if (substr($key, 0, 1) == "q") {
$key = substr($key,1);
set_field("quiz_question_grades", "grade", $value, "id", $key);
}
}
}
if ($cat) { //-----------------------------------------------------------
if ($catshow) {
$modform->category = $cat;
} else if ($catrename) {
redirect("category.php?rename=$cat");
} else if ($catdelete) {
redirect("category.php?delete=$cat");
} else if ($catnew) {
redirect("category.php?new=$cat");
}
}
$SESSION->modform = $modform;
save_session("SESSION");
$strediting = get_string("editingquiz", "quiz");
$strname = get_string("name");
print_header("$course->shortname: $strediting", "$course->shortname: $strediting",
"<A HREF=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</A> -> $strediting");
// Print basic page layout.
echo "<TABLE BORDER=0 WIDTH=\"100%\" CELLPADDING=2 CELLSPACING=0>";
echo "<TR><TD WIDTH=50%>";
print_simple_box_start("CENTER", "100%", $THEME->body);
print_heading($modform->name);
quiz_print_question_list($modform->questions);
?>
<CENTER>
<P>&nbsp;</P>
<FORM NAME=theform METHOD=post ACTION=<?=$modform->destination ?>>
<INPUT TYPE="hidden" NAME=course VALUE="<? p($modform->course) ?>">
<INPUT TYPE="submit" VALUE="<? print_string("savequiz", "quiz") ?>">
</FORM>
</CENTER>
<?
print_simple_box_end();
echo "</TD><TD VALIGN=top WIDTH=50%>";
print_simple_box_start("CENTER", "100%", $THEME->body);
quiz_print_category_form($course, $modform->category);
print_simple_box_end();
print_spacer(5,1);
print_simple_box_start("CENTER", "100%", $THEME->body);
quiz_print_cat_question_list($modform->category);
print_simple_box_end();
echo "</TD></TR>";
echo "</TABLE>";
print_footer($course);
?>

View file

@ -232,6 +232,195 @@ function quiz_print_quiz_questions($quiz, $results=NULL) {
echo "</FORM>";
}
function quiz_get_default_category($courseid) {
if ($categories = get_records("quiz_categories", "course", $courseid, "id")) {
foreach ($categories as $category) {
return $category; // Return the first one (lowest id)
}
}
// Otherwise, we need to make one
$category->name = get_string("miscellaneous", "quiz");
$category->info = get_string("miscellaneous", "quiz");
$category->course = $courseid;
$category->publish = 0;
if (!$category->id = insert_record("quiz_categories", $category)) {
notify("Error creating a default category!");
return false;
}
return $category;
}
function quiz_print_category_form($course, $current) {
// Prints a form to choose categories
if (!$categories = get_records_sql_menu("SELECT id,name FROM quiz_categories
WHERE course='$course->id' OR publish = '1'
ORDER by name ASC")) {
if (!$category = quiz_get_default_category($course->id)) {
notify("Error creating a default category!");
return false;
}
$categories[$category->id] = $category->name;
}
$strcategory = get_string("category", "quiz");
$strshow = get_string("show", "quiz");
$strrename = get_string("rename", "quiz");
$strdelete = get_string("delete");
$strnew = get_string("new");
echo "<FORM METHOD=POST ACTION=edit.php>";
echo "<B>$strcategory:</B>&nbsp;";
choose_from_menu($categories, "cat", "$current");
echo "<INPUT TYPE=submit NAME=catshow VALUE=\"$strshow\">";
echo "<INPUT TYPE=submit NAME=catrename VALUE=\"$strrename\">";
echo "<INPUT TYPE=submit NAME=catdelete VALUE=\"$strdelete\">";
echo "<INPUT TYPE=submit NAME=catnew VALUE=\"$strnew\">";
echo "</FORM>";
}
function quiz_print_question_list($questionlist) {
// Prints a list of quiz questions in a small layout form with knobs
global $THEME;
if (!$questionlist) {
echo "<P align=center>";
print_string("noquestions", "quiz");
echo "</P>";
return;
}
$order = explode(",", $questionlist);
if (!$questions = get_records_sql("SELECT q.*, qg.grade FROM quiz_questions q, quiz_question_grades qg
WHERE q.id in ($questionlist) and qg.question = q.id")) {
error("No questions were found!");
}
$strorder = get_string("order");
$strquestionname = get_string("questionname", "quiz");
$strgrade = get_string("grade");
$strdelete = get_string("delete");
$stredit = get_string("edit");
$strmoveup = get_string("moveup");
$strmovedown = get_string("movedown");
$strsavegrades = get_string("savegrades", "quiz");
for ($i=100; $i>=0; $i--) {
$grades[$i] = $i;
}
$count = 0;
$sumgrade = 0;
$total = count($order);
echo "<FORM METHOD=post ACTION=edit.php>";
echo "<TABLE BORDER=0 CELLPADDING=5 CELLSPACING=2 WIDTH=\"100%\">";
echo "<TR><TH COLSPAN=3>$strorder</TH><TH align=left>$strquestionname</TH><TH>$strgrade</TH><TH>$stredit</TH></TR>";
foreach ($order as $qnum) {
$count++;
echo "<TR BGCOLOR=\"$THEME->cellcontent\">";
echo "<TD>$count</TD>";
echo "<TD>";
if ($count != 1) {
echo "<A TITLE=\"$strmoveup\" HREF=\"edit.php?up=$qnum\"><IMG
SRC=\"../../pix/t/up.gif\" BORDER=0></A>";
}
echo "</TD>";
echo "<TD>";
if ($count != $total) {
echo "<A TITLE=\"$strmovedown\" HREF=\"edit.php?down=$qnum\"><IMG
SRC=\"../../pix/t/down.gif\" BORDER=0></A>";
}
echo "</TD>";
echo "<TD>".$questions[$qnum]->name."</TD>";
echo "<TD>";
choose_from_menu($grades, "q$qnum", $questions[$qnum]->grade, "");
echo "<TD>";
echo "<A TITLE=\"$strdelete\" HREF=\"edit.php?delete=$qnum\"><IMG
SRC=\"../../pix/t/delete.gif\" BORDER=0></A>&nbsp;";
echo "<A TITLE=\"$stredit\" HREF=\"question.php?id=$qnum\"><IMG
SRC=\"../../pix/t/edit.gif\" BORDER=0></A>";
echo "</TD>";
$sumgrade += $questions[$qnum]->grade;
}
echo "<TR><TD COLSPAN=3><TD ALIGN=right>";
echo "<INPUT TYPE=submit VALUE=\"$strsavegrades:\">";
echo "<INPUT TYPE=hidden NAME=grade VALUE=\"save\">";
echo "<TD ALIGN=LEFT BGCOLOR=\"$THEME->cellcontent\">";
echo "<B>$sumgrade</B>";
echo "</TD><TD></TD></TR>";
echo "</TABLE>";
echo "</FORM>";
}
function quiz_print_cat_question_list($categoryid) {
// Prints a form to choose categories
global $THEME, $QUIZ_QUESTION_TYPE;
$strquestion = get_string("question", "quiz");
$strnoquestions = get_string("noquestions", "quiz");
$strselect = get_string("select", "quiz");
$strcreatenewquestion = get_string("createnewquestion", "quiz");
$strquestionname = get_string("questionname", "quiz");
$strdelete = get_string("delete");
$stredit = get_string("edit");
$straddselectedtoquiz = get_string("addselectedtoquiz", "quiz");
if (!$categoryid) {
echo "<P align=center>";
print_string("selectcategoryabove", "quiz");
echo "</P>";
return;
}
if (!$category = get_record("quiz_categories", "id", "$categoryid")) {
notify("Category not found!");
return;
}
echo "<P>$category->info</P>";
echo "<FORM METHOD=POST ACTION=question.php>";
echo "<B>$strquestion:</B>&nbsp;";
choose_from_menu($QUIZ_QUESTION_TYPE, "type", "", "");
echo "<INPUT TYPE=hidden NAME=category VALUE=\"$category->id\">";
echo "<INPUT TYPE=submit NAME=new VALUE=\"$strcreatenewquestion\">";
echo "</FORM>";
if (!$questions = get_records("quiz_questions", "category", $category->id)) {
echo "<P align=center>";
print_string("noquestions", "quiz");
echo "</P>";
return;
}
echo "<FORM METHOD=post ACTION=edit.php>";
echo "<TABLE BORDER=0 CELLPADDING=5 CELLSPACING=2 WIDTH=\"100%\">";
echo "<TR><TH width=10>$strselect</TH><TH width=* align=left>$strquestionname</TH><TH width=10>$stredit</TH></TR>";
foreach ($questions as $question) {
echo "<TR BGCOLOR=\"$THEME->cellcontent\">";
echo "<TD ALIGN=CENTER>";
echo "<INPUT TYPE=CHECKBOX NAME=q$question->id VALUE=\"1\">";
echo "</TD>";
echo "<TD>".$question->name."</TD>";
echo "<TD>";
echo "<A TITLE=\"$strdelete\" HREF=\"question.php?delete=$question->id\"><IMG
SRC=\"../../pix/t/delete.gif\" BORDER=0></A>&nbsp;";
echo "<A TITLE=\"$stredit\" HREF=\"question.php?id=$question->id\"><IMG
SRC=\"../../pix/t/edit.gif\" BORDER=0></A>";
echo "</TD></TR>";
}
echo "<TR><TD COLSPAN=3>";
echo "<INPUT TYPE=hidden NAME=add VALUE=\"1\">";
echo "<INPUT TYPE=submit VALUE=\"<< $straddselectedtoquiz\">";
echo "</TD></TR>";
echo "</TABLE>";
echo "</FORM>";
}
function quiz_get_user_attempts($quizid, $userid) {

View file

@ -4,7 +4,8 @@
<? include_once("$CFG->dirroot/mod/quiz/lib.php") ?>
<H1 ALIGN=CENTER><FONT COLOR=RED>This module is not ready for use</FONT></H1>
<FORM name="form" method="post" action="<?=$ME ?>">
<FORM name="form" method="post" action="<?=$CFG->wwwroot?>/mod/quiz/edit.php">
<CENTER>
<TABLE cellpadding=5>
<TR valign=top>
@ -102,6 +103,7 @@
</TD>
</TR>
</TABLE>
<INPUT type="hidden" name=questions value="<? p($form->questions) ?>">
<!-- These hidden variables are always the same -->
<INPUT type="hidden" name=course value="<? p($form->course) ?>">
<INPUT type="hidden" name=coursemodule value="<? p($form->coursemodule) ?>">
@ -110,6 +112,7 @@
<INPUT type="hidden" name=modulename value="<? p($form->modulename) ?>">
<INPUT type="hidden" name=instance value="<? p($form->instance) ?>">
<INPUT type="hidden" name=mode value="<? p($form->mode) ?>">
<input type="hidden" name=destination value="<?=$ME ?>">
<INPUT type="submit" value="<? print_string("savechanges") ?>">
</CENTER>
</FORM>

View file

@ -1,3 +1,3 @@
<?PHP // $Id
<?PHP // $Id$
?>