Merge branch 'MDL-77272-main' of https://github.com/SergioComeron/moodle
[moodle.git] / completion / completion_completion.php
blob47bd4c003f9fbe5237a8627b7bca98fc9e7866bd
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;
69 /** @var float user's course grade. */
70 public $gradefinal;
72 /**
73 * Finds and returns a data_object instance based on params.
75 * @param array $params associative arrays varname = >value
76 * @return data_object instance of data_object or false if none found.
78 public static function fetch($params) {
79 $cache = cache::make('core', 'coursecompletion');
81 $key = $params['userid'] . '_' . $params['course'];
82 if ($hit = $cache->get($key)) {
83 return $hit['value'];
86 $tocache = self::fetch_helper('course_completions', __CLASS__, $params);
87 $cache->set($key, ['value' => $tocache]);
88 return $tocache;
91 /**
92 * Return status of this completion
94 * @return bool
96 public function is_complete() {
97 return (bool) $this->timecompleted;
101 * Mark this user as started (or enrolled) in this course
103 * If the user is already marked as started, no change will occur
105 * @param integer $timeenrolled Time enrolled (optional)
106 * @return int|null id of completion record on successful update.
108 public function mark_enrolled($timeenrolled = null) {
110 if ($this->timeenrolled === null) {
112 if ($timeenrolled === null) {
113 $timeenrolled = time();
116 $this->timeenrolled = $timeenrolled;
119 return $this->_save();
123 * Mark this user as inprogress in this course
125 * If the user is already marked as inprogress, the time will not be changed
127 * @param integer $timestarted Time started (optional)
128 * @return int|null id of completion record on successful update.
130 public function mark_inprogress($timestarted = null) {
132 $timenow = time();
134 // Set reaggregate flag
135 $this->reaggregate = $timenow;
137 if (!$this->timestarted) {
139 if (!$timestarted) {
140 $timestarted = $timenow;
143 $this->timestarted = $timestarted;
146 return $this->_save();
150 * Mark this user complete in this course
152 * This generally happens when the required completion criteria
153 * in the course are complete.
155 * @param integer $timecomplete Time completed (optional)
156 * @return int|null id of completion record on successful update.
158 public function mark_complete($timecomplete = null) {
159 global $USER;
161 // Never change a completion time.
162 if ($this->timecompleted) {
163 return null;
166 // Use current time if nothing supplied.
167 if (!$timecomplete) {
168 $timecomplete = time();
171 // Set time complete.
172 $this->timecompleted = $timecomplete;
173 // Save record.
174 if ($result = $this->_save()) {
175 $data = $this->get_record_data();
176 \core\event\course_completed::create_from_completion($data)->trigger();
179 // Notify user.
180 $course = get_course($data->course);
181 $messagesubject = get_string('coursecompleted', 'completion');
182 $options = new stdClass();
183 $options->context = context_course::instance($course->id);
184 $a = [
185 'coursename' => format_string(get_course_display_name_for_list($course), true, $options),
186 'courselink' => (string) new moodle_url('/course/view.php', array('id' => $course->id)),
188 $messagebody = get_string('coursecompletedmessage', 'completion', $a);
189 $messageplaintext = html_to_text($messagebody);
191 $eventdata = new \core\message\message();
192 $eventdata->courseid = $course->id;
193 $eventdata->component = 'moodle';
194 $eventdata->name = 'coursecompleted';
195 $eventdata->userfrom = core_user::get_noreply_user();
196 $eventdata->userto = $data->userid;
197 $eventdata->notification = 1;
198 $eventdata->subject = $messagesubject;
199 $eventdata->fullmessage = $messageplaintext;
200 $eventdata->fullmessageformat = FORMAT_HTML;
201 $eventdata->fullmessagehtml = $messagebody;
202 $eventdata->smallmessage = $messageplaintext;
204 if ($courseimage = \core_course\external\course_summary_exporter::get_course_image($course)) {
205 $eventdata->customdata = [
206 'notificationpictureurl' => $courseimage,
209 message_send($eventdata);
211 return $result;
215 * Save course completion status
217 * This method creates a course_completions record if none exists
218 * @access private
219 * @return int|null id of completion record on successful update.
221 private function _save() {
222 if ($this->timeenrolled === null) {
223 $this->timeenrolled = 0;
226 // Save record
227 if (isset($this->id)) {
228 $success = $this->update();
229 } else {
230 // Make sure reaggregate field is not null
231 if (!$this->reaggregate) {
232 $this->reaggregate = 0;
235 // Make sure timestarted is not null
236 if (!$this->timestarted) {
237 $this->timestarted = 0;
240 $success = $this->insert();
243 if ($success) {
244 // Update the cached record.
245 $cache = cache::make('core', 'coursecompletion');
246 $data = $this->get_record_data();
247 $key = $data->userid . '_' . $data->course;
248 $cache->set($key, ['value' => $data]);
249 return $this->id;
252 return null;