/** * Returns HTML to display a progress bar of progression through a lesson * * @param lesson $lesson * @return string */ public function progress_bar(lesson $lesson) { global $CFG, $USER, $DB; $context = get_context_instance(CONTEXT_MODULE, $this->page->cm->id); // lesson setting to turn progress bar on or off if (!$lesson->progressbar) { return ''; } // catch teachers if (has_capability('mod/lesson:manage', $context)) { 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, true)) { $viewedpageids = array_merge($viewedpageids, array_keys($attempts)); } // collect all of the branch tables viewed if ($viewedbranches = $DB->get_records("lesson_branch", array("lessonid" => $lesson->id, "userid" => $USER->id, "retry" => $ntries), 'timeseen DESC', 'id, pageid')) { $viewedpageids = array_merge($viewedpageids, array_keys($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; } // print out the Progress Bar. Attempted to put as much as possible in the style sheets. $cells = array(); if ($progress != 0) { // some browsers do not repsect the 0 width. $cells[0] = new html_table_cell(); $cells[0]->style = 'width:' . $progress . '%'; $cells[0]->attributes['class'] = 'progress_bar_completed'; $cells[0]->text = ' '; } $cells[] = '<div class="progress_bar_token"></div>'; $table = new html_table(); $table->attributes['class'] = 'progress_bar_table'; $table->data = array(new html_table_row($cells)); return $this->output->box(html_writer::table($table), 'progress_bar'); }
/** * Returns HTML to display a progress bar of progression through a lesson * * @param lesson $lesson * @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 if (!$lesson->progressbar) { return ''; } // catch teachers if (has_capability('mod/lesson:manage', $context)) { 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 = $DB->get_records("lesson_branch", array("lessonid" => $lesson->id, "userid" => $USER->id, "retry" => $ntries), '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; } // 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 . '%;')); $printprogress = html_writer::tag('div', get_string('progresscompleted', 'lesson', $progress) . $content, array('class' => 'progress_bar')); return $this->output->box($printprogress, 'progress_bar'); }