Skip to content

Commit 0316f3f

Browse files
committed
Activities Course overview integration.
1 parent 4b90bf2 commit 0316f3f

File tree

8 files changed

+627
-205
lines changed

8 files changed

+627
-205
lines changed

classes/courseformat/overview.php

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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.
13+
//
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/>.
16+
17+
namespace mod_questionnaire\courseformat;
18+
19+
use cm_info;
20+
use core\output\pix_icon;
21+
use mod_questionnaire\manager;
22+
use core\activity_dates;
23+
use core\output\action_link;
24+
use core_calendar\output\humandate;
25+
use core\output\local\properties\button;
26+
use core\output\local\properties\text_align;
27+
use core_courseformat\local\overview\overviewitem;
28+
29+
/**
30+
* Questionnaire overview integration.
31+
*
32+
* @package mod_questionnaire
33+
* @copyright 2025 Luca Bösch <luca.boesch@bfh.ch>
34+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35+
*/
36+
class overview extends \core_courseformat\activityoverviewbase {
37+
/**
38+
* @var manager the questionnaire manager.
39+
*/
40+
private manager $manager;
41+
42+
/**
43+
* Constructor.
44+
*
45+
* @param cm_info $cm the course module instance.
46+
* @param \core\output\renderer_helper $rendererhelper the renderer helper.
47+
*/
48+
public function __construct(
49+
cm_info $cm,
50+
/** @var \core\output\renderer_helper $rendererhelper the renderer helper */
51+
protected readonly \core\output\renderer_helper $rendererhelper,
52+
) {
53+
parent::__construct($cm);
54+
$this->manager = manager::create_from_coursemodule($cm);
55+
}
56+
57+
#[\Override]
58+
public function get_due_date_overview(): ?overviewitem {
59+
global $USER;
60+
61+
$dates = activity_dates::get_dates_for_module($this->cm, $USER->id);
62+
$closedate = null;
63+
foreach ($dates as $date) {
64+
if ($date['dataid'] === 'timeclose') {
65+
$closedate = $date['timestamp'];
66+
break;
67+
}
68+
}
69+
if (empty($closedate)) {
70+
return new overviewitem(
71+
name: get_string('duedate', 'questionnaire'),
72+
value: null,
73+
content: '-',
74+
);
75+
}
76+
77+
$content = humandate::create_from_timestamp($closedate);
78+
79+
return new overviewitem(
80+
name: get_string('duedate', 'questionnaire'),
81+
value: $closedate,
82+
content: $content,
83+
);
84+
}
85+
86+
#[\Override]
87+
public function get_actions_overview(): ?overviewitem {
88+
if (!has_capability('mod/questionnaire:viewsingleresponse', $this->context)) {
89+
return null;
90+
}
91+
92+
$currentanswerscount = $this->manager->count_all_users_answered();
93+
94+
$content = new action_link(
95+
url: new \moodle_url('/mod/questionnaire/report.php', ['instance' => $this->cm->instance]),
96+
text: get_string('view', 'core'),
97+
attributes: ['class' => button::SECONDARY_OUTLINE->classes()],
98+
);
99+
100+
return new overviewitem(
101+
name: get_string('actions'),
102+
value: get_string('viewallxresponses', 'questionnaire', $currentanswerscount),
103+
content: $content,
104+
textalign: text_align::CENTER,
105+
);
106+
}
107+
108+
#[\Override]
109+
public function get_extra_overview_items(): array {
110+
return [
111+
'studentwhoresponded' => $this->get_extra_students_who_responded_overview(),
112+
'responded' => $this->get_extra_status_for_user(),
113+
];
114+
}
115+
116+
/**
117+
* Get the response status overview item.
118+
*
119+
* @return overviewitem|null An overview item or null for teachers.
120+
*/
121+
private function get_extra_status_for_user(): ?overviewitem {
122+
if (has_capability('mod/questionnaire:viewsingleresponse', $this->cm->context)) {
123+
return null;
124+
}
125+
126+
$status = $this->manager->has_answered();
127+
$statustext = get_string('notanswered', 'questionnaire');
128+
if ($status) {
129+
$statustext = get_string('answered', 'questionnaire');
130+
}
131+
$submittedstatuscontent = "-";
132+
if ($status) {
133+
$submittedstatuscontent = new pix_icon(
134+
pix: 'i/checkedcircle',
135+
alt: $statustext,
136+
component: 'core',
137+
attributes: ['class' => 'text-success'],
138+
);
139+
}
140+
return new overviewitem(
141+
name: get_string('responded', 'questionnaire'),
142+
value: $status,
143+
content: $submittedstatuscontent,
144+
textalign: text_align::CENTER,
145+
);
146+
}
147+
148+
/**
149+
* Get the count of student who responded.
150+
*
151+
* @return overviewitem|null An overview item or null if for students.
152+
*/
153+
private function get_extra_students_who_responded_overview(): ?overviewitem {
154+
if (!has_capability('mod/questionnaire:viewsingleresponse', $this->cm->context)) {
155+
return null;
156+
}
157+
158+
if (is_callable([$this, 'get_groups_for_filtering'])) {
159+
$groupids = array_keys($this->get_groups_for_filtering());
160+
} else {
161+
$groupids = [];
162+
}
163+
$studentswhoresponded = $this->manager->count_all_users_answered($groupids);
164+
165+
return new overviewitem(
166+
name: get_string('studentwhoresponded', 'questionnaire'),
167+
value: $studentswhoresponded,
168+
content: $studentswhoresponded,
169+
textalign: text_align::END,
170+
);
171+
}
172+
}

classes/dates.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.
13+
//
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/>.
16+
17+
/**
18+
* Contains the class for fetching the important dates in mod_questionnaire for a given module instance and a user.
19+
*
20+
* @package mod_questionnaire
21+
* @copyright Luca Bösch <luca.boesch@bfh.ch>
22+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23+
*/
24+
25+
declare(strict_types=1);
26+
27+
namespace mod_questionnaire;
28+
29+
use core\activity_dates;
30+
31+
/**
32+
* Class for fetching the important dates in mod_questionnaire for a given module instance and a user.
33+
*
34+
* @copyright Luca Bösch <luca.boesch@bfh.ch>
35+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36+
*/
37+
class dates extends activity_dates {
38+
/**
39+
* Returns a list of important dates in mod_questionnaire
40+
*
41+
* @return array
42+
*/
43+
protected function get_dates(): array {
44+
$timeopen = $this->cm->customdata['timeopen'] ?? null;
45+
$timeclose = $this->cm->customdata['timeclose'] ?? null;
46+
$now = time();
47+
$dates = [];
48+
if ($timeopen) {
49+
$openlabelid = $timeopen > $now ? 'activitydate:opens' : 'activitydate:opened';
50+
$dates[] = [
51+
'dataid' => 'timeopen',
52+
'label' => get_string($openlabelid, 'course'),
53+
'timestamp' => (int) $timeopen,
54+
];
55+
}
56+
57+
if ($timeclose) {
58+
$closelabelid = $timeclose > $now ? 'activitydate:closes' : 'activitydate:closed';
59+
$dates[] = [
60+
'dataid' => 'timeclose',
61+
'label' => get_string($closelabelid, 'course'),
62+
'timestamp' => (int) $timeclose,
63+
];
64+
}
65+
66+
return $dates;
67+
}
68+
}

0 commit comments

Comments
 (0)