Skip to content
Snippets Groups Projects
Select Git revision
  • excel-export-evaluation
  • master default protected
  • 4xx_dev
  • 33_STABLE
  • 2022050502
  • 2022050501
  • 2022021501
  • 2022021500
8 results

participants_export_xls_groups.php

Blame
  • Peter Fricke's avatar
    Peter Fricke authored
    c986de8a
    History
    participants_export_xls_groups.php 3.61 KiB
    <?php
    
    // This file is part of Moodle - http://moodle.org/
    //
    // Moodle is free software: you can redistribute it and/or modify
    // it under the terms of the GNU General Public License as published by
    // the Free Software Foundation, either version 3 of the License, or
    // (at your option) any later version.
    //
    // Moodle is distributed in the hope that it will be useful,
    // but WITHOUT ANY WARRANTY; without even the implied warranty of
    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    // GNU General Public License for more details.
    //
    // You should have received a copy of the GNU General Public License
    // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
    
    require_once($CFG->dirroot . '/grade/export/lib.php');
    require_once($CFG->dirroot . '/lib/excellib.class.php');
    
    class participants_export_xls_groups {
    
        public $course = '';
        public $print_groups = true;
        public $profilefields = null;
        public $users = null;
        public $context = 0;
    
        /**
         * Constructor should set up all the private variables ready to be pulled
         * @param object $course
         * @param boolean $groups
         */
        public function __construct($course, $print_groups) {
            $this->course = $course;
            $this->print_groups = $print_groups;
        }
    
        public function print_grades() {
    
            $this->context = context_course::instance($this->course->id);
            require_capability('moodle/grade:export', $this->context);
            // Calculate file name
            $shortname = format_string($this->course->shortname, true, array('context' => $this->context));
            $downloadfilename = clean_filename("$shortname.xls");
            // Creating a workbook
            $workbook = new MoodleExcelWorkbook("-");
            // Sending HTTP headers
            $workbook->send($downloadfilename);
    
            // Print names of all the fields
            $this->profilefields = array('lastname' => get_string('lastname'), 'firstname' => get_string('firstname'), 'email' => get_string('email'));
    
            $this->users = get_enrolled_users($this->context, null, 0, 'u.*');
            $myxls = $workbook->add_worksheet(strip_tags($shortname));
            $myxls->write_string(0, 0, $shortname);
            $this->print_profilefields($myxls);
            $this->print_values($myxls);
    
            if ($this->print_groups) {
                // Get all groups
                $this->print_groups($workbook);
            }
            /// Close the workbook
            $workbook->close();
            exit;
        }
    
        public function print_profilefields($myxls) {
            $i = 0;
            foreach ($this->profilefields as $id => $field) {
                $myxls->write_string(1, $i++, $field);
            }
        }
    
        public function print_values($myxls) {
            $i = 2;
            foreach ($this->users as $userid => $value) {
                $j = 0;
                foreach ($this->profilefields as $id => $field) {
                    //$fieldvalue = grade_helper::get_user_field_value($userid, $field);
                    $array = (array) $value;
                    $myxls->write_string($i, $j++, $array[$id]);
                }
                $i++;
            }
        }
    
        public function print_groups($workbook) {
            global $DB;
            $groups = $DB->get_records('groups', array('courseid' => $this->course->id), 'name');
            foreach ($groups as $act_group) {
                // Adding the worksheet
                $myxls = $workbook->add_worksheet(strip_tags(format_string($act_group->name)));
                $myxls->write_string(0, 0, format_string($act_group->name));
                $this->print_profilefields($myxls);
                $this->users = get_enrolled_users($this->context, null, $act_group->id, 'u.*');
                $this->print_values($myxls);
            }
        }
    
    }