/**
  * Checks if user has capability to move all category content to the new parent before
  * removing this category
  *
  * @param int $newcatid
  * @return bool
  */
 public function can_move_content_to($newcatid)
 {
     global $CFG;
     require_once $CFG->libdir . '/questionlib.php';
     $context = $this->get_context();
     if (!$this->is_uservisible() || !has_capability('moodle/category:manage', $context)) {
         return false;
     }
     $testcaps = array();
     // If this category has courses in it, user must have 'course:create' capability in target category.
     if ($this->has_courses()) {
         $testcaps[] = 'moodle/course:create';
     }
     // If this category has subcategories or questions, user must have 'category:manage' capability in target category.
     if ($this->has_children() || question_context_has_any_questions($context)) {
         $testcaps[] = 'moodle/category:manage';
     }
     if (!empty($testcaps)) {
         return has_all_capabilities($testcaps, context_coursecat::instance($newcatid));
     }
     // There is no content but still return true.
     return true;
 }
Example #2
0
 /**
  * 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'));
 }
Example #3
0
 function definition()
 {
     global $CFG, $DB;
     $mform =& $this->_form;
     $category = $this->_customdata;
     $categorycontext = context_coursecat::instance($category->id);
     $this->_category = $category;
     /// Check permissions, to see if it OK to give the option to delete
     /// the contents, rather than move elsewhere.
     /// Are there any subcategories of this one, can they be deleted?
     $candeletecontent = true;
     $tocheck = get_child_categories($category->id);
     $containscategories = !empty($tocheck);
     $categoryids = array($category->id);
     while (!empty($tocheck)) {
         $checkcat = array_pop($tocheck);
         $childcategoryids[] = $checkcat->id;
         $tocheck = $tocheck + get_child_categories($checkcat->id);
         $chcontext = context_coursecat::instance($checkcat->id);
         if ($candeletecontent && !has_capability('moodle/category:manage', $chcontext)) {
             $candeletecontent = false;
         }
     }
     /// Are there any courses in here, can they be deleted?
     list($test, $params) = $DB->get_in_or_equal($categoryids);
     $containedcourses = $DB->get_records_sql("SELECT id,1 FROM {course} c WHERE c.category {$test}", $params);
     $containscourses = false;
     if ($containedcourses) {
         $containscourses = true;
         foreach ($containedcourses as $courseid => $notused) {
             if ($candeletecontent && !can_delete_course($courseid)) {
                 $candeletecontent = false;
                 break;
             }
         }
     }
     /// Are there any questions in the question bank here?
     $containsquestions = question_context_has_any_questions($categorycontext);
     /// Get the list of categories we might be able to move to.
     $testcaps = array();
     if ($containscourses) {
         $testcaps[] = 'moodle/course:create';
     }
     if ($containscategories || $containsquestions) {
         $testcaps[] = 'moodle/category:manage';
     }
     $displaylist = array();
     $notused = array();
     if (!empty($testcaps)) {
         make_categories_list($displaylist, $notused, $testcaps, $category->id);
     }
     /// Now build the options.
     $options = array();
     if ($displaylist) {
         $options[0] = get_string('movecontentstoanothercategory');
     }
     if ($candeletecontent) {
         $options[1] = get_string('deleteallcannotundo');
     }
     /// Now build the form.
     $mform->addElement('header', 'general', get_string('categorycurrentcontents', '', format_string($category->name, true, array('context' => $categorycontext))));
     if ($containscourses || $containscategories || $containsquestions) {
         if (empty($options)) {
             print_error('youcannotdeletecategory', 'error', 'index.php', format_string($category->name, true, array('context' => $categorycontext)));
         }
         /// Describe the contents of this category.
         $contents = '<ul>';
         if ($containscategories) {
             $contents .= '<li>' . get_string('subcategories') . '</li>';
         }
         if ($containscourses) {
             $contents .= '<li>' . get_string('courses') . '</li>';
         }
         if ($containsquestions) {
             $contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>';
         }
         $contents .= '</ul>';
         $mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), $contents);
         /// 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($category->parent, $displaylist)) {
                 $mform->setDefault('newparent', $category->parent);
             }
             $mform->disabledIf('newparent', 'fulldelete', 'eq', '1');
         }
     } else {
         $mform->addElement('hidden', 'fulldelete', 1);
         $mform->setType('fulldelete', PARAM_INT);
         $mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty'));
     }
     $mform->addElement('hidden', 'delete');
     $mform->setType('delete', PARAM_ALPHANUM);
     $mform->addElement('hidden', 'sure');
     $mform->setType('sure', PARAM_ALPHANUM);
     $mform->setDefault('sure', md5(serialize($category)));
     //--------------------------------------------------------------------------------
     $this->add_action_buttons(true, get_string('delete'));
 }
Example #4
0
     error('Incorrect category id', 'index.php');
 }
 $heading = get_string('deletecategory', '', format_string($deletecat->name));
 $context = get_context_instance(CONTEXT_COURSECAT, $delete);
 require_capability('moodle/category:delete', $context);
 $mform = new delete_category_form(null, $deletecat);
 $mform->set_data(array('delete' => $delete));
 if ($mform->is_cancelled()) {
     redirect('index.php');
 } else {
     if (!($data = $mform->get_data(false))) {
         require_once $CFG->libdir . '/questionlib.php';
         print_category_edit_header();
         print_heading($heading);
         print_box(get_string('deletecategorycheck2'), 'generalbox boxwidthnormal boxaligncenter');
         if (question_context_has_any_questions($context)) {
             print_box(get_string('deletecoursecategorywithquestions', 'question'), 'generalbox boxwidthnormal boxaligncenter');
         }
         $mform->display();
         print_footer();
         exit;
     }
 }
 print_category_edit_header();
 print_heading($heading);
 if ($data->fulldelete) {
     category_delete_full($deletecat, true);
 } else {
     category_delete_move($deletecat, $data->newparent, true);
 }
 print_continue('index.php');