Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

took out some debugging messages.

  • Loading branch information...
commit 108ef2026072f90125f6761b55c30ea80393045c 1 parent 7386d3d
jungwirr authored

Showing 1 changed file with 446 additions and 448 deletions. Show diff stats Hide diff stats

  1. +446 448 mod/quiz/format/coursetestmanager.php
894 mod/quiz/format/coursetestmanager.php
@@ -2,257 +2,255 @@
2 2 ////////////////////////////////////////////////////////////////////
3 3 /// Class for importing course test manager questions. //
4 4 /// //
5   -/// Russell Jungwirth //
  5 +/// //
6 6 ////////////////////////////////////////////////////////////////////
7 7 // Included by ../import.php
8   -
9   -
10 8 class quiz_file_format {
11 9 function importpreprocess($category) {
12 10 $this->category = $category; // Important
13 11 return true;
14 12 }
15 13 function importprocess($filename) {
16   - global $CFG,$QUIZ_FILE_FORMAT,$strimportquestions,$form,$question_category,$category,$course,$THEME,
17   - $hostname, $mdapath, $mdbpath;
18   - if ((PHP_OS == "Linux") and isset($hostname)) {
19   - $hostname = trim($hostname);
20   - // test the ODBC socket server connection
21   - // if failure, unset hostname and set hostname_access_error
22   - $question_categories = $this->getquestioncategories($mdbpath, $mdapath, $hostname);
23   - if (!$question_categories) {
24   - $hostname_access_error = $hostname . " ";
25   - unset($hostname);
26   - } else {
27   - $hostname_access_error = 0;
28   - }
  14 + global $CFG,$QUIZ_FILE_FORMAT,$strimportquestions,$form,$question_category,$category,$course,$THEME,
  15 + $hostname, $mdapath, $mdbpath;
  16 + if ((PHP_OS == "Linux") and isset($hostname)) {
  17 + $hostname = trim($hostname);
  18 + // test the ODBC socket server connection
  19 + // if failure, unset hostname and set hostname_access_error
  20 + $question_categories = $this->getquestioncategories($mdbpath, $mdapath, $hostname);
  21 + if (!$question_categories) {
  22 + $hostname_access_error = $hostname . " ";
  23 + unset($hostname);
  24 + } else {
  25 + $hostname_access_error = 0;
29 26 }
  27 + }
30 28
31   - if ((PHP_OS == "Linux") and !isset($hostname)) {
32   - // copy the file to a semi-permanent location
33   - if (! $basedir = make_upload_directory("$course->id")) {
34   - error("The site administrator needs to fix the file permissions for the data directory");
35   - }
36   - if (!isset($hostname_access_error)) {
37   - $bname=basename($filename);
38   - $cleanfilename = clean_filename($bname);
39   - if ($cleanfilename) {
40   - $newfile = "$basedir/$cleanfilename";
41   - if (move_uploaded_file($filename, $newfile)) {
42   - chmod($newfile, 0666);
43   - } else {
44   - notify(get_string("uploadproblem", "", $filename));
45   - }
46   - }
47   - $filename = $newfile;
48   - }
49   - print_heading_with_help($strimportquestions, "import", "quiz");
50   - print_simple_box_start("center", "", "$THEME->cellheading");
51   - if ($hostname_access_error) { notify("couldn't connect to ODBC Socket Server on " . $hostname_access_error); }
52   - echo "<form method=\"post\" action=\"import.php\">";
53   - echo "<table cellpadding=5>";
54   -
55   - echo "<tr><td align=right>";
56   - echo "What is the hostname or IP address of the ODBC Socket Server:</td><td>";
57   - echo " <input name=\"hostname\" type=\"text\" size=\"50\" value=\"".stripslashes($hostname_access_error)."\">";
58   - echo " <input name=\"filename\" type=\"hidden\" value=\"".$filename."\">";
59   - echo " <input name=\"category\" type=\"hidden\" value=\"".$category->id."\">";
60   - echo " <input name=\"format\" type=\"hidden\" value=\"".$form->format."\">";
61   - echo "</td><td>&nbsp;</td></tr>";
62   - echo "<tr><td align=right>";
63   - echo "What is the location of the database (.mdb file) on the Socket Server:</td><td>";
64   - echo " <input name=\"mdbpath\" type=\"text\" size=\"50\" value=\"".stripslashes($mdbpath)."\">";
65   - echo "</td><td>&nbsp;</td></tr>";
66   - echo "<tr><td align=right>";
67   - echo "What is the location of the system database (System.mda file) on the Socket Server:</td><td>";
68   - echo " <input name=\"mdapath\" type=\"text\" size=\"50\" value=\"".stripslashes($mdapath)."\">";
69   - echo "</td><td>&nbsp;</td></tr>";
70   - echo "<tr><td>&nbsp;</td><td>";
71   - echo " <input type=submit name=save value=\"Connect to Server\">";
72   - echo "</td></tr>";
73   - echo "</table>";
74   - echo "</form>";
75   - print_simple_box_end();
76   - print_footer($course);
77   - exit;
78   - }
  29 + if ((PHP_OS == "Linux") and !isset($hostname)) {
  30 + // copy the file to a semi-permanent location
  31 + if (! $basedir = make_upload_directory("$course->id")) {
  32 + error("The site administrator needs to fix the file permissions for the data directory");
  33 + }
  34 + if (!isset($hostname_access_error)) {
  35 + $bname=basename($filename);
  36 + $cleanfilename = clean_filename($bname);
  37 + if ($cleanfilename) {
  38 + $newfile = "$basedir/$cleanfilename";
  39 + if (move_uploaded_file($filename, $newfile)) {
  40 + chmod($newfile, 0666);
  41 + } else {
  42 + notify(get_string("uploadproblem", "", $filename));
  43 + }
  44 + }
  45 + $filename = $newfile;
  46 + }
  47 + print_heading_with_help($strimportquestions, "import", "quiz");
  48 + print_simple_box_start("center", "", "$THEME->cellheading");
  49 + if ($hostname_access_error) { notify("couldn't connect to ODBC Socket Server on " . $hostname_access_error); }
  50 + echo "<form method=\"post\" action=\"import.php\">";
  51 + echo "<table cellpadding=5>";
79 52
80   - // we get here if running windows or after connect to ODBC socket server on linux
81   - //
82   - // this generates the page to choose categories of questions to import
83   - //
84   - if (!isset($question_category)) {
  53 + echo "<tr><td align=right>";
  54 + echo "What is the hostname or IP address of the ODBC Socket Server:</td><td>";
  55 + echo " <input name=\"hostname\" type=\"text\" size=\"50\" value=\"".stripslashes($hostname_access_error)."\">";
  56 + echo " <input name=\"filename\" type=\"hidden\" value=\"".$filename."\">";
  57 + echo " <input name=\"category\" type=\"hidden\" value=\"".$category->id."\">";
  58 + echo " <input name=\"format\" type=\"hidden\" value=\"".$form->format."\">";
  59 + echo "</td><td>&nbsp;</td></tr>";
  60 + echo "<tr><td align=right>";
  61 + echo "What is the location of the database (.mdb file) on the Socket Server:</td><td>";
  62 + echo " <input name=\"mdbpath\" type=\"text\" size=\"50\" value=\"".stripslashes($mdbpath)."\">";
  63 + echo "</td><td>&nbsp;</td></tr>";
  64 + echo "<tr><td align=right>";
  65 + echo "What is the location of the system database (System.mda file) on the Socket Server:</td><td>";
  66 + echo " <input name=\"mdapath\" type=\"text\" size=\"50\" value=\"".stripslashes($mdapath)."\">";
  67 + echo "</td><td>&nbsp;</td></tr>";
  68 + echo "<tr><td>&nbsp;</td><td>";
  69 + echo " <input type=submit name=save value=\"Connect to Server\">";
  70 + echo "</td></tr>";
  71 + echo "</table>";
  72 + echo "</form>";
  73 + print_simple_box_end();
  74 + print_footer($course);
  75 + exit;
  76 + }
  77 +
  78 + // we get here if running windows or after connect to ODBC socket server on linux
  79 +//
  80 +// this generates the page to choose categories of questions to import
  81 +//
  82 + if (!isset($question_category)) {
85 83
86   - if (PHP_OS == "WINNT") {
87   - // copy the file to a semi-permanent location
88   - if (! $basedir = make_upload_directory("$course->id")) {
89   - error("The site administrator needs to fix the file permissions for the data directory");
90   - }
91   - $bname=basename($filename);
92   - $cleanfilename = clean_filename($bname);
93   - if ($cleanfilename) {
94   - $newfile = "$basedir/$cleanfilename";
95   - if (move_uploaded_file($filename, $newfile)) {
96   - chmod($newfile, 0666);
97   - } else {
98   - notify(get_string("uploadproblem", "", $filename));
99   - }
100   - }
101   - $filename = $newfile;
102   - }
103   - // end of file copy
  84 + if (PHP_OS == "WINNT") {
  85 + // copy the file to a semi-permanent location
  86 + if (! $basedir = make_upload_directory("$course->id")) {
  87 + error("The site administrator needs to fix the file permissions for the data directory");
  88 + }
  89 + $bname=basename($filename);
  90 + $cleanfilename = clean_filename($bname);
  91 + if ($cleanfilename) {
  92 + $newfile = "$basedir/$cleanfilename";
  93 + if (move_uploaded_file($filename, $newfile)) {
  94 + chmod($newfile, 0666);
  95 + } else {
  96 + notify(get_string("uploadproblem", "", $filename));
  97 + }
  98 + }
  99 + $filename = $newfile;
  100 + }
  101 + // end of file copy
104 102
105   - // don't have to do this on linux, since it's alreay been done in the test above
106   - if (PHP_OS == "WINNT") { $question_categories = $this->getquestioncategories($filename); }
107   - // print the intermediary form
108   - if (!$categories = quiz_get_category_menu($course->id, true)) {
109   - error("No categories!");
110   - }
111   - print_heading_with_help($strimportquestions, "import", "quiz");
112   - print_simple_box_start("center", "", "$THEME->cellheading");
113   - echo "<form method=\"post\" action=\"import.php\">";
114   - echo "<table cellpadding=5>";
115   - echo "<tr><td align=right>";
116   - echo "Choose a category of questions to import:</td><td>";
117   - asort($question_categories);
118   - choose_from_menu($question_categories, "question_category","All Categories","All Categories", "", "allcategories");
119   - echo " <input name=\"filename\" type=\"hidden\" value=\"".$filename."\">";
120   - echo " <input name=\"category\" type=\"hidden\" value=\"".$category->id."\">";
121   - echo " <input name=\"format\" type=\"hidden\" value=\"".$form->format."\">";
122   - if (PHP_OS == "Linux") {
123   - echo " <input name=\"hostname\" type=\"hidden\" value=\"".stripslashes(trim($hostname))."\">";
124   - echo " <input name=\"mdbpath\" type=\"hidden\" value=\"".stripslashes($mdbpath)."\">";
125   - echo " <input name=\"mdapath\" type=\"hidden\" value=\"".stripslashes($mdapath)."\">";
126   - }
127   - echo "</td><td>&nbsp;</td>";
128   - echo "</tr><tr><td>&nbsp;</td><td>";
129   - echo " <input type=submit name=save value=\"Import Questions\">";
130   - echo "</td></tr>";
131   - echo "</table>";
132   - echo "</form>";
133   - print_simple_box_end();
134   - print_footer($course);
135   - exit;
136   - }
137   - //
138   - // this is the main import section
139   - //
140   - notify("Importing questions");
141   - if (PHP_OS == "Linux") {
142   - $hostname = trim($hostname);
143   - $records = $this->getquestions($mdbpath,$question_category,$mdapath, $hostname);
144   - } else {
145   - $records = $this->getquestions($filename,$question_category);
146   - }
147   - foreach ($records as $qrec)
148   - {
149   - $question = NULL;
150   - $question->image = ""; // No images with this format
151   - if ($qrec[9] != "") {
152   - $question->image = $qrec[9];
153   - }
154   - $question->defaultgrade = 1;
155   - // 0 Selected
156   - // 1 PracticeTestOK?
157   - // 2 QuestionText
158   - // 3 QuestionType
159   - // 4 Option1Text
160   - // 5 Option2Text
161   - // 6 Option3Text
162   - // 7 Option4Text
163   - // 8 CorrectAnswer
164   - // 9 Graphic
165   - // 10 Module
166   - // 11 ChapterNumber
167   - // 12 PageNumber
168   - $ref = "Answer can be found in chapter ". $qrec[11] . ", page " . $qrec[12] . ".";
169   - switch ($qrec[3]) {
170   - case 1:
171   - $question->qtype = MULTICHOICE; // MULTICHOICE, SHORTANSWER, TRUEFALSE
172   - // echo "<pre>";echo htmlspecialchars($qrec[2]); echo "</pre>";
173   - $question->questiontext = addslashes(trim($qrec[2]));
174   - // echo "<pre>";echo $question->questiontext; echo "</pre>";
175   - $question->name = preg_replace("/<br>/", "", $question->questiontext);
176   - $question->single = 1; // Only one answer is allowed -- used for multiple choicers
177   - $fractionset = 0;
178   - for ($i=4;$i<=7;$i++) {
179   - if ($qrec[$i] != "") {
180   - $question->answer[$i-3]=addslashes($qrec[$i]);
181   - if ($qrec[8] == $i-3) { // if this is the index of CorrectAnswer
182   - $question->fraction[$i-3] = 1;
183   - $fractionset = 1;
184   - } else {
185   - $question->fraction[$i-3] = 0;
186   - }
187   - $question->feedback[$i-3] = (($qrec[8] == $i-3)?"Correct. ":"Incorrect. ") . $ref;
188   - }
189   - }
190   - if ($fractionset == 0) { $question->fraction[1] = 1; }
191   - break;
192   - case 2: // TRUE FALSE
193   - $question->qtype = TRUEFALSE;
194   - $question->questiontext = addslashes(trim($qrec[2]));
195   - $question->name = preg_replace("/<br>/", "", $question->questiontext);
196   - // for TF, $question->answer should be 1 for true, 0 for false
197   - if ($qrec[8] == "T") { $question->answer =1; echo "answer T"; } else { $question->answer = 0; }
198   - // for TF, use $question->feedbacktrue and feedbackfalse
199   - $question->feedbacktrue = (($qrec[8] =="T")?"Correct. ":"Incorrect. ") . $ref;
200   - $question->feedbackfalse = (($qrec[8] =="F")?"Correct. ":"Incorrect. ") . $ref;
201   - break;
202   - case 3:
203   - $question->qtype = SHORTANSWER;
204   - $question->questiontext = addslashes(trim($qrec[2]));
205   - // echo "<pre>";echo $question->questiontext; echo "</pre>";
206   - $question->name = preg_replace("/<br>/", "", $question->questiontext);
207   - $question->usecase=0; // Ignore case -- for SHORT ANSWER questions
208   - $answers = explode("~", $qrec[8]);
209   - $question->answer[1]=" ";
210   - $question->fraction[1]=1;
211   - for ($i=0;$i<count($answers);$i++) {
212   - $question->answer[$i] = addslashes(trim($answers[$i]));
213   - $question->feedback[$i] = $ref;
214   - $question->fraction[$i] = 1; // 1 for 100%, 0 for none or somewhere in between
215   - }
216   - break;
217   - case 4:
218   - $question = 0;
219   - notify("Cannot use essay questions - skipping question ". $qrec[2] . " " . $ref);
220   - break;
221   - default:
222   - $question = 0;
223   - notify("Misformatted Record. Question Skipped.");
224   - break;
225   - }
226   - if ($question) { $questions[] = $question; }
227   - }
228   - $count = 0;
229   - // process all the questions
230   - if (PHP_OS == "WINNT") {
231   - $filename = str_replace("\\\\","\\",$filename);
232   - $filename = str_replace("/","\\",$filename);
233   - }
234   - foreach ($questions as $question) { // Process and store each question
235   - $count++;
236   - echo "<hr><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
237   - $question->category = $this->category->id;
238   - $question->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
239   - $question->version = 1; // Original version of this question
240   - if (!$question->id = insert_record("quiz_questions", $question)) {
241   - error("Could not insert new question!");
242   - }
243   - $this->questionids[] = $question->id;
244   - // Now to save all the answers and type-specific options
245   - $result = quiz_save_question_options($question);
246   - if (!empty($result->error)) {
247   - notify($result->error);
248   - $this->deletedatabase($filename);
249   - return false;
250   - }
251   - if (!empty($result->notice)) {
252   - notify($result->notice);
253   - $this->deletedatabase($filename);
254   - return true;
255   - }
  103 + // don't have to do this on linux, since it's alreay been done in the test above
  104 + if (PHP_OS == "WINNT") { $question_categories = $this->getquestioncategories($filename); }
  105 + // print the intermediary form
  106 + if (!$categories = quiz_get_category_menu($course->id, true)) {
  107 + error("No categories!");
  108 + }
  109 + print_heading_with_help($strimportquestions, "import", "quiz");
  110 + print_simple_box_start("center", "", "$THEME->cellheading");
  111 + echo "<form method=\"post\" action=\"import.php\">";
  112 + echo "<table cellpadding=5>";
  113 + echo "<tr><td align=right>";
  114 + echo "Choose a category of questions to import:</td><td>";
  115 + asort($question_categories);
  116 + choose_from_menu($question_categories, "question_category","All Categories","All Categories", "", "allcategories");
  117 + echo " <input name=\"filename\" type=\"hidden\" value=\"".$filename."\">";
  118 + echo " <input name=\"category\" type=\"hidden\" value=\"".$category->id."\">";
  119 + echo " <input name=\"format\" type=\"hidden\" value=\"".$form->format."\">";
  120 + if (PHP_OS == "Linux") {
  121 + echo " <input name=\"hostname\" type=\"hidden\" value=\"".stripslashes(trim($hostname))."\">";
  122 + echo " <input name=\"mdbpath\" type=\"hidden\" value=\"".stripslashes($mdbpath)."\">";
  123 + echo " <input name=\"mdapath\" type=\"hidden\" value=\"".stripslashes($mdapath)."\">";
  124 + }
  125 + echo "</td><td>&nbsp;</td>";
  126 + echo "</tr><tr><td>&nbsp;</td><td>";
  127 + echo " <input type=submit name=save value=\"Import Questions\">";
  128 + echo "</td></tr>";
  129 + echo "</table>";
  130 + echo "</form>";
  131 + print_simple_box_end();
  132 + print_footer($course);
  133 + exit;
  134 + }
  135 +//
  136 +// this is the main import section
  137 +//
  138 + notify("Importing questions");
  139 + if (PHP_OS == "Linux") {
  140 + $hostname = trim($hostname);
  141 + $records = $this->getquestions($mdbpath,$question_category,$mdapath, $hostname);
  142 + } else {
  143 + $records = $this->getquestions($filename,$question_category);
  144 + }
  145 + foreach ($records as $qrec)
  146 + {
  147 + $question = NULL;
  148 + $question->image = ""; // No images with this format
  149 + if ($qrec[9] != "") {
  150 + $question->image = $qrec[9];
  151 + }
  152 + $question->defaultgrade = 1;
  153 +// 0 Selected
  154 +// 1 PracticeTestOK?
  155 +// 2 QuestionText
  156 +// 3 QuestionType
  157 +// 4 Option1Text
  158 +// 5 Option2Text
  159 +// 6 Option3Text
  160 +// 7 Option4Text
  161 +// 8 CorrectAnswer
  162 +// 9 Graphic
  163 +// 10 Module
  164 +// 11 ChapterNumber
  165 +// 12 PageNumber
  166 + $ref = "Answer can be found in chapter ". $qrec[11] . ", page " . $qrec[12] . ".";
  167 + switch ($qrec[3]) {
  168 + case 1:
  169 + $question->qtype = MULTICHOICE; // MULTICHOICE, SHORTANSWER, TRUEFALSE
  170 + // echo "<pre>";echo htmlspecialchars($qrec[2]); echo "</pre>";
  171 + $question->questiontext = addslashes(trim($qrec[2]));
  172 + // echo "<pre>";echo $question->questiontext; echo "</pre>";
  173 + $question->name = preg_replace("/<br>/", "", $question->questiontext);
  174 + $question->single = 1; // Only one answer is allowed -- used for multiple choicers
  175 + $fractionset = 0;
  176 + for ($i=4;$i<=7;$i++) {
  177 + if ($qrec[$i] != "") {
  178 + $question->answer[$i-3]=addslashes($qrec[$i]);
  179 + if ($qrec[8] == $i-3) { // if this is the index of CorrectAnswer
  180 + $question->fraction[$i-3] = 1;
  181 + $fractionset = 1;
  182 + } else {
  183 + $question->fraction[$i-3] = 0;
  184 + }
  185 + $question->feedback[$i-3] = (($qrec[8] == $i-3)?"Correct. ":"Incorrect. ") . $ref;
  186 + }
  187 + }
  188 + if ($fractionset == 0) { $question->fraction[1] = 1; }
  189 + break;
  190 + case 2: // TRUE FALSE
  191 + $question->qtype = TRUEFALSE;
  192 + $question->questiontext = addslashes(trim($qrec[2]));
  193 + $question->name = preg_replace("/<br>/", "", $question->questiontext);
  194 + // for TF, $question->answer should be 1 for true, 0 for false
  195 + if ($qrec[8] == "T") { $question->answer =1; echo "answer T"; } else { $question->answer = 0; }
  196 + // for TF, use $question->feedbacktrue and feedbackfalse
  197 + $question->feedbacktrue = (($qrec[8] =="T")?"Correct. ":"Incorrect. ") . $ref;
  198 + $question->feedbackfalse = (($qrec[8] =="F")?"Correct. ":"Incorrect. ") . $ref;
  199 + break;
  200 + case 3:
  201 + $question->qtype = SHORTANSWER;
  202 + $question->questiontext = addslashes(trim($qrec[2]));
  203 + // echo "<pre>";echo $question->questiontext; echo "</pre>";
  204 + $question->name = preg_replace("/<br>/", "", $question->questiontext);
  205 + $question->usecase=0; // Ignore case -- for SHORT ANSWER questions
  206 + $answers = explode("~", $qrec[8]);
  207 + $question->answer[1]=" ";
  208 + $question->fraction[1]=1;
  209 + for ($i=0;$i<count($answers);$i++) {
  210 + $question->answer[$i] = addslashes(trim($answers[$i]));
  211 + $question->feedback[$i] = $ref;
  212 + $question->fraction[$i] = 1; // 1 for 100%, 0 for none or somewhere in between
  213 + }
  214 + break;
  215 + case 4:
  216 + $question = 0;
  217 + notify("Cannot use essay questions - skipping question ". $qrec[2] . " " . $ref);
  218 + break;
  219 + default:
  220 + $question = 0;
  221 + notify("Misformatted Record. Question Skipped.");
  222 + break;
  223 + }
  224 + if ($question) { $questions[] = $question; }
  225 + }
  226 + $count = 0;
  227 + // process all the questions
  228 + if (PHP_OS == "WINNT") {
  229 + $filename = str_replace("\\\\","\\",$filename);
  230 + $filename = str_replace("/","\\",$filename);
  231 + }
  232 + foreach ($questions as $question) { // Process and store each question
  233 + $count++;
  234 + echo "<hr><p><b>$count</b>. ".stripslashes($question->questiontext)."</p>";
  235 + $question->category = $this->category->id;
  236 + $question->stamp = make_unique_id_code(); // Set the unique code (not to be changed)
  237 + $question->version = 1; // Original version of this question
  238 + if (!$question->id = insert_record("quiz_questions", $question)) {
  239 + error("Could not insert new question!");
  240 + }
  241 + $this->questionids[] = $question->id;
  242 + // Now to save all the answers and type-specific options
  243 + $result = quiz_save_question_options($question);
  244 + if (!empty($result->error)) {
  245 + notify($result->error);
  246 + $this->deletedatabase($filename);
  247 + return false;
  248 + }
  249 + if (!empty($result->notice)) {
  250 + notify($result->notice);
  251 + $this->deletedatabase($filename);
  252 + return true;
  253 + }
256 254 }
257 255 $this->deletedatabase($filename);
258 256 return true;
@@ -262,245 +260,245 @@ function importpostprocess() {
262 260 return true;
263 261 }
264 262
265   - function deletedatabase($filename) {
266   - if (! $this->fulldelete($filename)) {
267   - echo "<BR>Error: Could not delete: $filename";
268   - return false;
269   - }
270   - return true;
271   - }
  263 + function deletedatabase($filename) {
  264 + if (! $this->fulldelete($filename)) {
  265 + echo "<BR>Error: Could not delete: $filename";
  266 + return false;
  267 + }
  268 + return true;
  269 + }
272 270
273   - function getquestions($filename, $category, $mdapath="", $hostname="") {
274   - if (($category == "allcategories") or ($category == "")) {
275   - $sql = "SELECT * FROM TBQuestions";
276   - } else {
277   - $sql = "SELECT * FROM TBQuestions where module = '".$category."'";
278   - }
279   - if (PHP_OS == "WINNT") {
280   - $ldb =& $this->connect_win($filename);
281   - $qset = $ldb->Execute("$sql");
282   - if ( $qset->RecordCount() > 0 ) {
283   - $records = $qset->GetAssoc(true);
284   - } else {
285   - $this->err("There were no records in the database.",$dsn);
286   - $ldb->Close();
287   - return false;
288   - }
289   - $ldb->Close();
290   - } else { // if PHP_OS == WINNT
291   - // we have a linux installation
292   - $result = $this->query_linux($sql,$filename, $mdapath,$hostname);
293   - if ( count($result) > 0 ) {
294   - // get rid of the ID field in the first column.
295   - for($i=0;$i<count($result);$i++) {
296   - foreach (array_keys($result[$i]) as $j) {
297   - $records[$i][$j-1] = $result[$i][$j];
298   - }
299   - }
300   - } else {
301   - $this->err("There were no records in the database.",$dsn);
302   - $ldb->Close();
303   - return false;
304   - }
305   - // xml test and connect
306   - } // PHP_OS TEST
307   - return $records;
308   - }
  271 + function getquestions($filename, $category, $mdapath="", $hostname="") {
  272 + if (($category == "allcategories") or ($category == "")) {
  273 + $sql = "SELECT * FROM TBQuestions";
  274 + } else {
  275 + $sql = "SELECT * FROM TBQuestions where module = '".$category."'";
  276 + }
  277 + if (PHP_OS == "WINNT") {
  278 + $ldb =& $this->connect_win($filename);
  279 + $qset = $ldb->Execute("$sql");
  280 + if ( $qset->RecordCount() > 0 ) {
  281 + $records = $qset->GetAssoc(true);
  282 + } else {
  283 + $this->err("There were no records in the database.",$dsn);
  284 + $ldb->Close();
  285 + return false;
  286 + }
  287 + $ldb->Close();
  288 + } else { // if PHP_OS == WINNT
  289 + // we have a linux installation
  290 + $result = $this->query_linux($sql,$filename, $mdapath,$hostname);
  291 + if ( count($result) > 0 ) {
  292 + // get rid of the ID field in the first column.
  293 + for($i=0;$i<count($result);$i++) {
  294 + foreach (array_keys($result[$i]) as $j) {
  295 + $records[$i][$j-1] = $result[$i][$j];
  296 + }
  297 + }
  298 + } else {
  299 + $this->err("There were no records in the database.",$dsn);
  300 + $ldb->Close();
  301 + return false;
  302 + }
  303 + // xml test and connect
  304 + } // PHP_OS TEST
  305 + return $records;
  306 + }
309 307
310 308 function getquestioncategories($filename, $mdapath="", $hostname="") {
311   - global $CFG, $result;
312   - $sql = "SELECT Distinct module FROM TBQuestions";
313   - if (PHP_OS == "WINNT") {
314   - $ldb =& $this->connect_win($filename);
315   - $qset = $ldb->Execute("$sql");
316   - if ( $qset->RecordCount() > 0 ) {
317   - $records = $qset->GetArray(true);
318   - foreach ($records as $record) {
319   - $categories[$record[0]] = $record[0];
320   - }
321   - } else { // if recordcount
322   - $this->err("There were no records in the database.",$dsn);
323   - $ldb->Close();
324   - return false;
325   - }
326   - $ldb->Close();
327   - } else { // if PHP_OS == WINNT
328   - // we have a linux installation
329   - $result = $this->query_linux($sql, $filename, $mdapath, $hostname);
330   - for($i=0;$i<count($result);$i++) {
331   - $categories[$result[$i][0]] = $result[$i][0];
332   - }
333   - } // PHP_OS TEST
334   - return $categories;
  309 + global $CFG, $result;
  310 + $sql = "SELECT Distinct module FROM TBQuestions";
  311 + if (PHP_OS == "WINNT") {
  312 + $ldb =& $this->connect_win($filename);
  313 + $qset = $ldb->Execute("$sql");
  314 + if ( $qset->RecordCount() > 0 ) {
  315 + $records = $qset->GetArray(true);
  316 + foreach ($records as $record) {
  317 + $categories[$record[0]] = $record[0];
  318 + }
  319 + } else { // if recordcount
  320 + $this->err("There were no records in the database.",$dsn);
  321 + $ldb->Close();
  322 + return false;
  323 + }
  324 + $ldb->Close();
  325 + } else { // if PHP_OS == WINNT
  326 + // we have a linux installation
  327 + $result = $this->query_linux($sql, $filename, $mdapath, $hostname);
  328 + for($i=0;$i<count($result);$i++) {
  329 + $categories[$result[$i][0]] = $result[$i][0];
  330 + }
  331 + } // PHP_OS TEST
  332 + return $categories;
335 333 }
336   -
337   - function query_linux($sql, $mdbpath, $mdapath, $hostname) {
338   - global $result;
339   - include_once("$CFG->libdir/odbc.php");
340   - // set up socket server object to connect to remote host
341   - $oTest = new ODBCSocketServer;
342   - //Set the Hostname, port, and connection string
343   - $oTest->sHostName = $hostname;
344   - $oTest->nPort = 9628;
345   - // $oTest->sConnectionString="DRIVER=Microsoft Access Driver (*.mdb);SystemDB=C:\CTM\System.mda;DBQ=C:\CTM\of2K3\ctm.mdb;UID=Assess;PWD=VBMango;";
346   - $oTest->sConnectionString="DRIVER=Microsoft Access Driver (*.mdb);SystemDB=".
347   - $mdapath.";DBQ=".$mdbpath.";UID=Assess;PWD=VBMango;";
348   - // send and receive XML communication
349   - $qResult = $oTest->ExecSQL($sql);
350   - // set up XML parser to read the results
351   - $xml_parser = xml_parser_create("US-ASCII");
352   - xml_set_element_handler($xml_parser, "quiz_xmlstart", "quiz_xmlend");
  334 +
  335 + function query_linux($sql, $mdbpath, $mdapath, $hostname) {
  336 + global $result;
  337 + include_once("odbcsocketserver.class.php");
  338 + // set up socket server object to connect to remote host
  339 + $oTest = new ODBCSocketServer;
  340 + //Set the Hostname, port, and connection string
  341 + $oTest->sHostName = $hostname;
  342 + $oTest->nPort = 9628;
  343 +// $oTest->sConnectionString="DRIVER=Microsoft Access Driver (*.mdb);SystemDB=C:\CTM\System.mda;DBQ=C:\CTM\of2K3\ctm.mdb;UID=Assess;PWD=VBMango;";
  344 + $oTest->sConnectionString="DRIVER=Microsoft Access Driver (*.mdb);SystemDB=".
  345 + $mdapath.";DBQ=".$mdbpath.";UID=Assess;PWD=VBMango;";
  346 + // send and receive XML communication
  347 + $qResult = $oTest->ExecSQL($sql);
  348 + // set up XML parser to read the results
  349 + $xml_parser = xml_parser_create("US-ASCII");
  350 + xml_set_element_handler($xml_parser, "quiz_xmlstart", "quiz_xmlend");
353 351 xml_set_character_data_handler($xml_parser, "quiz_xmldata");
354 352 // parse the XML and get back the result set array
355 353 if (!xml_parse($xml_parser, $qResult)) {
356   - $this->err("XML error: ".xml_error_string(xml_get_error_code($xml_parser))
357   - ." at line ".xml_get_current_line_number($xml_parser),$oTest->sConnectionString);
358   - return false;
  354 + $this->err("XML error: ".xml_error_string(xml_get_error_code($xml_parser))
  355 + ." at line ".xml_get_current_line_number($xml_parser),$oTest->sConnectionString);
  356 + return false;
359 357 } else {
360   - // echo("Successful XML parse. ");
361   - // prepare the array for use in the pull-down
362   - /* echo "<br>count of rows is ". count ($result);
363   - echo "<pre>\n";
364   - $qResult = HtmlSpecialChars($qResult);
365   - echo $qResult;
366   - echo "\n</pre>";
367   - */
368   - xml_parser_free($xml_parser);
369   - // $sResult = HtmlSpecialChars($qResult);
370   - //echo("<pre>");
371   - // echo($sResult);
372   - // echo("</pre>");
  358 +// echo("Successful XML parse. ");
  359 + // prepare the array for use in the pull-down
  360 +/* echo "<br>count of rows is ". count ($result);
  361 + echo "<pre>\n";
  362 + $qResult = HtmlSpecialChars($qResult);
  363 + echo $qResult;
  364 + echo "\n</pre>";
  365 +*/
  366 + xml_parser_free($xml_parser);
  367 +// $sResult = HtmlSpecialChars($qResult);
  368 + //echo("<pre>");
  369 +// echo($sResult);
  370 +// echo("</pre>");
373 371
374   - return $result;
  372 + return $result;
375 373 }
376   - }
  374 + }
377 375
378   - function connect_win($filename) {
379   - global $CFG, $systemdb;
380   - // first, verify the location of System.mda
381   - if (!isset($systemdb)) {
382   - $systemdb=$this->findfile("System.mda");
383   - }
384   - if (! $systemdb) {
385   - $this->err("The system database System.mda cannot be found. Check that you've uploaded it to the course.",$dsn);
386   - die;
387   - }
  376 + function connect_win($filename) {
  377 + global $CFG, $systemdb;
  378 + // first, verify the location of System.mda
  379 + if (!isset($systemdb)) {
  380 + $systemdb=$this->findfile("System.mda");
  381 + }
  382 + if (! $systemdb) {
  383 + $this->err("The system database System.mda cannot be found. Check that you've uploaded it to the course.",$dsn);
  384 + die;
  385 + }
388 386
389   - $ldb = &ADONewConnection('access');
390   - $dsn="DRIVER=Microsoft Access Driver (*.mdb);SystemDB=".$systemdb.";DBQ=".$filename.";UID=Assess;PWD=VBMango;";
391   - $dbconnected = $ldb->Connect($dsn);
392   - if (! $dbconnected) {
393   - $this->err("Moodle could not connect to the database.",$dsn);
394   - die;
395   - }
396   - return $ldb;
397   - }
  387 + $ldb = &ADONewConnection('access');
  388 + $dsn="DRIVER=Microsoft Access Driver (*.mdb);SystemDB=".$systemdb.";DBQ=".$filename.";UID=Assess;PWD=VBMango;";
  389 + $dbconnected = $ldb->Connect($dsn);
  390 + if (! $dbconnected) {
  391 + $this->err("Moodle could not connect to the database.",$dsn);
  392 + die;
  393 + }
  394 + return $ldb;
  395 + }
398 396
399 397 function err($message, $dsn) {
400   - echo "<font color=\"#990000\">";
401   - echo "<p>Error: $message</p>";
402   - echo "<p>ODBC File DSN: $dsn<br />";
403   - echo "</font>";
  398 + echo "<font color=\"#990000\">";
  399 + echo "<p>Error: $message</p>";
  400 + echo "<p>ODBC File DSN: $dsn<br />";
  401 + echo "</font>";
404 402 }
405 403
406   - function fulldelete($location) {
407   - if (is_dir($location)) {
408   - $currdir = opendir($location);
409   - while ($file = readdir($currdir)) {
410   - if ($file <> ".." && $file <> ".") {
411   - $fullfile = $location."/".$file;
412   - if (is_dir($fullfile)) {
413   - if (!fulldelete($fullfile)) {
414   - return false;
415   - }
416   - } else {
417   - if (!unlink($fullfile)) {
418   - return false;
419   - }
420   - }
421   - }
422   - }
423   - closedir($currdir);
424   - if (! rmdir($location)) {
425   - return false;
426   - }
427   -
428   - } else {
429   - if (!unlink($location)) {
430   - return false;
431   - }
432   - }
433   - return true;
434   - }
  404 + function fulldelete($location) {
  405 + if (is_dir($location)) {
  406 + $currdir = opendir($location);
  407 + while ($file = readdir($currdir)) {
  408 + if ($file <> ".." && $file <> ".") {
  409 + $fullfile = $location."/".$file;
  410 + if (is_dir($fullfile)) {
  411 + if (!fulldelete($fullfile)) {
  412 + return false;
  413 + }
  414 + } else {
  415 + if (!unlink($fullfile)) {
  416 + return false;
  417 + }
  418 + }
  419 + }
  420 + }
  421 + closedir($currdir);
  422 + if (! rmdir($location)) {
  423 + return false;
  424 + }
  425 +
  426 + } else {
  427 + if (!unlink($location)) {
  428 + return false;
  429 + }
  430 + }
  431 + return true;
  432 + }
435 433
436 434
437 435 function findfile($filename) {
438   - global $CFG;
439   - $dirs = $this->getcoursedirs();
440   - $dirs[] = $CFG->dirroot."\mod\quiz\format";
441   - foreach ($dirs as $dir) {
442   - $file = $dir . "\System.mda";
443   - // look for System.mda
444   - if (is_file($file)) return $file;
445   - }
446   - return false;
  436 + global $CFG;
  437 + $dirs = $this->getcoursedirs();
  438 + $dirs[] = $CFG->dirroot."\mod\quiz\format";
  439 + foreach ($dirs as $dir) {
  440 + $file = $dir . "\System.mda";
  441 + // look for System.mda
  442 + if (is_file($file)) return $file;
  443 + }
  444 + return false;
447 445 }
448 446
449 447 function getcoursedirs() {
450   - global $CFG;
451   - // for every course in the system, find the root of the data directory
452   - $courses = get_records_sql("select distinct id,fullname from ".$CFG->prefix."course");
453   - $dirs = array();
454   - if ($courses) {
455   - foreach ($courses as $course) {
456   - $dir = $CFG->dataroot . "/" . $course->id;
457   - if (is_dir($dir)) { $dirs[] = $dir; }
458   - }
459   - }
460   - return $dirs;
  448 + global $CFG;
  449 + // for every course in the system, find the root of the data directory
  450 + $courses = get_records_sql("select distinct id,fullname from ".$CFG->prefix."course");
  451 + $dirs = array();
  452 + if ($courses) {
  453 + foreach ($courses as $course) {
  454 + $dir = $CFG->dataroot . "/" . $course->id;
  455 + if (is_dir($dir)) { $dirs[] = $dir; }
  456 + }
  457 + }
  458 + return $dirs;
461 459 }
462 460
463 461 } // END OF CLASS
464 462
465   -//Handler for starting elements
466   -function quiz_xmlstart($parser, $name, $attribs) {
467   - global $result,$row, $col, $incolumn;
468   - $name = strtolower($name);
469   - switch ($name) {
470   - case "row":
471   - $col=0;break;
472   - case "column":
473   - $incolumn = 1;break;
474   - case "error":
475   - break;
476   - case "result":
477   - $row = 0; break;
478   - } // switch
479   -}
  463 + //Handler for starting elements
  464 + function quiz_xmlstart($parser, $name, $attribs) {
  465 + global $result,$row, $col, $incolumn;
  466 + $name = strtolower($name);
  467 + switch ($name) {
  468 + case "row":
  469 + $col=0;break;
  470 + case "column":
  471 + $incolumn = 1;break;
  472 + case "error":
  473 + break;
  474 + case "result":
  475 + $row = 0; break;
  476 + } // switch
  477 + }
480 478
481   -//handler for the end of elements
482   -function quiz_xmlend($parser, $name) {
483   - global $result, $row, $col, $incolumn;
484   - $name = strtolower($name);
485   - switch ($name) {
486   - case "row":
487   - $row++;break;
488   - case "column":
489   - $incolumn = 0;
  479 + //handler for the end of elements
  480 + function quiz_xmlend($parser, $name) {
  481 + global $result, $row, $col, $incolumn;
  482 + $name = strtolower($name);
  483 + switch ($name) {
  484 + case "row":
  485 + $row++;break;
  486 + case "column":
  487 + $incolumn = 0;
490 488 $col++;
491 489 break;
492   - case "error":
493   - break;
494   - case "result":
495   - break;
496   - } // switch
497   -} // function
  490 + case "error":
  491 + break;
  492 + case "result":
  493 + break;
  494 + } // switch
  495 + } // function
498 496
499   -//handler for character data
500   -function quiz_xmldata($parser, $data) {
501   - global $result, $row, $col, $incolumn;
502   - if ($incolumn) { $result[$row][$col] = $result[$row][$col] . $data;
503   - }
504   -}
  497 + //handler for character data
  498 + function quiz_xmldata($parser, $data) {
  499 + global $result, $row, $col, $incolumn;
  500 + if ($incolumn) { $result[$row][$col] = $result[$row][$col] . $data;
  501 + }
  502 + }
505 503
506 504 ?>

0 comments on commit 108ef20

Please sign in to comment.
Something went wrong with that request. Please try again.