Skip to content

Commit

Permalink
MDL-35768 MDL-36017 Avoid calling format_base::get_course() when not …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
marinaglancy committed Nov 5, 2012
1 parent 92d3866 commit 850acb3
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 77 deletions.
8 changes: 4 additions & 4 deletions admin/oacleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 />';
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 8 additions & 7 deletions blocks/section_links/block_section_links.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ function get_content() {
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') {
Expand All @@ -86,31 +87,31 @@ function get_content() {
}
$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;
}
}

$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;
}
Expand Down
14 changes: 8 additions & 6 deletions course/changenumsections.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@

$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));

Expand All @@ -39,19 +40,20 @@
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']));
}
}

Expand Down
12 changes: 8 additions & 4 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
90 changes: 45 additions & 45 deletions course/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -359,64 +359,64 @@ public function test_create_courses() {

// 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');
}
Expand Down
4 changes: 2 additions & 2 deletions enrol/database/tests/sync_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,8 @@ public function test_sync_courses() {
$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.

Expand Down
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
9 changes: 6 additions & 3 deletions lib/navigationlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1886,12 +1886,15 @@ protected function generate_sections_and_activities(stdClass $course) {
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) {
Expand Down
10 changes: 6 additions & 4 deletions lib/weblib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>';
Expand All @@ -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;
}

Expand All @@ -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>';
Expand Down

0 comments on commit 850acb3

Please sign in to comment.