Skip to content

Commit 88d35cc

Browse files
committed
Activities Course overview integration.
1 parent 49ed52e commit 88d35cc

File tree

8 files changed

+587
-193
lines changed

8 files changed

+587
-193
lines changed

classes/courseformat/overview.php

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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 core\url;
20+
use core\activity_dates;
21+
use mod_questionnaire\manager;
22+
use core\output\pix_icon;
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+
* Class overview
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_extra_overview_items(): array {
59+
if (!has_capability('mod/questionnaire:viewsingleresponse', $this->context)) {
60+
return [
61+
'responded' => $this->get_extra_status_for_user(),
62+
];
63+
}
64+
65+
return [
66+
'studentwhoresponded' => $this->get_extra_studentwhoresponded_overview(),
67+
];
68+
}
69+
70+
#[\Override]
71+
public function get_due_date_overview(): ?overviewitem {
72+
global $USER;
73+
74+
$dates = activity_dates::get_dates_for_module($this->cm, $USER->id);
75+
76+
$closedate = null;
77+
foreach ($dates as $date) {
78+
if ($date['dataid'] === 'timeclose') {
79+
$closedate = $date['timestamp'];
80+
break;
81+
}
82+
}
83+
if (empty($closedate)) {
84+
return new overviewitem(
85+
name: get_string('duedate', 'questionnaire'),
86+
value: null,
87+
content: '-',
88+
);
89+
}
90+
91+
$content = humandate::create_from_timestamp($closedate);
92+
93+
return new overviewitem(
94+
name: get_string('duedate', 'questionnaire'),
95+
value: $closedate,
96+
content: $content,
97+
);
98+
}
99+
100+
#[\Override]
101+
public function get_actions_overview(): ?overviewitem {
102+
if (!has_capability('mod/questionnaire:viewsingleresponse', $this->context)) {
103+
return null;
104+
}
105+
106+
$content = new action_link(
107+
url: new url('/mod/questionnaire/view.php', ['id' => $this->cm->id]),
108+
text: get_string('view', 'core'),
109+
attributes: ['class' => button::BODY_OUTLINE->classes()],
110+
);
111+
112+
return new overviewitem(
113+
name: get_string('actions'),
114+
value: get_string('view'),
115+
content: $content,
116+
textalign: text_align::CENTER,
117+
);
118+
}
119+
120+
/**
121+
* Get the response status overview item.
122+
*
123+
* @return overviewitem|null An overview item or null for teachers.
124+
*/
125+
private function get_extra_status_for_user(): ?overviewitem {
126+
if (has_capability('mod/choice:readresponses', $this->cm->context)) {
127+
return null;
128+
}
129+
130+
$status = $this->manager->has_answered();
131+
$statustext = get_string('notanswered', 'choice');
132+
if ($status) {
133+
$statustext = get_string('answered', 'choice');
134+
}
135+
$submittedstatuscontent = "-";
136+
if ($status) {
137+
$submittedstatuscontent = new pix_icon(
138+
pix: 'i/checkedcircle',
139+
alt: $statustext,
140+
component: 'core',
141+
attributes: ['class' => 'text-success'],
142+
);
143+
}
144+
return new overviewitem(
145+
name: get_string('responded', 'choice'),
146+
value: $status,
147+
content: $submittedstatuscontent,
148+
textalign: text_align::CENTER,
149+
);
150+
}
151+
152+
/**
153+
* Get the students who responded item.
154+
*
155+
* @return overviewitem The overview item.
156+
*/
157+
private function get_extra_studentwhoresponded_overview(): overviewitem {
158+
159+
$groupids = array_keys(\core_courseformat\activityoverviewbase::get_groups_for_filtering());
160+
$studentwhoresponded = $this->manager->count_all_users_answered($groupids);
161+
162+
return new overviewitem(
163+
name: get_string('studentwhoresponded', 'mod_questionnaire'),
164+
value: $studentwhoresponded,
165+
content: $studentwhoresponded,
166+
textalign: text_align::END,
167+
);
168+
}
169+
170+
}

classes/dates.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
/**
40+
* Returns a list of important dates in mod_questionnaire
41+
*
42+
* @return array
43+
*/
44+
protected function get_dates(): array {
45+
$timeopen = $this->cm->customdata['timeopen'] ?? null;
46+
$timeclose = $this->cm->customdata['timeclose'] ?? null;
47+
$now = time();
48+
$dates = [];
49+
if ($timeopen) {
50+
$openlabelid = $timeopen > $now ? 'activitydate:opens' : 'activitydate:opened';
51+
$dates[] = [
52+
'dataid' => 'timeopen',
53+
'label' => get_string($openlabelid, 'course'),
54+
'timestamp' => (int) $timeopen,
55+
];
56+
}
57+
58+
if ($timeclose) {
59+
$closelabelid = $timeclose > $now ? 'activitydate:closes' : 'activitydate:closed';
60+
$dates[] = [
61+
'dataid' => 'timeclose',
62+
'label' => get_string($closelabelid, 'course'),
63+
'timestamp' => (int) $timeclose,
64+
];
65+
}
66+
67+
return $dates;
68+
}
69+
}

0 commit comments

Comments
 (0)