Skip to content
Browse files

MDL-1883 - /course/scales.php very slow. "Backported" the fix by Petr…

… from 1.9 to 1.8. Not a direct merge as the code has been refactored.
  • Loading branch information...
1 parent f99ed7c commit 186b0a153733a90df2f264bca56a104408bd1f6e samulik committed Sep 24, 2007
Showing with 125 additions and 37 deletions.
  1. +20 −20 course/scales.php
  2. +43 −17 lib/moodlelib.php
  3. +10 −0 mod/assignment/lib.php
  4. +10 −0 mod/forum/lib.php
  5. +10 −0 mod/glossary/lib.php
  6. +11 −0 mod/hotpot/lib.php
  7. +10 −0 mod/journal/lib.php
  8. +11 −0 mod/lams/lib.php
View
40 course/scales.php
@@ -146,15 +146,15 @@
$scale->description = "";
}
- //Calculate the uses
+ //Check if the scale is in use
if ($scale->courseid == 0) {
- $scale_uses = site_scale_used($scale->id,$courses);
+ $scale_used = site_scale_used($scale->id,$courses);
} else {
- $scale_uses = course_scale_used($course->id,$scale->id);
+ $scale_used = course_scale_used($course->id,$scale->id);
}
- //Check for scale_uses
- if (!empty($scale_uses)) {
+ //Check for scale use
+ if ($scale_used)) {
error("Scale is in use and cannot be modified",$CFG->wwwroot.'/course/scales.php?id='.$course->id);
}
@@ -210,7 +210,7 @@
echo "<tr valign=\"top\">";
echo "<td align=\"right\">";
echo "</td>";
- echo "<td>".get_string("usedinnplaces","",$scale_uses);
+ echo "<td>$stractivities: ".($scale_used ? get_string("yes") : get_string("no"));
echo "</td>";
echo "</tr>";
}
@@ -240,15 +240,15 @@
error("Scale ID was incorrect");
}
- //Calculate the uses
+ //Check if the scale is in use
if ($scale->courseid == 0) {
- $scale_uses = site_scale_used($scale->id,$courses);
+ $scale_used = site_scale_used($scale->id,$courses);
} else {
- $scale_uses = course_scale_used($course->id,$scale->id);
+ $scale_used = course_scale_used($course->id,$scale->id);
}
- //Check for scale_uses
- if (!empty($scale_uses)) {
+ //Check for scale use
+ if ($scale_used) {
error("Scale is in use and cannot be deleted",$CFG->wwwroot.'/course/scales.php?id='.$course->id);
}
@@ -271,15 +271,15 @@
error("Scale ID was incorrect");
}
- //Calculate the uses
+ //Check if the scale is in use
if ($scale->courseid == 0) {
- $scale_uses = site_scale_used($scale->id,$courses);
+ $scale_used = site_scale_used($scale->id,$courses);
} else {
- $scale_uses = course_scale_used($course->id,$scale->id);
+ $scale_used = course_scale_used($course->id,$scale->id);
}
- //Check for scale_uses
- if (!empty($scale_uses)) {
+ //Check for scale use
+ if ($scale_used)) {
error("Scale is in use and cannot be moved",$CFG->wwwroot.'/course/scales.php?id='.$course->id);
}
@@ -424,18 +424,18 @@
$line = array();
$line[] = "<a target=\"scale\" title=\"$scale->name\" href=\"$CFG->wwwroot/course/scales.php?id=$course->id&amp;scaleid=$scale->id&amp;action=details\" "."onclick=\"return openpopup('/course/scales.php?id=$course->id\&amp;scaleid=$scale->id&amp;action=details', 'scale', 'menubar=0,location=0,scrollbars,resizable,width=600,height=450', 0);\">".$scale->name."</a><br /><font size=\"-1\">".str_replace(",",", ",$scale->scale)."</font>";
if (!empty($scale->courseid)) {
- $scales_uses = course_scale_used($course->id,$scale->id);
+ $scale_used = course_scale_used($course->id,$scale->id);
} else {
- $scales_uses = site_scale_used($scale->id,$courses);
+ $scale_used = site_scale_used($scale->id,$courses);
}
- $line[] = $scales_uses;
+ $line[] = $scale_used ? get_string("yes") : get_string("no");
if ($incustom) {
$line[] = $strcustomscale;
} else {
$line[] = $strstandardscale;
}
$buttons = "";
- if (empty($scales_uses) && ($incustom || has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM, SITEID)))) {
+ if (!$scale_used && ($incustom || has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM, SITEID)))) {
$buttons .= "<a title=\"$stredit\" href=\"$path/scales.php?id=$course->id&amp;scaleid=$scale->id&amp;action=edit\"><img".
" src=\"$CFG->pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"$stredit\" /></a> ";
if ($incustom && has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM, SITEID))) {
View
60 lib/moodlelib.php
@@ -5953,12 +5953,11 @@ function make_grades_menu($gradingtype) {
}
/**
- * This function returns the nummber of activities
- * using scaleid in a courseid
+ * Returns true if a scale is in use in the given course.
*
- * @param int $courseid ?
- * @param int $scaleid ?
- * @return int
+ * @param int $courseid The course to be checked.
+ * @param int $scaleid The scale to be checked.
+ * @return boolean
* @todo Finish documenting this function
*/
function course_scale_used($courseid, $scaleid) {
@@ -5976,42 +5975,69 @@ function course_scale_used($courseid, $scaleid) {
$function_name = $cm->modname.'_scale_used';
if (function_exists($function_name)) {
if ($function_name($cm->instance,$scaleid)) {
- $return++;
+ return true;
}
}
}
}
}
}
- return $return;
+ return false;
}
/**
- * This function returns the nummber of activities
- * using scaleid in the entire site
+ * Returns true if a scale is in use anywhere on the site.
*
- * @param int $scaleid ?
- * @return int
- * @todo Finish documenting this function. Is return type correct?
+ * @param int $scaleid The scale to be checked.
+ * @param array $courses The site courses, needed is the old way to check has to be used.
+ * @return boolean
*/
function site_scale_used($scaleid,&$courses) {
+ if (empty($scaleid)) {
+ return false;
+ }
+
global $CFG;
$return = 0;
- if (!is_array($courses) || count($courses) == 0) {
- $courses = get_courses("all",false,"c.id,c.shortname");
+ $legacy_mods = false;
+ if ($mods = get_records('modules', 'visible', 1)) {
+ foreach ($mods as $mod) {
+ //Check $mod->name/lib.php exists
+ if (file_exists($CFG->dirroot.'/mod/'.$mod->name.'/lib.php')) {
+ include_once($CFG->dirroot.'/mod/'.$mod->name.'/lib.php');
+ $function_name = $mod->name.'_scale_used_anywhere';
+ $old_function_name = $mod->name.'_scale_used';
+ if (function_exists($function_name)) {
+ if ($function_name($scaleid)) {
+ return true;
+ }
+ } else if (function_exists($old_function_name)) {
+ $legacy_mods = true;
+ debugging('Please notify the developer of module "'.$mod->name.'" that new function module_scale_used_anywhere() should be implemented.', DEBUG_DEVELOPER);
+ break;
+ }
+ }
+ }
}
- if (!empty($scaleid)) {
+ // some mods are missing the new xxx_scale_used_anywhere() - use the really slow old way
+ if ($legacy_mods) {
+ if (!is_array($courses) || count($courses) == 0) {
+ $courses = get_courses("all",false,"c.id,c.shortname");
+ }
if (is_array($courses) && count($courses) > 0) {
foreach ($courses as $course) {
- $return += course_scale_used($course->id,$scaleid);
+ if (course_scale_used($course->id,$scaleid)) {
+ return true;
+ }
}
}
}
- return $return;
+
+ return false;
}
/**
View
10 mod/assignment/lib.php
@@ -1981,6 +1981,16 @@ function assignment_scale_used ($assignmentid, $scaleid) {
}
/**
+ * Returns true if the scale is in use in the system.
+ *
+ * @param $scaleid int The scale to be counted.
+ * @return boolean
+ */
+function assignment_scale_used_anywhere($scaleid) {
+ return record_exists("assignment","grade","-$scaleid");
+}
+
+/**
* Make sure up-to-date events are created for all assignment instances
*
* This standard function will check all instances of this module
View
10 mod/forum/lib.php
@@ -1222,6 +1222,16 @@ function forum_scale_used ($forumid,$scaleid) {
return $return;
}
+/**
+ * Returns true if the scale is in use in the system.
+ *
+ * @param $scaleid int The scale to be counted.
+ * @return boolean
+ */
+function forum_scale_used_anywhere($scaleid) {
+ return record_exists("forum","scale","-$scaleid");
+}
+
// SQL FUNCTIONS ///////////////////////////////////////////////////////////
/**
View
10 mod/glossary/lib.php
@@ -401,6 +401,16 @@ function glossary_scale_used ($glossaryid,$scaleid) {
return $return;
}
+/**
+ * Returns true if the scale is in use in the system.
+ *
+ * @param $scaleid int The scale to be counted.
+ * @return boolean
+ */
+function glossary_scale_used_anywhere($scaleid) {
+ return record_exists("glossary","scale","-$scaleid");
+}
+
//////////////////////////////////////////////////////////////////////////////////////
/// Any other glossary functions go here. Each of them must have a name that
/// starts with glossary_
View
11 mod/hotpot/lib.php
@@ -1225,6 +1225,17 @@ function hotpot_scale_used ($hotpotid, $scaleid) {
return $report;
}
+/**
+ * Returns true if the scale is in use in the system.
+ *
+ * @param $scaleid int The scale to be counted.
+ * @return boolean
+ */
+function hotpot_scale_used_anywhere($scaleid) {
+// return record_exists("hotpot","scale","-$scaleid");
+ return false; // Scales are not used by Hotpot module
+}
+
//////////////////////////////////////////////////////////
/// Any other hotpot functions go here.
/// Each of them must have a name that starts with hotpot
View
10 mod/journal/lib.php
@@ -307,6 +307,16 @@ function journal_scale_used ($journalid,$scaleid) {
return $return;
}
+/**
+ * Returns true if the scale is in use in the system.
+ *
+ * @param $scaleid int The scale to be counted.
+ * @return boolean
+ */
+function journal_scale_used_anywhere($scaleid) {
+ return record_exists("journal","assessed","-$scaleid");
+}
+
// SQL FUNCTIONS ///////////////////////////////////////////////////////////////////
function journal_get_users_done($journal) {
View
11 mod/lams/lib.php
@@ -140,6 +140,17 @@ function lams_scale_used ($lamsid,$scaleid) {
return $return;
}
+/**
+ * Returns true if the scale is in use in the system.
+ *
+ * @param $scaleid int The scale to be counted.
+ * @return boolean
+ */
+function lams_scale_used_anywhere($scaleid) {
+// return record_exists("lams","scale","-$scaleid");
+ return false;
+}
+
//////////////////////////////////////////////////////////////////////////////////////
/// Any other lams functions go here. Each of them must have a name that
/// starts with lams_

0 comments on commit 186b0a1

Please sign in to comment.
Something went wrong with that request. Please try again.