@@ -37,13 +37,20 @@ class award_criteria_activity extends award_criteria {
3737 public $ criteriatype = BADGE_CRITERIA_TYPE_ACTIVITY ;
3838
3939 private $ courseid ;
40+ private $ coursestartdate ;
4041
4142 public $ required_param = 'module ' ;
4243 public $ optional_params = array ('bydate ' );
4344
4445 public function __construct ($ record ) {
46+ global $ DB ;
4547 parent ::__construct ($ record );
46- $ this ->courseid = self ::get_course ();
48+
49+ $ course = $ DB ->get_record_sql ('SELECT b.courseid, c.startdate
50+ FROM {badge} b INNER JOIN {course} c ON b.courseid = c.id
51+ WHERE b.id = :badgeid ' , array ('badgeid ' => $ this ->badgeid ));
52+ $ this ->courseid = $ course ->courseid ;
53+ $ this ->coursestartdate = $ course ->startdate ;
4754 }
4855
4956 /**
@@ -95,17 +102,6 @@ public function get_details($short = '') {
95102 }
96103 }
97104
98- /**
99- * Return course ID for activities
100- *
101- * @return int
102- */
103- private function get_course () {
104- global $ DB ;
105- $ courseid = $ DB ->get_field ('badge ' , 'courseid ' , array ('id ' => $ this ->badgeid ));
106- return $ courseid ;
107- }
108-
109105 /**
110106 * Add appropriate new criteria options to the form
111107 *
@@ -184,14 +180,17 @@ public function get_options(&$mform) {
184180 * Review this criteria and decide if it has been completed
185181 *
186182 * @param int $userid User whose criteria completion needs to be reviewed.
183+ * @param bool $filtered An additional parameter indicating that user list
184+ * has been reduced and some expensive checks can be skipped.
185+ *
187186 * @return bool Whether criteria is complete
188187 */
189- public function review ($ userid ) {
190- global $ DB ;
188+ public function review ($ userid , $ filtered = false ) {
191189 $ completionstates = array (COMPLETION_COMPLETE , COMPLETION_COMPLETE_PASS );
192- $ course = $ DB ->get_record ('course ' , array ('id ' => $ this ->courseid ));
190+ $ course = new stdClass ();
191+ $ course ->id = $ this ->courseid ;
193192
194- if ($ course -> startdate > time ()) {
193+ if ($ this -> coursestartdate > time ()) {
195194 return false ;
196195 }
197196
@@ -217,7 +216,7 @@ public function review($userid) {
217216 } else {
218217 return false ;
219218 }
220- } else if ( $ this -> method == BADGE_CRITERIA_AGGREGATION_ANY ) {
219+ } else {
221220 if (in_array ($ data ->completionstate , $ completionstates ) && $ check_date ) {
222221 return true ;
223222 } else {
@@ -229,4 +228,44 @@ public function review($userid) {
229228
230229 return $ overall ;
231230 }
231+
232+ /**
233+ * Returns array with sql code and parameters returning all ids
234+ * of users who meet this particular criterion.
235+ *
236+ * @return array list($join, $where, $params)
237+ */
238+ public function get_completed_criteria_sql () {
239+ $ join = '' ;
240+ $ where = '' ;
241+ $ params = array ();
242+
243+ if ($ this ->method == BADGE_CRITERIA_AGGREGATION_ANY ) {
244+ foreach ($ this ->params as $ param ) {
245+ $ moduledata [] = " cmc.coursemoduleid = :completedmodule {$ param ['module ' ]} " ;
246+ $ params ["completedmodule {$ param ['module ' ]}" ] = $ param ['module ' ];
247+ }
248+ if (!empty ($ moduledata )) {
249+ $ extraon = implode (' OR ' , $ moduledata );
250+ $ join = " JOIN {course_modules_completion} cmc ON cmc.userid = u.id AND
251+ ( cmc.completionstate = :completionpass OR cmc.completionstate = :completioncomplete ) AND ( {$ extraon }) " ;
252+ $ params ["completionpass " ] = COMPLETION_COMPLETE_PASS ;
253+ $ params ["completioncomplete " ] = COMPLETION_COMPLETE ;
254+ }
255+ return array ($ join , $ where , $ params );
256+ } else {
257+ foreach ($ this ->params as $ param ) {
258+ $ join .= " LEFT JOIN {course_modules_completion} cmc {$ param ['module ' ]} ON
259+ cmc {$ param ['module ' ]}.userid = u.id AND
260+ cmc {$ param ['module ' ]}.coursemoduleid = :completedmodule {$ param ['module ' ]} AND
261+ ( cmc {$ param ['module ' ]}.completionstate = :completionpass {$ param ['module ' ]} OR
262+ cmc {$ param ['module ' ]}.completionstate = :completioncomplete {$ param ['module ' ]} ) " ;
263+ $ where .= " AND cmc {$ param ['module ' ]}.coursemoduleid IS NOT NULL " ;
264+ $ params ["completedmodule {$ param ['module ' ]}" ] = $ param ['module ' ];
265+ $ params ["completionpass {$ param ['module ' ]}" ] = COMPLETION_COMPLETE_PASS ;
266+ $ params ["completioncomplete {$ param ['module ' ]}" ] = COMPLETION_COMPLETE ;
267+ }
268+ return array ($ join , $ where , $ params );
269+ }
270+ }
232271}
0 commit comments