From: danmarsden Date: Tue, 4 Apr 2006 23:03:15 +0000 (+0000) Subject: Bug Fixes for: X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=acaf7b86e3df7898badddf842f0c4d128dd0b119;p=moodle.git Bug Fixes for: Bug 5042 Bug 4600 Bug 4021 Bad code - need to check vars aren't empty b4 using them! --- diff --git a/mod/choice/lib.php b/mod/choice/lib.php index c9ecdf0193..60588a8aab 100644 --- a/mod/choice/lib.php +++ b/mod/choice/lib.php @@ -115,7 +115,9 @@ function choice_update_instance($choice) { $option->id = substr($name, 9); // Get the ID of the answer that needs to be updated. $option->text = $value; $option->choiceid = $choice->id; - $option->maxanswers = $choice->{'oldlimit'.substr($name, 9)}; + if (isset($choice->{'oldlimit'.substr($name, 9)})) { + $option->maxanswers = $choice->{'oldlimit'.substr($name, 9)}; + } $option->timemodified = time(); update_record("choice_options", $option); } else { //empty old option - needs to be deleted. @@ -149,11 +151,13 @@ $cdisplay = array(); 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 (!empty($countanswers)) { + 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 { @@ -243,7 +247,7 @@ $cdisplay = array(); echo ""; } -function choice_user_submit_response($formanswer, $choice, $userid, $courseid) { +function choice_user_submit_response($formanswer, $choice, $userid, $courseid, $cm) { $current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $userid); @@ -267,11 +271,11 @@ $current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user $newanswer = $current; $newanswer->optionid = $formanswer; - $newanswer->timemodified = $timenow; + $newanswer->timemodified = time(); if (! update_record("choice_answers", $newanswer)) { error("Could not update your choice because of a database error"); } - add_to_log($course->id, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id); + add_to_log($courseid, "choice", "choose again", "view.php?id=$cm->id", $choice->id, $cm->id); } else { $newanswer = NULL; $newanswer->choiceid = $choice->id; @@ -281,7 +285,7 @@ $current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user if (! insert_record("choice_answers", $newanswer)) { error("Could not save your choice"); } - add_to_log($course->id, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id); + add_to_log($courseid, "choice", "choose", "view.php?id=$cm->id", $choice->id, $cm->id); } } else { error("this choice is full!"); @@ -429,13 +433,14 @@ function choice_show_results($choice, $course, $cm, $forcepublish='') { } echo ""; $countanswers = get_records("choice_answers", "optionid", $optionid); - $countans = 0; - foreach ($countanswers as $ca) { //only return enrolled users. + $countans = 0; + if (!empty($countanswers)) { + 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 $countans; diff --git a/mod/choice/report.php b/mod/choice/report.php index 30326d3366..22dd6bed11 100644 --- a/mod/choice/report.php +++ b/mod/choice/report.php @@ -116,9 +116,12 @@ $studentid=(!empty($user->idnumber) ? $user->idnumber : " "); $myxls->write_string($row,2,$studentid); $ug2 = ''; - foreach (user_group($course->id, $user->id) as $ug) { - $ug2 = $ug2. $ug->name; - } + $usergrp = user_group($course->id, $user->id); + if (!empty($usergrp)) { + foreach ($usergrp as $ug) { + $ug2 = $ug2. $ug->name; + } + } $myxls->write_string($row,3,$ug2); $useroption = choice_get_option_text($choice, $answers[$user->id]->optionid); @@ -156,17 +159,23 @@ $i=0; $row=1; if ($users) foreach ($users as $user) { - if (!($answers[$user->id]->optionid==0 && isadmin($user->id)) && + if (!empty($answers[$user->id]) && !($answers[$user->id]->optionid==0 && isadmin($user->id)) && (!($answers[$user->id]->optionid==0 && isteacher($course->id, $user->id) && !(isteacheredit($course->id, $user->id)) ) ) && !($choice->showunanswered==0 && $answers[$user->id]->optionid==0) ) { //make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page. echo $user->lastname; echo "\t".$user->firstname; - $studentid=(($user->idnumber != "") ? $user->idnumber : " "); + $studentid = " "; + if (!empty($user->idnumber)) { + $studentid = $user->idnumber; + } echo "\t". $studentid."\t"; $ug2 = ''; - foreach (user_group($course->id, $user->id) as $ug) { - $ug2 = $ug2. $ug->name; + $usergrp = user_group($course->id, $user->id); + if (!empty($usergrp)) { + foreach ($usergrp as $ug) { + $ug2 = $ug2. $ug->name; + } } echo $ug2. "\t"; echo format_string(choice_get_option_text($choice, $answers[$user->id]->optionid),true). "\n"; diff --git a/mod/choice/view.php b/mod/choice/view.php index 0b25a0c2d0..772fa5bfe1 100644 --- a/mod/choice/view.php +++ b/mod/choice/view.php @@ -30,7 +30,7 @@ if ($form = data_submitted()) { $timenow = time(); - if (isteacher($course->id, $user->id)) { + if (isteacher($course->id, $USER->id)) { 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. @@ -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, $course->id); + choice_user_submit_response($form->answer, $choice, $USER->id, $course->id, $cm); } redirect("view.php?id=$cm->id"); exit;