Skip to content

Commit

Permalink
MDL-57501 block_myoverview: add completion progress to renderables
Browse files Browse the repository at this point in the history
Part of MDL-55611 epic.
  • Loading branch information
ryanwyllie authored and Damyon Wiese committed Apr 3, 2017
1 parent cab053f commit 3f0c421
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 23 deletions.
17 changes: 15 additions & 2 deletions blocks/myoverview/classes/output/course_summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ class course_summary implements renderable, templatable {
/** @var array $courses List of courses the user is enrolled in. */
protected $courses = [];

/** @var array $coursesprogress List of progress percentage for each course. */
protected $coursesprogress = [];

/**
* The course_summary constructor.
*
* @param array $courses list of courses.
* @param array $coursesprogress list of courses progress.
*/
public function __construct($courses) {
public function __construct($courses, $coursesprogress) {
$this->courses = $courses;
$this->coursesprogress = $coursesprogress;
}

/**
Expand All @@ -60,7 +65,15 @@ public function export_for_template(renderer_base $output) {
foreach ($this->courses as $courseid => $value) {
$context = \context_course::instance($courseid);
$exporter = new course_summary_exporter($this->courses[$courseid], array('context' => $context));
$data[] = $exporter->export($output);
$exportedcourse = $exporter->export($output);

if (isset($this->coursesprogress[$courseid])) {
$courseprogress = $this->coursesprogress[$courseid];
$exportedcourse->hasprogress = !is_null($courseprogress);
$exportedcourse->progress = $courseprogress;
}

$data[] = $exportedcourse;
}
return $data;
}
Expand Down
54 changes: 36 additions & 18 deletions blocks/myoverview/classes/output/courses_view.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use renderable;
use renderer_base;
use templatable;
use core_course\external\course_summary_exporter;

/**
* Class containing data for courses view in the myoverview block.
*
Expand All @@ -40,13 +42,18 @@ class courses_view implements renderable, templatable {
/** @var array $courses List of courses the user is enrolled in. */
protected $courses = [];

/** @var array $coursesprogress List of progress percentage for each course. */
protected $coursesprogress = [];

/**
* The courses_view constructor.
*
* @param array $courses list of courses.
* @param array $coursesprogress list of courses progress.
*/
public function __construct($courses) {
public function __construct($courses, $coursesprogress) {
$this->courses = $courses;
$this->coursesprogress = $coursesprogress;
}

/**
Expand All @@ -66,27 +73,39 @@ public function export_for_template(renderer_base $output) {
foreach ($this->courses as $course) {
$startdate = $course->startdate;
$enddate = $course->enddate;
$courseid = $course->id;
$context = \context_course::instance($courseid);
$exporter = new course_summary_exporter($course, [
'context' => $context
]);
$exportedcourse = $exporter->export($output);

if (isset($this->coursesprogress[$courseid])) {
$courseprogress = $this->coursesprogress[$courseid];
$exportedcourse->hasprogress = !is_null($courseprogress);
$exportedcourse->progress = $courseprogress;
}

if ($startdate < $today && $enddate < $today) {

$pastpages = floor($coursesbystatus['past'] / $this::COURSES_PER_PAGE);

$coursesview['past']['pages'][$pastpages]['courses'][] = $course;
$coursesview['past']['pages'][$pastpages]['courses'][] = $exportedcourse;
$coursesview['past']['pages'][$pastpages]['active'] = ($pastpages == 0 ? true : false);
$coursesview['past']['pages'][$pastpages]['page'] = $pastpages + 1;
$coursesbystatus['past']++;

} elseif ($startdate <= $today && $enddate >= $today) {
$inprogresspages = floor($coursesbystatus['inprogress'] / $this::COURSES_PER_PAGE);

$coursesview['inprogress']['pages'][$inprogresspages]['courses'][] = $course;
$coursesview['inprogress']['pages'][$inprogresspages]['courses'][] = $exportedcourse;
$coursesview['inprogress']['pages'][$inprogresspages]['active'] = ($inprogresspages == 0 ? true : false);
$coursesview['inprogress']['pages'][$inprogresspages]['page'] = $inprogresspages + 1;
$coursesbystatus['inprogress']++;
} else {
$futurepages = floor($coursesbystatus['future'] / $this::COURSES_PER_PAGE);

$coursesview['future']['pages'][$futurepages]['courses'][] = $course;
$coursesview['future']['pages'][$futurepages]['courses'][] = $exportedcourse;
$coursesview['future']['pages'][$futurepages]['active'] = ($futurepages == 0 ? true : false);
$coursesview['future']['pages'][$futurepages]['page'] = $futurepages + 1;
$coursesbystatus['future']++;
Expand All @@ -96,23 +115,22 @@ public function export_for_template(renderer_base $output) {
// Build courses view paging bar structure.
foreach ($coursesbystatus as $status => $total) {
$quantpages = ceil($total / $this::COURSES_PER_PAGE);
$coursesview[$status]['pagingbar']['pagecount'] = $quantpages;
$coursesview[$status]['pagingbar']['first'] = ['page' => '&laquo;', 'url' => '#'];
$coursesview[$status]['pagingbar']['last'] = ['page' => '&raquo;', 'url' => '#'];
for ($page = 0; $page < $quantpages; $page++) {
$coursesview[$status]['pagingbar']['pages'][$page] = [
'number' => $page+1,
'page' => $page+1,
'url' => '#',
'active' => ($page == 0 ? true : false)
];

if ($quantpages) {
$coursesview[$status]['pagingbar']['pagecount'] = $quantpages;
$coursesview[$status]['pagingbar']['first'] = ['page' => '&laquo;', 'url' => '#'];
$coursesview[$status]['pagingbar']['last'] = ['page' => '&raquo;', 'url' => '#'];
for ($page = 0; $page < $quantpages; $page++) {
$coursesview[$status]['pagingbar']['pages'][$page] = [
'number' => $page+1,
'page' => $page+1,
'url' => '#',
'active' => ($page == 0 ? true : false)
];
}
}
}

// Check if we have any page to be rendered.
$coursesview['haspages'] = ($coursesbystatus['past'] > 0 || $coursesbystatus['inprogress'] > 0 ||
$coursesbystatus['future'] > 0);

return $coursesview;
}
}
20 changes: 17 additions & 3 deletions blocks/myoverview/classes/output/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use renderable;
use renderer_base;
use templatable;
use core_completion\progress;

/**
* Class containing data for my overview block.
*
Expand All @@ -42,9 +44,21 @@ class main implements renderable, templatable {
* @return stdClass
*/
public function export_for_template(renderer_base $output) {
$courses = enrol_get_my_courses('id, shortname, startdate, enddate, summary');
$coursesummary = new course_summary($courses);
$coursesview = new courses_view($courses);
$courses = enrol_get_my_courses('*');
$coursesprogress = [];

foreach ($courses as $course) {
$percentage = progress::get_course_progress_percentage($course);

if (!is_null($percentage)) {
$percentage = floor($percentage);
}

$coursesprogress[$course->id] = $percentage;
}

$coursesummary = new course_summary($courses, $coursesprogress);
$coursesview = new courses_view($courses, $coursesprogress);

return [
'courses' => $coursesummary->export_for_template($output),
Expand Down
2 changes: 2 additions & 0 deletions completion/classes/progress.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

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

require_once($CFG->libdir . '/completionlib.php');

/**
* Class used to return completion progress information.
*
Expand Down

0 comments on commit 3f0c421

Please sign in to comment.