get_string('publishanonymous', 'choice'), CHOICE_PUBLISH_NAMES => get_string('publishnames', 'choice')); $CHOICE_SHOWRESULTS = array (CHOICE_SHOWRESULTS_NOT => get_string('publishnot', 'choice'), CHOICE_SHOWRESULTS_AFTER_ANSWER => get_string('publishafteranswer', 'choice'), CHOICE_SHOWRESULTS_AFTER_CLOSE => get_string('publishafterclose', 'choice'), CHOICE_SHOWRESULTS_ALWAYS => get_string('publishalways', 'choice')); $CHOICE_DISPLAY = array (CHOICE_DISPLAY_HORIZONTAL => get_string('displayhorizontal', 'choice'), CHOICE_DISPLAY_VERTICAL => get_string('displayvertical','choice')); /// Standard functions ///////////////////////////////////////////////////////// function choice_user_outline($course, $user, $mod, $choice) { if ($answer = get_record('choice_answers', 'choiceid', $choice->id, 'userid', $user->id)) { $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'"; $result->time = $answer->timemodified; return $result; } return NULL; } function choice_user_complete($course, $user, $mod, $choice) { if ($answer = get_record('choice_answers', "choiceid", $choice->id, "userid", $user->id)) { $result->info = "'".format_string(choice_get_option_text($choice, $answer->optionid))."'"; $result->time = $answer->timemodified; echo get_string("answered", "choice").": $result->info. ".get_string("updated", '', userdate($result->time)); } else { print_string("notanswered", "choice"); } } function choice_add_instance($choice) { // Given an object containing all the necessary data, // (defined by the form in mod.html) this function // will create a new instance and return the id number // of the new instance. $choice->timemodified = time(); if (empty($choice->timerestrict)) { $choice->timeopen = 0; $choice->timeclose = 0; } //insert answers if ($choice->id = insert_record("choice", $choice)) { foreach ($choice->option as $key => $value) { $value = trim($value); if (isset($value) && $value <> '') { $option = new object(); $option->text = $value; $option->choiceid = $choice->id; if (isset($choice->limit[$key])) { $option->maxanswers = $choice->limit[$key]; } $option->timemodified = time(); insert_record("choice_options", $option); } } } return $choice->id; } function choice_update_instance($choice) { // Given an object containing all the necessary data, // (defined by the form in mod.html) this function // will update an existing instance with new data. $choice->id = $choice->instance; $choice->timemodified = time(); if (empty($choice->timerestrict)) { $choice->timeopen = 0; $choice->timeclose = 0; } //update, delete or insert answers foreach ($choice->option as $key => $value) { $value = trim($value); $option = new object(); $option->text = $value; $option->choiceid = $choice->id; if (isset($choice->limit[$key])) { $option->maxanswers = $choice->limit[$key]; } $option->timemodified = time(); if (isset($choice->optionid[$key]) && !empty($choice->optionid[$key])){//existing choice record $option->id=$choice->optionid[$key]; if (isset($value) && $value <> '') { update_record("choice_options", $option); } else { //empty old option - needs to be deleted. delete_records("choice_options", "id", $option->id); } } else { if (isset($value) && $value <> '') { insert_record("choice_options", $option); } } } return update_record('choice', $choice); } function choice_show_form($choice, $user, $cm, $allresponses) { //$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; $choicefull = false; $cdisplay = array(); if ($choice->limitanswers) { //set choicefull to true by default if limitanswers. $choicefull = true; } $context = get_context_instance(CONTEXT_MODULE, $cm->id); foreach ($choice->option as $optionid => $text) { if (isset($text)) { //make sure there are no dud entries in the db with blank text values. $cdisplay[$aid]->optionid = $optionid; $cdisplay[$aid]->text = $text; $cdisplay[$aid]->maxanswers = $choice->maxanswers[$optionid]; if (isset($allresponses[$optionid])) { $cdisplay[$aid]->countanswers = count($allresponses[$optionid]); } else { $cdisplay[$aid]->countanswers = 0; } 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 && ($cdisplay[$aid]->countanswers >= $cdisplay[$aid]->maxanswers) && (empty($cdisplay[$aid]->checked)) ) { $cdisplay[$aid]->disabled = ' disabled="disabled" '; } else { $cdisplay[$aid]->disabled = ''; if ($choice->limitanswers && ($cdisplay[$aid]->countanswers < $cdisplay[$aid]->maxanswers)) { $choicefull = false; //set $choicefull to false - as the above condition hasn't been set. } } $aid++; } } switch ($choice->display) { case CHOICE_DISPLAY_HORIZONTAL: echo "
";
echo "optionid."\" alt=\"".strip_tags(format_text($cd->text))."\"". $cd->checked.$cd->disabled." />";
if (!empty($cd->disabled)) {
echo format_text($cd->text." ".get_string('full', 'choice').""); } else { echo format_text($cd->text); } echo " | ";
}
echo "
"; echo "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 " | "; 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->maxanswers - $cd->countanswers); echo " ".get_string('spacesleft', 'choice'); } echo " | "; } else if ($choice->limitanswers && ($cd->countanswers >= $cd->maxanswers)) { //if limitanswers and answers exceeded, display "full" beside the choice. echo " ".get_string('full', 'choice').""; } echo ""; echo "
"; print_string('notanswered', 'choice'); echo " | "; $column[0] = 0; foreach ($allresponses[0] as $user) { $column[0]++; } $maxcolumn = $column[0]; } $count = 1; foreach ($choice->option as $optionid => $optiontext) { echo ""; echo format_string($optiontext); echo " | "; $column[$optionid] = 0; if (isset($allresponses[$optionid])) { $column[$optionid] = count($allresponses[$optionid]); if ($column[$optionid] > $maxcolumn) { $maxcolumn = $column[$optionid]; } } else { $column[$optionid] = 0; } } echo "
---|---|
"; echo ""; echo " | "; } $count = 1; foreach ($choice->option as $optionid => $optiontext) { if ($maxcolumn) { $height = $COLUMN_HEIGHT * ((float)$column[$optionid] / (float)$maxcolumn); } echo ""; echo ""; echo " | "; $count++; } echo "
';
if (!$choice->limitanswers) {
echo $column[0];
echo ' ('.format_float(((float)$column[0]/(float)$totalresponsecount)*100.0,1).'%)'; } echo ' | ';
}
$count = 1;
foreach ($choice->option as $optionid => $optiontext) {
echo "";
if ($choice->limitanswers) {
echo get_string("taken", "choice").":";
echo $column[$optionid].' '; echo get_string("limit", "choice").":"; $choice_option = get_record("choice_options", "id", $optionid); echo $choice_option->maxanswers; } else { echo $column[$optionid]; echo ' ('.format_float(((float)$column[$optionid]/(float)$totalresponsecount)*100.0,1).'%)'; } echo " | ";
$count++;
}
echo "