]> git.mjollnir.org Git - moodle.git/commitdiff
Bug Fixes for:
authordanmarsden <danmarsden>
Tue, 4 Apr 2006 23:03:15 +0000 (23:03 +0000)
committerdanmarsden <danmarsden>
Tue, 4 Apr 2006 23:03:15 +0000 (23:03 +0000)
Bug 5042
Bug 4600
Bug 4021
Bad code - need to check vars aren't empty b4 using them!

mod/choice/lib.php
mod/choice/report.php
mod/choice/view.php

index c9ecdf01939ffc8e2557068a2960b97b2d6e1e58..60588a8aab23ec9e7bc278b6634c2ebb79197424 100644 (file)
@@ -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 "</center>";
 }
 
-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 "<td align=\"center\" class=\"count\">";
                 $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;
index 30326d33661f9649948b39c941c737155e63e396..22dd6bed117811d87cb7c8c5952e28a7608d56c6 100644 (file)
                     $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);
       $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";
index 0b25a0c2d053b6bfbd62b67ce4f2bad52a63ac3e..772fa5bfe19346c3ef955d8aa96a940596d9dfa1 100644 (file)
@@ -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;