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
2005-04-14 19:00:23 +00:00
$id = required_param ( 'id' ); //moduleid
2001-11-22 06:23:56 +00:00
2005-04-14 19:00:23 +00:00
$format = optional_param ( 'format' , CHOICE_PUBLISH_NAMES , PARAM_INT );
$download = optional_param ( 'download' , '' , PARAM_ALPHA );
2005-03-30 18:17:58 +00:00
2001-11-22 06:23:56 +00:00
if ( ! $cm = get_record ( " course_modules " , " id " , $id )) {
error ( " Course Module ID was incorrect " );
}
if ( ! $course = get_record ( " course " , " id " , $cm -> course )) {
error ( " Course module is misconfigured " );
}
2005-02-16 10:40:48 +00:00
require_login ( $course -> id , false , $cm );
2001-11-22 06:23:56 +00:00
if ( ! isteacher ( $course -> id )) {
error ( " Only teachers can look at this page " );
}
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 " );
}
2002-08-12 08:28:04 +00:00
$strchoice = get_string ( " modulename " , " choice " );
$strchoices = get_string ( " modulenameplural " , " choice " );
$strresponses = get_string ( " responses " , " choice " );
2005-04-10 16:55:32 +00:00
add_to_log ( $course -> id , " choice " , " report " , " report.php?id= $cm->id " , " $choice->id " , $cm -> id );
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 , " report.php?id= $cm->id " );
} else {
$currentgroup = false ;
}
if ( $currentgroup ) {
2005-08-14 21:59:09 +00:00
$users = get_group_users ( $currentgroup , " u.firstname ASC " , '' , 'u.id, u.picture, u.firstname, u.lastname' );
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
}
2001-11-22 06:23:56 +00:00
2004-02-15 08:17:45 +00:00
if ( ! $users ) {
print_heading ( get_string ( " nousersyet " ));
2001-11-22 06:23:56 +00:00
}
2005-03-30 18:17:58 +00:00
if ( $allresponses = get_records ( " choice_answers " , " choiceid " , $choice -> id )) {
2005-03-29 23:46:17 +00:00
foreach ( $allresponses as $aa ) {
2002-12-23 09:39:26 +00:00
$answers [ $aa -> userid ] = $aa ;
2001-11-22 06:23:56 +00:00
}
} else {
$answers = array () ;
}
$timenow = time ();
2005-03-30 18:17:58 +00:00
foreach ( $choice -> option as $optionid => $text ) {
$useranswer [ $optionid ] = array ();
2002-08-18 09:47:59 +00:00
}
2002-08-18 09:43:01 +00:00
foreach ( $users as $user ) {
2005-03-30 18:17:58 +00:00
if ( ! empty ( $user -> id ) and ! empty ( $answers [ $user -> id ])) {
2003-01-15 10:55:54 +00:00
$answer = $answers [ $user -> id ];
2005-03-30 18:17:58 +00:00
$useranswer [( int ) $answer -> optionid ][] = $user ;
2003-01-15 10:55:54 +00:00
} else {
2005-03-30 18:17:58 +00:00
$useranswer [ 0 ][] = $user ;
2003-01-15 10:55:54 +00:00
}
2002-08-18 09:43:01 +00:00
}
2005-03-30 18:17:58 +00:00
foreach ( $choice -> option as $optionid => $text ) {
if ( ! $choice -> option [ $optionid ]) {
unset ( $useranswer [ $optionid ]); // Throw away any data that doesn't apply
2002-09-08 09:42:07 +00:00
}
}
2002-08-18 09:43:01 +00:00
ksort ( $useranswer );
2005-04-11 22:59:32 +00:00
//print spreadsheet if one is asked for:
if ( $download == " xls " ) {
require_once ( " $CFG->libdir /excel/Worksheet.php " );
require_once ( " $CFG->libdir /excel/Workbook.php " );
// HTTP headers
2005-04-14 19:00:23 +00:00
$filename = clean_filename ( " $course->shortname " . strip_tags ( format_string ( $choice -> name , true ))) . '.xls' ;
2005-04-11 22:59:32 +00:00
header ( " Content-type: application/vnd.ms-excel " );
header ( " Content-Disposition: attachment; filename= $filename " );
header ( " Expires: 0 " );
header ( " Cache-Control: must-revalidate, post-check=0,pre-check=0 " );
header ( " Pragma: public " );
// Creating a workbook
$workbook = new Workbook ( " - " );
// Creating the first worksheet
$myxls =& $workbook -> add_worksheet ( 'Responses' );
$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 " ));
/// generate the data for the body of the spreadsheet
$i = 0 ;
$row = 1 ;
if ( $users ) foreach ( $users as $user ) {
2005-04-12 22:53:22 +00:00
if ( ! ( $answers [ $user -> id ] -> optionid == 0 && isadmin ( $user -> id )) &&
( ! ( $answers [ $user -> id ] -> optionid == 0 && isteacher ( $course -> id , $user -> id ) && ! ( isteacheredit ( $course -> id , $user -> id )) ) ) &&
! ( $choice -> showunanswered == 0 && $answers [ $user -> id ] -> optionid == 0 ) ) { //make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
$myxls -> write_string ( $row , 0 , $user -> lastname );
$myxls -> write_string ( $row , 1 , $user -> firstname );
$studentid = (( $user -> idnumber != " " ) ? $user -> idnumber : " " );
$myxls -> write_string ( $row , 2 , $studentid );
$useroption = choice_get_option_text ( $choice , $answers [ $user -> id ] -> optionid );
if ( isset ( $useroption )) {
2005-04-14 19:00:23 +00:00
$myxls -> write_string ( $row , 3 , format_string ( $useroption , true ));
2005-04-12 22:53:22 +00:00
}
$row ++ ;
2005-04-11 22:59:32 +00:00
}
$pos = 4 ;
}
$workbook -> close ();
exit ;
}
// print text file
if ( $download == " txt " ) {
2005-04-14 19:00:23 +00:00
$filename = clean_filename ( " $course->shortname " . strip_tags ( format_string ( $choice -> name , true ))) . '.txt' ;
2005-04-11 22:59:32 +00:00
header ( " Content-Type: application/download \n " );
header ( " Content-Disposition: attachment; filename= \" " . $filename . " \" " );
/// Print names of all the fields
echo get_string ( " firstname " ) . " \t " . get_string ( " lastname " ) . " \t " . get_string ( " idnumber " ) . " \t " ;
echo get_string ( " choice " , " choice " ) . " \n " ;
/// generate the data for the body of the spreadsheet
$i = 0 ;
$row = 1 ;
if ( $users ) foreach ( $users as $user ) {
2005-04-12 22:53:22 +00:00
if ( ! ( $answers [ $user -> id ] -> optionid == 0 && isadmin ( $user -> id )) &&
( ! ( $answers [ $user -> id ] -> optionid == 0 && isteacher ( $course -> id , $user -> id ) && ! ( isteacheredit ( $course -> id , $user -> id )) ) ) &&
! ( $choice -> showunanswered == 0 && $answers [ $user -> id ] -> optionid == 0 ) ) { //make sure admins and hidden teachers are not shown in not answered yet column, and not answered only shown if set in config page.
2005-04-11 22:59:32 +00:00
echo $user -> lastname ;
echo " \t " . $user -> firstname ;
$studentid = (( $user -> idnumber != " " ) ? $user -> idnumber : " " );
echo " \t " . $studentid . " \t " ;
2005-04-14 19:00:23 +00:00
echo format_string ( choice_get_option_text ( $choice , $answers [ $user -> id ] -> optionid ), true ) . " \n " ;
2005-04-11 22:59:32 +00:00
}
$row ++ ;
}
exit ;
}
print_header_simple ( format_string ( $choice -> name ) . " : $strresponses " , " " ,
" <a href= \" index.php?id= $course->id\ " > $strchoices </ a > ->
2005-05-05 18:34:20 +00:00
< 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 ));
2001-11-22 06:23:56 +00:00
2005-03-30 18:17:58 +00:00
switch ( $format ) {
case CHOICE_PUBLISH_NAMES :
$tablewidth = ( int ) ( 100.0 / count ( $useranswer ));
2005-05-05 18:34:20 +00:00
echo " <table cellpadding= \" 5 \" cellspacing= \" 10 \" align= \" center \" class= \" results names \" > " ;
2005-03-30 18:17:58 +00:00
echo " <tr> " ;
2005-05-05 18:34:20 +00:00
$count = 0 ;
2005-03-30 18:17:58 +00:00
foreach ( $useranswer as $optionid => $userlist ) {
if ( $optionid ) {
2005-05-05 18:34:20 +00:00
echo " <th class= \" col $count header \" width= \" $tablewidth % \" > " ;
2005-03-30 18:17:58 +00:00
} else if ( $choice -> showunanswered ) {
2005-05-05 18:34:20 +00:00
echo " <th class= \" col $count header \" width= \" $tablewidth % \" > " ;
2005-03-30 18:17:58 +00:00
} else {
continue ;
}
echo format_string ( choice_get_option_text ( $choice , $optionid ));
echo " </th> " ;
2005-05-05 18:34:20 +00:00
$count ++ ;
2005-03-30 18:17:58 +00:00
}
echo " </tr><tr> " ;
2005-05-05 18:34:20 +00:00
$count = 0 ;
2005-03-30 18:17:58 +00:00
foreach ( $useranswer as $optionid => $userlist ) {
if ( $optionid ) {
2005-05-05 18:34:20 +00:00
echo " <td class= \" col $count data \" width= \" $tablewidth % \" valign= \" top \" nowrap= \" nowrap \" > " ;
2005-03-30 18:17:58 +00:00
} else if ( $choice -> showunanswered ) {
2005-05-05 18:34:20 +00:00
echo " <td class= \" col $count data \" width= \" $tablewidth % \" valign= \" top \" nowrap= \" nowrap \" > " ;
2005-03-30 18:17:58 +00:00
} else {
continue ;
}
2005-05-05 18:34:20 +00:00
echo " <table width= \" 100% \" cellpadding= \" 0 \" cellspacing= \" 0 \" > " ;
2005-03-30 18:17:58 +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.
2005-05-05 18:34:20 +00:00
echo " <tr><td width= \" 10 \" nowrap= \" nowrap \" class= \" picture \" > " ;
2005-04-05 00:35:05 +00:00
print_user_picture ( $user -> id , $course -> id , $user -> picture );
2005-05-05 18:34:20 +00:00
echo " </td><td width= \" 100% \" nowrap= \" nowrap \" class= \" fullname \" > " ;
2005-04-05 00:35:05 +00:00
echo " <p> " . fullname ( $user , true ) . " </p> " ;
echo " </td></tr> " ;
}
2005-03-30 18:17:58 +00:00
}
echo " </table> " ;
echo " </td> " ;
2005-05-05 18:34:20 +00:00
$count ++ ;
2005-03-30 18:17:58 +00:00
}
echo " </tr></table> " ;
break ;
case CHOICE_PUBLISH_ANONYMOUS :
$tablewidth = ( int ) ( 100.0 / count ( $useranswer ));
2005-05-05 18:34:20 +00:00
echo " <table cellpadding= \" 5 \" cellspacing= \" 10 \" align= \" center \" class= \" results anonymous \" > " ;
2005-03-30 18:17:58 +00:00
echo " <tr> " ;
2005-05-05 18:34:20 +00:00
$count = 0 ;
2005-03-30 18:17:58 +00:00
foreach ( $useranswer as $optionid => $userlist ) {
if ( $optionid ) {
2005-05-05 18:34:20 +00:00
echo " <th width= \" $tablewidth % \" class= \" col $count header \" > " ;
2005-03-30 18:17:58 +00:00
} else if ( $choice -> showunanswered ) {
2005-05-05 18:34:20 +00:00
echo " <th width= \" $tablewidth % \" class= \" col $count header \" > " ;
2005-03-30 18:17:58 +00:00
} else {
continue ;
}
echo choice_get_option_text ( $choice , $optionid );
echo " </th> " ;
2005-05-05 18:34:20 +00:00
$count ++ ;
2005-03-30 18:17:58 +00:00
}
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> " ;
2005-05-05 18:34:20 +00:00
$count = 0 ;
2005-03-30 18:17:58 +00:00
foreach ( $useranswer as $optionid => $userlist ) {
if ( ! $optionid and ! $choice -> showunanswered ) {
continue ;
}
$height = 0 ;
if ( $maxcolumn ) {
$height = $COLUMN_HEIGHT * (( float ) $column [ $optionid ] / ( float ) $maxcolumn );
}
2005-05-05 18:34:20 +00:00
echo " <td valign= \" bottom \" align= \" center \" class= \" col $count data \" > " ;
2005-03-30 18:17:58 +00:00
echo " <img src= \" column.png \" height= \" $height\ " width = \ " 49 \" alt= \" \" /> " ;
echo " </td> " ;
2005-05-05 18:34:20 +00:00
$count ++ ;
2005-03-30 18:17:58 +00:00
}
echo " </tr> " ;
echo " <tr> " ;
2005-05-05 18:34:20 +00:00
$count = 0 ;
2005-03-30 18:17:58 +00:00
foreach ( $useranswer as $optionid => $userlist ) {
if ( ! $optionid and ! $choice -> showunanswered ) {
continue ;
}
2005-05-05 18:34:20 +00:00
echo " <td align= \" center \" class= \" col $count count \" > " . $column [ $optionid ] . " </td> " ;
$count ++ ;
2005-03-30 18:17:58 +00:00
}
echo " </tr></table> " ;
break ;
2001-11-22 06:23:56 +00:00
}
2005-04-11 22:59:32 +00:00
echo " <br /> \n " ;
echo " <table border= \" 0 \" align= \" center \" ><tr> \n " ;
echo " <td> " ;
unset ( $options );
$options [ " id " ] = " $cm->id " ;
$options [ " download " ] = " xls " ;
print_single_button ( " report.php " , $options , get_string ( " downloadexcel " ));
echo " </td><td> " ;
$options [ " download " ] = " txt " ;
print_single_button ( " report.php " , $options , get_string ( " downloadtext " ));
echo " </td></tr></table> " ;
2005-03-30 18:17:58 +00:00
print_footer ( $course );
2001-11-22 06:23:56 +00:00
2005-02-16 10:40:48 +00:00
2001-11-22 06:23:56 +00:00
?>