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
2006-04-23 21:07:44 +00:00
$id = required_param ( 'id' , PARAM_INT ); //moduleid
2005-04-14 19:00:23 +00:00
$format = optional_param ( 'format' , CHOICE_PUBLISH_NAMES , PARAM_INT );
$download = optional_param ( 'download' , '' , PARAM_ALPHA );
2006-04-23 21:07:44 +00:00
$action = optional_param ( 'action' , '' , PARAM_ALPHA );
2005-03-30 18:17:58 +00:00
2006-08-08 22:09:55 +00:00
if ( ! $cm = get_coursemodule_from_id ( 'choice' , $id )) {
2001-11-22 06:23:56 +00:00
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 );
2006-08-08 05:13:06 +00:00
2006-08-09 13:45:49 +00:00
$context = get_context_instance ( CONTEXT_MODULE , $cm -> id );
2006-08-08 05:13:06 +00:00
2006-08-14 05:55:40 +00:00
require_capability ( 'mod/choice:readresponses' , $context );
2006-08-08 05:13:06 +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 " );
}
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 );
2006-02-23 01:20:24 +00:00
2006-08-14 05:55:40 +00:00
if ( $action == 'delete' && has_capability ( 'mod/choice:deleteresponses' , $context )) {
2006-02-23 01:20:24 +00:00
$attemptids = isset ( $_POST [ 'attemptid' ]) ? $_POST [ 'attemptid' ] : array (); //get array of repsonses to delete.
2006-02-23 21:20:44 +00:00
choice_delete_responses ( $attemptids ); //delete responses.
2006-08-09 13:45:49 +00:00
redirect ( " report.php?id= $cm->id " );
2006-02-23 01:20:24 +00:00
}
2006-12-21 19:46:29 +00:00
if ( $download <> " xls " and $download <> " txt " and $download <> " ods " ) {
2007-05-08 15:07:25 +00:00
2007-04-16 21:02:24 +00:00
$crumbs [] = array ( 'name' => $strchoices , 'link' => " index.php?id= $course->id " , 'type' => 'activity' );
$crumbs [] = array ( 'name' => format_string ( $choice -> name ), 'link' => " view.php?id= $cm->id " , 'type' => 'activityinstance' );
$crumbs [] = array ( 'name' => $strresponses , 'link' => '' , 'type' => 'title' );
2007-04-23 21:11:10 +00:00
$navigation = build_navigation ( $crumbs );
2007-04-16 21:02:24 +00:00
print_header_simple ( format_string ( $choice -> name ) . " : $strresponses " , " " , $navigation , " " , '' , true ,
2006-02-09 22:45:31 +00:00
update_module_button ( $cm -> id , $course -> id , $strchoice ), navmenu ( $course , $cm ));
2004-02-15 08:17:45 +00:00
}
2001-11-22 06:23:56 +00:00
2006-11-15 06:28:37 +00:00
$users = get_users_by_capability ( $context , 'mod/choice:choose' , 'u.id, u.picture, u.firstname, u.lastname, u.idnumber' , 'u.firstname ASC' );
2006-02-23 01:20:24 +00:00
2004-02-15 08:17:45 +00:00
if ( ! $users ) {
2006-01-05 07:09:35 +00:00
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 );
2006-12-21 19:46:29 +00:00
if ( $download == " ods " && has_capability ( 'mod/choice:downloadresponses' , $context )) {
require_once ( " $CFG->libdir /odslib.class.php " );
/// Calculate file name
$filename = clean_filename ( " $course->shortname " . strip_tags ( format_string ( $choice -> name , true ))) . '.ods' ;
/// Creating a workbook
$workbook = new MoodleODSWorkbook ( " - " );
/// Send HTTP headers
$workbook -> send ( $filename );
/// Creating the first worksheet
$myxls =& $workbook -> add_worksheet ( $strresponses );
/// Print names of all the fields
$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 ( " group " ));
$myxls -> write_string ( 0 , 4 , get_string ( " choice " , " choice " ));
/// generate the data for the body of the spreadsheet
$i = 0 ;
$row = 1 ;
if ( $users ) {
foreach ( $users as $user ) {
// this needs fixing
if ( ! ( $optionid == 0 && has_capability ( 'mod/choice:readresponses' , $context , $user -> id ))) {
if ( ! empty ( $answers [ $user -> id ]) && ! ( $answers [ $user -> id ] -> optionid == 0 && has_capability ( 'mod/choice:readresponses' , $context , $user -> id ) && $choice -> showunanswered == 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 = ( ! empty ( $user -> idnumber ) ? $user -> idnumber : " " );
$myxls -> write_string ( $row , 2 , $studentid );
$ug2 = '' ;
if ( $usergrps = user_group ( $course -> id , $user -> id )) {
foreach ( $usergrps 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 , 4 , format_string ( $useroption , true ));
}
$row ++ ;
}
$pos = 4 ;
}
}
/// Close the workbook
$workbook -> close ();
exit ;
}
}
2005-04-11 22:59:32 +00:00
//print spreadsheet if one is asked for:
2006-08-14 05:55:40 +00:00
if ( $download == " xls " && has_capability ( 'mod/choice:downloadresponses' , $context )) {
2006-01-05 16:33:29 +00:00
require_once ( " $CFG->libdir /excellib.class.php " );
2005-04-11 22:59:32 +00:00
2006-02-09 21:09:10 +00:00
/// Calculate file name
2006-01-05 16:33:29 +00:00
$filename = clean_filename ( " $course->shortname " . strip_tags ( format_string ( $choice -> name , true ))) . '.xls' ;
/// Creating a workbook
$workbook = new MoodleExcelWorkbook ( " - " );
/// Send HTTP headers
$workbook -> send ( $filename );
/// Creating the first worksheet
2006-02-09 21:09:10 +00:00
$myxls =& $workbook -> add_worksheet ( $strresponses );
2005-04-11 22:59:32 +00:00
2006-01-05 16:33:29 +00:00
/// Print names of all the fields
2005-04-11 22:59:32 +00:00
$myxls -> write_string ( 0 , 0 , get_string ( " lastname " ));
$myxls -> write_string ( 0 , 1 , get_string ( " firstname " ));
$myxls -> write_string ( 0 , 2 , get_string ( " idnumber " ));
2006-02-23 01:20:24 +00:00
$myxls -> write_string ( 0 , 3 , get_string ( " group " ));
$myxls -> write_string ( 0 , 4 , get_string ( " choice " , " choice " ));
2005-04-11 22:59:32 +00:00
/// generate the data for the body of the spreadsheet
2006-01-05 16:33:29 +00:00
$i = 0 ;
$row = 1 ;
if ( $users ) {
2006-08-29 08:43:59 +00:00
foreach ( $users as $user ) {
// this needs fixing
2006-09-11 07:17:10 +00:00
if ( ! ( $optionid == 0 && has_capability ( 'mod/choice:readresponses' , $context , $user -> id ))) {
if ( ! empty ( $answers [ $user -> id ]) && ! ( $answers [ $user -> id ] -> optionid == 0 && has_capability ( 'mod/choice:readresponses' , $context , $user -> id ) && $choice -> showunanswered == 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 = ( ! empty ( $user -> idnumber ) ? $user -> idnumber : " " );
$myxls -> write_string ( $row , 2 , $studentid );
$ug2 = '' ;
if ( $usergrps = user_group ( $course -> id , $user -> id )) {
foreach ( $usergrps as $ug ) {
$ug2 = $ug2 . $ug -> name ;
}
2006-08-09 13:45:49 +00:00
}
2006-09-11 07:17:10 +00:00
$myxls -> write_string ( $row , 3 , $ug2 );
2006-02-23 01:20:24 +00:00
2006-09-11 07:17:10 +00:00
$useroption = choice_get_option_text ( $choice , $answers [ $user -> id ] -> optionid );
if ( isset ( $useroption )) {
$myxls -> write_string ( $row , 4 , format_string ( $useroption , true ));
}
$row ++ ;
}
$pos = 4 ;
2006-01-05 16:33:29 +00:00
}
}
/// Close the workbook
2006-09-11 07:17:10 +00:00
$workbook -> close ();
2006-01-05 16:33:29 +00:00
2006-09-11 07:17:10 +00:00
exit ;
}
}
2006-08-08 05:13:06 +00:00
// print text file
2006-08-14 05:55:40 +00:00
if ( $download == " txt " && has_capability ( 'mod/choice:downloadresponses' , $context )) {
2005-04-14 19:00:23 +00:00
$filename = clean_filename ( " $course->shortname " . strip_tags ( format_string ( $choice -> name , true ))) . '.txt' ;
2006-08-14 05:55:40 +00:00
header ( " Content-Type: application/download \n " );
header ( " Content-Disposition: attachment; filename= \" $filename\ " " );
header ( " Expires: 0 " );
header ( " Cache-Control: must-revalidate,post-check=0,pre-check=0 " );
header ( " Pragma: public " );
/// Print names of all the fields
2005-04-11 22:59:32 +00:00
echo get_string ( " firstname " ) . " \t " . get_string ( " lastname " ) . " \t " . get_string ( " idnumber " ) . " \t " ;
2006-02-23 01:20:24 +00:00
echo get_string ( " group " ) . " \t " ;
2005-04-11 22:59:32 +00:00
echo get_string ( " choice " , " choice " ) . " \n " ;
2006-08-14 05:55:40 +00:00
/// generate the data for the body of the spreadsheet
$i = 0 ;
$row = 1 ;
if ( $users ) foreach ( $users as $user ) {
2006-09-11 07:17:10 +00:00
if ( ! empty ( $answers [ $user -> id ]) && ! ( $answers [ $user -> id ] -> optionid == 0 && has_capability ( 'mod/choice:readresponses' , $context , $user -> id ) && $choice -> showunanswered == 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.
2006-08-14 05:55:40 +00:00
echo $user -> lastname ;
echo " \t " . $user -> firstname ;
$studentid = " " ;
if ( ! empty ( $user -> idnumber )) {
$studentid = $user -> idnumber ;
}
echo " \t " . $studentid . " \t " ;
$ug2 = '' ;
if ( $usergrps = user_group ( $course -> id , $user -> id )) {
foreach ( $usergrps 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 ;
}
2006-02-23 01:20:24 +00:00
choice_show_results ( $choice , $course , $cm , $format ); //show table with students responses.
//now give links for downloading spreadsheets.
2005-04-11 22:59:32 +00:00
echo " <br /> \n " ;
2007-01-09 03:14:34 +00:00
echo " <table class= \" downloadreport \" ><tr> \n " ;
2005-04-11 22:59:32 +00:00
echo " <td> " ;
2006-12-21 19:46:29 +00:00
$options = array ();
2005-04-11 22:59:32 +00:00
$options [ " id " ] = " $cm->id " ;
2006-12-21 19:46:29 +00:00
$options [ " download " ] = " ods " ;
print_single_button ( " report.php " , $options , get_string ( " downloadods " ));
echo " </td><td> " ;
2005-04-11 22:59:32 +00:00
$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> " ;
2006-09-11 07:17:10 +00:00
print_footer ( $course );
2005-02-16 10:40:48 +00:00
2001-11-22 06:23:56 +00:00
?>