Skip to content

Commit

Permalink
MDL-15116 survery dml conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jun 8, 2008
1 parent ae8c356 commit deb3a60
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 161 deletions.
47 changes: 20 additions & 27 deletions mod/survey/backuplib.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@
//-----------------------------------------------------------

function survey_backup_mods($bf,$preferences) {

global $CFG;
global $DB;

$status = true;

//Iterate over survey table
$surveys = get_records ("survey","course",$preferences->backup_course,"id");
$surveys = $DB->get_records ("survey", array("course"=>$preferences->backup_course),"id");
if ($surveys) {
foreach ($surveys as $survey) {
if (backup_mod_selected($preferences,'survey',$survey->id)) {
Expand All @@ -41,11 +40,12 @@ function survey_backup_mods($bf,$preferences) {
}

function survey_backup_one_mod($bf,$preferences,$survey) {
global $DB;

$status = true;

if (is_numeric($survey)) {
$survey = get_record('survey','id',$survey);
$survey = $DB->get_record('survey', array('id'=>$survey));
}

//Start mod
Expand Down Expand Up @@ -74,12 +74,11 @@ function survey_backup_one_mod($bf,$preferences,$survey) {

//Backup survey_answers contents (executed from survey_backup_mods)
function backup_survey_answers ($bf,$preferences,$survey) {

global $CFG;
global $CFG, $DB;

$status = true;

$survey_answers = get_records("survey_answers","survey",$survey,"id");
$survey_answers = $DB->get_records("survey_answers", array("survey"=>$survey), "id");
//If there is answers
if ($survey_answers) {
//Write start tag
Expand All @@ -106,12 +105,11 @@ function backup_survey_answers ($bf,$preferences,$survey) {

//Backup survey_analysis contents (executed from survey_backup_mods)
function backup_survey_analysis ($bf,$preferences,$survey) {

global $CFG;
global $CFG, $DB;

$status = true;

$survey_analysis = get_records("survey_analysis","survey",$survey,"id");
$survey_analysis = $DB->get_records("survey_analysis", array("survey"=>$survey,"id"));
//If there is analysis
if ($survey_analysis) {
//Write start tag
Expand Down Expand Up @@ -202,33 +200,28 @@ function servey_encode_content_links ($content,$preferences) {

//Returns an array of surveys id
function survey_ids ($course) {
global $DB;

global $CFG;

return get_records_sql ("SELECT a.id, a.course
FROM {$CFG->prefix}survey a
WHERE a.course = '$course'");
return $DB->get_records_sql ("SELECT a.id, a.course
FROM {survey} a
WHERE a.course = ?", array($course));
}

//Returns an array of survey answer id
function survey_answer_ids_by_course ($course) {
global $DB;

global $CFG;

return get_records_sql ("SELECT s.id , s.survey
FROM {$CFG->prefix}survey_answers s,
{$CFG->prefix}survey a
WHERE a.course = '$course' AND
s.survey = a.id");
return $DB->get_records_sql ("SELECT s.id , s.survey
FROM {survey_answers} s, {survey} a
WHERE a.course = ? AND s.survey = a.id", array($course));
}

function survey_answer_ids_by_instance ($instanceid) {
global $DB;

global $CFG;

return get_records_sql ("SELECT s.id , s.survey
FROM {$CFG->prefix}survey_answers s
WHERE s.survey = $instanceid");
return $DB->get_records_sql ("SELECT s.id , s.survey
FROM {survey_answers} s
WHERE s.survey = ?", array($instanceid));
}

?>
67 changes: 33 additions & 34 deletions mod/survey/download.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
$type = optional_param('type', 'xls', PARAM_ALPHA);
$group = optional_param('group', 0, PARAM_INT);

if (! $cm = get_record("course_modules", "id", $id)) {
if (! $cm = $DB->get_record("course_modules", array("id"=>$id))) {
print_error("Course Module ID was incorrect");
}

if (! $course = get_record("course", "id", $cm->course)) {
if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
print_error("Course is misconfigured");
}

Expand All @@ -21,7 +21,7 @@
require_login($course->id, false, $cm);
require_capability('mod/survey:download', $context) ;

if (! $survey = get_record("survey", "id", $cm->instance)) {
if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
print_error("Survey ID was incorrect");
}

Expand Down Expand Up @@ -84,7 +84,6 @@
}
}

$order = explode(",", $fullorderlist);
$questions = $fullquestions;

// Translate all the question texts
Expand All @@ -96,21 +95,21 @@

// Get and collate all the results in one big array

if (! $aaa = get_records("survey_answers", "survey", "$survey->id", "time ASC")) {
if (! $aaa = $DB->get_records("survey_answers", array("survey"=>$survey->id), "time ASC")) {
print_error("There are no answers for this survey yet.");
}

foreach ($aaa as $a) {
if (!$group or isset($users[$a->userid])) {
if (empty($results["$a->userid"])) { // init new array
$results["$a->userid"]["time"] = $a->time;
foreach ($order as $key => $qid) {
$results["$a->userid"]["$qid"]["answer1"] = "";
$results["$a->userid"]["$qid"]["answer2"] = "";
if (empty($results[$a->userid])) { // init new array
$results[$a->userid]["time"] = $a->time;
foreach ($fullorderlist as $qid) {
$results[$a->userid][$qid]["answer1"] = "";
$results[$a->userid][$qid]["answer2"] = "";
}
}
$results["$a->userid"]["$a->question"]["answer1"] = $a->answer1;
$results["$a->userid"]["$a->question"]["answer2"] = $a->answer2;
$results[$a->userid][$a->question]["answer1"] = $a->answer1;
$results[$a->userid][$a->question]["answer2"] = $a->answer2;
}
}

Expand All @@ -134,7 +133,7 @@
$myxls->write_string(0,$col++,$item);
}
foreach ($order as $key => $qid) {
$question = $questions["$qid"];
$question = $questions[$qid];
if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
$myxls->write_string(0,$col++,"$question->text");
}
Expand All @@ -150,10 +149,10 @@
foreach ($results as $user => $rest) {
$col = 0;
$row++;
if (! $u = get_record("user", "id", $user)) {
if (! $u = $DB->get_record("user", array("id"=>$user))) {
print_error("Error finding student # $user");
}
if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) {
if ($n = $DB->get_record("survey_analysis", array("survey"=>$survey->id, "userid"=>$user))) {
$notes = $n->notes;
} else {
$notes = "No notes made";
Expand All @@ -165,17 +164,17 @@
$myxls->write_string($row,$col++,$u->lastname);
$myxls->write_string($row,$col++,$u->email);
$myxls->write_string($row,$col++,$u->idnumber);
$myxls->write_string($row,$col++, userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p") );
// $myxls->write_number($row,$col++,$results["$user"]["time"],$date);
$myxls->write_string($row,$col++, userdate($results[$user]["time"], "%d-%b-%Y %I:%M:%S %p") );
// $myxls->write_number($row,$col++,$results[$user]["time"],$date);
$myxls->write_string($row,$col++,$notes);

foreach ($order as $key => $qid) {
$question = $questions["$qid"];
$question = $questions[$qid];
if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
$myxls->write_string($row,$col++, $results["$user"]["$qid"]["answer1"] );
$myxls->write_string($row,$col++, $results[$user][$qid]["answer1"] );
}
if ($question->type == "2" || $question->type == "3") {
$myxls->write_string($row, $col++, $results["$user"]["$qid"]["answer2"] );
$myxls->write_string($row, $col++, $results[$user][$qid]["answer2"] );
}
}
}
Expand Down Expand Up @@ -204,7 +203,7 @@
$myxls->write_string(0,$col++,$item);
}
foreach ($order as $key => $qid) {
$question = $questions["$qid"];
$question = $questions[$qid];
if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
$myxls->write_string(0,$col++,"$question->text");
}
Expand All @@ -220,10 +219,10 @@
foreach ($results as $user => $rest) {
$col = 0;
$row++;
if (! $u = get_record("user", "id", $user)) {
if (! $u = $DB->get_record("user", array("id"=>$user))) {
print_error("Error finding student # $user");
}
if ($n = get_record("survey_analysis", "survey", $survey->id, "userid", $user)) {
if ($n = $DB->get_record("survey_analysis", array("survey"=>$survey->id, "userid"=>$user))) {
$notes = $n->notes;
} else {
$notes = "No notes made";
Expand All @@ -235,17 +234,17 @@
$myxls->write_string($row,$col++,$u->lastname);
$myxls->write_string($row,$col++,$u->email);
$myxls->write_string($row,$col++,$u->idnumber);
$myxls->write_string($row,$col++, userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p") );
// $myxls->write_number($row,$col++,$results["$user"]["time"],$date);
$myxls->write_string($row,$col++, userdate($results[$user]["time"], "%d-%b-%Y %I:%M:%S %p") );
// $myxls->write_number($row,$col++,$results[$user]["time"],$date);
$myxls->write_string($row,$col++,$notes);

foreach ($order as $key => $qid) {
$question = $questions["$qid"];
$question = $questions[$qid];
if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
$myxls->write_string($row,$col++, $results["$user"]["$qid"]["answer1"] );
$myxls->write_string($row,$col++, $results[$user][$qid]["answer1"] );
}
if ($question->type == "2" || $question->type == "3") {
$myxls->write_string($row, $col++, $results["$user"]["$qid"]["answer2"] );
$myxls->write_string($row, $col++, $results[$user][$qid]["answer2"] );
}
}
}
Expand All @@ -267,7 +266,7 @@

echo "surveyid surveyname userid firstname lastname email idnumber time ";
foreach ($order as $key => $qid) {
$question = $questions["$qid"];
$question = $questions[$qid];
if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
echo "$question->text ";
}
Expand All @@ -280,7 +279,7 @@
// Print all the lines of data.

foreach ($results as $user => $rest) {
if (! $u = get_record("user", "id", $user)) {
if (! $u = $DB->get_record("user", array("id"=>$user))) {
print_error("Error finding student # $user");
}
echo $survey->id."\t";
Expand All @@ -290,15 +289,15 @@
echo $u->lastname."\t";
echo $u->email."\t";
echo $u->idnumber."\t";
echo userdate($results["$user"]["time"], "%d-%b-%Y %I:%M:%S %p")."\t";
echo userdate($results[$user]["time"], "%d-%b-%Y %I:%M:%S %p")."\t";

foreach ($order as $key => $qid) {
$question = $questions["$qid"];
$question = $questions[$qid];
if ($question->type == "0" || $question->type == "1" || $question->type == "3" || $question->type == "-1") {
echo $results["$user"]["$qid"]["answer1"]." ";
echo $results[$user][$qid]["answer1"]." ";
}
if ($question->type == "2" || $question->type == "3") {
echo $results["$user"]["$qid"]["answer2"]." ";
echo $results[$user][$qid]["answer2"]." ";
}
}
echo "\n";
Expand Down
20 changes: 10 additions & 10 deletions mod/survey/graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
print_error("Course Module ID was incorrect");
}

if (! $course = get_record("course", "id", $cm->course)) {
if (! $course = $DB->get_record("course", array("id"=>$cm->course))) {
print_error("Course is misconfigured");
}

Expand All @@ -31,7 +31,7 @@
}
}

if (! $survey = get_record("survey", "id", $cm->instance)) {
if (! $survey = $DB->get_record("survey", array("id"=>$cm->instance))) {
print_error("Survey ID was incorrect");
}

Expand Down Expand Up @@ -61,7 +61,7 @@

case "question.png":

$question = get_record("survey_questions", "id", $qid);
$question = $DB->get_record("survey_questions", array("id"=>$qid));
$question->text = get_string($question->text, "survey");
$question->options = get_string($question->options, "survey");

Expand All @@ -72,7 +72,7 @@
$buckets2[$key] = 0;
}

if ($aaa = get_records_select("survey_answers", "survey = '$cm->instance' AND question = '$qid'")) {
if ($aaa = $DB->get_records('survey_answers', array('survey'=>$cm->instance, 'question'=>$qid))) {
foreach ($aaa as $aa) {
if (!$group or isset($users[$aa->userid])) {
if ($a1 = $aa->answer1) {
Expand Down Expand Up @@ -129,7 +129,7 @@

case "multiquestion.png":

$question = get_record("survey_questions", "id", $qid);
$question = $DB->get_record("survey_questions", array("id"=>$qid));
$question->text = get_string($question->text, "survey");
$question->options = get_string($question->options, "survey");

Expand All @@ -149,7 +149,7 @@
$stdev2[$i] = 0;
}

$aaa = get_records_select("survey_answers", "((survey = $cm->instance) AND (question in ($question->multi)))");
$aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($question->multi)))", array($cm->instance));

if ($aaa) {
foreach ($aaa as $a) {
Expand Down Expand Up @@ -293,7 +293,7 @@
$count1[$i] = 0;
$count2[$i] = 0;
$subquestions = $question[$i]->multi; // otherwise next line doesn't work
$aaa = get_records_select("survey_answers", "((survey = $cm->instance) AND (question in ($subquestions)))");
$aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($subquestions)))", array($cm->instance));

if ($aaa) {
foreach ($aaa as $a) {
Expand Down Expand Up @@ -438,7 +438,7 @@
$stdev2[$i] = 0.0;

$subquestions = $question[$i]->multi; // otherwise next line doesn't work
$aaa = get_records_select("survey_answers","((survey = $cm->instance) AND (question in ($subquestions)))");
$aaa = $DB->get_records_select("survey_answers","((survey = ?) AND (question in ($subquestions)))", array($cm->instance));

if ($aaa) {
foreach ($aaa as $a) {
Expand Down Expand Up @@ -565,7 +565,7 @@

case "studentmultiquestion.png":

$question = get_record("survey_questions", "id", $qid);
$question = $DB->get_record("survey_questions", array("id"=>$qid));
$question->text = get_string($question->text, "survey");
$question->options = get_string($question->options, "survey");

Expand All @@ -589,7 +589,7 @@
$stdev2[$i] = 0.0;
}

$aaa = get_records_select("survey_answers", "((survey = $cm->instance) AND (question in ($question->multi)))");
$aaa = $DB->get_records_select("survey_answers", "((survey = ?) AND (question in ($question->multi)))", array($cm->instance));

if ($aaa) {
foreach ($aaa as $a) {
Expand Down
Loading

0 comments on commit deb3a60

Please sign in to comment.