From 14e0e5ddb7255342d1a7360b6e7f7e81aac161b3 Mon Sep 17 00:00:00 2001 From: Angela Baier Date: Thu, 7 Apr 2022 09:46:55 +0200 Subject: [PATCH] EL-1194/dates infotext bug fix --- classes/dates.php | 71 +++++++++++++++++++++++++++++++++++++++++++++++ lib.php | 38 +++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 classes/dates.php diff --git a/classes/dates.php b/classes/dates.php new file mode 100644 index 00000000..16f46a6e --- /dev/null +++ b/classes/dates.php @@ -0,0 +1,71 @@ +. + +/** + * Contains the class for fetching the important dates in mod_questionnaire for a given module instance and a user. + * + * @package mod_questionnaire + * @copyright 2022 University of Vienna + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +declare(strict_types=1); + +namespace mod_questionnaire; + +use core\activity_dates; + +/** + * Class for fetching the important dates in mod_questionnaire for a given module instance and a user. + * + * @copyright 2022 University of Vienna + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class dates extends activity_dates { + + /** + * Returns a list of important dates in mod_questionnaire + * + * @return array + */ + protected function get_dates(): array { + global $DB; + + $timeopen = $this->cm->customdata['opendate'] ?? null; + $timeclose = $this->cm->customdata['closedate'] ?? null; + + $now = time(); + $dates = []; + + if ($timeopen) { + $openlabelid = $timeopen > $now ? 'activitydate:opens' : 'activitydate:opened'; + $dates[] = [ + 'label' => get_string($openlabelid, 'core_course'), + 'timestamp' => (int) $timeopen, + ]; + } + + if ($timeclose) { + $closelabelid = $timeclose > $now ? 'activitydate:closes' : 'activitydate:closed'; + $dates[] = [ + 'label' => get_string($closelabelid, 'core_course'), + 'timestamp' => (int) $timeclose, + ]; + } + + return $dates; + } +} diff --git a/lib.php b/lib.php index 46f15244..fbc519f1 100644 --- a/lib.php +++ b/lib.php @@ -1276,3 +1276,41 @@ function mod_questionnaire_coursemodule_edit_post_actions($data, $course) { return $data; } + +/** + * Add a get_coursemodule_info function in case any questionnaire type wants to add 'extra' information + * for the course (see resource). + * + * Given a course_module object, this function returns any "extra" information that may be needed + * when printing this activity in a course listing. See get_array_of_activities() in course/lib.php. + * + * @param stdClass $coursemodule The coursemodule object (record). + * @return cached_cm_info An object on information that the courses + * will know about (most noticeably, an icon). + */ +function questionnaire_get_coursemodule_info($coursemodule) { + global $DB; + + $dbparams = array('id'=>$coursemodule->instance); + if (! $questionnaire = $DB->get_record('questionnaire', $dbparams)) { + return false; + } + + $result = new cached_cm_info(); + $result->name = $questionnaire->name; + + if ($coursemodule->showdescription) { + // Convert intro to html. Do not filter cached version, filters run at display time. + $result->content = format_module_intro('questionnaire', $questionnaire, $coursemodule->id, false); + } + + // Populate some other values that can be used in calendar or on dashboard. + if ($questionnaire->opendate) { + $result->customdata['opendate'] = $questionnaire->opendate; + } + if ($questionnaire->closedate) { + $result->customdata['closedate'] = $questionnaire->closedate; + } + + return $result; +}