public function __construct($course, $section, $onsectionpage = false) { if (course_can_delete_section($course, $section)) { if (get_string_manager()->string_exists('deletesection', 'format_' . $course->format)) { $this->title = get_string('deletesection', 'format_' . $course->format); } else { $this->title = get_string('deletesection'); } $this->url = new moodle_url('/course/editsection.php', array('id' => $section->id, 'sr' => $onsectionpage ? $section->section : 0, 'delete' => 1)); } }
public function test_course_can_delete_section() { global $DB; $this->resetAfterTest(true); $generator = $this->getDataGenerator(); $courseweeks = $generator->create_course(array('numsections' => 5, 'format' => 'weeks'), array('createsections' => true)); $assign1 = $generator->create_module('assign', array('course' => $courseweeks, 'section' => 1)); $assign2 = $generator->create_module('assign', array('course' => $courseweeks, 'section' => 2)); $coursetopics = $generator->create_course(array('numsections' => 5, 'format' => 'topics'), array('createsections' => true)); $coursesingleactivity = $generator->create_course(array('format' => 'singleactivity'), array('createsections' => true)); // Enrol student and teacher. $roleids = $DB->get_records_menu('role', null, '', 'shortname, id'); $student = $generator->create_user(); $teacher = $generator->create_user(); $generator->enrol_user($student->id, $courseweeks->id, $roleids['student']); $generator->enrol_user($teacher->id, $courseweeks->id, $roleids['editingteacher']); $generator->enrol_user($student->id, $coursetopics->id, $roleids['student']); $generator->enrol_user($teacher->id, $coursetopics->id, $roleids['editingteacher']); $generator->enrol_user($student->id, $coursesingleactivity->id, $roleids['student']); $generator->enrol_user($teacher->id, $coursesingleactivity->id, $roleids['editingteacher']); // Teacher should be able to delete sections (except for 0) in topics and weeks format. $this->setUser($teacher); // For topics and weeks formats will return false for section 0 and true for any other section. $this->assertFalse(course_can_delete_section($courseweeks, 0)); $this->assertTrue(course_can_delete_section($courseweeks, 1)); $this->assertFalse(course_can_delete_section($coursetopics, 0)); $this->assertTrue(course_can_delete_section($coursetopics, 1)); // For singleactivity course format no section can be deleted. $this->assertFalse(course_can_delete_section($coursesingleactivity, 0)); $this->assertFalse(course_can_delete_section($coursesingleactivity, 1)); // Now let's revoke a capability from teacher to manage activity in section 1. $modulecontext = context_module::instance($assign1->cmid); assign_capability('moodle/course:manageactivities', CAP_PROHIBIT, $roleids['editingteacher'], $modulecontext); $modulecontext->mark_dirty(); $this->assertFalse(course_can_delete_section($courseweeks, 1)); $this->assertTrue(course_can_delete_section($courseweeks, 2)); // Student does not have permissions to delete sections. $this->setUser($student); $this->assertFalse(course_can_delete_section($courseweeks, 1)); $this->assertFalse(course_can_delete_section($coursetopics, 1)); $this->assertFalse(course_can_delete_section($coursesingleactivity, 1)); }
/** * Generate the edit control items of a section * * @param stdClass $course The course entry from DB * @param stdClass $section The course_section entry from DB * @param bool $onsectionpage true if being printed on a section page * @return array of edit control items */ protected function section_edit_control_items($course, $section, $onsectionpage = false) { global $PAGE; if (!$PAGE->user_is_editing()) { return array(); } $coursecontext = context_course::instance($course->id); $isstealth = isset($course->numsections) && $section->section > $course->numsections; if ($onsectionpage) { $baseurl = course_get_url($course, $section->section); } else { $baseurl = course_get_url($course); } $baseurl->param('sesskey', sesskey()); $controls = array(); if (!$isstealth && has_capability('moodle/course:update', $coursecontext)) { if ($section->section > 0 && get_string_manager()->string_exists('editsection', 'format_' . $course->format)) { $streditsection = get_string('editsection', 'format_' . $course->format); } else { $streditsection = get_string('editsection'); } $controls['edit'] = array('url' => new moodle_url('/course/editsection.php', array('id' => $section->id, 'sr' => $onsectionpage)), 'icon' => 'i/settings', 'name' => $streditsection, 'pixattr' => array('class' => '', 'alt' => $streditsection), 'attr' => array('class' => 'icon edit', 'title' => $streditsection)); } if ($section->section) { $url = clone $baseurl; if (!$isstealth) { if (has_capability('moodle/course:sectionvisibility', $coursecontext)) { if ($section->visible) { // Show the hide/show eye. $strhidefromothers = get_string('hidefromothers', 'format_' . $course->format); $url->param('hide', $section->section); $controls['visiblity'] = array('url' => $url, 'icon' => 'i/hide', 'name' => $strhidefromothers, 'pixattr' => array('class' => '', 'alt' => $strhidefromothers), 'attr' => array('class' => 'icon editing_showhide', 'title' => $strhidefromothers)); } else { $strshowfromothers = get_string('showfromothers', 'format_' . $course->format); $url->param('show', $section->section); $controls['visiblity'] = array('url' => $url, 'icon' => 'i/show', 'name' => $strshowfromothers, 'pixattr' => array('class' => '', 'alt' => $strshowfromothers), 'attr' => array('class' => 'icon editing_showhide', 'title' => $strshowfromothers)); } } if (!$onsectionpage) { if (has_capability('moodle/course:movesections', $coursecontext)) { $url = clone $baseurl; if ($section->section > 1) { // Add a arrow to move section up. $url->param('section', $section->section); $url->param('move', -1); $strmoveup = get_string('moveup'); $controls['moveup'] = array('url' => $url, 'icon' => 'i/up', 'name' => $strmoveup, 'pixattr' => array('class' => '', 'alt' => $strmoveup), 'attr' => array('class' => 'icon moveup', 'title' => $strmoveup)); } $url = clone $baseurl; if ($section->section < $course->numsections) { // Add a arrow to move section down. $url->param('section', $section->section); $url->param('move', 1); $strmovedown = get_string('movedown'); $controls['movedown'] = array('url' => $url, 'icon' => 'i/down', 'name' => $strmovedown, 'pixattr' => array('class' => '', 'alt' => $strmovedown), 'attr' => array('class' => 'icon movedown', 'title' => $strmovedown)); } } } } if (course_can_delete_section($course, $section)) { if (get_string_manager()->string_exists('deletesection', 'format_' . $course->format)) { $strdelete = get_string('deletesection', 'format_' . $course->format); } else { $strdelete = get_string('deletesection'); } $url = new moodle_url('/course/editsection.php', array('id' => $section->id, 'sr' => $onsectionpage ? $section->section : 0, 'delete' => 1)); $controls['delete'] = array('url' => $url, 'icon' => 'i/delete', 'name' => $strdelete, 'pixattr' => array('class' => '', 'alt' => $strdelete), 'attr' => array('class' => 'icon delete', 'title' => $strdelete)); } } return $controls; }
// course_sections.id $sectionreturn = optional_param('sr', 0, PARAM_INT); $deletesection = optional_param('delete', 0, PARAM_BOOL); $PAGE->set_url('/course/editsection.php', array('id' => $id, 'sr' => $sectionreturn)); $section = $DB->get_record('course_sections', array('id' => $id), '*', MUST_EXIST); $course = $DB->get_record('course', array('id' => $section->course), '*', MUST_EXIST); $sectionnum = $section->section; require_login($course); $context = context_course::instance($course->id); require_capability('moodle/course:update', $context); // Get section_info object with all availability options. $sectioninfo = get_fast_modinfo($course)->get_section_info($sectionnum); // Deleting the section. if ($deletesection) { $cancelurl = course_get_url($course, $sectioninfo, array('sr' => $sectionreturn)); if (course_can_delete_section($course, $sectioninfo)) { $confirm = optional_param('confirm', false, PARAM_BOOL) && confirm_sesskey(); if ($confirm) { course_delete_section($course, $sectioninfo, true); $courseurl = course_get_url($course, 0, array('sr' => $sectionreturn)); redirect($courseurl); } else { if (get_string_manager()->string_exists('deletesection', 'format_' . $course->format)) { $strdelete = get_string('deletesection', 'format_' . $course->format); } else { $strdelete = get_string('deletesection'); } $PAGE->navbar->add($strdelete); $PAGE->set_title($strdelete); $PAGE->set_heading($course->fullname); echo $OUTPUT->header();
/** * Generate the edit controls of a section * * @param stdClass $course The course entry from DB * @param stdClass $section The course_section entry from DB * @param bool $onsectionpage true if being printed on a section page * @return array of links with edit controls */ protected function section_edit_controls($course, $section, $onsectionpage = false) { global $PAGE; if (!$PAGE->user_is_editing()) { return array(); } $coursecontext = context_course::instance($course->id); $isstealth = isset($course->numsections) && $section->section > $course->numsections; if ($onsectionpage) { $baseurl = course_get_url($course, $section->section); } else { $baseurl = course_get_url($course); } $baseurl->param('sesskey', sesskey()); $controls = array(); $url = clone $baseurl; if (!$isstealth && has_capability('moodle/course:sectionvisibility', $coursecontext)) { if ($section->visible) { // Show the hide/show eye. $strhidefromothers = get_string('hidefromothers', 'format_' . $course->format); $url->param('hide', $section->section); $controls[] = html_writer::link($url, html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/hide'), 'class' => 'icon hide', 'alt' => $strhidefromothers)), array('title' => $strhidefromothers, 'class' => 'editing_showhide')); } else { $strshowfromothers = get_string('showfromothers', 'format_' . $course->format); $url->param('show', $section->section); $controls[] = html_writer::link($url, html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/show'), 'class' => 'icon hide', 'alt' => $strshowfromothers)), array('title' => $strshowfromothers, 'class' => 'editing_showhide')); } } if (course_can_delete_section($course, $section)) { if (get_string_manager()->string_exists('deletesection', 'format_' . $course->format)) { $strdelete = get_string('deletesection', 'format_' . $course->format); } else { $strdelete = get_string('deletesection'); } $url = new moodle_url('/course/editsection.php', array('id' => $section->id, 'sr' => $onsectionpage ? $section->section : 0, 'delete' => 1)); $controls[] = html_writer::link($url, html_writer::empty_tag('img', array('src' => $this->output->pix_url('t/delete'), 'class' => 'icon delete', 'alt' => $strdelete)), array('title' => $strdelete)); } if (!$isstealth && !$onsectionpage && has_capability('moodle/course:movesections', $coursecontext)) { $url = clone $baseurl; if ($section->section > 1) { // Add a arrow to move section up. $url->param('section', $section->section); $url->param('move', -1); $strmoveup = get_string('moveup'); $controls[] = html_writer::link($url, html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/up'), 'class' => 'icon up', 'alt' => $strmoveup)), array('title' => $strmoveup, 'class' => 'moveup')); } $url = clone $baseurl; if ($section->section < $course->numsections) { // Add a arrow to move section down. $url->param('section', $section->section); $url->param('move', 1); $strmovedown = get_string('movedown'); $controls[] = html_writer::link($url, html_writer::empty_tag('img', array('src' => $this->output->pix_url('i/down'), 'class' => 'icon down', 'alt' => $strmovedown)), array('title' => $strmovedown, 'class' => 'movedown')); } } return $controls; }