1
0
mirror of https://github.com/moodle/moodle.git synced 2025-05-11 02:36:05 +02:00
This commit is contained in:
Eloy Lafuente (stronk7) 2015-03-25 01:32:34 +01:00
commit 89267d6a51
18 changed files with 274 additions and 39 deletions

@ -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',

@ -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);

@ -88,9 +88,40 @@ $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;
/**
* Format of the criterion description.
* @var integer
*/
public $descriptionformat;
/**
* Any additional parameters.
* @var array
*/
public $params = array();
/**
@ -102,6 +133,8 @@ 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'] : '';
$this->descriptionformat = isset($params['descriptionformat']) ? $params['descriptionformat'] : FORMAT_HTML;
if (isset($params['id'])) {
$this->params = $this->get_params($params['id']);
}
@ -220,6 +253,14 @@ abstract class award_criteria {
}
echo $OUTPUT->heading($this->get_title() . $OUTPUT->help_icon('criteria_' . $this->criteriatype, 'badges'), 3, 'main help');
if (!empty($this->description)) {
$badge = new badge($this->badgeid);
echo $OUTPUT->box(
format_text($this->description, $this->descriptionformat, array('context' => $badge->get_context())),
'criteria-description'
);
}
if (!empty($this->params)) {
if (count($this->params) > 1) {
echo $OUTPUT->box(get_string('criteria_descr_' . $this->criteriatype, 'badges',
@ -305,23 +346,38 @@ 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 = '';
$descriptionformat = FORMAT_HTML;
if (isset($params['description']['text'])) {
$description = $params['description']['text'];
$descriptionformat = $params['description']['format'];
} 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;
$fordb->descriptionformat = $descriptionformat;
$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 +387,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 +405,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 +443,8 @@ abstract class award_criteria {
$fordb->criteriatype = $this->criteriatype;
$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) {

@ -75,6 +75,15 @@ 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(
format_text($this->description, $this->descriptionformat,
array('context' => context_course::instance($this->courseid))
),
'criteria-description'
);
}
if (!empty($this->params)) {
echo $OUTPUT->box(get_string('criteria_descr_' . $this->criteriatype, 'badges') . $this->get_details(), array('clearfix'));
}

@ -45,10 +45,27 @@ class award_criteria_overall extends award_criteria {
$prefix = 'criteria-' . $this->id;
if (count($data->criteria) > 2) {
echo $OUTPUT->box_start();
if (!empty($this->description)) {
$badge = new badge($this->badgeid);
echo $OUTPUT->box(
format_text($this->description, $this->descriptionformat, array('context' => $badge->get_context())),
'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 +172,38 @@ 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 = '';
$descriptionformat = FORMAT_HTML;
if (isset($params['description']['text'])) {
$description = $params['description']['text'];
$descriptionformat = $params['description']['format'];
} else if (isset($params['description'])) {
$description = $params['description'];
}
$fordb = new stdClass();
$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);
} else {
// New record in DB, set aggregation to ALL by default.
$fordb->method = BADGE_CRITERIA_AGGREGATION_ALL;
$DB->insert_record('badge_criteria', $fordb);
}
}
}

@ -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);

@ -55,6 +55,15 @@ 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,
'format' => $criteria->descriptionformat
)
);
$mform->closeHeaderBefore('buttonar');
$this->add_action_buttons(true, get_string('save', 'badges'));
}
@ -69,7 +78,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 +100,4 @@ class edit_criteria_form extends moodleform {
}
return $errors;
}
}
}

@ -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);
}

@ -704,32 +704,81 @@ 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 = '';
$overall = $badge->criteria[BADGE_CRITERIA_TYPE_OVERALL];
if (!$short && !empty($overall->description)) {
$overalldescr = $this->output->box(
format_text($overall->description, $overall->descriptionformat, array('context' => $badge->get_context())),
'criteria-description'
);
}
// Get the condition string.
if (count($badge->criteria) == 2) {
$condition = '';
if (!$short) {
$output .= get_string('criteria_descr', 'badges');
$condition = 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()]));
$condition = 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) {
$items = array();
// If only one criterion left, make sure its description goe to the top.
if (count($badge->criteria) == 1) {
$c = reset($badge->criteria);
if (!$short && !empty($c->description)) {
$overalldescr = $this->output->box(
format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
'criteria-description'
);
}
if (count($c->params) == 1) {
$items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') . $c->get_details($short);
$items[] = get_string('criteria_descr_single_' . $short . $c->criteriatype , 'badges') .
$c->get_details($short);
} else {
$items[] = get_string('criteria_descr_' . $short . $type , 'badges',
core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) . $c->get_details($short);
$items[] = get_string('criteria_descr_' . $short . $c->criteriatype, 'badges',
core_text::strtoupper($agg[$badge->get_aggregation_method($c->criteriatype)])) .
$c->get_details($short);
}
} else {
foreach ($badge->criteria as $type => $c) {
$criteriadescr = '';
if (!$short && !empty($c->description)) {
$criteriadescr = $this->output->box(
format_text($c->description, $c->descriptionformat, array('context' => $badge->get_context())),
'criteria-description'
);
}
if (count($c->params) == 1) {
$items[] = get_string('criteria_descr_single_' . $short . $type , 'badges') .
$c->get_details($short) . $criteriadescr;
} else {
$items[] = get_string('criteria_descr_' . $short . $type , 'badges',
core_text::strtoupper($agg[$badge->get_aggregation_method($type)])) .
$c->get_details($short) .
$criteriadescr;
}
}
}
$output .= html_writer::alist($items, array(), 'ul');
return $output;
return $overalldescr . $condition . html_writer::alist($items, array(), 'ul');;
}
// Prints criteria actions for badge editing.

@ -152,6 +152,28 @@ 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',
'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',
'descriptionformat' => FORMAT_HTML
);
$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));

@ -16,13 +16,17 @@ Feature: Award badges
And I upload "badges/tests/behat/badge.png" file to "Image" filemanager
And I press "Create badge"
And I set the field "type" to "Profile completion"
And I expand all fieldsets
And I set the field "First name" to "1"
And I set the field "Email address" to "1"
And I set the field "Phone" to "1"
And I set the field "id_description" to "Criterion description"
When I press "Save"
Then I should see "Profile completion"
And I should see "First name"
And I should see "Email address"
And I should see "Phone"
And I should see "Criterion description"
And I should not see "Criteria for this badge have not been set up yet."
And I press "Enable access"
And I press "Continue"

@ -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.';

@ -1982,6 +1982,7 @@ $capabilities = array(
// Set up/edit criteria of earning a badge.
'moodle/badges:configurecriteria' => array(
'riskbitmask' => RISK_XSS,
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(

@ -2863,6 +2863,8 @@
<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"/>
<FIELD NAME="descriptionformat" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>

@ -4252,5 +4252,23 @@ function xmldb_main_upgrade($oldversion) {
upgrade_main_savepoint(true, 2015031900.01);
}
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);
}
$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);
}
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

@ -29,7 +29,7 @@
defined('MOODLE_INTERNAL') || die();
$version = 2015031900.01; // 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.