diff --git a/report/completion/index.php b/report/completion/index.php index 457560eb883e0..a09a3dc577ff2 100644 --- a/report/completion/index.php +++ b/report/completion/index.php @@ -24,8 +24,8 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ -require('../../config.php'); -require_once($CFG->libdir.'/completionlib.php'); +require_once(dirname(__FILE__).'/../../config.php'); +require_once("{$CFG->libdir}/completionlib.php"); /** * Configuration @@ -55,6 +55,11 @@ $excel = ($format == 'excelcsv'); $csv = ($format == 'csv' || $excel); +// Load CSV library +if ($csv) { + require_once("{$CFG->libdir}/csvlib.class.php"); +} + // Paging $start = optional_param('start', 0, PARAM_INT); $sifirst = optional_param('sifirst', 'all', PARAM_ALPHA); @@ -64,17 +69,6 @@ $extrafields = get_extra_user_fields($context); $leftcols = 1 + count($extrafields); -// Function for quoting csv cell values -function csv_quote($value) { - global $excel; - if ($excel) { - return textlib::convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE'); - } else { - return '"'.str_replace('"',"'",$value).'"'; - } -} - - // Check permissions require_login($course); @@ -146,20 +140,12 @@ function csv_quote($value) { * Setup page header */ if ($csv) { + $shortname = format_string($course->shortname, true, array('context' => $context)); - header('Content-Disposition: attachment; filename=progress.'. - preg_replace('/[^a-z0-9-]/','_',textlib::strtolower(strip_tags($shortname))).'.csv'); - // Unicode byte-order mark for Excel - if ($excel) { - header('Content-Type: text/csv; charset=UTF-16LE'); - print chr(0xFF).chr(0xFE); - $sep="\t".chr(0); - $line="\n".chr(0); - } else { - header('Content-Type: text/csv; charset=UTF-8'); - $sep=","; - $line="\n"; - } + $shortname = preg_replace('/[^a-z0-9-]/', '_',textlib::strtolower(strip_tags($shortname))); + + $export = new csv_export_writer(); + $export->set_filename('completion-'.$shortname); } else { // Navigation and header @@ -177,7 +163,6 @@ function csv_quote($value) { groups_print_course_menu($course, $CFG->wwwroot.'/report/completion/?course='.$course->id); } - // Generate where clause $where = array(); $where_params = array(); @@ -220,7 +205,6 @@ function csv_quote($value) { ); } - // Build link for paging $link = $CFG->wwwroot.'/report/completion/?course='.$course->id; if (strlen($sort)) { @@ -308,7 +292,6 @@ function csv_quote($value) { $pagingbar .= ''; } - /* * Draw table header */ @@ -328,7 +311,6 @@ function csv_quote($value) { exit; } - print '
'; print ''; // Print criteria group names @@ -423,7 +405,6 @@ function csv_quote($value) { print ''; - // Print criteria titles if (COMPLETION_REPORT_COL_TITLES) { @@ -441,9 +422,7 @@ function csv_quote($value) { // Overall course completion status print ''; } @@ -466,7 +445,6 @@ function csv_quote($value) { } print ''; - // Print user identity columns foreach ($extrafields as $field) { echo ''; - } else { - // The CSV file does not contain any headers -} + // The CSV headers + $row = array(); + $row[] = get_string('id', 'report_completion'); + $row[] = get_string('name', 'report_completion'); + foreach ($extrafields as $field) { + $row[] = get_user_field_name($field); + } + + // Add activity headers + foreach ($criteria as $criterion) { + + // Handle activity completion differently + if ($criterion->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) { + + // Load activity + $mod = $criterion->get_mod_instance(); + $row[] = $mod->name; + $row[] = $mod->name . ' - ' . get_string('completiondate', 'report_completion'); + } + else { + // Handle all other criteria + $row[] = strip_tags($criterion->get_title_detailed()); + } + } + + $row[] = get_string('coursecomplete', 'completion'); + + $export->add_data($row); +} /// /// Display a row for each user @@ -547,23 +552,28 @@ function csv_quote($value) { // User name if ($csv) { - print csv_quote(fullname($user)); + $row = array(); + $row[] = $user->id; + $row[] = fullname($user); foreach ($extrafields as $field) { - echo $sep . csv_quote($user->{$field}); + $row[] = $user->{$field}; } } else { print PHP_EOL.''; - print ''; + $userurl = new moodle_url('/user/view.php', array('id' => $user->id, 'course' => $course->id)); + print ''; foreach ($extrafields as $field) { - echo ''; + echo ''; } } // Progress for each course completion criteria foreach ($criteria as $criterion) { + $criteria_completion = $completion->get_user_completion($user->id, $criterion); + $is_complete = $criteria_completion->is_complete(); + // Handle activity completion differently if ($criterion->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) { @@ -571,39 +581,42 @@ function csv_quote($value) { $activity = $modinfo->cms[$criterion->moduleinstance]; // Get progress information and state - if (array_key_exists($activity->id,$user->progress)) { - $thisprogress=$user->progress[$activity->id]; - $state=$thisprogress->completionstate; - $date=userdate($thisprogress->timemodified); + if ($is_complete) { + $date = userdate($criteria_completion->timecompleted, get_string('strftimedatetimeshort', 'langconfig')); + + if (array_key_exists($activity->id, $user->progress)) { + $thisprogress = $user->progress[$activity->id]; + $state = $thisprogress->completionstate; + } else { + $state = COMPLETION_COMPLETE; + } } else { - $state=COMPLETION_INCOMPLETE; - $date=''; + $date = ''; + $state = COMPLETION_INCOMPLETE; } - $criteria_completion = $completion->get_user_completion($user->id, $criterion); - // Work out how it corresponds to an icon switch($state) { - case COMPLETION_INCOMPLETE : $completiontype='n'; break; - case COMPLETION_COMPLETE : $completiontype='y'; break; - case COMPLETION_COMPLETE_PASS : $completiontype='pass'; break; - case COMPLETION_COMPLETE_FAIL : $completiontype='fail'; break; + case COMPLETION_INCOMPLETE : $completiontype = 'n'; break; + case COMPLETION_COMPLETE : $completiontype = 'y'; break; + case COMPLETION_COMPLETE_PASS : $completiontype = 'pass'; break; + case COMPLETION_COMPLETE_FAIL : $completiontype = 'fail'; break; } - $completionicon='completion-'. - ($activity->completion==COMPLETION_TRACKING_AUTOMATIC ? 'auto' : 'manual'). - '-'.$completiontype; + $auto = $activity->completion == COMPLETION_TRACKING_AUTOMATIC; + $completionicon = 'completion-'.($auto ? 'auto' : 'manual').'-'.$completiontype; - $describe = get_string('completion-' . $completiontype, 'completion'); - $a=new StdClass; - $a->state=$describe; - $a->date=$date; - $a->user=fullname($user); - $a->activity=strip_tags($activity->name); - $fulldescribe=get_string('progress-title','completion',$a); + $describe = get_string('completion-'.$completiontype, 'completion'); + $a = new StdClass(); + $a->state = $describe; + $a->date = $date; + $a->user = fullname($user); + $a->activity = strip_tags($activity->name); + $fulldescribe = get_string('progress-title', 'completion', $a); if ($csv) { - print $sep.csv_quote($describe).$sep.csv_quote($date); + $row[] = $describe; + $row[] = $date; } else { print ''; } else { - print ''; + print ''.$describe.''; } + + print ''; } } @@ -662,17 +688,23 @@ function csv_quote($value) { $ccompletion = new completion_completion($params); $completiontype = $ccompletion->is_complete() ? 'y' : 'n'; - $describe = get_string('completion-' . $completiontype, 'completion'); + $describe = get_string('completion-'.$completiontype, 'completion'); $a = new StdClass; + + if ($ccompletion->is_complete()) { + $a->date = userdate($ccompletion->timecompleted, get_string('strftimedatetimeshort', 'langconfig')); + } else { + $a->date = ''; + } + $a->state = $describe; - $a->date = ''; $a->user = fullname($user); $a->activity = strip_tags(get_string('coursecomplete', 'completion')); $fulldescribe = get_string('progress-title', 'completion', $a); if ($csv) { - print $sep.csv_quote($describe); + $row[] = $a->date; } else { print ''; } } if ($csv) { - exit; + $export->download_file(); } + print '
'; - print ''.get_string('coursecomplete', 'completion').''; - print '
' . @@ -486,6 +464,7 @@ function csv_quote($value) { switch ($criterion->criteriatype) { case COMPLETION_CRITERIA_TYPE_ACTIVITY: + // Display icon $icon = $OUTPUT->pix_url('icon', $criterion->module); $iconlink = $CFG->wwwroot.'/mod/'.$criterion->module.'/view.php?id='.$criterion->moduleinstance; @@ -534,11 +513,37 @@ function csv_quote($value) { print '
'.fullname($user).''.fullname($user).'' . s($user->{$field}) . ''.s($user->{$field}).''; @@ -617,37 +630,50 @@ function csv_quote($value) { } // Handle all other criteria - $criteria_completion = $completion->get_user_completion($user->id, $criterion); - $is_complete = $criteria_completion->is_complete(); - $completiontype = $is_complete ? 'y' : 'n'; $completionicon = 'completion-auto-'.$completiontype; - $describe = get_string('completion-' . $completiontype, 'completion'); + $describe = get_string('completion-'.$completiontype, 'completion'); $a = new stdClass(); $a->state = $describe; - $a->date = $is_complete ? userdate($criteria_completion->timecompleted) : ''; + + if ($is_complete) { + $a->date = userdate($criteria_completion->timecompleted, get_string('strftimedatetimeshort', 'langconfig')); + } else { + $a->date = ''; + } + $a->user = fullname($user); $a->activity = strip_tags($criterion->get_title()); $fulldescribe = get_string('progress-title', 'completion', $a); if ($csv) { - print $sep.csv_quote($describe); + $row[] = $a->date; } else { - if ($allow_marking_criteria === $criterion->id) { - $describe = get_string('completion-' . $completiontype, 'completion'); + print ''; - print ''. - ''. - 'out().'">'.$describe.''. - ''.$describe.''; @@ -685,22 +717,25 @@ function csv_quote($value) { } if ($csv) { - print $line; + $export->add_data($row); } else { print '
'; -print '
'; print $pagingbar; -print ''; +$csvurl = new moodle_url('/report/completion/index.php', array('course' => $course->id, 'format' => 'csv')); +$excelurl = new moodle_url('/report/completion/index.php', array('course' => $course->id, 'format' => 'excelcsv')); + +print ''; echo $OUTPUT->footer($course); diff --git a/report/completion/lang/en/report_completion.php b/report/completion/lang/en/report_completion.php index e3e8354241b75..3492b1f555131 100644 --- a/report/completion/lang/en/report_completion.php +++ b/report/completion/lang/en/report_completion.php @@ -26,6 +26,8 @@ $string['completion:view'] = 'View course completion report'; $string['completiondate'] = 'Completion date'; +$string['id'] = 'ID'; +$string['name'] = 'Name'; $string['page-report-completion-x'] = 'Any completion report'; $string['page-report-completion-index'] = 'Course completion report'; $string['page-report-completion-user'] = 'User course completion report'; diff --git a/report/completion/styles.css b/report/completion/styles.css new file mode 100644 index 0000000000000..ed84511eb9acc --- /dev/null +++ b/report/completion/styles.css @@ -0,0 +1,8 @@ +#page-report-completion-index table#completion-progress { + margin-top: 20px; + margin-bottom: 30px; +} + +#page-report-completion-index .export-actions { + text-align: center; +}