MDL-35768 MDL-36017 Avoid calling format_base::get_course() when not necessary

- If we want to check fields 'numsections' or 'hiddensections' call course_get_format()->get_format_options()
- We still use extended course object in course/edit.php, update_course(), create_course(), and inside course formats
This commit is contained in:
Marina Glancy
2012-11-05 11:22:06 +08:00
parent 92d38668ac
commit 850acb3560
9 changed files with 89 additions and 77 deletions
+4 -4
View File
@@ -59,8 +59,8 @@ function online_assignment_cleanup($output=false) {
if ($output) echo $OUTPUT->heading($fullname);
/// retrieve a list of sections beyond what is currently being shown
$course = course_get_format($course)->get_course();
if (!isset($course->numsections)) {
$courseformatoptions = course_get_format($course)->get_format_options();
if (!isset($courseformatoptions['numsections'])) {
// Course format does not use numsections
if ($output) {
echo 'No extra sections<br />';
@@ -71,7 +71,7 @@ function online_assignment_cleanup($output=false) {
FROM {course_sections}
WHERE course=? AND section>?
ORDER BY section ASC";
$params = array($course->id, $course->numsections);
$params = array($course->id, $courseformatoptions['numsections']);
if (!($xsections = $DB->get_records_sql($sql, $params))) {
if ($output) echo 'No extra sections<br />';
continue;
@@ -102,7 +102,7 @@ function online_assignment_cleanup($output=false) {
/// the journal update erroneously stored it in course_sections->section
$newsection = $xsection->section;
/// double check the new section
if ($newsection > $course->numsections) {
if ($newsection > $courseformatoptions['numsections']) {
/// get the record for section 0 for this course
if (!($zerosection = $DB->get_record('course_sections', array('course'=>$course->id, 'section'=>'0')))) {
continue;
+8 -7
View File
@@ -71,7 +71,8 @@ class block_section_links extends block_base {
return $this->content;
}
$course = course_get_format($this->page->course)->get_course();
$course = $this->page->course;
$courseformatoptions = course_get_format($course)->get_format_options();
$context = context_course::instance($course->id);
if ($course->format == 'weeks' or $course->format == 'weekscss') {
@@ -86,18 +87,18 @@ class block_section_links extends block_base {
}
$inc = 1;
if(!empty($config->numsections1) and ($course->numsections > $config->numsections1)) {
if(!empty($config->numsections1) and ($courseformatoptions['numsections'] > $config->numsections1)) {
$inc = $config->incby1;
} else {
if ($course->numsections > 22) {
if ($courseformatoptions['numsections'] > 22) {
$inc = 2;
}
}
if(!empty($config->numsections2) and ($course->numsections > $config->numsections2)) {
if(!empty($config->numsections2) and ($courseformatoptions['numsections'] > $config->numsections2)) {
$inc = $config->incby2;
} else {
if ($course->numsections > 40) {
if ($courseformatoptions['numsections'] > 40) {
$inc = 5;
}
}
@@ -105,12 +106,12 @@ class block_section_links extends block_base {
$sql = "SELECT section, visible
FROM {course_sections}
WHERE course = ? AND
section < ".($course->numsections+1)."
section < ".($courseformatoptions['numsections']+1)."
ORDER BY section";
if ($sections = $DB->get_records_sql($sql, array($course->id))) {
$text = '<ol class="inline-list">';
for ($i = $inc; $i <= $course->numsections; $i += $inc) {
for ($i = $inc; $i <= $courseformatoptions['numsections']; $i += $inc) {
if (!isset($sections[$i])) {
continue;
}
+8 -6
View File
@@ -30,7 +30,8 @@ require_once($CFG->dirroot.'/course/lib.php');
$courseid = required_param('courseid', PARAM_INT);
$increase = optional_param('increase', true, PARAM_BOOL);
$course = course_get_format($courseid)->get_course();
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$courseformatoptions = course_get_format($course)->get_format_options();
$PAGE->set_url('/course/changenumsections.php', array('courseid' => $courseid));
@@ -39,19 +40,20 @@ require_login($course);
require_capability('moodle/course:update', context_course::instance($course->id));
require_sesskey();
if (isset($course->numsections)) {
if (isset($courseformatoptions['numsections'])) {
if ($increase) {
// Add an additional section.
$course->numsections++;
$courseformatoptions['numsections']++;
} else {
// Remove a section.
$course->numsections--;
$courseformatoptions['numsections']--;
}
// Don't go less than 0, intentionally redirect silently (for the case of
// double clicks).
if ($course->numsections >= 0) {
course_get_format($course)->update_course_format_options(array('numsections' => $course->numsections));
if ($courseformatoptions['numsections'] >= 0) {
course_get_format($course)->update_course_format_options(
array('numsections' => $courseformatoptions['numsections']));
}
}
+8 -4
View File
@@ -2939,8 +2939,10 @@ function move_section($course, $section, $move) {
$sectiondest = $section + $move;
$course = course_get_format($course)->get_course();
if (isset($course->numsections) && $sectiondest > $course->numsections or $sectiondest < 1) {
// compartibility with course formats using field 'numsections'
$courseformatoptions = course_get_format($course)->get_format_options();
if (array_key_exists('numsections', $courseformatoptions) &&
$sectiondest > $courseformatoptions['numsections'] or $sectiondest < 1) {
return false;
}
@@ -2966,8 +2968,10 @@ function move_section_to($course, $section, $destination) {
return true;
}
$course = course_get_format($course)->get_course();
if ((isset($course->numsections) && ($destination > $course->numsections)) || ($destination < 1)) {
// compartibility with course formats using field 'numsections'
$courseformatoptions = course_get_format($course)->get_format_options();
if ((array_key_exists('numsections', $courseformatoptions) &&
($destination > $courseformatoptions['numsections'])) || ($destination < 1)) {
return false;
}
+45 -45
View File
@@ -359,64 +359,64 @@ class core_course_external_testcase extends externallib_advanced_testcase {
// Check that the courses were correctly created.
foreach ($createdcourses as $createdcourse) {
$dbcourse = course_get_format($createdcourse['id'])->get_course();
$courseinfo = course_get_format($createdcourse['id'])->get_course();
if ($createdcourse['shortname'] == $course2['shortname']) {
$this->assertEquals($dbcourse->fullname, $course2['fullname']);
$this->assertEquals($dbcourse->shortname, $course2['shortname']);
$this->assertEquals($dbcourse->category, $course2['categoryid']);
$this->assertEquals($dbcourse->idnumber, $course2['idnumber']);
$this->assertEquals($dbcourse->summary, $course2['summary']);
$this->assertEquals($dbcourse->summaryformat, $course2['summaryformat']);
$this->assertEquals($dbcourse->format, $course2['format']);
$this->assertEquals($dbcourse->showgrades, $course2['showgrades']);
$this->assertEquals($dbcourse->newsitems, $course2['newsitems']);
$this->assertEquals($dbcourse->startdate, $course2['startdate']);
$this->assertEquals($dbcourse->numsections, $course2['numsections']);
$this->assertEquals($dbcourse->maxbytes, $course2['maxbytes']);
$this->assertEquals($dbcourse->showreports, $course2['showreports']);
$this->assertEquals($dbcourse->visible, $course2['visible']);
$this->assertEquals($dbcourse->hiddensections, $course2['hiddensections']);
$this->assertEquals($dbcourse->groupmode, $course2['groupmode']);
$this->assertEquals($dbcourse->groupmodeforce, $course2['groupmodeforce']);
$this->assertEquals($dbcourse->defaultgroupingid, $course2['defaultgroupingid']);
$this->assertEquals($dbcourse->completionnotify, $course2['completionnotify']);
$this->assertEquals($dbcourse->lang, $course2['lang']);
$this->assertEquals($courseinfo->fullname, $course2['fullname']);
$this->assertEquals($courseinfo->shortname, $course2['shortname']);
$this->assertEquals($courseinfo->category, $course2['categoryid']);
$this->assertEquals($courseinfo->idnumber, $course2['idnumber']);
$this->assertEquals($courseinfo->summary, $course2['summary']);
$this->assertEquals($courseinfo->summaryformat, $course2['summaryformat']);
$this->assertEquals($courseinfo->format, $course2['format']);
$this->assertEquals($courseinfo->showgrades, $course2['showgrades']);
$this->assertEquals($courseinfo->newsitems, $course2['newsitems']);
$this->assertEquals($courseinfo->startdate, $course2['startdate']);
$this->assertEquals($courseinfo->numsections, $course2['numsections']);
$this->assertEquals($courseinfo->maxbytes, $course2['maxbytes']);
$this->assertEquals($courseinfo->showreports, $course2['showreports']);
$this->assertEquals($courseinfo->visible, $course2['visible']);
$this->assertEquals($courseinfo->hiddensections, $course2['hiddensections']);
$this->assertEquals($courseinfo->groupmode, $course2['groupmode']);
$this->assertEquals($courseinfo->groupmodeforce, $course2['groupmodeforce']);
$this->assertEquals($courseinfo->defaultgroupingid, $course2['defaultgroupingid']);
$this->assertEquals($courseinfo->completionnotify, $course2['completionnotify']);
$this->assertEquals($courseinfo->lang, $course2['lang']);
if (!empty($CFG->allowcoursethemes)) {
$this->assertEquals($dbcourse->theme, $course2['forcetheme']);
$this->assertEquals($courseinfo->theme, $course2['forcetheme']);
}
if (completion_info::is_enabled_for_site()) {
$this->assertEquals($dbcourse->enablecompletion, $course2['enabledcompletion']);
$this->assertEquals($dbcourse->completionstartonenrol, $course2['completionstartonenrol']);
$this->assertEquals($courseinfo->enablecompletion, $course2['enabledcompletion']);
$this->assertEquals($courseinfo->completionstartonenrol, $course2['completionstartonenrol']);
} else {
$this->assertEquals($dbcourse->enablecompletion, 0);
$this->assertEquals($dbcourse->completionstartonenrol, 0);
$this->assertEquals($courseinfo->enablecompletion, 0);
$this->assertEquals($courseinfo->completionstartonenrol, 0);
}
} else if ($createdcourse['shortname'] == $course1['shortname']) {
$courseconfig = get_config('moodlecourse');
$this->assertEquals($dbcourse->fullname, $course1['fullname']);
$this->assertEquals($dbcourse->shortname, $course1['shortname']);
$this->assertEquals($dbcourse->category, $course1['categoryid']);
$this->assertEquals($dbcourse->summaryformat, FORMAT_HTML);
$this->assertEquals($dbcourse->format, $courseconfig->format);
$this->assertEquals($dbcourse->showgrades, $courseconfig->showgrades);
$this->assertEquals($dbcourse->newsitems, $courseconfig->newsitems);
$this->assertEquals($dbcourse->maxbytes, $courseconfig->maxbytes);
$this->assertEquals($dbcourse->showreports, $courseconfig->showreports);
$this->assertEquals($dbcourse->groupmode, $courseconfig->groupmode);
$this->assertEquals($dbcourse->groupmodeforce, $courseconfig->groupmodeforce);
$this->assertEquals($dbcourse->defaultgroupingid, 0);
$this->assertEquals($courseinfo->fullname, $course1['fullname']);
$this->assertEquals($courseinfo->shortname, $course1['shortname']);
$this->assertEquals($courseinfo->category, $course1['categoryid']);
$this->assertEquals($courseinfo->summaryformat, FORMAT_HTML);
$this->assertEquals($courseinfo->format, $courseconfig->format);
$this->assertEquals($courseinfo->showgrades, $courseconfig->showgrades);
$this->assertEquals($courseinfo->newsitems, $courseconfig->newsitems);
$this->assertEquals($courseinfo->maxbytes, $courseconfig->maxbytes);
$this->assertEquals($courseinfo->showreports, $courseconfig->showreports);
$this->assertEquals($courseinfo->groupmode, $courseconfig->groupmode);
$this->assertEquals($courseinfo->groupmodeforce, $courseconfig->groupmodeforce);
$this->assertEquals($courseinfo->defaultgroupingid, 0);
} else if ($createdcourse['shortname'] == $course3['shortname']) {
$this->assertEquals($dbcourse->fullname, $course3['fullname']);
$this->assertEquals($dbcourse->shortname, $course3['shortname']);
$this->assertEquals($dbcourse->category, $course3['categoryid']);
$this->assertEquals($dbcourse->format, $course3['format']);
$this->assertEquals($dbcourse->hiddensections, $course3options['hiddensections']);
$this->assertEquals($dbcourse->numsections, $course3options['numsections']);
$this->assertEquals($dbcourse->coursedisplay, $course3options['coursedisplay']);
$this->assertEquals($courseinfo->fullname, $course3['fullname']);
$this->assertEquals($courseinfo->shortname, $course3['shortname']);
$this->assertEquals($courseinfo->category, $course3['categoryid']);
$this->assertEquals($courseinfo->format, $course3['format']);
$this->assertEquals($courseinfo->hiddensections, $course3options['hiddensections']);
$this->assertEquals($courseinfo->numsections, $course3options['numsections']);
$this->assertEquals($courseinfo->coursedisplay, $course3options['coursedisplay']);
} else {
throw moodle_exception('Unexpected shortname');
}
+2 -2
View File
@@ -684,8 +684,8 @@ class enrol_database_testcase extends advanced_testcase {
$course8['category'] = $defcat->id;
$record = $DB->get_record('course', $course8);
$this->assertFalse(empty($record));
$createdcourse = course_get_format($record)->get_course();
$this->assertEquals($createdcourse->numsections, 666);
$courseformatoptions = course_get_format($record)->get_format_options();
$this->assertEquals($courseformatoptions['numsections'], 666);
// Test invalid category.
+2 -2
View File
@@ -98,7 +98,7 @@
echo $OUTPUT->header();
/// Print Section or custom info
$site = course_get_format($SITE)->get_course();
$siteformatoptions = course_get_format($SITE)->get_format_options();
$modinfo = get_fast_modinfo($SITE);
$modnames = get_module_types_names();
$modnamesplural = get_module_types_names(true);
@@ -108,7 +108,7 @@
if (!empty($CFG->customfrontpageinclude)) {
include($CFG->customfrontpageinclude);
} else if ($site->numsections > 0) {
} else if ($siteformatoptions['numsections'] > 0) {
if ($editing) {
// make sure section with number 1 exists
course_create_sections_if_missing($SITE, 1);
+6 -3
View File
@@ -1886,12 +1886,15 @@ class global_navigation extends navigation_node {
global $CFG;
require_once($CFG->dirroot.'/course/lib.php');
$course = course_get_format($course)->get_course(); // retrieve all format options as well
$modinfo = get_fast_modinfo($course);
$sections = $modinfo->get_section_info_all();
if (isset($course->numsections)) {
$sections = array_slice($sections, 0, $course->numsections+1, true);
// For course formats using 'numsections' trim the sections list
$courseformatoptions = course_get_format($course)->get_format_options();
if (isset($courseformatoptions['numsections'])) {
$sections = array_slice($sections, 0, $courseformatoptions['numsections']+1, true);
}
$activities = array();
foreach ($sections as $key => $section) {
+6 -4
View File
@@ -2187,7 +2187,7 @@ function navmenulist($course, $sections, $modinfo, $strsection, $strjumpto, $wid
$menu = array();
$doneheading = false;
$course = course_get_format($course)->get_course();
$courseformatoptions = course_get_format($course)->get_format_options();
$coursecontext = context_course::instance($course->id);
$menu[] = '<ul class="navmenulist"><li class="jumpto section"><span>'.$strjumpto.'</span><ul>';
@@ -2197,7 +2197,8 @@ function navmenulist($course, $sections, $modinfo, $strsection, $strjumpto, $wid
continue;
}
if (isset($course->numsections) && $mod->sectionnum > $course->numsections) { /// Don't show excess hidden sections
// For course formats using 'numsections' do not show extra sections
if (isset($courseformatoptions['numsections']) && $mod->sectionnum > $courseformatoptions['numsections']) {
break;
}
@@ -2208,8 +2209,9 @@ function navmenulist($course, $sections, $modinfo, $strsection, $strjumpto, $wid
if ($mod->sectionnum >= 0 and $section != $mod->sectionnum) {
$thissection = $sections[$mod->sectionnum];
if ($thissection->visible or (isset($course->hiddensections) && !$course->hiddensections) or
has_capability('moodle/course:viewhiddensections', $coursecontext)) {
if ($thissection->visible or
(isset($courseformatoptions['hiddensections']) and !$courseformatoptions['hiddensections']) or
has_capability('moodle/course:viewhiddensections', $coursecontext)) {
$thissection->summary = strip_tags(format_string($thissection->summary,true));
if (!$doneheading) {
$menu[] = '</ul></li>';