Skip to content
Snippets Groups Projects
content.php 4.59 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/>.

/**
 * Contains the default content output class.
 *
 * @package   format_topics
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */

namespace format_iena\output\courseformat;

use core_courseformat\output\local\content as content_base;
use format_iena\entity\course_format_iena_completion;
require_once($CFG->libdir . '/accesslib.php');


/**
 * Base class to render a course content.
 *
 * @package   format_topics
 * @copyright 2020 Ferran Recio <ferran@moodle.com>
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 */
class content extends content_base {

    /**
     * Export this data so it can be used as the context for a mustache template (core/inplace_editable).
     *
     * @param renderer_base $output typically, the renderer that's calling this function
     * @return stdClass data context for a mustache template
     */
    public function export_for_template(\renderer_base $output) {
        global $CFG, $COURSE, $USER;

        $data=parent::export_for_template($output);
        //$section_entity = new course_format_iena_sections();
        
        // return false if completion disabled (site or course)
        $completion=new course_format_iena_completion();
        $cpl = $completion->get_completion_by_sections($data->sections);
        /* Passer le calcul de complétion de la classe course_format_iena_completion à part à la vue qui fait le header permet de ne pas récupérer deux fois les mêmes informations. */
        //$header = new view_course_header($nameSections, $idSections, $course, $cpl);
        $progress= $this->create_view($cpl);

        foreach ($progress as $key => $value) {
            $data->$key=$value;
        }
        //$data->progress=(object)$progress;
        //$data->student=$progress['student'];
        //$data->summary=$progress['summary'];
        //$course_infos=$header->get_data($cpl);


        return $data;
    }

    /**
     * Retourne un tableau d'informations : array progress_total, array progress_sections, bool student, string summary
     * @param array of object $infos 
     * @return array
     */
    private function get_data($infos){
        global $CFG, $COURSE, $USER;
        require_once($CFG->libdir . '/accesslib.php');
        $course_data=[];
        $course_data['student']=false;
        //if user is a student
        if(!has_capability('course/iena:suivi', $context = \context_course::instance($COURSE->id), $USER->id) &&$infos["progress"]){
            $course_data['progress_total']=$infos['progress']->total;
            


            // PROGRESS TOTAL : total progression percentage
            if ( $infos['progress'] !== false && $infos['progress']->total !== false ) {
                
                
                $course_data['progress_sections']=[];
                // section progress total
                foreach ($infos['progress']->sections as $section) {
                    if ( count($section->modules) > 0 ) {
                        array_push($course_data['progress_sections'], $section);
                    
                    }
                }
            }
            else {
            
            }
            $course_data['student']=true;
        }

        $course_data['summary']=$COURSE->summary;

        return $course_data;
    }

    /**
     * Description
     * @param type $progress 
     * @return type
     */
    public function create_view($progress) {
        // $this->get_progress_bis($this->section_names, $this->idSection, $this->course);
        //$prog = $progress != false ? $progress : $this->get_progress_bis($this->section_names, $this->idSection, $this->course);
        $infos = [
            'progress' => $progress,
            //'teachers' => $this->get_teachers(),
            //'groups' => $this->get_groupes(),
            // 'attendance' => $this->get_attendance_link()
        ];
        
        return $this->get_data($infos);
    }
}