### Eclipse Workspace Patch 1.0 #P ou-moodle Index: course/courselib.php =================================================================== RCS file: course/courselib.php diff -N course/courselib.php --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ course/courselib.php 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,254 @@ +parent)); + if ($cattomove->parent != $move_cat_id) { + $newparent = get_record('course_categories', 'id', $move_cat_id); + require_capability('moodle/category:manage', get_category_or_system_context($move_cat_id)); + if (!move_category($cattomove, $newparent)) { + notify('Could not update that category!'); + } + } + } +} + +/** + * Changes the visibility of the given category as long as the user has + * moodle/category:manage on the given categories parent category or system + * @param int $cat_id category id for category to move + * @param int $visible the new visibility + */ +function category_change_visibility($cat_id, $visible=0) { + if ($category = get_record('course_categories', 'id', $cat_id)) { + require_capability('moodle/category:manage', get_category_or_system_context($category->parent)); + if (! set_field('course_categories', 'visible', $visible, 'id', $category->id)) { + notify('Could not update that category!'); + } +// ou-specific begins + else { + set_field("course_categories", "timemodified", time(), "id", $category->id); + } +// ou-specific ends + if (! set_field('course', 'visible', $visible, 'category', $category->id)) { + notify('Could not hide/show any courses in this category !'); + } + } +} + +/* + * Helper function for category_change_visibility, just looks cleaner in code + * @param int $cat_id category id for category to move + */ +function category_hide($cat_id) { + category_change_visibility($cat_id, 0); +} + +/* + * Helper function for category_change_visibility, just looks cleaner in code + * @param int $cat_id category id for category to move + */ +function category_show($cat_id) { + category_change_visibility($cat_id, 1); +} + +/** + * 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 $cat_id category id for category to move + * @param string $type only allows 'up' or 'down'. + */ +function category_move($cat_id, $type) { + switch($type) { + 'up': + 'down': + break; + default: + print_error('Unknown category move type.'); + break; + } + + $swapcategory = NULL; + $movecategory = NULL; + + require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $cat_id)); + if ($movecategory = get_record('course_categories', 'id', $cat_id)) { + $categories = get_categories($movecategory->parent); + + $choosenext = false; + + foreach ($categories as $category) { + if($choosenext) { + $swapcategory = $category; + break; + } + if ($category->id == $movecategory->id) { + if($type == 'down') { + $choosenext = true; + } else { + break; + } + } + if($type == 'up') { + $swapcategory = $category; + } + } + unset($category); + } + // Renumber everything for robustness + if ($swapcategory && $movecategory) { + $count = 0; + foreach ($categories as $category) { + $count++; + if ($category->id == $swapcategory->id) { + $category = $movecategory; + } else if ($category->id == $movecategory->id) { + $category = $swapcategory; + } + if (! set_field('course_categories', 'sortorder', $count, 'id', $category->id)) { + notify('Could not update that category!'); + } + } + unset($category); + } +} + +/* + * Helper function for category_move, just looks cleaner in code + * @param int $cat_id category id for category to move up + */ +function category_move_up($cat_id) { + category_move($cat_id, 'up'); +} + +/* + * Helper function for category_move, just looks cleaner in code + * @param int $cat_id category id for category to move down + */ +function category_move_down($cat_id) { + category_move($cat_id, '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; + + 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->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 ($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]); + } + } + popup_form ("index.php?move=$category->id&sesskey=$USER->sesskey&moveto=", $tempdisplaylist, "moveform$category->id", $category->parent, '', '', '', false); + } + 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); + } + } +} + +/* + * Setup the page data for course/index.php + */ +function print_category_edit_header() { + global $CFG; + global $SITE; + + require_once($CFG->libdir.'/adminlib.php'); + admin_externalpage_setup('coursemgmt', update_category_button()); + admin_externalpage_print_header(); +} +?> \ No newline at end of file Index: course/index.php =================================================================== RCS file: /home/cvs_repositories/globalcvs/ou-moodle/course/index.php,v retrieving revision 1.52 diff -u -r1.52 index.php --- course/index.php 24 Jun 2010 11:19:57 -0000 1.52 +++ course/index.php 25 Oct 2010 11:12:33 -0000 @@ -495,94 +495,27 @@ /// Move a category to a new parent if required if (!empty($move) and ($moveto >= 0) and confirm_sesskey()) { - if ($cattomove = get_record('course_categories', 'id', $move)) { - require_capability('moodle/category:manage', get_category_or_system_context($cattomove->parent)); - if ($cattomove->parent != $moveto) { - $newparent = get_record('course_categories', 'id', $moveto); - require_capability('moodle/category:manage', get_category_or_system_context($moveto)); - if (!move_category($cattomove, $newparent)) { - notify('Could not update that category!'); - } - } - } + category_move_parent($move, $moveto); } /// Hide or show a category if ((!empty($hide) or !empty($show)) and confirm_sesskey()) { if (!empty($hide)) { - $tempcat = get_record('course_categories', 'id', $hide); - $visible = 0; + category_hide($hide); } else { - $tempcat = get_record('course_categories', 'id', $show); - $visible = 1; - } - require_capability('moodle/category:manage', get_category_or_system_context($tempcat->parent)); - if ($tempcat) { - if (! set_field('course_categories', 'visible', $visible, 'id', $tempcat->id)) { - notify('Could not update that category!'); - } -// ou-specific begins - else { - set_field("course_categories", "timemodified", time(), "id", $tempcat->id); - } -// ou-specific ends - if (! set_field('course', 'visible', $visible, 'category', $tempcat->id)) { - notify('Could not hide/show any courses in this category !'); - } + category_show($show); } } /// Move a category up or down if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) { - $swapcategory = NULL; - $movecategory = NULL; - if (!empty($moveup)) { - require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $moveup)); - if ($movecategory = get_record('course_categories', 'id', $moveup)) { - $categories = get_categories($movecategory->parent); - - foreach ($categories as $category) { - if ($category->id == $movecategory->id) { - break; - } - $swapcategory = $category; - } - unset($category); - } + /// Move category up + category_move_up($moveup); } if (!empty($movedown)) { - require_capability('moodle/category:manage', get_context_instance(CONTEXT_COURSECAT, $movedown)); - if ($movecategory = get_record('course_categories', 'id', $movedown)) { - $categories = get_categories($movecategory->parent); - - $choosenext = false; - foreach ($categories as $category) { - if ($choosenext) { - $swapcategory = $category; - break; - } - if ($category->id == $movecategory->id) { - $choosenext = true; - } - } - unset($category); - } - } - if ($swapcategory and $movecategory) { // Renumber everything for robustness - $count=0; - foreach ($categories as $category) { - $count++; - if ($category->id == $swapcategory->id) { - $category = $movecategory; - } else if ($category->id == $movecategory->id) { - $category = $swapcategory; - } - if (! set_field('course_categories', 'sortorder', $count, 'id', $category->id)) { - notify('Could not update that category!'); - } - } - unset($category); + /// Move category up + category_move_down($movedown); } } @@ -635,118 +568,4 @@ echo ''; admin_externalpage_print_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; - - 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->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 ($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]); - } - } - popup_form ("index.php?move=$category->id&sesskey=$USER->sesskey&moveto=", $tempdisplaylist, "moveform$category->id", $category->parent, '', '', '', false); - } - 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); - } - } -} - -function print_category_edit_header() { - global $CFG; - global $SITE; - - require_once($CFG->libdir.'/adminlib.php'); - admin_externalpage_setup('coursemgmt', update_category_button()); - admin_externalpage_print_header(); -} ?>