|
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_outcome extends grade_object { 00039 public $table = 'grade_outcomes'; 00040 00045 public $required_fields = array('id', 'courseid', 'shortname', 'fullname', 'scaleid','description', 00046 'descriptionformat', 'timecreated', 'timemodified', 'usermodified'); 00047 00052 public $courseid; 00053 00058 public $shortname; 00059 00064 public $fullname; 00065 00070 public $scale; 00071 00076 public $scaleid; 00077 00082 public $description; 00083 00088 public $usermodified; 00089 00095 public function delete($source=null) { 00096 global $DB; 00097 if (!empty($this->courseid)) { 00098 $DB->delete_records('grade_outcomes_courses', array('outcomeid' => $this->id, 'courseid' => $this->courseid)); 00099 } 00100 if (parent::delete($source)) { 00101 $context = get_context_instance(CONTEXT_SYSTEM); 00102 $fs = get_file_storage(); 00103 $files = $fs->get_area_files($context->id, 'grade', 'outcome', $this->id); 00104 foreach ($files as $file) { 00105 $file->delete(); 00106 } 00107 return true; 00108 } 00109 return false; 00110 } 00111 00119 public function insert($source=null) { 00120 global $DB; 00121 00122 $this->timecreated = $this->timemodified = time(); 00123 00124 if ($result = parent::insert($source)) { 00125 if (!empty($this->courseid)) { 00126 $goc = new stdClass(); 00127 $goc->courseid = $this->courseid; 00128 $goc->outcomeid = $this->id; 00129 $DB->insert_record('grade_outcomes_courses', $goc); 00130 } 00131 } 00132 return $result; 00133 } 00134 00140 public function update($source=null) { 00141 $this->timemodified = time(); 00142 00143 if ($result = parent::update($source)) { 00144 if (!empty($this->courseid)) { 00145 $this->use_in($this->courseid); 00146 } 00147 } 00148 return $result; 00149 } 00150 00156 public function use_in($courseid) { 00157 global $DB; 00158 if (!empty($this->courseid) and $courseid != $this->courseid) { 00159 return false; 00160 } 00161 00162 if (!$DB->record_exists('grade_outcomes_courses', array('courseid' => $courseid, 'outcomeid' => $this->id))) { 00163 $goc = new stdClass(); 00164 $goc->courseid = $courseid; 00165 $goc->outcomeid = $this->id; 00166 $DB->insert_record('grade_outcomes_courses', $goc); 00167 } 00168 return true; 00169 } 00170 00178 public static function fetch($params) { 00179 return grade_object::fetch_helper('grade_outcomes', 'grade_outcome', $params); 00180 } 00181 00189 public static function fetch_all($params) { 00190 return grade_object::fetch_all_helper('grade_outcomes', 'grade_outcome', $params); 00191 } 00192 00197 public function load_scale() { 00198 if (empty($this->scale->id) or $this->scale->id != $this->scaleid) { 00199 $this->scale = grade_scale::fetch(array('id'=>$this->scaleid)); 00200 $this->scale->load_items(); 00201 } 00202 return $this->scale; 00203 } 00204 00210 public static function fetch_all_global() { 00211 if (!$outcomes = grade_outcome::fetch_all(array('courseid'=>null))) { 00212 $outcomes = array(); 00213 } 00214 return $outcomes; 00215 } 00216 00223 public static function fetch_all_local($courseid) { 00224 if (!$outcomes =grade_outcome::fetch_all(array('courseid'=>$courseid))) { 00225 $outcomes = array(); 00226 } 00227 return $outcomes; 00228 } 00229 00236 public static function fetch_all_available($courseid) { 00237 global $CFG, $DB; 00238 00239 $result = array(); 00240 $params = array($courseid); 00241 $sql = "SELECT go.* 00242 FROM {grade_outcomes} go, {grade_outcomes_courses} goc 00243 WHERE go.id = goc.outcomeid AND goc.courseid = ? 00244 ORDER BY go.id ASC"; 00245 00246 if ($datas = $DB->get_records_sql($sql, $params)) { 00247 foreach($datas as $data) { 00248 $instance = new grade_outcome(); 00249 grade_object::set_properties($instance, $data); 00250 $result[$instance->id] = $instance; 00251 } 00252 } 00253 return $result; 00254 } 00255 00256 00262 public function get_name() { 00263 return format_string($this->fullname); 00264 } 00265 00270 public function get_shortname() { 00271 return $this->shortname; 00272 } 00273 00278 public function get_description() { 00279 global $CFG; 00280 require_once($CFG->libdir . '/filelib.php'); 00281 00282 $options = new stdClass; 00283 $options->noclean = true; 00284 $systemcontext = get_context_instance(CONTEXT_SYSTEM); 00285 $description = file_rewrite_pluginfile_urls($this->description, 'pluginfile.php', $systemcontext->id, 'grade', 'outcome', $this->id); 00286 return format_text($description, $this->descriptionformat, $options); 00287 } 00288 00293 public function can_delete() { 00294 if ($this->get_item_uses_count()) { 00295 return false; 00296 } 00297 if (empty($this->courseid)) { 00298 if ($this->get_course_uses_count()) { 00299 return false; 00300 } 00301 } 00302 return true; 00303 } 00304 00309 public function get_course_uses_count() { 00310 global $DB; 00311 00312 if (!empty($this->courseid)) { 00313 return 1; 00314 } 00315 00316 return $DB->count_records('grade_outcomes_courses', array('outcomeid' => $this->id)); 00317 } 00318 00323 public function get_item_uses_count() { 00324 global $DB; 00325 return $DB->count_records('grade_items', array('outcomeid' => $this->id)); 00326 } 00327 00340 public function get_grade_info($courseid=null, $average=true, $items=false) { 00341 global $CFG, $DB; 00342 00343 if (!isset($this->id)) { 00344 debugging("You must setup the outcome's id before calling its get_grade_info() method!"); 00345 return false; // id must be defined for this to work 00346 } 00347 00348 if ($average === false && $items === false) { 00349 debugging('Either the 1st or 2nd param of grade_outcome::get_grade_info() must be true, or both, but not both false!'); 00350 return false; 00351 } 00352 00353 $params = array($this->id); 00354 00355 $wheresql = ''; 00356 if (!is_null($courseid)) { 00357 $wheresql = " AND {grade_items}.courseid = ? "; 00358 $params[] = $courseid; 00359 } 00360 00361 $selectadd = ''; 00362 if ($items !== false) { 00363 $selectadd = ", {grade_items}.* "; 00364 } 00365 00366 $sql = "SELECT finalgrade $selectadd 00367 FROM {grade_grades}, {grade_items}, {grade_outcomes} 00368 WHERE {grade_outcomes}.id = {grade_items}.outcomeid 00369 AND {grade_items}.id = {grade_grades}.itemid 00370 AND {grade_outcomes}.id = ? 00371 $wheresql"; 00372 00373 $grades = $DB->get_records_sql($sql, $params); 00374 $retval = array(); 00375 00376 if ($average !== false && count($grades) > 0) { 00377 $count = 0; 00378 $total = 0; 00379 00380 foreach ($grades as $k => $grade) { 00381 // Skip null finalgrades 00382 if (!is_null($grade->finalgrade)) { 00383 $total += $grade->finalgrade; 00384 $count++; 00385 } 00386 unset($grades[$k]->finalgrade); 00387 } 00388 00389 $retval['avg'] = $total / $count; 00390 } 00391 00392 if ($items !== false) { 00393 foreach ($grades as $grade) { 00394 $retval['items'][$grade->id] = new grade_item($grade); 00395 } 00396 } 00397 00398 return $retval; 00399 } 00400 }