2004-09-12 15:06:29 +00:00
< ? php // $Id$
2001-11-22 06:23:56 +00:00
2003-01-05 14:19:20 +00:00
require_once ( " ../../config.php " );
require_once ( " lib.php " );
2001-11-22 06:23:56 +00:00
require_variable ( $id ); // Course Module ID
if ( ! $cm = get_record ( " course_modules " , " id " , $id )) {
error ( " Course Module ID was incorrect " );
}
if ( ! $course = get_record ( " course " , " id " , $cm -> course )) {
error ( " Course is misconfigured " );
}
2005-02-16 10:40:48 +00:00
require_course_login ( $course , false , $cm );
2001-11-22 06:23:56 +00:00
2002-09-08 09:42:07 +00:00
if ( ! $choice = choice_get_choice ( $cm -> instance )) {
2001-11-22 06:23:56 +00:00
error ( " Course module is incorrect " );
}
2005-03-30 17:32:23 +00:00
if ( $choice -> option ) {
foreach ( $choice -> option as $optionid => $text ) {
$answerchecked [ $optionid ] = '' ;
}
2003-01-01 06:34:13 +00:00
}
2005-03-30 17:32:23 +00:00
if ( isset ( $USER -> id ) && $current = get_record ( 'choice_answers' , 'choiceid' , $choice -> id , 'userid' , $USER -> id )) {
$answerchecked [ $current -> optionid ] = 'checked="checked"' ;
2004-08-22 14:38:47 +00:00
} else {
$current = false ;
2001-11-22 06:23:56 +00:00
}
2005-03-30 17:32:23 +00:00
/// Submit any new data if there is any
2003-01-02 14:49:23 +00:00
if ( $form = data_submitted ()) {
2002-08-12 08:56:36 +00:00
$timenow = time ();
2004-02-04 13:40:20 +00:00
if ( empty ( $form -> answer )) {
redirect ( " view.php?id= $cm->id " , get_string ( 'mustchooseone' , 'choice' ));
2001-11-22 06:23:56 +00:00
} else {
2005-04-14 21:10:39 +00:00
$countanswers = get_records ( " choice_answers " , " optionid " , $form -> answer );
if ( $countanswers ) {
$countanswers = count ( $countanswers );
} else {
$countanswers = 0 ;
2004-02-04 13:40:20 +00:00
}
2005-04-14 21:10:39 +00:00
$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 );
2004-02-04 13:40:20 +00:00
}
2005-04-14 21:10:39 +00:00
} else {
error ( " this choice is full! " );
2001-11-22 06:23:56 +00:00
}
}
2004-07-01 12:52:15 +00:00
redirect ( " view.php?id= $cm->id " );
2001-11-22 06:23:56 +00:00
exit ;
}
2005-03-30 17:32:23 +00:00
/// Display the choice and possibly results
2002-08-12 08:28:04 +00:00
$strchoice = get_string ( " modulename " , " choice " );
$strchoices = get_string ( " modulenameplural " , " choice " );
2004-01-31 15:22:04 +00:00
add_to_log ( $course -> id , " choice " , " view " , " view.php?id= $cm->id " , $choice -> id , $cm -> id );
2002-05-31 09:07:11 +00:00
2005-03-27 17:33:16 +00:00
print_header_simple ( format_string ( $choice -> name ), " " ,
" <a href= \" index.php?id= $course->id\ " > $strchoices </ a > -> " .format_string( $choice->name ), " " , " " , true,
2002-11-10 07:37:15 +00:00
update_module_button ( $cm -> id , $course -> id , $strchoice ), navmenu ( $course , $cm ));
2001-11-22 06:23:56 +00:00
2004-02-15 08:17:45 +00:00
/// 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 ;
}
2001-11-22 06:23:56 +00:00
if ( isteacher ( $course -> id )) {
2005-04-01 02:37:19 +00:00
if ( $allanswers = get_records ( " choice_answers " , " choiceid " , $choice -> id )) {
$responsecount = 0 ;
foreach ( $allanswers as $aa ) {
2005-04-01 03:33:44 +00:00
if ( isstudent ( $course -> id , $aa -> userid ) or isteacher ( $course -> id , $aa -> userid )) { //check to make sure user is enrolled in course.
2005-04-01 02:37:19 +00:00
$responsecount ++ ;
}
}
2002-08-18 09:52:51 +00:00
} else {
$responsecount = 0 ;
}
2005-04-13 07:45:47 +00:00
echo '<div class="reportlink">' ;
echo " <a href= \" report.php?id= $cm->id\ " > " .get_string( " viewallresponses " , " choice " , $responsecount ). " </ a > " ;
echo '</div>' ;
2003-05-15 18:03:22 +00:00
} else if ( ! $cm -> visible ) {
notice ( get_string ( " activityiscurrentlyhidden " ));
2001-11-22 06:23:56 +00:00
}
2005-03-13 16:17:55 +00:00
if ( $choice -> text ) {
print_simple_box ( format_text ( $choice -> text , $choice -> format ), 'center' , '70%' , '' , 5 , 'generalbox' , 'intro' );
}
2001-11-22 06:23:56 +00:00
2005-04-15 01:40:22 +00:00
//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 " );
}
2004-07-01 12:52:15 +00:00
2005-03-30 17:32:23 +00:00
/// Print the form
2005-02-16 10:40:48 +00:00
2004-07-01 12:52:15 +00:00
if ( $choice -> timeopen > time () ) {
print_simple_box ( get_string ( " notopenyet " , " choice " , userdate ( $choice -> timeopen )), " center " );
2005-03-17 15:12:07 +00:00
print_footer ( $course );
2004-07-01 12:52:15 +00:00
exit ;
}
2005-02-16 10:40:48 +00:00
if ( ( ! $current or $choice -> allowupdate ) and ( $choice -> timeclose >= time () or $choice -> timeclose == 0 ) ) {
2004-07-01 12:52:15 +00:00
// They haven't made their choice yet or updates allowed and choice is open
2005-02-16 10:40:48 +00:00
2005-03-29 23:46:17 +00:00
echo " <form name= \" form \" method= \" post \" action= \" view.php \" > " ;
2005-03-30 17:32:23 +00:00
switch ( $choice -> display ) {
case CHOICE_DISPLAY_HORIZONTAL :
echo " <table cellpadding= \" 20 \" cellspacing= \" 20 \" align= \" center \" ><tr> " ;
2005-04-14 21:10:39 +00:00
$aid = 0 ;
2005-03-30 17:32:23 +00:00
foreach ( $choice -> option as $optionid => $text ) {
2005-04-14 21:10:39 +00:00
if ( $text ) {
echo " <td align= \" center \" valign= \" top \" > " ;
echo " <input type= \" radio \" name= \" answer \" value= \" " . $optionid . " \" " . $answerchecked [ $optionid ] . " alt= \" " . strip_tags ( format_text ( $text )) . " \" /> " ;
$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 . " <br><strong> " . get_string ( 'full' , 'choice' ) . " </strong> " );
} else {
echo format_text ( $text );
}
2005-03-30 17:32:23 +00:00
echo " </td> " ;
2005-04-14 21:10:39 +00:00
$aid ++ ;
2005-03-30 17:32:23 +00:00
}
}
echo " </tr> " ;
2005-03-30 18:48:42 +00:00
echo " </table> " ;
2005-03-30 17:32:23 +00:00
break ;
case CHOICE_DISPLAY_VERTICAL :
2005-04-14 21:10:39 +00:00
$aid = 0 ;
2005-04-07 22:22:22 +00:00
$displayoptions -> para = false ;
2005-04-14 21:10:39 +00:00
echo " <table cellpadding= \" 10 \" cellspacing= \" 10 \" align= \" center \" > " ;
2005-03-30 17:32:23 +00:00
foreach ( $choice -> option as $optionid => $text ) {
2005-03-31 22:48:43 +00:00
if ( $text ) {
2005-04-14 21:10:39 +00:00
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 -> release == CHOICE_RELEASE_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> " ;
}
2005-03-30 17:32:23 +00:00
echo " </td> " ;
echo " </tr> " ;
2005-04-14 21:10:39 +00:00
$aid ++ ;
2005-03-29 23:46:17 +00:00
}
}
2005-03-30 18:48:42 +00:00
echo " </table> " ;
2005-03-30 17:32:23 +00:00
break ;
}
2004-10-22 07:45:58 +00:00
echo " <center> " ;
2004-09-12 15:06:29 +00:00
echo " <input type= \" hidden \" name= \" id \" value= \" $cm->id\ " /> " ;
2005-04-01 03:33:44 +00:00
if ( isstudent ( $course -> id ) or isteacher ( $course -> id , 0 )) {
2004-09-12 15:06:29 +00:00
echo " <input type= \" submit \" value= \" " . get_string ( " savemychoice " , " choice " ) . " \" /> " ;
2004-08-22 14:38:47 +00:00
} else {
print_string ( 'havetologin' , 'choice' );
2003-01-01 06:34:13 +00:00
}
2004-10-22 07:45:58 +00:00
echo " </center> " ;
echo " </form> " ;
2002-09-08 09:42:07 +00:00
2004-07-01 12:52:15 +00:00
}
2005-02-16 10:40:48 +00:00
2003-01-01 06:34:13 +00:00
2005-03-29 23:46:17 +00:00
// print the results at the bottom of the screen
2004-07-01 12:52:15 +00:00
if ( $choice -> release == CHOICE_RELEASE_ALWAYS or
( $choice -> release == CHOICE_RELEASE_AFTER_ANSWER and $current ) or
( $choice -> release == CHOICE_RELEASE_AFTER_CLOSE and $choice -> timeclose <= time () ) ) {
2005-02-16 10:40:48 +00:00
2003-01-01 06:34:13 +00:00
print_heading ( get_string ( " responses " , " choice " ));
2004-02-15 08:17:45 +00:00
if ( $currentgroup ) {
2005-04-01 03:26:35 +00:00
$users = get_group_users ( $currentgroup , " u.firstname ASC " , '' , 'u.id, u.picture, u.firstname, u.lastname' ) + get_admins ();
2004-02-15 08:17:45 +00:00
} else {
2005-04-01 03:26:35 +00:00
$users = get_course_users ( $course -> id , " u.firstname ASC " , '' , 'u.id, u.picture, u.firstname, u.lastname' ) + get_admins ();
2004-02-15 08:17:45 +00:00
}
2005-03-30 17:32:23 +00:00
2004-02-15 08:17:45 +00:00
if ( ! $users ) {
print_heading ( get_string ( " nousersyet " ));
print_footer ( $course );
exit ;
2003-01-01 06:34:13 +00:00
}
2001-11-22 06:23:56 +00:00
2005-03-30 17:32:23 +00:00
if ( $allresponses = get_records ( " choice_answers " , " choiceid " , $choice -> id )) {
2005-03-29 23:46:17 +00:00
foreach ( $allresponses as $aa ) {
2003-01-01 06:34:13 +00:00
$answers [ $aa -> userid ] = $aa ;
}
} else {
$answers = array () ;
}
$timenow = time ();
2005-03-30 17:32:23 +00:00
foreach ( $choice -> option as $optionid => $text ) {
$useranswer [ $optionid ] = array ();
2003-01-01 06:34:13 +00:00
}
foreach ( $users as $user ) {
if ( ! empty ( $user -> id ) and ! empty ( $answers [ $user -> id ])) {
$answer = $answers [ $user -> id ];
2005-03-30 17:32:23 +00:00
$useranswer [( int ) $answer -> optionid ][] = $user ;
2003-01-01 06:34:13 +00:00
} else {
2004-01-01 06:35:22 +00:00
$useranswer [ 0 ][] = $user ;
2003-01-01 06:34:13 +00:00
}
}
2005-03-30 17:32:23 +00:00
foreach ( $choice -> option as $optionid => $text ) {
if ( ! $choice -> option [ $optionid ]) {
unset ( $useranswer [ $optionid ]); // Throw away any data that doesn't apply
2003-01-01 06:34:13 +00:00
}
}
ksort ( $useranswer );
switch ( $choice -> publish ) {
case CHOICE_PUBLISH_NAMES :
2003-11-28 15:41:18 +00:00
$isteacher = isteacher ( $course -> id );
2003-01-01 06:34:13 +00:00
$tablewidth = ( int ) ( 100.0 / count ( $useranswer ));
2004-09-12 15:06:29 +00:00
echo " <table cellpadding= \" 5 \" cellspacing= \" 10 \" align= \" center \" > " ;
2003-05-17 06:17:10 +00:00
echo " <tr> " ;
2005-03-30 17:32:23 +00:00
foreach ( $useranswer as $optionid => $userlist ) {
if ( $optionid ) {
echo " <th class= \" col $optionid\ " width = \ " $tablewidth % \" > " ;
2004-01-01 06:35:22 +00:00
} else if ( $choice -> showunanswered ) {
2005-03-30 17:32:23 +00:00
echo " <th class= \" col $optionid\ " width = \ " $tablewidth % \" > " ;
2003-01-01 06:34:13 +00:00
} else {
2004-01-01 06:35:22 +00:00
continue ;
2003-01-01 06:34:13 +00:00
}
2005-03-30 17:32:23 +00:00
echo format_string ( choice_get_option_text ( $choice , $optionid ));
2003-05-17 06:17:10 +00:00
echo " </th> " ;
2003-01-01 06:34:13 +00:00
}
2003-05-17 06:17:10 +00:00
echo " </tr><tr> " ;
2005-02-16 10:40:48 +00:00
2005-03-30 17:32:23 +00:00
foreach ( $useranswer as $optionid => $userlist ) {
if ( $optionid ) {
echo " <td class= \" col $optionid\ " width = \ " $tablewidth % \" valign= \" top \" nowrap= \" nowrap \" > " ;
2004-01-01 06:35:22 +00:00
} else if ( $choice -> showunanswered ) {
2005-03-30 17:32:23 +00:00
echo " <td class= \" col $optionid\ " width = \ " $tablewidth % \" valign= \" top \" nowrap= \" nowrap \" > " ;
2004-01-01 06:35:22 +00:00
} else {
continue ;
2003-01-01 06:34:13 +00:00
}
2005-02-16 10:40:48 +00:00
2004-09-12 15:06:29 +00:00
echo " <table width= \" 100% \" > " ;
2005-03-30 17:32:23 +00:00
foreach ( $userlist as $user ) {
2005-04-05 00:35:05 +00:00
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 \" > " ;
print_user_picture ( $user -> id , $course -> id , $user -> picture );
echo " </td><td width= \" 100% \" nowrap= \" nowrap \" > " ;
2005-04-22 19:17:06 +00:00
echo fullname ( $user , $isteacher );
2005-04-05 00:35:05 +00:00
echo " </td></tr> " ;
}
2003-01-01 06:34:13 +00:00
}
2003-05-17 06:17:10 +00:00
echo " </table> " ;
2005-02-16 10:40:48 +00:00
2003-05-17 06:17:10 +00:00
echo " </td> " ;
2003-01-01 06:34:13 +00:00
}
2005-04-14 21:10:39 +00:00
echo " </tr><tr> " ;
foreach ( $useranswer as $optionid => $userlist ) {
if ( ! $optionid and ! $choice -> showunanswered ) {
continue ;
}
echo " <td align= \" center \" > " ;
$countanswers = get_records ( " choice_answers " , " optionid " , $optionid );
if ( $countanswers ) {
$countanswers = count ( $countanswers );
} else {
$countanswers = 0 ;
}
if ( $choice -> limitanswers && ! $optionid == 0 ) {
echo get_string ( " taken " , " choice " ) . " : " ;
echo $countanswers ;
echo " <br> " ;
echo get_string ( " limit " , " choice " ) . " : " ;
2005-04-15 08:13:27 +00:00
$choice_option = get_record ( " choice_options " , " id " , $optionid );
echo $choice_option -> maxanswers ;
2005-04-14 21:10:39 +00:00
echo " </td> " ;
}
}
2003-05-17 06:17:10 +00:00
echo " </tr></table> " ;
2003-01-01 06:34:13 +00:00
break ;
case CHOICE_PUBLISH_ANONYMOUS :
$tablewidth = ( int ) ( 100.0 / count ( $useranswer ));
2004-09-12 15:06:29 +00:00
echo " <table cellpadding= \" 5 \" cellspacing= \" 10 \" align= \" center \" > " ;
2003-05-17 06:17:10 +00:00
echo " <tr> " ;
2005-03-30 17:32:23 +00:00
foreach ( $useranswer as $optionid => $userlist ) {
if ( $optionid ) {
2003-05-17 06:17:10 +00:00
echo " <th width= \" $tablewidth % \" > " ;
2004-01-01 06:35:22 +00:00
} else if ( $choice -> showunanswered ) {
2005-02-09 17:15:27 +00:00
echo " <th width= \" $tablewidth % \" > " ;
2004-01-01 06:35:22 +00:00
} else {
continue ;
2003-01-01 06:34:13 +00:00
}
2005-04-05 23:08:00 +00:00
echo format_string ( choice_get_option_text ( $choice , $optionid ));
2003-05-17 06:17:10 +00:00
echo " </th> " ;
2003-01-01 06:34:13 +00:00
}
2005-04-14 21:10:39 +00:00
echo " </tr><tr> " ;
2003-01-01 06:34:13 +00:00
$maxcolumn = 0 ;
2005-03-30 17:32:23 +00:00
foreach ( $useranswer as $optionid => $userlist ) {
if ( ! $optionid and ! $choice -> showunanswered ) {
2004-01-01 06:35:22 +00:00
continue ;
2005-04-14 21:10:39 +00:00
}
2005-04-12 20:49:51 +00:00
$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 ] ++ ;
}
2005-04-14 21:10:39 +00:00
}
2005-03-30 17:32:23 +00:00
if ( $column [ $optionid ] > $maxcolumn ) {
$maxcolumn = $column [ $optionid ];
2003-01-01 06:34:13 +00:00
}
}
2005-04-14 21:10:39 +00:00
echo " </tr><tr> " ;
2005-03-30 17:32:23 +00:00
foreach ( $useranswer as $optionid => $userlist ) {
if ( ! $optionid and ! $choice -> showunanswered ) {
2004-01-01 06:35:22 +00:00
continue ;
}
2004-03-14 01:22:28 +00:00
$height = 0 ;
if ( $maxcolumn ) {
2005-03-30 17:32:23 +00:00
$height = $COLUMN_HEIGHT * (( float ) $column [ $optionid ] / ( float ) $maxcolumn );
2004-03-14 01:22:28 +00:00
}
2003-05-17 06:17:10 +00:00
echo " <td valign= \" bottom \" align= \" center \" > " ;
2004-09-16 17:13:57 +00:00
echo " <img src= \" column.png \" height= \" $height\ " width = \ " 49 \" alt= \" \" /> " ;
2004-01-01 06:35:22 +00:00
echo " </td> " ;
2003-01-01 06:34:13 +00:00
}
2003-05-17 06:17:10 +00:00
echo " </tr> " ;
2003-01-01 06:34:13 +00:00
2003-05-17 06:17:10 +00:00
echo " <tr> " ;
2005-03-30 17:32:23 +00:00
foreach ( $useranswer as $optionid => $userlist ) {
if ( ! $optionid and ! $choice -> showunanswered ) {
2004-01-01 06:35:22 +00:00
continue ;
}
2005-04-14 21:10:39 +00:00
echo " <td align= \" center \" > " ;
if ( $choice -> limitanswers && ! $optionid == 0 ) {
echo get_string ( " taken " , " choice " ) . " : " ;
echo $column [ $optionid ];
echo " <br> " ;
echo get_string ( " limit " , " choice " ) . " : " ;
2005-04-15 08:13:27 +00:00
$choice_option = get_record ( " choice_options " , " id " , $optionid );
echo $choice_option -> maxanswers ;
2005-04-14 21:10:39 +00:00
echo " </td> " ;
} else {
echo $column [ $optionid ];
}
2003-01-01 06:34:13 +00:00
}
2003-05-17 06:17:10 +00:00
echo " </tr></table> " ;
2003-01-01 06:34:13 +00:00
break ;
}
}
2005-02-16 10:40:48 +00:00
2001-11-22 06:23:56 +00:00
print_footer ( $course );
?>