Merge branch 'MDL-29320-master' of git://github.com/junpataleta/moodle
[moodle.git] / completion / completion_criteria_completion.php
CommitLineData
2be4d090 1<?php
2be4d090
MD
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.
13//
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/>.
16
17/**
18 * Completion data for a specific user, course and critieria
19 *
836375ec
SH
20 * @package core_completion
21 * @category completion
2be4d090 22 * @copyright 2009 Catalyst IT Ltd
836375ec
SH
23 * @author Aaron Barnes <aaronb@catalyst.net.nz>
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2be4d090 25 */
2be4d090 26
836375ec 27defined('MOODLE_INTERNAL') || die();
75342343 28require_once($CFG->dirroot.'/completion/data_object.php');
2be4d090
MD
29
30/**
31 * Completion data for a specific user, course and critieria
836375ec
SH
32 *
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
2be4d090
MD
38 */
39class completion_criteria_completion extends data_object {
40
95dd54ee 41 /* @var string Database table that stores completion type criteria */
2be4d090
MD
42 public $table = 'course_completion_crit_compl';
43
95dd54ee 44 /* @var array Array of required table fields, must start with 'id'. */
46eca1f7 45 public $required_fields = array('id', 'userid', 'course', 'criteriaid', 'gradefinal', 'rpl', 'unenroled', 'timecompleted');
2be4d090 46
dbfcf440
AB
47 /* @var array Array of unique fields, used in where clauses */
48 public $unique_fields = array('userid', 'course', 'criteriaid');
49
95dd54ee 50 /* @var int User ID */
2be4d090
MD
51 public $userid;
52
95dd54ee 53 /* @var int course ID */
2be4d090
MD
54 public $course;
55
95dd54ee 56 /* @var int The id of the course completion criteria this completion references */
2be4d090
MD
57 public $criteriaid;
58
95dd54ee 59 /* @var float The final grade for the user in the course (if completing a grade criteria) */
2be4d090
MD
60 public $gradefinal;
61
95dd54ee 62 /* @var string Record of prior learning, leave blank if none */
2be4d090
MD
63 public $rpl;
64
95dd54ee 65 /* @var int Timestamp of user unenrolment (if completing a unenrol criteria) */
2be4d090
MD
66 public $unenroled;
67
f55ff38a 68 /* @var int Timestamp of course criteria completion {@link completion_criteria_completion::mark_complete()} */
2be4d090
MD
69 public $timecompleted;
70
95dd54ee 71 /* @var completion_criterria Associated criteria object */
2be4d090
MD
72 private $_criteria;
73
74 /**
75 * Finds and returns a data_object instance based on params.
2be4d090
MD
76 *
77 * @param array $params associative arrays varname=>value
836375ec 78 * @return data_object instance of data_object or false if none found.
2be4d090
MD
79 */
80 public static function fetch($params) {
2be4d090
MD
81 return self::fetch_helper('course_completion_crit_compl', __CLASS__, $params);
82 }
83
84 /**
85 * Finds and returns all data_object instances based on params.
2be4d090
MD
86 *
87 * @param array $params associative arrays varname=>value
88 * @return array array of data_object insatnces or false if none found.
89 */
90 public static function fetch_all($params) {}
91
92 /**
93 * Return status of this criteria completion
95dd54ee
AA
94 *
95 * @return bool
2be4d090
MD
96 */
97 public function is_complete() {
98 return (bool) $this->timecompleted;
99 }
100
101 /**
102 * Mark this criteria complete for the associated user
103 *
104 * This method creates a course_completion_crit_compl record
2be4d090
MD
105 */
106 public function mark_complete() {
107 // Create record
108 $this->timecompleted = time();
109
110 // Save record
111 if ($this->id) {
112 $this->update();
113 } else {
114 $this->insert();
115 }
116
117 // Mark course completion record as started (if not already)
118 $cc = array(
119 'course' => $this->course,
120 'userid' => $this->userid
121 );
122 $ccompletion = new completion_completion($cc);
123 $ccompletion->mark_inprogress($this->timecompleted);
124 }
125
126 /**
127 * Attach a preloaded criteria object to this object
95dd54ee 128 *
2be4d090 129 * @param $criteria object completion_criteria
2be4d090
MD
130 */
131 public function attach_criteria(completion_criteria $criteria) {
132 $this->_criteria = $criteria;
133 }
134
135 /**
136 * Return the associated criteria with this completion
137 * If nothing attached, load from the db
95dd54ee 138 *
836375ec 139 * @return completion_criteria
2be4d090
MD
140 */
141 public function get_criteria() {
142
f55ff38a 143 if (!$this->_criteria) {
2be4d090
MD
144 global $DB;
145
146 $params = array(
147 'id' => $this->criteriaid
148 );
149
150 $record = $DB->get_record('course_completion_criteria', $params);
151
9710fe73 152 $this->attach_criteria(completion_criteria::factory((array) $record));
2be4d090
MD
153 }
154
155 return $this->_criteria;
156 }
157
158 /**
f55ff38a 159 * Return criteria status text for display in reports {@link completion_criteria::get_status()}
95dd54ee 160 *
836375ec 161 * @return string
2be4d090
MD
162 */
163 public function get_status() {
164 return $this->_criteria->get_status($this);
165 }
166}