MDL-78744 completion: Split activity_information in completion and dates

Deprecate core_course\output\activity_information class and move its code to two
new classes: core_course\output\activity_completion and core_course\output\activity_dates.
In this process refactor activity_completion improving readability and and also deprecate
renderer_base::activity_information() and core_course_renderer::render_activity_information().
This commit is contained in:
Mikel Martín
2023-09-04 11:37:20 +02:00
parent 206c3a66e7
commit bd7cc81be6
11 changed files with 297 additions and 33 deletions
@@ -0,0 +1,156 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_course\output;
use cm_info;
use core_availability\info;
use core_completion\cm_completion_details;
use core_user;
use core_user\fields;
use renderable;
use renderer_base;
use stdClass;
use templatable;
/**
* The activity completion renderable class.
*
* @package core_course
* @copyright 2023 Mikel Martín <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activity_completion implements renderable, templatable {
/**
* Constructor.
*
* @param cm_info $cminfo The course module information.
* @param cm_completion_details $cmcompletion The course module information.
*/
public function __construct(
protected cm_info $cminfo,
protected cm_completion_details $cmcompletion
) {
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output Renderer base.
* @return stdClass
*/
public function export_for_template(renderer_base $output): stdClass {
global $CFG;
$overallcompletion = $this->cmcompletion->get_overall_completion();
$overrideby = $this->get_overrideby();
$course = $this->cminfo->get_course();
// Whether the completion of this activity controls the availability of other activities/sections in the course.
// An activity with manual completion tracking which is used to enable access to other activities/sections in
// the course needs to refresh the page after having its completion state toggled. This withavailability flag will enable
// this functionality on the course homepage. Otherwise, the completion toggling will just happen normally via ajax.
if ($this->cmcompletion->has_completion() && $this->cmcompletion->is_manual()) {
$withavailability = !empty($CFG->enableavailability) && info::completion_value_used($course, $this->cminfo->id);
}
return (object) [
'cmid' => $this->cminfo->id,
'activityname' => $this->cminfo->get_formatted_name(),
'uservisible' => $this->cminfo->uservisible,
'hascompletion' => $this->cmcompletion->has_completion(),
'isautomatic' => $this->cmcompletion->is_automatic(),
'ismanual' => $this->cmcompletion->is_manual(),
'showmanualcompletion' => $this->cmcompletion->show_manual_completion(),
'istrackeduser' => $this->cmcompletion->is_tracked_user(),
'overallcomplete' => $overallcompletion == COMPLETION_COMPLETE,
'overallincomplete' => $overallcompletion == COMPLETION_INCOMPLETE,
'withavailability' => $withavailability ?? false,
'overrideby' => $overrideby,
'completiondetails' => $this->get_completion_details($overrideby),
'accessibledescription' => $this->get_accessible_description($overrideby, $overallcompletion),
];
}
/**
* Returns the name of the user overriding the completion condition, if available.
*
* @return string
*/
private function get_overrideby(): string {
$overrideby = $this->cmcompletion->overridden_by();
if (!empty($overrideby)) {
$userfields = fields::for_name();
$overridebyrecord = core_user::get_user($overrideby, 'id ' . $userfields->get_sql()->selects, MUST_EXIST);
return fullname($overridebyrecord);
}
return '';
}
/**
* Returns automatic completion details
*
* @param string $overrideby The name of the user overriding the completion condition, if available.
* @return array
*/
private function get_completion_details($overrideby): array {
$details = [];
foreach ($this->cmcompletion->get_details() as $key => $detail) {
$detail->key = $key;
$detail->statuscomplete = in_array($detail->status, [COMPLETION_COMPLETE, COMPLETION_COMPLETE_PASS]);
$detail->statuscompletefail = $detail->status == COMPLETION_COMPLETE_FAIL;
// This is not used by core themes but may be needed in custom themes.
$detail->statuscompletepass = $detail->status == COMPLETION_COMPLETE_PASS;
$detail->statusincomplete = $detail->status == COMPLETION_INCOMPLETE;
// Add an accessible description to be used for title and aria-label attributes for overridden completion details.
if ($overrideby) {
$setbydata = (object)[
'condition' => $detail->description,
'setby' => $overrideby,
];
$overridestatus = $detail->statuscomplete ? 'done' : 'todo';
$detail->accessibledescription = get_string('completion_setby:auto:' . $overridestatus, 'course', $setbydata);
}
unset($detail->status);
$details[] = $detail;
}
return $details;
}
/**
* Returns the accessible description for manual completions with overridden completion state.
*
* @param string $overrideby The name of the user overriding the completion condition, if available.
* @param int $overallcompletion The overall completion state of the activity.
* @return string
*/
private function get_accessible_description($overrideby, $overallcompletion): string {
if ($this->cmcompletion->is_manual() && $overrideby) {
$setbydata = (object)[
'activityname' => $this->cminfo->get_formatted_name(),
'setby' => $overrideby,
];
$isoverallcompleted = $overallcompletion == COMPLETION_COMPLETE;
$setbylangkey = $isoverallcompleted ? 'completion_setby:manual:done' : 'completion_setby:manual:markdone';
return get_string($setbylangkey, 'course', $setbydata);
}
return '';
}
}
+72
View File
@@ -0,0 +1,72 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
namespace core_course\output;
use renderable;
use renderer_base;
use stdClass;
use templatable;
/**
* The activity dates renderable class.
*
* @package core_course
* @copyright 2023 Mikel Martín <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class activity_dates implements renderable, templatable {
/**
* Constructor.
*
* @param array $activitydates The activity dates.
*/
public function __construct(
protected array $activitydates
) {
}
/**
* Export this data so it can be used as the context for a mustache template.
*
* @param renderer_base $output Renderer base.
* @return stdClass
*/
public function export_for_template(renderer_base $output): stdClass {
$activitydates = [];
foreach ($this->activitydates as $date) {
if (empty($date['relativeto'])) {
$date['datestring'] = userdate($date['timestamp'], get_string('strftimedaydatetime', 'core_langconfig'));
} else {
$diffstr = get_time_interval_string($date['timestamp'], $date['relativeto']);
if ($date['timestamp'] >= $date['relativeto']) {
$date['datestring'] = get_string('relativedatessubmissionduedateafter', 'core_course',
['datediffstr' => $diffstr]);
} else {
$date['datestring'] = get_string('relativedatessubmissionduedatebefore', 'core_course',
['datediffstr' => $diffstr]);
}
}
$activitydates[] = $date;
}
return (object) [
'hasdates' => !empty($this->activitydates),
'activitydates' => $activitydates,
];
}
}
@@ -41,6 +41,9 @@ use templatable;
/**
* The activity information renderable class.
*
* @deprecated since Moodle 4.3 MDL-78744
* @todo MDL-78926 This class will be deleted in Moodle 4.7
*
* @package core_course
* @copyright 2021 Jun Pataleta <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@@ -59,11 +62,14 @@ class activity_information implements renderable, templatable {
/**
* Constructor.
*
* @deprecated since Moodle 4.3
*
* @param cm_info $cminfo The course module information.
* @param cm_completion_details $cmcompletion The course module information.
* @param array $activitydates The activity dates.
*/
public function __construct(cm_info $cminfo, cm_completion_details $cmcompletion, array $activitydates) {
debugging('activity_information class is deprecated. Use activity_completion and activity_dates instead.', DEBUG_DEVELOPER);
$this->cminfo = $cminfo;
$this->cmcompletion = $cmcompletion;
$this->activitydates = $activitydates;
@@ -72,10 +78,14 @@ class activity_information implements renderable, templatable {
/**
* Export this data so it can be used as the context for a mustache template.
*
* @deprecated since Moodle 4.3
*
* @param renderer_base $output Renderer base.
* @return stdClass
*/
public function export_for_template(renderer_base $output): stdClass {
debugging('activity_information class is deprecated. Use activity_completion and activity_dates instead.', DEBUG_DEVELOPER);
$data = $this->build_completion_data();
$data->cmid = $this->cminfo->id;
@@ -120,11 +120,9 @@ class content_notification_task extends adhoc_task {
}
$eventdata->customdata = $eventcustomdata;
$completion = \core_completion\cm_completion_details::get_instance($cm, $user->id);
$activitydates = \core\activity_dates::get_dates_for_module($cm, $user->id);
if (!empty($activitydates)) {
$activityinfo = new \core_course\output\activity_information($cm, $completion, $activitydates);
$data = $activityinfo->export_for_template($OUTPUT);
$data = (new \core_course\output\activity_dates($activitydates))->export_for_template($OUTPUT);
foreach ($data->activitydates as $date) {
$eventdata->fullmessagehtml .= \html_writer::div($date['label'] . ' ' . $date['datestring']);
}