Skip to content

Commit

Permalink
MDL-36017 Fields numsections, hiddensections and coursedisplay are no…
Browse files Browse the repository at this point in the history
…w format-specific options

- Fields added to format_legacy as default course format options;
- Upgrade script copies fields values from table course to course_format_options;
- Fields removed from table course;
- Fields removed from edit course form;
- Since front-page course has a 'numsections' setting, format_site defines it as it's option;
- Removed accessing those fields in core code unless we know that format supports them and in this
  case instead of $course = $DB->get_record('course'); we use:
  $course = course_get_format($courseorid)->get_course(); This way all format-specific options
  are added to the $course object
  • Loading branch information
marinaglancy committed Nov 2, 2012
1 parent 7b7d2f4 commit b5cf83f
Show file tree
Hide file tree
Showing 24 changed files with 197 additions and 100 deletions.
6 changes: 6 additions & 0 deletions admin/oacleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ 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)) {
// Course format does not use numsections
if ($output) echo 'No extra sections<br />';
continue;
}
$sql = "SELECT *
FROM {course_sections}
WHERE course=? AND section>?
Expand Down
6 changes: 3 additions & 3 deletions backup/moodle2/backup_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,10 +437,10 @@ protected function define_structure() {

$course = new backup_nested_element('course', array('id', 'contextid'), array(
'shortname', 'fullname', 'idnumber',
'summary', 'summaryformat', 'format', 'coursedisplay', 'showgrades',
'newsitems', 'startdate', 'numsections',
'summary', 'summaryformat', 'format', 'showgrades',
'newsitems', 'startdate',
'marker', 'maxbytes', 'legacyfiles', 'showreports',
'visible', 'hiddensections', 'groupmode', 'groupmodeforce',
'visible', 'groupmode', 'groupmodeforce',
'defaultgroupingid', 'lang', 'theme',
'timecreated', 'timemodified',
'requested',
Expand Down
2 changes: 1 addition & 1 deletion blocks/section_links/block_section_links.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function get_content() {
return $this->content;
}

$course = $this->page->course;
$course = course_get_format($this->page->course)->get_course();
$context = context_course::instance($course->id);

if ($course->format == 'weeks' or $course->format == 'weekscss') {
Expand Down
28 changes: 15 additions & 13 deletions course/changenumsections.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

$courseid = required_param('courseid', PARAM_INT);
$increase = optional_param('increase', true, PARAM_BOOL);
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
$course = course_get_format($courseid)->get_course();

$PAGE->set_url('/course/changenumsections.php', array('courseid' => $courseid));

Expand All @@ -39,18 +39,20 @@
require_capability('moodle/course:update', context_course::instance($course->id));
require_sesskey();

if ($increase) {
// Add an additional section.
$course->numsections++;
} else {
// Remove a section.
$course->numsections--;
}

// Don't go less than 0, intentionally redirect silently (for the case of
// double clicks).
if ($course->numsections >= 0) {
$DB->update_record('course', $course);
if (isset($course->numsections)) {
if ($increase) {
// Add an additional section.
$course->numsections++;
} else {
// Remove a section.
$course->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));
}
}

$url = course_get_url($course);
Expand Down
19 changes: 0 additions & 19 deletions course/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,10 @@ function definition() {
$mform->registerNoSubmitButton('updatecourseformat');
$mform->addElement('submit', 'updatecourseformat', get_string('courseformatudpate'));

$mform->addElement('select', 'coursedisplay', get_string('coursedisplay'),
array(COURSE_DISPLAY_SINGLEPAGE => get_string('coursedisplay_single'),
COURSE_DISPLAY_MULTIPAGE => get_string('coursedisplay_multi')));
$mform->addHelpButton('coursedisplay', 'coursedisplay');
$mform->setDefault('coursedisplay', $courseconfig->coursedisplay);

for ($i = 0; $i <= $courseconfig->maxsections; $i++) {
$sectionmenu[$i] = "$i";
}
$mform->addElement('select', 'numsections', get_string('numberweeks'), $sectionmenu);
$mform->setDefault('numsections', $courseconfig->numsections);

$mform->addElement('date_selector', 'startdate', get_string('startdate'));
$mform->addHelpButton('startdate', 'startdate');
$mform->setDefault('startdate', time() + 3600 * 24);

$choices = array();
$choices['0'] = get_string('hiddensectionscollapsed');
$choices['1'] = get_string('hiddensectionsinvisible');
$mform->addElement('select', 'hiddensections', get_string('hiddensections'), $choices);
$mform->addHelpButton('hiddensections', 'hiddensections');
$mform->setDefault('hiddensections', $courseconfig->hiddensections);

$options = range(0, 10);
$mform->addElement('select', 'newsitems', get_string('newsitemsnumber'), $options);
$mform->addHelpButton('newsitems', 'newsitemsnumber');
Expand Down
42 changes: 40 additions & 2 deletions course/format/formatlegacy.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,46 @@ public function get_view_url($section, $options = array()) {
}
}

// else, default behavior:
return parent::get_view_url($section, $options);
// if function is not defined
if (!$this->uses_sections() ||
!array_key_exists('coursedisplay', $this->course_format_options())) {
// default behaviour
return parent::get_view_url($section, $options);
}

$course = $this->get_course();
$url = new moodle_url('/course/view.php', array('id' => $course->id));

$sr = null;
if (array_key_exists('sr', $options)) {
$sr = $options['sr'];
}
if (is_object($section)) {
$sectionno = $section->section;
} else {
$sectionno = $section;
}
if ($sectionno !== null) {
if ($sr !== null) {
if ($sr) {
$usercoursedisplay = COURSE_DISPLAY_MULTIPAGE;
$sectionno = $sr;
} else {
$usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE;
}
} else {
$usercoursedisplay = $course->coursedisplay;
}
if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) {
$url->param('section', $sectionno);
} else {
if (!empty($options['navigation'])) {
return null;
}
$url->set_anchor('section-'.$sectionno);
}
}
return $url;
}

/**
Expand Down
63 changes: 38 additions & 25 deletions course/format/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,35 +355,20 @@ public function ajax_section_move() {
public function get_view_url($section, $options = array()) {
$course = $this->get_course();
$url = new moodle_url('/course/view.php', array('id' => $course->id));

$sr = null;

if (array_key_exists('sr', $options)) {
$sr = $options['sr'];
}
if (is_object($section)) {
$sectionno = $options['sr'];
} else if (is_object($section)) {
$sectionno = $section->section;
} else {
$sectionno = $section;
}
if ($sectionno !== null) {
if ($sr !== null) {
if ($sr) {
$usercoursedisplay = COURSE_DISPLAY_MULTIPAGE;
$sectionno = $sr;
} else {
$usercoursedisplay = COURSE_DISPLAY_SINGLEPAGE;
}
} else {
$usercoursedisplay = $course->coursedisplay;
}
if ($sectionno != 0 && $usercoursedisplay == COURSE_DISPLAY_MULTIPAGE) {
$url->param('section', $sectionno);
} else {
if (!empty($options['navigation'])) {
return null;
}
$url->set_anchor('section-'.$sectionno);
}
if (!empty($options['navigation']) && $sectionno !== null) {
// by default assume that sections are never displayed on separate pages
return null;
}
if ($this->uses_sections() && $sectionno !== null) {
$url->set_anchor('section-'.$sectionno);
}
return $url;
}
Expand Down Expand Up @@ -796,5 +781,33 @@ function get_section_name($section) {
public function get_view_url($section, $options = array()) {
return new moodle_url('/');
}
}

/**
* Returns the list of blocks to be automatically added on the site frontpage when moodle is installed
*
* @return array of default blocks, must contain two keys BLOCK_POS_LEFT and BLOCK_POS_RIGHT
* each of values is an array of block names (for left and right side columns)
*/
public function get_default_blocks() {
return blocks_get_default_site_course_blocks();
}

/**
* Definitions of the additional options that site uses
*
* @param bool $foreditform
* @return array of options
*/
public function course_format_options($foreditform = false) {
static $courseformatoptions = false;
if ($courseformatoptions === false) {
$courseformatoptions = array(
'numsections' => array(
'default' => 1,
'type' => PARAM_INT,
),
);
}
return $courseformatoptions;
}
}
3 changes: 3 additions & 0 deletions course/format/renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ protected function course_activity_clipboard($course, $sectionno = null) {
*/
protected function get_nav_links($course, $sections, $sectionno) {
// FIXME: This is really evil and should by using the navigation API.
$course = course_get_format($course)->get_course();
$canviewhidden = has_capability('moodle/course:viewhiddensections', context_course::instance($course->id))
or !$course->hiddensections;

Expand Down Expand Up @@ -542,6 +543,7 @@ public function print_single_section_page($course, $sections, $mods, $modnames,
global $PAGE;

$modinfo = get_fast_modinfo($course);
$course = course_get_format($course)->get_course();

// Can we view the section in question?
if (!($sectioninfo = $modinfo->get_section_info($displaysection))) {
Expand Down Expand Up @@ -637,6 +639,7 @@ public function print_multiple_section_page($course, $sections, $mods, $modnames
global $PAGE;

$modinfo = get_fast_modinfo($course);
$course = course_get_format($course)->get_course();

$context = context_course::instance($course->id);
// Title with completion help icon.
Expand Down
1 change: 1 addition & 0 deletions course/format/topics/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
}

// make sure all sections are created
$course = course_get_format($course)->get_course();
course_create_sections_if_missing($course, range(0, $course->numsections));

$renderer = $PAGE->get_renderer('format_topics');
Expand Down
1 change: 1 addition & 0 deletions course/format/weeks/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
// End backwards-compatible aliasing..

// make sure all sections are created
$course = course_get_format($course)->get_course();
course_create_sections_if_missing($course, range(0, $course->numsections));

$renderer = $PAGE->get_renderer('format_weeks');
Expand Down
8 changes: 4 additions & 4 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2939,7 +2939,8 @@ function move_section($course, $section, $move) {

$sectiondest = $section + $move;

if ($sectiondest > $course->numsections or $sectiondest < 1) {
$course = course_get_format($course)->get_course();
if (isset($course->numsections) && $sectiondest > $course->numsections or $sectiondest < 1) {
return false;
}

Expand All @@ -2965,7 +2966,8 @@ function move_section_to($course, $section, $destination) {
return true;
}

if (($destination > $course->numsections) || ($destination < 1)) {
$course = course_get_format($course)->get_course();
if ((isset($course->numsections) && ($destination > $course->numsections)) || ($destination < 1)) {
return false;
}

Expand Down Expand Up @@ -4232,8 +4234,6 @@ public function approve() {

// Apply course default settings
$data->format = $courseconfig->format;
$data->numsections = $courseconfig->numsections;
$data->hiddensections = $courseconfig->hiddensections;
$data->newsitems = $courseconfig->newsitems;
$data->showgrades = $courseconfig->showgrades;
$data->showreports = $courseconfig->showreports;
Expand Down
3 changes: 1 addition & 2 deletions enrol/database/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,7 @@ public function sync_courses($verbose = false) {
$template = false;
if ($templatecourse) {
if ($template = $DB->get_record('course', array('shortname'=>$templatecourse))) {
$template = fullclone(course_get_format($template)->get_course());
unset($template->id);
unset($template->fullname);
unset($template->shortname);
Expand All @@ -769,8 +770,6 @@ public function sync_courses($verbose = false) {
$template->summary = '';
$template->summaryformat = FORMAT_HTML;
$template->format = $courseconfig->format;
$template->numsections = $courseconfig->numsections;
$template->hiddensections = $courseconfig->hiddensections;
$template->newsitems = $courseconfig->newsitems;
$template->showgrades = $courseconfig->showgrades;
$template->showreports = $courseconfig->showreports;
Expand Down
7 changes: 4 additions & 3 deletions enrol/database/tests/sync_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,9 +682,10 @@ public function test_sync_courses() {

$this->assertEquals(2+1+4+1+count(self::$courses), $DB->count_records('course'));
$course8['category'] = $defcat->id;
$course8['numsections'] = 666;
$this->assertTrue($DB->record_exists('course', $course8));

$record = $DB->get_record('course', $course8);
$this->assertFalse(empty($record));
$createdcourse = course_get_format($record)->get_course();
$this->assertEquals($createdcourse->numsections, 666);

// Test invalid category.

Expand Down
2 changes: 0 additions & 2 deletions enrol/imsenterprise/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,6 @@ function process_group_tag($tagcontents) {
$course->idnumber = $coursecode;
$course->format = $courseconfig->format;
$course->visible = $courseconfig->visible;
$course->numsections = $courseconfig->numsections;
$course->hiddensections = $courseconfig->hiddensections;
$course->newsitems = $courseconfig->newsitems;
$course->showgrades = $courseconfig->showgrades;
$course->showreports = $courseconfig->showreports;
Expand Down
2 changes: 0 additions & 2 deletions enrol/ldap/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -900,8 +900,6 @@ function create_course($course_ext, $skip_fix_course_sortorder=false) {
$template->summary = '';
$template->summaryformat = FORMAT_HTML;
$template->format = $courseconfig->format;
$template->numsections = $courseconfig->numsections;
$template->hiddensections = $courseconfig->hiddensections;
$template->newsitems = $courseconfig->newsitems;
$template->showgrades = $courseconfig->showgrades;
$template->showreports = $courseconfig->showreports;
Expand Down
3 changes: 2 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
echo $OUTPUT->header();

/// Print Section or custom info
$site = course_get_format($SITE)->get_course();
$modinfo = get_fast_modinfo($SITE);
$modnames = get_module_types_names();
$modnamesplural = get_module_types_names(true);
Expand All @@ -107,7 +108,7 @@
if (!empty($CFG->customfrontpageinclude)) {
include($CFG->customfrontpageinclude);

} else {
} else if ($site->numsections > 0) {
if ($editing) {
// make sure section with number 1 exists
course_create_sections_if_missing($SITE, 1);
Expand Down
Loading

0 comments on commit b5cf83f

Please sign in to comment.