Skip to content

Commit a266d8a

Browse files
author
David Monllao
committed
Merge branch 'MDL-63117-35' of git://github.com/rezaies/moodle into MOODLE_35_STABLE
2 parents 1d67ef4 + b3da439 commit a266d8a

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

mod/book/lib.php

+18-4
Original file line numberDiff line numberDiff line change
@@ -755,20 +755,34 @@ function mod_book_get_fontawesome_icon_map() {
755755
*
756756
* @param calendar_event $event
757757
* @param \core_calendar\action_factory $factory
758+
* @param int $userid User id to use for all capability checks, etc. Set to 0 for current user (default).
758759
* @return \core_calendar\local\event\entities\action_interface|null
759760
*/
760761
function mod_book_core_calendar_provide_event_action(calendar_event $event,
761-
\core_calendar\action_factory $factory) {
762-
$cm = get_fast_modinfo($event->courseid)->instances['book'][$event->instance];
762+
\core_calendar\action_factory $factory,
763+
int $userid = 0) {
764+
global $USER;
765+
766+
if (empty($userid)) {
767+
$userid = $USER->id;
768+
}
769+
770+
$cm = get_fast_modinfo($event->courseid, $userid)->instances['book'][$event->instance];
771+
772+
if (!$cm->uservisible) {
773+
// The module is not visible to the user for any reason.
774+
return null;
775+
}
776+
763777
$context = context_module::instance($cm->id);
764778

765-
if (!has_capability('mod/book:read', $context)) {
779+
if (!has_capability('mod/book:read', $context, $userid)) {
766780
return null;
767781
}
768782

769783
$completion = new \completion_info($cm->get_course());
770784

771-
$completiondata = $completion->get_data($cm, false);
785+
$completiondata = $completion->get_data($cm, false, $userid);
772786

773787
if ($completiondata->completionstate != COMPLETION_INCOMPLETE) {
774788
return null;

mod/book/tests/lib_test.php

+91
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,63 @@ public function test_book_core_calendar_provide_event_action() {
160160
$this->assertTrue($actionevent->is_actionable());
161161
}
162162

163+
public function test_book_core_calendar_provide_event_action_in_hidden_section() {
164+
// Create the activity.
165+
$course = $this->getDataGenerator()->create_course();
166+
$book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
167+
168+
// Enrol a student in the course.
169+
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
170+
171+
// Create a calendar event.
172+
$event = $this->create_action_event($course->id, $book->id,
173+
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
174+
175+
// Set sections 0 as hidden.
176+
set_section_visible($course->id, 0, 0);
177+
178+
// Now, log out.
179+
$this->setUser();
180+
181+
// Create an action factory.
182+
$factory = new \core_calendar\action_factory();
183+
184+
// Decorate action event for the student.
185+
$actionevent = mod_book_core_calendar_provide_event_action($event, $factory, $student->id);
186+
187+
// Confirm the event is not shown at all.
188+
$this->assertNull($actionevent);
189+
}
190+
191+
public function test_book_core_calendar_provide_event_action_for_user() {
192+
// Create the activity.
193+
$course = $this->getDataGenerator()->create_course();
194+
$book = $this->getDataGenerator()->create_module('book', array('course' => $course->id));
195+
196+
// Enrol a student in the course.
197+
$student = $this->getDataGenerator()->create_and_enrol($course, 'student');
198+
199+
// Create a calendar event.
200+
$event = $this->create_action_event($course->id, $book->id,
201+
\core_completion\api::COMPLETION_EVENT_TYPE_DATE_COMPLETION_EXPECTED);
202+
203+
// Now, log out.
204+
$this->setUser();
205+
206+
// Create an action factory.
207+
$factory = new \core_calendar\action_factory();
208+
209+
// Decorate action event for the student.
210+
$actionevent = mod_book_core_calendar_provide_event_action($event, $factory, $student->id);
211+
212+
// Confirm the event was decorated.
213+
$this->assertInstanceOf('\core_calendar\local\event\value_objects\action', $actionevent);
214+
$this->assertEquals(get_string('view'), $actionevent->get_name());
215+
$this->assertInstanceOf('moodle_url', $actionevent->get_url());
216+
$this->assertEquals(1, $actionevent->get_item_count());
217+
$this->assertTrue($actionevent->is_actionable());
218+
}
219+
163220
public function test_book_core_calendar_provide_event_action_as_non_user() {
164221
global $CFG;
165222

@@ -216,6 +273,40 @@ public function test_book_core_calendar_provide_event_action_already_completed()
216273
$this->assertNull($actionevent);
217274
}
218275

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

0 commit comments

Comments
 (0)