$lesson = $DB->get_record('lesson', array('id' => $lessonid), '*', MUST_EXIST);
list($course, $cm) = get_course_and_cm_from_instance($lesson, 'lesson');
- $lesson = new lesson($lesson, $cm);
+ $lesson = new lesson($lesson, $cm, $course);
$lesson->update_effective_access($USER->id);
$context = $lesson->context;
)
);
}
+
+ /**
+ * Describes the parameters for view_lesson.
+ *
+ * @return external_external_function_parameters
+ * @since Moodle 3.3
+ */
+ public static function view_lesson_parameters() {
+ return new external_function_parameters (
+ array(
+ 'lessonid' => new external_value(PARAM_INT, 'lesson instance id'),
+ 'password' => new external_value(PARAM_RAW, 'lesson password', VALUE_DEFAULT, ''),
+ )
+ );
+ }
+
+ /**
+ * Trigger the course module viewed event and update the module completion status.
+ *
+ * @param int $lessonid lesson instance id
+ * @param str $password optional password (the lesson may be protected)
+ * @return array of warnings and status result
+ * @since Moodle 3.3
+ * @throws moodle_exception
+ */
+ public static function view_lesson($lessonid, $password = '') {
+ global $DB;
+
+ $params = array('lessonid' => $lessonid, 'password' => $password);
+ $params = self::validate_parameters(self::view_lesson_parameters(), $params);
+ $warnings = array();
+
+ list($lesson, $course, $cm, $context) = self::validate_lesson($params['lessonid']);
+ self::validate_attempt($lesson, $params);
+
+ $lesson->set_module_viewed();
+
+ $result = array();
+ $result['status'] = true;
+ $result['warnings'] = $warnings;
+ return $result;
+ }
+
+ /**
+ * Describes the view_lesson return value.
+ *
+ * @return external_single_structure
+ * @since Moodle 3.3
+ */
+ public static function view_lesson_returns() {
+ return new external_single_structure(
+ array(
+ 'status' => new external_value(PARAM_BOOL, 'status: true if success'),
+ 'warnings' => new external_warnings(),
+ )
+ );
+ }
+
}
'capabilities' => 'mod/lesson:view',
'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE),
),
+ 'mod_lesson_view_lesson' => array(
+ 'classname' => 'mod_lesson_external',
+ 'methodname' => 'view_lesson',
+ 'description' => 'Trigger the course module viewed event and update the module completion status.',
+ 'type' => 'write',
+ 'capabilities' => 'mod/lesson:view',
+ 'services' => array(MOODLE_OFFICIAL_MOBILE_SERVICE)
+ ),
);
$this->assertTrue($result['canviewreports']);
}
+ /**
+ * Test test_view_lesson invalid id.
+ */
+ public function test_view_lesson_invalid_id() {
+ $this->setExpectedException('moodle_exception');
+ mod_lesson_external::view_lesson(0);
+ }
+
+ /**
+ * Test test_view_lesson user not enrolled.
+ */
+ public function test_view_lesson_user_not_enrolled() {
+ // Test not-enrolled user.
+ $usernotenrolled = self::getDataGenerator()->create_user();
+ $this->setUser($usernotenrolled);
+ $this->setExpectedException('moodle_exception');
+ mod_lesson_external::view_lesson($this->lesson->id);
+ }
+
+ /**
+ * Test test_view_lesson user student.
+ */
+ public function test_view_lesson_user_student() {
+ // Test user with full capabilities.
+ $this->setUser($this->student);
+
+ // Trigger and capture the event.
+ $sink = $this->redirectEvents();
+
+ $result = mod_lesson_external::view_lesson($this->lesson->id);
+ $result = external_api::clean_returnvalue(mod_lesson_external::view_lesson_returns(), $result);
+ $this->assertTrue($result['status']);
+
+ $events = $sink->get_events();
+ $this->assertCount(1, $events);
+ $event = array_shift($events);
+
+ // Checking that the event contains the expected values.
+ $this->assertInstanceOf('\mod_lesson\event\course_module_viewed', $event);
+ $this->assertEquals($this->context, $event->get_context());
+ $moodlelesson = new \moodle_url('/mod/lesson/view.php', array('id' => $this->cm->id));
+ $this->assertEquals($moodlelesson, $event->get_url());
+ $this->assertEventContextNotUsed($event);
+ $this->assertNotEmpty($event->get_name());
+ }
+
+ /**
+ * Test test_view_lesson user missing capabilities.
+ */
+ public function test_view_lesson_user_missing_capabilities() {
+ // Test user with no capabilities.
+ // We need a explicit prohibit since this capability is only defined in authenticated user and guest roles.
+ assign_capability('mod/lesson:view', CAP_PROHIBIT, $this->studentrole->id, $this->context->id);
+ // Empty all the caches that may be affected by this change.
+ accesslib_clear_all_caches_for_unit_testing();
+ course_modinfo::clear_instance_cache();
+
+ $this->setUser($this->student);
+ $this->setExpectedException('moodle_exception');
+ mod_lesson_external::view_lesson($this->lesson->id);
+ }
+
}
defined('MOODLE_INTERNAL') || die();
-$plugin->version = 2016120502; // The current module version (Date: YYYYMMDDXX)
+$plugin->version = 2016120503; // The current module version (Date: YYYYMMDDXX)
$plugin->requires = 2016112900; // Requires this Moodle version
$plugin->component = 'mod_lesson'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 0;