2009-11-01 11:42:23 +00:00
|
|
|
<?php
|
2007-04-27 01:17:02 +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-04-27 01:17:02 +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 //
|
|
|
|
// //
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An abstract object that holds methods and attributes common to all grade_* objects defined here.
|
|
|
|
* @abstract
|
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
abstract class grade_object {
|
|
|
|
public $table;
|
|
|
|
|
2007-04-27 01:17:02 +00:00
|
|
|
/**
|
2007-09-22 11:39:59 +00:00
|
|
|
* Array of required table fields, must start with 'id'.
|
|
|
|
* @var array $required_fields
|
2007-04-27 01:17:02 +00:00
|
|
|
*/
|
2010-01-11 07:35:40 +00:00
|
|
|
public $required_fields = array('id', 'timecreated', 'timemodified', 'hidden');
|
2007-04-27 01:17:02 +00:00
|
|
|
|
|
|
|
/**
|
2007-09-22 11:39:59 +00:00
|
|
|
* Array of optional fields with default values - usually long text information that is not always needed.
|
|
|
|
* If you want to create an instance without optional fields use: new grade_object($only_required_fields, false);
|
|
|
|
* @var array $optional_fields
|
2007-04-27 01:17:02 +00:00
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
public $optional_fields = array();
|
2007-06-23 16:51:09 +00:00
|
|
|
|
2007-04-27 01:17:02 +00:00
|
|
|
/**
|
|
|
|
* The PK.
|
2007-06-23 16:51:09 +00:00
|
|
|
* @var int $id
|
2007-04-27 01:17:02 +00:00
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
public $id;
|
2007-06-23 16:51:09 +00:00
|
|
|
|
2007-04-27 01:17:02 +00:00
|
|
|
/**
|
2007-09-22 11:39:59 +00:00
|
|
|
* The first time this grade_object was created.
|
2007-04-27 01:17:02 +00:00
|
|
|
* @var int $timecreated
|
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
public $timecreated;
|
2007-06-23 16:51:09 +00:00
|
|
|
|
2007-04-27 01:17:02 +00:00
|
|
|
/**
|
2007-09-22 11:39:59 +00:00
|
|
|
* The last time this grade_object was modified.
|
2007-04-27 01:17:02 +00:00
|
|
|
* @var int $timemodified
|
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
public $timemodified;
|
2007-06-23 16:51:09 +00:00
|
|
|
|
2010-01-11 07:35:40 +00:00
|
|
|
/**
|
|
|
|
* 0 if visible, 1 always hidden or date not visible until
|
|
|
|
* @var int $hidden
|
|
|
|
*/
|
|
|
|
var $hidden = 0;
|
|
|
|
|
2007-04-27 01:17:02 +00:00
|
|
|
/**
|
2007-04-27 07:28:41 +00:00
|
|
|
* Constructor. Optionally (and by default) attempts to fetch corresponding row from DB.
|
2007-06-24 22:26:33 +00:00
|
|
|
* @param array $params an array with required parameters for this grade object.
|
2007-09-22 18:50:46 +00:00
|
|
|
* @param boolean $fetch Whether to fetch corresponding row from DB or not,
|
|
|
|
* optional fields might not be defined if false used
|
2007-06-23 16:51:09 +00:00
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
public function __construct($params=NULL, $fetch=true) {
|
2007-06-24 22:26:33 +00:00
|
|
|
if (!empty($params) and (is_array($params) or is_object($params))) {
|
2007-09-22 18:46:51 +00:00
|
|
|
if ($fetch) {
|
|
|
|
if ($data = $this->fetch($params)) {
|
2007-11-22 05:42:46 +00:00
|
|
|
grade_object::set_properties($this, $data);
|
2007-09-22 18:46:51 +00:00
|
|
|
} else {
|
2007-11-22 05:42:46 +00:00
|
|
|
grade_object::set_properties($this, $this->optional_fields);//apply defaults for optional fields
|
|
|
|
grade_object::set_properties($this, $params);
|
2007-09-22 18:46:51 +00:00
|
|
|
}
|
2007-06-24 22:26:33 +00:00
|
|
|
|
|
|
|
} else {
|
2007-11-22 05:42:46 +00:00
|
|
|
grade_object::set_properties($this, $params);
|
2007-06-24 22:26:33 +00:00
|
|
|
}
|
2007-09-22 18:46:51 +00:00
|
|
|
|
|
|
|
} else {
|
2007-11-22 05:42:46 +00:00
|
|
|
grade_object::set_properties($this, $this->optional_fields);//apply defaults for optional fields
|
2007-06-24 22:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-22 11:39:59 +00:00
|
|
|
/**
|
|
|
|
* Makes sure all the optional fields are loaded.
|
|
|
|
* If id present (==instance exists in db) fetches data from db.
|
|
|
|
* Defaults are used for new instances.
|
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
public function load_optional_fields() {
|
|
|
|
global $DB;
|
2007-09-22 11:39:59 +00:00
|
|
|
foreach ($this->optional_fields as $field=>$default) {
|
|
|
|
if (array_key_exists($field, $this)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (empty($this->id)) {
|
|
|
|
$this->$field = $default;
|
|
|
|
} else {
|
2008-05-23 14:52:50 +00:00
|
|
|
$this->$field = $DB->get_field($this->table, $field, array('id', $this->id));
|
2007-09-22 11:39:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-24 22:26:33 +00:00
|
|
|
/**
|
|
|
|
* Finds and returns a grade_object instance based on params.
|
|
|
|
* @static abstract
|
|
|
|
*
|
|
|
|
* @param array $params associative arrays varname=>value
|
|
|
|
* @return object grade_object instance or false if none found.
|
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
public static abstract function fetch($params);
|
2007-06-24 22:26:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds and returns all grade_object instances based on params.
|
|
|
|
* @static abstract
|
|
|
|
*
|
|
|
|
* @param array $params associative arrays varname=>value
|
2010-05-21 18:57:05 +00:00
|
|
|
* @return array array of grade_object instances or false if none found.
|
2007-06-24 22:26:33 +00:00
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
public static abstract function fetch_all($params);
|
2007-06-24 22:26:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory method - uses the parameters to retrieve matching instance from the DB.
|
|
|
|
* @static final protected
|
2007-11-07 17:59:33 +00:00
|
|
|
* @return mixed object instance or false if not found
|
2007-06-24 22:26:33 +00:00
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
protected static function fetch_helper($table, $classname, $params) {
|
2007-11-22 05:42:46 +00:00
|
|
|
if ($instances = grade_object::fetch_all_helper($table, $classname, $params)) {
|
2007-06-24 22:26:33 +00:00
|
|
|
if (count($instances) > 1) {
|
2007-07-29 13:22:50 +00:00
|
|
|
// we should not tolerate any errors here - problems might appear later
|
2008-06-02 09:41:45 +00:00
|
|
|
print_error('morethanonerecordinfetch','debug');
|
2007-04-27 07:28:41 +00:00
|
|
|
}
|
2007-07-29 13:22:50 +00:00
|
|
|
return reset($instances);
|
2007-06-24 22:26:33 +00:00
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Factory method - uses the parameters to retrieve all matching instances from the DB.
|
|
|
|
* @static final protected
|
|
|
|
* @return mixed array of object instances or false if not found
|
|
|
|
*/
|
2008-06-02 16:06:33 +00:00
|
|
|
public static function fetch_all_helper($table, $classname, $params) {
|
2007-06-24 22:26:33 +00:00
|
|
|
$instance = new $classname();
|
|
|
|
|
|
|
|
$classvars = (array)$instance;
|
|
|
|
$params = (array)$params;
|
|
|
|
|
|
|
|
$wheresql = array();
|
2010-03-10 08:42:44 +00:00
|
|
|
$newparams = array();
|
2007-06-24 22:26:33 +00:00
|
|
|
|
|
|
|
foreach ($params as $var=>$value) {
|
2007-09-22 11:39:59 +00:00
|
|
|
if (!in_array($var, $instance->required_fields) and !array_key_exists($var, $instance->optional_fields)) {
|
2007-06-24 22:26:33 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (is_null($value)) {
|
|
|
|
$wheresql[] = " $var IS NULL ";
|
|
|
|
} else {
|
2008-05-26 05:06:01 +00:00
|
|
|
$wheresql[] = " $var = ? ";
|
2010-03-10 08:42:44 +00:00
|
|
|
$newparams[] = $value;
|
2007-06-24 22:26:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($wheresql)) {
|
|
|
|
$wheresql = '';
|
|
|
|
} else {
|
|
|
|
$wheresql = implode("AND", $wheresql);
|
|
|
|
}
|
|
|
|
|
2008-05-23 14:52:50 +00:00
|
|
|
global $DB;
|
2010-03-10 08:42:44 +00:00
|
|
|
if ($datas = $DB->get_records_select($table, $wheresql, $newparams)) {
|
2009-11-01 11:42:23 +00:00
|
|
|
|
2007-06-24 22:26:33 +00:00
|
|
|
$result = array();
|
|
|
|
foreach($datas as $data) {
|
|
|
|
$instance = new $classname();
|
2007-11-22 05:42:46 +00:00
|
|
|
grade_object::set_properties($instance, $data);
|
2007-06-24 22:26:33 +00:00
|
|
|
$result[$instance->id] = $instance;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
|
|
|
|
} else {
|
2009-11-01 11:42:23 +00:00
|
|
|
|
2007-06-24 22:26:33 +00:00
|
|
|
return false;
|
2007-06-23 16:51:09 +00:00
|
|
|
}
|
2007-04-27 01:17:02 +00:00
|
|
|
}
|
2007-06-23 16:51:09 +00:00
|
|
|
|
2007-04-27 01:17:02 +00:00
|
|
|
/**
|
|
|
|
* Updates this object in the Database, based on its object variables. ID must be set.
|
2007-07-06 12:49:28 +00:00
|
|
|
* @param string $source from where was the object updated (mod/forum, manual, etc.)
|
|
|
|
* @return boolean success
|
2007-04-27 01:17:02 +00:00
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
public function update($source=null) {
|
|
|
|
global $USER, $CFG, $DB;
|
2007-11-08 08:59:26 +00:00
|
|
|
|
|
|
|
if (empty($this->id)) {
|
|
|
|
debugging('Can not update grade object, no id!');
|
|
|
|
return false;
|
|
|
|
}
|
2007-05-10 09:07:37 +00:00
|
|
|
|
2007-10-20 15:00:31 +00:00
|
|
|
$data = $this->get_record_data();
|
2007-06-28 13:20:30 +00:00
|
|
|
|
2009-06-03 20:37:28 +00:00
|
|
|
$DB->update_record($this->table, $data);
|
2007-07-06 12:49:28 +00:00
|
|
|
|
2007-11-08 08:59:26 +00:00
|
|
|
if (empty($CFG->disablegradehistory)) {
|
|
|
|
unset($data->timecreated);
|
|
|
|
$data->action = GRADE_HISTORY_UPDATE;
|
|
|
|
$data->oldid = $this->id;
|
|
|
|
$data->source = $source;
|
|
|
|
$data->timemodified = time();
|
|
|
|
$data->userlogged = $USER->id;
|
2008-05-26 05:06:01 +00:00
|
|
|
$DB->insert_record($this->table.'_history', $data);
|
2007-11-08 08:59:26 +00:00
|
|
|
}
|
2007-07-06 12:49:28 +00:00
|
|
|
|
2008-07-28 12:31:29 +00:00
|
|
|
$this->notify_changed(false);
|
2007-07-06 12:49:28 +00:00
|
|
|
return true;
|
2007-04-27 01:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes this object from the database.
|
2007-07-06 12:49:28 +00:00
|
|
|
* @param string $source from where was the object deleted (mod/forum, manual, etc.)
|
|
|
|
* @return boolean success
|
2007-04-27 01:17:02 +00:00
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
public function delete($source=null) {
|
|
|
|
global $USER, $CFG, $DB;
|
2007-07-06 12:49:28 +00:00
|
|
|
|
2007-11-08 08:59:26 +00:00
|
|
|
if (empty($this->id)) {
|
|
|
|
debugging('Can not delete grade object, no id!');
|
|
|
|
return false;
|
2007-07-06 12:49:28 +00:00
|
|
|
}
|
|
|
|
|
2007-11-08 08:59:26 +00:00
|
|
|
$data = $this->get_record_data();
|
|
|
|
|
2008-06-05 18:01:22 +00:00
|
|
|
if ($DB->delete_records($this->table, array('id'=>$this->id))) {
|
2007-11-08 08:59:26 +00:00
|
|
|
if (empty($CFG->disablegradehistory)) {
|
|
|
|
unset($data->id);
|
|
|
|
unset($data->timecreated);
|
|
|
|
$data->action = GRADE_HISTORY_DELETE;
|
|
|
|
$data->oldid = $this->id;
|
|
|
|
$data->source = $source;
|
|
|
|
$data->timemodified = time();
|
|
|
|
$data->userlogged = $USER->id;
|
2008-05-26 05:06:01 +00:00
|
|
|
$DB->insert_record($this->table.'_history', $data);
|
2007-07-06 12:49:28 +00:00
|
|
|
}
|
2008-07-28 12:31:29 +00:00
|
|
|
$this->notify_changed(true);
|
2007-07-06 12:49:28 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2007-04-27 01:17:02 +00:00
|
|
|
}
|
2007-06-23 16:51:09 +00:00
|
|
|
|
2007-10-20 15:00:31 +00:00
|
|
|
/**
|
|
|
|
* Returns object with fields and values that are defined in database
|
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
public function get_record_data() {
|
2007-10-20 15:00:31 +00:00
|
|
|
$data = new object();
|
2008-06-09 16:53:30 +00:00
|
|
|
|
2007-10-20 15:00:31 +00:00
|
|
|
foreach ($this as $var=>$value) {
|
|
|
|
if (in_array($var, $this->required_fields) or array_key_exists($var, $this->optional_fields)) {
|
|
|
|
if (is_object($value) or is_array($value)) {
|
|
|
|
debugging("Incorrect property '$var' found when inserting grade object");
|
|
|
|
} else {
|
|
|
|
$data->$var = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2007-04-27 01:17:02 +00:00
|
|
|
/**
|
|
|
|
* Records this object in the Database, sets its id to the returned value, and returns that value.
|
2007-06-12 20:16:49 +00:00
|
|
|
* If successful this function also fetches the new object data from database and stores it
|
|
|
|
* in object properties.
|
2007-07-06 12:49:28 +00:00
|
|
|
* @param string $source from where was the object inserted (mod/forum, manual, etc.)
|
2007-04-27 01:17:02 +00:00
|
|
|
* @return int PK ID if successful, false otherwise
|
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
public function insert($source=null) {
|
|
|
|
global $USER, $CFG, $DB;
|
2007-05-10 09:07:37 +00:00
|
|
|
|
2007-05-29 00:56:44 +00:00
|
|
|
if (!empty($this->id)) {
|
2007-05-28 02:05:21 +00:00
|
|
|
debugging("Grade object already exists!");
|
|
|
|
return false;
|
2007-05-11 02:11:30 +00:00
|
|
|
}
|
|
|
|
|
2007-10-20 15:00:31 +00:00
|
|
|
$data = $this->get_record_data();
|
2007-06-28 13:20:30 +00:00
|
|
|
|
2009-06-03 20:37:28 +00:00
|
|
|
$this->id = $DB->insert_record($this->table, $data);
|
2007-06-12 20:16:49 +00:00
|
|
|
|
|
|
|
// set all object properties from real db data
|
|
|
|
$this->update_from_db();
|
|
|
|
|
2007-11-08 08:59:26 +00:00
|
|
|
$data = $this->get_record_data();
|
|
|
|
|
|
|
|
if (empty($CFG->disablegradehistory)) {
|
|
|
|
unset($data->timecreated);
|
|
|
|
$data->action = GRADE_HISTORY_INSERT;
|
|
|
|
$data->oldid = $this->id;
|
|
|
|
$data->source = $source;
|
|
|
|
$data->timemodified = time();
|
|
|
|
$data->userlogged = $USER->id;
|
2008-05-26 05:06:01 +00:00
|
|
|
$DB->insert_record($this->table.'_history', $data);
|
2007-11-08 08:59:26 +00:00
|
|
|
}
|
2007-07-06 12:49:28 +00:00
|
|
|
|
2008-07-28 12:31:29 +00:00
|
|
|
$this->notify_changed(false);
|
2007-06-12 20:16:49 +00:00
|
|
|
return $this->id;
|
2007-04-27 01:17:02 +00:00
|
|
|
}
|
2007-05-08 08:01:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Using this object's id field, fetches the matching record in the DB, and looks at
|
|
|
|
* each variable in turn. If the DB has different data, the db's data is used to update
|
|
|
|
* the object. This is different from the update() function, which acts on the DB record
|
|
|
|
* based on the object.
|
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
public function update_from_db() {
|
2007-05-08 08:01:55 +00:00
|
|
|
if (empty($this->id)) {
|
2007-05-21 05:54:12 +00:00
|
|
|
debugging("The object could not be used in its state to retrieve a matching record from the DB, because its id field is not set.");
|
2007-05-08 08:01:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-05-23 14:52:50 +00:00
|
|
|
global $DB;
|
|
|
|
if (!$params = $DB->get_record($this->table, array('id' => $this->id))) {
|
2007-06-28 13:20:30 +00:00
|
|
|
debugging("Object with this id:{$this->id} does not exist in table:{$this->table}, can not update from db!");
|
2007-06-12 20:16:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-11-22 05:42:46 +00:00
|
|
|
grade_object::set_properties($this, $params);
|
2007-06-12 20:16:49 +00:00
|
|
|
|
2007-05-08 08:01:55 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-06-23 16:51:09 +00:00
|
|
|
|
2007-04-27 07:28:41 +00:00
|
|
|
/**
|
|
|
|
* Given an associated array or object, cycles through each key/variable
|
|
|
|
* and assigns the value to the corresponding variable in this object.
|
2007-06-24 22:26:33 +00:00
|
|
|
* @static final
|
2007-04-27 07:28:41 +00:00
|
|
|
*/
|
2008-05-23 14:52:50 +00:00
|
|
|
public static function set_properties(&$instance, $params) {
|
2007-10-11 09:15:57 +00:00
|
|
|
$params = (array) $params;
|
2007-06-24 22:26:33 +00:00
|
|
|
foreach ($params as $var => $value) {
|
2007-09-22 11:39:59 +00:00
|
|
|
if (in_array($var, $instance->required_fields) or array_key_exists($var, $instance->optional_fields)) {
|
2007-06-24 22:26:33 +00:00
|
|
|
$instance->$var = $value;
|
2007-04-27 07:28:41 +00:00
|
|
|
}
|
2007-06-23 16:51:09 +00:00
|
|
|
}
|
2007-04-27 07:28:41 +00:00
|
|
|
}
|
2008-07-28 12:31:29 +00:00
|
|
|
|
|
|
|
/**
|
2009-11-01 11:42:23 +00:00
|
|
|
* Called immediately after the object data has been inserted, updated, or
|
|
|
|
* deleted in the database. Default does nothing, can be overridden to
|
2008-07-28 12:31:29 +00:00
|
|
|
* hook in special behaviour.
|
|
|
|
*
|
|
|
|
* @param bool $deleted
|
|
|
|
*/
|
|
|
|
function notify_changed($deleted) {
|
|
|
|
}
|
2010-01-11 07:35:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the hidden state of this grade_item
|
|
|
|
* @return boolean hidden state
|
|
|
|
*/
|
|
|
|
function is_hidden() {
|
|
|
|
return ($this->hidden == 1 or ($this->hidden != 0 and $this->hidden > time()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check grade hidden status. Uses data from both grade item and grade.
|
|
|
|
* @return boolean true if hiddenuntil, false if not
|
|
|
|
*/
|
|
|
|
function is_hiddenuntil() {
|
|
|
|
return $this->hidden > 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check grade item hidden status.
|
|
|
|
* @return int 0 means visible, 1 hidden always, timestamp hidden until
|
|
|
|
*/
|
|
|
|
function get_hidden() {
|
|
|
|
return $this->hidden;
|
|
|
|
}
|
|
|
|
|
|
|
|
function set_hidden($hidden, $cascade=false) {
|
|
|
|
$this->hidden = $hidden;
|
|
|
|
$this->update();
|
|
|
|
}
|
2007-04-27 01:17:02 +00:00
|
|
|
}
|