Skip to content

Commit cb5fb29

Browse files
author
David Monllao
committed
Merge branch 'MDL-63138-34' of git://github.com/rezaies/moodle into MOODLE_34_STABLE
2 parents 693959a + ae9af4e commit cb5fb29

File tree

2 files changed

+140
-3
lines changed

2 files changed

+140
-3
lines changed

mod/folder/lib.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -788,15 +788,28 @@ function folder_check_updates_since(cm_info $cm, $from, $filter = array()) {
788788
*
789789
* @param calendar_event $event
790790
* @param \core_calendar\action_factory $factory
791+
* @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
791792
* @return \core_calendar\local\event\entities\action_interface|null
792793
*/
793794
function mod_folder_core_calendar_provide_event_action(calendar_event $event,
794-
\core_calendar\action_factory $factory) {
795-
$cm = get_fast_modinfo($event->courseid)->instances['folder'][$event->instance];
795+
\core_calendar\action_factory $factory,
796+
int $userid = 0) {
797+
global $USER;
798+
799+
if (!$userid) {
800+
$userid = $USER->id;
801+
}
802+
803+
$cm = get_fast_modinfo($event->courseid, $userid)->instances['folder'][$event->instance];
804+
805+
if (!$cm->uservisible) {
806+
// The module is not visible to the user for any reason.
807+
return null;
808+
}
796809

797810
$completion = new \completion_info($cm->get_course());
798811

799-
$completiondata = $completion->get_data($cm, false);
812+
$completiondata = $completion->get_data($cm, false, $userid);
800813

801814
if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
802815
return null;

mod/folder/tests/lib_test.php

+124
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,91 @@ public function test_folder_core_calendar_provide_event_action() {
118118
$this->assertTrue($actionevent->is_actionable());
119119
}
120120

121+
public function test_folder_core_calendar_provide_event_action_for_non_user() {
122+
global $CFG;
123+
124+
// Create a course.
125+
$course = $this->getDataGenerator()->create_course();
126+
127+
// Create the activity.
128+
$folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id));
129+
130+
// Create a calendar event.
131+
$event = $this->create_action_event($course->id, $folder->id,
132+
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
133+
134+
// Now, log out.
135+
$CFG->forcelogin = true; // We don't want to be logged in as guest, as guest users might still have some capabilities.
136+
$this->setUser();
137+
138+
// Create an action factory.
139+
$factory = new \core_calendar\action_factory();
140+
141+
// Decorate action event.
142+
$actionevent = mod_folder_core_calendar_provide_event_action($event, $factory);
143+
144+
// Confirm the event is not shown at all.
145+
$this->assertNull($actionevent);
146+
}
147+
148+
public function test_folder_core_calendar_provide_event_action_in_hidden_section() {
149+
// Create a course.
150+
$course = $this->getDataGenerator()->create_course();
151+
152+
// Create a student.
153+
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
154+
155+
// Create the activity.
156+
$folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id));
157+
158+
// Create a calendar event.
159+
$event = $this->create_action_event($course->id, $folder->id,
160+
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
161+
162+
// Set sections 0 as hidden.
163+
set_section_visible($course->id, 0, 0);
164+
165+
// Create an action factory.
166+
$factory = new \core_calendar\action_factory();
167+
168+
// Decorate action event.
169+
$actionevent = mod_folder_core_calendar_provide_event_action($event, $factory, $student->id);
170+
171+
// Confirm the event is not shown at all.
172+
$this->assertNull($actionevent);
173+
}
174+
175+
public function test_folder_core_calendar_provide_event_action_for_user() {
176+
// Create a course.
177+
$course = $this->getDataGenerator()->create_course();
178+
179+
// Create a student.
180+
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
181+
182+
// Create the activity.
183+
$folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id));
184+
185+
// Create a calendar event.
186+
$event = $this->create_action_event($course->id, $folder->id,
187+
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
188+
189+
// Now, log out.
190+
$this->setUser();
191+
192+
// Create an action factory.
193+
$factory = new \core_calendar\action_factory();
194+
195+
// Decorate action event for the student.
196+
$actionevent = mod_folder_core_calendar_provide_event_action($event, $factory, $student->id);
197+
198+
// Confirm the event was decorated.
199+
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
200+
$this->assertEquals(get_string('view'), $actionevent->get_name());
201+
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
202+
$this->assertEquals(1, $actionevent->get_item_count());
203+
$this->assertTrue($actionevent->is_actionable());
204+
}
205+
121206
public function test_folder_core_calendar_provide_event_action_already_completed() {
122207
global $CFG;
123208

@@ -149,6 +234,45 @@ public function test_folder_core_calendar_provide_event_action_already_completed
149234
$this->assertNull($actionevent);
150235
}
151236

237+
public function test_folder_core_calendar_provide_event_action_already_completed_for_user() {
238+
global $CFG;
239+
240+
$CFG->enablecompletion = 1;
241+
242+
// Create a course.
243+
$course = $this->getDataGenerator()->create_course(array('enablecompletion' => 1));
244+
245+
// Create a student.
246+
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
247+
248+
// Create the activity.
249+
$folder = $this->getDataGenerator()->create_module('folder', array('course' => $course->id),
250+
array('completion' => 2, 'completionview' => 1, 'completionexpected' => time() + DAYSECS));
251+
252+
// Get some additional data.
253+
$cm = get_coursemodule_from_instance('folder', $folder->id);
254+
255+
// Create a calendar event.
256+
$event = $this->create_action_event($course->id, $folder->id,
257+
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
258+
259+
// Mark the activity as completed for the student.
260+
$completion = new completion_info($course);
261+
$completion->set_module_viewed($cm, $student->id);
262+
263+
// Now, log out.
264+
$this->setUser();
265+
266+
// Create an action factory.
267+
$factory = new \core_calendar\action_factory();
268+
269+
// Decorate action event for the student.
270+
$actionevent = mod_folder_core_calendar_provide_event_action($event, $factory, $student->id);
271+
272+
// Ensure result was null.
273+
$this->assertNull($actionevent);
274+
}
275+
152276
/**
153277
* Creates an action event.
154278
*

0 commit comments

Comments
 (0)