MDL-63044 block_timeline: add timeline block
[moodle.git] / blocks / timeline / classes / output / main.php
blobb5e96097b2232df457cb1a9a4f0aaa46f470de5f
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Class containing data for timeline block.
20 * @package block_timeline
21 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 namespace block_timeline\output;
25 defined('MOODLE_INTERNAL') || die();
27 use renderable;
28 use renderer_base;
29 use templatable;
30 use core_course\external\course_summary_exporter;
32 require_once($CFG->dirroot . '/course/lib.php');
33 require_once($CFG->libdir . '/completionlib.php');
35 /**
36 * Class containing data for timeline block.
38 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class main implements renderable, templatable {
43 /** Number of courses to load per page */
44 const COURSES_PER_PAGE = 2;
46 /**
47 * Export this data so it can be used as the context for a mustache template.
49 * @param \renderer_base $output
50 * @return stdClass
52 public function export_for_template(renderer_base $output) {
54 $nocoursesurl = $output->image_url('courses', 'block_timeline')->out();
55 $noeventsurl = $output->image_url('activities', 'block_timeline')->out();
57 $requiredproperties = course_summary_exporter::define_properties();
58 $fields = join(',', array_keys($requiredproperties));
59 $courses = course_get_enrolled_courses_for_logged_in_user(0, 0, null, $fields);
60 list($inprogresscourses, $processedcount) = course_filter_courses_by_timeline_classification(
61 $courses,
62 COURSE_TIMELINE_INPROGRESS,
63 self::COURSES_PER_PAGE
65 $formattedcourses = array_map(function($course) use ($output) {
66 \context_helper::preload_from_record($course);
67 $context = \context_course::instance($course->id);
68 $exporter = new course_summary_exporter($course, ['context' => $context]);
69 return $exporter->export($output);
70 }, $inprogresscourses);
72 return [
73 'midnight' => usergetmidnight(time()),
74 'coursepages' => [$formattedcourses],
75 'urls' => [
76 'nocourses' => $nocoursesurl,
77 'noevents' => $noeventsurl