View source
<?php
require_once $CFG->dirroot . '/lib/uploadlib.php';
class qformat_coursetestmanager extends qformat_default {
function provide_import() {
return true;
}
function importpreprocess($category) {
$this->category = $category;
return true;
}
function importprocess($filename) {
global $CFG, $USER, $strimportquestions, $form, $question_category, $category, $COURSE, $hostname, $mdapath, $mdbpath;
if (PHP_OS == "Linux" and isset($hostname)) {
$hostname = trim($hostname);
$question_categories = $this
->getquestioncategories($mdbpath, $mdapath, $hostname);
if (!$question_categories) {
$hostname_access_error = $hostname . " ";
unset($hostname);
}
else {
$hostname_access_error = 0;
}
}
if (PHP_OS == "Linux" and !isset($hostname)) {
if (!($basedir = make_upload_directory("{$COURSE->id}"))) {
error("The site administrator needs to fix the file permissions for the data directory");
}
if (!isset($hostname_access_error)) {
$bname = basename($filename);
$cleanfilename = clean_filename($bname);
if ($cleanfilename) {
$newfile = "{$basedir}/{$cleanfilename}";
if (move_uploaded_file($filename, $newfile)) {
chmod($newfile, 0666);
clam_log_upload($newfile, $COURSE);
}
else {
notify(get_string("uploadproblem", "", $filename));
}
}
$filename = $newfile;
}
print_heading_with_help($strimportquestions, "import", "quiz");
print_simple_box_start("center");
if ($hostname_access_error) {
notify("couldn't connect to ODBC Socket Server on " . $hostname_access_error);
}
echo "<form method=\"post\" action=\"import.php\">";
echo '<fieldset class="invisiblefieldset">';
echo "<table cellpadding=\"5\">";
echo "<tr><td align=\"right\">";
echo "What is the hostname or IP address of the ODBC Socket Server:</td><td>";
echo " <input name=\"hostname\" type=\"text\" size=\"50\" value=\"" . stripslashes($hostname_access_error) . "\" />";
echo " <input name=\"filename\" type=\"hidden\" value=\"" . $filename . "\" />";
echo " <input name=\"category\" type=\"hidden\" value=\"" . $category->id . "\" />";
echo " <input name=\"format\" type=\"hidden\" value=\"" . $form->format . "\" />";
echo "</td><td> </td></tr>";
echo "<tr><td align=\"right\">";
echo "What is the location of the database (.mdb file) on the Socket Server:</td><td>";
echo " <input name=\"mdbpath\" type=\"text\" size=\"50\" value=\"" . stripslashes($mdbpath) . "\" />";
echo "</td><td> </td></tr>";
echo "<tr><td align=\"right\">";
echo "What is the location of the system database (System.mda file) on the Socket Server:</td><td>";
echo " <input name=\"mdapath\" type=\"text\" size=\"50\" value=\"" . stripslashes($mdapath) . "\" />";
echo "</td><td> </td></tr>";
echo "<tr><td> </td><td>";
echo " <input type=\"submit\" name=\"save\" value=\"Connect to Server\" />";
echo "</td></tr>";
echo "</table>";
echo '</fieldset>';
echo "</form>";
print_simple_box_end();
print_footer($COURSE);
exit;
}
if (!isset($question_category)) {
if (PHP_OS == "WINNT") {
if (!($basedir = make_upload_directory("{$COURSE->id}"))) {
error("The site administrator needs to fix the file permissions for the data directory");
}
$bname = basename($filename);
$cleanfilename = clean_filename($bname);
if ($cleanfilename) {
$newfile = "{$basedir}/{$cleanfilename}";
if (move_uploaded_file($filename, $newfile)) {
chmod($newfile, 0666);
clam_log_upload($newfile, $COURSE);
}
else {
notify(get_string("uploadproblem", "", $filename));
}
}
$filename = $newfile;
}
if (PHP_OS == "WINNT") {
$question_categories = $this
->getquestioncategories($filename);
}
if (!($categories = question_category_options($COURSE->id, true))) {
error("No categories!");
}
print_heading_with_help($strimportquestions, "import", "quiz");
print_simple_box_start("center");
echo "<form method=\"post\" action=\"import.php\">";
echo '<fieldset class="invisiblefieldset">';
echo "<table cellpadding=\"5\">";
echo "<tr><td align=\"right\">";
echo "Choose a category of questions to import:</td><td>";
asort($question_categories);
choose_from_menu($question_categories, "question_category", "All Categories", "All Categories", "", "allcategories");
echo " <input name=\"filename\" type=\"hidden\" value=\"" . $filename . "\" />";
echo " <input name=\"category\" type=\"hidden\" value=\"" . $category->id . "\" />";
echo " <input name=\"format\" type=\"hidden\" value=\"" . $form->format . "\" />";
if (PHP_OS == "Linux") {
echo " <input name=\"hostname\" type=\"hidden\" value=\"" . stripslashes(trim($hostname)) . "\" />";
echo " <input name=\"mdbpath\" type=\"hidden\" value=\"" . stripslashes($mdbpath) . "\" />";
echo " <input name=\"mdapath\" type=\"hidden\" value=\"" . stripslashes($mdapath) . "\" />";
}
echo "</td><td> </td>";
echo "</tr><tr><td> </td><td>";
echo " <input type=\"submit\" name=\"save\" value=\"Import Questions\" />";
echo "</td></tr>";
echo "</table>";
echo '</fieldset>';
echo "</form>";
print_simple_box_end();
print_footer($COURSE);
exit;
}
notify("Importing questions");
if (PHP_OS == "Linux") {
$hostname = trim($hostname);
$records = $this
->getquestions($mdbpath, $question_category, $mdapath, $hostname);
}
else {
$records = $this
->getquestions($filename, $question_category);
}
foreach ($records as $qrec) {
$question = $this
->defaultquestion();
if ($qrec[9] != "") {
$question->image = $qrec[9];
}
$ref = "Answer can be found in chapter " . $qrec[11] . ", page " . $qrec[12] . ".";
switch ($qrec[3]) {
case 1:
$question->qtype = MULTICHOICE;
$question->questiontext = addslashes(trim($qrec[2]));
$question->name = preg_replace("/<br />/", "", $question->questiontext);
$question->single = 1;
$fractionset = 0;
for ($i = 4; $i <= 7; $i++) {
if ($qrec[$i] != "") {
$question->answer[$i - 3] = addslashes($qrec[$i]);
if ($qrec[8] == $i - 3) {
$question->fraction[$i - 3] = 1;
$fractionset = 1;
}
else {
$question->fraction[$i - 3] = 0;
}
$question->feedback[$i - 3] = ($qrec[8] == $i - 3 ? "Correct. " : "Incorrect. ") . $ref;
}
}
if ($fractionset == 0) {
$question->fraction[1] = 1;
}
break;
case 2:
$question->qtype = TRUEFALSE;
$question->questiontext = addslashes(trim($qrec[2]));
$question->name = preg_replace("/<br />/", "", $question->questiontext);
if ($qrec[8] == "T") {
$question->answer = 1;
}
else {
$question->answer = 0;
}
$question->feedbacktrue = ($qrec[8] == "T" ? "Correct. " : "Incorrect. ") . $ref;
$question->feedbackfalse = ($qrec[8] == "F" ? "Correct. " : "Incorrect. ") . $ref;
break;
case 3:
$question->qtype = SHORTANSWER;
$question->questiontext = addslashes(trim($qrec[2]));
$question->name = preg_replace("/<br />/", "", $question->questiontext);
$question->usecase = 0;
$answers = explode("~", $qrec[8]);
$question->answer[0] = " ";
$question->fraction[0] = 1;
for ($i = 0; $i < count($answers); $i++) {
$question->answer[$i] = addslashes(trim($answers[$i]));
$question->feedback[$i] = $ref;
$question->fraction[$i] = 1;
}
break;
case 4:
$question = 0;
notify("Cannot use essay questions - skipping question " . $qrec[2] . " " . $ref);
break;
default:
$question = 0;
notify("Misformatted Record. Question Skipped.");
break;
}
if ($question) {
$questions[] = $question;
}
}
$count = 0;
if (PHP_OS == "WINNT") {
$filename = str_replace("\\\\", "\\", $filename);
$filename = str_replace("/", "\\", $filename);
}
foreach ($questions as $question) {
$count++;
echo "<hr /><p><b>{$count}</b>. " . stripslashes($question->questiontext) . "</p>";
$question->category = $this->category->id;
$question->stamp = make_unique_id_code();
$question->createdby = $USER->id;
$question->timecreated = time();
if (!($question->id = insert_record("question", $question))) {
error("Could not insert new question!");
}
$this->questionids[] = $question->id;
$result = save_question_options($question);
if (!empty($result->error)) {
notify($result->error);
$this
->deletedatabase($filename);
return false;
}
if (!empty($result->notice)) {
notify($result->notice);
$this
->deletedatabase($filename);
return true;
}
set_field('question', 'version', question_hash($question), 'id', $question->id);
}
$this
->deletedatabase($filename);
return true;
}
function importpostprocess() {
return true;
}
function deletedatabase($filename) {
if (!$this
->fulldelete($filename)) {
echo "<br />Error: Could not delete: {$filename}";
return false;
}
return true;
}
function getquestions($filename, $category, $mdapath = "", $hostname = "") {
if ($category == "allcategories" or $category == "") {
$sql = "SELECT * FROM TBQuestions";
}
else {
$sql = "SELECT * FROM TBQuestions where module = '" . $category . "'";
}
if (PHP_OS == "WINNT") {
$ldb =& $this
->connect_win($filename);
$qset = $ldb
->Execute("{$sql}");
if (!$qset->EOF) {
$records = $qset
->GetAssoc(true);
}
else {
$this
->err("There were no records in the database.", $dsn);
$ldb
->Close();
return false;
}
$ldb
->Close();
}
else {
$result = $this
->query_linux($sql, $filename, $mdapath, $hostname);
if (count($result) > 0) {
for ($i = 0; $i < count($result); $i++) {
foreach (array_keys($result[$i]) as $j) {
$records[$i][$j - 1] = $result[$i][$j];
}
}
}
else {
$this
->err("There were no records in the database.", $dsn);
$ldb
->Close();
return false;
}
}
return $records;
}
function getquestioncategories($filename, $mdapath = "", $hostname = "") {
global $CFG, $result;
$sql = "SELECT Distinct module FROM TBQuestions";
if (PHP_OS == "WINNT") {
$ldb =& $this
->connect_win($filename);
$qset = $ldb
->Execute("{$sql}");
if (!$qset->EOF) {
$records = $qset
->GetArray(true);
foreach ($records as $record) {
$categories[$record[0]] = $record[0];
}
}
else {
$this
->err("There were no records in the database.", $dsn);
$ldb
->Close();
return false;
}
$ldb
->Close();
}
else {
$result = $this
->query_linux($sql, $filename, $mdapath, $hostname);
for ($i = 0; $i < count($result); $i++) {
$categories[$result[$i][0]] = $result[$i][0];
}
}
return $categories;
}
function query_linux($sql, $mdbpath, $mdapath, $hostname) {
global $result;
include_once "odbcsocketserver.class.php";
$oTest = new ODBCSocketServer();
$oTest->sHostName = $hostname;
$oTest->nPort = 9628;
$oTest->sConnectionString = "DRIVER=Microsoft Access Driver (*.mdb);SystemDB=" . $mdapath . ";DBQ=" . $mdbpath . ";UID=Assess;PWD=VBMango;";
$qResult = $oTest
->ExecSQL($sql);
$xml_parser = xml_parser_create("US-ASCII");
xml_set_element_handler($xml_parser, "quiz_xmlstart", "quiz_xmlend");
xml_set_character_data_handler($xml_parser, "quiz_xmldata");
if (!xml_parse($xml_parser, $qResult)) {
$this
->err("XML error: " . xml_error_string(xml_get_error_code($xml_parser)) . " at line " . xml_get_current_line_number($xml_parser), $oTest->sConnectionString);
return false;
}
else {
xml_parser_free($xml_parser);
return $result;
}
}
function connect_win($filename) {
global $CFG, $systemdb;
if (!isset($systemdb)) {
$systemdb = $this
->findfile("System.mda");
}
if (!$systemdb) {
$this
->err("The system database System.mda cannot be found. Check that you've uploaded it to the course.", $dsn);
die;
}
$ldb =& ADONewConnection('access');
$dsn = "DRIVER=Microsoft Access Driver (*.mdb);SystemDB=" . $systemdb . ";DBQ=" . $filename . ";UID=Assess;PWD=VBMango;";
$dbconnected = $ldb
->Connect($dsn);
if (!$dbconnected) {
$this
->err("Moodle could not connect to the database.", $dsn);
die;
}
return $ldb;
}
function err($message, $dsn) {
echo "<font color=\"#990000\">";
echo "<p>Error: {$message}</p>";
echo "<p>ODBC File DSN: {$dsn}<br />";
echo "</font>";
}
function fulldelete($location) {
if (is_dir($location)) {
$currdir = opendir($location);
while (false !== ($file = readdir($currdir))) {
if ($file != ".." && $file != ".") {
$fullfile = $location . "/" . $file;
if (is_dir($fullfile)) {
if (!fulldelete($fullfile)) {
return false;
}
}
else {
if (!unlink($fullfile)) {
return false;
}
}
}
}
closedir($currdir);
if (!rmdir($location)) {
return false;
}
}
else {
if (!unlink($location)) {
return false;
}
}
return true;
}
function findfile($filename) {
global $CFG;
$dirs = $this
->getcoursedirs();
$dirs[] = $CFG->dirroot . "\\mod\\quiz\format";
foreach ($dirs as $dir) {
$file = $dir . "\\System.mda";
if (is_file($file)) {
return $file;
}
}
return false;
}
function getcoursedirs() {
global $CFG;
$courses = get_records_sql("select distinct id,fullname from " . $CFG->prefix . "course");
$dirs = array();
if ($courses) {
foreach ($courses as $course) {
$dir = $CFG->dataroot . "/" . $course->id;
if (is_dir($dir)) {
$dirs[] = $dir;
}
}
}
return $dirs;
}
}
function quiz_xmlstart($parser, $name, $attribs) {
global $result, $row, $col, $incolumn;
$name = strtolower($name);
switch ($name) {
case "row":
$col = 0;
break;
case "column":
$incolumn = 1;
break;
case "error":
break;
case "result":
$row = 0;
break;
}
}
function quiz_xmlend($parser, $name) {
global $result, $row, $col, $incolumn;
$name = strtolower($name);
switch ($name) {
case "row":
$row++;
break;
case "column":
$incolumn = 0;
$col++;
break;
case "error":
break;
case "result":
break;
}
}
function quiz_xmldata($parser, $data) {
global $result, $row, $col, $incolumn;
if ($incolumn) {
$result[$row][$col] = $result[$row][$col] . $data;
}
}