course/index: minor context-related fixes

- The code uses the system context a lot. Declare
   $sysctx at the top and use it.

 - If the category has a context property, use it
   (saves 1 DB query per category displayed)

The DB costs of this page in editing mode are

 - ~100 DB queries for fix_course_sortorder()

 - ~50 DB queries for the categories listing

If we rewrite both to lose the recursion, we could resolve the
page in perhaps 5 DBq.
This commit is contained in:
martinlanghoff 2007-09-19 07:27:31 +00:00
parent 40fb8aa69a
commit dfbf98cdb3

View file

@ -15,8 +15,9 @@
$moveto = optional_param('moveto',-1,PARAM_INT); $moveto = optional_param('moveto',-1,PARAM_INT);
$moveup = optional_param('moveup',0,PARAM_INT); $moveup = optional_param('moveup',0,PARAM_INT);
$movedown = optional_param('movedown',0,PARAM_INT); $movedown = optional_param('movedown',0,PARAM_INT);
$context = get_context_instance(CONTEXT_SYSTEM, SITEID); $sysctx = get_context_instance(CONTEXT_SYSTEM);
$context = $sysctx;
if (!$site = get_site()) { if (!$site = get_site()) {
error('Site isn\'t defined!'); error('Site isn\'t defined!');
@ -26,7 +27,7 @@
require_login(); require_login();
} }
if (has_capability('moodle/category:update', get_context_instance(CONTEXT_SYSTEM, SITEID))) { if (has_capability('moodle/category:update', $sysctx)) {
if ($categoryedit !== -1) { if ($categoryedit !== -1) {
$USER->categoryediting = $categoryedit; $USER->categoryediting = $categoryedit;
} }
@ -91,16 +92,16 @@
print_box_end(); print_box_end();
} }
/// I am not sure this context in the next has_capability call is correct. /// I am not sure this context in the next has_capability call is correct.
if (isloggedin() and !isguest() and !has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID)) and $CFG->enablecourserequests) { // Print link to request a new course if (isloggedin() and !isguest() and !has_capability('moodle/course:create', $sysctx) and $CFG->enablecourserequests) { // Print link to request a new course
print_single_button('request.php', NULL, get_string('courserequest'), 'get'); print_single_button('request.php', NULL, get_string('courserequest'), 'get');
} }
if (has_capability('moodle/course:create', get_context_instance(CONTEXT_SYSTEM, SITEID))) { // Print link to create a new course if (has_capability('moodle/course:create', $sysctx)) { // Print link to create a new course
/// Get the 1st available category /// Get the 1st available category
$options = array('category' => get_field('course_categories', 'id', 'parent', '0')); $options = array('category' => get_field('course_categories', 'id', 'parent', '0'));
print_single_button('edit.php', $options, get_string('addnewcourse'), 'get'); print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
} }
if (has_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM, SITEID)) and !empty($CFG->enablecourserequests)) { if (has_capability('moodle/site:approvecourse', $sysctx) and !empty($CFG->enablecourserequests)) {
print_single_button('pending.php',NULL, get_string('coursespending'),'get'); print_single_button('pending.php',NULL, get_string('coursespending'),'get');
} }
print_footer(); print_footer();
@ -111,7 +112,7 @@
/// Print headings /// Print headings
if (has_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM))) { if (has_capability('moodle/site:config', $sysctx)) {
require_once($CFG->libdir.'/adminlib.php'); require_once($CFG->libdir.'/adminlib.php');
admin_externalpage_setup('coursemgmt'); admin_externalpage_setup('coursemgmt');
admin_externalpage_print_header(); admin_externalpage_print_header();
@ -164,7 +165,11 @@
"index.php?delete=$delete&sure=".md5($deletecat->timemodified)."&sesskey=$USER->sesskey", "index.php?delete=$delete&sure=".md5($deletecat->timemodified)."&sesskey=$USER->sesskey",
"index.php?sesskey=$USER->sesskey"); "index.php?sesskey=$USER->sesskey");
print_footer(); if (has_capability('moodle/site:config', $sysctx)) {
admin_externalpage_print_footer($adminroot);
} else {
print_footer();
}
exit(); exit();
} }
} }
@ -235,6 +240,7 @@
} }
$swapcategory = $category; $swapcategory = $category;
} }
unset($category);
} }
} }
if (!empty($movedown)) { if (!empty($movedown)) {
@ -251,6 +257,7 @@
$choosenext = true; $choosenext = true;
} }
} }
unset($category);
} }
} }
if ($swapcategory and $movecategory) { // Renumber everything for robustness if ($swapcategory and $movecategory) { // Renumber everything for robustness
@ -266,6 +273,7 @@
notify('Could not update that category!'); notify('Could not update that category!');
} }
} }
unset($category);
} }
} }
@ -309,11 +317,13 @@
/// Print link to create a new course /// Print link to create a new course
if (has_capability('moodle/course:create', $context)) { if (has_capability('moodle/course:create', $context)) {
unset($options); unset($options);
$options['category'] = $category->id; if (!empty($category->id)) {
print_single_button('edit.php', $options, get_string('addnewcourse'), 'get'); $options['category'] = $category->id;
print_single_button('edit.php', $options, get_string('addnewcourse'), 'get');
}
} }
if (has_capability('moodle/site:approvecourse', get_context_instance(CONTEXT_SYSTEM, SITEID)) and !empty($CFG->enablecourserequests)) { if (has_capability('moodle/site:approvecourse', $sysctx) and !empty($CFG->enablecourserequests)) {
print_single_button('pending.php',NULL, get_string('coursespending'), 'get'); print_single_button('pending.php',NULL, get_string('coursespending'), 'get');
} }
// admin page does not allow custom buttons in the navigation bar // admin page does not allow custom buttons in the navigation bar
@ -321,8 +331,11 @@
echo update_categories_button(); echo update_categories_button();
echo '</div></div>'; echo '</div></div>';
print_footer(); if (has_capability('moodle/site:config', $sysctx)) {
admin_externalpage_print_footer($adminroot);
} else {
print_footer();
}
function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) { function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $up=false, $down=false) {
/// Recursive function to print all the categories ready for editing /// Recursive function to print all the categories ready for editing
@ -342,7 +355,9 @@ function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $
if ($category) { if ($category) {
$context = get_context_instance(CONTEXT_COURSECAT, $category->id); if (!isset($category->context)) {
$category->context = get_context_instance(CONTEXT_COURSECAT, $category->id);
}
echo '<tr><td align="left" class="name">'; echo '<tr><td align="left" class="name">';
for ($i=0; $i<$depth;$i++) { for ($i=0; $i<$depth;$i++) {
@ -358,12 +373,12 @@ function print_category_edit($category, $displaylist, $parentslist, $depth=-1, $
echo '<td class="icons">'; /// Print little icons echo '<td class="icons">'; /// Print little icons
if (has_capability('moodle/category:delete', $context)) { if (has_capability('moodle/category:delete', $category->context)) {
echo '<a title="'.$str->delete.'" href="index.php?delete='.$category->id.'&amp;sesskey='.sesskey().'"><img'. echo '<a title="'.$str->delete.'" href="index.php?delete='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
' src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$str->delete.'" /></a> '; ' src="'.$CFG->pixpath.'/t/delete.gif" class="iconsmall" alt="'.$str->delete.'" /></a> ';
} }
if (has_capability('moodle/category:visibility', $context)) { if (has_capability('moodle/category:visibility', $category->context)) {
if (!empty($category->visible)) { if (!empty($category->visible)) {
echo '<a title="'.$str->hide.'" href="index.php?hide='.$category->id.'&amp;sesskey='.sesskey().'"><img'. echo '<a title="'.$str->hide.'" href="index.php?hide='.$category->id.'&amp;sesskey='.sesskey().'"><img'.
' src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$str->hide.'" /></a> '; ' src="'.$CFG->pixpath.'/t/hide.gif" class="iconsmall" alt="'.$str->hide.'" /></a> ';