3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
20 * Course completion status for a particular user/course
23 * @copyright 2009 Catalyst IT Ltd
24 * @author Aaron Barnes <aaronb@catalyst.net.nz>
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once($CFG->libdir.'/completion/data_object.php');
31 * Course completion status for a particular user/course
33 class completion_completion extends data_object {
39 public $table = 'course_completions';
42 * Array of required table fields, must start with 'id'.
43 * @var array $required_fields
45 public $required_fields = array('id', 'userid', 'course', 'deleted', 'timenotified',
46 'timeenrolled', 'timestarted', 'timecompleted', 'reaggregate');
63 * Set to 1 if this record has been deleted
70 * Timestamp the interested parties were notified
71 * of this user's completion
78 * Time of course enrolment
79 * @see completion_completion::mark_enrolled()
86 * Time the user started their course completion
87 * @see completion_completion::mark_inprogress()
94 * Timestamp of course completion
95 * @see completion_completion::mark_complete()
99 public $timecompleted;
102 * Flag to trigger cron aggregation (timestamp)
110 * Finds and returns a data_object instance based on params.
113 * @param array $params associative arrays varname=>value
114 * @return object data_object instance or false if none found.
116 public static function fetch($params) {
117 $params['deleted'] = null;
118 return self::fetch_helper('course_completions', __CLASS__, $params);
122 * Return status of this completion
126 public function is_complete() {
127 return (bool) $this->timecompleted;
131 * Mark this user as started (or enrolled) in this course
133 * If the user is already marked as started, no change will occur
136 * @param integer $timeenrolled Time enrolled (optional)
139 public function mark_enrolled($timeenrolled = null) {
141 if ($this->timeenrolled === null) {
143 if ($timeenrolled === null) {
144 $timeenrolled = time();
147 $this->timeenrolled = $timeenrolled;
154 * Mark this user as inprogress in this course
156 * If the user is already marked as inprogress,
157 * the time will not be changed
160 * @param integer $timestarted Time started (optional)
163 public function mark_inprogress($timestarted = null) {
167 // Set reaggregate flag
168 $this->reaggregate = $timenow;
170 if (!$this->timestarted) {
173 $timestarted = $timenow;
176 $this->timestarted = $timestarted;
183 * Mark this user complete in this course
185 * This generally happens when the required completion criteria
186 * in the course are complete.
189 * @param integer $timecomplete Time completed (optional)
192 public function mark_complete($timecomplete = null) {
194 // Never change a completion time
195 if ($this->timecompleted) {
199 // Use current time if nothing supplied
200 if (!$timecomplete) {
201 $timecomplete = time();
205 $this->timecompleted = $timecomplete;
212 * Save course completion status
214 * This method creates a course_completions record if none exists
218 private function _save() {
222 if ($this->timeenrolled === null) {
223 $this->timeenrolled = 0;
230 // Make sure reaggregate field is not null
231 if (!$this->reaggregate) {
232 $this->reaggregate = 0;