|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00026 defined('MOODLE_INTERNAL') || die(); 00027 00028 require_once('grade_object.php'); 00029 00034 class grade_scale extends grade_object { 00039 public $table = 'scale'; 00040 00045 public $required_fields = array('id', 'courseid', 'userid', 'name', 'scale', 'description', 'descriptionformat', 'timemodified'); 00046 00051 public $courseid; 00052 00053 public $userid; 00054 00059 public $name; 00060 00065 public $scale_items = array(); 00066 00071 public $scale; 00072 00077 public $description; 00078 00086 public static function fetch($params) { 00087 return grade_object::fetch_helper('scale', 'grade_scale', $params); 00088 } 00089 00097 public static function fetch_all($params) { 00098 return grade_object::fetch_all_helper('scale', 'grade_scale', $params); 00099 } 00100 00108 public function insert($source=null) { 00109 $this->timecreated = time(); 00110 $this->timemodified = time(); 00111 return parent::insert($source); 00112 } 00113 00119 public function update($source=null) { 00120 $this->timemodified = time(); 00121 return parent::update($source); 00122 } 00123 00129 public function delete($source=null) { 00130 global $DB; 00131 if (parent::delete($source)) { 00132 $context = get_context_instance(CONTEXT_SYSTEM); 00133 $fs = get_file_storage(); 00134 $files = $fs->get_area_files($context->id, 'grade', 'scale', $this->id); 00135 foreach ($files as $file) { 00136 $file->delete(); 00137 } 00138 return true; 00139 } 00140 return false; 00141 } 00142 00148 public function get_name() { 00149 return format_string($this->name); 00150 } 00151 00162 public function load_items($items=NULL) { 00163 if (empty($items)) { 00164 $this->scale_items = explode(',', $this->scale); 00165 } elseif (is_array($items)) { 00166 $this->scale_items = $items; 00167 } else { 00168 $this->scale_items = explode(',', $items); 00169 } 00170 00171 // Trim whitespace around each value 00172 foreach ($this->scale_items as $key => $val) { 00173 $this->scale_items[$key] = trim($val); 00174 } 00175 00176 return $this->scale_items; 00177 } 00178 00192 public function compact_items($items=NULL) { 00193 if (empty($items)) { 00194 $this->scale = implode(',', $this->scale_items); 00195 } elseif (is_array($items)) { 00196 $this->scale = implode(',', $items); 00197 } else { 00198 $this->scale = $items; 00199 } 00200 00201 return $this->scale; 00202 } 00203 00212 public function get_nearest_item($grade) { 00213 global $DB; 00214 // Obtain nearest scale item from average 00215 $scales_array = $DB->get_records('scale', array('id' => $this->id)); 00216 $scale = $scales_array[$this->id]; 00217 $scales = explode(",", $scale->scale); 00218 00219 // this could be a 0 when summed and rounded, e.g, 1, no grade, no grade, no grade 00220 if ($grade < 1) { 00221 $grade = 1; 00222 } 00223 00224 return $scales[$grade-1]; 00225 } 00226 00231 public function fetch_all_global() { 00232 return grade_scale::fetch_all(array('courseid'=>0)); 00233 } 00234 00239 public static function fetch_all_local($courseid) { 00240 return grade_scale::fetch_all(array('courseid'=>$courseid)); 00241 } 00242 00247 public function can_delete() { 00248 return !$this->is_used(); 00249 } 00250 00255 public function is_used() { 00256 global $DB; 00257 global $CFG; 00258 00259 // count grade items excluding the 00260 $params = array($this->id); 00261 $sql = "SELECT COUNT(id) FROM {grade_items} WHERE scaleid = ? AND outcomeid IS NULL"; 00262 if ($DB->count_records_sql($sql, $params)) { 00263 return true; 00264 } 00265 00266 // count outcomes 00267 $sql = "SELECT COUNT(id) FROM {grade_outcomes} WHERE scaleid = ?"; 00268 if ($DB->count_records_sql($sql, $params)) { 00269 return true; 00270 } 00271 00272 $legacy_mods = false; 00273 if ($mods = $DB->get_records('modules', array('visible' => 1))) { 00274 foreach ($mods as $mod) { 00275 //Check cm->name/lib.php exists 00276 if (file_exists($CFG->dirroot.'/mod/'.$mod->name.'/lib.php')) { 00277 include_once($CFG->dirroot.'/mod/'.$mod->name.'/lib.php'); 00278 $function_name = $mod->name.'_scale_used_anywhere'; 00279 $old_function_name = $mod->name.'_scale_used'; 00280 if (function_exists($function_name)) { 00281 if ($function_name($this->id)) { 00282 return true; 00283 } 00284 00285 } else if (function_exists($old_function_name)) { 00286 $legacy_mods = true; 00287 debugging('Please notify the developer of module "'.$mod->name.'" that new function module_scale_used_anywhere() should be implemented.', DEBUG_DEVELOPER); 00288 break; 00289 } 00290 } 00291 } 00292 } 00293 00294 // some mods are missing the new xxx_scale_used_anywhere() - use the really slow old way 00295 if ($legacy_mods) { 00296 if (!empty($this->courseid)) { 00297 if (course_scale_used($this->courseid,$this->id)) { 00298 return true; 00299 } 00300 } else { 00301 $courses = array(); 00302 if (site_scale_used($this->id,$courses)) { 00303 return true; 00304 } 00305 } 00306 } 00307 00308 return false; 00309 } 00310 00315 public function get_description() { 00316 global $CFG; 00317 require_once($CFG->libdir . '/filelib.php'); 00318 00319 $systemcontext = get_context_instance(CONTEXT_SYSTEM); 00320 $options = new stdClass; 00321 $options->noclean = true; 00322 $description = file_rewrite_pluginfile_urls($this->description, 'pluginfile.php', $systemcontext->id, 'grade', 'scale', $this->id); 00323 return format_text($description, $this->descriptionformat, $options); 00324 } 00325 }