Commit 5dc361e1 authored by Sam Hemelryk's avatar Sam Hemelryk
Browse files

MDL-31830 core_course: new course category management interface

* Implemented a new course and category management page.
* Deprecated the old management interface.
* Unit tests to cover the new API and core functions.
* Behat tests to cover the new interface.
parent 56cc9b38
......@@ -29,6 +29,7 @@ define("MAX_USERS_TO_LIST_PER_ROLE", 10);
$contextid = required_param('contextid', PARAM_INT);
$roleid = optional_param('roleid', 0, PARAM_INT);
$returnto = optional_param('return', null, PARAM_ALPHANUMEXT);
list($context, $course, $cm) = get_context_info_array($contextid);
......@@ -181,6 +182,9 @@ if ($roleid) {
// Print the form.
$assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
if ($returnto !== null) {
$assignurl->param('return', $returnto);
}
?>
<form id="assignform" method="post" action="<?php echo $assignurl ?>"><div>
<input type="hidden" name="sesskey" value="<?php echo sesskey() ?>" />
......@@ -225,10 +229,17 @@ if ($roleid) {
// Print a form to swap roles, and a link back to the all roles list.
echo '<div class="backlink">';
$select = new single_select($PAGE->url, 'roleid', $nameswithcounts, $roleid, null);
$newroleurl = new moodle_url($PAGE->url);
if ($returnto !== null) {
$newroleurl->param('return', $returnto);
}
$select = new single_select($newroleurl, 'roleid', $nameswithcounts, $roleid, null);
$select->label = get_string('assignanotherrole', 'core_role');
echo $OUTPUT->render($select);
$backurl = new moodle_url('/admin/roles/assign.php', array('contextid' => $contextid));
if ($returnto !== null) {
$backurl->param('return', $returnto);
}
echo '<p><a href="' . $backurl->out() . '">' . get_string('backtoallroles', 'core_role') . '</a></p>';
echo '</div>';
......@@ -266,6 +277,9 @@ if ($roleid) {
}
} else if ($assigncounts[$roleid] > MAX_USERS_TO_LIST_PER_ROLE) {
$assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
if ($returnto !== null) {
$assignurl->param('return', $returnto);
}
$roleholdernames[$roleid] = '<a href="'.$assignurl.'">'.$strmorethanmax.'</a>';
} else {
$roleholdernames[$roleid] = '';
......@@ -286,6 +300,9 @@ if ($roleid) {
foreach ($assignableroles as $roleid => $rolename) {
$description = format_string($DB->get_field('role', 'description', array('id'=>$roleid)));
$assignurl = new moodle_url($PAGE->url, array('roleid'=>$roleid));
if ($returnto !== null) {
$assignurl->param('return', $returnto);
}
$row = array('<a href="'.$assignurl.'">'.$rolename.'</a>',
$description, $assigncounts[$roleid]);
if ($showroleholders) {
......@@ -297,8 +314,15 @@ if ($roleid) {
echo html_writer::table($table);
if ($context->contextlevel > CONTEXT_USER) {
if ($context->contextlevel === CONTEXT_COURSECAT && $returnto === 'management') {
$url = new moodle_url('/course/management.php', array('categoryid' => $context->instanceid));
} else {
$url = $context->get_url();
}
echo html_writer::start_tag('div', array('class'=>'backlink'));
echo html_writer::tag('a', get_string('backto', '', $contextname), array('href'=>$context->get_url()));
echo html_writer::tag('a', get_string('backto', '', $contextname), array('href' => $url));
echo html_writer::end_tag('div');
}
}
......
......@@ -33,6 +33,7 @@ $prevent = optional_param('prevent', 0, PARAM_BOOL);
$allow = optional_param('allow', 0, PARAM_BOOL);
$unprohibit = optional_param('unprohibit', 0, PARAM_BOOL);
$prohibit = optional_param('prohibit', 0, PARAM_BOOL);
$return = optional_param('return', null, PARAM_ALPHANUMEXT);
list($context, $course, $cm) = get_context_info_array($contextid);
......@@ -201,8 +202,15 @@ echo $OUTPUT->box_end();
if ($context->contextlevel > CONTEXT_USER) {
if ($context->contextlevel === CONTEXT_COURSECAT && $return === 'management') {
$url = new moodle_url('/course/management.php', array('categoryid' => $context->instanceid));
} else {
$url = $context->get_url();
}
echo html_writer::start_tag('div', array('class'=>'backlink'));
echo html_writer::tag('a', get_string('backto', '', $contextname), array('href'=>$context->get_url()));
echo html_writer::tag('a', get_string('backto', '', $contextname), array('href' => $url));
echo html_writer::end_tag('div');
}
......
<?php
// This file defines settingpages and externalpages under the "courses" category
if ($hassiteconfig
or has_capability('moodle/backup:backupcourse', $systemcontext)
or has_capability('moodle/category:manage', $systemcontext)
or has_capability('moodle/course:create', $systemcontext)
or has_capability('moodle/site:approvecourse', $systemcontext)) { // speedup for non-admins, add all caps used on this page
$ADMIN->add('courses', new admin_externalpage('coursemgmt', new lang_string('coursemgmt', 'admin'), $CFG->wwwroot . '/course/manage.php',
array('moodle/category:manage', 'moodle/course:create')));
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* This file defines settingpages and externalpages under the "courses" category
*
* @package core
* @copyright 2002 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
$capabilities = array(
'moodle/backup:backupcourse',
'moodle/category:manage',
'moodle/course:create',
'moodle/site:approvecourse'
);
if ($hassiteconfig or has_any_capability($capabilities, $systemcontext)) {
// Speedup for non-admins, add all caps used on this page.
$ADMIN->add('courses',
new admin_externalpage('coursemgmt', new lang_string('coursemgmt', 'admin'),
$CFG->wwwroot . '/course/management.php',
array('moodle/category:manage', 'moodle/course:create')
)
);
$ADMIN->add('courses',
new admin_externalpage('addcategory', new lang_string('addcategory', 'admin'),
new moodle_url('/course/editcategory.php', array('parent' => 0)),
array('moodle/category:manage')
)
);
// Course Default Settings Page.
// NOTE: these settings must be applied after all other settings because they depend on them.
// Main course settings.
$temp = new admin_settingpage('coursesettings', new lang_string('coursesettings'));
require_once($CFG->dirroot.'/course/lib.php');
......@@ -25,7 +56,6 @@ if ($hassiteconfig
$temp->add(new admin_setting_configselect('moodlecourse/visible', new lang_string('visible'), new lang_string('visible_help'),
1, $choices));
// Course format.
$temp->add(new admin_setting_heading('courseformathdr', new lang_string('type_format', 'plugin'), ''));
......@@ -55,7 +85,6 @@ if ($hassiteconfig
$temp->add(new admin_setting_configselect('moodlecourse/coursedisplay', new lang_string('coursedisplay'),
new lang_string('coursedisplay_help'), COURSE_DISPLAY_SINGLEPAGE, $choices));
// Appearance.
$temp->add(new admin_setting_heading('appearancehdr', new lang_string('appearance'), ''));
......@@ -73,7 +102,6 @@ if ($hassiteconfig
$temp->add(new admin_setting_configselect('moodlecourse/showreports', new lang_string('showreports'), '', 0,
array(0 => new lang_string('no'), 1 => new lang_string('yes'))));
// Files and uploads.
$temp->add(new admin_setting_heading('filesanduploadshdr', new lang_string('filesanduploads'), ''));
......@@ -92,13 +120,11 @@ if ($hassiteconfig
$temp->add(new admin_setting_configselect('moodlecourse/maxbytes', new lang_string('maximumupload'),
new lang_string('coursehelpmaximumupload'), key($choices), $choices));
// Completion tracking.
$temp->add(new admin_setting_heading('progress', new lang_string('completion','completion'), ''));
$temp->add(new admin_setting_configselect('moodlecourse/enablecompletion', new lang_string('completion', 'completion'),
new lang_string('enablecompletion_help', 'completion'), 0, array(0 => new lang_string('no'), 1 => new lang_string('yes'))));
// Groups.
$temp->add(new admin_setting_heading('groups', new lang_string('groups', 'group'), ''));
$choices = array();
......@@ -119,13 +145,13 @@ if ($hassiteconfig
$temp->add(new admin_setting_users_with_capability('courserequestnotify', new lang_string('courserequestnotify', 'admin'), new lang_string('configcourserequestnotify2', 'admin'), array(), 'moodle/site:approvecourse'));
$ADMIN->add('courses', $temp);
/// Pending course requests.
// Pending course requests.
if (!empty($CFG->enablecourserequests)) {
$ADMIN->add('courses', new admin_externalpage('coursespending', new lang_string('pendingrequests'),
$CFG->wwwroot . '/course/pending.php', array('moodle/site:approvecourse')));
}
// Add a category for backups
// Add a category for backups.
$ADMIN->add('courses', new admin_category('backups', new lang_string('backups','admin')));
// Create a page for general backups configuration and defaults.
......@@ -239,10 +265,8 @@ if ($hassiteconfig
$temp->add(new admin_setting_configcheckbox('backup/backup_auto_histories', new lang_string('generalhistories','backup'), new lang_string('configgeneralhistories','backup'), 0));
$temp->add(new admin_setting_configcheckbox('backup/backup_auto_questionbank', new lang_string('generalquestionbank','backup'), new lang_string('configgeneralquestionbank','backup'), 1));
//$temp->add(new admin_setting_configcheckbox('backup/backup_auto_messages', new lang_string('messages', 'message'), new lang_string('backupmessageshelp','message'), 0));
//$temp->add(new admin_setting_configcheckbox('backup/backup_auto_blogs', new lang_string('blogs', 'blog'), new lang_string('backupblogshelp','blog'), 0));
$ADMIN->add('backups', $temp);
} // end of speedup
}
......@@ -37,9 +37,9 @@ Feature: Set up contextual data for tests
And I follow "Cat 3"
And I should see "Course 1"
And I should see "Course 2"
And I select "Cat 1 / Cat 2" from "Course categories:"
And I follow "Cat 2"
And I should see "No courses in this category"
And I select "Miscellaneous" from "Course categories:"
And I follow "Miscellaneous"
And I should see "Course 3"
@javascript
......
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Performs course category management ajax actions.
*
* Please note functions may throw exceptions, please ensure your JS handles them as well as the outcome objects.
*
* @package core_course
* @copyright 2013 Sam Hemelryk
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('AJAX_SCRIPT', true);
require_once('../../config.php');
require_once($CFG->dirroot.'/lib/coursecatlib.php');
require_once($CFG->dirroot.'/course/lib.php');
$action = required_param('action', PARAM_ALPHA);
require_sesskey(); // Gotta have the sesskey.
require_login(); // Gotta be logged in (of course).
// Prepare an outcome object. We always use this.
$outcome = new stdClass;
$outcome->error = false;
$outcome->outcome = false;
echo $OUTPUT->header();
switch ($action) {
case 'movecourseup' :
$courseid = required_param('courseid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_course_moveup_by_record($courseid);
break;
case 'movecoursedown' :
$courseid = required_param('courseid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_course_movedown_by_record($courseid);
break;
case 'movecourseintocategory':
$courseid = required_param('courseid', PARAM_INT);
$categoryid = required_param('categoryid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::move_courses_into_category($categoryid, $courseid);
break;
case 'movecourseafter' :
$courseid = required_param('courseid', PARAM_INT);
$moveaftercourseid = required_param('moveafter', PARAM_INT);
$outcome->outcome = course_move_after_course($courseid, $moveaftercourseid);
break;
case 'hidecourse' :
$courseid = required_param('courseid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_course_hide_by_record($courseid);
break;
case 'showcourse' :
$courseid = required_param('courseid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_course_show_by_record($courseid);
break;
case 'movecategoryup' :
$categoryid = required_param('categoryid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_category_moveup_by_id($categoryid);
break;
case 'movecategorydown' :
$categoryid = required_param('categoryid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_category_movedown_by_id($categoryid);
break;
case 'hidecategory' :
$categoryid = required_param('categoryid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_category_hide_by_id($categoryid);
$outcome->categoryvisibility = \core_course\management\helper::get_category_children_visibility($categoryid);
$outcome->coursevisibility = \core_course\management\helper::get_category_courses_visibility($categoryid);
break;
case 'showcategory' :
$categoryid = required_param('categoryid', PARAM_INT);
$outcome->outcome = \core_course\management\helper::action_category_show_by_id($categoryid);
$outcome->categoryvisibility = \core_course\management\helper::get_category_children_visibility($categoryid);
$outcome->coursevisibility = \core_course\management\helper::get_category_courses_visibility($categoryid);
break;
case 'getsubcategorieshtml' :
$categoryid = required_param('categoryid', PARAM_INT);
/* @var core_course_management_renderer $renderer */
$renderer = $PAGE->get_renderer('core_course', 'management');
$outcome->html = html_writer::start_tag('ul', array('class' => 'ml'));
$coursecat = coursecat::get($categoryid);
foreach ($coursecat->get_children() as $subcat) {
$outcome->html .= $renderer->category_listitem($subcat, array(), $subcat->get_children_count());
}
$outcome->html .= html_writer::end_tag('ul');
$outcome->outcome = true;
break;
}
echo json_encode($outcome);
echo $OUTPUT->footer();
// Thats all folks.
// Don't ever even consider putting anything after this. It just wouldn't make sense.
// But you already knew that, you smart developer you.
exit;
\ No newline at end of file
......@@ -17,8 +17,7 @@
/**
* Displays the top level category or all courses
*
* @package core
* @subpackage course
* @package core_coure
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
......
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Delete category form.
*
* @package core_course
* @copyright 2002 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir . '/formslib.php');
require_once($CFG->libdir . '/questionlib.php');
require_once($CFG->libdir . '/coursecatlib.php');
/**
* Delete category moodleform.
* @package core_course
* @copyright 2002 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_course_deletecategory_form extends moodleform {
/**
* The coursecat object for that category being deleted.
* @var coursecat
*/
protected $coursecat;
/**
* Defines the form.
*/
public function definition() {
$mform = $this->_form;
$this->coursecat = $this->_customdata;
$categorycontext = context_coursecat::instance($this->coursecat->id);
$categoryname = $this->coursecat->get_formatted_name();
// Check permissions, to see if it OK to give the option to delete
// the contents, rather than move elsewhere.
$candeletecontent = $this->coursecat->can_delete_full();
// Get the list of categories we might be able to move to.
$displaylist = $this->coursecat->move_content_targets_list();
// Now build the options.
$options = array();
if ($displaylist) {
$options[0] = get_string('movecontentstoanothercategory');
}
if ($candeletecontent) {
$options[1] = get_string('deleteallcannotundo');
}
if (empty($options)) {
print_error('youcannotdeletecategory', 'error', 'index.php', $categoryname);
}
// Now build the form.
$mform->addElement('header', 'general', get_string('categorycurrentcontents', '', $categoryname));
// Describe the contents of this category.
$contents = '';
if ($this->coursecat->has_children()) {
$contents .= '<li>' . get_string('subcategories') . '</li>';
}
if ($this->coursecat->has_courses()) {
$contents .= '<li>' . get_string('courses') . '</li>';
}
if (question_context_has_any_questions($categorycontext)) {
$contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>';
}
if (!empty($contents)) {
$mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), html_writer::tag('ul', $contents));
} else {
$mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
}
// Give the options for what to do.
$mform->addElement('select', 'fulldelete', get_string('whattodo'), $options);
if (count($options) == 1) {
$optionkeys = array_keys($options);
$option = reset($optionkeys);
$mform->hardFreeze('fulldelete');
$mform->setConstant('fulldelete', $option);
}
if ($displaylist) {
$mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist);
if (in_array($this->coursecat->parent, $displaylist)) {
$mform->setDefault('newparent', $this->coursecat->parent);
}
$mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
}
$mform->addElement('hidden', 'categoryid', $this->coursecat->id);
$mform->setType('categoryid', PARAM_ALPHANUM);
$mform->addElement('hidden', 'action', 'deletecategory');
$mform->setType('action', PARAM_ALPHANUM);
$mform->addElement('hidden', 'sure');
// This gets set by default to ensure that if the user changes it manually we can detect it.
$mform->setDefault('sure', md5(serialize($this->coursecat)));
$mform->setType('sure', PARAM_ALPHANUM);
$this->add_action_buttons(true, get_string('delete'));
}
/**
* Perform some extra moodle validation.
*
* @param array $data
* @param array $files
* @return array An array of errors.
*/
public function validation($data, $files) {
$errors = parent::validation($data, $files);
if (empty($data['fulldelete']) && empty($data['newparent'])) {
// When they have chosen the move option, they must specify a destination.
$errors['newparent'] = get_string('required');
}
if ($data['sure'] !== md5(serialize($this->coursecat))) {
$errors['categorylabel'] = get_string('categorymodifiedcancel');
}
return $errors;
}
}
\ No newline at end of file
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Edit category form.
*
* @package core_course
* @copyright 2002 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
require_once($CFG->libdir.'/formslib.php');
require_once($CFG->libdir.'/coursecatlib.php');
/**
* Edit category form.
*
* @package core_course
* @copyright 2002 onwards Martin Dougiamas (http://dougiamas.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_course_editcategory_form extends moodleform {
/**
* The form definition.
*/
public function definition() {
global $CFG, $DB;
$mform = $this->_form;
$categoryid = $this->_customdata['categoryid'];
$parent = $this->_customdata['parent'];
// Get list of categories to use as parents, with site as the first one.
$options = array();
if (has_capability('moodle/category:manage', context_system::instance()) || $parent == 0) {
$options[0] = get_string('top');
}
if ($categoryid) {
// Editing an existing category.
$options += coursecat::make_categories_list('moodle/category:manage', $categoryid);
if (empty($options[$parent])) {
// Ensure the the category parent has been included in the options.
$options[$parent] = $DB->get_field('course_categories', 'name', array('id'=>$parent));
}
$strsubmit = get_string('savechanges');
} else {
// Making a new category.
$options += coursecat::make_categories_list('moodle/category:manage');
$strsubmit = get_string('createcategory');
}
$mform->addElement('select', 'parent', get_string('parentcategory'), $options);
$mform->addElement('text', 'name', get_string('categoryname'), array('size' => '30'));
$mform->addRule('name', get_string('required'), 'required', null);
$mform->setType('name', PARAM_TEXT);
$mform->addElement('text', 'idnumber', get_string('idnumbercoursecategory'), 'maxlength="100" size="10"');
$mform->addHelpButton('idnumber', 'idnumbercoursecategory');
$mform->setType('idnumber', PARAM_RAW);
$mform->addElement('editor', 'description_editor', get_string('description'), null,
$this->get_description_editor_options());
$mform->setType('description_editor', PARAM_RAW);
if (!empty($CFG->allowcategorythemes)) {
$themes = array(''=>get_string('forceno'));
$allthemes = get_list_of_themes();
foreach ($allthemes as $key => $theme) {
if (empty($theme->hidefromselector)) {
$themes[$key] = get_string('pluginname', 'theme_'.$theme->name);
}
}
$mform->addElement('select', 'theme', get_string('forcetheme'), $themes);
}
$mform->addElement('hidden', 'id', 0);
$mform->setType('id', PARAM_INT);
$mform->setDefault('id', $categoryid);
$this->add_action_buttons(true, $strsubmit);
}
/**
* Returns the description editor options.
* @return array
*/
public function get_description_editor_options() {
global $CFG;