From b3aa7ae8bcf5c11af8e26cafe841f493d7bf9a97 Mon Sep 17 00:00:00 2001
From: sam_marshall <sam_marshall>
Date: Mon, 28 Jul 2008 17:11:51 +0000
Subject: [PATCH] MDL-15498: Completion fix: provided full UTF-8 support and
 Excel support for progress CSV output

---
 course/report/progress/index.php | 40 +++++++++++++++++++++++---------
 lang/en_utf8/completion.php      |  3 ++-
 2 files changed, 31 insertions(+), 12 deletions(-)

diff --git a/course/report/progress/index.php b/course/report/progress/index.php
index 2f52a68ca8..68ee2d2ba0 100644
--- a/course/report/progress/index.php
+++ b/course/report/progress/index.php
@@ -14,13 +14,18 @@ $sort=optional_param('sort','',PARAM_ALPHA);
 $firstnamesort=$sort=='firstname';
 
 // CSV format
-$csv=optional_param('format','',PARAM_ALPHA)==='csv';
+$format=optional_param('format','',PARAM_ALPHA);
+$excel=$format=='excelcsv';
+$csv=$format=='csv' || $excel;
 
 function csv_quote($value) {
-    $tl=textlib_get_instance();
-    
-    $value=$tl->specialtoascii($value);
-    return '"'.str_replace('"',"'",$value).'"';
+    global $excel;
+    if($excel) {
+        $tl=textlib_get_instance();
+        return $tl->convert('"'.str_replace('"',"'",$value).'"','UTF-8','UTF-16LE');
+    } else {
+        return '"'.str_replace('"',"'",$value).'"';
+    }
 }
 
 require_login($course->id);
@@ -45,10 +50,21 @@ if(count($activities)==0) {
 }
 $progress=$completion->get_progress_all($firstnamesort,$group);
 
+
 if($csv) {
-    header('Content-Type: text/csv; charset=ISO-8859-1');
     header('Content-Disposition: attachment; filename=progress.'.
         preg_replace('/[^a-z0-9-]/','_',strtolower($course->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";
+    }
 } else {
     // Use SVG to draw sideways text if supported
     $svgcleverness=check_browser_version('Firefox',2.0) && !$USER->screenreader;
@@ -110,7 +126,7 @@ foreach($activities as $activity) {
     }
 
     if($csv) {
-        print ','.csv_quote($activity->name).','.csv_quote($datetext);
+        print $sep.csv_quote($activity->name).$sep.csv_quote($datetext);
     } else {
         print '<th scope="col" class="'.$activity->datepassedclass.'">'.
             '<a href="'.$CFG->wwwroot.'/mod/'.$activity->modname.
@@ -126,7 +142,7 @@ foreach($activities as $activity) {
 }
 
 if($csv) {
-    print "\n";
+    print $line;
 } else {
     print '</tr>';
 }
@@ -174,7 +190,7 @@ foreach($progress as $user) {
         $fulldescribe=get_string('progress-title','completion',$a);
 
         if($csv) {
-            print ','.csv_quote($describe).','.csv_quote($date);
+            print $sep.csv_quote($describe).$sep.csv_quote($date);
         } else {
             print '<td class="completion-progresscell '.$activity->datepassedclass.'">'.
                 '<img src="'.$CFG->pixpath.'/i/completion-'.$completiontype.
@@ -183,7 +199,7 @@ foreach($progress as $user) {
     }
 
     if($csv) {
-        print "\n";
+        print $line;
     } else {
         print '</tr>';
     }
@@ -195,7 +211,9 @@ if($csv) {
 print '</table>';
 
 print '<ul class="progress-actions"><li><a href="index.php?course='.$course->id.
-    '&amp;format=csv">'.get_string('csvdownload','completion').'</a></li></ul>';
+    '&amp;format=csv">'.get_string('csvdownload','completion').'</a></li>
+    <li><a href="index.php?course='.$course->id.'&amp;format=excelcsv">'.
+    get_string('excelcsvdownload','completion').'</a></li></ul>';
 
 print_footer($course);
 ?>
diff --git a/lang/en_utf8/completion.php b/lang/en_utf8/completion.php
index 9df67eb69f..38e1bd4a85 100644
--- a/lang/en_utf8/completion.php
+++ b/lang/en_utf8/completion.php
@@ -29,7 +29,8 @@ $string['completionview']='Require view';
 $string['completionview_text']='User must view activity';
 $string['configenablecompletion'] = 'When enabled, this lets you turn on completion tracking (progress) features at course level.';
 $string['configprogresstrackedroles'] = 'Roles that are displayed in the progress-tracking screen. (Usually includes just students and equivalent roles.)';
-$string['csvdownload']='Download in spreadsheet format (.csv)';
+$string['csvdownload']='Download in spreadsheet format (UTF-8 .csv)';
+$string['excelcsvdownload']='Download in Excel-compatible format (.csv)';
 $string['enablecompletion'] = 'Enable completion tracking';
 $string['err_noactivities']='Completion information is not enabled for any activity, so none can be displayed. You can enable completion information by editing the settings for an activity.';
 $string['err_nousers']='There are no users on this course or group for whom completion information is displayed. (By default, completion information is displayed only for students, so if there are no students, you will see this error. Administrators can alter this option via the admin screens.)';
-- 
2.39.5