File: multiple.php

package info (click to toggle)
moodle 1.4.4.dfsg.1-3sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 57,876 kB
  • ctags: 29,496
  • sloc: php: 271,617; sql: 5,084; xml: 702; perl: 638; sh: 403; java: 283; makefile: 42; pascal: 21
file content (162 lines) | stat: -rw-r--r-- 5,719 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?PHP // $Id: multiple.php,v 1.9 2004/08/22 14:38:44 gustav_delius Exp $
      // A quick way to add lots of questions to a category (and a quiz)

    require_once("../../config.php");
    require_once("lib.php");

    require_variable($category);

    // This script can only be called while editing a quiz

    if (!isset($SESSION->modform)) {
        error("You have used this page incorrectly!");
    } else {
        $modform = $SESSION->modform;
    }

    if (! $category = get_record("quiz_categories", "id", $category)) {
        error("Course ID is incorrect");
    }

    if (! $course = get_record("course", "id", $category->course)) {
        error("Course ID is incorrect");
    }

    require_login($course->id);

    if (!isteacher($course->id)) {
        error("Only teachers can use this page!");
    }



/// If data submitted, then process and store.

    if ($form = data_submitted()) {
        if ($form->randomcreate > 0) {
            $existing = count_records("quiz_questions", "qtype", RANDOM, "category", $category->id);
            $randomcreate = $form->randomcreate - $existing;

            if ($randomcreate > 0) {
                $newquestionids = array();

                $question->qtype = RANDOM;
                $question->category = $category->id;
                $question->name = get_string("random", "quiz")." (".$category->name.")";
                $question->questiontext = "---";
                $question->image = "";
                $question->defaultgrade = $form->randomgrade;
                for ($i=0; $i<$randomcreate; $i++) {
                    $question->stamp = make_unique_id_code();  // Set the unique code (not to be changed)
                    if (!$newquestionids[] = insert_record("quiz_questions", $question)) {
                        error("Could not insert new random question!");
                    }
                }

                // Add them to the quiz if necessary
                if (!empty($form->addquestionstoquiz)) {
                    if (!empty($modform->questions)) {
                        $questionids = explode(",", $modform->questions);
                        foreach ($questionids as $questionid) {
                            foreach ($newquestionids as $key => $newquestionid) {
                                if ($newquestionid == $questionid) {
                                    unset($newquestionids[$key]);
                                    break;
                                }
                            }
                        }
                    } else {
                        $questionids = array();
                    }

                    foreach ($newquestionids as $newquestionid) {
                        $modform->grades[$newquestionid] = $form->randomgrade;
                        $modform->sumgrades += $form->randomgrade;
                    }

                    $newquestionids = array_merge($questionids, $newquestionids);
                    $modform->questions = implode(",", $newquestionids);
                    $SESSION->modform = $modform;
                }
            }
        }
        redirect("edit.php");
    }


/// Otherwise print the form

/// Print headings

    $strquestions = get_string("questions", "quiz");
    $strpublish = get_string("publish", "quiz");
    $strdelete = get_string("delete");
    $straction = get_string("action");
    $stradd = get_string("add");
    $strcancel = get_string("cancel");
    $strsavechanges = get_string("savechanges");
    $strbacktoquiz = get_string("backtoquiz", "quiz");
    $strquizzes = get_string('modulenameplural', 'quiz');
    $streditingquiz = get_string(isset($SESSION->modform->instance) ? "editingquiz" : "editquestions", "quiz");
    $strcreatemultiple = get_string("createmultiple", "quiz");

    print_header_simple("$strcreatemultiple", "$strcreatemultiple",
                 "<a href=\"$CFG->wwwroot/mod/quiz/index.php?id=$course->id\">$strquizzes</a>".
                 " -> <a href=\"edit.php\">$streditingquiz</a> -> $strcreatemultiple");


    print_heading_with_help($strcreatemultiple, "createmultiple", "quiz");

    if (!$categories = quiz_get_category_menu($course->id, true)) {
        error("No categories!");
    }

    for ($i=1;$i<=100; $i++) {
        $randomcount[$i] = $i;
    }
    for ($i=1;$i<=10; $i++) {
        $gradecount[$i] = $i;
    }
    $options = array();
    $options[0] = get_string("no");
    $options[1] = get_string("yes");

    print_simple_box_start("center", "", "$THEME->cellheading");
    echo "<FORM METHOD=\"POST\" ACTION=multiple.php>";
    echo "<TABLE cellpadding=5>";
    echo "<TR><TD align=right>";
    print_string("category", "quiz");
    echo ":</TD><TD>";
    // choose_from_menu($categories, "category", "$category->id", "");
    quiz_category_select_menu($course->id, true, true, $category->id );
    echo "</TR>";

    echo "<TR><TD align=right>";
    print_string("randomcreate", "quiz");
    echo ":</TD><TD>";
    choose_from_menu($randomcount, "randomcreate", "10", "");
    echo "</TR>";

    echo "<TR><TD align=right>";
    print_string("defaultgrade", "quiz");
    echo ":</TD><TD>";
    choose_from_menu($gradecount, "randomgrade", "1", "");
    echo "</TR>";

    echo "<TR><TD align=right>";
    print_string("addquestionstoquiz", "quiz");
    echo ":</TD><TD>";
    choose_from_menu($options, "addquestionstoquiz", "1", "");
    echo "</TR>";

    echo "<TR><TD>&nbsp;</TD><TD>";
    echo " <INPUT TYPE=hidden NAME=category VALUE=\"$category->id\">";
    echo " <INPUT TYPE=submit NAME=save VALUE=\"$strcreatemultiple\">";
    echo "</TD></TR>";
    echo "</TABLE>";
    echo "</FORM>";
    print_simple_box_end();

    print_footer();

?>