public function get_predictions(\context $context, $skiphidden = true, $page = false, $perpage = 100) {
        global $DB, $USER;

        \core_analytics\manager::check_can_list_insights($context);

        // Filters out previous predictions keeping only the last time range one.
        $sql = "SELECT ap.*
                  FROM {analytics_predictions} ap
                  JOIN (
                    SELECT sampleid, max(rangeindex) AS rangeindex
                      FROM {analytics_predictions}
                     WHERE modelid = :modelidsubap and contextid = :contextidsubap
                    GROUP BY sampleid
                  ) apsub
                  ON ap.sampleid = apsub.sampleid AND ap.rangeindex = apsub.rangeindex
                WHERE ap.modelid = :modelid and ap.contextid = :contextid";

        $params = array('modelid' => $this->model->id, 'contextid' => $context->id,
            'modelidsubap' => $this->model->id, 'contextidsubap' => $context->id);

        if ($skiphidden) {
            $sql .= " AND NOT EXISTS (
              SELECT 1
                FROM {analytics_prediction_actions} apa
               WHERE apa.predictionid = ap.id AND apa.userid = :userid AND (apa.actionname = :fixed OR apa.actionname = :notuseful)
            )";
            $params['userid'] = $USER->id;
            $params['fixed'] = \core_analytics\prediction::ACTION_FIXED;
            $params['notuseful'] = \core_analytics\prediction::ACTION_NOT_USEFUL;
        }

        $sql .= " ORDER BY ap.timecreated DESC";
        if (!$predictions = $DB->get_records_sql($sql, $params)) {
            return array();
        }

        // Get predicted samples' ids.
        $sampleids = array_map(function($prediction) {
            return $prediction->sampleid;
        }, $predictions);

        list($unused, $samplesdata) = $this->get_analyser()->get_samples($sampleids);

        $current = 0;

        if ($page !== false) {
            $offset = $page * $perpage;
            $limit = $offset + $perpage;
        }

        foreach ($predictions as $predictionid => $predictiondata) {

            $sampleid = $predictiondata->sampleid;

            // Filter out predictions which samples are not available anymore.
            if (empty($samplesdata[$sampleid])) {
                unset($predictions[$predictionid]);
                continue;
            }

            // Return paginated dataset - we cannot paginate in the DB because we post filter the list.
            if ($page === false || ($current >= $offset && $current < $limit)) {
                // Replace \stdClass object by \core_analytics\prediction objects.
                $prediction = new \core_analytics\prediction($predictiondata, $samplesdata[$sampleid]);
                $predictions[$predictionid] = $prediction;
            } else {
                unset($predictions[$predictionid]);
            }

            $current++;
        }

        return [$current, $predictions];
    }