Merge branch 'MOODLE_311_STABLE_MDL-59115' of https://github.com/mattporritt/moodle...
[moodle.git] / completion / completion_completion.php
blob4ddc81ab2810a6586cd6056c303d4acdcb5d2ba2
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Course completion status for a particular user/course
20 * @package core_completion
21 * @category completion
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
27 defined('MOODLE_INTERNAL') || die();
28 require_once($CFG->dirroot.'/completion/data_object.php');
30 /**
31 * Course completion status for a particular user/course
33 * @package core_completion
34 * @category completion
35 * @copyright 2009 Catalyst IT Ltd
36 * @author Aaron Barnes <aaronb@catalyst.net.nz>
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class completion_completion extends data_object {
41 /* @var string $table Database table name that stores completion information */
42 public $table = 'course_completions';
44 /* @var array $required_fields Array of required table fields, must start with 'id'. */
45 public $required_fields = array('id', 'userid', 'course',
46 'timeenrolled', 'timestarted', 'timecompleted', 'reaggregate');
48 /* @var int $userid User ID */
49 public $userid;
51 /* @var int $course Course ID */
52 public $course;
54 /* @var int Time of course enrolment {@link completion_completion::mark_enrolled()} */
55 public $timeenrolled;
57 /**
58 * Time the user started their course completion {@link completion_completion::mark_inprogress()}
59 * @var int
61 public $timestarted;
63 /* @var int Timestamp of course completion {@link completion_completion::mark_complete()} */
64 public $timecompleted;
66 /* @var int Flag to trigger cron aggregation (timestamp) */
67 public $reaggregate;
70 /**
71 * Finds and returns a data_object instance based on params.
73 * @param array $params associative arrays varname = >value
74 * @return data_object instance of data_object or false if none found.
76 public static function fetch($params) {
77 $cache = cache::make('core', 'coursecompletion');
79 $key = $params['userid'] . '_' . $params['course'];
80 if ($hit = $cache->get($key)) {
81 return $hit['value'];
84 $tocache = self::fetch_helper('course_completions', __CLASS__, $params);
85 $cache->set($key, ['value' => $tocache]);
86 return $tocache;
89 /**
90 * Return status of this completion
92 * @return bool
94 public function is_complete() {
95 return (bool) $this->timecompleted;
98 /**
99 * Mark this user as started (or enrolled) in this course
101 * If the user is already marked as started, no change will occur
103 * @param integer $timeenrolled Time enrolled (optional)
105 public function mark_enrolled($timeenrolled = null) {
107 if ($this->timeenrolled === null) {
109 if ($timeenrolled === null) {
110 $timeenrolled = time();
113 $this->timeenrolled = $timeenrolled;
116 return $this->_save();
120 * Mark this user as inprogress in this course
122 * If the user is already marked as inprogress, the time will not be changed
124 * @param integer $timestarted Time started (optional)
126 public function mark_inprogress($timestarted = null) {
128 $timenow = time();
130 // Set reaggregate flag
131 $this->reaggregate = $timenow;
133 if (!$this->timestarted) {
135 if (!$timestarted) {
136 $timestarted = $timenow;
139 $this->timestarted = $timestarted;
142 return $this->_save();
146 * Mark this user complete in this course
148 * This generally happens when the required completion criteria
149 * in the course are complete.
151 * @param integer $timecomplete Time completed (optional)
152 * @return void
154 public function mark_complete($timecomplete = null) {
155 global $USER;
157 // Never change a completion time.
158 if ($this->timecompleted) {
159 return;
162 // Use current time if nothing supplied.
163 if (!$timecomplete) {
164 $timecomplete = time();
167 // Set time complete.
168 $this->timecompleted = $timecomplete;
170 // Save record.
171 if ($result = $this->_save()) {
172 $data = $this->get_record_data();
173 \core\event\course_completed::create_from_completion($data)->trigger();
176 // Notify user.
177 $course = get_course($data->course);
178 $messagesubject = get_string('coursecompleted', 'completion');
179 $a = [
180 'coursename' => get_course_display_name_for_list($course),
181 'courselink' => (string) new moodle_url('/course/view.php', array('id' => $course->id)),
183 $messagebody = get_string('coursecompletedmessage', 'completion', $a);
184 $messageplaintext = html_to_text($messagebody);
186 $eventdata = new \core\message\message();
187 $eventdata->courseid = $course->id;
188 $eventdata->component = 'moodle';
189 $eventdata->name = 'coursecompleted';
190 $eventdata->userfrom = core_user::get_noreply_user();
191 $eventdata->userto = $data->userid;
192 $eventdata->notification = 1;
193 $eventdata->subject = $messagesubject;
194 $eventdata->fullmessage = $messageplaintext;
195 $eventdata->fullmessageformat = FORMAT_HTML;
196 $eventdata->fullmessagehtml = $messagebody;
197 $eventdata->smallmessage = $messageplaintext;
199 if ($courseimage = \core_course\external\course_summary_exporter::get_course_image($course)) {
200 $eventdata->customdata = [
201 'notificationpictureurl' => $courseimage,
204 message_send($eventdata);
206 return $result;
210 * Save course completion status
212 * This method creates a course_completions record if none exists
213 * @access private
214 * @return bool
216 private function _save() {
217 if ($this->timeenrolled === null) {
218 $this->timeenrolled = 0;
221 $result = false;
222 // Save record
223 if ($this->id) {
224 $result = $this->update();
225 } else {
226 // Make sure reaggregate field is not null
227 if (!$this->reaggregate) {
228 $this->reaggregate = 0;
231 // Make sure timestarted is not null
232 if (!$this->timestarted) {
233 $this->timestarted = 0;
236 $result = $this->insert();
239 if ($result) {
240 // Update the cached record.
241 $cache = cache::make('core', 'coursecompletion');
242 $data = $this->get_record_data();
243 $key = $data->userid . '_' . $data->course;
244 $cache->set($key, ['value' => $data]);
247 return $result;