--- tmp/cvs/contrib/questionnaire/locallib.php 2007-01-01 22:41:50.000000000 +0000 +++ locallib.php 2007-01-01 22:41:35.000000000 +0000 @@ -1562,6 +1562,8 @@ $sid = $this->survey->id; $values = array(); + + $colnames = array(); if ($col == null) { $col = ''; @@ -1570,8 +1572,12 @@ $col = explode(',', preg_replace("/\s/",'', $col)); } if (is_array($col) && count($col) > 0) { + $colnames = $col; $col = ',' . implode(',', array_map(create_function('$a','return "q.$a";'), $col)); } + + + ####die('col= ' .$col); if ($qids == null) { $qids = ''; @@ -1589,17 +1595,19 @@ 'WHERE a.response_id=\''.$rid.'\' AND a.question_id=q.id '.$qids; if ($records = get_records_sql($sql)) { foreach ($records as $row) { - /// Change the data object into a numerically indexed array. - ///*** Because of the way the following code works, we need to remove the non-numeric keys of the array. - ///*** The problem is that the Moodle db calls returns hask and numeric keys for each value (2 per). + $newrow = array(); $row = (array)$row; - $qid = array_shift($row); - foreach ($row as $key => $val) { - if (!is_numeric($key)) unset($row[$key]); - } - $val = array_pop($row); - $values[$qid] = $row; - array_push($values["$qid"], ($val == 'Y') ? get_string('yes') : get_string('no'), $val); + + $newrow = $this->generate_response_array($colnames, $row); + + if( $row['choice_id'] == 'Y' ){ + $newrow[] = get_string('yes'); + }else{ + $newrow[] = get_string('no'); + } + $newrow[] = $row['choice_id']; + + $values[$row['id']] = $newrow; } } @@ -1611,21 +1619,23 @@ 'WHERE a.response_id=\''.$rid.'\' AND a.question_id=q.id AND a.choice_id=c.id '.$qids; if ($records = get_records_sql($sql)) { foreach ($records as $row) { - /// Change the data object into a numerically indexed array. - ///*** Because of the way the following code works, we need to remove the non-numeric keys of the array. - ///*** The problem is that the Moodle db calls returns hask and numeric keys for each value (2 per). + $newrow = array(); + $row = (array)$row; - $qid = array_shift($row); - foreach ($row as $key => $val) { - if (!is_numeric($key)) unset($row[$key]); - } - $c = count($row); - $val = $row[$c - 2]; - if (ereg('^!other', $val)) - $row[$c - 1] = 'other_' . $row[$c - 1]; - else - settype($row[$c - 1], 'integer'); - $values[$qid] = $row; + $qid = $row['id']; + + $newrow = $this->generate_response_array($colnames, $row); + + $newrow[] = $row['content']; + $newrow[] = $row['ccontent']; + + if (ereg('^!other', $row['ccontent'])){ + $newrow[] = 'other_' . $row['cid']; + }else{ + $newrow[] = $row['cid']; + } + + $values[$qid] = $newrow; } } @@ -1639,33 +1649,32 @@ $tmp = null; if ($records = get_records_sql($sql)) { foreach ($records as $row) { - /// Change the data object into a numerically indexed array. - ///*** Because of the way the following code works, we need to remove the non-numeric keys of the array. - ///*** The problem is that the Moodle db calls returns hask and numeric keys for each value (2 per). + + $newrow = array(); $row = (array)$row; - array_shift($row); // get rid of the answer id. - $qid = array_shift($row); - foreach ($row as $key => $val) { - if (!is_numeric($key)) unset($row[$key]); - } - $c = count($row); - $val = $row[$c - 2]; - if (ereg('^!other', $val)) - $row[$c - 1] = 'other_' . $row[$c - 1]; - else - settype($row[$c - 1], 'integer'); + + $qid = $row['qid']; + $newrow = $this->generate_response_array($colnames, $row); + $newrow[] = $row['ccontent']; + + if (ereg('^!other', $row['ccontent'])){ + $newrow[] = 'other_' . $row['cid']; + }else{ + $newrow[] = $row['cid']; + } if($tmp == $qid) { - $arr[] = $row; + $arr[] = $newrow; continue; } if($tmp != null) $values["$tmp"]=$arr; $tmp = $qid; - $arr = array($row); + $arr = array($newrow); } } if($tmp != null) $values["$tmp"]=$arr; + unset($arr); unset($tmp); unset($row); @@ -1678,17 +1687,16 @@ 'ORDER BY a.question_id,c.id'; if ($records = get_records_sql($sql)) { foreach ($records as $row) { - /// Change the data object into a numerically indexed array. - ///*** Because of the way the following code works, we need to remove the non-numeric keys of the array. - ///*** The problem is that the Moodle db calls returns hask and numeric keys for each value (2 per). + $newrow = array(); $row = (array)$row; - $qid = array_shift($row); - foreach ($row as $key => $val) { - if (!is_numeric($key)) unset($row[$key]); - } - $cid = array_shift($row); - array_push($row, $row[count($row) - 1]); - $values["${qid}_${cid}"] = $row; + + $newrow = $this->generate_response_array($colnames, $row); + $newrow[] = $row['aresponse']; + $newrow[] = $row['aresponse']; + + $idstring = $row['id'] . '_' . $row['cid']; + + $values["$idstring"] = $newrow; } } @@ -1701,23 +1709,15 @@ 'ORDER BY aid, a.question_id,c.id'; if ($records = get_records_sql($sql)) { foreach ($records as $row) { - /// Change the data object into a numerically indexed array. - ///*** Because of the way the following code works, we need to remove the non-numeric keys of the array. - ///*** The problem is that the Moodle db calls returns hash and numeric keys for each value (2 per). - $row = (array)$row; - array_shift($row); // get rid of the answer id. - - /// Next two are 'qid' and 'cid', each with numeric and hash keys. - $qid = array_shift($row); - array_shift($row); - $qid .= '_'.array_shift($row); - array_shift($row); - - foreach ($row as $key => $val) { - if (!is_numeric($key)) unset($row[$key]); - } - settype($row[count($row) - 1], 'integer'); - $values[$qid] = $row; + $newrow = array(); + $row = (array) $row; + + $newrow = $this->generate_response_array($colnames, $row); + $newrow[] = $row['ccontent']; + $newrow[] = $row['arank']; + + $qid = $row['qid'] . '_' . $row['cid']; + $values[$qid] = $newrow; } } @@ -1726,18 +1726,14 @@ 'FROM '.$CFG->prefix.'questionnaire_response_text a, '.$CFG->prefix.'questionnaire_question q '. 'WHERE a.response_id=\''.$rid.'\' AND a.question_id=q.id '.$qids; if ($records = get_records_sql($sql)) { - foreach ($records as $row) { - /// Change the data object into a numerically indexed array. - ///*** Because of the way the following code works, we need to remove the non-numeric keys of the array. - ///*** The problem is that the Moodle db calls returns hask and numeric keys for each value (2 per). + foreach ($records as $row) { + $newrow = array(); $row = (array)$row; - $qid = array_shift($row); - foreach ($row as $key => $val) { - if (!is_numeric($key)) unset($row[$key]); - } - $values["$qid"]=$row; - $val = array_pop($values["$qid"]); - array_push($values["$qid"], $val, $val); + + $newrow = $this->generate_response_array($colnames, $row); + $newrow[] = $row['aresponse']; + $newrow[] = $row['aresponse']; + $values[$row['id']] = $newrow; } } @@ -1747,17 +1743,12 @@ 'WHERE a.response_id=\''.$rid.'\' AND a.question_id=q.id '.$qids; if ($records = get_records_sql($sql)) { foreach ($records as $row) { - /// Change the data object into a numerically indexed array. - ///*** Because of the way the following code works, we need to remove the non-numeric keys of the array. - ///*** The problem is that the Moodle db calls returns hask and numeric keys for each value (2 per). + + $newrow = array(); $row = (array)$row; - $qid = array_shift($row); - foreach ($row as $key => $val) { - if (!is_numeric($key)) unset($row[$key]); - } - $values["$qid"]=$row; - $val = array_pop($values["$qid"]); - array_push($values["$qid"], $val, $val); + $newrow = $this->generate_response_array($colnames, $row); + $newrow[] = $row['aresponse']; + $values[$row['id']] = $newrow; } } @@ -1765,6 +1756,22 @@ uksort($values, 'questionnaire_response_key_cmp'); return($values); } + + /* This function purely maps the start of an array to the columns + * specified, in order to allow for $col in the above function + * its quick and dirty + */ + function generate_response_array($colnames, $answers){ + $output = array(); + + foreach($colnames as $col){ + if(array_key_exists($col, $answers)){ + $output[]=$answers[$col]; + } + } + + return $output; + } function response_goto_thankyou() { global $CFG; @@ -2104,7 +2111,7 @@ $counts = array(); // --------------------------------------------------------------------------- - if (!empty($guicross) && $question["result_id"] == 1){ + if (!empty($guicross) && $question->result_id == 1){ $this->mkcrossformat($counts,$qid,$question->type_id ); } else { $question->display_results($rids);