Proyectos de Subversion Moodle

Rev

Rev 1296 | Rev 1298 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |

<?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/>.

namespace theme_universe_child\output;

use html_writer;
use stdClass;
use moodle_url;
use context_course;
use context_system;
use core_course_list_element;
use custom_menu;
use action_menu_filler;
use action_menu_link_secondary;
use action_menu;
use action_link;
use core_text;
use coding_exception;
use navigation_node;
use context_header;
use core\oauth2\rest;
use pix_icon;
use renderer_base;
use theme_config;
use get_string;
use core_course_category;
use theme_universe\util\user;
use theme_universe\util\course;
use core_completion\progress;

require_once($CFG->dirroot . '/cesa/statics_blocks.php'); // Incluimos StaticsBlocks


/**
 * Renderers to align Moodle's HTML with that expected by Bootstrap
 *
 * @package    theme_universe
 * @copyright  2023 Marcin Czaja (https://rosea.io)
 * @license    Commercial https://themeforest.net/licenses
 */
class core_renderer extends \theme_universe\output\core_renderer
{

    public function cesa_navigation_course_menu_lateral_output()
    {



        global $CFG, $COURSE, $PAGE, $DB, $OUTPUT;




        if (!$COURSE->id) {
            return '';
        }

        $course_id = $COURSE->id;

        $parent = optional_param('parent', 0, PARAM_INT); // Course module id
        if (!$parent) {
            $parent = optional_param('amp;parent', 0, PARAM_INT); // Course module id
        }

        //$PAGE->requires->js('/theme/edumynew/javascript/menu-lateral.js');


        if ($parent) {
            $sql  = ' SELECT * FROM {subcourse} WHERE course = ' . $parent;
            $sql .= ' AND refcourse = ' . $course_id;

            $recordParentRefCourse  = $DB->get_record_sql($sql);
            if ($recordParentRefCourse) {
                $course_id = $recordParentRefCourse->course;
            }
        }



        $course = get_course($course_id);
        $course_context = context_course::instance($course->id);
        $completioninfo = new \completion_info($course);

        // $course_context = !empty($PAGE->cm->id)  ? $PAGE->cm : \context_course::instance($COURSE->id);
        // First we should check if we want to add navigation.
        // Get a list of all the activities in the course.



        $menu = [];

        $currentServerLink = strtolower(trim($_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']));
        $currentServerLink = html_entity_decode($currentServerLink);
        $currentServerLink = htmlentities($currentServerLink);

        $parts = explode('&', $currentServerLink);
        if (count($parts) > 1) {
            $currentServerLink = $parts[0];
        }




        $modules = get_fast_modinfo($course->id)->get_cms();

        $prevmod = null;
        $nextmod = null;
        $currentmod = new stdClass();
        $currentmod->name = "Sin Módulos";
        $mods = [];
        $menu = [];
        $numberOfTemary = 1;
        $prevlink = new \StdClass();
        $nextlink = new \StdClass();
        $prevlink->url = "#";
        $nextlink->url = "#";

        $isACoursePage = $course->id !== "1" ? true : false;




        $modules = get_fast_modinfo($COURSE->id)->get_cms();
        /*
            echo '<pre>';
            print_r($modules);
            echo '</pre>';
            */

        if (!empty($modules)) {
            // Put the modules into an array in order by the position they are shown in the course.
            foreach ($modules as $module) {
                if (!$module->uservisible || $module->is_stealth() || empty($module->url)) {
                    continue;
                }
                $mods[$module->id] = $module;
            }


            $nummods = count($mods);


            // If there is only one mod then do nothing.
            if ($nummods == 1) {
            }

            // Get an array of just the course module ids used to get the cmid value based on their position in the course.
            $modids = array_keys($mods);

            // Get the position in the array of the course module we are viewing.
            $position = array_search($course_context->id, $modids);   //array_search($this->page->cm->id, $modids);

            $currentmod = $mods[$modids[$position]];



            // Check if we have a previous mod to show.
            if ($position > 0) {
                $prevmod = $mods[$modids[$position - 1]];
            }

            // Check if we have a next mod to show.
            if ($position < ($nummods - 1)) {
                $nextmod = $mods[$modids[$position + 1]];
            }
        }


        //$sections = $DB->get_records('course_sections', ['course' => $COURSE->id], 'section ASC', 'id,name,section,sequence,visible');




        $modinfo = get_fast_modinfo($course);
        $records  =  $modinfo->get_section_info_all();
        $sections = [];

        foreach ($records as $record) {
            if (!$record->visible) {
                continue;
            }

            $section = new \stdClass();
            $section->id = $record->id;
            $section->section = $record->section;
            $section->name = $record->name;
            $section->parent = $record->parent;
            $section->visible = 1;


            array_push($sections, $section);
        }



        $openParent = 0;
        $maxSections = count($sections);
        for ($i = 0; $i < $maxSections; $i++) {



            $j = $i + 1;
            if ($j < $maxSections) {

                if ($sections[$j]->parent) {
                    $openParent = true;
                    $sections[$i]->close_section_parent = 0;
                    $sections[$i]->close_section = 0;
                } else {
                    $sections[$i]->close_section_parent = $openParent ? 1 : 0;

                    $sections[$i]->close_section = 1;
                }
            } else {
                $sections[$i]->close_section_parent = $openParent ? 1 : 0;
                $sections[$i]->close_section = 1;
            }

            // print_r($section);


        }

        /*
             echo '<pre>';
           print_r($sections);
           echo '</pre>';
      
      */




        foreach ($sections as $key =>  $section) {

            if (!$section->visible) {
                continue;
            }

            $activities = [];
            $section_active = false;

            foreach ($modules as $module) {

                if ($module->section  != $section->id) {
                    continue;
                }

                if (!$module->uservisible || $module->is_stealth()) {
                    // if (!$module->uservisible || $module->is_stealth() || empty($module->url)) {
                    continue;
                }

                $mods[$module->id] = $module;

                $duration = '';
                $type = '';
                $filepath = '';

                $sql = 'SELECT md.name, cm.instance  FROM {modules} md  ' .
                    ' JOIN {course_modules} cm ON cm.module = md.id ' .
                    ' WHERE cm.id = '  . $module->id . ' LIMIT 1 ';

                $record = $DB->get_record_sql($sql);
                if ($record) {

                    /*
                        echo '<pre>';
                        print_r($record);
                        echo '<pre>';
                        */

                    $type = $record->name;

                    if ($type == 'hvp') {
                        $instance   = $record->instance;

                        $sql = 'SELECT json_content FROM {hvp} WHERE id = ' . $instance . ' LIMIT 1';
                        $record = $DB->get_record_sql($sql);

                        if ($record) {
                            $json_content = json_decode($record->json_content);

                            if (!empty($json_content->interactiveVideo->video->files[0]->path)) {
                                $filepath = trim($json_content->interactiveVideo->video->files[0]->path);
                                $filepath = trim(str_replace('#tmp', '', $filepath));


                                $arr = explode('/', $filepath);
                                if (count($arr) > 1) {
                                    $filename = $arr[count($arr) - 1];
                                } else {
                                    $filename = $arr[0];
                                }

                                $record = $DB->get_record('files', ['filename' => $filename]);
                                if ($record) {
                                    $duration = $this->extractDurationFromVideo($record, $filename);
                                }
                            }
                        }
                    }
                }

                $modname = $module->get_formatted_name();
                $modcontent = $module->get_formatted_content();

                if (empty($module->url)) {
                    $linkurl = '';
                    $currentLink = '';
                } else {

                    $linkurl = new moodle_url($module->url, array('forceview' => 1));
                    $linkurl = strtolower(trim($linkurl->__toString()));

                    $parts = explode('&', $linkurl);
                    if (count($parts) > 1) {
                        $currentLink = $parts[0];
                    } else {
                        $currentLink = $linkurl;
                    }
                }

                $completiondata = $completioninfo->get_data($module, true);

                if (strcasecmp($currentLink,  $currentServerLink) == 0) {
                    $active = true;
                    $section_active = true;
                } else {
                    $active = false;
                }

                array_push($activities, [
                    'active' => $active,
                    'indexElement' => $key,
                    'numberOfTemary' => $numberOfTemary,
                    'activity' => true,
                    'blank' => !empty($section->section),
                    'name' => $this->substr_cesa_navigation_course_menu_name($modname),
                    'content' => $modcontent,
                    'duration' => $duration,
                    'type' =>  $type,
                    'completed' => $completiondata->completionstate == COMPLETION_COMPLETE || $completiondata->completionstate == COMPLETION_COMPLETE_PASS,
                    'failed' => $completiondata->completionstate == COMPLETION_COMPLETE_FAIL,
                    'url' => $linkurl
                ]);
            }


            if ($activities) {

                if ($section->section) {
                    $sectionname = trim($section->name);
                    $sectionname = $sectionname ? $sectionname : ' Tema ' . $section->section;
                } else {
                    $sectionname = 'Recursos';
                }

                array_push($menu, [
                    'active' => $section_active,
                    'indexElement' => $key,
                    'id' => $section->id,
                    'numberOfTemary' => $numberOfTemary,
                    'section' => true,
                    'name' => $this->substr_cesa_navigation_course_menu_name($sectionname),
                    'close_section_parent' => $section->close_section_parent,
                    'close_section' => $section->close_section,
                    'completed' => false,
                    'url' => false,
                    'activities' => $activities,


                ]);
            }


            $numberOfTemary = ++$numberOfTemary;
        }

        // Check if there is a previous module to display.
        if ($prevmod) {
            $linkurl = new \moodle_url($prevmod->url, array('forceview' => 1));
            $attributes = [];
            $prevlink = new \action_link($linkurl, $OUTPUT->larrow(), null, $attributes);
        }

        // Check if there is a next module to display.
        if ($nextmod) {
            $linkurl = new \moodle_url($nextmod->url, array('forceview' => 1));
            $attributes = [];
            $nextlink = new \action_link($linkurl, $OUTPUT->rarrow(), null, $attributes);
        }


        //  echo '<pre>'; 
        // print_r($activities);
        // echo '</pre>';
        // exit;


        $progreso = number_format(progress::get_course_progress_percentage($course), 2); // Progreso por curso

        $course_navigation = new \theme_universe_child\output\custom_drawer($prevlink->url, $nextlink->url, $menu, $this->substr_cesa_navigation_course_menu_name($currentmod->name, 10), false, $isACoursePage,  $COURSE->summary, $progreso);
        return $course_navigation->export_for_template($OUTPUT);
    }

    public function render_statics_blocks($userid = null)
    {
        global $USER;

        if (!$userid) {
            $userid = $USER->id;
        }

        // Instanciamos StaticsBlocks para renderizar los bloques
        $statics_blocks = new \StaticsBlocks(
            'side-pre',
            ['messageteacher', 'comments', 'cesa_course_rating', 'cesa_notes']
        );

        $blocks = $statics_blocks->renderBlocks();

        return $blocks;
    }

    function universe_child_course_drawer(): string
    {
        global $PAGE;

        // If the course index is explicitly set and if it should be hidden.
        if ($PAGE->get_show_course_index() === false) {
            return '';
        }

        // Only add course index on non-site course pages.
        if (!$PAGE->course || $PAGE->course->id == SITEID) {
            return '';
        }

        // Show course index to users can access the course only.
        if (!can_access_course($PAGE->course, null, '', true)) {
            return '';
        }

        $format = course_get_format($PAGE->course);
        $renderer = $format->get_renderer($PAGE);
        if (method_exists($renderer, 'course_index_drawer')) {
            return $this->render_from_template('theme_universe_child/courseindex/drawer', []);
        }

        return '';
    }
}