Skip to content

Commit

Permalink
MDL-14580 rewritten course sorting algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jun 16, 2008
1 parent 977b3d3 commit 0cbe811
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 373 deletions.
14 changes: 3 additions & 11 deletions admin/cliupgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -1166,17 +1166,9 @@
$page = page_create_object(PAGE_COURSE_VIEW, $newid);
blocks_repopulate_page($page); // Return value not checked because you can always edit later

$cat = new Object();
$cat->name = get_string('miscellaneous');
$cat->depth = 1;
if ($catid = $DB->insert_record('course_categories', $cat)) {
// make sure category context exists
get_context_instance(CONTEXT_COURSECAT, $catid);
mark_context_dirty('/'.SYSCONTEXTID);
// do nothing
} else {
print_error('cannotsetupcategory', 'error');
}
// create default course category
$cat = get_course_category();

} else {
print_error('cannotsetupsite', 'error');
}
Expand Down
11 changes: 2 additions & 9 deletions admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,8 @@
$page = page_create_object(PAGE_COURSE_VIEW, $newid);
blocks_repopulate_page($page); // Return value not checked because you can always edit later

$cat = new object();
$cat->name = get_string('miscellaneous');
$cat->depth = 1;
if (!$catid = $DB->insert_record('course_categories', $cat)) {
print_error('cannotsetupcategory', 'error');
}
// make sure category context exists
get_context_instance(CONTEXT_COURSECAT, $catid);
mark_context_dirty('/'.SYSCONTEXTID);
// create default course category
$cat = get_course_category();

redirect('index.php');
}
Expand Down
41 changes: 6 additions & 35 deletions backup/restorelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,34 +651,13 @@ function restore_create_new_course($restore,&$course_header) {
$category = $DB->get_record("course_categories", array("name"=>$course_header->category->name));
}

//If no exists, get category id 1
//If no exists, get default category
if (!$category) {
$category = $DB->get_record("course_categories", array("id"=>"1"));
$category = get_course_category();
}

//If category 1 doesn'exists, lets create the course category (get it from backup file)
if (!$category) {
$ins_category = new object();
$ins_category->name = $course_header->category->name;
$ins_category->parent = 0;
$ins_category->sortorder = 0;
$ins_category->coursecount = 0;
$ins_category->visible = 0; //To avoid interferences with the rest of the site
$ins_category->timemodified = time();
$newid = $DB->insert_record("course_categories",$ins_category);
$category->id = $newid;
$category->name = $course_header->category->name;
}
//If exists, put new category id
if ($category) {
$course_header->category->id = $category->id;
$course_header->category->name = $category->name;
//Error, cannot locate category
} else {
$course_header->category->id = 0;
$course_header->category->name = get_string("unknowncategory");
$status = false;
}
$course_header->category->id = $category->id;
$course_header->category->name = $category->name;

//Create the course_object
if ($status) {
Expand Down Expand Up @@ -737,16 +716,8 @@ function restore_create_new_course($restore,&$course_header) {
$course->enrolenddate += $restore->course_startdateoffset;
}
$course->enrolperiod = $course_header->course_enrolperiod;
//Calculate sortorder field
$sortmax = $DB->get_record_sql('SELECT MAX(sortorder) AS max
FROM {course}
WHERE category=?', array($course->category));
if (!empty($sortmax->max)) {
$course->sortorder = $sortmax->max + 1;
unset($sortmax);
} else {
$course->sortorder = 100;
}
//Put as last course in category
$course->sortorder = $category->sortorder + MAX_COURSES_IN_CATEGORY - 1;

//Now, recode some languages (Moodle 1.5)
if ($course->lang == 'ma_nt') {
Expand Down
48 changes: 16 additions & 32 deletions course/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,12 @@

if ($resort and confirm_sesskey()) {
if ($courses = get_courses($category->id, "fullname ASC", 'c.id,c.fullname,c.sortorder')) {
// move it off the range
$count = $DB->get_record_sql('SELECT MAX(sortorder) AS max, 1 FROM {course} WHERE category= ?', array($category->id));
$count = $count->max + 100;
$DB->begin_sql();
$i = 1;
foreach ($courses as $course) {
$DB->set_field('course', 'sortorder', $count, array('id'=>$course->id));
$count++;
$DB->set_field('course', 'sortorder', $category->sortorder+$i, array('id'=>$course->id));
$i++;
}
$DB->commit_sql();
fix_course_sortorder($category->id);
fix_course_sortorder(); // should not be needed
}
}
}
Expand Down Expand Up @@ -205,36 +201,24 @@

if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {
require_capability('moodle/category:update', $context);
$movecourse = NULL;
$swapcourse = NULL;

// ensure the course order has no gaps
// and isn't at 0
fix_course_sortorder($category->id);

// we are going to need to know the range
$max = $DB->get_record_sql('SELECT MAX(sortorder) AS max, 1 FROM {course} WHERE category=?', array($category->id));
$max = $max->max + 100;
// ensure the course order has continuous ordering
fix_course_sortorder();
$swapcourse = NULL;

if (!empty($moveup)) {
$movecourse = $DB->get_record('course', array('id'=>$moveup));
$swapcourse = $DB->get_record('course', array('category'=>$category->id, 'sortorder'=>($movecourse->sortorder-1)));
if ($movecourse = $DB->get_record('course', array('id'=>$moveup))) {
$swapcourse = $DB->get_record('course', array('sortorder'=>$movecourse->sortorder-1));
}
} else {
$movecourse = $DB->get_record('course', array('id'=>$movedown));
$swapcourse = $DB->get_record('course', array('category'=>$category->id, 'sortorder'=>($movecourse->sortorder+1)));
}

if ($swapcourse and $movecourse) { // Renumber everything for robustness
$DB->begin_sql();
if (!( $DB->set_field("course", "sortorder", $max, array("id"=>$swapcourse->id))
&& $DB->set_field("course", "sortorder", $swapcourse->sortorder, array("id"=>$movecourse->id))
&& $DB->set_field("course", "sortorder", $movecourse->sortorder, array("id"=>$swapcourse->id))
)) {
notify("Could not update that course!");
if ($movecourse = $DB->get_record('course', array('id'=>$movedown))) {
$swapcourse = $DB->get_record('course', array('sortorder'=>$movecourse->sortorder+1));
}
$DB->commit_sql();
}

if ($swapcourse and $movecourse) {
$DB->set_field('course', 'sortorder', $swapcourse->sortorder, array('id'=>$movecourse->id));
$DB->set_field('course', 'sortorder', $movecourse->sortorder, array('id'=>$swapcourse->id));
}
}

} // End of editing stuff
Expand Down
8 changes: 4 additions & 4 deletions course/editcategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,29 @@
$newcategory = new stdClass();
$newcategory->name = $data->name;
$newcategory->description = $data->description;
$newcategory->sortorder = 999;
$newcategory->parent = $data->parent; // if $id = 0, the new category will be a top-level category

if (!empty($data->theme) && !empty($CFG->allowcategorythemes)) {
$newcategory->theme = $data->theme;
theme_setup();
}

if (empty($category) && has_capability('moodle/category:create', $context)) { // Create a new category
if (empty($category) && has_capability('moodle/category:create', $context)) { // Create a new category
$newcategory->sortorder = MAX_COURSES_IN_CATEGORY*MAX_COURSE_CATEGORIES; // put as last category in any parent cat
if (!$newcategory->id = $DB->insert_record('course_categories', $newcategory)) {
notify( "Could not insert the new category '$newcategory->name' ");
} else {
$newcategory->context = get_context_instance(CONTEXT_COURSECAT, $newcategory->id);
mark_context_dirty($newcategory->context->path);
fix_course_sortorder();
redirect('index.php?categoryedit=on');
}
} elseif (has_capability('moodle/category:update', $context)) {
$newcategory->id = $category->id;

if ($newcategory->parent != $category->parent) {
$parent_cat = $DB->get_record('course_categories', array('id'=>$newcategory->parent));
move_category($newcategory, $parent_cat);
move_category($newcategory, $parent_cat); // includes sortorder fix
}

if (!$DB->update_record('course_categories', $newcategory)) {
Expand All @@ -90,7 +91,6 @@
} else {
$redirect_link = 'category.php?id='.$newcategory->id.'&categoryedit=on';
}
fix_course_sortorder();
redirect($redirect_link);
}
}
Expand Down
56 changes: 14 additions & 42 deletions course/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,61 +199,33 @@

if ((!empty($moveup) or !empty($movedown)) and confirm_sesskey()) {

fix_course_sortorder();
$swapcategory = NULL;
$movecategory = NULL;

if (!empty($moveup)) {
if ($movecategory = $DB->get_record('course_categories', array('id'=>$moveup))) {
$categories = get_categories($movecategory->parent);

foreach ($categories as $category) {
if ($category->id == $movecategory->id) {
break;
}
$swapcategory = $category;
if ($swapcategory = $DB->get_records_select('course_categories', "sortorder<? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder ASC', '*', 0, 1)) {
$swapcategory = reset($swapcategory);
}
unset($category);
}
}
if (!empty($movedown)) {
} else {
if ($movecategory = $DB->get_record('course_categories', array('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;
}
if ($swapcategory = $DB->get_records_select('course_categories', "sortorder>? AND parent=?", array($movecategory->sortorder, $movecategory->parent), 'sortorder ASC', '*', 0, 1)) {
$swapcategory = reset($swapcategory);
}
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 (!$DB->set_field('course_categories', 'sortorder', $count, array('id'=>$category->id))) {
notify('Could not update that category!');
}
}
unset($category);
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));
}
}

/// Find any orphan courses that don't yet have a valid category and set to default
fix_coursecategory_orphans();
// finally reorder courses
fix_course_sortorder();
}

/// Should be a no-op 99% of the cases
fix_course_sortorder();
/// This should not be needed anymore
//fix_course_sortorder();

/// Print out the categories with all the knobs

Expand Down
71 changes: 25 additions & 46 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -2854,49 +2854,28 @@ function category_delete_move($category, $newparentid, $showfeedback=true) {
function move_courses($courseids, $categoryid) {
global $CFG, $DB;

if (!empty($courseids)) {
if (!empty($courseids) and $category = $DB->get_record('course_categories', array('id'=>$categoryid))) {
$courseids = array_reverse($courseids);
$i = 1;

$courseids = array_reverse($courseids);

foreach ($courseids as $courseid) {

if (! $course = $DB->get_record("course", array("id"=>$courseid))) {
notify("Error finding course $courseid");
} else {
// figure out a sortorder that we can use in the destination category
$sortorder = $DB->get_field_sql('SELECT MIN(sortorder)-1 AS min
FROM {course} WHERE category=?', array($categoryid));
if (is_null($sortorder) || $sortorder === false) {
// the category is empty
// rather than let the db default to 0
// set it to > 100 and avoid extra work in fix_coursesortorder()
$sortorder = 200;
} else if ($sortorder < 10) {
fix_course_sortorder($categoryid);
}

$course->category = $categoryid;
$course->sortorder = $sortorder;
$course->fullname = $course->fullname;
$course->shortname = $course->shortname;
$course->summary = $course->summary;
$course->password = $course->password;
$course->teacher = $course->teacher;
$course->teachers = $course->teachers;
$course->student = $course->student;
$course->students = $course->students;

if (!$DB->update_record('course', $course)) {
notify("An error occurred - course not moved!");
}
foreach ($courseids as $courseid) {
if (!$course = $DB->get_record("course", array("id"=>$courseid))) {
notify("Error finding course $courseid");
} else {
$course->category = $categoryid;
$course->sortorder = $category->sortorder + MAX_COURSES_IN_CATEGORY - $i++;

$context = get_context_instance(CONTEXT_COURSE, $course->id);
$newparent = get_context_instance(CONTEXT_COURSECAT, $course->category);
context_moved($context, $newparent);
if (!$DB->update_record('course', $course)) {
notify("An error occurred - course not moved!");
}

$context = get_context_instance(CONTEXT_COURSE, $course->id);
$newparent = get_context_instance(CONTEXT_COURSECAT, $course->category);
context_moved($context, $newparent);
}
fix_course_sortorder();
}
fix_course_sortorder();
}
return true;
}

Expand All @@ -2913,7 +2892,9 @@ function move_category ($category, $newparentcat) {
if (!$DB->set_field('course_categories', 'parent', 0, array('id'=>$category->id))) {
return false;
}

$newparent = get_context_instance(CONTEXT_SYSTEM);

} else {
if (!$DB->set_field('course_categories', 'parent', $newparentcat->id, array('id'=>$category->id))) {
return false;
Expand All @@ -2923,8 +2904,10 @@ function move_category ($category, $newparentcat) {

context_moved($context, $newparent);

// The most effective thing would be to find the common parent,
// until then, do it sitewide...
// now make it last in new category
$DB->set_field('course_categories', 'sortorder', MAX_COURSES_IN_CATEGORY*MAX_COURSE_CATEGORIES, array('id'=>$category->id));

// and fix the sortorders
fix_course_sortorder();

return true;
Expand Down Expand Up @@ -2990,12 +2973,8 @@ function create_course($data) {

$data->timecreated = time();

// place at beginning of category
fix_course_sortorder();
$data->sortorder = $DB->get_field_sql("SELECT MIN(sortorder)-1 FROM {course} WHERE category=?", array($data->category));
if (empty($data->sortorder)) {
$data->sortorder = 100;
}
// place at beginning of any category
$data->sortorder = 0;

if ($newcourseid = $DB->insert_record('course', $data)) { // Set up new course

Expand Down
Loading

0 comments on commit 0cbe811

Please sign in to comment.