| [ Index ] |
PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008] |
[Summary view] [Print] [Text view]
1 <?php // $Id$ 2 /** 3 * Allows someone with appropriate permissions to move a category and associated 4 * files to another context. 5 * 6 * @author Jamie Pratt 7 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License 8 * @package questionbank 9 */ 10 11 require_once("../config.php"); 12 require_once($CFG->dirroot."/question/editlib.php"); 13 require_once($CFG->dirroot."/question/contextmove_form.php"); 14 15 list($thispageurl, $contexts, $cmid, $cm, $module, $pagevars) = question_edit_setup('categories'); 16 17 // get values from form for actions on this page 18 $toparent = required_param('toparent', PARAM_SEQUENCE); 19 $cattomove = required_param('cattomove', PARAM_INT); 20 $totop = optional_param('totop', 0, PARAM_INT); // optional param moves category to top of peers. Default is 21 //to add it to the bottom. 22 23 24 25 $onerrorurl = $CFG->wwwroot.'/question/category.php?'.$thispageurl->get_query_string(); 26 list($toparent, $contextto) = explode(',', $toparent); 27 if (!empty($toparent)){//not top level category, make it a child of $toparent 28 if (!$toparent = get_record('question_categories', 'id', $toparent)){ 29 error('Invalid category id for parent!', $onerrorurl); 30 } 31 $contextto = $toparent->contextid; 32 } else { 33 $toparent = new object(); 34 $toparent->id = 0; 35 $toparent->contextid = $contextto; 36 } 37 if (!$cattomove = get_record('question_categories', 'id', $cattomove)){ 38 error('Invalid category id to move!', $onerrorurl); 39 } 40 if ($cattomove->contextid == $contextto){ 41 error("You shouldn't have got here if you're not moving a category to another context.", $onerrorurl); 42 } 43 $cattomove->categorylist = question_categorylist($cattomove->id); 44 45 $thispageurl->params(array('cattomove'=>$cattomove->id, 46 'toparent'=>"{$toparent->id},{$toparent->contextid}", 47 'totop'=>$totop)); 48 49 $contextfrom = get_context_instance_by_id($cattomove->contextid); 50 $contextto = get_context_instance_by_id($contextto); 51 $contexttostring = print_context_name($contextto); 52 53 require_capability('moodle/question:managecategory', $contextfrom); 54 require_capability('moodle/question:managecategory', $contextto); 55 56 57 $fromcoursefilesid = get_filesdir_from_context($contextfrom);//siteid or courseid 58 $tocoursefilesid = get_filesdir_from_context($contextto);//siteid or courseid 59 if ($fromcoursefilesid != $tocoursefilesid){ 60 $questions = get_records_select('question', "category IN ({$cattomove->categorylist})"); 61 $urls = array(); 62 if ($questions){ 63 foreach ($questions as $id => $question){ 64 $QTYPES[$questions[$id]->qtype]->get_question_options($questions[$id]); 65 $urls = array_merge_recursive($urls, $QTYPES[$questions[$id]->qtype]->find_file_links($questions[$id], $fromcoursefilesid)); 66 } 67 } 68 ksort($urls); 69 } else { 70 $urls = array(); 71 } 72 $brokenurls = array(); 73 foreach (array_keys($urls) as $url){ 74 if (!file_exists($CFG->dataroot."/$fromcoursefilesid/".$url)){ 75 $brokenurls[] = $url; 76 } 77 } 78 if ($fromcoursefilesid == SITEID){ 79 $fromareaname = get_string('filesareasite', 'question'); 80 } else { 81 $fromareaname = get_string('filesareacourse', 'question'); 82 } 83 if ($tocoursefilesid == SITEID){ 84 $toareaname = get_string('filesareasite', 'question'); 85 } else { 86 $toareaname = get_string('filesareacourse', 'question'); 87 } 88 $contextmoveform = new question_context_move_form($thispageurl, 89 compact('urls', 'fromareaname', 'toareaname', 'brokenurls', 90 'fromcoursefilesid', 'tocoursefilesid')); 91 if ($contextmoveform->is_cancelled()){ 92 $thispageurl->remove_params('cattomove', 'toparent', 'totop'); 93 redirect($CFG->wwwroot."/question/category.php?".$thispageurl->get_query_string()); 94 }elseif ($moveformdata = $contextmoveform->get_data()) { 95 if (isset($moveformdata->urls) && is_array($moveformdata->urls)){ 96 check_dir_exists($CFG->dataroot."/$tocoursefilesid/", true); 97 $flipurls = array_keys($urls); 98 foreach ($moveformdata->urls as $key => $urlaction){ 99 $source = $CFG->dataroot."/$fromcoursefilesid/".$flipurls[$key]; 100 $destination = $flipurls[$key]; 101 if (($urlaction != QUESTION_FILEDONOTHING) && ($urlaction != QUESTION_FILEMOVELINKSONLY)){ 102 // Ensure the target folder exists. 103 check_dir_exists(dirname($CFG->dataroot."/$tocoursefilesid/".$destination), true); 104 105 // Then make sure the destination file name does not exist. If it does, change the name to be unique. 106 while (file_exists($CFG->dataroot."/$tocoursefilesid/".$destination)){ 107 $matches = array(); 108 //check for '_'. copyno after filename, before extension. 109 if (preg_match('!\_([0-9]+)(\.[^\.\\/]+)?$!', $destination, $matches)){ 110 $copyno = $matches[1]+1; 111 } else { 112 $copyno = 1; 113 } 114 //replace old copy no with incremented one. 115 $destination = preg_replace('!(\_[0-9]+)?(\.[^\.\\/]+)?$!', '_'.$copyno.'\\2', $destination, 1); 116 } 117 } 118 switch ($urlaction){ 119 case QUESTION_FILECOPY : 120 if (!copy($source, $CFG->dataroot."/$tocoursefilesid/".$destination)){ 121 print_error('errorfilecannotbecopied', 'question', $onerrorurl, $source); 122 } 123 break; 124 case QUESTION_FILEMOVE : 125 if (!rename($source, $CFG->dataroot."/$tocoursefilesid/".$destination)){ 126 print_error('errorfilecannotbemoved', 'question', $onerrorurl, $source); 127 } 128 break; 129 case QUESTION_FILEDONOTHING : 130 case QUESTION_FILEMOVELINKSONLY : 131 break; 132 default : 133 error('Invalid action selected!', $onerrorurl); 134 } 135 switch ($urlaction){ 136 //now search and replace urls in questions. 137 case QUESTION_FILECOPY : 138 case QUESTION_FILEMOVE : 139 case QUESTION_FILEMOVELINKSONLY : 140 $url = $flipurls[$key]; 141 $questionids = array_unique($urls[$url]); 142 foreach ($questionids as $questionid){ 143 $question = $questions[$questionid]; 144 $QTYPES[$question->qtype]->replace_file_links($question, $fromcoursefilesid, $tocoursefilesid, $url, $destination); 145 } 146 break; 147 case QUESTION_FILEDONOTHING : 148 default : 149 break; 150 } 151 152 153 } 154 } 155 156 //adjust sortorder before we make the cat a peer of it's new peers 157 $peers = get_records_select_menu('question_categories', "contextid = {$toparent->contextid} AND ". 158 "parent = {$toparent->id}", "sortorder ASC", 159 "id, id"); 160 $peers = array_keys($peers); 161 if ($totop){ 162 array_unshift($peers, $cattomove->id); 163 } else { 164 $peers[] = $cattomove->id; 165 } 166 $sortorder = 0; 167 foreach ($peers as $peer) { 168 if (! set_field('question_categories', "sortorder", $sortorder, "id", $peer)) { 169 print_error('listupdatefail', '', $onerrorurl); 170 } 171 $sortorder++; 172 } 173 //now move category 174 $cat = new object(); 175 $cat->id = $cattomove->id; 176 $cat->parent = $toparent->id; 177 //set context of category we are moving and all children also! 178 if (!execute_sql("UPDATE {$CFG->prefix}question_categories SET contextid = {$contextto->id} WHERE id IN ({$cattomove->categorylist})", false)){ 179 error("Could not move the category '$newname' to ".$contexttostring, $onerrorurl); 180 } 181 //finally set the new parent id 182 if (!update_record("question_categories", $cat)) { 183 error("Could not update the category '$updatename'", $onerrorurl); 184 } 185 $thispageurl->remove_params('cattomove', 'toparent', 'totop'); 186 redirect($CFG->wwwroot."/question/category.php?".$thispageurl->get_query_string(array('cat'=>"{$cattomove->id},{$contextto->id}"))); 187 } 188 189 $streditingcategories = get_string('editcategories', 'quiz'); 190 $crumbs = array(); 191 if ($cm!==null) { 192 // Page header 193 $strupdatemodule = has_capability('moodle/course:manageactivities', $contexts->lowest()) 194 ? update_module_button($cm->id, $COURSE->id, get_string('modulename', $cm->modname)) 195 : ""; 196 $crumbs[] = array('name' => get_string('modulenameplural', $cm->modname), 197 'link' => "$CFG->wwwroot/mod/{$cm->modname}/index.php?id=$COURSE->id", 198 'type' => 'activity'); 199 $crumbs[] = array('name' => format_string($module->name), 200 'link' => "$CFG->wwwroot/mod/{$cm->modname}/view.php?id={$cm->id}", 201 'type' => 'title'); 202 } else { 203 // Print basic page layout. 204 $strupdatemodule = ''; 205 } 206 207 208 $crumbs[] = array('name' => $streditingcategories, 'link' => $thispageurl->out(), 'type' => 'title'); 209 $crumbs[] = array('name' => get_string('movingcategory', 'question'), 'link' => '', 'type' => 'title'); 210 211 $navigation = build_navigation($crumbs); 212 print_header_simple($streditingcategories, '', $navigation, "", "", true, $strupdatemodule); 213 214 // print tabs 215 if ($cm!==null) { 216 $currenttab = 'edit'; 217 $mode = 'categories'; 218 ${$cm->modname} = $module; 219 include($CFG->dirroot."/mod/{$cm->modname}/tabs.php"); 220 } else { 221 $currenttab = 'categories'; 222 $context = $contexts->lowest(); 223 include ('tabs.php'); 224 } 225 //parameter for get_string 226 $cattomove->contextto = $contexttostring; 227 if (count($urls)){ 228 $defaults = array(); 229 for ($default_key = 0; $default_key < count($urls); $default_key++){ 230 $defaults['urls'][$default_key] = QUESTION_FILECOPY; 231 } 232 $contextmoveform->set_data($defaults); 233 //some parameters for get_string 234 $cattomove->urlcount = count($urls); 235 $cattomove->toareaname = $toareaname; 236 $cattomove->fromareaname = $fromareaname; 237 238 print_box(get_string('movingcategoryandfiles', 'question', $cattomove), 'boxwidthnarrow boxaligncenter generalbox'); 239 } else { 240 print_box(get_string('movingcategorynofiles', 'question', $cattomove), 'boxwidthnarrow boxaligncenter generalbox'); 241 } 242 $contextmoveform->display(); 243 print_footer($COURSE); 244 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Jan 14 11:33:29 2009 | Cross-referenced by PHPXref 0.7 |