]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-1883 improved perf when editing scales - new function xxx_scale_used_anywhere()
authorskodak <skodak>
Tue, 18 Sep 2007 18:37:58 +0000 (18:37 +0000)
committerskodak <skodak>
Tue, 18 Sep 2007 18:37:58 +0000 (18:37 +0000)
grade/edit/scale/edit_form.php
grade/edit/scale/index.php
lang/en_utf8/moodle.php
lib/grade/grade_scale.php
mod/assignment/lib.php
mod/forum/lib.php
mod/glossary/lib.php
mod/hotpot/lib.php
mod/journal/lib.php
mod/lams/lib.php

index e97bc24a38b8730df0ff5b4db1e5151231380ee9..88b045e4566e0877c447ea550efcef9da1f9bb36 100644 (file)
@@ -18,7 +18,7 @@ class edit_scale_form extends moodleform {
         $mform->setHelpButton('standard', array(false, get_string('scalestandard'),
                 false, true, false, get_string('scalestandardhelp', 'grades')));
 
-        $mform->addElement('static', 'activities', get_string('activities'));
+        $mform->addElement('static', 'used', get_string('used'));
 
         $mform->addElement('textarea', 'scale', get_string('scale'), array('cols'=>50, 'rows'=>2));
         $mform->setHelpButton('scale', array('scales', get_string('scale')));
@@ -55,9 +55,9 @@ class edit_scale_form extends moodleform {
 
         if ($id = $mform->getElementValue('id')) {
             $scale = grade_scale::fetch(array('id'=>$id));
-            $count = $scale->get_item_uses_count();
+            $used = $scale->is_used();
 
-            if ($count) {
+            if ($used) {
                 $mform->hardFreeze('scale');
             }
 
@@ -67,15 +67,16 @@ class edit_scale_form extends moodleform {
             } else if (empty($scale->courseid) and !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM))) {
                 $mform->hardFreeze('standard');
 
-            } else if ($count and !empty($scale->courseid)) {
+            } else if ($used and !empty($scale->courseid)) {
                 $mform->hardFreeze('standard');
             }
 
-            $activities_el =& $mform->getElement('activities');
-            $activities_el->setValue(get_string('usedinnplaces', '', $count));
+            $usedstr = $scale->is_used() ? get_string('yes') : get_string('no');
+            $used_el =& $mform->getElement('used');
+            $used_el->setValue($usedstr);
 
         } else {
-            $mform->removeElement('activities');
+            $mform->removeElement('used');
             if (empty($courseid) or !has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM))) {
                 $mform->hardFreeze('standard');
             }
index 300857884b032b3a725a7bf115a46d568d24326a..fd1eb272bd13792dee95f05989c32cf5147f3fe9 100644 (file)
@@ -37,7 +37,7 @@ $strname           = get_string('name');
 $strdelete         = get_string('delete');
 $stredit           = get_string('edit');
 $srtcreatenewscale = get_string('scalescustomcreate');
-$stritems          = get_string('items', 'grades');
+$strused           = get_string('used');
 $stredit           = get_string('edit');
 
 switch ($action) {
@@ -82,20 +82,20 @@ if ($courseid and $scales = grade_scale::fetch_all_local($courseid)) {
         $line = array();
         $line[] = format_string($scale->name).'<div class="scale_options">'.str_replace(",",", ",$scale->scale).'</div>';
 
-        $scales_uses = $scale->get_item_uses_count();
-        $line[] = $scales_uses;
+        $used = $scale->is_used();
+        $line[] = $used ? get_string('yes') : get_string('no');
 
         $buttons = "";
         $buttons .= "<a title=\"$stredit\" href=\"edit.php?courseid=$courseid&amp;id=$scale->id\"><img".
                     " src=\"$CFG->pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"$stredit\" /></a> ";
-        if (empty($scales_uses)) {
+        if (!$used) {
             $buttons .= "<a title=\"$strdelete\" href=\"index.php?id=$courseid&amp;scaleid=$scale->id&amp;action=delete&amp;sesskey=$USER->sesskey\"><img".
                         " src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a> ";
         }
         $line[] = $buttons;
         $data[] = $line;
     }
-    $table->head  = array($strscale, $stritems, $stredit);
+    $table->head  = array($strscale, $strused, $stredit);
     $table->size  = array('70%', '20%', '10%');
     $table->align = array('left', 'center', 'center');
     $table->width = '90%';
@@ -110,22 +110,22 @@ if ($scales = grade_scale::fetch_all_global()) {
         $line = array();
         $line[] = format_string($scale->name).'<div class="scale_options">'.str_replace(",",", ",$scale->scale).'</div>';
 
-        $scales_uses = $scale->get_item_uses_count();
-        $line[] = $scales_uses;
+        $used = $scale->is_used();
+        $line[] = $used ? get_string('yes') : get_string('no');
 
         $buttons = "";
         if (has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM))) {
             $buttons .= "<a title=\"$stredit\" href=\"edit.php?courseid=$courseid&amp;id=$scale->id\"><img".
                         " src=\"$CFG->pixpath/t/edit.gif\" class=\"iconsmall\" alt=\"$stredit\" /></a> ";
         }
-        if (empty($scales_uses) and has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM))) {
+        if (!$used and has_capability('moodle/course:managescales', get_context_instance(CONTEXT_SYSTEM))) {
             $buttons .= "<a title=\"$strdelete\" href=\"index.php?id=$courseid&amp;scaleid=$scale->id&amp;action=delete&amp;sesskey=$USER->sesskey\"><img".
                         " src=\"$CFG->pixpath/t/delete.gif\" class=\"iconsmall\" alt=\"$strdelete\" /></a> ";
         }
         $line[] = $buttons;
         $data[] = $line;
     }
-    $table->head  = array($strscale, $stritems, $stredit);
+    $table->head  = array($strscale, $strused, $stredit);
     $table->size  = array('70%', '20%', '10%');
     $table->align = array('left', 'center', 'center');
     $table->width = '90%';
index 735d8d82e31657e7ce0010cd122ebfa0c064f1f6..64ec5eb282ee3e8a0036c8de12a442254014f945 100644 (file)
@@ -1489,6 +1489,7 @@ $string['uploadrenamedcollision'] = 'File was renamed from $a->oldname to $a->ne
 $string['uploadserverlimit'] = 'Uploaded file exceeded the maximum size limit set by the server';
 $string['uploadthisfile'] = 'Upload this file';
 $string['uploadusers'] = 'Upload users';
+$string['used'] = 'Used';
 $string['usedinnplaces'] = 'Used in $a places';
 $string['usemessageform'] = 'or use the form below to send a message to the selected students';
 $string['user'] = 'User';
index af1df4d0c243b9f2da49aecd8b9878f8c74ae5b3..703e060f2a45dbced13d3a5e477a14c5f5a3022e 100644 (file)
@@ -200,43 +200,64 @@ class grade_scale extends grade_object {
      * @return boolean
      */
     function can_delete() {
-        $count = $this->get_item_uses_count();
-        return empty($count);
+        return !$this->is_used();
     }
 
     /**
-     * Returns the number of places where scale is used - activities, grade items, outcomes, etc.
-     * @return int
+     * Returns if scale used anywhere - activities, grade items, outcomes, etc.
+     * @return bool
      */
-    function get_item_uses_count() {
+    function is_used() {
         global $CFG;
 
-//TODO: fix me - this methods does some duplicate counting in grade items and activities
-        $count = 0;
-        if (!empty($this->courseid)) {
-            if ($scales_uses = course_scale_used($this->courseid,$this->id)) {
-                $count += count($scales_uses);
-            }
-        } else {
-            $courses = array();
-            if ($scales_uses = site_scale_used($this->id,$courses)) {
-                $count += count($scales_uses);
-            }
-        }
-
-        // count grade items
+        // count grade items excluding the 
         $sql = "SELECT COUNT(id) FROM {$CFG->prefix}grade_items WHERE scaleid = {$this->id} AND outcomeid IS NULL";
-        if ($scales_uses = count_records_sql($sql)) {
-            $count += $scales_uses;
+        if (count_records_sql($sql)) {
+            return true;
         }
 
         // count outcomes
         $sql = "SELECT COUNT(id) FROM {$CFG->prefix}grade_outcomes WHERE scaleid = {$this->id}";
-        if ($scales_uses = count_records_sql($sql)) {
-            $count += $scales_uses;
+        if (count_records_sql($sql)) {
+            return true;
+        }
+
+        $legacy_mods = false;
+        if ($mods = get_records('modules', 'visible', 1)) {
+            foreach ($mods as $mod) {
+                //Check cm->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($this->id)) {
+                            return true;
+                        }
+
+                    } else if (function_exists($old_function_name)) {
+                        $legacy_mods = true;
+                        break;
+                    }
+                }
+            }
+        }
+
+        // some mods are missing the new xxx_scale_used_anywhere() - use the really slow old way
+        if ($legacy_mods) {
+            if (!empty($this->courseid)) {
+                if (course_scale_used($this->courseid,$this->id)) {
+                    return true;
+                }
+            } else {
+                $courses = array();
+                if (site_scale_used($this->id,$courses)) {
+                    return true;
+                }
+            }
         }
 
-        return $count;
+        return false;
     }
 }
 ?>
index 6aed46fe3e0b81a286586c37e7ced4de7e8ed6c0..67dca3c5657a1919e3edfd2b2f1a6b171151c1cb 100644 (file)
@@ -2234,7 +2234,7 @@ function assignment_get_participants($assignmentid) {
  * @param $scaleid int
  * @return boolean True if the scale is used by the assignment
  */
-function assignment_scale_used ($assignmentid, $scaleid) {
+function assignment_scale_used($assignmentid, $scaleid) {
 
     $return = false;
 
@@ -2247,6 +2247,21 @@ function assignment_scale_used ($assignmentid, $scaleid) {
     return $return;
 }
 
+/**
+ * Checks if scale is being used by any instance of assignment
+ *
+ * This is used to find out if scale used anywhere
+ * @param $scaleid int
+ * @return boolean True if the scale is used by any assignment
+ */
+function assignment_scale_used_anywhere($scaleid) {
+    if ($scaleid and record_exists('assignment', 'grade', -$scaleid)) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
 /**
  * Make sure up-to-date events are created for all assignment instances
  *
index 123fdb246290544c9468a31e1db83c8e775090f3..30a64099e3560da8234fd7c50da7cb53736ba1af 100644 (file)
@@ -1272,6 +1272,21 @@ function forum_scale_used ($forumid,$scaleid) {
     return $return;
 }
 
+/**
+ * Checks if scale is being used by any instance of forum
+ *
+ * This is used to find out if scale used anywhere
+ * @param $scaleid int
+ * @return boolean True if the scale is used by any forum
+ */
+function forum_scale_used_anywhere($scaleid) {
+    if ($scaleid and record_exists('forum', 'scale', -$scaleid)) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
 // SQL FUNCTIONS ///////////////////////////////////////////////////////////
 
 /**
index d54b508d877d8bb0957908d492bea1c04a7e1c28..395b67944c0a7c518ea78ed12e1ebe7b39a564b0 100644 (file)
@@ -444,6 +444,21 @@ function glossary_scale_used ($glossaryid,$scaleid) {
     return $return;
 }
 
+/**
+ * Checks if scale is being used by any instance of glossary
+ *
+ * This is used to find out if scale used anywhere
+ * @param $scaleid int
+ * @return boolean True if the scale is used by any glossary
+ */
+function glossary_scale_used_anywhere($scaleid) {
+    if ($scaleid and record_exists('glossary', 'scale', -$scaleid)) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
 //////////////////////////////////////////////////////////////////////////////////////
 /// Any other glossary functions go here.  Each of them must have a name that
 /// starts with glossary_
index c22d6e934b9fb5d504e7e654b71a8578013d02cf..1df8537a21c1e4093cc31a879bc7e85d565a5f79 100644 (file)
@@ -1217,6 +1217,17 @@ function hotpot_scale_used ($hotpotid, $scaleid) {
     return $report;
 }
 
+/**
+ * Checks if scale is being used by any instance of hotpot
+ *
+ * This is used to find out if scale used anywhere
+ * @param $scaleid int
+ * @return boolean True if the scale is used by any hotpot
+ */
+function hotpot_scale_used_anywhere($scaleid) {
+ return false;
+}
+
 //////////////////////////////////////////////////////////
 /// Any other hotpot functions go here.
 /// Each of them must have a name that starts with hotpot
index a542bd11a5db708e804142fe3ab2300d6208b8f6..44ba271236b55cd9d1abd2cbb414e99916205a34 100644 (file)
@@ -307,6 +307,21 @@ function journal_scale_used ($journalid,$scaleid) {
     return $return;
 }
 
+/**
+ * Checks if scale is being used by any instance of journal
+ *
+ * This is used to find out if scale used anywhere
+ * @param $scaleid int
+ * @return boolean True if the scale is used by any journal
+ */
+function journal_scale_used_anywhere($scaleid) {
+    if ($scaleid and record_exists('journal', 'assessed', -$scaleid)) {
+        return true;
+    } else {
+        return false;
+    }
+}
+
 // SQL FUNCTIONS ///////////////////////////////////////////////////////////////////
 
 function journal_get_users_done($journal) {
index f96f32877a977897c219b2af8736c1162bdf710e..06244abbbe68aa0647a9edec599a445ff430c7b1 100644 (file)
@@ -140,6 +140,17 @@ function lams_scale_used ($lamsid,$scaleid) {
   return $return;
 }
 
+/**
+ * Checks if scale is being used by any instance of lams
+ *
+ * This is used to find out if scale used anywhere
+ * @param $scaleid int
+ * @return boolean True if the scale is used by any lams
+ */
+function lams_scale_used_anywhere($scaleid) {
+ return false;
+}
+
 //////////////////////////////////////////////////////////////////////////////////////
 /// Any other lams functions go here.  Each of them must have a name that
 /// starts with lams_