MDL-49543 badges: Add description to the badge criteria

Signed-off-by: Yuliya Bozhko <[email protected]>
This commit is contained in:
Yuliya Bozhko
2015-03-23 08:47:49 +13:00
committed by Yuliya
parent a149d6a177
commit 3784d3be98
14 changed files with 180 additions and 29 deletions
+59 -16
View File
@@ -88,9 +88,34 @@ $BADGE_CRITERIA_TYPES = array(
*/
abstract class award_criteria {
/**
* ID of the criterion.
* @var integer
*/
public $id;
/**
* Aggregation method [BADGE_CRITERIA_AGGREGATION_ANY, BADGE_CRITERIA_AGGREGATION_ALL].
* @var integer
*/
public $method;
/**
* ID of a badge this criterion belongs to.
* @var integer
*/
public $badgeid;
/**
* Criterion HTML/plain text description.
* @var string
*/
public $description;
/**
* Any additional parameters.
* @var array
*/
public $params = array();
/**
@@ -102,6 +127,7 @@ abstract class award_criteria {
$this->id = isset($params['id']) ? $params['id'] : 0;
$this->method = isset($params['method']) ? $params['method'] : BADGE_CRITERIA_AGGREGATION_ANY;
$this->badgeid = $params['badgeid'];
$this->description = isset($params['description']) ? $params['description'] : '';
if (isset($params['id'])) {
$this->params = $this->get_params($params['id']);
}
@@ -220,6 +246,10 @@ abstract class award_criteria {
}
echo $OUTPUT->heading($this->get_title() . $OUTPUT->help_icon('criteria_' . $this->criteriatype, 'badges'), 3, 'main help');
if (!empty($this->description)) {
echo $OUTPUT->box(clean_text($this->description, FORMAT_HTML), array('criteria-description'));
}
if (!empty($this->params)) {
if (count($this->params) > 1) {
echo $OUTPUT->box(get_string('criteria_descr_' . $this->criteriatype, 'badges',
@@ -305,23 +335,35 @@ abstract class award_criteria {
/**
* Saves intial criteria records with required parameters set up.
*
* @param array $params Values from the form or any other array.
*/
public function save($params = array()) {
global $DB;
// Figure out criteria description.
// If it is coming from the form editor, it is an array(text, format).
$description = '';
if (isset($params['description']['text'])) {
$description = $params['description']['text'];
} else if (isset($params['description'])) {
$description = $params['description'];
}
$fordb = new stdClass();
$fordb->criteriatype = $this->criteriatype;
$fordb->method = isset($params->agg) ? $params->agg : $params['agg'];
$fordb->method = isset($params['agg']) ? $params['agg'] : BADGE_CRITERIA_AGGREGATION_ALL;
$fordb->badgeid = $this->badgeid;
$fordb->description = $description;
$t = $DB->start_delegated_transaction();
// Unset unnecessary parameters supplied with form.
if (isset($params->agg)) {
unset($params->agg);
} else {
unset($params['agg']);
}
unset($params->submitbutton);
// Pick only params that are required by this criterion.
// Filter out empty values first.
$params = array_filter((array)$params);
// Find out which param matches optional and required ones.
$match = array_merge($this->optional_params, array($this->required_param));
$regex = implode('|', array_map(create_function('$a', 'return $a . "_";'), $match));
$requiredkeys = preg_grep('/^(' . $regex . ').*$/', array_keys($params));
if ($this->id !== 0) {
$cid = $this->id;
@@ -331,7 +373,7 @@ abstract class award_criteria {
$DB->update_record('badge_criteria', $fordb, true);
$existing = $DB->get_fieldset_select('badge_criteria_param', 'name', 'critid = ?', array($cid));
$todelete = array_diff($existing, array_keys($params));
$todelete = array_diff($existing, $requiredkeys);
if (!empty($todelete)) {
// A workaround to add some disabled elements that are still being submitted from the form.
@@ -349,32 +391,32 @@ abstract class award_criteria {
$DB->delete_records_select('badge_criteria_param', 'critid = :critid AND name ' . $sql, $sqlparams);
}
foreach ($params as $key => $value) {
foreach ($requiredkeys as $key) {
if (in_array($key, $existing)) {
$updp = $DB->get_record('badge_criteria_param', array('name' => $key, 'critid' => $cid));
$updp->value = $value;
$updp->value = $params[$key];
$DB->update_record('badge_criteria_param', $updp, true);
} else {
$newp = new stdClass();
$newp->critid = $cid;
$newp->name = $key;
$newp->value = $value;
$newp->value = $params[$key];
$DB->insert_record('badge_criteria_param', $newp);
}
}
} else {
$cid = $DB->insert_record('badge_criteria', $fordb, true);
if ($cid) {
foreach ($params as $key => $value) {
foreach ($requiredkeys as $key) {
$newp = new stdClass();
$newp->critid = $cid;
$newp->name = $key;
$newp->value = $value;
$newp->value = $params[$key];
$DB->insert_record('badge_criteria_param', $newp, false, true);
}
}
}
$t->allow_commit();
}
$t->allow_commit();
}
/**
@@ -387,6 +429,7 @@ abstract class award_criteria {
$fordb->criteriatype = $this->criteriatype;
$fordb->method = $this->method;
$fordb->badgeid = $newbadgeid;
$fordb->description = $this->description;
if (($newcrit = $DB->insert_record('badge_criteria', $fordb, true)) && isset($this->params)) {
foreach ($this->params as $k => $param) {
foreach ($param as $key => $value) {
@@ -75,6 +75,10 @@ class award_criteria_course extends award_criteria {
}
echo $OUTPUT->heading($this->get_title() . $OUTPUT->help_icon('criteria_' . $this->criteriatype, 'badges'), 3, 'main help');
if (!empty($this->description)) {
echo $OUTPUT->box(clean_text($this->description, FORMAT_HTML), array('criteria-description'));
}
if (!empty($this->params)) {
echo $OUTPUT->box(get_string('criteria_descr_' . $this->criteriatype, 'badges') . $this->get_details(), array('clearfix'));
}
+46 -1
View File
@@ -45,10 +45,24 @@ class award_criteria_overall extends award_criteria {
$prefix = 'criteria-' . $this->id;
if (count($data->criteria) > 2) {
echo $OUTPUT->box_start();
if (!empty($this->description)) {
echo $OUTPUT->box(clean_text($this->description, FORMAT_HTML), array('criteria-description'));
}
echo $OUTPUT->heading($this->get_title(), 2);
$agg = $data->get_aggregation_methods();
if (!$data->is_locked() && !$data->is_active()) {
$editurl = new moodle_url('/badges/criteria_settings.php',
array('badgeid' => $this->badgeid,
'edit' => true,
'type' => $this->criteriatype,
'crit' => $this->id
)
);
$editaction = $OUTPUT->action_icon($editurl, new pix_icon('t/edit', get_string('edit')), null,
array('class' => 'criteria-action'));
echo $OUTPUT->box($editaction, array('criteria-header'));
$url = new moodle_url('criteria.php', array('id' => $data->id, 'sesskey' => sesskey()));
$table = new html_table();
$table->attributes = array('class' => 'clearfix');
@@ -155,4 +169,35 @@ class award_criteria_overall extends award_criteria {
*/
public function get_params($cid) {
}
}
/**
* Saves overall badge criteria description.
*
* @param array $params Values from the form or any other array.
*/
public function save($params = array()) {
global $DB;
// Sort out criteria description.
// If it is coming from the form editor, it is an array of (text, format).
$description = '';
if (isset($params['description']['text'])) {
$description = $params['description']['text'];
} else if (isset($params['description'])) {
$description = $params['description'];
}
$fordb = new stdClass();
$fordb->criteriatype = $this->criteriatype;
$fordb->badgeid = $this->badgeid;
$fordb->description = $description;
if ($this->id !== 0) {
$fordb->id = $this->id;
$DB->update_record('badge_criteria', $fordb);
} else {
// New record in DB, set aggregation to ALL by default.
$fordb->method = BADGE_CRITERIA_AGGREGATION_ALL;
$DB->insert_record('badge_criteria', $fordb);
}
}
}
+3
View File
@@ -61,6 +61,9 @@ $PAGE->set_heading($badge->name);
$PAGE->set_title($badge->name);
if ($delete && has_capability('moodle/badges:configurecriteria', $context)) {
if ($type == BADGE_CRITERIA_TYPE_OVERALL) {
redirect($return, get_string('error:cannotdeletecriterion', 'badges'));
}
if (!$confirm) {
$optionsyes = array('confirm' => 1, 'sesskey' => sesskey(), 'badgeid' => $badgeid, 'delete' => true, 'type' => $type);
+7 -2
View File
@@ -55,6 +55,11 @@ class edit_criteria_form extends moodleform {
$mform->addElement('html', html_writer::tag('div', $message));
$mform->addElement('submit', 'cancel', get_string('continue'));
} else {
$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->closeHeaderBefore('buttonar');
$this->add_action_buttons(true, get_string('save', 'badges'));
}
@@ -69,7 +74,7 @@ class edit_criteria_form extends moodleform {
$errors = parent::validation($data, $files);
$addcourse = $this->_customdata['addcourse'];
if (!$addcourse) {
if (!$addcourse && isset($this->_customdata['criteria']->required_param)) {
$required = $this->_customdata['criteria']->required_param;
$pattern1 = '/^' . $required . '_(\d+)$/';
$pattern2 = '/^' . $required . '_(\w+)$/';
@@ -91,4 +96,4 @@ class edit_criteria_form extends moodleform {
}
return $errors;
}
}
}
+1 -1
View File
@@ -100,7 +100,7 @@ if (!empty($addcourse)) {
$criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $badge->id));
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
}
$criteria->save($data);
$criteria->save((array)$data);
$return->param('msg', $msg);
redirect($return);
}
+25 -7
View File
@@ -704,27 +704,45 @@ class core_badges_renderer extends plugin_renderer_base {
return null;
}
// Prints badge criteria.
/**
* Returns information about badge criteria in a list form.
*
* @param badge $badge Badge objects
* @param string $short Indicates whether to print full info about this badge
* @return string $output HTML string to output
*/
public function print_badge_criteria(badge $badge, $short = '') {
$output = "";
$agg = $badge->get_aggregation_methods();
if (empty($badge->criteria)) {
return get_string('nocriteria', 'badges');
} else if (count($badge->criteria) == 2) {
}
$overalldescr = '';
if (!$short) {
$overall = $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL];
$overalldescr .= $this->output->box(clean_text($overall->description, FORMAT_HTML));
}
if (count($badge->criteria) == 2) {
if (!$short) {
$output .= get_string('criteria_descr', 'badges');
$output .= $overalldescr . get_string('criteria_descr', 'badges');
}
} else {
$output .= get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
core_text::strtoupper($agg[$badge->get_aggregation_method()]));
$output .= $overalldescr . get_string('criteria_descr_' . $short . BADGE_CRITERIA_TYPE_OVERALL, 'badges',
core_text::strtoupper($agg[$badge->get_aggregation_method()]));
}
$items = array();
unset($badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]);
foreach ($badge->criteria as $type => $c) {
$criteriadescr = '';
if (!$short) {
$criteriadescr = $this->output->box(clean_text($c->description, FORMAT_HTML));
}
if (count($c->params) == 1) {
$items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') . $c->get_details($short);
$items[] = $criteriadescr . get_string('criteria_descr_single_' . $short . $type , 'badges') .
$c->get_details($short);
} else {
$items[] = get_string('criteria_descr_' . $short . $type , 'badges',
$items[] = $criteriadescr . get_string('criteria_descr_' . $short . $type , 'badges',
core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details($short);
}
}
+13
View File
@@ -152,6 +152,19 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
$this->assertCount(2, $badge->get_criteria());
}
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'));
$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');
$criteriaprofile->save($params);
$badge = new badge($this->badgeid);
$this->assertEquals('Overall description', $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL]->description);
$this->assertEquals('Description', $badge->criteria[BADGE_CRITERIA_TYPE_PROFILE]->description);
}
public function test_delete_badge_criteria() {
$criteria_overall = award_criteria::build(array('criteriatype' => BADGE_CRITERIA_TYPE_OVERALL, 'badgeid' => $this->badgeid));
$criteria_overall->save(array('agg' => BADGE_CRITERIA_AGGREGATION_ALL));
+1
View File
@@ -218,6 +218,7 @@ $string['error:backpackproblem'] = 'There was a problem connecting to your backp
$string['error:badjson'] = 'The connection attempt returned invalid data.';
$string['error:cannotact'] = 'Cannot activate the badge. ';
$string['error:cannotawardbadge'] = 'Cannot award badge to a user.';
$string['error:cannotdeletecriterion'] = 'This criterion cannot be deleted. ';
$string['error:connectionunknownreason'] = 'The connection was unsuccessful but no reason was given.';
$string['error:clone'] = 'Cannot clone the badge.';
$string['error:duplicatename'] = 'Badge with such name already exists in the system.';
+1
View File
@@ -2863,6 +2863,7 @@
<FIELD NAME="badgeid" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<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"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
+12
View File
@@ -4247,5 +4247,17 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2015031400.00);
}
if ($oldversion < 2015032000.00) {
$table = new xmldb_table('badge_criteria');
$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);
}
upgrade_main_savepoint(true, 2015032000.00);
}
return true;
}
@@ -1903,6 +1903,12 @@ a.criteria-action {
padding: 0px 3px;
float: right;
}
div.criteria-description {
padding: 10px 15px;
margin: 5px 0px;
background: none repeat scroll 0 0 #f9f9f9;
border: 1px solid #EEE;
}
ul.badges {
margin: 0;
list-style: none;
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2015031900.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2015032000.00; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.