course)) { error("Course is misconfigured"); } require_course_login($course, false, $cm); if (!$choice = choice_get_choice($cm->instance)) { error("Course module is incorrect"); } 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 (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!"); } } redirect("view.php?id=$cm->id"); exit; } /// 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), "", "id\">$strchoices -> ".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 ''; } else if (!$cm->visible) { notice(get_string("activityiscurrentlyhidden")); } if ($choice->text) { print_simple_box(format_text($choice->text, $choice->format), 'center', '70%', '', 5, 'generalbox', 'intro'); } //if user has already made a selection, and they are not allowed to update it, show their selected answer. if (isset($USER->id) && ($current = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $USER->id)) && !$choice->allowupdate) { print_simple_box(get_string("yourselection", "choice", userdate($choice->timeopen)).": ".format_string(choice_get_option_text($choice, $current->optionid)), "center"); } /// Print the form if ($choice->timeopen > time() ) { print_simple_box(get_string("notopenyet", "choice", userdate($choice->timeopen)), "center"); print_footer($course); exit; } if ( (!$current or $choice->allowupdate) and ($choice->timeclose >= time() or $choice->timeclose == 0) ) { // They haven't made their choice yet or updates allowed and choice is open echo "
"; switch ($choice->display) { case CHOICE_DISPLAY_HORIZONTAL: echo ""; $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 ""; $aid++; } } echo ""; echo "
"; echo "limitanswers && ($countanswers >= $maxans) && !($answerchecked[$optionid]) ) { echo ' disabled="disabled"'; } echo " />"; if ($choice->limitanswers && ($countanswers >= $maxans) ) { echo format_text($text."
".get_string('full', 'choice').""); } else { echo format_text($text); } echo "
"; break; case CHOICE_DISPLAY_VERTICAL: $aid = 0; $displayoptions->para = false; echo ""; foreach ($choice->option as $optionid => $text) { if ($text) { echo ""; } else if ($choice->limitanswers && ($countanswers >= $maxans)) { echo " ".get_string('full', 'choice').""; } echo ""; echo ""; $aid++; } } echo "
"; echo ""; $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 ""; } } echo format_text($text. ' ', FORMAT_MOODLE, $displayoptions); if ($choice->limitanswers && ($choice->showresults==CHOICE_SHOWRESULTS_ALWAYS) ){ echo ""; 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 "
"; break; } echo "
"; echo "id\" />"; if (!isguest()) { echo ""; } else { print_string('havetologin', 'choice'); } echo "
"; echo "
"; } // print the results at the bottom of the screen if ( $choice->showresults == CHOICE_SHOWRESULTS_ALWAYS or ( $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 ""; echo ""; $count = 0; foreach ($useranswer as $optionid => $userlist) { if ($optionid) { echo ""; $count++; } echo ""; $count = 0; foreach ($useranswer as $optionid => $userlist) { if ($optionid) { echo ""; } echo ""; $count = 0; foreach ($useranswer as $optionid => $userlist) { if (!$optionid and !$choice->showunanswered) { continue; } echo ""; $count++; } echo "
"; } else if ($choice->showunanswered) { echo ""; } else { continue; } echo format_string(choice_get_option_text($choice, $optionid)); echo "
"; } else if ($choice->showunanswered) { echo ""; } else { continue; } echo ""; 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 ""; } } $count++; echo "
"; print_user_picture($user->id, $course->id, $user->picture); echo ""; echo "wwwroot/user/view.php?id=$user->id&course=$course->id\">"; echo fullname($user, $isteacher); echo ""; echo "
"; echo "
"; $countanswers = count_records("choice_answers", "optionid", $optionid); if ($choice->limitanswers && !$optionid==0) { echo get_string("taken", "choice").":"; echo $countanswers; echo "
"; echo get_string("limit", "choice").":"; $choice_option = get_record("choice_options", "id", $optionid); echo $choice_option->maxanswers; } echo "
"; break; case CHOICE_PUBLISH_ANONYMOUS: $tablewidth = (int) (100.0 / count($useranswer)); echo ""; echo ""; $count = 0; foreach ($useranswer as $optionid => $userlist) { if ($optionid) { echo ""; $count++; } echo ""; $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 ""; $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 ""; $count++; } echo ""; echo ""; $count = 0; foreach ($useranswer as $optionid => $userlist) { if (!$optionid and !$choice->showunanswered) { continue; } echo ""; $count++; } echo "
"; } else if ($choice->showunanswered) { echo ""; } else { continue; } echo format_string(choice_get_option_text($choice, $optionid)); echo "
"; echo "\"\""; echo "
"; if ($choice->limitanswers && !$optionid==0) { echo get_string("taken", "choice").":"; echo $column[$optionid]; echo "
"; echo get_string("limit", "choice").":"; $choice_option = get_record("choice_options", "id", $optionid); echo $choice_option->maxanswers; } else { echo $column[$optionid]; } echo "
"; break; } } print_footer($course); ?>