Skip to content

Commit

Permalink
MDL-21724 course: ability to return when editing/creating
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Hemelryk committed Dec 2, 2014
1 parent d87bcfb commit 24d0d81
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 45 deletions.
2 changes: 1 addition & 1 deletion blocks/navigation/tests/behat/expand_courses_node.feature
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Feature: Expand the courses nodes within the navigation block
And I click on "Edit settings" "link" in the "Administration" "block"
And I set the following fields to these values:
| Allow guest access | Yes |
And I press "Save changes"
And I press "Save and return"
And I set the following administration settings values:
| Show all courses | 1 |
And I log out
Expand Down
2 changes: 1 addition & 1 deletion course/classes/management/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public static function get_course_listitem_actions(\coursecat $category, \course
// Edit.
if ($course->can_edit()) {
$actions[] = array(
'url' => new \moodle_url('/course/edit.php', array('id' => $course->id)),
'url' => new \moodle_url('/course/edit.php', array('id' => $course->id, 'returnto' => 'catmanage')),
'icon' => new \pix_icon('t/edit', \get_string('edit')),
'attributes' => array('class' => 'action-edit')
);
Expand Down
93 changes: 65 additions & 28 deletions course/edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,51 @@
$id = optional_param('id', 0, PARAM_INT); // Course id.
$categoryid = optional_param('category', 0, PARAM_INT); // Course category - can be changed in edit form.
$returnto = optional_param('returnto', 0, PARAM_ALPHANUM); // Generic navigation return page switch.
$returnurl = optional_param('returnurl', '', PARAM_LOCALURL); // A return URL. returnto must also be set to 'url'.

if ($returnto === 'url' && confirm_sesskey() && $returnurl) {
// If returnto is 'url' then $returnurl may be used as the destination to return to after saving or cancelling.
// Sesskey must be specified, and would be set by the form anyway.
$returnurl = new moodle_url($returnurl);
} else {
switch ($returnto) {
case 'category':
$returnurl = new moodle_url($CFG->wwwroot.'/course/index.php', array('categoryid' => $categoryid));
break;
case 'catmanage':
$returnurl = new moodle_url($CFG->wwwroot.'/course/management.php', array('categoryid' => $categoryid));
break;
case 'topcatmanage':
$returnurl = new moodle_url($CFG->wwwroot.'/course/management.php');
break;
case 'topcat':
$returnurl = new moodle_url($CFG->wwwroot.'/course/');
break;
case 'url':
// You may be wondering about this, but the url case should be dealt with above.
// If we get here than either sesskey was not given or returnurl was not provided.
default:
if (!empty($course->id)) {
$returnurl = new moodle_url($CFG->wwwroot.'/course/view.php', array('id' => $course->id));
} else {
$returnurl = new moodle_url($CFG->wwwroot.'/course/');
}
break;
}
}

$PAGE->set_pagelayout('admin');
if ($id) {
$pageparams = array('id' => $id);
} else {
$pageparams = array('category' => $categoryid);
}
if ($returnto !== 0) {
$pageparams['returnto'] = $returnto;
if ($returnto === 'url' && $returnurl) {
$pageparams['returnurl'] = $returnurl;
}
}
$PAGE->set_url('/course/edit.php', $pageparams);

// Basic access control checks.
Expand Down Expand Up @@ -98,31 +136,17 @@
}

// First create the form.
$editform = new course_edit_form(NULL, array('course'=>$course, 'category'=>$category, 'editoroptions'=>$editoroptions, 'returnto'=>$returnto));
$args = array(
'course' => $course,
'category' => $category,
'editoroptions' => $editoroptions,
'returnto' => $returnto,
'returnurl' => $returnurl
);
$editform = new course_edit_form(null, $args);
if ($editform->is_cancelled()) {
switch ($returnto) {
case 'category':
$url = new moodle_url($CFG->wwwroot.'/course/index.php', array('categoryid' => $categoryid));
break;
case 'catmanage':
$url = new moodle_url($CFG->wwwroot.'/course/management.php', array('categoryid' => $categoryid));
break;
case 'topcatmanage':
$url = new moodle_url($CFG->wwwroot.'/course/management.php');
break;
case 'topcat':
$url = new moodle_url($CFG->wwwroot.'/course/');
break;
default:
if (!empty($course->id)) {
$url = new moodle_url($CFG->wwwroot.'/course/view.php', array('id'=>$course->id));
} else {
$url = new moodle_url($CFG->wwwroot.'/course/');
}
break;
}
redirect($url);

// The form has been cancelled, take them back to what ever the return to is.
redirect($returnurl);
} else if ($data = $editform->get_data()) {
// Process data if submitted.
if (empty($course->id)) {
Expand All @@ -136,25 +160,38 @@
// Deal with course creators - enrol them internally with default role.
enrol_try_internal_enrol($course->id, $USER->id, $CFG->creatornewroleid);
}
if (!is_enrolled($context)) {

// The URL to take them to if they chose save and display.
$courseurl = new moodle_url('/course/view.php', array('id' => $course->id));

// If they choose to save and display, and they are not enrolled take them to the enrolments page instead.
if (!is_enrolled($context) && isset($data->saveanddisplay)) {
// Redirect to manual enrolment page if possible.
$instances = enrol_get_instances($course->id, true);
foreach($instances as $instance) {
if ($plugin = enrol_get_plugin($instance->enrol)) {
if ($plugin->get_manual_enrol_link($instance)) {
// We know that the ajax enrol UI will have an option to enrol.
redirect(new moodle_url('/enrol/users.php', array('id'=>$course->id)));
$courseurl = new moodle_url('/enrol/users.php', array('id' => $course->id));
break;
}
}
}
}
} else {
// Save any changes to the files used in the editor.
update_course($data, $editoroptions);
// Set the URL to take them too if they choose save and display.
$courseurl = new moodle_url('/course/view.php', array('id' => $course->id));
}

// Redirect user to newly created/updated course.
redirect(new moodle_url('/course/view.php', array('id' => $course->id)));
if (isset($data->saveanddisplay)) {
// Redirect user to newly created/updated course.
redirect($courseurl);
} else {
// Save and return. Take them back to wherever.
redirect($returnurl);
}
}

// Print the form.
Expand Down
15 changes: 14 additions & 1 deletion course/edit_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function definition() {
$category = $this->_customdata['category'];
$editoroptions = $this->_customdata['editoroptions'];
$returnto = $this->_customdata['returnto'];
$returnurl = $this->_customdata['returnurl'];

$systemcontext = context_system::instance();
$categorycontext = context_coursecat::instance($category->id);
Expand All @@ -51,6 +52,10 @@ function definition() {
$mform->setType('returnto', PARAM_ALPHANUM);
$mform->setConstant('returnto', $returnto);

$mform->addElement('hidden', 'returnurl', null);
$mform->setType('returnurl', PARAM_LOCALURL);
$mform->setConstant('returnurl', $returnurl);

$mform->addElement('text','fullname', get_string('fullnamecourse'),'maxlength="254" size="50"');
$mform->addHelpButton('fullname', 'fullnamecourse');
$mform->addRule('fullname', get_string('missingfullname'), 'required', null, 'client');
Expand Down Expand Up @@ -296,7 +301,15 @@ function definition() {
}
}

$this->add_action_buttons();
// When two elements we need a group.
$buttonarray = array();
if ($returnto !== 0) {
$buttonarray[] = &$mform->createElement('submit', 'saveandreturn', get_string('savechangesandreturn'));
}
$buttonarray[] = &$mform->createElement('submit', 'saveanddisplay', get_string('savechangesanddisplay'));
$buttonarray[] = &$mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
$mform->closeHeaderBefore('buttonar');

$mform->addElement('hidden', 'id', null);
$mform->setType('id', PARAM_INT);
Expand Down
2 changes: 1 addition & 1 deletion course/tests/behat/activities_group_icons.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Feature: Toggle activities groups mode from the course page
And I set the following fields to these values:
| Group mode | No groups |
| Force group mode | No |
When I press "Save changes"
When I press "Save and display"
Then "No groups (Click to change)" "link" should exist
And "//a/child::img[contains(@src, 'groupn')]" "xpath_element" should exist
And I click on "No groups (Click to change)" "link" in the "Test forum name" activity
Expand Down
2 changes: 1 addition & 1 deletion course/tests/behat/behat_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function i_create_a_course_with(TableNode $table) {
$steps[] = new Given('I set the following fields to these values:', $table);
}

$steps[] = new Given('I press "' . get_string('savechanges') . '"');
$steps[] = new Given('I press "' . get_string('savechangesanddisplay') . '"');

return $steps;
}
Expand Down
18 changes: 18 additions & 0 deletions course/tests/behat/course_creation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,21 @@ Feature: Managers can create courses
And I follow "News forum"
And "Add a new topic" "button" should not exist
And I should see "Forced subscription" in the "Administration" "block"

Scenario: Create a course from the management interface and return to it
Given the following "courses" exist:
| fullname | shortname | idnumber |
| Course 1 | Course 1 | C1 |
And I log in as "admin"
And I go to the courses management page
And I should see the "Categories" management page
And I click on category "Miscellaneous" in the management interface
And I should see the "Course categories and courses" management page
And I click on "Create new course" "link" in the "#course-listing" "css_element"
When I set the following fields to these values:
| Course full name | Course 2 |
| Course short name | Course 2 |
| Course summary | Course 2 summary |
And I press "Save and return"
Then I should see the "Course categories and courses" management page
And I should see course listing "Course 1" before "Course 2"
3 changes: 1 addition & 2 deletions course/tests/behat/create_delete_course.feature
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ Feature: Test we can both create and delete a course.
| Course short name | TCCAC |
| Course ID number | TC3401 |
| Course summary | This course has been created by automated tests. |
And I press "Save changes"
And I press "Save and return"
# Redirect
And I go to the courses management page
And I should see the "Course categories and courses" management page
And I click on category "Cat 1" in the management interface
# Redirect
Expand Down
22 changes: 21 additions & 1 deletion course/tests/behat/edit_settings.feature
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Feature: Edit course settings
| Course full name | Edited course fullname |
| Course short name | Edited course shortname |
| Course summary | Edited course summary |
And I press "Save changes"
And I press "Save and display"
And I follow "Edited course fullname"
Then I should not see "Course 1"
And I should not see "C1"
Expand All @@ -34,3 +34,23 @@ Feature: Edit course settings
And the field "Course summary" matches value "Edited course summary"
And I am on homepage
And I should see "Edited course fullname"

Scenario: Edit course settings and return to the management interface
Given the following "categories" exist:
| name | category | idnumber |
| Cat 1 | 0 | CAT1 |
And the following "courses" exist:
| category | fullname | shortname | idnumber |
| CAT1 | Course 1 | Course 1 | C1 |
And I log in as "admin"
And I go to the courses management page
And I should see the "Categories" management page
And I click on category "Cat 1" in the management interface
And I should see the "Course categories and courses" management page
When I click on "edit" action for "Course 1" in management course listing
And I set the following fields to these values:
| Course full name | Edited course fullname |
| Course short name | Edited course shortname |
| Course summary | Edited course summary |
And I press "Save and return"
Then I should see the "Course categories and courses" management page
6 changes: 3 additions & 3 deletions course/tests/behat/force_group_mode.feature
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Feature: Force group mode in a course
Given I set the following fields to these values:
| Group mode | Separate groups |
| Force group mode | Yes |
When I press "Save changes"
When I press "Save and display"
Then "//a/child::img[contains(@alt, 'Separate groups (forced mode)')]" "xpath_element" should not exist
And "//img[contains(@alt, 'Separate groups (forced mode)')]" "xpath_element" should not exist

Expand All @@ -36,7 +36,7 @@ Feature: Force group mode in a course
Given I set the following fields to these values:
| Group mode | Visible groups |
| Force group mode | Yes |
And I press "Save changes"
And I press "Save and display"
Then "//a/child::img[contains(@alt, 'Visible groups (forced mode)')]" "xpath_element" should not exist
And "//img[contains(@alt, 'Visible groups (forced mode)')]" "xpath_element" should not exist

Expand All @@ -45,7 +45,7 @@ Feature: Force group mode in a course
Given I set the following fields to these values:
| Group mode | No groups |
| Force group mode | Yes |
And I press "Save changes"
And I press "Save and display"
Then "//a/child::img[contains(@alt, 'No groups (forced mode)')]" "xpath_element" should not exist
And "//img[contains(@alt, 'No groups (forced mode)')]" "xpath_element" should not exist

4 changes: 2 additions & 2 deletions course/tests/behat/move_activities.feature
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Feature: Activities can be moved between sections
Given I click on "Edit settings" "link" in the "Administration" "block"
And I set the following fields to these values:
| Course layout | Show one section per page |
And I press "Save changes"
And I press "Save and display"
When I move "Test forum name" activity to section "2"
Then I should see "Test forum name" in the "#section-2" "css_element"
And I should not see "Test forum name" in the "#section-1" "css_element"
Expand All @@ -45,7 +45,7 @@ Feature: Activities can be moved between sections
Given I click on "Edit settings" "link" in the "Administration" "block"
And I set the following fields to these values:
| Course layout | Show one section per page |
And I press "Save changes"
And I press "Save and display"
And I add a "Forum" to section "1" and I fill the form with:
| Forum name | Second forum name |
| Description | Second forum description |
Expand Down
4 changes: 2 additions & 2 deletions course/tests/behat/move_sections.feature
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Feature: Sections can be moved
Given I click on "Edit settings" "link" in the "Administration" "block"
And I set the following fields to these values:
| Course layout | Show one section per page |
And I press "Save changes"
And I press "Save and display"
And I add a "Forum" to section "1" and I fill the form with:
| Forum name | Test forum name |
| Description | Test forum description |
Expand All @@ -44,7 +44,7 @@ Feature: Sections can be moved
Given I click on "Edit settings" "link" in the "Administration" "block"
And I set the following fields to these values:
| Course layout | Show one section per page |
And I press "Save changes"
And I press "Save and display"
And I add a "Forum" to section "2" and I fill the form with:
| Forum name | Test forum name |
| Description | Test forum description |
Expand Down
4 changes: 2 additions & 2 deletions course/tests/behat/rename_roles.feature
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Feature: Rename roles within a course
And I set the following fields to these values:
| Your word for 'Non-editing teacher' | Tutor |
| Your word for 'Student' | Learner |
And I press "Save changes"
And I press "Save and display"
And I expand "Switch role to..." node
Then I should see "Tutor"
And I should see "Learner"
Expand All @@ -37,7 +37,7 @@ Feature: Rename roles within a course
And I set the following fields to these values:
| Your word for 'Non-editing teacher' | |
| Your word for 'Student' | |
And I press "Save changes"
And I press "Save and display"
And I expand "Switch role to..." node
And I should see "Teacher"
And I should see "Student"
Expand Down
1 change: 1 addition & 0 deletions lang/en/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -1567,6 +1567,7 @@
$string['savedat'] = 'Saved at:';
$string['savechanges'] = 'Save changes';
$string['savechangesanddisplay'] = 'Save and display';
$string['savechangesandreturn'] = 'Save and return';
$string['savechangesandreturntocourse'] = 'Save and return to course';
$string['savecomment'] = 'Save comment';
$string['savepreferences'] = 'Save preferences';
Expand Down

0 comments on commit 24d0d81

Please sign in to comment.