|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 if (!defined('MOODLE_INTERNAL')) { 00004 die('Direct access to this script is forbidden.'); 00005 } 00006 00007 require_once($CFG->libdir.'/formslib.php'); 00008 require_once($CFG->libdir.'/questionlib.php'); 00009 00010 class delete_category_form extends moodleform { 00011 00012 var $_category; 00013 00014 function definition() { 00015 global $CFG, $DB; 00016 00017 $mform =& $this->_form; 00018 $category = $this->_customdata; 00019 $categorycontext = get_context_instance(CONTEXT_COURSECAT, $category->id); 00020 $this->_category = $category; 00021 00025 $candeletecontent = true; 00026 $tocheck = get_child_categories($category->id); 00027 $containscategories = !empty($tocheck); 00028 $categoryids = array($category->id); 00029 while (!empty($tocheck)) { 00030 $checkcat = array_pop($tocheck); 00031 $childcategoryids[] = $checkcat->id; 00032 $tocheck = $tocheck + get_child_categories($checkcat->id); 00033 $chcontext = get_context_instance(CONTEXT_COURSECAT, $checkcat->id); 00034 if ($candeletecontent && !has_capability('moodle/category:manage', $chcontext)) { 00035 $candeletecontent = false; 00036 } 00037 } 00038 00040 list($test, $params) = $DB->get_in_or_equal($categoryids); 00041 $containedcourses = $DB->get_records_sql( 00042 "SELECT id,1 FROM {course} c WHERE c.category $test", $params); 00043 $containscourses = false; 00044 if ($containedcourses) { 00045 $containscourses = true; 00046 foreach ($containedcourses as $courseid => $notused) { 00047 if ($candeletecontent && !can_delete_course($courseid)) { 00048 $candeletecontent = false; 00049 break; 00050 } 00051 } 00052 } 00053 00055 $containsquestions = question_context_has_any_questions($categorycontext); 00056 00058 $testcaps = array(); 00059 if ($containscourses) { 00060 $testcaps[] = 'moodle/course:create'; 00061 } 00062 if ($containscategories || $containsquestions) { 00063 $testcaps[] = 'moodle/category:manage'; 00064 } 00065 $displaylist = array(); 00066 $notused = array(); 00067 if (!empty($testcaps)) { 00068 make_categories_list($displaylist, $notused, $testcaps, $category->id); 00069 } 00070 00072 $options = array(); 00073 if ($displaylist) { 00074 $options[0] = get_string('movecontentstoanothercategory'); 00075 } 00076 if ($candeletecontent) { 00077 $options[1] = get_string('deleteallcannotundo'); 00078 } 00079 00081 $mform->addElement('header','general', get_string('categorycurrentcontents', '', format_string($category->name, true, array('context' => $categorycontext)))); 00082 00083 if ($containscourses || $containscategories || $containsquestions) { 00084 if (empty($options)) { 00085 print_error('youcannotdeletecategory', 'error', 'index.php', format_string($category->name, true, array('context' => $categorycontext))); 00086 } 00087 00089 $contents = '<ul>'; 00090 if ($containscategories) { 00091 $contents .= '<li>' . get_string('subcategories') . '</li>'; 00092 } 00093 if ($containscourses) { 00094 $contents .= '<li>' . get_string('courses') . '</li>'; 00095 } 00096 if ($containsquestions) { 00097 $contents .= '<li>' . get_string('questionsinthequestionbank') . '</li>'; 00098 } 00099 $contents .= '</ul>'; 00100 $mform->addElement('static', 'emptymessage', get_string('thiscategorycontains'), $contents); 00101 00103 $mform->addElement('select', 'fulldelete', get_string('whattodo'), $options); 00104 if (count($options) == 1) { 00105 $mform->hardFreeze('fulldelete'); 00106 $mform->setConstant('fulldelete', reset(array_keys($options))); 00107 } 00108 00109 if ($displaylist) { 00110 $mform->addElement('select', 'newparent', get_string('movecategorycontentto'), $displaylist); 00111 if (in_array($category->parent, $displaylist)) { 00112 $mform->setDefault('newparent', $category->parent); 00113 } 00114 $mform->disabledIf('newparent', 'fulldelete', 'eq', '1'); 00115 } 00116 } else { 00117 $mform->addElement('hidden', 'fulldelete', 1); 00118 $mform->setType('fulldelete', PARAM_INT); 00119 $mform->addElement('static', 'emptymessage', '', get_string('deletecategoryempty')); 00120 } 00121 00122 $mform->addElement('hidden', 'delete'); 00123 $mform->setType('delete', PARAM_ALPHANUM); 00124 $mform->addElement('hidden', 'sure'); 00125 $mform->setType('sure', PARAM_ALPHANUM); 00126 $mform->setDefault('sure', md5(serialize($category))); 00127 00128 //-------------------------------------------------------------------------------- 00129 $this->add_action_buttons(true, get_string('delete')); 00130 00131 } 00132 00134 function validation($data, $files) { 00135 $errors = parent::validation($data, $files); 00136 00137 if (empty($data['fulldelete']) && empty($data['newparent'])) { 00139 $errors['newparent'] = get_string('required'); 00140 } 00141 00142 if ($data['sure'] != md5(serialize($this->_category))) { 00143 $errors['categorylabel'] = get_string('categorymodifiedcancel'); 00144 } 00145 00146 return $errors; 00147 } 00148 } 00149