Exemplo n.º 1
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);
}
Exemplo n.º 2
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);
 }
Exemplo n.º 3
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;
    }
Exemplo n.º 4
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);
 }