weekly release 2.3dev
[moodle.git] / lib / 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
SH
27defined('MOODLE_INTERNAL') || die();
28require_once($CFG->libdir.'/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'. */
2be4d090
MD
45 public $required_fields = array('id', 'userid', 'course', 'criteriaid', 'gradefinal', 'rpl', 'deleted', 'unenroled', 'timecompleted');
46
95dd54ee 47 /* @var int User ID */
2be4d090
MD
48 public $userid;
49
95dd54ee 50 /* @var int course ID */
2be4d090
MD
51 public $course;
52
95dd54ee 53 /* @var int The id of the course completion criteria this completion references */
2be4d090
MD
54 public $criteriaid;
55
95dd54ee 56 /* @var float The final grade for the user in the course (if completing a grade criteria) */
2be4d090
MD
57 public $gradefinal;
58
95dd54ee 59 /* @var string Record of prior learning, leave blank if none */
2be4d090
MD
60 public $rpl;
61
95dd54ee 62 /* @var bool Course deleted flag */
2be4d090
MD
63 public $deleted;
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) {
81 $params['deleted'] = null;
82 return self::fetch_helper('course_completion_crit_compl', __CLASS__, $params);
83 }
84
85 /**
86 * Finds and returns all data_object instances based on params.
2be4d090
MD
87 *
88 * @param array $params associative arrays varname=>value
89 * @return array array of data_object insatnces or false if none found.
90 */
91 public static function fetch_all($params) {}
92
93 /**
94 * Return status of this criteria completion
95dd54ee
AA
95 *
96 * @return bool
2be4d090
MD
97 */
98 public function is_complete() {
99 return (bool) $this->timecompleted;
100 }
101
102 /**
103 * Mark this criteria complete for the associated user
104 *
105 * This method creates a course_completion_crit_compl record
2be4d090
MD
106 */
107 public function mark_complete() {
108 // Create record
109 $this->timecompleted = time();
110
111 // Save record
112 if ($this->id) {
113 $this->update();
114 } else {
115 $this->insert();
116 }
117
118 // Mark course completion record as started (if not already)
119 $cc = array(
120 'course' => $this->course,
121 'userid' => $this->userid
122 );
123 $ccompletion = new completion_completion($cc);
124 $ccompletion->mark_inprogress($this->timecompleted);
125 }
126
127 /**
128 * Attach a preloaded criteria object to this object
95dd54ee 129 *
2be4d090 130 * @param $criteria object completion_criteria
2be4d090
MD
131 */
132 public function attach_criteria(completion_criteria $criteria) {
133 $this->_criteria = $criteria;
134 }
135
136 /**
137 * Return the associated criteria with this completion
138 * If nothing attached, load from the db
95dd54ee 139 *
836375ec 140 * @return completion_criteria
2be4d090
MD
141 */
142 public function get_criteria() {
143
f55ff38a 144 if (!$this->_criteria) {
2be4d090
MD
145 global $DB;
146
147 $params = array(
148 'id' => $this->criteriaid
149 );
150
151 $record = $DB->get_record('course_completion_criteria', $params);
152
153 $this->attach_criteria(completion_criteria::factory($record));
154 }
155
156 return $this->_criteria;
157 }
158
159 /**
f55ff38a 160 * Return criteria status text for display in reports {@link completion_criteria::get_status()}
95dd54ee 161 *
836375ec 162 * @return string
2be4d090
MD
163 */
164 public function get_status() {
165 return $this->_criteria->get_status($this);
166 }
167}