Ejemplo n.º 1
0
/**
 * Gather course overview data and print the chart.
 *
 * @param int $report represents the report type field on the course overview report filter.
 * @param int $time represents the time period field on the course overview report filter.
 * @param int $numcourses represents the number of courses field on the course overview report filter.
 * @return void
 */
function report_courseoverview_print_chart($report, $time, $numcourses)
{
    global $DB, $OUTPUT, $PAGE;
    $param = stats_get_parameters($time, $report, SITEID, STATS_MODE_RANKED);
    if (!empty($param->sql)) {
        $sql = $param->sql;
    } else {
        $sql = "SELECT courseid, {$param->fields}\n                  FROM {" . 'stats_' . $param->table . "}\n                 WHERE timeend >= {$param->timeafter}\n                   AND stattype = 'activity'\n                   AND roleid = 0\n              GROUP BY courseid\n                   {$param->extras}\n              ORDER BY {$param->orderby}";
    }
    $courses = $DB->get_records_sql($sql, $param->params, 0, $numcourses);
    if (empty($courses)) {
        $PAGE->set_url('/report/courseoverview/index.php');
        print_error('statsnodata', 'error', $PAGE->url->out());
    }
    $data = [];
    $i = 0;
    foreach ($courses as $c) {
        $data['labels'][$i] = $DB->get_field('course', 'shortname', array('id' => $c->courseid));
        // Line3 represents the third column of the report table except for the most active users report.
        // It is a float number and can be participation radio or activity per user.
        if (isset($c->line3)) {
            $data['series'][$i] = round($c->line3, 2);
        } else {
            $data['series'][$i] = $c->{$param->graphline};
        }
        $i++;
    }
    $chart = new \core\chart_bar();
    $series = new \core\chart_series($param->{$param->graphline}, $data['series']);
    $chart->add_series($series);
    $chart->set_labels($data['labels']);
    echo $OUTPUT->render($chart);
}
Ejemplo n.º 2
0
/**
 * This function is used to generate and display the log activity graph
 *
 * @global stdClass $CFG
 * @param  stdClass $course course instance
 * @param  int|stdClass    $user id/object of the user whose logs are needed
 * @param  string $typeormode type of logs graph needed (usercourse.png/userday.png) or the mode (today, all).
 * @param  int $date timestamp in GMT (seconds since epoch)
 * @param  string $logreader Log reader.
 * @return void
 */
function report_log_print_graph($course, $user, $typeormode, $date = 0, $logreader = '')
{
    global $CFG, $OUTPUT;
    if (!is_object($user)) {
        $user = core_user::get_user($user);
    }
    $logmanager = get_log_manager();
    $readers = $logmanager->get_readers();
    if (empty($logreader)) {
        $reader = reset($readers);
    } else {
        $reader = $readers[$logreader];
    }
    // If reader is not a sql_internal_table_reader and not legacy store then don't show graph.
    if (!$reader instanceof \core\log\sql_internal_table_reader && !$reader instanceof logstore_legacy\log\store) {
        return array();
    }
    $coursecontext = context_course::instance($course->id);
    $a = new stdClass();
    $a->coursename = format_string($course->shortname, true, array('context' => $coursecontext));
    $a->username = fullname($user, true);
    if ($typeormode == 'today' || $typeormode == 'userday.png') {
        $logs = report_log_usertoday_data($course, $user, $date, $logreader);
        $title = get_string("hitsoncoursetoday", "", $a);
    } else {
        if ($typeormode == 'all' || $typeormode == 'usercourse.png') {
            $logs = report_log_userall_data($course, $user, $logreader);
            $title = get_string("hitsoncourse", "", $a);
        }
    }
    if (!empty($CFG->preferlinegraphs)) {
        $chart = new \core\chart_line();
    } else {
        $chart = new \core\chart_bar();
    }
    $series = new \core\chart_series(get_string("hits"), $logs['series']);
    $chart->add_series($series);
    $chart->set_title($title);
    $chart->set_labels($logs['labels']);
    $yaxis = $chart->get_yaxis(0, true);
    $yaxis->set_label(get_string("hits"));
    $yaxis->set_stepsize(max(1, round(max($logs['series']) / 10)));
    echo $OUTPUT->render($chart);
}
Ejemplo n.º 3
0
 public function print_analysed($item, $itemnr = '', $groupid = false, $courseid = false)
 {
     global $OUTPUT;
     $analysed_item = $this->get_analysed($item, $groupid, $courseid);
     if ($analysed_item) {
         echo "<table class=\"analysis itemtype_{$item->typ}\">";
         echo '<tr><th colspan="2" align="left">';
         echo $itemnr . ' ';
         if (strval($item->label) !== '') {
             echo '(' . format_string($item->label) . ') ';
         }
         echo format_string($analysed_item[1]);
         echo '</th></tr>';
         echo '</table>';
         $analysed_vals = $analysed_item[2];
         $avg = 0.0;
         $count = 0;
         $data = [];
         foreach ($analysed_vals as $val) {
             $avg += $val->avg;
             $quotient = format_float($val->quotient * 100, 2);
             $answertext = '(' . $val->value . ') ' . format_text(trim($val->answertext), FORMAT_HTML, array('noclean' => true, 'para' => false));
             if ($val->quotient > 0) {
                 $strquotient = ' (' . $quotient . ' %)';
             } else {
                 $strquotient = '';
             }
             $data['labels'][$count] = $answertext;
             $data['series'][$count] = $val->answercount;
             $data['series_labels'][$count] = $val->answercount . $strquotient;
             $count++;
         }
         $chart = new \core\chart_bar();
         $chart->set_horizontal(true);
         $series = new \core\chart_series(format_string(get_string("responses", "feedback")), $data['series']);
         $series->set_labels($data['series_labels']);
         $chart->add_series($series);
         $chart->set_labels($data['labels']);
         echo $OUTPUT->render($chart);
         $avg = format_float($avg, 2);
         echo '<tr><td align="left" colspan="2"><b>';
         echo get_string('average', 'feedback') . ': ' . $avg . '</b>';
         echo '</td></tr>';
     }
 }
Ejemplo n.º 4
0
 /**
  * Output the HTML needed to show the statistics graph.
  *
  * @param int|object $quizorid The quiz, or its ID.
  * @param qubaid_condition $qubaids the question usages whose responses to analyse.
  * @param string $whichattempts Which attempts constant.
  */
 protected function output_statistics_graph($quizorid, $qubaids)
 {
     global $DB, $PAGE;
     $quiz = $quizorid;
     if (!is_object($quiz)) {
         $quiz = $DB->get_record('quiz', array('id' => $quizorid), '*', MUST_EXIST);
     }
     // Load the rest of the required data.
     $questions = quiz_report_get_significant_questions($quiz);
     // Only load main question not sub questions.
     $questionstatistics = $DB->get_records_select('question_statistics', 'hashcode = ? AND slot IS NOT NULL', [$qubaids->get_hash_code()]);
     // Configure what to display.
     $fieldstoplot = ['facility' => get_string('facility', 'quiz_statistics'), 'discriminativeefficiency' => get_string('discriminative_efficiency', 'quiz_statistics')];
     $fieldstoplotfactor = ['facility' => 100, 'discriminativeefficiency' => 1];
     // Prepare the arrays to hold the data.
     $xdata = [];
     foreach (array_keys($fieldstoplot) as $fieldtoplot) {
         $ydata[$fieldtoplot] = [];
     }
     // Fill in the data for each question.
     foreach ($questionstatistics as $questionstatistic) {
         $number = $questions[$questionstatistic->slot]->number;
         $xdata[$number] = $number;
         foreach ($fieldstoplot as $fieldtoplot => $notused) {
             $value = $questionstatistic->{$fieldtoplot};
             if (is_null($value)) {
                 $value = 0;
             }
             $value *= $fieldstoplotfactor[$fieldtoplot];
             $ydata[$fieldtoplot][$number] = number_format($value, 2);
         }
     }
     // Create the chart.
     sort($xdata);
     $chart = new \core\chart_bar();
     $chart->get_xaxis(0, true)->set_label(get_string('position', 'quiz_statistics'));
     $chart->set_labels(array_values($xdata));
     foreach ($fieldstoplot as $fieldtoplot => $notused) {
         ksort($ydata[$fieldtoplot]);
         $series = new \core\chart_series($fieldstoplot[$fieldtoplot], array_values($ydata[$fieldtoplot]));
         $chart->add_series($series);
     }
     // Find max.
     $max = 0;
     foreach ($fieldstoplot as $fieldtoplot => $notused) {
         $max = max($max, max($ydata[$fieldtoplot]));
     }
     // Set Y properties.
     $yaxis = $chart->get_yaxis(0, true);
     $yaxis->set_stepsize(10);
     $yaxis->set_label('%');
     $output = $PAGE->get_renderer('mod_quiz');
     $graphname = get_string('statisticsreportgraph', 'quiz_statistics');
     echo $output->chart($chart, $graphname);
 }
Ejemplo n.º 5
0
    /**
     * Get a chart.
     *
     * @param string[] $labels Chart labels.
     * @param int[] $data The data.
     * @return \core\chart_base
     */
    protected static function get_chart($labels, $data) {
        $chart = new \core\chart_bar();
        $chart->set_labels($labels);
        $chart->get_xaxis(0, true)->set_label(get_string('grade'));

        $yaxis = $chart->get_yaxis(0, true);
        $yaxis->set_label(get_string('participants'));
        $yaxis->set_stepsize(max(1, round(max($data) / 10)));

        $series = new \core\chart_series(get_string('participants'), $data);
        $chart->add_series($series);
        return $chart;
    }
Ejemplo n.º 6
0
 /**
  * Generate the choice result chart.
  *
  * Can be displayed either in the vertical or horizontal position.
  *
  * @param stdClass $choices Choices responses object.
  * @param int $displaylayout The constants DISPLAY_HORIZONTAL_LAYOUT or DISPLAY_VERTICAL_LAYOUT.
  * @return string the rendered chart.
  */
 public function display_publish_anonymous($choices, $displaylayout)
 {
     global $OUTPUT;
     $count = 0;
     $data = [];
     $numberofuser = 0;
     $percentageamount = 0;
     foreach ($choices->options as $optionid => $option) {
         if (!empty($option->user)) {
             $numberofuser = count($option->user);
         }
         if ($choices->numberofuser > 0) {
             $percentageamount = (double) $numberofuser / (double) $choices->numberofuser * 100.0;
         }
         $data['labels'][$count] = $option->text;
         $data['series'][$count] = $numberofuser;
         $data['series_labels'][$count] = $numberofuser . ' (' . format_float($percentageamount, 1) . '%)';
         $count++;
         $numberofuser = 0;
     }
     $chart = new \core\chart_bar();
     if ($displaylayout == DISPLAY_HORIZONTAL_LAYOUT) {
         $chart->set_horizontal(true);
     }
     $series = new \core\chart_series(format_string(get_string("responses", "choice")), $data['series']);
     $series->set_labels($data['series_labels']);
     $chart->add_series($series);
     $chart->set_labels($data['labels']);
     $yaxis = $chart->get_yaxis(0, true);
     $yaxis->set_stepsize(max(1, round(max($data['series']) / 10)));
     return $OUTPUT->render($chart);
 }
Ejemplo n.º 7
0
$chart5->add_series($sales);
$chart5->add_series($expenses);
$chart5->set_labels($labels);
$chart6 = new \core\chart_bar();
$chart6->set_title('HORIZONTAL BAR CHART');
$chart6->set_horizontal(true);
$chart6->add_series($sales);
$chart6->add_series($expenses);
$chart6->set_labels($labels);
$chart7 = new \core\chart_bar();
$chart7->set_title('STACKED BAR CHART');
$chart7->set_stacked(true);
$chart7->add_series($sales);
$chart7->add_series($expenses);
$chart7->set_labels($labels);
$chart8 = new \core\chart_bar();
$chart8->set_title('BAR CHART COMBINED WITH LINE CHART');
$expensesline = new \core\chart_series('Expenses', [400, 460, 1120, 540]);
$expensesline->set_type(\core\chart_series::TYPE_LINE);
$chart8->add_series($expensesline);
$chart8->add_series($sales);
$chart8->set_labels($labels);
echo $OUTPUT->render($chart);
echo $OUTPUT->render($chart2);
echo $OUTPUT->render($chart3);
echo $OUTPUT->render($chart4);
echo $OUTPUT->render($chart5);
echo $OUTPUT->render($chart6);
echo $OUTPUT->render($chart7);
echo $OUTPUT->render($chart8);
echo $OUTPUT->footer();