Proyectos de Subversion Moodle

Rev

Autoría | 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/>.

/**
 *  Internal library of functions for module
 */

defined('MOODLE_INTERNAL') || die();

require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->libdir.'/formslib.php');
require_once('admin/dashboard_time_form.php');

/**
 * Loads the saved data from report_training_data
 * @param int $starttime filters entries with starttime
 * @param int $endtime filters entries with endtime
 * @return Array $chartsdata of records or null
 */
function report_training_load_datas($starttime = null, $endtime = null) {
    global $DB;

    if (empty($starttime) || is_null($starttime)) {
        $conf = $DB->get_record('report_training', array());
        $starttime = $conf->starttime;
    }
    if (empty($endtime) || is_null($endtime)) {
        $endtime = time();
    }
    $chartsdata = $DB->get_records_sql('SELECT * FROM {report_training_data} WHERE objectdate >= ? AND objectdate <= ?',
        array($starttime, $endtime));
    if (! empty($chartsdata) || ! is_null($chartsdata)) {

        //var_dump($chartsdata);
        
        return $chartsdata;
    } else {
        return null;
    }
}

/**
 * Creates the Outputcontainer for dashboard.php
 * @param Array $chartout array of containers
 * @param Array $vtables array of html_tables
 * @return String all containers for output
 */
function report_training_create_containers($chartout, $vtables) {
    $vout = array();
    $vout[] = report_deviceanaltics_create_wrapper_container(
        'device_types',
        'dashboard_chart_device_types',
        $chartout[0],
        $vtables[0]);
    $vout[] = report_deviceanaltics_create_wrapper_container(
        'device_systems',
        'dashboard_chart_device_systems',
        $chartout[1],
        $vtables[1]);
    $vout[] = report_deviceanaltics_create_wrapper_container(
        'device_browser',
        'dashboard_chart_device_browser',
        $chartout[2],
        $vtables[2]);
    $vout[] = report_deviceanaltics_create_wrapper_container(
        'screen_sizes',
        'dashboard_chart_screen_sizes',
        $chartout[3],
        $vtables[3]);
    $vout[] = report_deviceanaltics_create_wrapper_container(
        'window_sizes',
        'dashboard_chart_window_sizes',
        $chartout[4],
        $vtables[4]);
    $vout[] = report_deviceanaltics_create_wrapper_container(
        'pointing_method',
        'dashboard_chart_pointing_method',
        $chartout[5],
        $vtables[5]);
    return $vout;
}


/**
 * Check if object holds screen data
 * @param Object $var entryobject
 * @return bool true/false
 * @deprecated
 */
function report_device_analytics_is_not_null($var) {
    return !is_null($var->devicedisplaysizex);
}


/**
 * Calculate the number of subversion inside array
 * @param Array $datarray array ob records
 * @return int count of subversion
 */
function report_device_analytics_calc_numbers_of_version($datarray) {
    $returnnumber = 0;
    foreach ($datarray as $value) {
         $returnnumber += $value;
    }
    return $returnnumber;
}

/**
 * Calculate the percent for element inside array
 * @param Array $datarray array with prechecked elements
 * @param String $calckey calc elementkey
 * @return float percent of searched value
 */
function report_device_analytics_calc_percent($datarray, $calckey) {
    $groundvalue = 0;
    $procvalue = $datarray[$calckey];
    foreach ($datarray as $val) {
        $groundvalue += $val;
    }
    return number_format((($procvalue / $groundvalue) * 100) , 2);
}

/**
 * Same as report_device_analytics_calc_percent, but from subarray
 * @param Array $datarray array with prechecked elements
 * @param String $calckey calc elementkey
 * @return float percent of searched value
 */
function report_device_analytics_calc_percent_from_sub($datarray, $calckey) {
    $groundvalue = 0;
    $procsub = array();
    foreach ($datarray as $key => $subvalue) {
        $subval = 0;
        foreach ($subvalue as $value) {
            $subval += $value;
        }
        $procsub[$key] = $subval;
        $groundvalue += $subval;
    }
    return number_format((($procsub[$calckey] / $groundvalue) * 100) , 2);
}

/**
 * Creates the wrapper container for charts and tables - also write tables
 * @param String $wrappername name of the div wrapper
 * @param String $headername heading line - from moodle/lang
 * @param String $chartoutput holds all information for the charts
 * @param html_table $vtables table object for html_writer
 * @return String $oretrun output-string
 */
function report_deviceanaltics_create_wrapper_container($wrappername, $headername, $chartoutput, $vtables) {
    global $OUTPUT;
    $oreturn = $OUTPUT->heading(get_string($headername, 'report_training'), 4);
    $oreturn .= $OUTPUT->container_start('wrapper', $wrappername);
    if (! empty($chartoutput) || ! is_null($chartoutput)) {
        $oreturn .= $chartoutput;
    }
    if (! empty($vtables) || ! is_null($vtables)) {
        $oreturn .= html_writer::table($vtables);
    }
    $oreturn .= $OUTPUT->container_end();
    return $oreturn;
}

/**
 * Calculate the Tables
 * @param Array $datas - preselected entries form data table
 * @return String $returntables output-string
 */
function report_training_create_data_tables($datas) {
    $returntables = array();
    $returntables[0] = new html_table();
    $returntables[0]->head = (array) get_strings(array('table_type', 'table_percent', 'table_count'), 'report_training');
    $devicetypes = array();
    foreach ($datas as $devicetypedata) {
        $type = $devicetypedata->devicetype;

        if (array_key_exists($type, $devicetypes)) {
            $devicetypes[$devicetypedata->devicetype]++;
        } else {
            $devicetypes[$devicetypedata->devicetype] = 1;
        }
    }
    foreach ($devicetypes as $tname => $count) {
        $returntables[0]->data[] = array($tname, report_device_analytics_calc_percent($devicetypes, $tname)."%", $count);
    }

    $returntables[1] = new html_table();
    $returntables[1]->head = (array) get_strings(array('table_os', 'table_percent', 'table_count'), 'report_training');
    $deviceoses = array();
    foreach ($datas as $deviceosdata) {
        $type = $deviceosdata->devicesystem;
        if (array_key_exists($type, $deviceoses)) {
            $deviceoses[$deviceosdata->devicesystem]++;
        } else {
            $deviceoses[$deviceosdata->devicesystem] = 1;
        }
    }
    foreach ($deviceoses as $tname => $count) {
        $returntables[1]->data[] = array($tname, report_device_analytics_calc_percent($deviceoses, $tname)."%", $count);
    }

    $returntables[2] = new html_table();
    $returntables[2]->head = (array) get_strings(array('table_browser', 'table_percent', 'table_count'), 'report_training');
    $devicebrowser = array();

    foreach ($datas as $devicebrowserdata) {
        $browser = $devicebrowserdata->devicebrowser;
        $browserversion = $devicebrowserdata->devicebrowserversion;
        if (array_key_exists($browser, $devicebrowser)) {
            if (array_key_exists($devicebrowserdata->devicebrowserversion, $devicebrowser[$devicebrowserdata->devicebrowser])) {
                $devicebrowser[$devicebrowserdata->devicebrowser][$devicebrowserdata->devicebrowserversion]++;
            } else {
                $devicebrowser[$devicebrowserdata->devicebrowser][$devicebrowserdata->devicebrowserversion] = 1;
            }
        } else {
            $devicebrowser[$devicebrowserdata->devicebrowser] = array();
            $devicebrowser[$devicebrowserdata->devicebrowser][$devicebrowserdata->devicebrowserversion] = 1;
        }
    }
    foreach ($devicebrowser as $bname => $sub) {
        $returntables[2]->data[] = array(
            '<b>'.$bname.'</b>',
            report_device_analytics_calc_percent_from_sub($devicebrowser, $bname).'%',
            report_device_analytics_calc_numbers_of_version($sub)
        );
        foreach ($sub as $vnum => $scount) {
            $returntables[2]->data[] = array(
                get_string('table_version', 'report_training').': '.$vnum,
                report_device_analytics_calc_percent($sub, $vnum).'%',
                $scount
            );
        }
    }

    $returntables[3] = new html_table(); //era null
    $returntables[3] ->head = (array) get_strings(array('table_userid', 'table_name', 'table_fecha'), 'report_training'); //null;
    $usuarios = array();
    foreach ($datas as $userdata) {
        $userid = $userdata->userid;

        // if (array_key_exists($userid, $usuarios)) {
            $usuarios[$userdata->userid]++;
        // } else {
            // $usuarios[$userdata->userid] = 1;
        // }
    }

    foreach ($usuarios as $tname => $count) {
        $returntables[3]->data[] = array($tname, report_device_analytics_calc_percent($devicetypes, $tname)."%", $count);
    }
   

    $returntables[5] = new html_table();
    $returntables[5]->head = (array) get_strings(array('table_pointing', 'table_percent', 'table_count'), 'report_training');
    $devicepointing = array();
    foreach ($datas as $devicepointdata) {
        $ptype = $devicepointdata->devicepointingmethod;
        if (array_key_exists($ptype, $devicepointing)) {
            $devicepointing[$devicepointdata->devicepointingmethod]++;
        } else {
            $devicepointing[$devicepointdata->devicepointingmethod] = 1;
        }
    }
    foreach ($devicepointing as $tname => $count) {
        $returntables[5]->data[] = array($tname, report_device_analytics_calc_percent($devicepointing, $tname)."%", $count);
    }

    return $returntables;
}

/**
 * Create Canvas Elements for the charts output
 * @return Array $returncharts array of canvas elements
 */
function report_training_create_charts() {
    $returncharts = array();
    $returncharts[0] = '<canvas class="rd_chart" id="chart_devicetypes"></canvas>';
    $returncharts[1] = '<canvas class="rd_chart" id="chart_devicesystems"></canvas>';
    $returncharts[2] = '<canvas class="rd_chart" id="chart_devicebrowsers"></canvas>';
    $returncharts[3] = '<canvas class="rd_chart" id="chart_devicedisplaysize"></canvas>';
    $returncharts[4] = '<canvas class="rd_chart" id="chart_devicewindowsize"></canvas>';
    $returncharts[5] = null;
    return $returncharts;
}

/**
 * Loads the saved data from report_training_data
 * @param string $userid filters entries by user name
 * @return Array $conf of records or null
 */
function report_training_get_user_connections($userid = null) {
    global $DB;
    if ($userid !== null) {
        $conf = $DB->get_records('report_training_data', ['userid' => (int) $userid], '', 'devicesystem,devicebrowser,devicetype,ip');
        return $conf;
    } else {
        return null;
    }
}
// TODO: CAMBIAR CONSULTA DEL LAST ACCES A FORM_DATA
/**
 * Loads the saved data from report_training_activities
 * @param string $userid filters entries by user id
 * @return Array $conf of records or null
 */
function report_training_get_user_courses_activity($userid = null) {
    global $DB;
    if ($userid !== null) {
        $user_last_access = $DB->get_records('report_training_activities', ["userid" => (int) $userid]);
       
        foreach($user_last_access as $record) {
            $course = $DB->get_record('course', ["id" => $record->courseid], 'fullname');
            if ($course) {
                $coursename = $course->fullname;
                $coursemodules = get_fast_modinfo($record->courseid);
                $module = $coursemodules->get_cm($record->cmid);
                $record->timeaccess = date("d-m-Y", $record->timeaccess);
                $record->coursename = $coursename;
                $record->modname = $module->name;
            } else {
                $record->coursename = null;
            }
        }
        
        return $user_last_access;
    } else {
        return null;
    }
}

/**
 * Creates tables with user connections data
 * @param Array $userconnections provides data to create the table
 * @return html_table $table element to display user data or null
 */
function report_training_create_user_connections_table($userconnections = null) {

    if ($userconnections !== null) {
        $table = new html_table();

        $table->head = array(get_string('table_os', 'report_training'), get_string('table_browser', 'report_training'), get_string('dashboard_chart_device_types', 'report_training'), get_string('userdata_table_ip', 'report_training'));

        foreach($userconnections as $connectioninfo) {
            $table->data[] = array($connectioninfo->devicesystem, $connectioninfo->devicebrowser, $connectioninfo->devicetype, $connectioninfo->ip);
        }
        return $table;
    } else {
        return null;
    }
}

/**
 * Creates tables with user activity data
 * @param Array $useractivityrecords provides data to create the table
 * @return html_table $table element to display user data or null
 */
function report_training_create_activity_user_table($useractivityrecords = null) {
    if ($useractivityrecords !== null) {
        $table = new html_table();

        $table->head = array(get_string('userdata_table_viewed_date', 'report_training'), get_string('userdata_table_course_name', 'report_training'), get_string('userdata_table_module_name', 'report_training'), get_string('userdata_table_module_link', 'report_training'));

        foreach($useractivityrecords as $record) {
            if ($record->coursename) {
                $table->data[] = array($record->timeaccess, $record->coursename, $record->modname,'<a class="report-training-table-link" href='.$record->viewurl.'>'.get_string('userdata_table_module_click', 'report_training') . '</a>');
            }
        }

        return $table;
    } else {
        return null;
    }
}