diff --git a/badges/criteria/award_criteria_course.php b/badges/criteria/award_criteria_course.php index 1ad58a52b5d36..691281376ff3b 100644 --- a/badges/criteria/award_criteria_course.php +++ b/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; diff --git a/badges/criteria/award_criteria_courseset.php b/badges/criteria/award_criteria_courseset.php index 6443bd5aa3860..22adb08ea6a3f 100644 --- a/badges/criteria/award_criteria_courseset.php +++ b/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); } } diff --git a/badges/criteria_form.php b/badges/criteria_form.php index 65cc718e7cfb0..208060d4cbe96 100644 --- a/badges/criteria_form.php +++ b/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) { diff --git a/badges/criteria_settings.php b/badges/criteria_settings.php index 943ecb0a73d89..486febda0e0c4 100644 --- a/badges/criteria_settings.php +++ b/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()) { diff --git a/lib/badgeslib.php b/lib/badgeslib.php index 3c82be7d72db4..e170dc741e9fc 100644 --- a/lib/badgeslib.php +++ b/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); + } +} diff --git a/lib/moodlelib.php b/lib/moodlelib.php index 8ef210f5c455b..e81be0f2ba67c 100644 --- a/lib/moodlelib.php +++ b/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);