moving duplicated code into Lib.php as functions & clean up the code a bit. - also now include users group info in text/excel downloads. - preparation for new choice block!

This commit is contained in:
danmarsden 2006-02-23 01:20:24 +00:00
parent d8c33a44a9
commit 348630d87e
3 changed files with 467 additions and 534 deletions

View File

@ -137,6 +137,423 @@ function choice_update_instance($choice) {
}
function choice_show_form($choice, $user, $cm) {
//$cdisplay is an array of the display info for a choice $cdisplay[$optionid]->text - text name of option.
// ->maxanswers -maxanswers for this option
// ->full - whether this option is full or not. 0=not full, 1=full
$cdisplay = array();
$aid = 0;
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));
if ($countanswers) {
$countanswers = count($countanswers);
} else {
$countanswers = 0;
}
$maxans = $choice->maxanswers[$optionid];
$cdisplay[$aid]->optionid = $optionid;
$cdisplay[$aid]->text = $text;
$cdisplay[$aid]->maxanswers = $maxans;
$cdisplay[$aid]->countanswers = $countanswers;
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)) ) {
$cdisplay[$aid]->disabled = ' disabled="disabled" ';
} else {
$cdisplay[$aid]->disabled = '';
}
$aid++;
}
}
switch ($choice->display) {
case CHOICE_DISPLAY_HORIZONTAL:
echo "<table cellpadding=\"20\" cellspacing=\"20\" align=\"center\"><tr>";
foreach ($cdisplay as $cd) {
echo "<td align=\"center\" valign=\"top\">";
echo "<input type=\"radio\" name=\"answer\" value=\"".$cd->optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";
if (!empty($cd->disabled)) {
echo format_text($cd->text."<br /><strong>".get_string('full', 'choice')."</strong>");
} else {
echo format_text($cd->text);
}
echo "</td>";
}
echo "</tr>";
echo "</table>";
break;
case CHOICE_DISPLAY_VERTICAL:
$displayoptions->para = false;
echo "<table cellpadding=\"10\" cellspacing=\"10\" align=\"center\">";
foreach ($cdisplay as $cd) {
echo "<tr><td align=\"left\">";
echo "<input type=\"radio\" name=\"answer\" value=\"".$cd->optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";
echo format_text($cd->text. ' ', FORMAT_MOODLE, $displayoptions); //display text for option.
if ($choice->limitanswers && ($choice->showresults==CHOICE_SHOWRESULTS_ALWAYS) ){ //if limit is enabled, and show results always has been selected, display info beside each choice.
echo "</td><td>";
if (!empty($cd->disabled)) {
echo get_string('full', 'choice');
} elseif(!empty($cd->checked)) {
//currently do nothing - maybe some text could be added here to signfy that the choice has been 'selected'
} elseif ($cd->maxanswers-$cd->countanswers==1) {
echo ($cd->maxanswers - $cd->countanswers);
echo " ".get_string('spaceleft', 'choice');
} else {
echo ($cd->maxans - $cd->countanswers);
echo " ".get_string('spacesleft', 'choice');
}
echo "</td>";
} else if ($choice->limitanswers && ($cd->countanswers >= $cd->maxanswers)) { //if limitanswers and answers exceeded, display "full" beside the choice.
echo " <strong>".get_string('full', 'choice')."</strong>";
}
echo "</td>";
echo "</tr>";
}
echo "</table>";
break;
}
//show save choice button
echo "<center>";
echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
if (!isguest()) { //don't show save button if the logged in user is the guest user.
echo "<input type=\"submit\" value=\"".get_string("savemychoice","choice")."\" />";
} else {
print_string('havetologin', 'choice');
}
echo "</center>";
}
function choice_user_submit_response($formanswer, $choice, $userid) {
$current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $userid);
$countanswers = get_records("choice_answers", "optionid", $formanswer);
if ($countanswers) {
$countanswers = count($countanswers);
} else {
$countanswers = 0;
}
$maxans = $choice->maxanswers[$formanswer];
if (!($choice->limitanswers && ($countanswers >= $maxans) )) {
if ($current) {
$newanswer = $current;
$newanswer->optionid = $formanswer;
$newanswer->timemodified = $timenow;
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);
} else {
$newanswer = NULL;
$newanswer->choiceid = $choice->id;
$newanswer->userid = $userid;
$newanswer->optionid = $formanswer;
$newanswer->timemodified = time();
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);
}
} else {
error("this choice is full!");
}
}
function choice_show_reportlink($choice, $courseid, $cmid) {
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.
$responsecount++;
}
}
} else {
$responsecount = 0;
}
echo '<div class="reportlink">';
echo "<a href=\"report.php?id=$cmid\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
echo '</div>';
}
function choice_show_results($choice, $course, $cm, $forcepublish='') {
global $CFG, $COLUMN_HEIGHT;
print_heading(get_string("responses", "choice"));
if (empty($forcepublish)) { //alow the publish setting to be overridden
$forcepublish = $choice->publish;
}
/// Check to see if groups are being used in this choice
if ($groupmode = groupmode($course, $cm)) { // Groups are being used
$currentgroup = setup_and_print_groups($course, $groupmode, $_SERVER['PHP_SELF']."?id=$cm->id");
} else {
$currentgroup = false;
}
if ($currentgroup) {
$users = get_group_users($currentgroup, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber');
} else {
$users = get_course_users($course->id, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber') + get_admins();
}
if (!$users) {
print_heading(get_string("nousersyet"));
}
if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
foreach ($allresponses as $aa) {
$answers[$aa->userid] = $aa;
}
} else {
$answers = array () ;
}
$timenow = time();
foreach ($choice->option as $optionid => $text) {
$useranswer[$optionid] = array();
}
foreach ($users as $user) {
if (!empty($user->id) and !empty($answers[$user->id])) {
$answer = $answers[$user->id];
$useranswer[(int)$answer->optionid][] = $user;
} else {
$useranswer[0][] = $user;
}
}
foreach ($choice->option as $optionid => $text) {
if (!$choice->option[$optionid]) {
unset($useranswer[$optionid]); // Throw away any data that doesn't apply
}
}
ksort($useranswer);
switch ($forcepublish) {
case CHOICE_PUBLISH_NAMES:
$isteacher = isteacher($course->id);
$tablewidth = (int) (100.0 / count($useranswer));
if (isteacher($course->id, $USER->id)) {
echo '<div id="tablecontainer">';
echo '<form id="attemptsform" method="post" action="'.$_SERVER['PHP_SELF'].'" onsubmit="var menu = document.getElementById(\'menuaction\'); return (menu.options[menu.selectedIndex].value == \'delete\' ? \''.addslashes(get_string('deleteattemptcheck','quiz')).'\' : true);">';
echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
echo '<input type="hidden" name="mode" value="overview" />';
}
echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\" class=\"results names\">";
echo "<tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if ($optionid) {
echo "<th class=\"col$count header\" width=\"$tablewidth%\">";
} else if ($choice->showunanswered) {
echo "<th class=\"col$count header\" width=\"$tablewidth%\">";
} else {
continue;
}
echo format_string(choice_get_option_text($choice, $optionid));
echo "</th>";
$count++;
}
echo "</tr><tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if ($optionid) {
echo "<td class=\"col$count data\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
} else if ($choice->showunanswered) {
echo "<td class=\"col$count data\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
} else {
continue;
}
echo "<table width=\"100%\">";
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 "<tr>";
if (isteacher($course->id, $user->id)) {
echo '<td width=\"5\" nowrap=\"nowrap\"><input type="checkbox" name="attemptid[]" value="'. $answers[$user->id]->id. '" /></td>';
}
echo "<td width=\"10\" nowrap=\"nowrap\" class=\"picture\">";
print_user_picture($user->id, $course->id, $user->picture);
echo "</td><td width=\"100%\" nowrap=\"nowrap\" class=\"fullname\">";
echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
echo fullname($user, $isteacher);
echo "</a>";
echo "</td></tr>";
}
}
$count++;
echo "</table>";
echo "</td>";
}
echo "</tr><tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if (!$optionid and !$choice->showunanswered) {
continue;
}
echo "<td align=\"center\" class=\"count\">";
$countanswers = count_records("choice_answers", "optionid", $optionid);
if ($choice->limitanswers && !$optionid==0) {
echo get_string("taken", "choice").":";
echo $countanswers;
echo "<br>";
echo get_string("limit", "choice").":";
$choice_option = get_record("choice_options", "id", $optionid);
echo $choice_option->maxanswers;
}
echo "</td>";
$count++;
}
/// Print "Select all" etc.
if (isteacher($course->id, $user->id)) {
echo '<tr><td><p>';
echo '<tr><td>';
echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
echo '&nbsp;&nbsp;';
$options = array('delete' => get_string('delete'));
echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
echo '<noscript id="noscriptmenuaction" style="display: inline;">';
echo '<input type="submit" value="'.get_string('go').'" /></noscript>';
echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
echo '</p></td></tr>';
}
echo "</tr></table>";
if (isteacher($course->id, $user->id)) {
echo "</form></div>";
}
break;
case CHOICE_PUBLISH_ANONYMOUS:
$tablewidth = (int) (100.0 / count($useranswer));
echo "<table cellpadding=\"5\" cellspacing=\"0\" align=\"center\" class=\"results anonymous\">";
echo "<tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if ($optionid) {
echo "<th width=\"$tablewidth%\" class=\"col$count header\">";
} else if ($choice->showunanswered) {
echo "<th width=\"$tablewidth%\" class=\"col$count header\">";
} else {
continue;
}
echo format_string(choice_get_option_text($choice, $optionid));
echo "</th>";
$count++;
}
echo "</tr><tr>";
$maxcolumn = 0;
foreach ($useranswer as $optionid => $userlist) {
if (!$optionid and !$choice->showunanswered) {
continue;
}
$column[$optionid] = 0;
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.
$column[$optionid]++;
}
}
if ($column[$optionid] > $maxcolumn) {
$maxcolumn = $column[$optionid];
}
}
echo "</tr><tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if (!$optionid and !$choice->showunanswered) {
continue;
}
$height = 0;
if ($maxcolumn) {
$height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
}
echo "<td valign=\"bottom\" align=\"center\" class=\"col$count data\">";
echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
echo "</td>";
$count++;
}
echo "</tr>";
echo "<tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if (!$optionid and !$choice->showunanswered) {
continue;
}
echo "<td align=\"center\" class=\"col$count count\">";
if ($choice->limitanswers && !$optionid==0) {
echo get_string("taken", "choice").":";
echo $column[$optionid];
echo "<br>";
echo get_string("limit", "choice").":";
$choice_option = get_record("choice_options", "id", $optionid);
echo $choice_option->maxanswers;
} else {
echo $column[$optionid];
}
echo "</td>";
$count++;
}
echo "</tr></table>";
break;
}
}
function choice_delete_responses($attemptids) {
if(!is_array($attemptids) || empty($attemptids)) {
return false;
}
foreach($attemptids as $num => $attemptid) {
if(empty($attemptid)) {
unset($attemptids[$num]);
}
}
foreach($attemptids as $attemptid) {
if ($todelete = get_record('choice_answers', 'id', $attemptid)) {
delete_records('choice_answers', 'id', $attemptid);
}
}
return true;
}
function choice_delete_instance($id) {
// Given an ID of an instance of this module,
@ -165,6 +582,7 @@ function choice_delete_instance($id) {
return $result;
}
function choice_get_participants($choiceid) {
//Returns the users with data in one choice
//(users with records in choice_responses, students)

View File

@ -7,6 +7,7 @@
$format = optional_param('format', CHOICE_PUBLISH_NAMES, PARAM_INT);
$download = optional_param('download', '', PARAM_ALPHA);
$action = optional_param('action', '');
if (! $cm = get_record("course_modules", "id", $id)) {
error("Course Module ID was incorrect");
@ -31,51 +32,21 @@
$strresponses = get_string("responses", "choice");
add_to_log($course->id, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id);
$action = optional_param('action', '');
switch($action) {
case 'delete': /// Some attempts need to be deleted
// the following needs to be improved to delete all associated data as well
$attemptids = isset($_POST['attemptid']) ? $_POST['attemptid'] : array();
if(!is_array($attemptids) || empty($attemptids)) {
break;
}
foreach($attemptids as $num => $attemptid) {
if(empty($attemptid)) {
unset($attemptids[$num]);
}
}
foreach($attemptids as $attemptid) {
if ($todelete = get_record('choice_answers', 'id', $attemptid)) {
delete_records('choice_answers', 'id', $attemptid);
}
}
break;
}
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.
}
if ($download <> "xls" and $download <> "txt" ) {
print_header_simple(format_string($choice->name).": $strresponses", "",
"<a href=\"index.php?id=$course->id\">$strchoices</a> ->
<a href=\"view.php?id=$cm->id\">".format_string($choice->name,true)."</a> -> $strresponses", "", '', true,
update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
}
$users = get_course_users($course->id, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber') + get_admins();
/// Check to see if groups are being used in this choice
if ($groupmode = groupmode($course, $cm)) { // Groups are being used
$currentgroup = setup_and_print_groups($course, $groupmode, "report.php?id=$cm->id");
} else {
$currentgroup = false;
}
}
if ($currentgroup) {
$users = get_group_users($currentgroup, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber');
} else {
$users = get_course_users($course->id, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname, u.idnumber') + get_admins();
}
if (!$users) {
print_heading(get_string("nousersyet"));
@ -126,7 +97,9 @@
$myxls->write_string(0,0,get_string("lastname"));
$myxls->write_string(0,1,get_string("firstname"));
$myxls->write_string(0,2,get_string("idnumber"));
$myxls->write_string(0,3,get_string("choice","choice"));
$myxls->write_string(0,3,get_string("group"));
$myxls->write_string(0,4,get_string("choice","choice"));
/// generate the data for the body of the spreadsheet
$i=0;
@ -141,9 +114,15 @@
$myxls->write_string($row,1,$user->firstname);
$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;
}
$myxls->write_string($row,3,$ug2);
$useroption = choice_get_option_text($choice, $answers[$user->id]->optionid);
if (isset($useroption)) {
$myxls->write_string($row,3,format_string($useroption,true));
$myxls->write_string($row,4,format_string($useroption,true));
}
$row++;
}
@ -169,6 +148,7 @@
/// Print names of all the fields
echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
echo get_string("group"). "\t";
echo get_string("choice","choice"). "\n";
/// generate the data for the body of the spreadsheet
@ -183,151 +163,20 @@
echo "\t".$user->firstname;
$studentid=(($user->idnumber != "") ? $user->idnumber : " ");
echo "\t". $studentid."\t";
$ug2 = '';
foreach (user_group($course->id, $user->id) as $ug) {
$ug2 = $ug2. $ug->name;
}
echo $ug2. "\t";
echo format_string(choice_get_option_text($choice, $answers[$user->id]->optionid),true). "\n";
}
$row++;
}
exit;
}
switch ($format) {
case CHOICE_PUBLISH_NAMES:
echo '<div id="tablecontainer">';
echo '<form id="attemptsform" method="post" action="report.php" onsubmit="var menu = document.getElementById(\'menuaction\'); return (menu.options[menu.selectedIndex].value == \'delete\' ? \''.addslashes(get_string('deleteattemptcheck','quiz')).'\' : true);">';
echo '<input type="hidden" name="id" value="'.$cm->id.'" />';
echo '<input type="hidden" name="mode" value="overview" />';
$tablewidth = (int) (100.0 / count($useranswer));
echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\" class=\"results names\">";
echo "<tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if ($optionid) {
echo "<th class=\"col$count header\" width=\"$tablewidth%\">";
} else if ($choice->showunanswered) {
echo "<th class=\"col$count header\" width=\"$tablewidth%\">";
} else {
continue;
}
echo format_string(choice_get_option_text($choice, $optionid));
echo "</th>";
$count++;
}
echo "</tr><tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if ($optionid) {
echo "<td class=\"col$count data\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
} else if ($choice->showunanswered) {
echo "<td class=\"col$count data\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
} else {
continue;
}
echo "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">";
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 "<tr><td width=\"5\" nowrap=\"nowrap\">";
echo '<input type="checkbox" name="attemptid[]" value="'. $answers[$user->id]->id. '" />';
echo "</td><td width=\"10\" nowrap=\"nowrap\" class=\"picture\">";
print_user_picture($user->id, $course->id, $user->picture);
echo "</td><td width=\"100%\" nowrap=\"nowrap\" class=\"fullname\">";
echo "<p>".fullname($user, true)."</p>";
echo "</td></tr>";
}
}
echo "</table>";
echo "</td>";
$count++;
}
echo "</tr><tr><td>";
/// Print "Select all" etc.
// if (!empty($attempts)) {
echo '<table id="commands">';
echo '<tr><td>';
echo '<a href="javascript:select_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectall', 'quiz').'</a> / ';
echo '<a href="javascript:deselect_all_in(\'DIV\',null,\'tablecontainer\');">'.get_string('selectnone', 'quiz').'</a> ';
echo '&nbsp;&nbsp;';
$options = array('delete' => get_string('delete'));
echo choose_from_menu($options, 'action', '', get_string('withselected', 'quiz'), 'if(this.selectedIndex > 0) submitFormById(\'attemptsform\');', '', true);
echo '<noscript id="noscriptmenuaction" style="display: inline;">';
echo '<input type="submit" value="'.get_string('go').'" /></noscript>';
echo '<script type="text/javascript">'."\n<!--\n".'document.getElementById("noscriptmenuaction").style.display = "none";'."\n-->\n".'</script>';
echo '</td></tr></table>';
// }
echo "</td></tr></table></div>";
/// Close form
echo '</form>';
break;
case CHOICE_PUBLISH_ANONYMOUS:
$tablewidth = (int) (100.0 / count($useranswer));
echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\" class=\"results anonymous\">";
echo "<tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if ($optionid) {
echo "<th width=\"$tablewidth%\" class=\"col$count header\">";
} else if ($choice->showunanswered) {
echo "<th width=\"$tablewidth%\" class=\"col$count header\">";
} else {
continue;
}
echo choice_get_option_text($choice, $optionid);
echo "</th>";
$count++;
}
echo "</tr>";
$maxcolumn = 0;
foreach ($useranswer as $optionid => $userlist) {
if (!$optionid and !$choice->showunanswered) {
continue;
}
$column[$optionid] = count($userlist);
if ($column[$optionid] > $maxcolumn) {
$maxcolumn = $column[$optionid];
}
}
echo "<tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if (!$optionid and !$choice->showunanswered) {
continue;
}
$height = 0;
if ($maxcolumn) {
$height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
}
echo "<td valign=\"bottom\" align=\"center\" class=\"col$count data\">";
echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
echo "</td>";
$count++;
}
echo "</tr>";
echo "<tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if (!$optionid and !$choice->showunanswered) {
continue;
}
echo "<td align=\"center\" class=\"col$count count\">".$column[$optionid]."</td>";
$count++;
}
echo "</tr></table>";
break;
}
choice_show_results($choice, $course, $cm, $format); //show table with students responses.
//now give links for downloading spreadsheets.
echo "<br />\n";
echo "<table border=\"0\" align=\"center\"><tr>\n";
echo "<td>";

View File

@ -4,7 +4,8 @@
require_once("lib.php");
require_variable($id); // Course Module ID
$action = optional_param('action', '');
if (! $cm = get_record("course_modules", "id", $id)) {
error("Course Module ID was incorrect");
}
@ -18,58 +19,29 @@
if (!$choice = choice_get_choice($cm->instance)) {
error("Course module is incorrect");
}
$strchoice = get_string("modulename", "choice");
$strchoices = get_string("modulenameplural", "choice");
if ($choice->option) {
foreach ($choice->option as $optionid => $text) {
$answerchecked[$optionid] = '';
}
}
if (isset($USER->id) && $current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $USER->id)) {
$answerchecked[$current->optionid] = 'checked="checked"';
} else {
$current = false;
}
/// Submit any new data if there is any
if ($form = data_submitted()) {
$timenow = time();
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.
redirect("view.php?id=$cm->id");
}
}
if (empty($form->answer)) {
redirect("view.php?id=$cm->id", get_string('mustchooseone', 'choice'));
} else {
$countanswers = get_records("choice_answers", "optionid", $form->answer);
if ($countanswers) {
$countanswers = count($countanswers);
} else {
$countanswers = 0;
}
$maxans = $choice->maxanswers[$form->answer];
if (!($choice->limitanswers && ($countanswers >= $maxans) )) {
if ($current) {
$newanswer = $current;
$newanswer->optionid = $form->answer;
$newanswer->timemodified = $timenow;
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);
} else {
$newanswer = NULL;
$newanswer->choiceid = $choice->id;
$newanswer->userid = $USER->id;
$newanswer->optionid = $form->answer;
$newanswer->timemodified = $timenow;
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);
}
} else {
error("this choice is full!");
}
choice_user_submit_response($form->answer, $choice, $USER->id);
}
redirect("view.php?id=$cm->id");
exit;
@ -78,36 +50,15 @@
/// Display the choice and possibly results
$strchoice = get_string("modulename", "choice");
$strchoices = get_string("modulenameplural", "choice");
add_to_log($course->id, "choice", "view", "view.php?id=$cm->id", $choice->id, $cm->id);
print_header_simple(format_string($choice->name), "",
"<a href=\"index.php?id=$course->id\">$strchoices</a> -> ".format_string($choice->name), "", "", true,
update_module_button($cm->id, $course->id, $strchoice), navmenu($course, $cm));
/// Check to see if groups are being used in this choice
if ($groupmode = groupmode($course, $cm)) { // Groups are being used
$currentgroup = setup_and_print_groups($course, $groupmode, "view.php?id=$cm->id");
} else {
$currentgroup = false;
}
if (isteacher($course->id)) {
if ( $allanswers = get_records("choice_answers", "choiceid", $choice->id)) {
$responsecount = 0;
foreach ($allanswers as $aa) {
if (isstudent($course->id, $aa->userid) or isteacher($course->id, $aa->userid)) { //check to make sure user is enrolled in course.
$responsecount++;
}
}
} else {
$responsecount = 0;
}
echo '<div class="reportlink">';
echo "<a href=\"report.php?id=$cm->id\">".get_string("viewallresponses", "choice", $responsecount)."</a>";
echo '</div>';
choice_show_reportlink($choice, $course->id, $cm->id);
} else if (!$cm->visible) {
notice(get_string("activityiscurrentlyhidden"));
}
@ -134,98 +85,8 @@
echo "<form name=\"form\" method=\"post\" action=\"view.php\">";
switch ($choice->display) {
case CHOICE_DISPLAY_HORIZONTAL:
echo "<table cellpadding=\"20\" cellspacing=\"20\" align=\"center\"><tr>";
$aid = 0;
foreach ($choice->option as $optionid => $text) {
$countanswers = (get_records("choice_answers", "optionid", $optionid));
if ($countanswers) {
$countanswers = count($countanswers);
} else {
$countanswers = 0;
}
$maxans = $choice->maxanswers[$optionid];
if (isset($text)) {
echo "<td align=\"center\" valign=\"top\">";
echo "<input type=\"radio\" name=\"answer\" value=\"".$optionid."\" ".$answerchecked[$optionid]." alt=\"".strip_tags(format_text($text))."\"";
if ($choice->limitanswers && ($countanswers >= $maxans) && !($answerchecked[$optionid]) ) {
echo ' disabled="disabled"';
}
echo " />";
if ($choice->limitanswers && ($countanswers >= $maxans) ) {
echo format_text($text."<br /><strong>".get_string('full', 'choice')."</strong>");
} else {
echo format_text($text);
}
echo "</td>";
$aid++;
}
}
echo "</tr>";
echo "</table>";
break;
case CHOICE_DISPLAY_VERTICAL:
$aid = 0;
$displayoptions->para = false;
echo "<table cellpadding=\"10\" cellspacing=\"10\" align=\"center\">";
foreach ($choice->option as $optionid => $text) {
if ($text) {
echo "<tr><td align=\"left\">";
echo "<input type=\"radio\" name=\"answer\" value=\"".$optionid."\" ".$answerchecked[$optionid]." alt=\"".strip_tags(format_text($text, FORMAT_MOODLE, $displayoptions))."\" />";
$countanswers = get_records("choice_answers", "optionid", $optionid);
if ($countanswers) {
$countanswers = count($countanswers);
} else {
$countanswers = 0;
}
$maxans = $choice->maxanswers[$optionid];
if ($choice->limitanswers && ($countanswers >= $maxans) ) {
if (!($answerchecked[$optionid])) {
echo "<script type=\"text/javascript\">";
echo "document.form.answer[".$aid."].disabled = true;";
echo "</script>";
}
}
echo format_text($text. ' ', FORMAT_MOODLE, $displayoptions);
if ($choice->limitanswers && ($choice->showresults==CHOICE_SHOWRESULTS_ALWAYS) ){
echo "</td><td>";
if ($maxans-$countanswers==0) {
echo get_string('full', 'choice');
} elseif ($maxans-$countanswers==1) {
echo ($maxans - $countanswers);
echo " ".get_string('spaceleft', 'choice');
} else {
echo ($maxans - $countanswers);
echo " ".get_string('spacesleft', 'choice');
}
echo "</td>";
} else if ($choice->limitanswers && ($countanswers >= $maxans)) {
echo " <strong>".get_string('full', 'choice')."</strong>";
}
echo "</td>";
echo "</tr>";
$aid++;
}
}
echo "</table>";
break;
}
echo "<center>";
echo "<input type=\"hidden\" name=\"id\" value=\"$cm->id\" />";
if (!isguest()) {
echo "<input type=\"submit\" value=\"".get_string("savemychoice","choice")."\" />";
} else {
print_string('havetologin', 'choice');
}
echo "</center>";
choice_show_form($choice, $USER, $cm);
echo "</form>";
}
@ -238,202 +99,7 @@
( $choice->showresults == CHOICE_SHOWRESULTS_AFTER_ANSWER and $current ) or
( $choice->showresults == CHOICE_SHOWRESULTS_AFTER_CLOSE and $choice->timeclose <= time() ) ) {
print_heading(get_string("responses", "choice"));
if ($currentgroup) {
$users = get_group_users($currentgroup, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname');
} else {
$users = get_course_users($course->id, "u.firstname ASC", '', 'u.id, u.picture, u.firstname, u.lastname') + get_admins();
}
if (!$users) {
print_heading(get_string("nousersyet"));
}
if ($allresponses = get_records("choice_answers", "choiceid", $choice->id)) {
foreach ($allresponses as $aa) {
$answers[$aa->userid] = $aa;
}
} else {
$answers = array () ;
}
$timenow = time();
foreach ($choice->option as $optionid => $text) {
$useranswer[$optionid] = array();
}
foreach ($users as $user) {
if (!empty($user->id) and !empty($answers[$user->id])) {
$answer = $answers[$user->id];
$useranswer[(int)$answer->optionid][] = $user;
} else {
$useranswer[0][] = $user;
}
}
foreach ($choice->option as $optionid => $text) {
if (!$choice->option[$optionid]) {
unset($useranswer[$optionid]); // Throw away any data that doesn't apply
}
}
ksort($useranswer);
switch ($choice->publish) {
case CHOICE_PUBLISH_NAMES:
$isteacher = isteacher($course->id);
$tablewidth = (int) (100.0 / count($useranswer));
echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\" class=\"results names\">";
echo "<tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if ($optionid) {
echo "<th class=\"col$count header\" width=\"$tablewidth%\">";
} else if ($choice->showunanswered) {
echo "<th class=\"col$count header\" width=\"$tablewidth%\">";
} else {
continue;
}
echo format_string(choice_get_option_text($choice, $optionid));
echo "</th>";
$count++;
}
echo "</tr><tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if ($optionid) {
echo "<td class=\"col$count data\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
} else if ($choice->showunanswered) {
echo "<td class=\"col$count data\" width=\"$tablewidth%\" valign=\"top\" nowrap=\"nowrap\">";
} else {
continue;
}
echo "<table width=\"100%\">";
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 "<tr><td width=\"10\" nowrap=\"nowrap\" class=\"picture\">";
print_user_picture($user->id, $course->id, $user->picture);
echo "</td><td width=\"100%\" nowrap=\"nowrap\" class=\"fullname\">";
echo "<a href=\"$CFG->wwwroot/user/view.php?id=$user->id&amp;course=$course->id\">";
echo fullname($user, $isteacher);
echo "</a>";
echo "</td></tr>";
}
}
$count++;
echo "</table>";
echo "</td>";
}
echo "</tr><tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if (!$optionid and !$choice->showunanswered) {
continue;
}
echo "<td align=\"center\" class=\"count\">";
$countanswers = count_records("choice_answers", "optionid", $optionid);
if ($choice->limitanswers && !$optionid==0) {
echo get_string("taken", "choice").":";
echo $countanswers;
echo "<br>";
echo get_string("limit", "choice").":";
$choice_option = get_record("choice_options", "id", $optionid);
echo $choice_option->maxanswers;
}
echo "</td>";
$count++;
}
echo "</tr></table>";
break;
case CHOICE_PUBLISH_ANONYMOUS:
$tablewidth = (int) (100.0 / count($useranswer));
echo "<table cellpadding=\"5\" cellspacing=\"10\" align=\"center\" class=\"results anonymous\">";
echo "<tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if ($optionid) {
echo "<th width=\"$tablewidth%\" class=\"col$count header\">";
} else if ($choice->showunanswered) {
echo "<th width=\"$tablewidth%\" class=\"col$count header\">";
} else {
continue;
}
echo format_string(choice_get_option_text($choice, $optionid));
echo "</th>";
$count++;
}
echo "</tr><tr>";
$maxcolumn = 0;
foreach ($useranswer as $optionid => $userlist) {
if (!$optionid and !$choice->showunanswered) {
continue;
}
$column[$optionid] = 0;
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.
$column[$optionid]++;
}
}
if ($column[$optionid] > $maxcolumn) {
$maxcolumn = $column[$optionid];
}
}
echo "</tr><tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if (!$optionid and !$choice->showunanswered) {
continue;
}
$height = 0;
if ($maxcolumn) {
$height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn);
}
echo "<td valign=\"bottom\" align=\"center\" class=\"col$count data\">";
echo "<img src=\"column.png\" height=\"$height\" width=\"49\" alt=\"\" />";
echo "</td>";
$count++;
}
echo "</tr>";
echo "<tr>";
$count = 0;
foreach ($useranswer as $optionid => $userlist) {
if (!$optionid and !$choice->showunanswered) {
continue;
}
echo "<td align=\"center\" class=\"col$count count\">";
if ($choice->limitanswers && !$optionid==0) {
echo get_string("taken", "choice").":";
echo $column[$optionid];
echo "<br>";
echo get_string("limit", "choice").":";
$choice_option = get_record("choice_options", "id", $optionid);
echo $choice_option->maxanswers;
} else {
echo $column[$optionid];
}
echo "</td>";
$count++;
}
echo "</tr></table>";
break;
}
choice_show_results($choice, $course, $cm);
}
print_footer($course);