mirror of
https://github.com/moodle/moodle.git
synced 2025-04-25 10:26:17 +02:00
MDL-57633 mod_lesson: Move to function the lesson availability checks
This commit is just about moving code and updating the lesson class to manage the context and cm.
This commit is contained in:
parent
e7059ec5e2
commit
4a3391b694
@ -1010,6 +1010,32 @@ class lesson extends lesson_base {
|
||||
*/
|
||||
protected $loadedallpages = false;
|
||||
|
||||
/**
|
||||
* Course module object gets set and retrieved by directly calling <code>$lesson->cm;</code>
|
||||
* @see get_cm()
|
||||
* @var stdClass
|
||||
*/
|
||||
protected $cm = null;
|
||||
|
||||
/**
|
||||
* Context object gets set and retrieved by directly calling <code>$lesson->context;</code>
|
||||
* @see get_context()
|
||||
* @var stdClass
|
||||
*/
|
||||
protected $context = null;
|
||||
|
||||
/**
|
||||
* Constructor method
|
||||
*
|
||||
* @param object $properties
|
||||
* @param stdClass $cm course module object
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public function __construct($properties, $cm = null) {
|
||||
parent::__construct($properties);
|
||||
$this->cm = $cm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simply generates a lesson object given an array/object of properties
|
||||
* Overrides {@see lesson_base->create()}
|
||||
@ -2066,6 +2092,169 @@ class lesson extends lesson_base {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the lesson context object.
|
||||
*
|
||||
* @return stdClass context
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public function get_context() {
|
||||
if ($this->context == null) {
|
||||
$this->context = context_module::instance($this->get_cm()->id);
|
||||
}
|
||||
return $this->context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the lesson course module object.
|
||||
*
|
||||
* @param stdClass $cm course module objct
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
private function set_cm($cm) {
|
||||
$this->cm = $cm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the lesson course module object.
|
||||
*
|
||||
* @return stdClass course module
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public function get_cm() {
|
||||
if ($this->cm == null) {
|
||||
$this->cm = get_coursemodule_from_instance('lesson', $this->properties->id);
|
||||
}
|
||||
return $this->cm;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the user can manage the lesson activity.
|
||||
*
|
||||
* @return bool true if the user can manage the lesson
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public function can_manage() {
|
||||
return has_capability('mod/lesson:manage', $this->get_context());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if time restriction is applied.
|
||||
*
|
||||
* @return mixed false if there aren't restrictions or an object with the restriction information
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public function get_time_restriction_status() {
|
||||
if ($this->can_manage()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!$this->is_accessible()) {
|
||||
if ($this->properties->deadline != 0 && time() > $this->properties->deadline) {
|
||||
$status = ['reason' => 'lessonclosed', 'time' => $this->properties->deadline];
|
||||
} else {
|
||||
$status = ['reason' => 'lessonopen', 'time' => $this->properties->available];
|
||||
}
|
||||
return (object) $status;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if password restriction is applied.
|
||||
*
|
||||
* @param string $userpassword the user password to check (if the restriction is set)
|
||||
* @return mixed false if there aren't restrictions or an object with the restriction information
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public function get_password_restriction_status($userpassword) {
|
||||
global $USER;
|
||||
if ($this->can_manage()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->properties->usepassword && empty($USER->lessonloggedin[$this->id])) {
|
||||
$correctpass = false;
|
||||
if (!empty($userpassword) &&
|
||||
(($this->properties->password == md5(trim($userpassword))) || ($this->properties->password == trim($userpassword)))) {
|
||||
// With or without md5 for backward compatibility (MDL-11090).
|
||||
$correctpass = true;
|
||||
$USER->lessonloggedin[$this->id] = true;
|
||||
} else if (isset($this->properties->extrapasswords)) {
|
||||
// Group overrides may have additional passwords.
|
||||
foreach ($this->properties->extrapasswords as $password) {
|
||||
if (strcmp($password, md5(trim($userpassword))) === 0 || strcmp($password, trim($userpassword)) === 0) {
|
||||
$correctpass = true;
|
||||
$USER->lessonloggedin[$this->id] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return !$correctpass;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if dependencies restrictions are applied.
|
||||
*
|
||||
* @return mixed false if there aren't restrictions or an object with the restriction information
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public function get_dependencies_restriction_status() {
|
||||
global $DB, $USER;
|
||||
if ($this->can_manage()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($dependentlesson = $DB->get_record('lesson', array('id' => $this->properties->dependency))) {
|
||||
// Lesson exists, so we can proceed.
|
||||
$conditions = unserialize($this->properties->conditions);
|
||||
// Assume false for all.
|
||||
$errors = array();
|
||||
// Check for the timespent condition.
|
||||
if ($conditions->timespent) {
|
||||
$timespent = false;
|
||||
if ($attempttimes = $DB->get_records('lesson_timer', array("userid" => $USER->id, "lessonid" => $dependentlesson->id))) {
|
||||
// Go through all the times and test to see if any of them satisfy the condition.
|
||||
foreach ($attempttimes as $attempttime) {
|
||||
$duration = $attempttime->lessontime - $attempttime->starttime;
|
||||
if ($conditions->timespent < $duration / 60) {
|
||||
$timespent = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$timespent) {
|
||||
$errors[] = get_string('timespenterror', 'lesson', $conditions->timespent);
|
||||
}
|
||||
}
|
||||
// Check for the gradebetterthan condition.
|
||||
if ($conditions->gradebetterthan) {
|
||||
$gradebetterthan = false;
|
||||
if ($studentgrades = $DB->get_records('lesson_grades', array("userid" => $USER->id, "lessonid" => $dependentlesson->id))) {
|
||||
// Go through all the grades and test to see if any of them satisfy the condition.
|
||||
foreach ($studentgrades as $studentgrade) {
|
||||
if ($studentgrade->grade >= $conditions->gradebetterthan) {
|
||||
$gradebetterthan = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$gradebetterthan) {
|
||||
$errors[] = get_string('gradebetterthanerror', 'lesson', $conditions->gradebetterthan);
|
||||
}
|
||||
}
|
||||
// Check for the completed condition.
|
||||
if ($conditions->completed) {
|
||||
if (!$DB->count_records('lesson_grades', array('userid' => $USER->id, 'lessonid' => $dependentlesson->id))) {
|
||||
$errors[] = get_string('completederror', 'lesson');
|
||||
}
|
||||
}
|
||||
if (!empty($errors)) {
|
||||
return (object) ['errors' => $errors, 'dependentlesson' => $dependentlesson];
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -34,16 +34,15 @@ $printclose = optional_param('printclose', 0, PARAM_INT);
|
||||
|
||||
$cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);
|
||||
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
|
||||
$lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
|
||||
$lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST), $cm);
|
||||
|
||||
require_login($course, false, $cm);
|
||||
|
||||
|
||||
// Apply overrides.
|
||||
$lesson->update_effective_access($USER->id);
|
||||
|
||||
$context = context_module::instance($cm->id);
|
||||
$canmanage = has_capability('mod/lesson:manage', $context);
|
||||
$context = $lesson->context;
|
||||
$canmanage = $lesson->can_manage();
|
||||
|
||||
$url = new moodle_url('/mod/lesson/mediafile.php', array('id'=>$id));
|
||||
if ($printclose !== '') {
|
||||
@ -67,49 +66,26 @@ if ($printclose) { // this is for framesets
|
||||
exit();
|
||||
}
|
||||
|
||||
echo $lessonoutput->header($lesson, $cm);
|
||||
|
||||
//TODO: this is copied from view.php - the access should be the same!
|
||||
/// Check these for students only TODO: Find a better method for doing this!
|
||||
/// Check lesson availability
|
||||
/// Check for password
|
||||
/// Check dependencies
|
||||
if (!$canmanage) {
|
||||
if (!$lesson->is_accessible()) { // Deadline restrictions
|
||||
echo $lessonoutput->header($lesson, $cm);
|
||||
if ($lesson->deadline != 0 && time() > $lesson->deadline) {
|
||||
echo $lessonoutput->lesson_inaccessible(get_string('lessonclosed', 'lesson', userdate($lesson->deadline)));
|
||||
} else {
|
||||
echo $lessonoutput->lesson_inaccessible(get_string('lessonopen', 'lesson', userdate($lesson->available)));
|
||||
}
|
||||
echo $lessonoutput->footer();
|
||||
exit();
|
||||
} else if ($lesson->usepassword && empty($USER->lessonloggedin[$lesson->id])) { // Password protected lesson code
|
||||
$correctpass = false;
|
||||
if (!empty($userpassword) && (($lesson->password == md5(trim($userpassword))) || ($lesson->password == trim($userpassword)))) {
|
||||
require_sesskey();
|
||||
// with or without md5 for backward compatibility (MDL-11090)
|
||||
$USER->lessonloggedin[$lesson->id] = true;
|
||||
$correctpass = true;
|
||||
} else if (isset($lesson->extrapasswords)) {
|
||||
// Group overrides may have additional passwords.
|
||||
foreach ($lesson->extrapasswords as $password) {
|
||||
if (strcmp($password, md5(trim($userpassword))) === 0 || strcmp($password, trim($userpassword)) === 0) {
|
||||
require_sesskey();
|
||||
$correctpass = true;
|
||||
$USER->lessonloggedin[$lesson->id] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$correctpass) {
|
||||
echo $lessonoutput->header($lesson, $cm);
|
||||
echo $lessonoutput->login_prompt($lesson, $userpassword !== '');
|
||||
echo $lessonoutput->footer();
|
||||
exit();
|
||||
}
|
||||
}
|
||||
// Check access restrictions.
|
||||
if ($timerestriction = $lesson->get_time_restriction_status()) { // Deadline restrictions.
|
||||
echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('notavailable'));
|
||||
echo $lessonoutput->lesson_inaccessible(get_string($timerestriction->reason, 'lesson', userdate($timerestriction->time)));
|
||||
echo $lessonoutput->footer();
|
||||
exit();
|
||||
} else if ($passwordrestriction = $lesson->get_password_restriction_status(null)) { // Password protected lesson code.
|
||||
echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('passwordprotectedlesson', 'lesson', format_string($lesson->name)));
|
||||
echo $lessonoutput->login_prompt($lesson, $userpassword !== '');
|
||||
echo $lessonoutput->footer();
|
||||
exit();
|
||||
} else if ($dependenciesrestriction = $lesson->get_dependencies_restriction_status()) { // Check for dependencies.
|
||||
echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('completethefollowingconditions', 'lesson', format_string($lesson->name)));
|
||||
echo $lessonoutput->dependancy_errors($dependenciesrestriction->dependentlesson, $dependenciesrestriction->errors);
|
||||
echo $lessonoutput->footer();
|
||||
exit();
|
||||
}
|
||||
|
||||
echo $lessonoutput->header($lesson, $cm);
|
||||
|
||||
// print the embedded media html code
|
||||
echo $OUTPUT->box(lesson_get_media_html($lesson, $context));
|
||||
|
||||
|
@ -37,7 +37,7 @@ $backtocourse = optional_param('backtocourse', false, PARAM_RAW);
|
||||
|
||||
$cm = get_coursemodule_from_id('lesson', $id, 0, false, MUST_EXIST);
|
||||
$course = $DB->get_record('course', array('id' => $cm->course), '*', MUST_EXIST);
|
||||
$lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST));
|
||||
$lesson = new lesson($DB->get_record('lesson', array('id' => $cm->instance), '*', MUST_EXIST), $cm);
|
||||
|
||||
require_login($course, false, $cm);
|
||||
|
||||
@ -59,8 +59,8 @@ if ($pageid !== null) {
|
||||
$PAGE->set_url($url);
|
||||
$PAGE->force_settings_menu();
|
||||
|
||||
$context = context_module::instance($cm->id);
|
||||
$canmanage = has_capability('mod/lesson:manage', $context);
|
||||
$context = $lesson->context;
|
||||
$canmanage = $lesson->can_manage();
|
||||
|
||||
$lessonoutput = $PAGE->get_renderer('mod_lesson');
|
||||
|
||||
@ -70,103 +70,29 @@ if ($userhasgrade && !$lesson->retake) {
|
||||
$reviewmode = true;
|
||||
}
|
||||
|
||||
/// Check these for students only TODO: Find a better method for doing this!
|
||||
/// Check lesson availability
|
||||
/// Check for password
|
||||
/// Check dependencies
|
||||
if (!$canmanage) {
|
||||
if (!$lesson->is_accessible()) { // Deadline restrictions
|
||||
echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('notavailable'));
|
||||
if ($lesson->deadline != 0 && time() > $lesson->deadline) {
|
||||
echo $lessonoutput->lesson_inaccessible(get_string('lessonclosed', 'lesson', userdate($lesson->deadline)));
|
||||
} else {
|
||||
echo $lessonoutput->lesson_inaccessible(get_string('lessonopen', 'lesson', userdate($lesson->available)));
|
||||
}
|
||||
echo $lessonoutput->footer();
|
||||
exit();
|
||||
} else if ($lesson->usepassword && empty($USER->lessonloggedin[$lesson->id])) { // Password protected lesson code
|
||||
$correctpass = false;
|
||||
if (!empty($userpassword) && (($lesson->password == md5(trim($userpassword))) || ($lesson->password == trim($userpassword)))) {
|
||||
require_sesskey();
|
||||
// with or without md5 for backward compatibility (MDL-11090)
|
||||
$correctpass = true;
|
||||
$USER->lessonloggedin[$lesson->id] = true;
|
||||
|
||||
} else if (isset($lesson->extrapasswords)) {
|
||||
|
||||
// Group overrides may have additional passwords.
|
||||
foreach ($lesson->extrapasswords as $password) {
|
||||
if (strcmp($password, md5(trim($userpassword))) === 0 || strcmp($password, trim($userpassword)) === 0) {
|
||||
require_sesskey();
|
||||
$correctpass = true;
|
||||
$USER->lessonloggedin[$lesson->id] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$correctpass) {
|
||||
echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('passwordprotectedlesson', 'lesson', format_string($lesson->name)));
|
||||
echo $lessonoutput->login_prompt($lesson, $userpassword !== '');
|
||||
echo $lessonoutput->footer();
|
||||
exit();
|
||||
}
|
||||
} else if ($lesson->dependency) { // check for dependencies
|
||||
if ($dependentlesson = $DB->get_record('lesson', array('id' => $lesson->dependency))) {
|
||||
// lesson exists, so we can proceed
|
||||
$conditions = unserialize($lesson->conditions);
|
||||
// assume false for all
|
||||
$errors = array();
|
||||
|
||||
// check for the timespent condition
|
||||
if ($conditions->timespent) {
|
||||
$timespent = false;
|
||||
if ($attempttimes = $DB->get_records('lesson_timer', array("userid"=>$USER->id, "lessonid"=>$dependentlesson->id))) {
|
||||
// go through all the times and test to see if any of them satisfy the condition
|
||||
foreach($attempttimes as $attempttime) {
|
||||
$duration = $attempttime->lessontime - $attempttime->starttime;
|
||||
if ($conditions->timespent < $duration/60) {
|
||||
$timespent = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$timespent) {
|
||||
$errors[] = get_string('timespenterror', 'lesson', $conditions->timespent);
|
||||
}
|
||||
}
|
||||
|
||||
// check for the gradebetterthan condition
|
||||
if($conditions->gradebetterthan) {
|
||||
$gradebetterthan = false;
|
||||
if ($studentgrades = $DB->get_records('lesson_grades', array("userid"=>$USER->id, "lessonid"=>$dependentlesson->id))) {
|
||||
// go through all the grades and test to see if any of them satisfy the condition
|
||||
foreach($studentgrades as $studentgrade) {
|
||||
if ($studentgrade->grade >= $conditions->gradebetterthan) {
|
||||
$gradebetterthan = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$gradebetterthan) {
|
||||
$errors[] = get_string('gradebetterthanerror', 'lesson', $conditions->gradebetterthan);
|
||||
}
|
||||
}
|
||||
|
||||
// check for the completed condition
|
||||
if ($conditions->completed) {
|
||||
if (!$DB->count_records('lesson_grades', array('userid'=>$USER->id, 'lessonid'=>$dependentlesson->id))) {
|
||||
$errors[] = get_string('completederror', 'lesson');
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($errors)) { // print out the errors if any
|
||||
echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('completethefollowingconditions', 'lesson', format_string($lesson->name)));
|
||||
echo $lessonoutput->dependancy_errors($dependentlesson, $errors);
|
||||
echo $lessonoutput->footer();
|
||||
exit();
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($lesson->usepassword && !empty($userpassword)) {
|
||||
require_sesskey();
|
||||
}
|
||||
|
||||
// this is called if a student leaves during a lesson
|
||||
// Check these for students only TODO: Find a better method for doing this!
|
||||
if ($timerestriction = $lesson->get_time_restriction_status()) { // Deadline restrictions.
|
||||
echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('notavailable'));
|
||||
echo $lessonoutput->lesson_inaccessible(get_string($timerestriction->reason, 'lesson', userdate($timerestriction->time)));
|
||||
echo $lessonoutput->footer();
|
||||
exit();
|
||||
} else if ($passwordrestriction = $lesson->get_password_restriction_status($userpassword)) { // Password protected lesson code.
|
||||
echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('passwordprotectedlesson', 'lesson', format_string($lesson->name)));
|
||||
echo $lessonoutput->login_prompt($lesson, $userpassword !== '');
|
||||
echo $lessonoutput->footer();
|
||||
exit();
|
||||
} else if ($dependenciesrestriction = $lesson->get_dependencies_restriction_status()) { // Check for dependencies.
|
||||
echo $lessonoutput->header($lesson, $cm, '', false, null, get_string('completethefollowingconditions', 'lesson', format_string($lesson->name)));
|
||||
echo $lessonoutput->dependancy_errors($dependenciesrestriction->dependentlesson, $dependenciesrestriction->errors);
|
||||
echo $lessonoutput->footer();
|
||||
exit();
|
||||
}
|
||||
|
||||
// This is called if a student leaves during a lesson.
|
||||
if ($pageid == LESSON_UNSEENBRANCHPAGE) {
|
||||
$pageid = lesson_unseen_question_jump($lesson, $USER->id, $pageid);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user