Skip to content

Commit

Permalink
MDL-39397 badges: Handle course badges when course is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuliya Bozhko committed May 8, 2013
1 parent cf5a329 commit 7deff81
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 13 deletions.
30 changes: 21 additions & 9 deletions badges/criteria/award_criteria_course.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,17 @@ public function get_details($short = '') {
$param = reset($this->params);

$course = $DB->get_record('course', array('id' => $param['course']));
$str = '"' . $course->fullname . '"';
if (isset($param['bydate'])) {
$str .= get_string('criteria_descr_bydate', 'badges', userdate($param['bydate'], get_string('strftimedate', 'core_langconfig')));
}
if (isset($param['grade'])) {
$str .= get_string('criteria_descr_grade', 'badges', $param['grade']);
if (!$course) {
$str = $OUTPUT->error_text(get_string('error:nosuchcourse', 'badges'));
} else {
$options = array('context' => context_course::instance($course->id));
$str = html_writer::tag('b', '"' . format_string($course->fullname, true, $options) . '"');
if (isset($param['bydate'])) {
$str .= get_string('criteria_descr_bydate', 'badges', userdate($param['bydate'], get_string('strftimedate', 'core_langconfig')));
}
if (isset($param['grade'])) {
$str .= get_string('criteria_descr_grade', 'badges', $param['grade']);
}
}
return $str;
}
Expand All @@ -92,9 +97,16 @@ public function get_details($short = '') {
*
*/
public function get_options(&$mform) {
global $PAGE, $DB;
$param = array_shift($this->params);
$course = $DB->get_record('course', array('id' => $PAGE->course->id));
global $DB;
$param = array();

if ($this->id !== 0) {
$param = reset($this->params);
} else {
$param['course'] = $mform->getElementValue('course');
$mform->removeElement('course');
}
$course = $DB->get_record('course', array('id' => $param['course']));

if (!($course->enablecompletion == COMPLETION_ENABLED)) {
$none = true;
Expand Down
6 changes: 3 additions & 3 deletions badges/criteria/award_criteria_courseset.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function get_details($short = '') {
}

public function get_courses(&$mform) {
global $DB, $CFG, $PAGE;
global $DB, $CFG;
require_once($CFG->dirroot . '/course/lib.php');
$buttonarray = array();

Expand All @@ -100,7 +100,7 @@ public function get_courses(&$mform) {
$mform->addHelpButton('courses', 'addcourse', 'badges');

$buttonarray[] =& $mform->createElement('submit', 'submitcourse', get_string('addcourse', 'badges'));
$buttonarray[] =& $mform->createElement('submit', 'back', get_string('cancel'));
$buttonarray[] =& $mform->createElement('submit', 'cancel', get_string('cancel'));
$mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);

$mform->addElement('hidden', 'addcourse', 'addcourse');
Expand All @@ -111,7 +111,7 @@ public function get_courses(&$mform) {
$mform->setType('agg', PARAM_INT);
} else {
$mform->addElement('static', 'nocourses', '', get_string('error:nocourses', 'badges'));
$buttonarray[] =& $mform->createElement('submit', 'back', get_string('continue'));
$buttonarray[] =& $mform->createElement('submit', 'cancel', get_string('continue'));
$mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
}
}
Expand Down
5 changes: 5 additions & 0 deletions badges/criteria_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ public function definition() {
$mform = $this->_form;
$criteria = $this->_customdata['criteria'];
$addcourse = $this->_customdata['addcourse'];
$course = $this->_customdata['course'];

// Get course selector first if it's a new courseset criteria.
if (($criteria->id == 0 || $addcourse) && $criteria->criteriatype == BADGE_CRITERIA_TYPE_COURSESET) {
$criteria->get_courses($mform);
} else {
if ($criteria->id == 0 && $criteria->criteriatype == BADGE_CRITERIA_TYPE_COURSE) {
$mform->addElement('hidden', 'course', $course);
$mform->setType('course', PARAM_INT);
}
list($none, $message) = $criteria->get_options($mform);

if ($none) {
Expand Down
2 changes: 1 addition & 1 deletion badges/criteria_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
$criteria = award_criteria::build($cparams);
}

$mform = new edit_criteria_form($FULLME, array('criteria' => $criteria, 'addcourse' => $addcourse));
$mform = new edit_criteria_form($FULLME, array('criteria' => $criteria, 'addcourse' => $addcourse, 'course' => $badge->courseid));

if (!empty($addcourse)) {
if ($data = $mform->get_data()) {
Expand Down
30 changes: 30 additions & 0 deletions lib/badgeslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1305,3 +1305,33 @@ function badges_user_has_backpack($userid) {
global $DB;
return $DB->record_exists('badge_backpack', array('userid' => $userid));
}

/**
* Handles what happens to the course badges when a course is deleted.
*
* @param int $courseid course ID.
* @return void.
*/
function badges_handle_course_deletion($courseid) {
global $CFG, $DB;
include_once $CFG->libdir . '/filelib.php';

$systemcontext = context_system::instance();
$coursecontext = context_course::instance($courseid);
$fs = get_file_storage();

// Move badges images to the system context.
$fs->move_area_files_to_new_context($coursecontext->id, $systemcontext->id, 'badges', 'badgeimage');

// Get all course badges.
$badges = $DB->get_records('badge', array('type' => BADGE_TYPE_COURSE, 'courseid' => $courseid));
foreach ($badges as $badge) {
// Archive badges in this course.
$toupdate = new stdClass();
$toupdate->id = $badge->id;
$toupdate->type = BADGE_TYPE_SITE;
$toupdate->courseid = null;
$toupdate->status = BADGE_STATUS_ARCHIVED;
$DB->update_record('badge', $toupdate);
}
}
3 changes: 3 additions & 0 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -4730,6 +4730,9 @@ function delete_course($courseorid, $showfeedback = true) {
return false;
}

// Handle course badges.
badges_handle_course_deletion($courseid);

// make the course completely empty
remove_course_contents($courseid, $showfeedback);

Expand Down

0 comments on commit 7deff81

Please sign in to comment.