Browse files

MDL-39397 badges: Handle course badges when course is deleted

  • Loading branch information...
1 parent cf5a329 commit 7deff81f80834afc1b84a8d2b65da9d0f6aa7f7f @ybozhko ybozhko committed Apr 29, 2013
View
30 badges/criteria/award_criteria_course.php
@@ -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;
}
@@ -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;
View
6 badges/criteria/award_criteria_courseset.php
@@ -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();
@@ -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');
@@ -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);
}
}
View
5 badges/criteria_form.php
@@ -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) {
View
2 badges/criteria_settings.php
@@ -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()) {
View
30 lib/badgeslib.php
@@ -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);
+ }
+}
View
3 lib/moodlelib.php
@@ -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);

0 comments on commit 7deff81

Please sign in to comment.