Merge branch 'install_master' of git://github.com/amosbot/moodle
[moodle.git] / lib / completion / completion_criteria_completion.php
CommitLineData
2be4d090
MD
1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
19 * Completion data for a specific user, course and critieria
20 *
21 * @package moodlecore
22 * @copyright 2009 Catalyst IT Ltd
23 * @author Aaron Barnes <aaronb@catalyst.net.nz>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
26require_once($CFG->libdir.'/completion/data_object.php');
27
28
29/**
30 * Completion data for a specific user, course and critieria
31 */
32class completion_criteria_completion extends data_object {
33
34 /**
35 * DB Table
36 * @var string $table
37 */
38 public $table = 'course_completion_crit_compl';
39
40 /**
41 * Array of required table fields, must start with 'id'.
42 * @var array $required_fields
43 */
44 public $required_fields = array('id', 'userid', 'course', 'criteriaid', 'gradefinal', 'rpl', 'deleted', 'unenroled', 'timecompleted');
45
46 /**
47 * User ID
48 * @access public
49 * @var int
50 */
51 public $userid;
52
53 /**
54 * Course ID
55 * @access public
56 * @var int
57 */
58 public $course;
59
60 /**
61 * The id of the course completion criteria this completion references
62 * @access public
63 * @var int
64 */
65 public $criteriaid;
66
67 /**
68 * The final grade for the user in the course (if completing a grade criteria)
69 * @access public
70 * @var float
71 */
72 public $gradefinal;
73
74 /**
75 * Record of prior learning, leave blank if none
76 * @access public
77 * @var string
78 */
79 public $rpl;
80
81 /**
82 * Course deleted flag
83 * @access public
84 * @var boolean
85 */
86 public $deleted;
87
88 /**
89 * Timestamp of user unenrolment (if completing a unenrol criteria)
90 * @access public
91 * @var int (timestamp)
92 */
93 public $unenroled;
94
95 /**
96 * Timestamp of course criteria completion
97 * @see completion_criteria_completion::mark_complete()
98 * @access public
99 * @var int (timestamp)
100 */
101 public $timecompleted;
102
103 /**
104 * Associated criteria object
105 * @access private
106 * @var object completion_criteria
107 */
108 private $_criteria;
109
110 /**
111 * Finds and returns a data_object instance based on params.
112 * @static abstract
113 *
114 * @param array $params associative arrays varname=>value
115 * @return object data_object instance or false if none found.
116 */
117 public static function fetch($params) {
118 $params['deleted'] = null;
119 return self::fetch_helper('course_completion_crit_compl', __CLASS__, $params);
120 }
121
122 /**
123 * Finds and returns all data_object instances based on params.
124 * @static abstract
125 *
126 * @param array $params associative arrays varname=>value
127 * @return array array of data_object insatnces or false if none found.
128 */
129 public static function fetch_all($params) {}
130
131 /**
132 * Return status of this criteria completion
133 * @access public
134 * @return boolean
135 */
136 public function is_complete() {
137 return (bool) $this->timecompleted;
138 }
139
140 /**
141 * Mark this criteria complete for the associated user
142 *
143 * This method creates a course_completion_crit_compl record
144 * @access public
145 * @return void
146 */
147 public function mark_complete() {
148 // Create record
149 $this->timecompleted = time();
150
151 // Save record
152 if ($this->id) {
153 $this->update();
154 } else {
155 $this->insert();
156 }
157
158 // Mark course completion record as started (if not already)
159 $cc = array(
160 'course' => $this->course,
161 'userid' => $this->userid
162 );
163 $ccompletion = new completion_completion($cc);
164 $ccompletion->mark_inprogress($this->timecompleted);
165 }
166
167 /**
168 * Attach a preloaded criteria object to this object
169 * @access public
170 * @param $criteria object completion_criteria
171 * @return void
172 */
173 public function attach_criteria(completion_criteria $criteria) {
174 $this->_criteria = $criteria;
175 }
176
177 /**
178 * Return the associated criteria with this completion
179 * If nothing attached, load from the db
180 * @access public
181 * @return object completion_criteria
182 */
183 public function get_criteria() {
184
185 if (!$this->_criteria)
186 {
187 global $DB;
188
189 $params = array(
190 'id' => $this->criteriaid
191 );
192
193 $record = $DB->get_record('course_completion_criteria', $params);
194
195 $this->attach_criteria(completion_criteria::factory($record));
196 }
197
198 return $this->_criteria;
199 }
200
201 /**
202 * Return criteria status text for display in reports
203 * @see completion_criteria::get_status()
204 * @access public
205 * @return string
206 */
207 public function get_status() {
208 return $this->_criteria->get_status($this);
209 }
210}