### Eclipse Workspace Patch 1.0 #P core-moodle Index: course/index.php =================================================================== RCS file: /cvsroot/moodle/moodle/course/index.php,v retrieving revision 1.146 diff -u -r1.146 index.php --- course/index.php 21 Sep 2010 08:13:11 -0000 1.146 +++ course/index.php 1 Nov 2010 11:13:23 -0000 @@ -210,31 +210,11 @@ /// Move a category up or down if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) { - fix_course_sortorder(); - $swapcategory = NULL; - if (!empty($moveup)) { - require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveup)); - if ($movecategory = $DB->get_record('course_categories', array('id'=>$moveup))) { - if ($swapcategory = $DB->get_records_select('course_categories', "sortordersortorder, $movecategory->parent), 'sortorder DESC', '*', 0, 1)) { - $swapcategory = reset($swapcategory); - } - } + move_category_order_up($moveup); } else { - require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $movedown)); - if ($movecategory = $DB->get_record('course_categories', array('id'=>$movedown))) { - if ($swapcategory = $DB->get_records_select('course_categories', "sortorder>? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder ASC', '*', 0, 1)) { - $swapcategory = reset($swapcategory); - } - } - } - if ($swapcategory and $movecategory) { - $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder, array('id'=>$movecategory->id)); - $DB->set_field('course_categories', 'sortorder', $movecategory->sortorder, array('id'=>$swapcategory->id)); + move_category_order_down($movedown); } - - // finally reorder courses - fix_course_sortorder(); } /// Print headings @@ -280,117 +260,4 @@ print_course_request_buttons($systemcontext); echo ''; -echo $OUTPUT->footer(); - -function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) { -/// Recursive function to print all the categories ready for editing - - global $CFG, $USER, $OUTPUT; - - static $str = NULL; - - if (is_null($str)) { - $str = new stdClass; - $str->edit = get_string('edit'); - $str->delete = get_string('delete'); - $str->moveup = get_string('moveup'); - $str->movedown = get_string('movedown'); - $str->edit = get_string('editthiscategory'); - $str->hide = get_string('hide'); - $str->show = get_string('show'); - $str->cohorts = get_string('cohorts', 'cohort'); - $str->spacer = $OUTPUT->spacer().' '; - } - - if (!empty($category)) { - - if (!isset($category->context)) { - $category->context = get_context_instance(CONTEXT_COURSECAT, $category->id); - } - - echo ''; - for ($i=0; $i<$depth;$i++) { - echo '      '; - } - $linkcss = $category->visible ? '' : ' class="dimmed" '; - echo ''. - format_string($category->name).''; - echo ''; - - echo ''.$category->coursecount.''; - - echo ''; /// Print little icons - - if (has_capability('moodle/category:manage', $category->context)) { - echo ' '; - - echo ' '; - - if (!empty($category->visible)) { - echo ' '; - } else { - echo ' '; - } - - if (has_capability('moodle/cohort:manage', $category->context) or has_capability('moodle/cohort:view', $category->context)) { - echo ' '; - } - - if ($up) { - echo ' '; - } else { - echo $str->spacer; - } - if ($down) { - echo ' '; - } else { - echo $str->spacer; - } - } - echo ''; - - echo ''; - if (has_capability('moodle/category:manage', $category->context)) { - $tempdisplaylist = $displaylist; - unset($tempdisplaylist[$category->id]); - foreach ($parentslist as $key => $parents) { - if (in_array($category->id, $parents)) { - unset($tempdisplaylist[$key]); - } - } - $popupurl = new moodle_url("index.php?move=$category->id&sesskey=".sesskey()); - $select = new single_select($popupurl, 'moveto', $tempdisplaylist, $category->parent, null, "moveform$category->id"); - echo $OUTPUT->render($select); - } - echo ''; - echo ''; - } else { - $category->id = '0'; - } - - if ($categories = get_categories($category->id)) { // Print all the children recursively - $countcats = count($categories); - $count = 0; - $first = true; - $last = false; - foreach ($categories as $cat) { - $count++; - if ($count == $countcats) { - $last = true; - } - $up = $first ? false : true; - $down = $last ? false : true; - $first = false; - - print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down); - } - } -} \ No newline at end of file +echo $OUTPUT->footer(); \ No newline at end of file Index: course/lib.php =================================================================== RCS file: /cvsroot/moodle/moodle/course/lib.php,v retrieving revision 1.780 diff -u -r1.780 lib.php --- course/lib.php 19 Oct 2010 15:38:53 -0000 1.780 +++ course/lib.php 1 Nov 2010 11:13:23 -0000 @@ -4022,4 +4022,175 @@ $eventdata->smallmessage = ''; message_send($eventdata); } + +/** + * Move the category up or down within it's parent category as long as the user + * has moodle/category:manage on the given category + * @param int $catid category id for category to move + * @param string $type only allows 'up' or 'down'. + */ +}function move_category_order($catid, $type) { + global $DB; + fix_course_sortorder(); + $swapcategory = NULL; + $movecategory = NULL; + + switch($type) { + case 'up': + $order = 'DESC'; + $sort = '<'; + break; + case 'down': + $order = 'ASC'; + $sort = '>'; + break; + default: + print_error('Incorrect Category move type.'); + break; + } + + require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $catid)); + if ($movecategory = $DB->get_record('course_categories', array('id'=>$catid))) { + if ($swapcategory = $DB->get_records_select('course_categories', "sortorder$sort? AND parent=?", array($movecategory->sortorder, $movecategory->parent), "sortorder $order", '*', 0, 1)) { + $swapcategory = reset($swapcategory); + } + } + + if ($swapcategory and $movecategory) { + $DB->set_field('course_categories', 'sortorder', $swapcategory->sortorder, array('id'=>$movecategory->id)); + $DB->set_field('course_categories', 'sortorder', $movecategory->sortorder, array('id'=>$swapcategory->id)); + } + + // finally reorder courses + fix_course_sortorder(); +} + +/* + * Helper function for move_category_order, just looks cleaner in code + * @param int $catid category id for category to move up + */ +function move_category_order_up($catid) { + move_category_order($catid, 'up'); +} + +/* + * Helper function for move_category_order, just looks cleaner in code + * @param int $catid category id for category to move up + */ +function move_category_order_down($catid) { + move_category_order($catid, 'down'); +} + +/* + * Recursive function to print all the categories ready for editing + */ +function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) { + global $CFG, $USER, $OUTPUT; + + static $str = NULL; + + if (is_null($str)) { + $str = new stdClass; + $str->edit = get_string('edit'); + $str->delete = get_string('delete'); + $str->moveup = get_string('moveup'); + $str->movedown = get_string('movedown'); + $str->edit = get_string('editthiscategory'); + $str->hide = get_string('hide'); + $str->show = get_string('show'); + $str->cohorts = get_string('cohorts', 'cohort'); + $str->spacer = $OUTPUT->spacer().' '; + } + + if (!empty($category)) { + + if (!isset($category->context)) { + $category->context = get_context_instance(CONTEXT_COURSECAT, $category->id); + } + + echo ''; + for ($i=0; $i<$depth;$i++) { + echo '      '; + } + $linkcss = $category->visible ? '' : ' class="dimmed" '; + echo ''. + format_string($category->name).''; + echo ''; + + echo ''.$category->coursecount.''; + + echo ''; /// Print little icons + + if (has_capability('moodle/category:manage', $category->context)) { + echo ' '; + + echo ' '; + + if (!empty($category->visible)) { + echo ' '; + } else { + echo ' '; + } + + if (has_capability('moodle/cohort:manage', $category->context) or has_capability('moodle/cohort:view', $category->context)) { + echo ' '; + } + + if ($up) { + echo ' '; + } else { + echo $str->spacer; + } + if ($down) { + echo ' '; + } else { + echo $str->spacer; + } + } + echo ''; + + echo ''; + if (has_capability('moodle/category:manage', $category->context)) { + $tempdisplaylist = $displaylist; + unset($tempdisplaylist[$category->id]); + foreach ($parentslist as $key => $parents) { + if (in_array($category->id, $parents)) { + unset($tempdisplaylist[$key]); + } + } + $popupurl = new moodle_url("index.php?move=$category->id&sesskey=".sesskey()); + $select = new single_select($popupurl, 'moveto', $tempdisplaylist, $category->parent, null, "moveform$category->id"); + echo $OUTPUT->render($select); + } + echo ''; + echo ''; + } else { + $category->id = '0'; + } + + if ($categories = get_categories($category->id)) { // Print all the children recursively + $countcats = count($categories); + $count = 0; + $first = true; + $last = false; + foreach ($categories as $cat) { + $count++; + if ($count == $countcats) { + $last = true; + } + $up = $first ? false : true; + $down = $last ? false : true; + $first = false; + + print_category_edit($cat, $displaylist, $parentslist, $depth+1, $up, $down); + } + } }