mirror of
https://github.com/moodle/moodle.git
synced 2025-04-21 00:12:56 +02:00
MDL-1883 improved perf when editing scales - new function xxx_scale_used_anywhere()
This commit is contained in:
parent
423c029090
commit
85c9ebb926
@ -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');
|
||||
}
|
||||
|
@ -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&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&scaleid=$scale->id&action=delete&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&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&scaleid=$scale->id&action=delete&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%';
|
||||
|
@ -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';
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
return $count;
|
||||
$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 false;
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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 ///////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
|
@ -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_
|
||||
|
@ -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
|
||||
|
@ -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) {
|
||||
|
@ -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_
|
||||
|
Loading…
x
Reference in New Issue
Block a user