Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/choice/report.php
Go to the documentation of this file.
00001 <?php
00002 
00003     require_once("../../config.php");
00004     require_once("lib.php");
00005 
00006     $id         = required_param('id', PARAM_INT);   //moduleid
00007     $format     = optional_param('format', CHOICE_PUBLISH_NAMES, PARAM_INT);
00008     $download   = optional_param('download', '', PARAM_ALPHA);
00009     $action     = optional_param('action', '', PARAM_ALPHA);
00010     $attemptids = optional_param_array('attemptid', array(), PARAM_INT); //get array of responses to delete.
00011 
00012     $url = new moodle_url('/mod/choice/report.php', array('id'=>$id));
00013     if ($format !== CHOICE_PUBLISH_NAMES) {
00014         $url->param('format', $format);
00015     }
00016     if ($download !== '') {
00017         $url->param('download', $download);
00018     }
00019     if ($action !== '') {
00020         $url->param('action', $action);
00021     }
00022     $PAGE->set_url($url);
00023 
00024     if (! $cm = get_coursemodule_from_id('choice', $id)) {
00025         print_error("invalidcoursemodule");
00026     }
00027 
00028     if (! $course = $DB->get_record("course", array("id" => $cm->course))) {
00029         print_error("coursemisconf");
00030     }
00031 
00032     require_login($course->id, false, $cm);
00033 
00034     $context = get_context_instance(CONTEXT_MODULE, $cm->id);
00035 
00036     require_capability('mod/choice:readresponses', $context);
00037 
00038     if (!$choice = choice_get_choice($cm->instance)) {
00039         print_error('invalidcoursemodule');
00040     }
00041 
00042     $strchoice = get_string("modulename", "choice");
00043     $strchoices = get_string("modulenameplural", "choice");
00044     $strresponses = get_string("responses", "choice");
00045 
00046     add_to_log($course->id, "choice", "report", "report.php?id=$cm->id", "$choice->id",$cm->id);
00047 
00048     if (data_submitted() && $action == 'delete' && has_capability('mod/choice:deleteresponses',$context) && confirm_sesskey()) {
00049         choice_delete_responses($attemptids, $choice, $cm, $course); //delete responses.
00050         redirect("report.php?id=$cm->id");
00051     }
00052 
00053     if (!$download) {
00054         $PAGE->navbar->add($strresponses);
00055         $PAGE->set_title(format_string($choice->name).": $strresponses");
00056         $PAGE->set_heading($course->fullname);
00057         echo $OUTPUT->header();
00059         $groupmode = groups_get_activity_groupmode($cm);
00060         if ($groupmode) {
00061             groups_get_activity_group($cm, true);
00062             groups_print_activity_menu($cm, $CFG->wwwroot . '/mod/choice/report.php?id='.$id);
00063         }
00064     } else {
00065         $groupmode = groups_get_activity_groupmode($cm);
00066     }
00067     $users = choice_get_response_data($choice, $cm, $groupmode);
00068 
00069     if ($download == "ods" && has_capability('mod/choice:downloadresponses', $context)) {
00070         require_once("$CFG->libdir/odslib.class.php");
00071 
00073         $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.ods';
00075         $workbook = new MoodleODSWorkbook("-");
00077         $workbook->send($filename);
00079         $myxls =& $workbook->add_worksheet($strresponses);
00080 
00082         $myxls->write_string(0,0,get_string("lastname"));
00083         $myxls->write_string(0,1,get_string("firstname"));
00084         $myxls->write_string(0,2,get_string("idnumber"));
00085         $myxls->write_string(0,3,get_string("group"));
00086         $myxls->write_string(0,4,get_string("choice","choice"));
00087 
00089         $i=0;
00090         $row=1;
00091         if ($users) {
00092             foreach ($users as $option => $userid) {
00093                 $option_text = choice_get_option_text($choice, $option);
00094                 foreach($userid as $user) {
00095                     $myxls->write_string($row,0,$user->lastname);
00096                     $myxls->write_string($row,1,$user->firstname);
00097                     $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
00098                     $myxls->write_string($row,2,$studentid);
00099                     $ug2 = '';
00100                     if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
00101                         foreach ($usergrps as $ug) {
00102                             $ug2 = $ug2. $ug->name;
00103                         }
00104                     }
00105                     $myxls->write_string($row,3,$ug2);
00106 
00107                     if (isset($option_text)) {
00108                         $myxls->write_string($row,4,format_string($option_text,true));
00109                     }
00110                     $row++;
00111                     $pos=4;
00112                 }
00113             }
00114         }
00116         $workbook->close();
00117 
00118         exit;
00119     }
00120 
00121     //print spreadsheet if one is asked for:
00122     if ($download == "xls" && has_capability('mod/choice:downloadresponses', $context)) {
00123         require_once("$CFG->libdir/excellib.class.php");
00124 
00126         $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.xls';
00128         $workbook = new MoodleExcelWorkbook("-");
00130         $workbook->send($filename);
00132         $myxls =& $workbook->add_worksheet($strresponses);
00133 
00135         $myxls->write_string(0,0,get_string("lastname"));
00136         $myxls->write_string(0,1,get_string("firstname"));
00137         $myxls->write_string(0,2,get_string("idnumber"));
00138         $myxls->write_string(0,3,get_string("group"));
00139         $myxls->write_string(0,4,get_string("choice","choice"));
00140 
00141 
00143         $i=0;
00144         $row=1;
00145         if ($users) {
00146             foreach ($users as $option => $userid) {
00147                 $option_text = choice_get_option_text($choice, $option);
00148                 foreach($userid as $user) {
00149                     $myxls->write_string($row,0,$user->lastname);
00150                     $myxls->write_string($row,1,$user->firstname);
00151                     $studentid=(!empty($user->idnumber) ? $user->idnumber : " ");
00152                     $myxls->write_string($row,2,$studentid);
00153                     $ug2 = '';
00154                     if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
00155                         foreach ($usergrps as $ug) {
00156                             $ug2 = $ug2. $ug->name;
00157                         }
00158                     }
00159                     $myxls->write_string($row,3,$ug2);
00160                     if (isset($option_text)) {
00161                         $myxls->write_string($row,4,format_string($option_text,true));
00162                     }
00163                     $row++;
00164                 }
00165             }
00166             $pos=4;
00167         }
00169         $workbook->close();
00170         exit;
00171     }
00172 
00173     // print text file
00174     if ($download == "txt" && has_capability('mod/choice:downloadresponses', $context)) {
00175         $filename = clean_filename("$course->shortname ".strip_tags(format_string($choice->name,true))).'.txt';
00176 
00177         header("Content-Type: application/download\n");
00178         header("Content-Disposition: attachment; filename=\"$filename\"");
00179         header("Expires: 0");
00180         header("Cache-Control: must-revalidate,post-check=0,pre-check=0");
00181         header("Pragma: public");
00182 
00184 
00185         echo get_string("firstname")."\t".get_string("lastname") . "\t". get_string("idnumber") . "\t";
00186         echo get_string("group"). "\t";
00187         echo get_string("choice","choice"). "\n";
00188 
00190         $i=0;
00191         if ($users) {
00192             foreach ($users as $option => $userid) {
00193                 $option_text = choice_get_option_text($choice, $option);
00194                 foreach($userid as $user) {
00195                     echo $user->lastname;
00196                     echo "\t".$user->firstname;
00197                     $studentid = " ";
00198                     if (!empty($user->idnumber)) {
00199                         $studentid = $user->idnumber;
00200                     }
00201                     echo "\t". $studentid."\t";
00202                     $ug2 = '';
00203                     if ($usergrps = groups_get_all_groups($course->id, $user->id)) {
00204                         foreach ($usergrps as $ug) {
00205                             $ug2 = $ug2. $ug->name;
00206                         }
00207                     }
00208                     echo $ug2. "\t";
00209                     if (isset($option_text)) {
00210                         echo format_string($option_text,true);
00211                     }
00212                     echo "\n";
00213                 }
00214             }
00215         }
00216         exit;
00217     }
00218     // Show those who haven't answered the question.
00219     if (!empty($choice->showunanswered)) {
00220         $choice->option[0] = get_string('notanswered', 'choice');
00221         $choice->maxanswers[0] = 0;
00222     }
00223 
00224     $results = prepare_choice_show_results($choice, $course, $cm, $users);
00225     $renderer = $PAGE->get_renderer('mod_choice');
00226     echo $renderer->display_result($results, has_capability('mod/choice:readresponses', $context));
00227 
00228    //now give links for downloading spreadsheets.
00229     if (!empty($users) && has_capability('mod/choice:downloadresponses',$context)) {
00230         $downloadoptions = array();
00231         $options = array();
00232         $options["id"] = "$cm->id";
00233         $options["download"] = "ods";
00234         $button =  $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadods"));
00235         $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
00236 
00237         $options["download"] = "xls";
00238         $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadexcel"));
00239         $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
00240 
00241         $options["download"] = "txt";
00242         $button = $OUTPUT->single_button(new moodle_url("report.php", $options), get_string("downloadtext"));
00243         $downloadoptions[] = html_writer::tag('li', $button, array('class'=>'reportoption'));
00244 
00245         $downloadlist = html_writer::tag('ul', implode('', $downloadoptions));
00246         $downloadlist .= html_writer::tag('div', '', array('class'=>'clearfloat'));
00247         echo html_writer::tag('div',$downloadlist, array('class'=>'downloadreport'));
00248     }
00249     echo $OUTPUT->footer();
00250 
 All Data Structures Namespaces Files Functions Variables Enumerations