MDL-32203 completion: Fix issues with data object where clauses

This commit is contained in:
Aaron Barnes
2012-05-04 15:05:48 +12:00
committed by Aaron Barnes
parent eddceb69c7
commit 4ef74c4834
13 changed files with 296 additions and 298 deletions
+19 -17
View File
@@ -103,44 +103,43 @@ if ($form->is_cancelled()){
// Handle aggregation methods
// Overall aggregation
$aggregation = new completion_aggregation();
$aggregation->course = $data->id;
$aggregation->criteriatype = null;
$aggdata = array(
'course' => $data->id,
'criteriatype' => null
);
$aggregation = new completion_aggregation($aggdata);
$aggregation->setMethod($data->overall_aggregation);
$aggregation->insert();
$aggregation->save();
// Activity aggregation
if (empty($data->activity_aggregation)) {
$data->activity_aggregation = 0;
}
$aggregation = new completion_aggregation();
$aggregation->course = $data->id;
$aggregation->criteriatype = COMPLETION_CRITERIA_TYPE_ACTIVITY;
$aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ACTIVITY;
$aggregation = new completion_aggregation($aggdata);
$aggregation->setMethod($data->activity_aggregation);
$aggregation->insert();
$aggregation->save();
// Course aggregation
if (empty($data->course_aggregation)) {
$data->course_aggregation = 0;
}
$aggregation = new completion_aggregation();
$aggregation->course = $data->id;
$aggregation->criteriatype = COMPLETION_CRITERIA_TYPE_COURSE;
$aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_COURSE;
$aggregation = new completion_aggregation($aggdata);
$aggregation->setMethod($data->course_aggregation);
$aggregation->insert();
$aggregation->save();
// Role aggregation
if (empty($data->role_aggregation)) {
$data->role_aggregation = 0;
}
$aggregation = new completion_aggregation();
$aggregation->course = $data->id;
$aggregation->criteriatype = COMPLETION_CRITERIA_TYPE_ROLE;
$aggdata['criteriatype'] = COMPLETION_CRITERIA_TYPE_ROLE;
$aggregation = new completion_aggregation($aggdata);
$aggregation->setMethod($data->role_aggregation);
$aggregation->insert();
$aggregation->save();
// Update course total passing grade
if (!empty($data->criteria_grade)) {
@@ -152,7 +151,10 @@ if ($form->is_cancelled()){
}
}
redirect($CFG->wwwroot."/course/view.php?id=$course->id", get_string('changessaved'));
add_to_log($course->id, 'course', 'completion updated', 'completion.php?id='.$course->id);
$url = new moodle_url('/course/view.php', array('id' => $course->id));
redirect($url);
}
+40 -35
View File
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -19,65 +18,58 @@
/**
* Course completion critieria aggregation
*
* @package moodlecore
* @package core_completion
* @category completion
* @copyright 2009 Catalyst IT Ltd
* @author Aaron Barnes <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Aaron Barnes <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/completion/data_object.php');
/**
* Course completion critieria aggregation
*
* @package core_completion
* @category completion
* @copyright 2009 Catalyst IT Ltd
* @author Aaron Barnes <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class completion_aggregation extends data_object {
/**
* DB Table
* @var string $table
*/
/* @var string Database table name that stores completion aggregation information */
public $table = 'course_completion_aggr_methd';
/**
* Array of required table fields, must start with 'id'.
* @var array $required_fields
* Defaults to id, course, criteriatype, method, value
* @var array
*/
public $required_fields = array('id', 'course', 'criteriatype', 'method', 'value');
/**
* Course id
* @access public
* @var int
*/
/* @var array Array of unique fields, used in where clauses */
public $unique_fields = array('course', 'criteriatype');
/* @var int Course id */
public $course;
/**
* Criteria type this aggregation method applies to, or NULL for overall course aggregation
* @access public
* @var int
*/
/* @var int Criteria type this aggregation method applies to, or NULL for overall course aggregation */
public $criteriatype;
/**
* Aggregation method (COMPLETION_AGGREGATION_* constant)
* @access public
* @var int
*/
/* @var int Aggregation method (COMPLETION_AGGREGATION_* constant) */
public $method;
/**
* Method value
* @access public
* @var mixed
*/
/* @var mixed Method value */
public $value;
/**
* Finds and returns a data_object instance based on params.
* @static abstract
*
* @param array $params associative arrays varname=>value
* @return object data_object instance or false if none found.
* @return data_object instance of data_object or false if none found.
*/
public static function fetch($params) {
return self::fetch_helper('course_completion_aggr_methd', __CLASS__, $params);
@@ -86,7 +78,6 @@ class completion_aggregation extends data_object {
/**
* Finds and returns all data_object instances based on params.
* @static abstract
*
* @param array $params associative arrays varname=>value
* @return array array of data_object insatnces or false if none found.
@@ -95,9 +86,8 @@ class completion_aggregation extends data_object {
/**
* Set the aggregation method
* @access public
* @param $method int
* @return void
*
* @param int $method One of COMPLETION_AGGREGATION_ALL or COMPLETION_AGGREGATION_ANY
*/
public function setMethod($method) {
$methods = array(
@@ -111,4 +101,19 @@ class completion_aggregation extends data_object {
$this->method = COMPLETION_AGGREGATION_ALL;
}
}
/**
* Save aggregation method to database
*
* @access public
* @return boolean
*/
public function save() {
if ($this->id) {
return $this->update();
} else {
return $this->insert();
}
}
}
+48 -91
View File
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -15,103 +14,70 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Course completion status for a particular user/course
*
* @package core_completion
* @category completion
* @copyright 2009 Catalyst IT Ltd
* @author Aaron Barnes <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/completion/data_object.php');
/**
* Course completion status for a particular user/course
*
* @package moodlecore
* @package core_completion
* @category completion
* @copyright 2009 Catalyst IT Ltd
* @author Aaron Barnes <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->libdir.'/completion/data_object.php');
/**
* Course completion status for a particular user/course
* @author Aaron Barnes <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class completion_completion extends data_object {
/**
* DB Table
* @var string $table
*/
/* @var string $table Database table name that stores completion information */
public $table = 'course_completions';
/**
* Array of required table fields, must start with 'id'.
* @var array $required_fields
*/
/* @var array $required_fields Array of required table fields, must start with 'id'. */
public $required_fields = array('id', 'userid', 'course', 'deleted', 'timenotified',
'timeenrolled', 'timestarted', 'timecompleted', 'reaggregate');
/**
* User ID
* @access public
* @var int
*/
/* @var int $userid User ID */
public $userid;
/**
* Course ID
* @access public
* @var int
*/
/* @var int $course Course ID */
public $course;
/**
* Set to 1 if this record has been deleted
* @access public
* @var int
*/
/* @var int $deleted set to 1 if this record has been deleted */
public $deleted;
/**
* Timestamp the interested parties were notified
* of this user's completion
* @access public
* @var int
*/
/* @var int Timestamp the interested parties were notified of this user's completion. */
public $timenotified;
/**
* Time of course enrolment
* @see completion_completion::mark_enrolled()
* @access public
* @var int
*/
/* @var int Time of course enrolment {@link completion_completion::mark_enrolled()} */
public $timeenrolled;
/**
* Time the user started their course completion
* @see completion_completion::mark_inprogress()
* @access public
* @var int
* Time the user started their course completion {@link completion_completion::mark_inprogress()}
* @var int
*/
public $timestarted;
/**
* Timestamp of course completion
* @see completion_completion::mark_complete()
* @access public
* @var int
*/
/* @var int Timestamp of course completion {@link completion_completion::mark_complete()} */
public $timecompleted;
/**
* Flag to trigger cron aggregation (timestamp)
* @access public
* @var int
*/
/* @var int Flag to trigger cron aggregation (timestamp) */
public $reaggregate;
/**
* Finds and returns a data_object instance based on params.
* @static abstract
*
* @param array $params associative arrays varname=>value
* @return object data_object instance or false if none found.
* @param array $params associative arrays varname = >value
* @return data_object instance of data_object or false if none found.
*/
public static function fetch($params) {
$params['deleted'] = null;
@@ -120,8 +86,8 @@ class completion_completion extends data_object {
/**
* Return status of this completion
* @access public
* @return boolean
*
* @return bool
*/
public function is_complete() {
return (bool) $this->timecompleted;
@@ -132,9 +98,7 @@ class completion_completion extends data_object {
*
* If the user is already marked as started, no change will occur
*
* @access public
* @param integer $timeenrolled Time enrolled (optional)
* @return void
* @param integer $timeenrolled Time enrolled (optional)
*/
public function mark_enrolled($timeenrolled = null) {
@@ -147,18 +111,15 @@ class completion_completion extends data_object {
$this->timeenrolled = $timeenrolled;
}
$this->_save();
return $this->_save();
}
/**
* Mark this user as inprogress in this course
*
* If the user is already marked as inprogress,
* the time will not be changed
* If the user is already marked as inprogress, the time will not be changed
*
* @access public
* @param integer $timestarted Time started (optional)
* @return void
* @param integer $timestarted Time started (optional)
*/
public function mark_inprogress($timestarted = null) {
@@ -176,7 +137,7 @@ class completion_completion extends data_object {
$this->timestarted = $timestarted;
}
$this->_save();
return $this->_save();
}
/**
@@ -185,9 +146,8 @@ class completion_completion extends data_object {
* This generally happens when the required completion criteria
* in the course are complete.
*
* @access public
* @param integer $timecomplete Time completed (optional)
* @return void
* @param integer $timecomplete Time completed (optional)
* @return void
*/
public function mark_complete($timecomplete = null) {
@@ -205,39 +165,36 @@ class completion_completion extends data_object {
$this->timecompleted = $timecomplete;
// Save record
$this->_save();
return $this->_save();
}
/**
* Save course completion status
*
* This method creates a course_completions record if none exists
* @access public
* @return void
* @access private
* @return bool
*/
private function _save() {
global $DB;
if ($this->timeenrolled === null) {
$this->timeenrolled = 0;
}
// Save record
if ($this->id) {
$this->update();
return $this->update();
} else {
// Make sure reaggregate field is not null
if (!$this->reaggregate) {
$this->reaggregate = 0;
}
// Make sure timestarted is not null
if (!$this->timestarted) {
$this->timestarted = 0;
}
$this->insert();
// Make sure timestarted is not null
if (!$this->timestarted) {
$this->timestarted = 0;
}
return $this->insert();
}
}
}
+2 -2
View File
@@ -141,11 +141,11 @@ abstract class completion_criteria extends data_object {
public static function factory($params) {
global $CFG, $COMPLETION_CRITERIA_TYPES;
if (!isset($params->criteriatype) || !isset($COMPLETION_CRITERIA_TYPES[$params->criteriatype])) {
if (!isset($params['criteriatype']) || !isset($COMPLETION_CRITERIA_TYPES[$params['criteriatype']])) {
error('invalidcriteriatype', 'completion');
}
$class = 'completion_criteria_'.$COMPLETION_CRITERIA_TYPES[$params->criteriatype];
$class = 'completion_criteria_'.$COMPLETION_CRITERIA_TYPES[$params['criteriatype']];
require_once($CFG->libdir.'/completion/'.$class.'.php');
return new $class($params, false);
@@ -229,8 +229,7 @@ class completion_criteria_activity extends completion_criteria {
// Loop through completions, and mark as complete
$rs = $DB->get_recordset_sql($sql);
foreach ($rs as $record) {
$completion = new completion_criteria_completion((array)$record);
$completion = new completion_criteria_completion($record, DATA_OBJECT_FETCH_BY_KEY);
$completion->mark_complete($record->timecompleted);
}
$rs->close();
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -18,101 +17,68 @@
/**
* Completion data for a specific user, course and critieria
*
* @package moodlecore
* @package core_completion
* @category completion
* @copyright 2009 Catalyst IT Ltd
* @author Aaron Barnes <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @author Aaron Barnes <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once($CFG->libdir.'/completion/data_object.php');
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/completion/data_object.php');
/**
* Completion data for a specific user, course and critieria
*
* @package core_completion
* @category completion
* @copyright 2009 Catalyst IT Ltd
* @author Aaron Barnes <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class completion_criteria_completion extends data_object {
/**
* DB Table
* @var string $table
*/
/* @var string Database table that stores completion type criteria */
public $table = 'course_completion_crit_compl';
/**
* Array of required table fields, must start with 'id'.
* @var array $required_fields
*/
/* @var array Array of required table fields, must start with 'id'. */
public $required_fields = array('id', 'userid', 'course', 'criteriaid', 'gradefinal', 'rpl', 'deleted', 'unenroled', 'timecompleted');
/**
* User ID
* @access public
* @var int
*/
/* @var array Array of unique fields, used in where clauses */
public $unique_fields = array('userid', 'course', 'criteriaid');
/* @var int User ID */
public $userid;
/**
* Course ID
* @access public
* @var int
*/
/* @var int course ID */
public $course;
/**
* The id of the course completion criteria this completion references
* @access public
* @var int
*/
/* @var int The id of the course completion criteria this completion references */
public $criteriaid;
/**
* The final grade for the user in the course (if completing a grade criteria)
* @access public
* @var float
*/
/* @var float The final grade for the user in the course (if completing a grade criteria) */
public $gradefinal;
/**
* Record of prior learning, leave blank if none
* @access public
* @var string
*/
/* @var string Record of prior learning, leave blank if none */
public $rpl;
/**
* Course deleted flag
* @access public
* @var boolean
*/
/* @var bool Course deleted flag */
public $deleted;
/**
* Timestamp of user unenrolment (if completing a unenrol criteria)
* @access public
* @var int (timestamp)
*/
/* @var int Timestamp of user unenrolment (if completing a unenrol criteria) */
public $unenroled;
/**
* Timestamp of course criteria completion
* @see completion_criteria_completion::mark_complete()
* @access public
* @var int (timestamp)
*/
/* @var int Timestamp of course criteria completion {@link completion_criteria_completion::mark_complete()} */
public $timecompleted;
/**
* Associated criteria object
* @access private
* @var object completion_criteria
*/
/* @var completion_criterria Associated criteria object */
private $_criteria;
/**
* Finds and returns a data_object instance based on params.
* @static abstract
*
* @param array $params associative arrays varname=>value
* @return object data_object instance or false if none found.
* @return data_object instance of data_object or false if none found.
*/
public static function fetch($params) {
$params['deleted'] = null;
@@ -121,7 +87,6 @@ class completion_criteria_completion extends data_object {
/**
* Finds and returns all data_object instances based on params.
* @static abstract
*
* @param array $params associative arrays varname=>value
* @return array array of data_object insatnces or false if none found.
@@ -130,8 +95,8 @@ class completion_criteria_completion extends data_object {
/**
* Return status of this criteria completion
* @access public
* @return boolean
*
* @return bool
*/
public function is_complete() {
return (bool) $this->timecompleted;
@@ -141,8 +106,6 @@ class completion_criteria_completion extends data_object {
* Mark this criteria complete for the associated user
*
* This method creates a course_completion_crit_compl record
* @access public
* @return void
*/
public function mark_complete() {
// Create record
@@ -166,9 +129,8 @@ class completion_criteria_completion extends data_object {
/**
* Attach a preloaded criteria object to this object
* @access public
*
* @param $criteria object completion_criteria
* @return void
*/
public function attach_criteria(completion_criteria $criteria) {
$this->_criteria = $criteria;
@@ -177,13 +139,12 @@ class completion_criteria_completion extends data_object {
/**
* Return the associated criteria with this completion
* If nothing attached, load from the db
* @access public
* @return object completion_criteria
*
* @return completion_criteria
*/
public function get_criteria() {
if (!$this->_criteria)
{
if (!$this->_criteria) {
global $DB;
$params = array(
@@ -199,10 +160,9 @@ class completion_criteria_completion extends data_object {
}
/**
* Return criteria status text for display in reports
* @see completion_criteria::get_status()
* @access public
* @return string
* Return criteria status text for display in reports {@link completion_criteria::get_status()}
*
* @return string
*/
public function get_status() {
return $this->_criteria->get_status($this);
@@ -189,8 +189,8 @@ class completion_criteria_course extends completion_criteria {
// Loop through completions, and mark as complete
$rs = $DB->get_recordset_sql($sql);
foreach ($rs as $record) {
$completion = new completion_criteria_completion((array)$record);
$completion->mark_complete($record->timecompleted);
$completion = new completion_criteria_completion($record, DATA_OBJECT_FETCH_BY_KEY);
$completion->mark_complete($record['timecompleted']);
}
$rs->close();
}
+2 -2
View File
@@ -179,8 +179,8 @@ class completion_criteria_date extends completion_criteria {
// Loop through completions, and mark as complete
$rs = $DB->get_recordset_sql($sql, array(time()));
foreach ($rs as $record) {
$completion = new completion_criteria_completion((array)$record);
$completion->mark_complete($record->timeend);
$completion = new completion_criteria_completion($record, DATA_OBJECT_FETCH_BY_KEY);
$completion->mark_complete($record['timeend']);
}
$rs->close();
}
@@ -225,8 +225,7 @@ class completion_criteria_duration extends completion_criteria {
$now = time();
$rs = $DB->get_recordset_sql($sql, array($now, $now));
foreach ($rs as $record) {
$completion = new completion_criteria_completion((array)$record);
$completion = new completion_criteria_completion($record, DATA_OBJECT_FETCH_BY_KEY);
// Use time start if not 0, otherwise use timeenrolled
if ($record->otimestart) {
+2 -2
View File
@@ -215,8 +215,8 @@ class completion_criteria_grade extends completion_criteria {
// Loop through completions, and mark as complete
$rs = $DB->get_recordset_sql($sql);
foreach ($rs as $record) {
$completion = new completion_criteria_completion((array)$record);
$completion->mark_complete($record->timecompleted);
$completion = new completion_criteria_completion($record, DATA_OBJECT_FETCH_BY_KEY);
$completion->mark_complete($record['timecompleted']);
}
$rs->close();
}
+1 -1
View File
@@ -322,7 +322,7 @@ function completion_cron_completions() {
foreach ($completions as $params) {
$timecompleted = max($timecompleted, $params->timecompleted);
$completion = new completion_criteria_completion($params, false);
$completion = new completion_criteria_completion((array)$params, false);
// Handle aggregation special cases
if ($params->criteriatype == COMPLETION_CRITERIA_TYPE_ACTIVITY) {
+139 -64
View File
@@ -1,85 +1,137 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle 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 3 of the License, or
// (at your option) any later version.
//
// Moodle 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.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.com //
// //
// Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com //
// //
// 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 //
// //
///////////////////////////////////////////////////////////////////////////
/**
* Course completion critieria aggregation
*
* @package core_completion
* @category completion
* @copyright 2009 Catalyst IT Ltd
* @author Aaron Barnes <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Trigger for the new data_object api.
*
* See data_object::__constructor
*/
define('DATA_OBJECT_FETCH_BY_KEY', 2);
/**
* A data abstraction object that holds methods and attributes
* @abstract
*
* @package core_completion
* @category completion
* @copyright 2009 Catalyst IT Ltd
* @author Aaron Barnes <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
abstract class data_object {
/**
* Table that the class maps to in the database
* @var string $table
*/
/* @var string Table that the class maps to in the database */
public $table;
/**
* Array of required table fields, must start with 'id'.
* @var array $required_fields
*/
/* @var array Array of required table fields, must start with 'id'. */
public $required_fields = array('id');
/**
* 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 data_object($only_required_fields, false);
* @var array $optional_fields
* @var array
*/
public $optional_fields = array();
/**
* The PK.
* @var int $id
*/
/* @var Array of unique fields, used in where clauses and constructor */
public $unique_fields = array();
/* @var int The primary key */
public $id;
/**
* Constructor. Optionally (and by default) attempts to fetch corresponding row from DB.
* @param array $params an array with required parameters for this data object.
* @param boolean $fetch Whether to fetch corresponding row from DB or not,
* optional fields might not be defined if false used
*
* If $fetch is not false, there are a few different things that can happen:
* - true:
* load corresponding row from the database, using $params as the WHERE clause
*
* - DATA_OBJECT_FETCH_BY_KEY:
* load corresponding row from the database, using only the $id in the WHERE clause (if set),
* otherwise using the columns listed in $this->unique_fields.
*
* - array():
* load corresponding row from the database, using the columns listed in this array
* in the WHERE clause
*
* @param array $params required parameters and their values for this data object
* @param mixed $fetch if false, do not attempt to fetch from the database, otherwise see notes
*/
public function __construct($params=NULL, $fetch=true) {
if (!empty($params) and (is_array($params) or is_object($params))) {
if ($fetch) {
if ($data = $this->fetch($params)) {
self::set_properties($this, $data);
} else {
self::set_properties($this, $this->optional_fields);//apply defaults for optional fields
self::set_properties($this, $params);
}
public function __construct($params = null, $fetch = true) {
} else {
self::set_properties($this, $params);
if (is_object($params)) {
throw new coding_exception('data_object params should be in the form of an array, not an object');
}
// If no params given, apply defaults for optional fields
if (empty($params) || !is_array($params)) {
self::set_properties($this, $this->optional_fields);
return;
}
// If fetch is false, do not load from database
if ($fetch === false) {
self::set_properties($this, $params);
return;
}
// Compose where clause only from fields in unique_fields
if ($fetch === DATA_OBJECT_FETCH_BY_KEY && !empty($this->unique_fields)) {
if (empty($params['id'])) {
$where = array_intersect_key($params, array_flip($this->unique_fields));
}
else {
$where = array('id' => $params['id']);
}
// Compose where clause from given field names
} else if (is_array($fetch) && !empty($fetch)) {
$where = array_intersect_key($params, array_flip($fetch));
// Use entire params array for where clause
} else {
self::set_properties($this, $this->optional_fields);//apply defaults for optional fields
$where = $params;
}
// Attempt to load from database
if ($data = $this->fetch($where)) {
// Apply data from database, then data sent to constructor
self::set_properties($this, $data);
self::set_properties($this, $params);
} else {
// Apply defaults for optional fields, then data from constructor
self::set_properties($this, $this->optional_fields);
self::set_properties($this, $params);
}
}
/**
* 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.
*/
@@ -99,10 +151,12 @@ abstract class data_object {
/**
* Finds and returns a data_object instance based on params.
* @static abstract
*
* @param array $params associative arrays varname=>value
* @return object data_object instance or false if none found.
* This function MUST be overridden by all deriving classes.
*
* @param array $params associative arrays varname => value
* @throws coding_exception This function MUST be overridden
* @return data_object instance of data_object or false if none found.
*/
public static function fetch($params) {
throw new coding_exception('fetch() method needs to be overridden in each subclass of data_object');
@@ -111,7 +165,10 @@ abstract class data_object {
/**
* Finds and returns all data_object instances based on params.
*
* @param array $params associative arrays varname=>value
* This function MUST be overridden by all deriving classes.
*
* @param array $params associative arrays varname => value
* @throws coding_exception This function MUST be overridden
* @return array array of data_object instances or false if none found.
*/
public static function fetch_all($params) {
@@ -120,8 +177,12 @@ abstract class data_object {
/**
* Factory method - uses the parameters to retrieve matching instance from the DB.
* @static final protected
* @return mixed object instance or false if not found
*
* @final
* @param string $table The table name to fetch from
* @param string $classname The class that you want the result instantiated as
* @param array $params Any params required to select the desired row
* @return object Instance of $classname or false.
*/
protected static function fetch_helper($table, $classname, $params) {
if ($instances = self::fetch_all_helper($table, $classname, $params)) {
@@ -137,7 +198,11 @@ abstract class data_object {
/**
* Factory method - uses the parameters to retrieve all matching instances from the DB.
* @static final protected
*
* @final
* @param string $table The table name to fetch from
* @param string $classname The class that you want the result instantiated as
* @param array $params Any params required to select the desired row
* @return mixed array of object instances or false if not found
*/
public static function fetch_all_helper($table, $classname, $params) {
@@ -185,7 +250,8 @@ abstract class data_object {
/**
* Updates this object in the Database, based on its object variables. ID must be set.
* @return boolean success
*
* @return bool success
*/
public function update() {
global $DB;
@@ -205,7 +271,8 @@ abstract class data_object {
/**
* Deletes this object from the database.
* @return boolean success
*
* @return bool success
*/
public function delete() {
global $DB;
@@ -228,6 +295,8 @@ abstract class data_object {
/**
* Returns object with fields and values that are defined in database
*
* @return stdClass
*/
public function get_record_data() {
$data = new stdClass();
@@ -248,6 +317,7 @@ abstract class data_object {
* 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.
*
* @return int PK ID if successful, false otherwise
*/
public function insert() {
@@ -274,6 +344,8 @@ abstract class data_object {
* 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.
*
* @return bool True for success, false otherwise.
*/
public function update_from_db() {
if (empty($this->id)) {
@@ -294,7 +366,10 @@ abstract class data_object {
/**
* Given an associated array or object, cycles through each key/variable
* and assigns the value to the corresponding variable in this object.
* @static final
*
* @final
* @param data_object $instance
* @param array $params
*/
public static function set_properties(&$instance, $params) {
$params = (array) $params;
@@ -310,8 +385,8 @@ abstract class data_object {
* deleted in the database. Default does nothing, can be overridden to
* hook in special behaviour.
*
* @param bool $deleted
* @param bool $deleted Set this to true if it has been deleted.
*/
function notify_changed($deleted) {
public function notify_changed($deleted) {
}
}
+3 -2
View File
@@ -293,8 +293,9 @@ class completion_info {
*/
public function get_user_completion($user_id, $criteria) {
$params = array(
'course' => $this->course_id,
'userid' => $user_id,
'criteriaid' => $criteria->id,
'userid' => $user_id
);
$completion = new completion_criteria_completion($params);
@@ -336,7 +337,7 @@ class completion_info {
// Build array of criteria objects
$this->criteria = array();
foreach ($records as $record) {
$this->criteria[$record->id] = completion_criteria::factory($record);
$this->criteria[$record->id] = completion_criteria::factory((array)$record);
}
}