From 322f3141556db52ff865f7e615bfb558b820d784 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Wed, 16 Mar 2016 13:15:36 +0800 Subject: [PATCH] MDL-51867 scales: any plugin type can declare a scale as used --- lib/deprecatedlib.php | 74 +++++++++++++++++++++++++++++++++++++++ lib/grade/grade_scale.php | 36 +++---------------- lib/moodlelib.php | 66 ---------------------------------- lib/upgrade.txt | 1 + 4 files changed, 80 insertions(+), 97 deletions(-) diff --git a/lib/deprecatedlib.php b/lib/deprecatedlib.php index 5b806b14705..49fb9be474e 100644 --- a/lib/deprecatedlib.php +++ b/lib/deprecatedlib.php @@ -4408,3 +4408,77 @@ function get_clam_error_code($returncode) { $antivirus = \core\antivirus\manager::get_antivirus('clamav'); return $antivirus->get_clam_error_code($returncode); } + +/** + * This function returns the number of activities using the given scale in the given course. + * + * @deprecated since Moodle 3.1 + * @param int $courseid The course ID to check. + * @param int $scaleid The scale ID to check + * @return int + */ +function course_scale_used($courseid, $scaleid) { + global $CFG, $DB; + + debugging('course_scale_used() is deprecated and never used, plugins must implement _scale_used_anywhere, '. + 'all implementations of _scale_used are now ignored', DEBUG_DEVELOPER); + + $return = 0; + + if (!empty($scaleid)) { + if ($cms = get_course_mods($courseid)) { + foreach ($cms as $cm) { + // Check cm->name/lib.php exists. + if (file_exists($CFG->dirroot.'/mod/'.$cm->modname.'/lib.php')) { + include_once($CFG->dirroot.'/mod/'.$cm->modname.'/lib.php'); + $functionname = $cm->modname.'_scale_used'; + if (function_exists($functionname)) { + if ($functionname($cm->instance, $scaleid)) { + $return++; + } + } + } + } + } + + // Check if any course grade item makes use of the scale. + $return += $DB->count_records('grade_items', array('courseid' => $courseid, 'scaleid' => $scaleid)); + + // Check if any outcome in the course makes use of the scale. + $return += $DB->count_records_sql("SELECT COUNT('x') + FROM {grade_outcomes_courses} goc, + {grade_outcomes} go + WHERE go.id = goc.outcomeid + AND go.scaleid = ? AND goc.courseid = ?", + array($scaleid, $courseid)); + } + return $return; +} + +/** + * This function returns the number of activities using scaleid in the entire site + * + * @deprecated since Moodle 3.1 + * @param int $scaleid + * @param array $courses + * @return int + */ +function site_scale_used($scaleid, &$courses) { + $return = 0; + + debugging('site_scale_used() is deprecated and never used, plugins must implement _scale_used_anywhere, '. + 'all implementations of _scale_used are now ignored', DEBUG_DEVELOPER); + + if (!is_array($courses) || count($courses) == 0) { + $courses = get_courses("all", false, "c.id, c.shortname"); + } + + if (!empty($scaleid)) { + if (is_array($courses) && count($courses) > 0) { + foreach ($courses as $course) { + $return += course_scale_used($course->id, $scaleid); + } + } + } + return $return; +} diff --git a/lib/grade/grade_scale.php b/lib/grade/grade_scale.php index 46167a9ce2e..cd1cb2345d5 100644 --- a/lib/grade/grade_scale.php +++ b/lib/grade/grade_scale.php @@ -289,37 +289,11 @@ class grade_scale extends grade_object { return true; } - $legacy_mods = false; - if ($mods = $DB->get_records('modules', array('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; - debugging('Please notify the developer of module "'.$mod->name.'" that new function module_scale_used_anywhere() should be implemented.', DEBUG_DEVELOPER); - 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)) { + // Ask all plugins if the scale is used anywhere. + $pluginsfunction = get_plugins_with_function('scale_used_anywhere'); + foreach ($pluginsfunction as $plugintype => $plugins) { + foreach ($plugins as $pluginfunction) { + if ($pluginfunction($this->id)) { return true; } } diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 4ce2921f721..45602948161 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -8156,72 +8156,6 @@ function make_grades_menu($gradingtype) { return $grades; } -/** - * This function returns the number of activities using the given scale in the given course. - * - * @param int $courseid The course ID to check. - * @param int $scaleid The scale ID to check - * @return int - */ -function course_scale_used($courseid, $scaleid) { - global $CFG, $DB; - - $return = 0; - - if (!empty($scaleid)) { - if ($cms = get_course_mods($courseid)) { - foreach ($cms as $cm) { - // Check cm->name/lib.php exists. - if (file_exists($CFG->dirroot.'/mod/'.$cm->modname.'/lib.php')) { - include_once($CFG->dirroot.'/mod/'.$cm->modname.'/lib.php'); - $functionname = $cm->modname.'_scale_used'; - if (function_exists($functionname)) { - if ($functionname($cm->instance, $scaleid)) { - $return++; - } - } - } - } - } - - // Check if any course grade item makes use of the scale. - $return += $DB->count_records('grade_items', array('courseid' => $courseid, 'scaleid' => $scaleid)); - - // Check if any outcome in the course makes use of the scale. - $return += $DB->count_records_sql("SELECT COUNT('x') - FROM {grade_outcomes_courses} goc, - {grade_outcomes} go - WHERE go.id = goc.outcomeid - AND go.scaleid = ? AND goc.courseid = ?", - array($scaleid, $courseid)); - } - return $return; -} - -/** - * This function returns the number of activities using scaleid in the entire site - * - * @param int $scaleid - * @param array $courses - * @return int - */ -function site_scale_used($scaleid, &$courses) { - $return = 0; - - if (!is_array($courses) || count($courses) == 0) { - $courses = get_courses("all", false, "c.id, c.shortname"); - } - - if (!empty($scaleid)) { - if (is_array($courses) && count($courses) > 0) { - foreach ($courses as $course) { - $return += course_scale_used($course->id, $scaleid); - } - } - } - return $return; -} - /** * make_unique_id_code * diff --git a/lib/upgrade.txt b/lib/upgrade.txt index 72e47f20d7f..9424010dc98 100644 --- a/lib/upgrade.txt +++ b/lib/upgrade.txt @@ -81,6 +81,7 @@ information provided here is intended especially for developers. * \repository::antivir_scan_file() has been deprecated, \core\antivirus\manager::scan_file() that applies antivirus plugins is replacing its functionality. * Added core_text::str_max_bytes() which safely truncates multi-byte strings to a maximum number of bytes. +* Any plugin can report when a scale is being used with the callback function [pluginname]_scale_used_anywhere(int $scaleid). === 3.0 ===