Permalink
Browse files

Big clean up of survey functions and removal of deadwood (more to go …

…still though)
  • Loading branch information...
1 parent 8f9c8aa commit 551b0b9806ccfeb88d0bc43d5839820f5f886c7c martin committed Aug 1, 2002
View
@@ -1,284 +0,0 @@
-<?PHP // $Id$
- require("../../config.php");
- require("surveylib.php");
-
- require_login();
-
- print_header("Edit my surveys", "Edit my surveys", "Edit my surveys", "");
-
- if ($edit == "new") {
- include("edit_new.phtml");
- print_footer($course);
- die;
- }
-
- if ($edit == "release") {
- release_survey($id);
- unset($edit);
- }
-
- if ($edit == "update") {
- if (match_referer() && isset($HTTP_POST_VARS)) {
- $survey = (object)$HTTP_POST_VARS;
- validate_form($survey, $err);
- if (count($err) == 0) {
- update_survey($survey);
- notify("The survey \"$survey->name\" was updated.");
- unset($edit);
- }
- } else {
- notify("Error: This page was called wrongly.");
- }
- }
-
- if ($edit == "delete") {
- if ($id) {
-
- if (get_responses_for_survey($id) > 0) {
- notify("Could not delete survey as it has responses");
-
- } else if ($ss = $db->Execute("DELETE FROM surveys WHERE owner = $USER->id AND id = $id")) {
- notify("The survey was deleted.");
-
- } else {
- notify("Serious error: could not find any templates.");
- }
-
- } else {
- notify("Serious error: could not find any templates.");
- }
- unset($edit);
- }
-
-
- if ($edit == "add") {
- if ($template) {
- if ($tt = $db->Execute("SELECT * FROM surveys WHERE id = $template")) {
- $survey = (object)$tt->fields;
- $survey->owner = $USER->id;
- $survey->name = "";
- $survey->template = $template;
- add_survey($survey);
- } else {
- notify("Serious error: could not find template $template");
- }
- } else {
- unset($edit);
- }
- }
-
- if ($edit == "edit") {
- if ($id) {
- $survey = get_survey($id);
- } else {
- notify("Error: script called badly.");
- die;
- }
- }
-
- if ($edit) {
- $survey->status = get_survey_status($survey);
- include("edit_form.phtml");
-
- } else {
- clean_up_surveys();
- print_list_of_my_surveys();
- print_additional_commands();
- }
- print_footer($course);
-
-
-
-
-/// FUNCTIONS //////////////////
-
-function print_additional_commands() {
- echo "<HR><P ALIGN=center><A HREF=\"edit.php?edit=new\">Add a new survey</A></P>";
-}
-
-function validate_form(&$survey, &$err) {
-
- if (empty($survey->id)) {
- notify("A serious error occurred.");
- die;
- }
-
- if (empty($survey->name))
- $err["name"] = "Missing name";
-
- if (empty($survey->password))
- $err["password"] = "Missing password";
-
- else if ($survey->password == "changeme")
- $err["password"] = "You should change this password";
-
- settype($survey->days, "integer");
-
- if ($survey->days < 0 || $survey->days > 365)
- $err["days"] = "Must be a number between 0 and 365";
-
-
-}
-
-function add_survey(&$survey) {
-
- global $db, $USER;
-
- $timenow = time();
-
- $survey->intro = addslashes($survey->intro); // to make sure
-
- $rs = $db->Execute("INSERT INTO surveys SET
- timecreated = $timenow,
- timemodified = $timenow,
- template = '$survey->template',
- course = '$survey->course',
- owner = '$survey->owner',
- name = '$survey->name',
- password = '$survey->password',
- intro = '$survey->intro',
- url = '$survey->url',
- questions = '$survey->questions' ");
-
- if (!$rs) {
- notify("Could not insert a record");
- die;
- }
-
-// Now get it out again (most compatible way to get the id)
-
- $rs = $db->Execute("SELECT * FROM surveys WHERE owner = $survey->owner AND timecreated = $timenow");
- if ($rs) {
- $survey = (object) $rs->fields;
- $survey->intro = stripslashes($survey->intro);
- } else {
- notify("Error: Could not find the record I just inserted!");
- die;
- }
-}
-
-function release_survey($id) {
-
- global $db;
-
- if ($ss = $db->Execute("SELECT * FROM surveys WHERE id = $id")) {
- $survey = (object)$ss->fields;
- } else {
- notify("Serious error: could not find survey $id");
- die;
- }
-
- $timenow = time();
- $timeend = $timenow + ($survey->days * 86400);
-
- if ($ss = $db->Execute("UPDATE surveys SET locked=1, timeopen = $timenow, timeclose = $timeend
- WHERE id = $survey->id")) {
- notify("The survey \"$survey->name\" was released and can no longer be edited.");
- } else {
- notify("An error occurred while releasing \"$survey->name\"");
- }
-}
-
-
-function update_survey($survey) {
-
- global $db, $USER;
-
- $timenow = time();
-
- $rs = $db->Execute("UPDATE surveys SET
- timemodified = $timenow,
- name = '$survey->name',
- password = '$survey->password',
- intro = '$survey->intro',
- url = '$survey->url',
- days = $survey->days
- WHERE
- id = $survey->id AND
- owner = $USER->id ");
-
- if ($rs) {
- return true;
- } else {
- notify("Could not update the survey!");
- die;
- }
-}
-
-function get_survey($id) {
- global $db, $USER;
-
- if ($ss = $db->Execute("SELECT * FROM surveys WHERE id = $id AND owner = $USER->id")) {
- $survey = (object)$ss->fields;
- $survey->intro = stripslashes($survey->intro);
- $survey->name = stripslashes($survey->name);
- $survey->password = stripslashes($survey->password);
- return $survey;
- } else {
- notify("Serious error: could not find specified survey.");
- die;
- }
-}
-
-
-function make_survey_menu($chosenid) {
- global $db;
-
- $chosenname = get_template_name($chosenid);
- if ($ss = $db->Execute("SELECT name,id FROM surveys WHERE owner = 0 ORDER BY id")) {
- print $ss->GetMenu("template", $chosenname, true);
- } else {
- notify("Serious error: could not find any templates.");
- }
-}
-
-function clean_up_surveys() {
- global $db, $USER;
-
- if (!$rs = $db->Execute("DELETE FROM surveys WHERE owner = $USER->id AND name = ''")) {
- notify("Error: could not clean up surveys");
- }
-}
-
-
-function print_list_of_my_surveys() {
- global $db, $USER;
-
- if ($rs = $db->Execute("SELECT * FROM surveys WHERE owner = $USER->id ORDER BY id")) {
- if ($rs->RowCount()) {
- echo "<H3 ALIGN=center>Existing surveys</H3>";
- echo "<TABLE align=center cellpadding=6>";
- echo "<TR><TD><B><P>Survey name<TD><B><P>Survey type<TD><B><P>Details<TD><B><P>Status<TD><B><P></TR>";
- while (!$rs->EOF) {
- $survey = (object)$rs->fields;
-
- $numresponses = get_responses_for_survey($survey->id);
- $templatename = get_template_name($survey->template);
- $status = get_survey_status($survey);
-
- echo "<TR bgcolor=#f0f0f0>";
- echo "<TD><P><B><A HREF=\"view.php?id=$survey->id\">$survey->name</A>";
- echo "<TD><P>$templatename";
- if ($status == "editing") {
- echo "<TD><P><B><A HREF=\"edit.php?edit=edit&id=$survey->id\">Edit</A>";
- echo "<TD><P><B><FONT COLOR=#990000>$status";
- echo "<TD><P><A HREF=\"edit.php?edit=release&id=$survey->id\">Release to students</A>";
- } else {
- echo "<TD><P><B><A HREF=\"edit.php?edit=edit&id=$survey->id\">View</A>";
- echo "<TD><P><B><FONT COLOR=#009900>$status";
- echo "<TD><P><A HREF=\"report.php?id=$survey->id\">$numresponses responses</A>";
- }
- echo "</TR>";
- $rs->MoveNext();
- }
- echo "</TABLE>";
- } else {
- echo "<H3 align=center>You don't have any surveys yet</H3>";
- }
- } else {
- notify("Error: could not list surveys");
- }
-
-}
-
-?>
@@ -1,66 +0,0 @@
-<table align=center cellpadding=20> <tr> <td bgcolor=#f0f0f0>
-
-<form name="form" method="post" action="edit.php">
-<table cellpadding=5>
-<tr valign=top>
- <td><P><? link_to_popup_window("/mod/survey/docs/surveytype.html", "info", "Survey type") ?>:</td>
- <td><? print get_template_name($survey->template) ?>
- <input type="hidden" name=template value="<? p($survey->template) ?>">
- </td>
-</tr>
-<tr valign=top>
- <td><P><? link_to_popup_window("/mod/survey/docs/name.html", "info", "Survey name") ?>:</td>
- <td><input type="text" name="name" size=40 value="<? p($survey->name) ?>">
- <? formerr($err["name"]) ?>
- </td>
-</tr>
-<tr valign=top>
- <td><P><? link_to_popup_window("/mod/survey/docs/password.html", "info", "Password") ?>:</td>
- <td><input type="text" name="password" size=20 value="<? p($survey->password) ?>">
- <? formerr($err["password"]) ?>
- </td>
-</tr>
-<tr valign=top>
- <td><P><? link_to_popup_window("/mod/survey/docs/days.html", "info", "Available Days") ?>:</td>
- <td><input type="text" name="days" size=3 value="<? p($survey->days) ?>">
- <? formerr($err["days"]) ?>
- </td>
-</tr>
-<tr valign=top>
- <td><P><? link_to_popup_window("/mod/survey/docs/url.html", "info", "Course URL") ?>:</td>
- <td><input type="text" name="url" size=50 value="<? p($survey->url) ?>">
- <? formerr($err["url"]) ?>
- </td>
-</tr>
-<tr valign=top>
- <td><P><? link_to_popup_window("/mod/survey/docs/intro.html", "info", "Introduction") ?>:</td>
- <td><textarea name=intro rows=15 cols=50 wrap=virtual><? p($survey->intro) ?></textarea>
- </td>
-</tr>
-<tr>
- <td></td>
- <td>
- <table><tr><td valign=top>
- <input type="hidden" name=id value="<? p($survey->id) ?>">
- <input type="hidden" name=edit value="update">
- <? if ($survey->status == "editing") { ?>
- <input type="submit" value="Save changes">
- <? } ?>
- </FORM>
- <td valign=top>
- <? if (get_responses_for_survey($survey->id) == 0) { ?>
- <FORM name="delete" method="post" action="edit.php">
- <input type="hidden" name=id value="<? p($survey->id) ?>">
- <input type="hidden" name=edit value="delete">
- <input type="submit" value="Delete this survey">
- </FORM>
- <? } ?>
- <td valign=top>
- <FORM name="cancel" method="post" action="edit.php">
- <input type="submit" value="Cancel">
- </FORM>
- </td></tr></table>
-</table>
-
-</td></tr></table>
-
Oops, something went wrong.

0 comments on commit 551b0b9

Please sign in to comment.