mirror of
https://github.com/moodle/moodle.git
synced 2025-04-15 05:25:08 +02:00
MDL-39536 completion: Prevent DB call when fetching activities
This commit is contained in:
parent
b6f8a93642
commit
8ca0c12370
@ -1045,40 +1045,36 @@ class completion_info {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return whether or not the course has activities with completion enabled.
|
||||
*
|
||||
* @return boolean true when there is at least one activity with completion enabled.
|
||||
*/
|
||||
public function has_activities() {
|
||||
$modinfo = get_fast_modinfo($this->course);
|
||||
foreach ($modinfo->get_cms() as $cm) {
|
||||
if ($cm->completion != COMPLETION_TRACKING_NONE) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains a list of activities for which completion is enabled on the
|
||||
* course. The list is ordered by the section order of those activities.
|
||||
*
|
||||
* @param array $modinfo For unit testing only, supply the value
|
||||
* here. Otherwise the method calls get_fast_modinfo
|
||||
* @return array Array from $cmid => $cm of all activities with completion enabled,
|
||||
* empty array if none
|
||||
*/
|
||||
public function get_activities($modinfo=null) {
|
||||
global $DB;
|
||||
|
||||
// Obtain those activities which have completion turned on
|
||||
$withcompletion = $DB->get_records_select('course_modules', 'course='.$this->course->id.
|
||||
' AND completion<>'.COMPLETION_TRACKING_NONE);
|
||||
if (!$withcompletion) {
|
||||
return array();
|
||||
}
|
||||
|
||||
// Use modinfo to get section order and also add in names
|
||||
if (empty($modinfo)) {
|
||||
$modinfo = get_fast_modinfo($this->course);
|
||||
}
|
||||
public function get_activities() {
|
||||
$modinfo = get_fast_modinfo($this->course);
|
||||
$result = array();
|
||||
foreach ($modinfo->sections as $sectioncms) {
|
||||
foreach ($sectioncms as $cmid) {
|
||||
if (array_key_exists($cmid, $withcompletion)) {
|
||||
$result[$cmid] = $withcompletion[$cmid];
|
||||
$result[$cmid]->modname = $modinfo->cms[$cmid]->modname;
|
||||
$result[$cmid]->name = $modinfo->cms[$cmid]->name;
|
||||
}
|
||||
foreach ($modinfo->get_cms() as $cm) {
|
||||
if ($cm->completion != COMPLETION_TRACKING_NONE) {
|
||||
$result[$cm->id] = $cm;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ function report_progress_extend_navigation_course($navigation, $course, $context
|
||||
}
|
||||
|
||||
$completion = new completion_info($course);
|
||||
$showonnavigation = ($showonnavigation && $completion->is_enabled() && count($completion->get_activities())>0);
|
||||
$showonnavigation = ($showonnavigation && $completion->is_enabled() && $completion->has_activities());
|
||||
if ($showonnavigation) {
|
||||
$url = new moodle_url('/report/progress/index.php', array('course'=>$course->id));
|
||||
$navigation->add(get_string('pluginname','report_progress'), $url, navigation_node::TYPE_SETTING, null, null, new pix_icon('i/report', ''));
|
||||
|
Loading…
x
Reference in New Issue
Block a user