]> git.mjollnir.org Git - moodle.git/commitdiff
More cleanups to survey - renamed/moved functions, modernised mod.php
authormartin <martin>
Thu, 1 Aug 2002 05:18:07 +0000 (05:18 +0000)
committermartin <martin>
Thu, 1 Aug 2002 05:18:07 +0000 (05:18 +0000)
mod/survey/graph.php
mod/survey/lib.php
mod/survey/mod.php
mod/survey/view.php

index d8e34b1adbedbea9f6b89524778d67efbc6496d9..368cae985ee8c10b3c91247d6bc73522bb48649b 100644 (file)
        $qqq = get_records_sql("SELECT * FROM survey_questions WHERE id in ($question->multi)");
 
        foreach ($questionorder as $i => $val) {
-           $names[$i] = shorten_name($qqq["$val"]->text, 4);
+           $names[$i] = survey_shorten_name($qqq["$val"]->text, 4);
            $buckets1[$i] = 0;
            $buckets2[$i] = 0;
            $count1[$i] = 0;
        $qqq = get_records_sql("SELECT * FROM survey_questions WHERE id in ($question->multi)");
 
        foreach ($questionorder as $i => $val) {
-           $names[$i] = shorten_name($qqq["$val"]->text, 4);
+           $names[$i] = survey_shorten_name($qqq["$val"]->text, 4);
            $buckets1[$i] = 0;
            $buckets2[$i] = 0;
            $count1[$i] = 0;
 
    exit;
 
-function shorten_name ($name, $numwords) {
-    $words = explode(" ", $name);
-    for ($i=0; $i < $numwords; $i++) {
-        $output .= $words[$i]." ";
-    }
-    return $output;
-}
          
 ?>
index 0811b50f192b3ebb2a46956435a90b48d4391c19..c4f0856eb1fe6e750a904e1e2b8a23fc44d5f668 100644 (file)
@@ -103,6 +103,13 @@ function survey_add_analysis($survey, $user, $notes) {
     return $db->Execute("INSERT INTO survey_analysis SET notes='$notes', survey='$survey', user='$user'");
 }
 
+function survey_shorten_name ($name, $numwords) {
+    $words = explode(" ", $name);
+    for ($i=0; $i < $numwords; $i++) {
+        $output .= $words[$i]." ";
+    }
+    return $output;
+}
 
 function survey_user_summary($course, $user, $mod, $survey) {
     global $CFG;
@@ -132,4 +139,125 @@ function survey_user_complete($course, $user, $mod, $survey) {
     }
 }
 
+
+function survey_print_multi($question) {
+    GLOBAL $db, $qnum, $checklist, $THEME;
+
+
+    echo "<P>&nbsp</P>\n";
+       echo "<P><FONT SIZE=4><B>$question->text</B></FONT></P>";
+
+       echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=1 BORDER=0>";
+
+    $options = explode( ",", $question->options);
+    $numoptions = count($options);
+
+    $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
+       if ($question->type == 2) {
+               $P = "P";
+       } else {
+               $P = "";
+       }
+   
+    if ($oneanswer) { 
+        echo "<TR WIDTH=100% ><TD COLSPAN=2><P>$question->intro</P></TD>";
+    } else {
+        echo "<TR WIDTH=100% ><TD COLSPAN=3><P>$question->intro</P></TD>"; 
+    }
+
+    while (list ($key, $val) = each ($options)) {
+        echo "<TD width=10% ALIGN=CENTER><FONT SIZE=1><P>$val</P></FONT></TD>\n";
+    }
+    echo "<TD ALIGN=CENTER BGCOLOR=\"$THEME->body\">&nbsp</TD></TR>\n";
+
+    $subquestions = get_records_sql("SELECT * FROM survey_questions WHERE id in ($question->multi) ");
+
+    foreach ($subquestions as $q) {
+        $qnum++;
+        $bgcolor = survey_question_color($qnum);
+
+        echo "<TR BGCOLOR=$bgcolor>";
+        if ($oneanswer) {
+            echo "<TD WIDTH=10 VALIGN=top><P><B>$qnum</B></P></TD>";
+            echo "<TD VALIGN=top><P>$q->text</P></TD>";
+            for ($i=1;$i<=$numoptions;$i++) {
+                echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$P$q->id VALUE=$i></TD>";
+            }
+            echo "<TD BGCOLOR=white><INPUT TYPE=radio NAME=q$P$q->id VALUE=0 checked></TD>";
+            $checklist["q$P$q->id"] = $numoptions;
+        
+        } else {
+            echo "<TD WIDTH=10 VALIGN=middle rowspan=2><P><B>$qnum</B></P></TD>";
+            echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>I prefer that&nbsp;</FONT></P></TD>";
+            echo "<TD WIDTH=40% VALIGN=middle rowspan=2><P>$q->text</P></TD>";
+            for ($i=1;$i<=$numoptions;$i++) {
+                echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=qP$q->id VALUE=$i></TD>";
+            }
+            echo "<TD BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=qP$q->id VALUE=0 checked></TD>";
+            echo "</TR>";
+
+            echo "<TR BGCOLOR=$bgcolor>";
+            echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>I found that&nbsp;</P></TD>";
+            for ($i=1;$i<=$numoptions;$i++) {
+                echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$q->id VALUE=$i></TD>";
+            }
+            echo "<TD WIDTH=5% BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=q$q->id VALUE=0 checked></TD>";
+            $checklist["qP$q->id"] = $numoptions;
+            $checklist["q$q->id"] = $numoptions;
+        }
+        echo "</TR>\n";
+    }
+    echo "</TABLE>";
+}
+
+
+
+function survey_print_single($question) {
+    GLOBAL $db, $qnum;
+
+    $bgcolor = survey_question_color(0);
+
+    $qnum++;
+
+    echo "<P>&nbsp</P>\n";
+    echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=0>\n";
+    echo "<TR BGCOLOR=$bgcolor>";
+    echo "<TD VALIGN=top><B>$qnum</B></TD>";
+    echo "<TD WIDTH=50% VALIGN=top><P>$question->text</P></TD>\n";
+    echo "<TD WIDTH=50% VALIGN=top><P><FONT SIZE=+1>\n";
+
+
+    if ($question->type == 0) {           // Plain text field
+        echo "<TEXTAREA ROWS=3 COLS=30 WRAP=virtual NAME=\"$question->id\">$question->options</TEXTAREA>";
+
+    } else if ($question->type > 0) {     // Choose one of a number
+        echo "<SELECT NAME=$question->id>";
+        echo "<OPTION VALUE=0 SELECTED>Choose...</OPTION>";
+        $options = explode( ",", $question->options);
+        foreach ($options as $key => $val) {
+            $key++;
+            echo "<OPTION VALUE=\"$key\">$val</OPTION>";
+        }
+        echo "</SELECT>";
+
+    } else if ($question->type < 0) {     // Choose several of a number
+        $options = explode( ",", $question->options);
+        echo "<P>THIS TYPE OF QUESTION NOT SUPPORTED YET</P>";
+    }
+
+    echo "</FONT></TD></TR></TABLE>";
+
+}
+
+function survey_question_color($qnum) {
+    global $THEME;
+
+    if ($qnum) {
+        return $qnum % 2 ? $THEME->cellcontent : $THEME->cellcontent2;
+        //return $qnum % 2 ? "#CCFFCC" : "#CCFFFF";
+    } else {
+        return $THEME->cellcontent;
+    }
+}
+
 ?>
index 973bff3771b16a59cd71b8e686f7b5c2c91e244b..9534d546c5520780b04db7a68d087ec0ba05768c 100644 (file)
@@ -14,34 +14,17 @@ function add_instance($survey) {
 // (defined by the form in mod.html) this function 
 // will create a new instance and return the id number 
 // of the new instance.
-//
-    GLOBAL $db;
-
-    $timenow = time();
 
     if (!$template = get_record("survey", "id", $survey->template)) {
         return 0;
     }
 
-    if (!$rs = $db->Execute("INSERT into survey
-                                SET course    = '$survey->course', 
-                                    name      = '$survey->name',
-                                    days      = '$survey->days',
-                                    intro     = '$survey->intro',
-                                    template  = '$template->id',
-                                    questions = '$template->questions',
-                                    timecreated = '$timenow',
-                                    timemodified = '$timenow' ")) {
-        return 0;
-    }
-    
-    // Get it out again - this is the most compatible way to determine the ID
-    if ($rs = $db->Execute("SELECT id FROM survey
-                            WHERE course = $survey->course AND timemodified = '$timenow'")) {
-        return $rs->fields[0];
-    } else {
-        return 0;
-    }
+    $survey->questions    = $template->questions; 
+    $survey->timecreated  = time();
+    $survey->timemodified = $survey->timecreated;
+
+    return insert_record("survey", $survey);
+
 }
 
 
@@ -49,42 +32,42 @@ function update_instance($survey) {
 // Given an object containing all the necessary data, 
 // (defined by the form in mod.html) this function 
 // will update an existing instance with new data.
-//
-    GLOBAL $db;
-
-    $timenow = time();
 
     if (!$template = get_record("survey", "id", $survey->template)) {
         return 0;
     }
 
-    if (!$rs = $db->Execute("UPDATE survey
-                                SET name         = '$survey->name',
-                                    days         = '$survey->days',
-                                    intro        = '$survey->intro',
-                                    template     = '$template->id',
-                                    questions    = '$template->questions',
-                                    timemodified = '$timenow'
-                              WHERE id = '$survey->instance' ")) {
-        return false;
-    }
-    return true;
-}
+    $survey->id           = $survey->instance; 
+    $survey->questions    = $template->questions; 
+    $survey->timemodified = time();
 
+    return update_record("survey", $survey);
+}
 
 function delete_instance($id) {
 // Given an ID of an instance of this module, 
 // this function will permanently delete the instance 
 // and any data that depends on it.  
-//
-    GLOBAL $db;
 
-    if (!$rs = $db->Execute("DELETE from survey WHERE id = '$id' ")) {
+    if (! $survey = get_record("survey", "id", "$id")) {
         return false;
     }
 
-    return true;
-    
+    $result = true;
+
+    if (! delete_records("survey_analysis", "survey", "$survey->id")) {
+        $result = false;
+    }
+
+    if (! delete_records("survey_answers", "survey", "$survey->id")) {
+        $result = false;
+    }
+
+    if (! delete_records("survey", "id", "$survey->id")) {
+        $result = false;
+    }
+
+    return $result;
 }
 
 
index 5d010f9340bfe8563db36916e176424a2f258efb..53a75ebf09d0f707c0f577e5cd772a4502acba99 100644 (file)
@@ -19,7 +19,6 @@
         error("Survey ID was incorrect");
     }
 
-
     print_header("$course->shortname: $survey->name", "$course->fullname",
                  "<A HREF=../../course/view.php?id=$course->id>$course->shortname</A> ->
                   <A HREF=index.php?id=$course->id>Surveys</A> -> $survey->name", "", "", true,
@@ -67,9 +66,9 @@
         
         if ($question->type > 0) {
             if ($question->multi) {
-                print_multi($question);
+                survey_print_multi($question);
             } else {
-                print_single($question);
+                survey_print_single($question);
             }
         }
     }
@@ -119,130 +118,4 @@ document.write('<INPUT TYPE="button" VALUE="Click here to check and continue" on
 
    print_footer($course);
 
-   exit;
-
-
-//////////////////////////////////////////////////////////////////////////////////////
-
-
-function print_multi($question) {
-    GLOBAL $db, $qnum, $checklist, $THEME;
-
-
-    echo "<P>&nbsp</P>\n";
-       echo "<P><FONT SIZE=4><B>$question->text</B></FONT></P>";
-
-       echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=1 BORDER=0>";
-
-    $options = explode( ",", $question->options);
-    $numoptions = count($options);
-
-    $oneanswer = ($question->type == 1 || $question->type == 2) ? true : false;
-       if ($question->type == 2) {
-               $P = "P";
-       } else {
-               $P = "";
-       }
-   
-    if ($oneanswer) { 
-        echo "<TR WIDTH=100% ><TD COLSPAN=2><P>$question->intro</P></TD>";
-    } else {
-        echo "<TR WIDTH=100% ><TD COLSPAN=3><P>$question->intro</P></TD>"; 
-    }
-
-    while (list ($key, $val) = each ($options)) {
-        echo "<TD width=10% ALIGN=CENTER><FONT SIZE=1><P>$val</P></FONT></TD>\n";
-    }
-    echo "<TD ALIGN=CENTER BGCOLOR=\"$THEME->body\">&nbsp</TD></TR>\n";
-
-    $subquestions = get_records_sql("SELECT * FROM survey_questions WHERE id in ($question->multi) ");
-
-    foreach ($subquestions as $q) {
-        $qnum++;
-        $bgcolor = question_color($qnum);
-
-        echo "<TR BGCOLOR=$bgcolor>";
-        if ($oneanswer) {
-            echo "<TD WIDTH=10 VALIGN=top><P><B>$qnum</B></P></TD>";
-            echo "<TD VALIGN=top><P>$q->text</P></TD>";
-            for ($i=1;$i<=$numoptions;$i++) {
-                echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$P$q->id VALUE=$i></TD>";
-            }
-            echo "<TD BGCOLOR=white><INPUT TYPE=radio NAME=q$P$q->id VALUE=0 checked></TD>";
-            $checklist["q$P$q->id"] = $numoptions;
-        
-        } else {
-            echo "<TD WIDTH=10 VALIGN=middle rowspan=2><P><B>$qnum</B></P></TD>";
-            echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>I prefer that&nbsp;</FONT></P></TD>";
-            echo "<TD WIDTH=40% VALIGN=middle rowspan=2><P>$q->text</P></TD>";
-            for ($i=1;$i<=$numoptions;$i++) {
-                echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=qP$q->id VALUE=$i></TD>";
-            }
-            echo "<TD BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=qP$q->id VALUE=0 checked></TD>";
-            echo "</TR>";
-
-            echo "<TR BGCOLOR=$bgcolor>";
-            echo "<TD WIDTH=10% NOWRAP><P><FONT SIZE=1>I found that&nbsp;</P></TD>";
-            for ($i=1;$i<=$numoptions;$i++) {
-                echo "<TD WIDTH=10% ALIGN=CENTER><INPUT TYPE=radio NAME=q$q->id VALUE=$i></TD>";
-            }
-            echo "<TD WIDTH=5% BGCOLOR=\"$THEME->body\"><INPUT TYPE=radio NAME=q$q->id VALUE=0 checked></TD>";
-            $checklist["qP$q->id"] = $numoptions;
-            $checklist["q$q->id"] = $numoptions;
-        }
-        echo "</TR>\n";
-    }
-    echo "</TABLE>";
-}
-
-
-
-function print_single($question) {
-    GLOBAL $db, $qnum;
-
-    $bgcolor = question_color(0);
-
-    $qnum++;
-
-    echo "<P>&nbsp</P>\n";
-    echo "<TABLE ALIGN=CENTER WIDTH=90% CELLPADDING=4 CELLSPACING=0>\n";
-    echo "<TR BGCOLOR=$bgcolor>";
-    echo "<TD VALIGN=top><B>$qnum</B></TD>";
-    echo "<TD WIDTH=50% VALIGN=top><P>$question->text</P></TD>\n";
-    echo "<TD WIDTH=50% VALIGN=top><P><FONT SIZE=+1>\n";
-
-
-    if ($question->type == 0) {           // Plain text field
-        echo "<TEXTAREA ROWS=3 COLS=30 WRAP=virtual NAME=\"$question->id\">$question->options</TEXTAREA>";
-
-    } else if ($question->type > 0) {     // Choose one of a number
-        echo "<SELECT NAME=$question->id>";
-        echo "<OPTION VALUE=0 SELECTED>Choose...</OPTION>";
-        $options = explode( ",", $question->options);
-        foreach ($options as $key => $val) {
-            $key++;
-            echo "<OPTION VALUE=\"$key\">$val</OPTION>";
-        }
-        echo "</SELECT>";
-
-    } else if ($question->type < 0) {     // Choose several of a number
-        $options = explode( ",", $question->options);
-        echo "<P>THIS TYPE OF QUESTION NOT SUPPORTED YET</P>";
-    }
-
-    echo "</FONT></TD></TR></TABLE>";
-
-}
-
-function question_color($qnum) {
-    global $THEME;
-
-    if ($qnum) {
-        return $qnum % 2 ? $THEME->cellcontent : $THEME->cellcontent2;
-        //return $qnum % 2 ? "#CCFFCC" : "#CCFFFF";
-    } else {
-        return $THEME->cellcontent;
-    }
-}
-
 ?>