2009-11-01 11:42:23 +00:00
< ? php
2007-05-01 05:47:10 +00:00
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
2007-11-07 17:59:33 +00:00
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
2007-05-01 05:47:10 +00:00
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////
require_once ( 'grade_object.php' );
/**
2007-05-03 08:51:48 +00:00
* Class representing a grade scale . It is responsible for handling its DB representation ,
2007-05-01 05:47:10 +00:00
* modifying and returning its metadata .
*/
class grade_scale extends grade_object {
/**
* DB Table ( used by grade_object ) .
* @ var string $table
*/
2008-05-23 14:52:50 +00:00
public $table = 'scale' ;
2007-06-23 16:51:09 +00:00
2007-05-01 05:47:10 +00:00
/**
2007-09-22 11:39:59 +00:00
* Array of required table fields , must start with 'id' .
* @ var array $required_fields
2007-05-01 05:47:10 +00:00
*/
2009-11-04 06:14:06 +00:00
public $required_fields = array ( 'id' , 'courseid' , 'userid' , 'name' , 'scale' , 'description' , 'descriptionformat' , 'timemodified' );
2007-06-23 16:51:09 +00:00
2007-05-01 05:47:10 +00:00
/**
* The course this scale belongs to .
* @ var int $courseid
*/
2008-05-23 14:52:50 +00:00
public $courseid ;
2007-06-23 16:51:09 +00:00
2008-05-23 14:52:50 +00:00
public $userid ;
2007-06-24 22:26:33 +00:00
2007-05-01 05:47:10 +00:00
/**
* The name of the scale .
* @ var string $name
*/
2008-05-23 14:52:50 +00:00
public $name ;
2007-05-01 05:47:10 +00:00
/**
* The items in this scale .
* @ var array $scale_items
*/
2008-05-23 14:52:50 +00:00
public $scale_items = array ();
2007-05-01 05:47:10 +00:00
/**
2010-05-21 18:57:05 +00:00
* A string representation of the scale items ( a comma - separated list ) .
2007-05-01 05:47:10 +00:00
* @ var string $scale
*/
2008-05-23 14:52:50 +00:00
public $scale ;
2007-05-01 05:47:10 +00:00
/**
* A description for this scale .
* @ var string $description
*/
2008-05-23 14:52:50 +00:00
public $description ;
2007-06-23 16:51:09 +00:00
2007-05-04 07:40:51 +00:00
/**
2007-06-24 22:26:33 +00:00
* Finds and returns a grade_scale instance based on params .
2007-06-23 16:33:17 +00:00
* @ static
2007-05-04 07:40:51 +00:00
*
2007-06-24 22:26:33 +00:00
* @ param array $params associative arrays varname => value
* @ return object grade_scale instance or false if none found .
2007-05-04 07:40:51 +00:00
*/
2008-05-23 14:52:50 +00:00
public static function fetch ( $params ) {
2007-11-22 05:42:46 +00:00
return grade_object :: fetch_helper ( 'scale' , 'grade_scale' , $params );
2007-06-24 22:26:33 +00:00
}
2007-06-23 16:33:17 +00:00
2007-06-24 22:26:33 +00:00
/**
* Finds and returns all grade_scale instances based on params .
* @ static
*
* @ param array $params associative arrays varname => value
2010-05-21 18:57:05 +00:00
* @ return array array of grade_scale instances or false if none found .
2007-06-24 22:26:33 +00:00
*/
2008-05-23 14:52:50 +00:00
public static function fetch_all ( $params ) {
2007-11-22 05:42:46 +00:00
return grade_object :: fetch_all_helper ( 'scale' , 'grade_scale' , $params );
2007-05-04 07:40:51 +00:00
}
2007-06-23 16:51:09 +00:00
2007-11-01 08:25:05 +00:00
/**
* Records this object in the Database , sets its id to the returned value , and returns that value .
* If successful this function also fetches the new object data from database and stores it
* in object properties .
* @ param string $source from where was the object inserted ( mod / forum , manual , etc . )
* @ return int PK ID if successful , false otherwise
*/
2008-05-23 14:52:50 +00:00
public function insert ( $source = null ) {
2007-11-07 17:59:33 +00:00
$this -> timecreated = time ();
2007-11-01 08:25:05 +00:00
$this -> timemodified = time ();
return parent :: insert ( $source );
}
/**
* In addition to update () it also updates grade_outcomes_courses if needed
* @ param string $source from where was the object inserted
* @ return boolean success
*/
2008-05-23 14:52:50 +00:00
public function update ( $source = null ) {
2007-11-01 08:25:05 +00:00
$this -> timemodified = time ();
return parent :: update ( $source );
}
2009-11-04 06:14:06 +00:00
/**
* Deletes this outcome from the database .
* @ param string $source from where was the object deleted ( mod / forum , manual , etc . )
* @ return boolean success
*/
public function delete ( $source = null ) {
global $DB ;
if ( parent :: delete ( $source )) {
2009-12-01 18:32:53 +00:00
$context = get_context_instance ( CONTEXT_SYSTEM );
2009-11-04 06:14:06 +00:00
$fs = get_file_storage ();
2010-07-03 13:37:13 +00:00
$files = $fs -> get_area_files ( $context -> id , 'grade' , 'scale' , $this -> id );
2009-11-04 06:14:06 +00:00
foreach ( $files as $file ) {
$file -> delete ();
}
return true ;
}
return false ;
}
2007-07-30 22:56:45 +00:00
/**
* Returns the most descriptive field for this object . This is a standard method used
* when we do not know the exact type of an object .
* @ return string name
*/
2008-05-23 14:52:50 +00:00
public function get_name () {
2007-07-30 22:56:45 +00:00
return format_string ( $this -> name );
}
2007-05-01 05:47:10 +00:00
/**
2007-06-23 16:51:09 +00:00
* Loads the scale ' s items into the $scale_items array .
2007-05-01 05:47:10 +00:00
* There are three ways to achieve this :
* 1. No argument given : The $scale string is already loaded and exploded to an array of items .
* 2. A string is given : A comma - separated list of items is exploded into an array of items .
* 3. An array of items is given and saved directly as the array of items for this scale .
*
* @ param mixed $items Could be null , a string or an array . The method behaves differently for each case .
* @ return array The resulting array of scale items or null if the method failed to produce one .
*/
2008-05-23 14:52:50 +00:00
public function load_items ( $items = NULL ) {
2007-05-01 05:47:10 +00:00
if ( empty ( $items )) {
$this -> scale_items = explode ( ',' , $this -> scale );
} elseif ( is_array ( $items )) {
$this -> scale_items = $items ;
} else {
$this -> scale_items = explode ( ',' , $items );
}
2007-06-23 16:51:09 +00:00
2007-05-01 05:47:10 +00:00
// Trim whitespace around each value
foreach ( $this -> scale_items as $key => $val ) {
$this -> scale_items [ $key ] = trim ( $val );
}
return $this -> scale_items ;
}
/**
* Compacts ( implodes ) the array of items in $scale_items into a comma - separated string , $scale .
* There are three ways to achieve this :
* 1. No argument given : The $scale_items array is already loaded and imploded to a string of items .
* 2. An array is given and is imploded into a string of items .
* 3. A string of items is given and saved directly as the $scale variable .
* NOTE : This method is the exact reverse of load_items , and their input / output should be interchangeable . However ,
* because load_items () trims the whitespace around the items , when the string is reconstructed these whitespaces will
* be missing . This is not an issue , but should be kept in mind when comparing the two strings .
*
* @ param mixed $items Could be null , a string or an array . The method behaves differently for each case .
* @ return array The resulting string of scale items or null if the method failed to produce one .
*/
2008-05-23 14:52:50 +00:00
public function compact_items ( $items = NULL ) {
2007-05-01 05:47:10 +00:00
if ( empty ( $items )) {
$this -> scale = implode ( ',' , $this -> scale_items );
} elseif ( is_array ( $items )) {
$this -> scale = implode ( ',' , $items );
} else {
$this -> scale = $items ;
}
2007-06-23 16:51:09 +00:00
return $this -> scale ;
}
2007-07-24 14:26:05 +00:00
/**
* When called on a loaded scale object ( with a valid id ) and given a float grade between
* the grademin and grademax , this method returns the scale item that falls closest to the
* float given ( which is usually an average of several grades on a scale ) . If the float falls
* below 1 but above 0 , it will be rounded up to 1.
* @ param float $grade
* @ return string
*/
2008-05-23 14:52:50 +00:00
public function get_nearest_item ( $grade ) {
global $DB ;
2007-07-24 14:26:05 +00:00
// Obtain nearest scale item from average
2009-02-13 08:07:27 +00:00
$scales_array = $DB -> get_records ( 'scale' , array ( 'id' => $this -> id ));
2007-07-24 14:26:05 +00:00
$scale = $scales_array [ $this -> id ];
$scales = explode ( " , " , $scale -> scale );
// this could be a 0 when summed and rounded, e.g, 1, no grade, no grade, no grade
if ( $grade < 1 ) {
$grade = 1 ;
}
return $scales [ $grade - 1 ];
}
2007-07-29 23:02:03 +00:00
/**
2007-07-30 22:56:45 +00:00
* Static function returning all global scales
* @ return object
*/
2008-05-23 14:52:50 +00:00
public function fetch_all_global () {
2007-11-22 05:42:46 +00:00
return grade_scale :: fetch_all ( array ( 'courseid' => 0 ));
2007-07-30 22:56:45 +00:00
}
/**
* Static function returning all local course scales
* @ return object
*/
2008-05-23 14:52:50 +00:00
public static function fetch_all_local ( $courseid ) {
2007-11-22 05:42:46 +00:00
return grade_scale :: fetch_all ( array ( 'courseid' => $courseid ));
2007-07-30 22:56:45 +00:00
}
/**
* Checks if scale can be deleted .
2007-07-29 23:02:03 +00:00
* @ return boolean
*/
2008-05-23 14:52:50 +00:00
public function can_delete () {
2007-09-18 18:37:58 +00:00
return ! $this -> is_used ();
2007-07-29 23:02:03 +00:00
}
/**
2007-09-18 18:37:58 +00:00
* Returns if scale used anywhere - activities , grade items , outcomes , etc .
* @ return bool
2007-07-29 23:02:03 +00:00
*/
2008-05-23 14:52:50 +00:00
public function is_used () {
global $DB ;
2007-07-29 23:02:03 +00:00
global $CFG ;
2007-11-01 08:25:05 +00:00
// count grade items excluding the
2008-05-26 05:06:01 +00:00
$params = array ( $this -> id );
$sql = " SELECT COUNT(id) FROM { grade_items} WHERE scaleid = ? AND outcomeid IS NULL " ;
if ( $DB -> count_records_sql ( $sql , $params )) {
2007-09-18 18:37:58 +00:00
return true ;
2007-07-29 23:02:03 +00:00
}
// count outcomes
2008-05-26 05:06:01 +00:00
$sql = " SELECT COUNT(id) FROM { grade_outcomes} WHERE scaleid = ? " ;
if ( $DB -> count_records_sql ( $sql , $params )) {
2007-09-18 18:37:58 +00:00
return true ;
}
$legacy_mods = false ;
2008-05-23 14:52:50 +00:00
if ( $mods = $DB -> get_records ( 'modules' , array ( 'visible' => 1 ))) {
2007-09-18 18:37:58 +00:00
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 ;
2007-09-18 18:50:14 +00:00
debugging ( 'Please notify the developer of module "' . $mod -> name . '" that new function module_scale_used_anywhere() should be implemented.' , DEBUG_DEVELOPER );
2007-09-18 18:37:58 +00:00
break ;
}
}
}
}
// some mods are missing the new xxx_scale_used_anywhere() - use the really slow old way
if ( $legacy_mods ) {
if ( ! empty ( $this -> courseid )) {
2007-11-22 05:42:46 +00:00
if ( course_scale_used ( $this -> courseid , $this -> id )) {
2007-09-18 18:37:58 +00:00
return true ;
}
} else {
$courses = array ();
2007-11-22 05:42:46 +00:00
if ( site_scale_used ( $this -> id , $courses )) {
2007-09-18 18:37:58 +00:00
return true ;
}
}
2007-07-29 23:02:03 +00:00
}
2007-09-18 18:37:58 +00:00
return false ;
2007-07-29 23:02:03 +00:00
}
2009-11-04 06:14:06 +00:00
/**
* Returns the formatted grade description with URL ' s converted
* @ return string
*/
public function get_description () {
$systemcontext = get_context_instance ( CONTEXT_SYSTEM );
$options = new stdClass ;
$options -> noclean = true ;
2010-07-03 13:37:13 +00:00
$description = file_rewrite_pluginfile_urls ( $this -> description , 'pluginfile.php' , $systemcontext -> id , 'grade' , 'scale' , $this -> id );
2009-11-04 06:14:06 +00:00
return format_text ( $description , $this -> descriptionformat , $options );
}
2007-05-01 05:47:10 +00:00
}