Browse Source

Add export reports to PDF see BT#10043

Julio Montoya 9 years ago
parent
commit
33a9576e0d

+ 130 - 0
main/gradebook/gradebook_display_summary.php

@@ -0,0 +1,130 @@
+<?php
+/* For licensing terms, see /license.txt */
+
+/**
+ * Script
+ * @package chamilo.gradebook
+ */
+
+$language_file = 'gradebook';
+
+require_once '../inc/global.inc.php';
+$current_course_tool  = TOOL_GRADEBOOK;
+
+api_protect_course_script();
+
+set_time_limit(0);
+ini_set('max_execution_time', 0);
+api_block_anonymous_users();
+
+if (!api_is_allowed_to_edit()) {
+    api_not_allowed(true);
+}
+
+$cat_id = isset($_GET['selectcat']) ? (int)$_GET['selectcat'] : null;
+$action = isset($_GET['action']) && $_GET['action'] ? $_GET['action'] : null;
+
+$userList = CourseManager::get_user_list_from_course_code(
+    api_get_course_id(),
+    api_get_session_id()
+);
+
+switch ($action) {
+    case 'export_all':
+
+        $params = array();
+        $pdf = new PDF('A4', 'P', $params);
+
+        $pdfList = array();
+        foreach ($userList as $index => $value) {
+            $pdfList[] = GradebookUtils::generateTable(
+                $value['user_id'],
+                $cat_id,
+                false,
+                true
+            );
+        }
+
+        if (!empty($pdfList)) {
+            // Print certificates (without the common header/footer/watermark
+            //  stuff) and return as one multiple-pages PDF
+            $pdf->html_to_pdf(
+                $pdfList,
+                null,
+                null,
+                false,
+                false,
+                true
+            );
+        }
+
+        break;
+    case 'download':
+        $userId = isset($_GET['user_id']) && $_GET['user_id'] ? $_GET['user_id'] : null;
+        GradebookUtils::generateTable($userId, $cat_id);
+        break;
+    /*case 'generate_all_certificates':
+        $user_list = CourseManager::get_user_list_from_course_code(api_get_course_id(), api_get_session_id());
+        if (!empty($user_list)) {
+            foreach ($user_list as $user_info) {
+                if ($user_info['status'] == INVITEE) {
+                    continue;
+                }
+
+                Category::register_user_certificate($cat_id, $user_info['user_id']);
+            }
+        }
+        break;
+    case 'delete_all_certificates':
+        Category::deleteAllCertificates($cat_id);
+        break;*/
+}
+
+$course_code = api_get_course_id();
+
+$interbreadcrumb[] = array('url' => Security::remove_XSS($_SESSION['gradebook_dest']).'?',	'name' => get_lang('Gradebook'));
+$interbreadcrumb[] = array('url' => '#','name' => get_lang('GradebookListOfStudentsReports'));
+
+$this_section = SECTION_COURSES;
+
+Display::display_header('');
+
+$token = Security::get_token();
+
+echo Display::page_header(get_lang('GradebookListOfStudentsReports'));
+
+echo '<div class="btn-group">';
+/*$url = api_get_self().'?action=generate_all_certificates'.'&'.api_get_cidReq().'&selectcat='.$cat_id;
+echo Display::url(get_lang('GenerateCertificates'), $url, array('class' => 'btn'));
+*/
+/*$url = api_get_self().'?action=delete_all_certificates'.'&'.api_get_cidReq().'&selectcat='.$cat_id;
+echo Display::url(get_lang('DeleteAllCertificates'), $url, array('class' => 'btn'));*/
+
+if (count($userList) > 0) {
+    $url = api_get_self().'?action=export_all&'.api_get_cidReq().'&selectcat='.$cat_id;
+    echo Display::url(get_lang('ExportAllToPDF'), $url, array('class' => 'btn btn-default'));
+}
+echo '</div>';
+
+if (count($userList) == 0 ) {
+    echo Display::display_warning_message(get_lang('NoResultsAvailable'));
+} else {
+    echo '<br /><br /><table class="data_table">';
+    foreach ($userList as $index => $value) {
+        echo '<tr>
+                <td width="100%" >'.
+                get_lang('Student').' : '.api_get_person_name($value['firstname'], $value['lastname']).' ('.$value['username'].') </td>';
+        echo '<td>';
+        $url = api_get_self().'?'.api_get_cidreq().'&action=download&user_id='.$value['user_id'].'&selectcat='.$cat_id;
+        $link = Display::url(
+            get_lang('ExportToPDF'),
+            $url,
+            array('target' => '_blank', 'class' => 'btn btn-default')
+        );
+        echo $link;
+        echo '</td></tr>';
+    }
+    echo '</table>';
+}
+
+Display::display_footer();

+ 89 - 0
main/gradebook/lib/GradebookUtils.php

@@ -1299,4 +1299,93 @@ class GradebookUtils
         return $sessionList;
     }
 
+    /**
+     * @param int $userId
+     * @param int $categoryId
+     * @param bool $saveToFile
+     * @param bool $saveToHtmlFile
+     *
+     * @return string
+     */
+    public static function generateTable($userId, $categoryId, $saveToFile = false, $saveToHtmlFile = false)
+    {
+        $courseInfo = api_get_course_info();
+        $userInfo = api_get_user_info($userId);
+
+        $cats = Category::load($categoryId, null, null, null, null, null, false);
+        $cat = $cats[0];
+
+        $allcat = $cats[0]->get_subcategories(
+            $userId,
+            api_get_course_id(),
+            api_get_session_id()
+        );
+        $alleval = $cats[0]->get_evaluations($userId);
+        $alllink = $cats[0]->get_links($userId);
+
+        $gradebooktable = new GradebookTable(
+            $cat,
+            $allcat,
+            $alleval,
+            $alllink,
+            null, // params
+            true, // $exportToPdf
+            false, // showteacher
+            $userId
+        );
+
+        if (api_is_allowed_to_edit()) {
+            $gradebooktable->td_attributes = [
+                4 => 'class=centered'
+            ];
+        } else {
+            $gradebooktable->td_attributes = [
+                3 => 'class=centered',
+                4 => 'class=centered',
+                5 => 'class=centered',
+                6 => 'class=centered',
+                7 => 'class=centered'
+            ];
+        }
+
+        $table = $gradebooktable->return_table();
+        $graph = $gradebooktable->getGraph();
+
+        $sessionName = api_get_session_name(api_get_session_id());
+        $sessionName = !empty($sessionName) ? " - $sessionName" : '';
+        $params = array(
+            //'filename' => get_lang('FlatView') . '_' . api_get_utc_datetime(),
+            'pdf_title' => $courseInfo['title'].$sessionName,
+            'course_code' => api_get_course_id(),
+            'session_info' => api_get_session_info(api_get_session_id()),
+            'add_signatures' => false,
+            'student_info' => $userInfo,
+            'show_grade_generated_date' => true,
+            'show_real_course_teachers' => true
+        );
+
+        $file = api_get_path(SYS_ARCHIVE_PATH).uniqid().'.html';
+
+        $content =
+            $table.
+            $graph.
+            '<br />'.get_lang('Feedback').'<br />
+            <textarea rows="5" cols="100" ></textarea>';
+
+        $pdf = new PDF('A4', $params['orientation'], $params);
+
+        $result = $pdf->html_to_pdf_with_template(
+            $content,
+            $saveToFile,
+            $saveToHtmlFile
+        );
+
+        if ($saveToHtmlFile) {
+            file_put_contents($file, $result);
+            return $file;
+        }
+
+        return $file;
+    }
+
 }

+ 13 - 0
main/gradebook/lib/fe/displaygradebook.php

@@ -584,6 +584,19 @@ class DisplayGradebook
                             . "</td>";
                     }
 
+                    $header .= "<td style=\"vertical-align: top;\">"
+                        . Display::url(
+                            Display::return_icon(
+                                'user.png',
+                                get_lang('GradebookListOfStudentsReports'),
+                                '',
+                                ICON_SIZE_MEDIUM
+                            ),
+                            "gradebook_display_summary.php?$my_api_cidreq&selectcat=" . intval($_GET['selectcat'])
+                        )
+                        . "</td>";
+
+
                     // Right icons
                     $modify_icons = '<a href="gradebook_edit_cat.php?editcat=' . $catobj->get_id() . '&amp;cidReq=' . $catobj->get_course_code() . '&id_session='.$catobj->get_session_id(). '">' .
                         Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_MEDIUM) . '</a>';

+ 40 - 21
main/gradebook/lib/fe/gradebooktable.class.php

@@ -20,6 +20,8 @@ class GradebookTable extends SortableTable
     public $cats;
     private $dataForGraph;
     public $exportToPdf;
+    public $teacherView;
+    public $userId;
 
     /**
      * Constructor
@@ -29,8 +31,20 @@ class GradebookTable extends SortableTable
      * @param array $links
      * @param null $addparams
      */
-    public function __construct($currentcat, $cats = array(), $evals = array(), $links = array(), $addparams = null, $exportToPdf = false)
-    {
+    public function __construct(
+        $currentcat,
+        $cats = array(),
+        $evals = array(),
+        $links = array(),
+        $addparams = null,
+        $exportToPdf = false,
+        $showTeacherView = null,
+        $userId = null
+    ) {
+        $this->teacherView = is_null($showTeacherView) ? api_is_allowed_to_edit(null, true) : $showTeacherView;
+        $this->userId = is_null($userId) ? api_get_user_id() : $userId;
+        $this->exportToPdf = $exportToPdf;
+
         parent::__construct('gradebooklist', null, null, api_is_allowed_to_edit() ? 1 : 0, 20, 'ASC', 'gradebook_list');
         $this->evals_links = array_merge($evals, $links);
         $this->currentcat = $currentcat;
@@ -42,7 +56,7 @@ class GradebookTable extends SortableTable
         }
 
         $column= 0;
-        if (api_is_allowed_to_edit(null, true)) {
+        if ($this->teacherView) {
             if ($this->exportToPdf == false) {
                 $this->set_header($column++, '', '', 'width="25px"');
             }
@@ -55,7 +69,7 @@ class GradebookTable extends SortableTable
             $this->set_header($column++, get_lang('Description'), false);
         }
 
-        if (api_is_allowed_to_edit(null, true)) {
+        if ($this->teacherView) {
             $this->set_header(
                 $column++,
                 get_lang('Weight'),
@@ -80,7 +94,7 @@ class GradebookTable extends SortableTable
         $this->odd_even_rows_enabled = false;
 
         // Admins get an edit column.
-        if (api_is_allowed_to_edit(null, true)) {
+        if ($this->teacherView) {
             $this->set_header($column++, get_lang('Modify'), false, 'width="195px"');
             // Actions on multiple selected documents.
             $this->set_form_actions(array(
@@ -90,8 +104,14 @@ class GradebookTable extends SortableTable
                 )
             );
         } else {
-            if (empty($_GET['selectcat']) && !api_is_allowed_to_edit()) {
-                $this->set_header($column++, get_lang('Certificates'), false);
+            if (empty($_GET['selectcat']) && !$this->teacherView) {
+                if ($this->exportToPdf == false) {
+                    $this->set_header(
+                        $column++,
+                        get_lang('Certificates'),
+                        false
+                    );
+                }
             }
         }
     }
@@ -211,7 +231,7 @@ class GradebookTable extends SortableTable
             $invisibility_span_close = api_is_allowed_to_edit() && $item->is_visible() == '0' ? '</span>' : '';
 
             // Id
-            if (api_is_allowed_to_edit(null, true)) {
+            if ($this->teacherView) {
                 if ($this->exportToPdf == false) {
                     $row[] = $this->build_id_column($item);
                 }
@@ -251,7 +271,7 @@ class GradebookTable extends SortableTable
                 true
             );
 
-            if (api_is_allowed_to_edit(null, true)) {
+            if ($this->teacherView) {
                 $row[] = $invisibility_span_open .Display::tag('h4', $weight).$invisibility_span_close;
             } else {
                 $row[] = $invisibility_span_open .$weight.$invisibility_span_close;
@@ -260,7 +280,7 @@ class GradebookTable extends SortableTable
             $category_weight = $item->get_weight();
             $mainCategoryWeight = $main_cat[0]->get_weight();
 
-            if (api_is_allowed_to_edit(null, true)) {
+            if ($this->teacherView) {
                 $weight_total_links += $data[3];
             } else {
                 $cattotal = Category::load($_GET['selectcat']);
@@ -269,7 +289,7 @@ class GradebookTable extends SortableTable
             }
 
             // Edit (for admins).
-            if (api_is_allowed_to_edit(null, true)) {
+            if ($this->teacherView) {
                 $cat = new Category();
                 $show_message = $cat->show_message_resource_delete($item->get_course_code());
                 if ($show_message === false) {
@@ -344,16 +364,15 @@ class GradebookTable extends SortableTable
 
             // Loading children
             if (get_class($item) == 'Category') {
-                $stud_id = api_get_user_id();
                 $course_code = api_get_course_id();
                 $session_id = api_get_session_id();
                 $parent_id = $item->get_id();
                 $cats = Category::load($parent_id, null, null, null, null, null);
 
                 if (isset($cats[0])) {
-                    $allcat  = $cats[0]->get_subcategories($stud_id, $course_code, $session_id);
-                    $alleval = $cats[0]->get_evaluations($stud_id);
-                    $alllink = $cats[0]->get_links($stud_id);
+                    $allcat  = $cats[0]->get_subcategories($this->userId, $course_code, $session_id);
+                    $alleval = $cats[0]->get_evaluations($this->userId);
+                    $alllink = $cats[0]->get_links($this->userId);
 
                     $sub_cat_info = new GradebookDataGenerator($allcat, $alleval, $alllink);
                     $data_array = $sub_cat_info->get_data($sorting, $from, $this->per_page);
@@ -374,7 +393,7 @@ class GradebookTable extends SortableTable
                             $main_categories[$parent_id]['children'][$item->get_id()]['weight'] = $item->get_weight();
                         }
 
-                        if (api_is_allowed_to_edit(null, true)) {
+                        if ($this->teacherView) {
                             if ($this->exportToPdf == false) {
                                 $row[] = $this->build_id_column($item);
                             }
@@ -395,7 +414,7 @@ class GradebookTable extends SortableTable
                         // Weight
                         $row[] = $invisibility_span_open.$weight.$invisibility_span_close;
 
-                        if (api_is_allowed_to_edit(null, true)) {
+                        if ($this->teacherView) {
                             //$weight_total_links += intval($data[3]);
                         } else {
                             $cattotal = Category::load($_GET['selectcat']);
@@ -404,7 +423,7 @@ class GradebookTable extends SortableTable
                         }
 
                         // Admins get an edit column.
-                        if (api_is_allowed_to_edit(null, true)) {
+                        if ($this->teacherView) {
                             $cat = new Category();
                             $show_message = $cat->show_message_resource_delete($item->get_course_code());
                             if ($show_message === false) {
@@ -452,7 +471,7 @@ class GradebookTable extends SortableTable
 
                     // "Warning row"
                     if (!empty($data_array)) {
-                        if (api_is_allowed_to_edit()) {
+                        if ($this->teacherView) {
                             // Compare the category weight to the sum of all weights inside the category
                             if (intval($total_weight) == $category_weight) {
                                 $label = null;
@@ -484,7 +503,7 @@ class GradebookTable extends SortableTable
             }
         } //end looping categories
 
-        if (api_is_allowed_to_edit()) {
+        if ($this->teacherView) {
             // Total for teacher.
             if (count($main_cat) > 1) {
                 $main_weight = intval($main_cat[0]->get_weight());
@@ -573,7 +592,7 @@ class GradebookTable extends SortableTable
         // Warning messages
         $view = isset($_GET['view']) ? $_GET['view']: null;
 
-        if (api_is_allowed_to_edit()) {
+        if ($this->teacherView) {
             if (isset($_GET['selectcat']) &&
                 $_GET['selectcat'] > 0 &&
                 $view <> 'presence'

+ 57 - 7
main/inc/lib/pdf.lib.php

@@ -46,6 +46,7 @@ class PDF
         $this->params['add_signatures'] = isset($params['add_signatures']) ? $params['add_signatures'] : false;
         $this->params['show_real_course_teachers'] = isset($params['show_real_course_teachers']) ? $params['show_real_course_teachers'] : false;
         $this->params['student_info'] = isset($params['student_info']) ? $params['student_info'] : false;
+        $this->params['show_grade_generated_date'] = isset($params['show_grade_generated_date']) ? $params['show_grade_generated_date'] : false;
 
         $this->pdf = new mPDF(
             'UTF-8',
@@ -64,11 +65,16 @@ class PDF
 
     /**
      * Export the given HTML to PDF, using a global template
-     * @param string $content the HTML content
      *
      * @uses export/table_pdf.tpl
+
+     * @param $content
+     * @param bool|false $saveToFile
+     * @param bool|false $returnHtml
+     *
+     * @return string
      */
-    public function html_to_pdf_with_template($content)
+    public function html_to_pdf_with_template($content, $saveToFile = false, $returnHtml = false)
     {
         global $_configuration;
         Display :: display_no_header();
@@ -132,6 +138,8 @@ class PDF
         Display::$global_template->assign('pdf_date', api_format_date(api_get_local_time(), DATE_TIME_FORMAT_LONG));
         Display::$global_template->assign('pdf_teachers', $teacher_list);
         Display::$global_template->assign('pdf_title', $this->params['pdf_title']);
+        Display::$global_template->assign('pdf_student_info', $this->params['student_info']);
+        Display::$global_template->assign('show_grade_generated_date', $this->params['show_grade_generated_date']);
         Display::$global_template->assign('add_signatures', $this->params['add_signatures']);
 
         // Getting template
@@ -141,12 +149,21 @@ class PDF
 
         $css_file = api_get_path(TO_SYS, WEB_CSS_PATH).'/print.css';
         $css = file_exists($css_file) ? @file_get_contents($css_file) : '';
-        self::content_to_pdf(
+
+        $html = self::content_to_pdf(
             $html,
             $css,
             $this->params['filename'],
-            $this->params['course_code']
+            $this->params['course_code'],
+            'D',
+            $saveToFile,
+            null,
+            $returnHtml
         );
+
+        if ($returnHtml) {
+            return $html;
+        }
     }
 
     /**
@@ -170,7 +187,8 @@ class PDF
         $pdf_name = '',
         $course_code = null,
         $print_title = false,
-        $complete_style = true
+        $complete_style = true,
+        $addStyle = true
     ) {
         if ($complete_style === false) {
             error_log(__FUNCTION__.' with no style');
@@ -242,6 +260,13 @@ class PDF
                 continue;
             }
 
+            if ($addStyle) {
+                $css_file = api_get_path(TO_SYS, WEB_CSS_PATH).'/print.css';
+                $css = file_exists($css_file) ? @file_get_contents($css_file) : '';
+
+                $this->pdf->WriteHTML($css, 1);
+            }
+
             //it's not a chapter but the file exists, print its title
             if ($print_title) {
                 $this->pdf->WriteHTML(
@@ -363,7 +388,10 @@ class PDF
         $css = '',
         $pdf_name = '',
         $course_code = null,
-        $outputMode = 'D'
+        $outputMode = 'D',
+        $saveInFile = false,
+        $fileToSave = null,
+        $returnHtml = false
     ) {
         global $_configuration;
 
@@ -434,6 +462,10 @@ class PDF
         // $_GET[] too, as it is done with file name.
         // At the moment the title is retrieved from the html document itself.
 
+        if ($returnHtml) {
+            return "<style>$css</style>".$document_html;
+        }
+
         if (!empty($css)) {
             $this->pdf->WriteHTML($css, 1);
         }
@@ -445,7 +477,25 @@ class PDF
             $pdf_name = api_replace_dangerous_char($pdf_name);
             $output_file = $pdf_name.'.pdf';
         }
-        $this->pdf->Output($output_file, $outputMode); // F to save the pdf in a file
+        //$this->pdf->Output($output_file, $outputMode); // F to save the pdf in a file
+
+        if ($saveInFile) {
+            $fileToSave = !empty($fileToSave) ? $fileToSave : api_get_path(SYS_ARCHIVE_PATH).uniqid();
+
+            $this->pdf->Output(
+                $fileToSave,
+                $outputMode
+            ); // F to save the pdf in a file
+
+        } else {
+            $this->pdf->Output(
+                $output_file,
+                $outputMode
+            ); // F to save the pdf in a file
+            exit;
+        }
+
+
         if ($outputMode == 'F') {
             // Do NOT exit when export to file
         } else {

+ 7 - 0
main/template/default/export/table_pdf.tpl

@@ -63,6 +63,13 @@
     </tr>
 </table>
 
+
+{% if show_grade_generated_date == true %}
+    <h5 align="right">
+        {{ "GradeGenerated" | get_lang }} {{ "now"|date("d/m/Y") }}
+    </h5>
+{% endif %}
+
 <br />
 
 {{ pdf_content }}