From: moodler Date: Fri, 20 Feb 2004 16:03:05 +0000 (+0000) Subject: Groups support on Surveys. Phew. X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=a9ccbf60bf2a918747e186e699a1ad009ef0a555;p=moodle.git Groups support on Surveys. Phew. --- diff --git a/mod/survey/db/mysql.php b/mod/survey/db/mysql.php index 70fa9eca5a..6ed3c66515 100644 --- a/mod/survey/db/mysql.php +++ b/mod/survey/db/mysql.php @@ -172,6 +172,11 @@ function survey_upgrade($oldversion) { table_column("survey_answers", "answer2", "answer2", "text", "", "", ""); } + if ($oldversion < 2004021900) { + modify_database("", "INSERT INTO prefix_log_display VALUES ('survey', 'add', 'survey', 'name');"); + modify_database("", "INSERT INTO prefix_log_display VALUES ('survey', 'update', 'survey', 'name');"); + } + return true; } diff --git a/mod/survey/db/mysql.sql b/mod/survey/db/mysql.sql index 257c9b2e8a..16f73eb24c 100755 --- a/mod/survey/db/mysql.sql +++ b/mod/survey/db/mysql.sql @@ -178,6 +178,8 @@ INSERT INTO `prefix_survey_questions` (`id`, `text`, `shorttext`, `multi`, `intr # Dumping data for table `log_display` # +INSERT INTO prefix_log_display VALUES ('survey', 'add', 'survey', 'name'); +INSERT INTO prefix_log_display VALUES ('survey', 'update', 'survey', 'name'); INSERT INTO prefix_log_display VALUES ('survey', 'download', 'survey', 'name'); INSERT INTO prefix_log_display VALUES ('survey', 'view form', 'survey', 'name'); INSERT INTO prefix_log_display VALUES ('survey', 'view graph', 'survey', 'name'); diff --git a/mod/survey/db/postgres7.php b/mod/survey/db/postgres7.php index 94c1764224..3748daa273 100644 --- a/mod/survey/db/postgres7.php +++ b/mod/survey/db/postgres7.php @@ -19,6 +19,10 @@ function survey_upgrade($oldversion) { table_column("survey_answers", "answer1", "answer1", "text", "", "", ""); table_column("survey_answers", "answer2", "answer2", "text", "", "", ""); } + if ($oldversion < 2004021900) { + modify_database("", "INSERT INTO prefix_log_display VALUES ('survey', 'add', 'survey', 'name');"); + modify_database("", "INSERT INTO prefix_log_display VALUES ('survey', 'update', 'survey', 'name');"); + } return true; } diff --git a/mod/survey/db/postgres7.sql b/mod/survey/db/postgres7.sql index 7af7bc5cb7..e86549ce8d 100755 --- a/mod/survey/db/postgres7.sql +++ b/mod/survey/db/postgres7.sql @@ -172,6 +172,8 @@ INSERT INTO prefix_survey_questions (id, text, shorttext, multi, intro, type, op # Dumping data for table log_display # +INSERT INTO prefix_log_display VALUES ('survey', 'add', 'survey', 'name'); +INSERT INTO prefix_log_display VALUES ('survey', 'update', 'survey', 'name'); INSERT INTO prefix_log_display VALUES ('survey', 'download', 'survey', 'name'); INSERT INTO prefix_log_display VALUES ('survey', 'view form', 'survey', 'name'); INSERT INTO prefix_log_display VALUES ('survey', 'view graph', 'survey', 'name'); diff --git a/mod/survey/details.php b/mod/survey/details.php index 1ac72bffd0..5ca79fedbe 100644 --- a/mod/survey/details.php +++ b/mod/survey/details.php @@ -15,10 +15,12 @@ } $streditingasurvey = get_string("editingasurvey", "survey"); + $strsurveys = get_string("modulenameplural", "survey"); print_header("$course->shortname: $streditingasurvey", "$course->fullname", - "wwwroot/course/view.php?id=$course->id\">$course->shortname - -> $streditingasurvey"); + "wwwroot/course/view.php?id=$course->id\">$course->shortname". + " -> id\">$strsurveys". + " -> $form->name ($streditingasurvey)"); if (!$form->name or !$form->template) { error(get_string("filloutallfields"), $_SERVER["HTTP_REFERER"]); diff --git a/mod/survey/download.php b/mod/survey/download.php index 4c4d7c2e43..854c49a8e7 100644 --- a/mod/survey/download.php +++ b/mod/survey/download.php @@ -6,6 +6,7 @@ require_variable($id); // Course Module ID optional_variable($type, "xls"); + optional_variable($group, 0); if (! $cm = get_record("course_modules", "id", $id)) { error("Course Module ID was incorrect"); @@ -25,8 +26,18 @@ error("Survey ID was incorrect"); } - add_to_log($course->id, "survey", "download", "download.php?id=$cm->id&type=$type", "$survey->id"); + add_to_log($course->id, "survey", "download", "download.php?id=$cm->id&type=$type", "$survey->id", $cm->id); +/// Check to see if groups are being used in this survey + + $groupmode = groupmode($course, $cm); // Groups are being used + + if ($groupmode and $group) { + $users = get_users_in_group($group); + } else { + $users = get_course_users($course->id); + $group = false; + } // Get all the questions and their proper order @@ -94,15 +105,17 @@ } foreach ($aaa as $a) { - if (!$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 (!$group or isset($users[$a->userid])) { + if (!$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"] = ""; + } } + $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; } // Output the file as a valid Excel spreadsheet if required diff --git a/mod/survey/graph.php b/mod/survey/graph.php index fed1e359a3..9bbd5b214c 100644 --- a/mod/survey/graph.php +++ b/mod/survey/graph.php @@ -6,6 +6,7 @@ require_variable($id); // Course Module ID require_variable($type); // Graph Type + optional_variable($group, "0"); // Group ID optional_variable($sid); // Student ID if (! $cm = get_record("course_modules", "id", $id)) { @@ -18,8 +19,12 @@ require_login($course->id); - if (!isteacher($course->id) && !isadmin()) { - if (! ($type == "student.png" && $sid == $USER->id) ) { + $groupmode = groupmode($course, $cm); // Groups are being used + + if (!isteacher($course->id)) { + if ($type != "student.png" or $sid != $USER->id ) { + error("Sorry, you aren't allowed to see this."); + } else if ($groupmode and !ismember($group)) { error("Sorry, you aren't allowed to see this."); } } @@ -28,6 +33,14 @@ error("Survey ID was incorrect"); } +/// Check to see if groups are being used in this survey + if ($groupmode and $group) { + $users = get_users_in_group($group); + } else { + $users = get_course_users($course->id); + $group = false; + } + $stractual = get_string("actual", "survey"); $stractualclass = get_string("actualclass", "survey"); $stractualstudent = get_string("actualstudent", "survey", $course->student); @@ -53,11 +66,13 @@ if ($aaa = get_records_select("survey_answers", "survey = '$cm->instance' AND question = '$qid'")) { foreach ($aaa as $aa) { - if ($a1 = $aa->answer1) { - $buckets1[$a1 - 1]++; - } - if ($a2 = $aa->answer2) { - $buckets2[$a2 - 1]++; + if (!$group or isset($users[$aa->userid])) { + if ($a1 = $aa->answer1) { + $buckets1[$a1 - 1]++; + } + if ($a2 = $aa->answer2) { + $buckets2[$a2 - 1]++; + } } } } @@ -127,14 +142,16 @@ if ($aaa) { foreach ($aaa as $a) { - $index = $indexof[$a->question]; - if ($a->answer1) { - $buckets1[$index] += $a->answer1; - $count1[$index]++; - } - if ($a->answer2) { - $buckets2[$index] += $a->answer2; - $count2[$index]++; + if (!$group or isset($users[$a->userid])) { + $index = $indexof[$a->question]; + if ($a->answer1) { + $buckets1[$index] += $a->answer1; + $count1[$index]++; + } + if ($a->answer2) { + $buckets2[$index] += $a->answer2; + $count2[$index]++; + } } } } @@ -150,14 +167,16 @@ if ($aaa) { foreach ($aaa as $a) { - $index = $indexof[$a->question]; - if ($a->answer1) { - $difference = (float) ($a->answer1 - $buckets1[$index]); - $stdev1[$index] += ($difference * $difference); - } - if ($a->answer2) { - $difference = (float) ($a->answer2 - $buckets2[$index]); - $stdev2[$index] += ($difference * $difference); + if (!$group or isset($users[$a->userid])) { + $index = $indexof[$a->question]; + if ($a->answer1) { + $difference = (float) ($a->answer1 - $buckets1[$index]); + $stdev1[$index] += ($difference * $difference); + } + if ($a->answer2) { + $difference = (float) ($a->answer2 - $buckets2[$index]); + $stdev2[$index] += ($difference * $difference); + } } } } @@ -266,13 +285,15 @@ if ($aaa) { foreach ($aaa as $a) { - if ($a->answer1) { - $buckets1[$i] += $a->answer1; - $count1[$i]++; - } - if ($a->answer2) { - $buckets2[$i] += $a->answer2; - $count2[$i]++; + if (!$group or isset($users[$a->userid])) { + if ($a->answer1) { + $buckets1[$i] += $a->answer1; + $count1[$i]++; + } + if ($a->answer2) { + $buckets2[$i] += $a->answer2; + $count2[$i]++; + } } } } @@ -287,13 +308,15 @@ // Calculate the standard devaiations if ($aaa) { foreach ($aaa as $a) { - if ($a->answer1) { - $difference = (float) ($a->answer1 - $buckets1[$i]); - $stdev1[$i] += ($difference * $difference); - } - if ($a->answer2) { - $difference = (float) ($a->answer2 - $buckets2[$i]); - $stdev2[$i] += ($difference * $difference); + if (!$group or isset($users[$a->userid])) { + if ($a->answer1) { + $difference = (float) ($a->answer1 - $buckets1[$i]); + $stdev1[$i] += ($difference * $difference); + } + if ($a->answer2) { + $difference = (float) ($a->answer2 - $buckets2[$i]); + $stdev2[$i] += ($difference * $difference); + } } } } @@ -406,24 +429,26 @@ if ($aaa) { foreach ($aaa as $a) { - if ($a->userid == $sid) { + if (!$group or isset($users[$a->userid])) { + if ($a->userid == $sid) { + if ($a->answer1) { + $studbuckets1[$i] += $a->answer1; + $studcount1[$i]++; + } + if ($a->answer2) { + $studbuckets2[$i] += $a->answer2; + $studcount2[$i]++; + } + } if ($a->answer1) { - $studbuckets1[$i] += $a->answer1; - $studcount1[$i]++; + $buckets1[$i] += $a->answer1; + $count1[$i]++; } if ($a->answer2) { - $studbuckets2[$i] += $a->answer2; - $studcount2[$i]++; + $buckets2[$i] += $a->answer2; + $count2[$i]++; } } - if ($a->answer1) { - $buckets1[$i] += $a->answer1; - $count1[$i]++; - } - if ($a->answer2) { - $buckets2[$i] += $a->answer2; - $count2[$i]++; - } } } @@ -442,13 +467,15 @@ // Calculate the standard devaiations foreach ($aaa as $a) { - if ($a->answer1) { - $difference = (float) ($a->answer1 - $buckets1[$i]); - $stdev1[$i] += ($difference * $difference); - } - if ($a->answer2) { - $difference = (float) ($a->answer2 - $buckets2[$i]); - $stdev2[$i] += ($difference * $difference); + if (!$group or isset($users[$a->userid])) { + if ($a->answer1) { + $difference = (float) ($a->answer1 - $buckets1[$i]); + $stdev1[$i] += ($difference * $difference); + } + if ($a->answer2) { + $difference = (float) ($a->answer2 - $buckets2[$i]); + $stdev2[$i] += ($difference * $difference); + } } } @@ -553,24 +580,26 @@ if ($aaa) { foreach ($aaa as $a) { - $index = $indexof[$a->question]; - if ($a->userid == $sid) { - if ($a->answer1) { - $studbuckets1[$index] += $a->answer1; - $studcount1[$index]++; - } - if ($a->answer2) { - $studbuckets2[$index] += $a->answer2; - $studcount2[$index]++; + if (!$group or isset($users[$a->userid])) { + $index = $indexof[$a->question]; + if ($a->userid == $sid) { + if ($a->answer1) { + $studbuckets1[$index] += $a->answer1; + $studcount1[$index]++; + } + if ($a->answer2) { + $studbuckets2[$index] += $a->answer2; + $studcount2[$index]++; + } } + if ($a->answer1) { + $buckets1[$index] += $a->answer1; + $count1[$index]++; + } + if ($a->answer2) { + $buckets2[$index] += $a->answer2; + $count2[$index]++; } - if ($a->answer1) { - $buckets1[$index] += $a->answer1; - $count1[$index]++; - } - if ($a->answer2) { - $buckets2[$index] += $a->answer2; - $count2[$index]++; } } } @@ -591,14 +620,16 @@ } foreach ($aaa as $a) { - $index = $indexof[$a->question]; - if ($a->answer1) { - $difference = (float) ($a->answer1 - $buckets1[$index]); - $stdev1[$index] += ($difference * $difference); - } - if ($a->answer2) { - $difference = (float) ($a->answer2 - $buckets2[$index]); - $stdev2[$index] += ($difference * $difference); + if (!$group or isset($users[$a->userid])) { + $index = $indexof[$a->question]; + if ($a->answer1) { + $difference = (float) ($a->answer1 - $buckets1[$index]); + $stdev1[$index] += ($difference * $difference); + } + if ($a->answer2) { + $difference = (float) ($a->answer2 - $buckets2[$index]); + $stdev2[$index] += ($difference * $difference); + } } } diff --git a/mod/survey/lib.php b/mod/survey/lib.php index 2844f7f59d..5f35efee79 100644 --- a/mod/survey/lib.php +++ b/mod/survey/lib.php @@ -185,15 +185,23 @@ function survey_log_info($log) { AND u.id = '$log->userid'"); } -function survey_get_responses($survey) { +function survey_get_responses($surveyid, $groupid) { global $CFG; + + if ($groupid) { + $groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid"; + } else { + $groupsql = ""; + } + return get_records_sql("SELECT MAX(a.time) as time, count(*) as numanswers, u.id, u.firstname, u.lastname, u.picture FROM {$CFG->prefix}survey_answers AS a, - {$CFG->prefix}user AS u - WHERE a.survey = $survey - AND a.userid = u.id + {$CFG->prefix}user AS u, + {$CFG->prefix}groups_members AS gm + WHERE a.survey = $surveyid + AND a.userid = u.id $groupsql GROUP BY u.id, u.firstname, u.lastname ORDER BY time ASC"); } @@ -217,15 +225,22 @@ function survey_update_analysis($survey, $user, $notes) { } -function survey_get_user_answers($surveyid, $questionid, $sort="sa.answer1,sa.answer2 ASC") { +function survey_get_user_answers($surveyid, $questionid, $groupid, $sort="sa.answer1,sa.answer2 ASC") { global $CFG; + if ($groupid) { + $groupsql = "AND gm.groupid = $groupid AND u.id = gm.userid"; + } else { + $groupsql = ""; + } + return get_records_sql("SELECT sa.*,u.firstname,u.lastname,u.picture FROM {$CFG->prefix}survey_answers sa, - {$CFG->prefix}user u + {$CFG->prefix}user u, + {$CFG->prefix}groups_members gm WHERE sa.survey = '$surveyid' AND sa.question = $questionid - AND u.id = sa.userid + AND u.id = sa.userid $groupsql ORDER BY $sort"); } @@ -255,8 +270,8 @@ function survey_already_done($survey, $user) { return record_exists("survey_answers", "survey", $survey, "userid", $user); } -function survey_count_responses($survey) { - if ($responses = survey_get_responses($survey)) { +function survey_count_responses($surveyid, $groupid) { + if ($responses = survey_get_responses($surveyid, $groupid)) { return count($responses); } else { return 0; @@ -439,8 +454,8 @@ function survey_print_graph($url) { echo "(".get_string("gdneed").")"; } else { - echo "wwwroot/mod/survey/graph.php?$url\">"; + echo "wwwroot/mod/survey/graph.php?$url\">"; } } diff --git a/mod/survey/report.php b/mod/survey/report.php index 035e16e959..4da44c863b 100644 --- a/mod/survey/report.php +++ b/mod/survey/report.php @@ -5,8 +5,9 @@ // Check that all the parameters have been provided. - require_variable($id); // Course Module ID - optional_variable($action, "students"); // What to look at + require_variable($id); // Course Module ID + optional_variable($action, ""); // What to look at + optional_variable($qid, "0"); // Question id if (! $cm = get_record("course_modules", "id", $id)) { error("Course Module ID was incorrect"); @@ -62,6 +63,20 @@ "", "", true, update_module_button($cm->id, $course->id, $strsurvey), navmenu($course, $cm)); +/// Check to see if groups are being used in this survey + if ($groupmode = groupmode($course, $cm)) { // Groups are being used + $currentgroup = setup_and_print_groups($course, $groupmode, + "report.php?id=$cm->id&action=$action&qid=$qid"); + } else { + $currentgroup = 0; + } + + if ($currentgroup) { + $users = get_users_in_group($currentgroup); + } else { + $users = get_course_users($course->id); + } + print_simple_box_start("center"); if ($showscales) { echo "$strsummary"; @@ -69,10 +84,16 @@ echo "    $strquestions"; echo "    $course->students"; echo "    $strdownload"; + if (empty($action)) { + $action = "summary"; + } } else { echo "$strquestions"; echo "    $course->students"; echo "    $strdownload"; + if (empty($action)) { + $action = "questions"; + } } print_simple_box_end(); @@ -86,9 +107,9 @@ case "summary": print_heading($strsummary); - if (survey_count_responses($survey->id)) { + if (survey_count_responses($survey->id, $currentgroup)) { echo "

"; - survey_print_graph("id=$id&type=overall.png"); + survey_print_graph("id=$id&group=$currentgroup&type=overall.png"); echo ""; } else { echo "

".get_string("nobodyyet","survey")."

"; @@ -116,7 +137,7 @@ continue; } echo "

multi>"; - survey_print_graph("id=$id&qid=$question->id&type=multiquestion.png"); + survey_print_graph("id=$id&qid=$question->id&group=$currentgroup&type=multiquestion.png"); echo "


"; } } @@ -169,14 +190,14 @@ if ($subquestion->type > 0) { echo "

"; echo "id\">"; - survey_print_graph("id=$id&qid=$subquestion->id&type=question.png"); + survey_print_graph("id=$id&qid=$subquestion->id&group=$currentgroup&type=question.png"); echo "

"; } } } else if ($question->type > 0 ) { echo "

"; echo "id\">"; - survey_print_graph("id=$id&qid=$question->id&type=question.png"); + survey_print_graph("id=$id&qid=$question->id&group=$currentgroup&type=question.png"); echo "

"; } else { @@ -186,7 +207,7 @@ $contents = ''; - if ($aaa = survey_get_user_answers($survey->id, $question->id, "sa.time ASC")) { + if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup, "sa.time ASC")) { foreach ($aaa as $a) { $contents .= ""; $contents .= ''; @@ -227,7 +248,7 @@ $table->align = array ("left", "left", "left", "left", "right"); $table->size = array (35, "", "", "", ""); - if ($aaa = survey_get_user_answers($survey->id, $question->id)) { + if ($aaa = survey_get_user_answers($survey->id, $question->id, $currentgroup)) { foreach ($aaa as $a) { if ($a->answer1) { $answer1 = "$a->answer1 - ".$answers[$a->answer1 - 1]; @@ -257,7 +278,7 @@ print_heading(get_string("analysisof", "survey", "$course->students")); - if (! $results = survey_get_responses($survey->id) ) { + if (! $results = survey_get_responses($survey->id, $currentgroup) ) { notify(get_string("nobodyyet","survey")); } else { survey_print_all_responses($cm->id, $results, $course->id); @@ -373,6 +394,7 @@ echo '
'; $options["id"] = "$cm->id"; $options["type"] = "xls"; + $options["group"] = $currentgroup; print_single_button("download.php", $options, get_string("downloadexcel", "survey")); $options["type"] = "txt"; diff --git a/mod/survey/save.php b/mod/survey/save.php index 8f696016d2..6098088711 100644 --- a/mod/survey/save.php +++ b/mod/survey/save.php @@ -90,8 +90,8 @@ $strsurveysaved = get_string("surveysaved", "survey"); print_header("$course->shortname: $strsurveysaved", "$course->fullname", - "id>$course->shortname -> - id>$strsurveys -> $survey->name -> $strsurveysaved", ""); + "wwwroot/course/view.php?id=$course->id\">$course->shortname -> + id\">$strsurveys -> $survey->name -> $strsurveysaved", ""); notice(get_string("thanksforanswers","survey", $USER->firstname), "$CFG->wwwroot/course/view.php?id=$course->id"); diff --git a/mod/survey/version.php b/mod/survey/version.php index daa60c1cf1..9bea028409 100644 --- a/mod/survey/version.php +++ b/mod/survey/version.php @@ -5,7 +5,7 @@ // This fragment is called by /admin/index.php //////////////////////////////////////////////////////////////////////////////// -$module->version = 2004021602; +$module->version = 2004021900; $module->requires = 2004013101; // Requires this Moodle version $module->cron = 0; diff --git a/mod/survey/view.php b/mod/survey/view.php index b782bbc5b7..a734920ea8 100644 --- a/mod/survey/view.php +++ b/mod/survey/view.php @@ -36,8 +36,19 @@ "$navigation id>$strsurveys -> $survey->name", "", "", true, update_module_button($cm->id, $course->id, $strsurvey), navmenu($course, $cm)); +/// Check to see if groups are being used in this survey + if ($groupmode = groupmode($course, $cm)) { // Groups are being used + $currentgroup = get_current_group($course->id); + } else { + $currentgroup = 0; + } + + if (isteacheredit($course->id) or ($groupmode == VISIBLEGROUPS)) { + $currentgroup = 0; + } + if (isteacher($course->id)) { - $numusers = survey_count_responses($survey->id); + $numusers = survey_count_responses($survey->id, $currentgroup); echo "

id\">". get_string("viewsurveyresponses", "survey", $numusers)."

"; } else if (!$cm->visible) { @@ -54,13 +65,13 @@ if (survey_already_done($survey->id, $USER->id)) { add_to_log($course->id, "survey", "view graph", "view.php?id=$cm->id", $survey->id, $cm->id); - $numusers = survey_count_responses($survey->id); + $numusers = survey_count_responses($survey->id, $currentgroup); if ($showscales) { print_heading(get_string("surveycompleted", "survey")); print_heading(get_string("peoplecompleted", "survey", $numusers)); echo "
"; - survey_print_graph("id=$cm->id&sid=$USER->id&type=student.png"); + survey_print_graph("id=$cm->id&sid=$USER->id&group=$currentgroup&type=student.png"); echo "
"; } else {
'.fullname($a).'