MDL-39397 badges: Handle course badges when course is deleted
authorYuliya Bozhko <yuliya.bozhko@totaralms.com>
Mon, 29 Apr 2013 01:26:24 +0000 (13:26 +1200)
committerYuliya Bozhko <yuliya.bozhko@totaralms.com>
Wed, 8 May 2013 04:12:44 +0000 (16:12 +1200)
badges/criteria/award_criteria_course.php
badges/criteria/award_criteria_courseset.php
badges/criteria_form.php
badges/criteria_settings.php
lib/badgeslib.php
lib/moodlelib.php

index 1ad58a5..6912813 100644 (file)
@@ -77,12 +77,17 @@ class award_criteria_course extends award_criteria {
         $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 @@ class award_criteria_course extends award_criteria {
      *
      */
     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;
index 6443bd5..22adb08 100644 (file)
@@ -74,7 +74,7 @@ class award_criteria_courseset extends award_criteria {
     }
 
     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 @@ class award_criteria_courseset extends award_criteria {
             $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 @@ class award_criteria_courseset extends award_criteria {
             $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);
         }
     }
index 65cc718..208060d 100644 (file)
@@ -39,11 +39,16 @@ class edit_criteria_form extends moodleform {
         $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) {
index 943ecb0..486febd 100644 (file)
@@ -75,7 +75,7 @@ if ($edit) {
     $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()) {
index 3c82be7..e170dc7 100644 (file)
@@ -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);
+    }
+}
index 8ef210f..e81be0f 100644 (file)
@@ -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);