MDL-49543 badges: Add decription format and backup/restore
authorYuliya Bozhko <yuliya.bozhko@totaralms.com>
Mon, 23 Mar 2015 20:22:47 +0000 (09:22 +1300)
committerYuliya Bozhko <yuliya.bozhko@totaralms.com>
Mon, 23 Mar 2015 20:22:47 +0000 (09:22 +1300)
Signed-off-by: Yuliya Bozhko <yuliya.bozhko@totaralms.com>
backup/moodle2/backup_stepslib.php
backup/moodle2/restore_stepslib.php
badges/criteria/award_criteria.php
badges/criteria/award_criteria_course.php
badges/criteria/award_criteria_overall.php
badges/criteria_form.php
badges/renderer.php
badges/tests/badgeslib_test.php
lib/db/install.xml
lib/db/upgrade.php

index c761eba..edad7c9 100644 (file)
@@ -825,7 +825,7 @@ class backup_badges_structure_step extends backup_structure_step {
 
         $criteria = new backup_nested_element('criteria');
         $criterion = new backup_nested_element('criterion', array('id'), array('badgeid',
-                'criteriatype', 'method'));
+                'criteriatype', 'method', 'description', 'descriptionformat'));
 
         $parameters = new backup_nested_element('parameters');
         $parameter = new backup_nested_element('parameter', array('id'), array('critid',
index ca66102..014cfb4 100644 (file)
@@ -2269,9 +2269,11 @@ class restore_badges_structure_step extends restore_structure_step {
         $data = (object)$data;
 
         $params = array(
-                'badgeid'      => $this->get_new_parentid('badge'),
-                'criteriatype' => $data->criteriatype,
-                'method'       => $data->method
+                'badgeid'           => $this->get_new_parentid('badge'),
+                'criteriatype'      => $data->criteriatype,
+                'method'            => $data->method,
+                'description'       => $data->description,
+                'descriptionformat' => $data->descriptionformat,
         );
         $newid = $DB->insert_record('badge_criteria', $params);
         $this->set_mapping('criterion', $data->id, $newid);
index a4127db..2d102dc 100644 (file)
@@ -112,6 +112,12 @@ abstract class award_criteria {
      */
     public $description;
 
+    /**
+     * Format of the criterion description.
+     * @var integer
+     */
+    public $descriptionformat;
+
     /**
      * Any additional parameters.
      * @var array
@@ -128,6 +134,7 @@ abstract class award_criteria {
         $this->method = isset($params['method']) ? $params['method'] : BADGE_CRITERIA_AGGREGATION_ANY;
         $this->badgeid = $params['badgeid'];
         $this->description = isset($params['description']) ? $params['description'] : '';
+        $this->descriptionformat = isset($params['descriptionformat']) ? $params['descriptionformat'] : FORMAT_HTML;
         if (isset($params['id'])) {
             $this->params = $this->get_params($params['id']);
         }
@@ -249,7 +256,7 @@ abstract class award_criteria {
         if (!empty($this->description)) {
             $badge = new badge($this->badgeid);
             echo $OUTPUT->box(
-                format_text($this->description, FORMAT_HTML, array('context' => $badge->get_context())),
+                format_text($this->description, $this->descriptionformat, array('context' => $badge->get_context())),
                 'criteria-description'
                 );
         }
@@ -348,8 +355,10 @@ abstract class award_criteria {
         // Figure out criteria description.
         // If it is coming from the form editor, it is an array(text, format).
         $description = '';
+        $descriptionformat = FORMAT_HTML;
         if (isset($params['description']['text'])) {
             $description = $params['description']['text'];
+            $descriptionformat = $params['description']['format'];
         } else if (isset($params['description'])) {
             $description = $params['description'];
         }
@@ -359,6 +368,7 @@ abstract class award_criteria {
         $fordb->method = isset($params['agg']) ? $params['agg'] : BADGE_CRITERIA_AGGREGATION_ALL;
         $fordb->badgeid = $this->badgeid;
         $fordb->description = $description;
+        $fordb->descriptionformat = $descriptionformat;
         $t = $DB->start_delegated_transaction();
 
         // Pick only params that are required by this criterion.
@@ -434,6 +444,7 @@ abstract class award_criteria {
         $fordb->method = $this->method;
         $fordb->badgeid = $newbadgeid;
         $fordb->description = $this->description;
+        $fordb->descriptionformat = $this->descriptionformat;
         if (($newcrit = $DB->insert_record('badge_criteria', $fordb, true)) && isset($this->params)) {
             foreach ($this->params as $k => $param) {
                 foreach ($param as $key => $value) {
index 89897ea..2582365 100644 (file)
@@ -77,9 +77,11 @@ class award_criteria_course extends award_criteria {
 
         if (!empty($this->description)) {
             echo $OUTPUT->box(
-                format_text($this->description, FORMAT_HTML, array('context' => context_course::instance($this->courseid))),
+                format_text($this->description, $this->descriptionformat,
+                        array('context' => context_course::instance($this->courseid))
+                ),
                 'criteria-description'
-                );
+            );
         }
 
         if (!empty($this->params)) {
index 6e49098..7d6aad8 100644 (file)
@@ -48,7 +48,7 @@ class award_criteria_overall extends award_criteria {
             if (!empty($this->description)) {
                 $badge = new badge($this->badgeid);
                 echo $OUTPUT->box(
-                    format_text($this->description, FORMAT_HTML, array('context' => $badge->get_context())),
+                    format_text($this->description, $this->descriptionformat, array('context' => $badge->get_context())),
                     'criteria-description');
             }
             echo $OUTPUT->heading($this->get_title(), 2);
@@ -184,8 +184,10 @@ class award_criteria_overall extends award_criteria {
         // Sort out criteria description.
         // If it is coming from the form editor, it is an array of (text, format).
         $description = '';
+        $descriptionformat = FORMAT_HTML;
         if (isset($params['description']['text'])) {
             $description = $params['description']['text'];
+            $descriptionformat = $params['description']['format'];
         } else if (isset($params['description'])) {
             $description = $params['description'];
         }
@@ -194,6 +196,7 @@ class award_criteria_overall extends award_criteria {
         $fordb->criteriatype = $this->criteriatype;
         $fordb->badgeid = $this->badgeid;
         $fordb->description = $description;
+        $fordb->descriptionformat = $descriptionformat;
         if ($this->id !== 0) {
             $fordb->id = $this->id;
             $DB->update_record('badge_criteria', $fordb);
index 3bd659f..c8d18d1 100644 (file)
@@ -58,7 +58,11 @@ class edit_criteria_form extends moodleform {
                 $mform->addElement('header', 'description_header', get_string('description'));
                 $mform->addElement('editor', 'description', '', null, null);
                 $mform->setType('description', PARAM_RAW);
-                $mform->setDefault('description', array('text' => $criteria->description));
+                $mform->setDefault('description', array(
+                        'text' => $criteria->description,
+                        'format' => $criteria->descriptionformat
+                    )
+                );
 
                 $mform->closeHeaderBefore('buttonar');
                 $this->add_action_buttons(true, get_string('save', 'badges'));
index 990d3a8..d4fad08 100644 (file)
@@ -721,7 +721,7 @@ class core_badges_renderer extends plugin_renderer_base {
         $overall = $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL];
         if (!$short && !empty($overall->description)) {
             $overalldescr = $this->output->box(
-                format_text($overall->description, FORMAT_HTML, array('context' => $badge->get_context())),
+                format_text($overall->description, $overall->descriptionformat, array('context' => $badge->get_context())),
                 'criteria-description'
                 );
         }
@@ -745,7 +745,7 @@ class core_badges_renderer extends plugin_renderer_base {
             $c = reset($badge->criteria);
             if (!$short && !empty($c->description)) {
                 $overalldescr = $this->output->box(
-                    format_text($c->description, FORMAT_HTML, array('context' => $badge->get_context())),
+                    format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
                     'criteria-description'
                     );
             }
@@ -762,7 +762,7 @@ class core_badges_renderer extends plugin_renderer_base {
                 $criteriadescr = '';
                 if (!$short && !empty($c->description)) {
                     $criteriadescr = $this->output->box(
-                        format_text($c->description, FORMAT_HTML, array('context' => $badge->get_context())),
+                        format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
                         'criteria-description'
                         );
                 }
index 4483d14..b628431 100644 (file)
@@ -154,10 +154,19 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
 
     public function test_add_badge_criteria_description() {
         $criteriaoverall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
-        $criteriaoverall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'description' => 'Overall description'));
+        $criteriaoverall->save(array(
+                'agg' => BADGE_CRITERIA_AGGREGATION_ALL,
+                'description' => 'Overall description',
+                'descriptionformat' => FORMAT_HTML
+        ));
 
         $criteriaprofile = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_PROFILE, 'badgeid' => $this->badgeid));
-        $params = array('agg' => BADGE_CRITERIA_AGGREGATION_ALL, 'field_address' => 'address', 'description' => 'Description');
+        $params = array(
+                'agg' => BADGE_CRITERIA_AGGREGATION_ALL,
+                'field_address' => 'address',
+                'description' => 'Description',
+                'descriptionformat' => FORMAT_HTML
+        );
         $criteriaprofile->save($params);
 
         $badge = new badge($this->badgeid);
index 852bead..da0ef3b 100644 (file)
         <FIELD NAME="criteriatype" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The criteria type we are aggregating"/>
         <FIELD NAME="method" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="1" SEQUENCE="false" COMMENT="1 = all, 2 = any"/>
         <FIELD NAME="description" TYPE="text" NOTNULL="false" SEQUENCE="false"/>
+        <FIELD NAME="descriptionformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
       </FIELDS>
       <KEYS>
         <KEY NAME="primary" TYPE="primary" FIELDS="id"/>
index 0c785bd..f4df15b 100644 (file)
@@ -4249,13 +4249,19 @@ function xmldb_main_upgrade($oldversion) {
 
     if ($oldversion < 2015032000.00) {
         $table = new xmldb_table('badge_criteria');
-        $field = new xmldb_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
 
+        $field = new xmldb_field('description', XMLDB_TYPE_TEXT, null, null, null, null, null);
         // Conditionally add description field to the badge_criteria table.
         if (!$dbman->field_exists($table, $field)) {
             $dbman->add_field($table, $field);
         }
 
+        $field = new xmldb_field('descriptionformat', XMLDB_TYPE_INTEGER, 2, null, XMLDB_NOTNULL, null, 0);
+        // Conditionally add description format field to the badge_criteria table.
+        if (!$dbman->field_exists($table, $field)) {
+            $dbman->add_field($table, $field);
+        }
+
         upgrade_main_savepoint(true, 2015032000.00);
     }