Skip to content

Commit

Permalink
MDL-58837 course: allow to add multiple sections
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed May 11, 2017
1 parent b166037 commit f24e17c
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 15 deletions.
2 changes: 1 addition & 1 deletion course/amd/build/actions.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 43 additions & 3 deletions course/amd/src/actions.js
Expand Up @@ -22,8 +22,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since 3.3
*/
define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str', 'core/url', 'core/yui'],
function($, ajax, templates, notification, str, url, Y) {
define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str', 'core/url', 'core/yui',
'core/modal_factory', 'core/modal_events', 'core/key_codes'],
function($, ajax, templates, notification, str, url, Y, ModalFactory, ModalEvents, KeyCodes) {
var CSS = {
EDITINPROGRESS: 'editinprogress',
SECTIONDRAGGABLE: 'sectiondraggable',
Expand All @@ -36,7 +37,8 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'
MENU: '.moodle-actionmenu[data-enhance=moodle-core-actionmenu]',
TOGGLE: '.toggle-display,.dropdown-toggle',
SECTIONLI: 'li.section',
SECTIONACTIONMENU: '.section_action_menu'
SECTIONACTIONMENU: '.section_action_menu',
ADDSECTIONS: '#changenumsections [data-add-sections]'
};

Y.use('moodle-course-coursebase', function() {
Expand Down Expand Up @@ -576,6 +578,44 @@ define(['jquery', 'core/ajax', 'core/templates', 'core/notification', 'core/str'
editSection(sectionElement, sectionId, actionItem, courseformat);
}
});

// Add a handler for "Add sections" link to ask for a number of sections to add.
str.get_string('numberweeks').done(function(strNumberSections) {
var trigger = $(SELECTOR.ADDSECTIONS),
modalTitle = trigger.attr('data-add-sections');
var modalBody = $('<div><label for="add_section_numsections"></label> ' +
'<input id="add_section_numsections" type="number" min="1" value="1"></div>');
modalBody.find('label').html(strNumberSections);
ModalFactory.create({
title: modalTitle,
type: ModalFactory.types.SAVE_CANCEL,
body: modalBody.html()
}, trigger)
.done(function(modal) {
var numSections = $(modal.getBody()).find('#add_section_numsections'),
addSections = function() {
// Check if value of the "Number of sections" is a valid positive integer and redirect
// to adding a section script.
if ('' + parseInt(numSections.val()) === numSections.val() && parseInt(numSections.val()) >= 1) {
document.location = trigger.attr('href') + '&numsections=' + parseInt(numSections.val());
}
};
modal.setSaveButtonText(modalTitle);
modal.getRoot().on(ModalEvents.shown, function() {
// When modal is shown focus and select the input and add a listener to keypress of "Enter".
numSections.focus().select().on('keydown', function(e) {
if (e.keyCode === KeyCodes.enter) {
addSections();
}
});
});
modal.getRoot().on(ModalEvents.save, function(e) {
// When modal "Add" button is pressed.
e.preventDefault();
addSections();
});
});
});
},

/**
Expand Down
8 changes: 6 additions & 2 deletions course/changenumsections.php
Expand Up @@ -31,6 +31,7 @@
$courseid = required_param('courseid', PARAM_INT);
$increase = optional_param('increase', null, PARAM_BOOL);
$insertsection = optional_param('insertsection', null, PARAM_INT); // Insert section at position; 0 means at the end.
$numsections = optional_param('numsections', 1, PARAM_INT); // Number of sections to insert.
$returnurl = optional_param('returnurl', null, PARAM_LOCALURL); // Where to return to after the action.
$sectionreturn = optional_param('sectionreturn', null, PARAM_INT); // Section to return to, ignored if $returnurl is specified.

Expand Down Expand Up @@ -70,9 +71,12 @@
// Inserting sections at any position except in the very end requires capability to move sections.
require_capability('moodle/course:movesections', context_course::instance($course->id));
}
$section = course_create_section($course, $insertsection);
$sections = [];
for ($i = 0; $i < max($numsections, 1); $i ++) {
$sections[] = course_create_section($course, $insertsection);
}
if (!$returnurl) {
$returnurl = course_get_url($course, $section->section,
$returnurl = course_get_url($course, $sections[0]->section,
($sectionreturn !== null) ? ['sr' => $sectionreturn] : []);
}
}
Expand Down

0 comments on commit f24e17c

Please sign in to comment.