Skip to content

Commit

Permalink
MDL-39955 completion: Added course_completed event
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajesh Taneja committed Aug 13, 2013
1 parent cdc5419 commit 06f8ea7
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 48 deletions.
36 changes: 36 additions & 0 deletions badges/classes/observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,40 @@ public static function course_module_criteria_review(\core\event\course_module_c
}
}

/**
* Triggered when 'course_completed' event is triggered.
*
* @param \core\event\course_completed $event
*/
public static function course_criteria_review(\core\event\course_completed $event) {
global $DB, $CFG;

if (!empty($CFG->enablebadges)) {
require_once($CFG->dirroot.'/lib/badgeslib.php');

$eventdata = $event->get_record_snapshot('course_completions', $event->objectid);
$userid = $event->other['relateduserid'];
$courseid = $event->courseid;

// Need to take into account that course can be a part of course_completion and courseset_completion criteria.
if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'course_' . $courseid, 'value' => $courseid))) {
foreach ($rs as $r) {
$crit = $DB->get_record('badge_criteria', array('id' => $r->critid), 'badgeid, criteriatype', MUST_EXIST);
$badge = new badge($crit->badgeid);
if (!$badge->is_active() || $badge->is_issued($userid)) {
continue;
}

if ($badge->criteria[$crit->criteriatype]->review($userid)) {
$badge->criteria[$crit->criteriatype]->mark_complete($userid);

if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
$badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
$badge->issue($userid);
}
}
}
}
}
}
}
22 changes: 17 additions & 5 deletions completion/completion_completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,35 @@ public function mark_inprogress($timestarted = null) {
* @return void
*/
public function mark_complete($timecomplete = null) {
global $USER;

// Never change a completion time
// Never change a completion time.
if ($this->timecompleted) {
return;
}

// Use current time if nothing supplied
// Use current time if nothing supplied.
if (!$timecomplete) {
$timecomplete = time();
}

// Set time complete
// Set time complete.
$this->timecompleted = $timecomplete;

// Save record
// Save record.
if ($result = $this->_save()) {
events_trigger('course_completed', $this->get_record_data());
$data = $this->get_record_data();
$event = \core\event\course_completed::create(
array(
'objectid' => $data->id,
'userid' => $USER->id,
'context' => context_course::instance($data->course),
'courseid' => $data->course,
'other' => array('relateduserid' => $data->userid)
)
);
$event->add_record_snapshot('course_completions', $data);
$event->trigger();
}

return $result;
Expand Down
1 change: 1 addition & 0 deletions lang/en/completion.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
$string['err_nousers'] = 'There are no students on this course or group for whom completion information is displayed. (By default, completion information is displayed only for students, so if there are no students, you will see this error. Administrators can alter this option via the admin screens.)';
$string['err_settingslocked'] = 'One or more students have already completed a criteria so the settings have been locked. Unlocking the completion criteria settings will delete any existing user data and may cause confusion.';
$string['err_system'] = 'An internal error occurred in the completion system. (System administrators can enable debugging information to see more detail.)';
$string['eventcoursecompleted'] = 'Course completed';
$string['eventcoursemodulecompletionupdated'] = 'Course module completion updated';
$string['excelcsvdownload'] = 'Download in Excel-compatible format (.csv)';
$string['fraction'] = 'Fraction';
Expand Down
37 changes: 0 additions & 37 deletions lib/badgeslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -931,43 +931,6 @@ function badges_add_course_navigation(navigation_node $coursenode, stdClass $cou
}
}

/**
* Triggered when 'course_completed' event happens.
*
* @param object $eventdata
* @return boolean
*/
function badges_award_handle_course_criteria_review(stdClass $eventdata) {
global $DB, $CFG;

if (!empty($CFG->enablebadges)) {
$userid = $eventdata->userid;
$courseid = $eventdata->course;

// Need to take into account that course can be a part of course_completion and courseset_completion criteria.
if ($rs = $DB->get_records('badge_criteria_param', array('name' => 'course_' . $courseid, 'value' => $courseid))) {
foreach ($rs as $r) {
$crit = $DB->get_record('badge_criteria', array('id' => $r->critid), 'badgeid, criteriatype', MUST_EXIST);
$badge = new badge($crit->badgeid);
if (!$badge->is_active() || $badge->is_issued($userid)) {
continue;
}

if ($badge->criteria[$crit->criteriatype]->review($userid)) {
$badge->criteria[$crit->criteriatype]->mark_complete($userid);

if ($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->review($userid)) {
$badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->mark_complete($userid);
$badge->issue($userid);
}
}
}
}
}

return true;
}

/**
* Triggered when 'user_updated' event happens.
*
Expand Down
83 changes: 83 additions & 0 deletions lib/classes/event/course_completed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace core\event;

/**
* Event when course completed.
*
* @package core_event
* @copyright 2013 Rajesh Taneja <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class course_completed extends base {

/**
* Initialise required event data properties.
*/
protected function init() {
$this->data['objecttable'] = 'course_completions';
$this->data['crud'] = 'u';
// TODO: MDL-37658 set level.
$this->data['level'] = 50;
}

/**
* Returns localised event name.
*
* @return string
*/
public static function get_name() {
return new get_string('eventcoursecompleted', 'core_completion');
}

/**
* Returns non-localised event description with id's for admin use only.
*
* @return string
*/
public function get_description() {
return 'Course completed by user '.$this->userid;
}

/**
* Returns relevant URL.
*
* @return \moodle_url
*/
public function get_url() {
return new moodle_url('/report/completion/index.php', array('course' => $this->courseid));
}

/**
* Return name of the legacy event, which is replaced by this event.
*
* @return string legacy event name
*/
public static function get_legacy_eventname() {
return 'course_completed';
}

/**
* Return course_completed legacy event data.
*
* @return \stdClass completion data.
*/
protected function get_legacy_eventdata() {
return $this->get_record_snapshot('course_completions', $this->objectid);
}

}
11 changes: 5 additions & 6 deletions lib/db/events.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@

$handlers = array(

'course_completed' => array (
'handlerfile' => '/lib/badgeslib.php',
'handlerfunction' => 'badges_award_handle_course_criteria_review',
'schedule' => 'instant',
'internal' => 1,
),
'user_updated' => array (
'handlerfile' => '/lib/badgeslib.php',
'handlerfunction' => 'badges_award_handle_profile_criteria_review',
Expand Down Expand Up @@ -72,7 +66,12 @@
array(
'eventname' => '\core\event\course_module_completion_updated',
'callback' => 'core_badges_observer::course_module_criteria_review',
),
array(
'eventname' => '\core\event\course_completed',
'callback' => 'core_badges_observer::course_criteria_review',
)

);


Expand Down

0 comments on commit 06f8ea7

Please sign in to comment.