Skip to content

Commit

Permalink
MDL-15680 Added grade category aggregation settings and weights, plus…
Browse files Browse the repository at this point in the history
… correct handling of 3 types of weights.
nicolasconnault committed Oct 10, 2008
1 parent fdcc3c5 commit 4dc81cc
Showing 10 changed files with 401 additions and 89 deletions.
6 changes: 2 additions & 4 deletions grade/edit/tree/item_form.php
Original file line number Diff line number Diff line change
@@ -136,15 +136,13 @@ function definition() {
$mform->addElement('header', 'headerparent', get_string('parentcategory', 'grades'));

$options = array();
$default = '';
$coefstring = '';
$categories = grade_category::fetch_all(array('courseid'=>$COURSE->id));

foreach ($categories as $cat) {
$cat->apply_forced_settings();
$options[$cat->id] = $cat->get_name();
if ($cat->is_course_category()) {
$default = $cat->id;
}

if ($cat->is_aggregationcoef_used()) {
if ($cat->aggregation == GRADE_AGGREGATE_WEIGHTED_MEAN) {
$coefstring = ($coefstring=='' or $coefstring=='aggregationcoefweight') ? 'aggregationcoefweight' : 'aggregationcoef';
51 changes: 31 additions & 20 deletions grade/edit/weights/ajax.php
Original file line number Diff line number Diff line change
@@ -22,36 +22,47 @@
}

if (element.item.table == 'grade_categories') {
var tmpNode = new YAHOO.widget.TextNode(element.item.name, parentNode, parentNode.isRoot());
var aggregation_types = '<select name="aggregation_'+element.item.id+'">';

for (var i = 0; i < gew.options.length; i++) {
aggregation_types += '<option>' + gew.options[i] + '</option>';
}

var gradecategory = '<span class="gradecategory">'+element.item.name+' '+aggregation_types+'<'+'/span>';
var tmpNode = new YAHOO.widget.TextNode(gradecategory, parentNode, parentNode.isRoot());
// var tmpNode = new YAHOO.widget.TextNode(element.item.name, parentNode, parentNode.isRoot());
for (var i = 0; i < element.children.length; i++) {
gew.buildTreeNode(element.children[i], tmpNode, false);
}
} else if (element.item.itemtype == 'mod') {
var tmpNode = new YAHOO.widget.TextNode(element.item.name, parentNode, false);
var gradeitem = '<span class="gradeitem"><input type="text" size="6" name="weight_'+element.item.id+'" value="'+element.item.aggregationcoef+'"/>'+
element.item.name + '<'+'/span>';
var tmpNode = new YAHOO.widget.HTMLNode(gradeitem, parentNode, false);
}
};

YAHOO.grade_edit_weights.init = function() {
var gew = YAHOO.grade_edit_weights;
var div = document.getElementById('weightstree');
gew.tree = new YAHOO.widget.TreeView('weightstree');

//handler for expanding all nodes
YAHOO.util.Event.on("expand", "click", function(e) {
YAHOO.log("Expanding all TreeView nodes.", "info", "example");
gew.tree.expandAll();
YAHOO.util.Event.preventDefault(e);
});

//handler for collapsing all nodes
YAHOO.util.Event.on("collapse", "click", function(e) {
YAHOO.log("Collapsing all TreeView nodes.", "info", "example");
gew.tree.collapseAll();
YAHOO.util.Event.preventDefault(e);
});

gew.buildTreeNode();
gew.tree.draw();
gew.tree = new YAHOO.widget.TreeView('gradetree');

//handler for expanding all nodes
YAHOO.util.Event.on("expand", "click", function(e) {
YAHOO.log("Expanding all TreeView nodes.", "info", "example");
gew.tree.expandAll();
YAHOO.util.Event.preventDefault(e);
});

//handler for collapsing all nodes
YAHOO.util.Event.on("collapse", "click", function(e) {
YAHOO.log("Collapsing all TreeView nodes.", "info", "example");
gew.tree.collapseAll();
YAHOO.util.Event.preventDefault(e);
});

// gew.buildTreeNode();
gew.tree.render();
gew.tree.expandAll();
};

YAHOO.util.Event.onDOMReady(YAHOO.grade_edit_weights.init);
125 changes: 89 additions & 36 deletions grade/edit/weights/index.php
Original file line number Diff line number Diff line change
@@ -26,10 +26,13 @@
require_once '../../../config.php';
require_once $CFG->dirroot.'/grade/lib.php';
require_once $CFG->dirroot.'/grade/report/lib.php'; // for preferences
require_once $CFG->dirroot.'/grade/edit/weights/lib.php';

$courseid = required_param('id', PARAM_INT);
$action = optional_param('action', 0, PARAM_ALPHA);
$eid = optional_param('eid', 0, PARAM_ALPHANUM);
$courseid = required_param('id', PARAM_INT);
$action = optional_param('action', 0, PARAM_ALPHA);
$eid = optional_param('eid', 0, PARAM_ALPHANUM);
$category = optional_param('category', null, PARAM_INT);
$aggregationtype = optional_param('aggregationtype', null, PARAM_INT);

require_js(array('yui_yahoo', 'yui_dom', 'yui_event', 'yui_json', 'yui_connection', 'yui_dragdrop', 'yui_treeview'));

@@ -44,9 +47,19 @@
require_capability('moodle/grade:manage', $context);

/// return tracking object
$gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'weight', 'courseid'=>$courseid));
$gpr = new grade_plugin_return(array('type'=>'edit', 'plugin'=>'weights', 'courseid'=>$courseid));
$returnurl = $gpr->get_return_url(null);

// Change category aggregation if requested
if (!is_null($category) && !is_null($aggregationtype)) {
if (!$grade_category = grade_category::fetch(array('id'=>$category, 'courseid'=>$courseid))) {
error('Incorrect category id!');
}
$data->aggregation = $aggregationtype;
grade_category::set_properties($grade_category, $data);
$grade_category->update();
}

//first make sure we have proper final grades - we need it for locking changes
grade_regrade_final_grades($courseid);

@@ -64,57 +77,97 @@
$navigation = grade_build_nav(__FILE__, $strcategoriesanditems, array('courseid' => $courseid));
$moving = false;

switch ($action) {
default:
break;
}

$CFG->stylesheets[] = $CFG->wwwroot.'/grade/edit/weights/tree.css';
print_header_simple($strgrades . ': ' . $strgraderreport, ': ' . $strcategoriesedit, $navigation, '', '', true, '', navmenu($course));

/// Print the plugin selector at the top
print_grade_plugin_selector($courseid, 'edit', 'tree');

print_heading(get_string('categoriesedit', 'grades'));
print_heading(get_string('weightsedit', 'grades'));
$tree_json = json_encode(get_tree_json($gtree, $gtree->top_element));

require_once('ajax.php');
$form_key = optional_param('sesskey', null, PARAM_ALPHANUM);

if ($form_key) {
$data = data_submitted();

foreach ($data as $key => $value) {
if (preg_match('/aggregation_type_([0-9]*)/', $key, $matches)) {
$aggtype = required_param($matches[0], PARAM_INT);
$a->id = $matches[1];

if (!$DB->update_record('grade_categories', array('id' => $matches[1], 'aggregation' => $aggtype))) {
print_error('errorupdatinggradecategoryaggregation', 'grades', $a);
}

} elseif (preg_match('/weight_([0-9]*)/', $key, $matches)) {
$weight = required_param($matches[0], PARAM_NUMBER);
$a->id = $matches[1];

if (!$DB->update_record('grade_items', array('id' => $matches[1], 'aggregationcoef' => $weight))) {
print_error('errorupdatinggradeitemaggregationcoef', 'grades', $a);
}

} elseif (preg_match('/extracredit_original_([0-9]*)/', $key, $matches)) { // Sum extra credit checkbox
$extracredit = optional_param("extracredit_{$matches[1]}", null, PARAM_BOOL);
$original_value = required_param($matches[0], PARAM_BOOL);
$a->id = $matches[1];
$newvalue = null;

if ($original_value == 1 && is_null($extracredit)) {
$newvalue = 0;
} elseif ($original_value == 0 && $extracredit == 1) {
$newvalue = 1;
} else {
continue;
}

if (!$DB->update_record('grade_items', array('id' => $matches[1], 'aggregationcoef' => $newvalue))) {
print_error('errorupdatinggradeitemaggregationcoef', 'grades', $a);
}

} elseif (preg_match('/aggregate(onlygraded|subcats|outcomes)_original_([0-9]*)/', $key, $matches)) {
$setting = optional_param('aggregate'.$matches[1].'_'.$matches[2], null, PARAM_BOOL);
$original_value = required_param($matches[0], PARAM_BOOL);
$a->id = $matches[2];

$newvalue = null;
if ($original_value == 1 && is_null($setting)) {
$newvalue = 0;
} elseif ($original_value == 0 && $setting == 1) {
$newvalue = 1;
} else {
continue;
}

if (!$DB->update_record('grade_categories', array('id' => $matches[2], 'aggregate'.$matches[1] => $newvalue))) {
print_error('errorupdatinggradecategoryaggregate'.$matches[1], 'grades', $a);
}
}
}
}

// AJAX interface not really needed: adds nice tree functions but not very useful
// require_once('ajax.php');

print_box_start('gradetreebox generalbox');

echo '<div id="expandcontractdiv">
<a id="expand" href="#">Expand all</a>
<a id="collapse" href="#">Collapse all</a>
</div> ';
// echo '<div id="expandcontractdiv"> <a id="expand" href="#">Expand all</a> <a id="collapse" href="#">Collapse all</a> </div> ';

echo '<form method="post" action="'.$returnurl.'">';
echo '<div id="weightstree">';
echo '<input type="hidden" name="sesskey" value="'.sesskey().'" />';
echo '<div id="gradetree">';
echo build_html_tree(get_tree_json($gtree, $gtree->top_element), null, $returnurl);

// print_grade_tree($gtree, $gtree->top_element, $gpr, $switch);
//
echo '</div>';
echo '</form>';
echo '</div><div>';

echo '<input type="submit" value="Update weights" />';
echo '</div></form>';
print_box_end();

print_footer($course);
die;

function get_tree_json(&$gtree, $element) {

$return_array = array();

$object = $element['object'];
$eid = $element['eid'];
$object->name = $gtree->get_element_header($element, false, false, false);

$return_array['item'] = $object;

if ($element['type'] == 'category') {
foreach($element['children'] as $child_el) {
$return_array['children'][] = get_tree_json($gtree, $child_el);
}
}

return $return_array;
}
?>
Loading

0 comments on commit 4dc81cc

Please sign in to comment.