Skip to content

Commit

Permalink
Merge branch 'wip-MDL-31750-master' of git://github.com/abgreeve/moodle
Browse files Browse the repository at this point in the history
Conflicts:
	course/tests/courselib_test.php
  • Loading branch information
stronk7 committed Jan 9, 2013
2 parents 7316589 + 2b9e957 commit 8361f55
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 11 deletions.
16 changes: 7 additions & 9 deletions course/category.php
Expand Up @@ -101,13 +101,8 @@

// Move a specified course to a new category
if (!empty($moveto) and $data = data_submitted()) {
// Some courses are being moved
// user must have category update in both cats to perform this
require_capability('moodle/category:manage', $context);
require_capability('moodle/category:manage', context_coursecat::instance($moveto));

if (!$destcategory = $DB->get_record('course_categories', array('id' => $data->moveto))) {
print_error('cannotfindcategory', '', '', $data->moveto);
if (!$destcategory = $DB->get_record('course_categories', array('id' => $moveto))) {
print_error('cannotfindcategory', '', '', $moveto);
}

$courses = array();
Expand All @@ -126,7 +121,10 @@
}
}
}
move_courses($courses, $data->moveto);
if (!can_move_courses_to_category($courses, $moveto, $category->id)) {
print_error('cannotmovecoursetocategory');
}
move_courses($courses, $moveto);
}

// Hide or show a course
Expand Down Expand Up @@ -436,7 +434,7 @@
if ($abletomovecourses) {
$movetocategories = array();
$notused = array();
make_categories_list($movetocategories, $notused, 'moodle/category:manage');
make_categories_list($movetocategories, $notused, array('moodle/course:create', 'moodle/course:delete', 'moodle/category:manage'));
$movetocategories[$category->id] = get_string('moveselectedcoursesto');
echo '<tr><td colspan="3" align="right">';
echo html_writer::label(get_string('moveselectedcoursesto'), 'movetoid', false, array('class' => 'accesshide'));
Expand Down
3 changes: 3 additions & 0 deletions course/edit.php
Expand Up @@ -131,6 +131,9 @@
}
}
} else {
if (!can_move_courses_to_category($course->id, $data->category)) {
print_error('cannotmovecoursetocategory');
}
// Save any changes to the files used in the editor
update_course($data, $editoroptions);
}
Expand Down
4 changes: 2 additions & 2 deletions course/edit_form.php
Expand Up @@ -50,7 +50,7 @@ function definition() {
if (has_capability('moodle/course:create', $categorycontext)) {
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist, 'moodle/course:create');
make_categories_list($displaylist, $parentlist, array('moodle/course:create', 'moodle/course:delete', 'moodle/category:manage'));
$mform->addElement('select', 'category', get_string('category'), $displaylist);
$mform->addHelpButton('category', 'category');
$mform->setDefault('category', $category->id);
Expand All @@ -63,7 +63,7 @@ function definition() {
if (has_capability('moodle/course:changecategory', $coursecontext)) {
$displaylist = array();
$parentlist = array();
make_categories_list($displaylist, $parentlist, 'moodle/course:create');
make_categories_list($displaylist, $parentlist, array('moodle/course:create', 'moodle/course:delete', 'moodle/category:manage'));
if (!isset($displaylist[$course->category])) {
//always keep current
$displaylist[$course->category] = format_string($DB->get_field('course_categories', 'name', array('id'=>$course->category)));
Expand Down
53 changes: 53 additions & 0 deletions course/lib.php
Expand Up @@ -4605,3 +4605,56 @@ function get_sorted_course_formats($enabledonly = false) {
function course_get_url($courseorid, $section = null, $options = array()) {
return course_get_format($courseorid)->get_view_url($section, $options);
}

/**
* Determine whether a user can move a course to a different category.
*
* @param int|array $courseid The course ID (int) or course IDs (array) that are being moved.
* @param int $moveto The category ID of where we are moving the course to.
* @param int $movefrom The current category ID. If not provided will be looked up.
* @return bool True if the user can move the course. False if the user can't move the course.
*/
function can_move_courses_to_category($courseid, $moveto, $movefrom = null) {
global $DB;

$tocontext = context_coursecat::instance($moveto);

if (!has_capability('moodle/category:manage', $tocontext)) {
return false;
}

if (is_array($courseid)) {
foreach ($courseid as $id) {
if (!$movefrom) {
$movefrom = $DB->get_field('course', 'category', array('id' => $id));
}

$fromcontext = context_coursecat::instance($movefrom);
if (!has_capability('moodle/category:manage', $fromcontext)) {
return false;
}

$coursecontext = context_course::instance($id);
$capabilities = array('moodle/course:delete', 'moodle/course:create');
if (!has_all_capabilities($capabilities, $coursecontext)) {
return false;
}
}
} else {
if (!$movefrom) {
$movefrom = $DB->get_field('course', 'category', array('id' => $courseid));
}

$fromcontext = context_coursecat::instance($movefrom);
if (!has_capability('moodle/category:manage', $fromcontext)) {
return false;
}

$coursecontext = context_course::instance($courseid);
$capabilities = array('moodle/course:delete', 'moodle/course:create');
if (!has_all_capabilities($capabilities, $coursecontext)) {
return false;
}
}
return true;
}
96 changes: 96 additions & 0 deletions course/tests/courselib_test.php
Expand Up @@ -435,4 +435,100 @@ public function check_module_visibility($mod, $visibility, $visibleold) {
}
}

/**
* These tests check for moving courses around categories with different permissions enabled and disabled.
* (@see course/lib.php can_move_courses_to_category())
*/
function test_move_course_to_category_check() {
$this->resetAfterTest(true);

// Create a user.
$user = $this->getDataGenerator()->create_user();

// Log in as the user.
$this->setUser($user);

$catstructure[] = array(
'fromcategory' => array('catcap' => 'moodle/category:manage', 'allow' => CAP_ALLOW),
'tocategory' => array('catcap' => 'moodle/category:manage', 'allow' => CAP_ALLOW),
'courses' => array(
array('coursecap' => array('moodle/course:create')),
array('coursecap' => array('moodle/course:delete')),
array('coursecap' => array('moodle/course:delete', 'moodle/course:create'))
),
'movecourse' => array(
array('course' => 0, 'result' => false),
array('course' => 1, 'result' => false),
array('course' => 2, 'result' => true),
array('course' => array(0,1), 'result' => false),
array('course' => array(0,1,2), 'result' => false),
)
);

// Create a system context to ascertain the correct role id.
$systemcontext = context_system::instance();

$roleid = 4; // role id of student?
// This is to ensure that we get the correct role id.
$roles = role_get_names($systemcontext);
foreach ($roles as $role) {
if ($role->shortname == 'student') {
$roleid = $role->id;
}
}

// Create category.
foreach ($catstructure as $key => $value) {
// Create to catgory.
$cat = $this->getDataGenerator()->create_category(array('name' => 'FromTestCat '.$key));
$catstructure[$key]['tocategory']['catid'] = $cat->id;
$catstructure[$key]['tocategory']['catcontext'] = context_coursecat::instance($cat->id);
$catcap = $catstructure[$key]['tocategory']['catcap'];
$catallow = $catstructure[$key]['tocategory']['allow'];
assign_capability($catcap, $catallow, $roleid, $catstructure[$key]['tocategory']['catcontext'], true);

// Create from category.
$fromcat = $this->getDataGenerator()->create_category(array('name' => 'ToTestCat '.$key));
$catstructure[$key]['fromcategory']['catid'] = $fromcat->id;
$catstructure[$key]['fromcategory']['catcontext'] = context_coursecat::instance($fromcat->id);
$catcap = $catstructure[$key]['fromcategory']['catcap'];
$catallow = $catstructure[$key]['fromcategory']['allow'];
assign_capability($catcap, $catallow, $roleid, $catstructure[$key]['fromcategory']['catcontext'], true);

// Create course in from category.
foreach($value['courses'] as $coursekey => $coursedata) {
$coursed = array('category' => $fromcat->id,
'fullname' => 'Coursefullname '.$fromcat->id.$coursekey,
'shortname' => 'Courseshortname'.$fromcat->id.$coursekey);

$course = $this->getDataGenerator()->create_course($coursed);
$catstructure[$key]['courses'][$coursekey]['id'] = $course->id;
$coursecontext = context_course::instance($course->id);

foreach ($coursedata['coursecap'] as $coursecap) {
assign_capability($coursecap, CAP_ALLOW, $roleid, $coursecontext, true);
}
}
// Assign course creator to user on category.
role_assign($roleid, $user->id, $catstructure[$key]['tocategory']['catcontext']);
role_assign($roleid, $user->id, $catstructure[$key]['fromcategory']['catcontext']);
}

// We loop through the catstructure array and test moving courses to different categories with different permissions.
foreach ($catstructure as $key => $data) {
foreach ($data['movecourse'] as $move) {
if (is_array($move['course'])) {
$courseids = array();
foreach ($move['course'] as $courseindex) {
$courseids[] = $data['courses'][$courseindex]['id'];
}
} else {
$courseids = $data['courses'][$move['course']]['id'];
}

$canmove = can_move_courses_to_category($courseids, $data['tocategory']['catid']);
$this->assertEquals($canmove, $move['result']);
}
}
}
}
1 change: 1 addition & 0 deletions lang/en/error.php
Expand Up @@ -106,6 +106,7 @@
$string['cannotmodulename'] = 'Cannot get the module name in build navigation';
$string['cannotmoduletype'] = 'Cannot get the module type in build navigation';
$string['cannotmoverolewithid'] = 'Cannot move role with ID {$a}';
$string['cannotmovecoursetocategory'] = 'You can not move this course to the category specified';
$string['cannotopencsv'] = 'Cannot open CSV file';
$string['cannotopenfile'] = 'Cannot open file ({$a})';
$string['cannotopenforwrit'] = 'Cannot open for writing: {$a}';
Expand Down

0 comments on commit 8361f55

Please sign in to comment.