MDL-57693 mod_lesson: Refactor to move code to lesson methods
This is a big refactor to avoid code duplication in the new WS. Basically I’ve moved code from the page and renderer to the lesson class. New code is into methods, there are no code functionality changes (just minor fixes in comments).
This commit is contained in:
@@ -2525,6 +2525,156 @@ class lesson extends lesson_base {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ongoing score message for the user (depending on the user permission and lesson settings).
|
||||
*
|
||||
* @return str the ongoing score message
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public function get_ongoing_score_message() {
|
||||
global $USER, $DB;
|
||||
|
||||
$context = $this->get_context();
|
||||
|
||||
if (has_capability('mod/lesson:manage', $context)) {
|
||||
return get_string('teacherongoingwarning', 'lesson');
|
||||
} else {
|
||||
$ntries = $DB->count_records("lesson_grades", array("lessonid" => $this->properties->id, "userid" => $USER->id));
|
||||
if (isset($USER->modattempts[$this->properties->id])) {
|
||||
$ntries--;
|
||||
}
|
||||
$gradeinfo = lesson_grade($this, $ntries);
|
||||
$a = new stdClass;
|
||||
if ($this->properties->custom) {
|
||||
$a->score = $gradeinfo->earned;
|
||||
$a->currenthigh = $gradeinfo->total;
|
||||
return get_string("ongoingcustom", "lesson", $a);
|
||||
} else {
|
||||
$a->correct = $gradeinfo->earned;
|
||||
$a->viewed = $gradeinfo->attempts;
|
||||
return get_string("ongoingnormal", "lesson", $a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the progress of the current user in the lesson.
|
||||
*
|
||||
* @return int the progress (scale 0-100)
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public function calculate_progress() {
|
||||
global $USER, $DB;
|
||||
|
||||
// Check if the user is reviewing the attempt.
|
||||
if (isset($USER->modattempts[$this->properties->id])) {
|
||||
return 100;
|
||||
}
|
||||
|
||||
// All of the lesson pages.
|
||||
$pages = $this->load_all_pages();
|
||||
foreach ($pages as $page) {
|
||||
if ($page->prevpageid == 0) {
|
||||
$pageid = $page->id; // Find the first page id.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Current attempt number.
|
||||
if (!$ntries = $DB->count_records("lesson_grades", array("lessonid" => $this->properties->id, "userid" => $USER->id))) {
|
||||
$ntries = 0; // May not be necessary.
|
||||
}
|
||||
|
||||
$viewedpageids = array();
|
||||
if ($attempts = $this->get_attempts($ntries, false)) {
|
||||
foreach ($attempts as $attempt) {
|
||||
$viewedpageids[$attempt->pageid] = $attempt;
|
||||
}
|
||||
}
|
||||
|
||||
$viewedbranches = array();
|
||||
// Collect all of the branch tables viewed.
|
||||
if ($branches = $this->get_content_pages_viewed($ntries, $USER->id, 'timeseen ASC', 'id, pageid')) {
|
||||
foreach ($branches as $branch) {
|
||||
$viewedbranches[$branch->pageid] = $branch;
|
||||
}
|
||||
$viewedpageids = array_merge($viewedpageids, $viewedbranches);
|
||||
}
|
||||
|
||||
// Filter out the following pages:
|
||||
// - End of Cluster
|
||||
// - End of Branch
|
||||
// - Pages found inside of Clusters
|
||||
// Do not filter out Cluster Page(s) because we count a cluster as one.
|
||||
// By keeping the cluster page, we get our 1.
|
||||
$validpages = array();
|
||||
while ($pageid != 0) {
|
||||
$pageid = $pages[$pageid]->valid_page_and_view($validpages, $viewedpageids);
|
||||
}
|
||||
|
||||
// Progress calculation as a percent.
|
||||
return round(count($viewedpageids) / count($validpages), 2) * 100;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the correct page and prepare contents for a given page id (could be a page jump id).
|
||||
*
|
||||
* @param int $pageid the given page id
|
||||
* @param mod_lesson_renderer $lessonoutput the lesson output rendered
|
||||
* @param bool $reviewmode whether we are in review mode or not
|
||||
* @return array the page object and contents
|
||||
* @throws moodle_exception
|
||||
* @since Moodle 3.3
|
||||
*/
|
||||
public function prepare_page_and_contents($pageid, $lessonoutput, $reviewmode) {
|
||||
global $USER, $CFG;
|
||||
|
||||
$page = $this->load_page($pageid);
|
||||
// Check if the page is of a special type and if so take any nessecary action.
|
||||
$newpageid = $page->callback_on_view($this->can_manage());
|
||||
if (is_numeric($newpageid)) {
|
||||
$page = $this->load_page($newpageid);
|
||||
}
|
||||
|
||||
// Add different informative messages to the given page.
|
||||
$this->add_messages_on_page_view($page, $reviewmode);
|
||||
|
||||
if (is_array($page->answers) && count($page->answers) > 0) {
|
||||
// This is for modattempts option. Find the users previous answer to this page,
|
||||
// and then display it below in answer processing.
|
||||
if (isset($USER->modattempts[$this->properties->id])) {
|
||||
$retries = $this->count_user_retries($USER->id);
|
||||
if (!$attempts = $this->get_attempts($retries - 1, false, $page->id)) {
|
||||
throw new moodle_exception('cannotfindpreattempt', 'lesson');
|
||||
}
|
||||
$attempt = end($attempts);
|
||||
$USER->modattempts[$this->properties->id] = $attempt;
|
||||
} else {
|
||||
$attempt = false;
|
||||
}
|
||||
$lessoncontent = $lessonoutput->display_page($this, $page, $attempt);
|
||||
} else {
|
||||
require_once($CFG->dirroot . '/mod/lesson/view_form.php');
|
||||
$data = new stdClass;
|
||||
$data->id = $this->get_cm()->id;
|
||||
$data->pageid = $page->id;
|
||||
$data->newpageid = $this->get_next_page($page->nextpageid);
|
||||
|
||||
$customdata = array(
|
||||
'title' => $page->title,
|
||||
'contents' => $page->get_contents()
|
||||
);
|
||||
$mform = new lesson_page_without_answers($CFG->wwwroot.'/mod/lesson/continue.php', $customdata);
|
||||
$mform->set_data($data);
|
||||
ob_start();
|
||||
$mform->display();
|
||||
$lessoncontent = ob_get_contents();
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
return array($page, $lessoncontent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
+3
-71
@@ -477,28 +477,7 @@ class mod_lesson_renderer extends plugin_renderer_base {
|
||||
* @return string
|
||||
*/
|
||||
public function ongoing_score(lesson $lesson) {
|
||||
global $USER, $DB;
|
||||
|
||||
$context = context_module::instance($this->page->cm->id);
|
||||
if (has_capability('mod/lesson:manage', $context)) {
|
||||
return $this->output->box(get_string('teacherongoingwarning', 'lesson'), "ongoing center");
|
||||
} else {
|
||||
$ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
|
||||
if (isset($USER->modattempts[$lesson->id])) {
|
||||
$ntries--;
|
||||
}
|
||||
$gradeinfo = lesson_grade($lesson, $ntries);
|
||||
$a = new stdClass;
|
||||
if ($lesson->custom) {
|
||||
$a->score = $gradeinfo->earned;
|
||||
$a->currenthigh = $gradeinfo->total;
|
||||
return $this->output->box(get_string("ongoingcustom", "lesson", $a), "ongoing center");
|
||||
} else {
|
||||
$a->correct = $gradeinfo->earned;
|
||||
$a->viewed = $gradeinfo->attempts;
|
||||
return $this->output->box(get_string("ongoingnormal", "lesson", $a), "ongoing center");
|
||||
}
|
||||
}
|
||||
return $this->output->box($lesson->get_ongoing_score_message(), "ongoing center");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -508,8 +487,6 @@ class mod_lesson_renderer extends plugin_renderer_base {
|
||||
* @return string
|
||||
*/
|
||||
public function progress_bar(lesson $lesson) {
|
||||
global $CFG, $USER, $DB;
|
||||
|
||||
$context = context_module::instance($this->page->cm->id);
|
||||
|
||||
// lesson setting to turn progress bar on or off
|
||||
@@ -522,53 +499,8 @@ class mod_lesson_renderer extends plugin_renderer_base {
|
||||
return $this->output->notification(get_string('progressbarteacherwarning2', 'lesson'));
|
||||
}
|
||||
|
||||
if (!isset($USER->modattempts[$lesson->id])) {
|
||||
// all of the lesson pages
|
||||
$pages = $lesson->load_all_pages();
|
||||
foreach ($pages as $page) {
|
||||
if ($page->prevpageid == 0) {
|
||||
$pageid = $page->id; // find the first page id
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// current attempt number
|
||||
if (!$ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id))) {
|
||||
$ntries = 0; // may not be necessary
|
||||
}
|
||||
|
||||
$viewedpageids = array();
|
||||
if ($attempts = $lesson->get_attempts($ntries, false)) {
|
||||
foreach($attempts as $attempt) {
|
||||
$viewedpageids[$attempt->pageid] = $attempt;
|
||||
}
|
||||
}
|
||||
|
||||
$viewedbranches = array();
|
||||
// collect all of the branch tables viewed
|
||||
if ($branches = $lesson->get_content_pages_viewed($ntries, $USER->id, 'timeseen ASC', 'id, pageid')) {
|
||||
foreach($branches as $branch) {
|
||||
$viewedbranches[$branch->pageid] = $branch;
|
||||
}
|
||||
$viewedpageids = array_merge($viewedpageids, $viewedbranches);
|
||||
}
|
||||
|
||||
// Filter out the following pages:
|
||||
// End of Cluster
|
||||
// End of Branch
|
||||
// Pages found inside of Clusters
|
||||
// Do not filter out Cluster Page(s) because we count a cluster as one.
|
||||
// By keeping the cluster page, we get our 1
|
||||
$validpages = array();
|
||||
while ($pageid != 0) {
|
||||
$pageid = $pages[$pageid]->valid_page_and_view($validpages, $viewedpageids);
|
||||
}
|
||||
|
||||
// progress calculation as a percent
|
||||
$progress = round(count($viewedpageids)/count($validpages), 2) * 100;
|
||||
} else {
|
||||
$progress = 100;
|
||||
}
|
||||
// Check if the user is reviewing the attempt.
|
||||
$progress = $lesson->calculate_progress();
|
||||
|
||||
// print out the Progress Bar. Attempted to put as much as possible in the style sheets.
|
||||
$content = '<br />' . html_writer::tag('div', $progress . '%', array('class' => 'progress_bar_completed', 'style' => 'width: '. $progress . '%;'));
|
||||
|
||||
+11
-55
@@ -25,7 +25,6 @@
|
||||
|
||||
require_once(__DIR__ . '/../../config.php');
|
||||
require_once($CFG->dirroot.'/mod/lesson/locallib.php');
|
||||
require_once($CFG->dirroot.'/mod/lesson/view_form.php');
|
||||
require_once($CFG->libdir . '/grade/constants.php');
|
||||
|
||||
$id = required_param('id', PARAM_INT); // Course Module ID
|
||||
@@ -181,24 +180,14 @@ $lessonpageid = null;
|
||||
$timer = null;
|
||||
|
||||
if ($pageid != LESSON_EOL) {
|
||||
/// This is the code updates the lessontime for a timed test
|
||||
$startlastseen = optional_param('startlastseen', '', PARAM_ALPHA);
|
||||
|
||||
$page = $lesson->load_page($pageid);
|
||||
// Check if the page is of a special type and if so take any nessecary action
|
||||
$newpageid = $page->callback_on_view($canmanage);
|
||||
if (is_numeric($newpageid)) {
|
||||
$page = $lesson->load_page($newpageid);
|
||||
}
|
||||
|
||||
$lesson->set_module_viewed();
|
||||
|
||||
$timer = null;
|
||||
// This is the code updates the lessontime for a timed test.
|
||||
$startlastseen = optional_param('startlastseen', '', PARAM_ALPHA);
|
||||
|
||||
// This is where several messages (usually warnings) are displayed
|
||||
// all of this is displayed above the actual page
|
||||
|
||||
// check to see if the user can see the left menu
|
||||
// Check to see if the user can see the left menu.
|
||||
if (!$canmanage) {
|
||||
$lesson->displayleft = lesson_displayleftif($lesson);
|
||||
|
||||
@@ -213,8 +202,11 @@ if ($pageid != LESSON_EOL) {
|
||||
}
|
||||
}
|
||||
|
||||
// Add different informative messages to the given page.
|
||||
$lesson->add_messages_on_page_view($page, $reviewmode);
|
||||
list($page, $lessoncontent) = $lesson->prepare_page_and_contents($pageid, $lessonoutput, $reviewmode);
|
||||
|
||||
if (($edit != -1) && $PAGE->user_allowed_editing()) {
|
||||
$USER->editing = $edit;
|
||||
}
|
||||
|
||||
$PAGE->set_subpage($page->id);
|
||||
$currenttab = 'view';
|
||||
@@ -222,49 +214,13 @@ if ($pageid != LESSON_EOL) {
|
||||
$lessonpageid = $page->id;
|
||||
$extrapagetitle = $page->title;
|
||||
|
||||
if (($edit != -1) && $PAGE->user_allowed_editing()) {
|
||||
$USER->editing = $edit;
|
||||
}
|
||||
|
||||
if (is_array($page->answers) && count($page->answers)>0) {
|
||||
// this is for modattempts option. Find the users previous answer to this page,
|
||||
// and then display it below in answer processing
|
||||
if (isset($USER->modattempts[$lesson->id])) {
|
||||
$retries = $lesson->count_user_retries($USER->id);
|
||||
if (!$attempts = $lesson->get_attempts($retries-1, false, $page->id)) {
|
||||
print_error('cannotfindpreattempt', 'lesson');
|
||||
}
|
||||
$attempt = end($attempts);
|
||||
$USER->modattempts[$lesson->id] = $attempt;
|
||||
} else {
|
||||
$attempt = false;
|
||||
}
|
||||
$lessoncontent = $lessonoutput->display_page($lesson, $page, $attempt);
|
||||
} else {
|
||||
$data = new stdClass;
|
||||
$data->id = $PAGE->cm->id;
|
||||
$data->pageid = $page->id;
|
||||
$data->newpageid = $lesson->get_next_page($page->nextpageid);
|
||||
|
||||
$customdata = array(
|
||||
'title' => $page->title,
|
||||
'contents' => $page->get_contents()
|
||||
);
|
||||
$mform = new lesson_page_without_answers($CFG->wwwroot.'/mod/lesson/continue.php', $customdata);
|
||||
$mform->set_data($data);
|
||||
ob_start();
|
||||
$mform->display();
|
||||
$lessoncontent = ob_get_contents();
|
||||
ob_end_clean();
|
||||
}
|
||||
|
||||
lesson_add_fake_blocks($PAGE, $cm, $lesson, $timer);
|
||||
echo $lessonoutput->header($lesson, $cm, $currenttab, $extraeditbuttons, $lessonpageid, $extrapagetitle);
|
||||
if ($attemptflag) {
|
||||
// We are using level 3 header because attempt heading is a sub-heading of lesson title (MDL-30911).
|
||||
echo $OUTPUT->heading(get_string('attempt', 'lesson', $retries), 3);
|
||||
}
|
||||
/// This calculates and prints the ongoing score
|
||||
// This calculates and prints the ongoing score.
|
||||
if ($lesson->ongoing && !empty($pageid) && !$reviewmode) {
|
||||
echo $lessonoutput->ongoing_score($lesson);
|
||||
}
|
||||
@@ -278,8 +234,8 @@ if ($pageid != LESSON_EOL) {
|
||||
} else {
|
||||
|
||||
$lessoncontent = '';
|
||||
// end of lesson reached work out grade
|
||||
// Used to check to see if the student ran out of time
|
||||
// End of lesson reached work out grade.
|
||||
// Used to check to see if the student ran out of time.
|
||||
$outoftime = optional_param('outoftime', '', PARAM_ALPHA);
|
||||
|
||||
$ntries = $DB->count_records("lesson_grades", array("lessonid"=>$lesson->id, "userid"=>$USER->id));
|
||||
|
||||
Reference in New Issue
Block a user