Skip to content

Commit 4453613

Browse files
author
David Monllao
committed
Merge branch 'MDL-63140-master' of git://github.com/rezaies/moodle
2 parents 25561b3 + 50f3472 commit 4453613

File tree

2 files changed

+150
-3
lines changed

2 files changed

+150
-3
lines changed

mod/glossary/lib.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4226,15 +4226,28 @@ function mod_glossary_get_fontawesome_icon_map() {
42264226
*
42274227
* @param calendar_event $event
42284228
* @param \core_calendar\action_factory $factory
4229+
* @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
42294230
* @return \core_calendar\local\event\entities\action_interface|null
42304231
*/
42314232
function mod_glossary_core_calendar_provide_event_action(calendar_event $event,
4232-
\core_calendar\action_factory $factory) {
4233-
$cm = get_fast_modinfo($event->courseid)->instances['glossary'][$event->instance];
4233+
\core_calendar\action_factory $factory,
4234+
int $userid = 0) {
4235+
global $USER;
4236+
4237+
if (!$userid) {
4238+
$userid = $USER->id;
4239+
}
4240+
4241+
$cm = get_fast_modinfo($event->courseid, $userid)->instances['glossary'][$event->instance];
4242+
4243+
if (!$cm->uservisible) {
4244+
// The module is not visible to the user for any reason.
4245+
return null;
4246+
}
42344247

42354248
$completion = new \completion_info($cm->get_course());
42364249

4237-
$completiondata = $completion->get_data($cm, false);
4250+
$completiondata = $completion->get_data($cm, false, $userid);
42384251

42394252
if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
42404253
return null;

mod/glossary/tests/lib_test.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,101 @@ public function test_glossary_core_calendar_provide_event_action() {
141141
$this->assertTrue($actionevent->is_actionable());
142142
}
143143

144+
public function test_glossary_core_calendar_provide_event_action_as_non_user() {
145+
global $CFG;
146+
147+
$this->resetAfterTest();
148+
$this->setAdminUser();
149+
150+
// Create the activity.
151+
$course = $this->getDataGenerator()->create_course();
152+
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
153+
154+
// Create a calendar event.
155+
$event = $this->create_action_event($course->id, $glossary->id,
156+
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
157+
158+
// Now log out.
159+
$CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
160+
$this->setUser();
161+
162+
// Create an action factory.
163+
$factory = new \core_calendar\action_factory();
164+
165+
// Decorate action event for the student.
166+
$actionevent = mod_glossary_core_calendar_provide_event_action($event, $factory);
167+
168+
// Confirm the event is not shown at all.
169+
$this->assertNull($actionevent);
170+
}
171+
172+
public function test_glossary_core_calendar_provide_event_action_for_user() {
173+
global $CFG;
174+
175+
$this->resetAfterTest();
176+
$this->setAdminUser();
177+
178+
// Create a course.
179+
$course = $this->getDataGenerator()->create_course();
180+
181+
// Create a student.
182+
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
183+
184+
// Create the activity.
185+
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
186+
187+
// Create a calendar event.
188+
$event = $this->create_action_event($course->id, $glossary->id,
189+
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
190+
191+
// Now log out.
192+
$CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
193+
$this->setUser();
194+
195+
// Create an action factory.
196+
$factory = new \core_calendar\action_factory();
197+
198+
// Decorate action event for the student.
199+
$actionevent = mod_glossary_core_calendar_provide_event_action($event, $factory, $student->id);
200+
201+
// Confirm the event was decorated.
202+
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
203+
$this->assertEquals(get_string('view'), $actionevent->get_name());
204+
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
205+
$this->assertEquals(1, $actionevent->get_item_count());
206+
$this->assertTrue($actionevent->is_actionable());
207+
}
208+
209+
public function test_glossary_core_calendar_provide_event_action_in_hidden_section() {
210+
$this->resetAfterTest();
211+
$this->setAdminUser();
212+
213+
// Create a course.
214+
$course = $this->getDataGenerator()->create_course();
215+
216+
// Create a student.
217+
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
218+
219+
// Create the activity.
220+
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id));
221+
222+
// Create a calendar event.
223+
$event = $this->create_action_event($course->id, $glossary->id,
224+
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
225+
226+
// Set sections 0 as hidden.
227+
set_section_visible($course->id, 0, 0);
228+
229+
// Create an action factory.
230+
$factory = new \core_calendar\action_factory();
231+
232+
// Decorate action event for the student.
233+
$actionevent = mod_glossary_core_calendar_provide_event_action($event, $factory, $student->id);
234+
235+
// Confirm the event is not shown at all.
236+
$this->assertNull($actionevent);
237+
}
238+
144239
public function test_glossary_core_calendar_provide_event_action_already_completed() {
145240
global $CFG;
146241

@@ -175,6 +270,45 @@ public function test_glossary_core_calendar_provide_event_action_already_complet
175270
$this->assertNull($actionevent);
176271
}
177272

273+
public function test_glossary_core_calendar_provide_event_action_already_completed_for_user() {
274+
global $CFG;
275+
276+
$this->resetAfterTest();
277+
$this->setAdminUser();
278+
279+
$CFG->enablecompletion = 1;
280+
281+
// Create a course.
282+
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
283+
284+
// Create a student.
285+
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
286+
287+
// Create the activity.
288+
$glossary = $this->getDataGenerator()->create_module('glossary', array('course' => $course->id),
289+
array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
290+
291+
// Get some additional data.
292+
$cm = get_coursemodule_from_instance('glossary', $glossary->id);
293+
294+
// Create a calendar event.
295+
$event = $this->create_action_event($course->id, $glossary->id,
296+
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
297+
298+
// Mark the activity as completed for the user.
299+
$completion = new completion_info($course);
300+
$completion->set_module_viewed($cm, $student->id);
301+
302+
// Create an action factory.
303+
$factory = new \core_calendar\action_factory();
304+
305+
// Decorate action event.
306+
$actionevent = mod_glossary_core_calendar_provide_event_action($event, $factory, $student->id);
307+
308+
// Ensure result was null.
309+
$this->assertNull($actionevent);
310+
}
311+
178312
/**
179313
* Creates an action event.
180314
*

0 commit comments

Comments
 (0)