From 90ca2ce87d79be8f6bf472fabb1c9389b443b5a8 Mon Sep 17 00:00:00 2001 From: danmarsden Date: Thu, 23 Feb 2006 21:20:44 +0000 Subject: [PATCH] fixed bug with limits not limiting for only enrolled students - limit was counting non-enrolled students as well. - also removed checkboxes to delete response from not-answered column. --- mod/choice/lib.php | 47 ++++++++++++++++++++++++++++++------------- mod/choice/report.php | 3 ++- mod/choice/view.php | 2 +- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/mod/choice/lib.php b/mod/choice/lib.php index dd3ba82d03a5..c9ecdf01939f 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -148,7 +148,12 @@ function choice_show_form($choice, $user, $cm) { foreach ($choice->option as $optionid => $text) { if (isset($text)) { //make sure there are no dud entries in the db with blank text values. $countanswers = (get_records("choice_answers", "optionid", $optionid)); - + $countans = 0; + foreach ($countanswers as $ca) { //only return enrolled users. + if (isstudent($cm->course, $ca->userid) or isteacher($cm->course, $ca->userid)) { + $countans = $countans+1; + } + } if ($countanswers) { $countanswers = count($countanswers); } else { @@ -159,14 +164,14 @@ function choice_show_form($choice, $user, $cm) { $cdisplay[$aid]->optionid = $optionid; $cdisplay[$aid]->text = $text; $cdisplay[$aid]->maxanswers = $maxans; - $cdisplay[$aid]->countanswers = $countanswers; + $cdisplay[$aid]->countanswers = $countans; if ($current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id, 'optionid', $optionid)) { $cdisplay[$aid]->checked = ' checked="checked" '; } else { $cdisplay[$aid]->checked = ''; - } - if ($choice->limitanswers && ($countanswers >= $maxans) && (empty($cdisplay[$aid]->checked)) ) { + } + if ($choice->limitanswers && ($countans >= $maxans) && (empty($cdisplay[$aid]->checked)) ) { $cdisplay[$aid]->disabled = ' disabled="disabled" '; } else { $cdisplay[$aid]->disabled = ''; @@ -238,13 +243,20 @@ function choice_show_form($choice, $user, $cm) { echo ""; } -function choice_user_submit_response($formanswer, $choice, $userid) { +function choice_user_submit_response($formanswer, $choice, $userid, $courseid) { $current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $userid); $countanswers = get_records("choice_answers", "optionid", $formanswer); if ($countanswers) { - $countanswers = count($countanswers); + $countans = 0; + foreach ($countanswers as $ca) { //only return enrolled users. + if (isstudent($courseid, $ca->userid) or isteacher($courseid, $ca->userid)) { + $countans = $countans+1; + } + } + + $countanswers = count($countans); } else { $countanswers = 0; } @@ -278,10 +290,10 @@ function choice_user_submit_response($formanswer, $choice, $userid) { function choice_show_reportlink($choice, $courseid, $cmid) { - if ( $allanswers = get_records("choice_answers", "choiceid", $choice->id)) { + if ( $allanswers = get_records("choice_answers", "choiceid", $choice->id)) { $responsecount = 0; foreach ($allanswers as $aa) { - if (isstudent($course->id, $aa->userid) or isteacher($courseid, $aa->userid)) { //check to make sure user is enrolled in course. + if (isstudent($courseid, $aa->userid) or isteacher($courseid, $aa->userid)) { //check to make sure user is enrolled in course. $responsecount++; } } @@ -295,7 +307,7 @@ function choice_show_reportlink($choice, $courseid, $cmid) { function choice_show_results($choice, $course, $cm, $forcepublish='') { - global $CFG, $COLUMN_HEIGHT; + global $CFG, $COLUMN_HEIGHT, $USER; print_heading(get_string("responses", "choice")); if (empty($forcepublish)) { //alow the publish setting to be overridden $forcepublish = $choice->publish; @@ -353,7 +365,7 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { $isteacher = isteacher($course->id); $tablewidth = (int) (100.0 / count($useranswer)); - + if (isteacher($course->id, $USER->id)) { echo '
'; echo '
'; @@ -392,7 +404,7 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { foreach ($userlist as $user) { if (!($optionid==0 && isadmin($user->id)) && !($optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) { //make sure admins and hidden teachers are not shown in not answered yet column. echo ""; - if (isteacher($course->id, $user->id)) { + if (isteacher($course->id, $USER->id) && !($optionid==0)) { echo ''; } echo ""; @@ -416,10 +428,17 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { continue; } echo ""; - $countanswers = count_records("choice_answers", "optionid", $optionid); + $countanswers = get_records("choice_answers", "optionid", $optionid); + $countans = 0; + foreach ($countanswers as $ca) { //only return enrolled users. + if (isstudent($course->id, $ca->userid) or isteacher($course->id, $ca->userid)) { + $countans = $countans+1; + } + } + if ($choice->limitanswers && !$optionid==0) { echo get_string("taken", "choice").":"; - echo $countanswers; + echo $countans; echo "
"; echo get_string("limit", "choice").":"; $choice_option = get_record("choice_options", "id", $optionid); @@ -430,7 +449,7 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { } /// Print "Select all" etc. - if (isteacher($course->id, $user->id)) { + if (isteacher($course->id, $USER->id)) { echo '

'; echo ''; echo ''.get_string('selectall', 'quiz').' / '; diff --git a/mod/choice/report.php b/mod/choice/report.php index 844fa3403a11..30326d33661f 100644 --- a/mod/choice/report.php +++ b/mod/choice/report.php @@ -35,7 +35,8 @@ if ($action == 'delete') { //some responses need to be deleted $attemptids = isset($_POST['attemptid']) ? $_POST['attemptid'] : array(); //get array of repsonses to delete. - choice_delete_responses($attemptids); //delete responses. + choice_delete_responses($attemptids); //delete responses. + redirect("report.php?id=$cm->id"); } if ($download <> "xls" and $download <> "txt" ) { diff --git a/mod/choice/view.php b/mod/choice/view.php index d730ba71c1da..0b25a0c2d053 100644 --- a/mod/choice/view.php +++ b/mod/choice/view.php @@ -41,7 +41,7 @@ if (empty($form->answer)) { redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice')); } else { - choice_user_submit_response($form->answer, $choice, $USER->id); + choice_user_submit_response($form->answer, $choice, $USER->id, $course->id); } redirect("view.php?id=$cm->id"); exit;