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
+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();
}
}
}