MDL-12773, MDL-12144 grade category selection on modedit page
This commit is contained in:
+63
-58
@@ -112,14 +112,32 @@
|
||||
$form->return = $return;
|
||||
$form->update = $update;
|
||||
|
||||
// add existing outcomes
|
||||
if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$form->modulename,
|
||||
'iteminstance'=>$form->instance, 'courseid'=>$COURSE->id))) {
|
||||
// add existing outcomes
|
||||
foreach ($items as $item) {
|
||||
if (!empty($item->outcomeid)) {
|
||||
$form->{'outcome_'.$item->outcomeid} = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// set category if present
|
||||
$gradecat = false;
|
||||
foreach ($items as $item) {
|
||||
if ($gradecat === false) {
|
||||
$gradecat = $item->categoryid;
|
||||
continue;
|
||||
}
|
||||
if ($gradecat != $item->categoryid) {
|
||||
//mixed categories
|
||||
$gradecat = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ($gradecat !== false) {
|
||||
// do not set if mixed categories present
|
||||
$form->gradecat = $gradecat;
|
||||
}
|
||||
}
|
||||
|
||||
$sectionname = get_section_name($course->format);
|
||||
@@ -300,7 +318,7 @@
|
||||
error("Data submitted is invalid.");
|
||||
}
|
||||
|
||||
//sync idnumber with grade_item
|
||||
// sync idnumber with grade_item
|
||||
if ($grade_item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
|
||||
'iteminstance'=>$fromform->instance, 'itemnumber'=>0, 'courseid'=>$COURSE->id))) {
|
||||
if ($grade_item->idnumber != $fromform->cmidnumber) {
|
||||
@@ -309,6 +327,31 @@
|
||||
}
|
||||
}
|
||||
|
||||
$items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
|
||||
'iteminstance'=>$fromform->instance, 'courseid'=>$COURSE->id));
|
||||
|
||||
// create parent category if requested and move to correct parent category
|
||||
if ($items and isset($fromform->gradecat)) {
|
||||
if ($fromform->gradecat == -1) {
|
||||
$grade_category = new grade_category();
|
||||
$grade_category->courseid = $COURSE->id;
|
||||
$grade_category->fullname = stripslashes($fromform->name);
|
||||
$grade_category->insert();
|
||||
if ($grade_item) {
|
||||
$parent = $grade_item->get_parent_category();
|
||||
$grade_category->set_parent($parent->id);
|
||||
}
|
||||
$fromform->gradecat = $grade_category->id;
|
||||
}
|
||||
foreach ($items as $itemid=>$unused) {
|
||||
$items[$itemid]->set_parent($fromform->gradecat);
|
||||
if ($item->id == $grade_item->id) {
|
||||
// use updated grade_item
|
||||
$grade_item = $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// add outcomes if requested
|
||||
if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
|
||||
$grade_items = array();
|
||||
@@ -318,12 +361,9 @@
|
||||
|
||||
if (array_key_exists($elname, $fromform) and $fromform->$elname) {
|
||||
// we have a request for new outcome grade item
|
||||
$grade_item = new grade_item();
|
||||
|
||||
// Outcome grade_item.itemnumber start at 1000
|
||||
$max_itemnumber = 999;
|
||||
if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$fromform->modulename,
|
||||
'iteminstance'=>$fromform->instance, 'courseid'=>$COURSE->id))) {
|
||||
if ($items) {
|
||||
$exists = false;
|
||||
foreach($items as $item) {
|
||||
if ($item->outcomeid == $outcome->id) {
|
||||
@@ -341,61 +381,26 @@
|
||||
if ($exists) {
|
||||
continue;
|
||||
}
|
||||
$grade_item->courseid = $COURSE->id;
|
||||
$grade_item->itemtype = 'mod';
|
||||
$grade_item->itemmodule = $fromform->modulename;
|
||||
$grade_item->iteminstance = $fromform->instance;
|
||||
$grade_item->itemnumber = $max_itemnumber + 1;
|
||||
$grade_item->itemname = $outcome->fullname;
|
||||
$grade_item->outcomeid = $outcome->id;
|
||||
$grade_item->gradetype = GRADE_TYPE_SCALE;
|
||||
$grade_item->scaleid = $outcome->scaleid;
|
||||
|
||||
$grade_item->insert();
|
||||
$outcome_item = new grade_item();
|
||||
$outcome_item->courseid = $COURSE->id;
|
||||
$outcome_item->itemtype = 'mod';
|
||||
$outcome_item->itemmodule = $fromform->modulename;
|
||||
$outcome_item->iteminstance = $fromform->instance;
|
||||
$outcome_item->itemnumber = $max_itemnumber + 1;
|
||||
$outcome_item->itemname = $outcome->fullname;
|
||||
$outcome_item->outcomeid = $outcome->id;
|
||||
$outcome_item->gradetype = GRADE_TYPE_SCALE;
|
||||
$outcome_item->scaleid = $outcome->scaleid;
|
||||
$outcome_item->insert();
|
||||
|
||||
// TODO comment on these next 4 lines
|
||||
if ($item = grade_item::fetch(array('itemtype'=>'mod', 'itemmodule'=>$grade_item->itemmodule,
|
||||
'iteminstance'=>$grade_item->iteminstance, 'itemnumber'=>0, 'courseid'=>$COURSE->id))) {
|
||||
$grade_item->set_parent($item->categoryid);
|
||||
$grade_item->move_after_sortorder($item->sortorder);
|
||||
}
|
||||
$grade_items[] = $grade_item;
|
||||
}
|
||||
}
|
||||
// move the new outcome into correct category and fix sortorder if needed
|
||||
if ($grade_item) {
|
||||
$outcome_item->set_parent($grade_item->categoryid);
|
||||
$outcome_item->move_after_sortorder($grade_item->sortorder);
|
||||
|
||||
// Create a grade_category to represent this module, if outcomes have been attached
|
||||
if (!empty($grade_items)) {
|
||||
// Add the module's normal grade_item as a child of this category
|
||||
$item_params = array('itemtype'=>'mod',
|
||||
'itemmodule'=>$fromform->modulename,
|
||||
'iteminstance'=>$fromform->instance,
|
||||
'itemnumber'=>0,
|
||||
'courseid'=>$COURSE->id);
|
||||
$item = grade_item::fetch($item_params);
|
||||
|
||||
// Only create the category if it will contain at least 2 items
|
||||
if ($item OR count($grade_items) > 1) { // If we are here it means there is at least 1 outcome
|
||||
$cat_params = array('courseid'=>$COURSE->id, 'fullname'=>$fromform->name);
|
||||
$grade_category = grade_category::fetch($cat_params);
|
||||
|
||||
if (!$grade_category) {
|
||||
$grade_category = new grade_category($cat_params);
|
||||
$grade_category->courseid = $COURSE->id;
|
||||
$grade_category->fullname = $fromform->name;
|
||||
$grade_category->insert();
|
||||
}
|
||||
|
||||
$sortorder = $grade_category->sortorder;
|
||||
|
||||
if ($item) {
|
||||
$item->set_parent($grade_category->id);
|
||||
$sortorder = $item->sortorder;
|
||||
}
|
||||
|
||||
// Add the outcomes as children of this category
|
||||
foreach ($grade_items as $gi) {
|
||||
$gi->set_parent($grade_category->id);
|
||||
$gi->move_after_sortorder($sortorder);
|
||||
} else if (isset($fromform->gradecat)) {
|
||||
$outcome_item->set_parent($fromform->gradecat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+73
-23
@@ -28,6 +28,10 @@ class moodleform_mod extends moodleform {
|
||||
* @var mixed
|
||||
*/
|
||||
var $_cm;
|
||||
/**
|
||||
* List of modform features
|
||||
*/
|
||||
var $_features;
|
||||
|
||||
function moodleform_mod($instance, $section, $cm) {
|
||||
$this->_instance = $instance;
|
||||
@@ -44,23 +48,53 @@ class moodleform_mod extends moodleform {
|
||||
function data_preprocessing(&$default_values){
|
||||
}
|
||||
|
||||
/**
|
||||
* Each module which defines definition_after_data() must call this method using parent::definition_after_data();
|
||||
*/
|
||||
function definition_after_data() {
|
||||
global $COURSE;
|
||||
global $CFG, $COURSE;
|
||||
$mform =& $this->_form;
|
||||
|
||||
if ($id = $mform->getElementValue('update')) {
|
||||
$modulename = $mform->getElementValue('modulename');
|
||||
$instance = $mform->getElementValue('instance');
|
||||
|
||||
if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename,
|
||||
'iteminstance'=>$instance, 'courseid'=>$COURSE->id))) {
|
||||
foreach ($items as $item) {
|
||||
if (!empty($item->outcomeid)) {
|
||||
$elname = 'outcome_'.$item->outcomeid;
|
||||
if ($mform->elementExists($elname)) {
|
||||
$mform->hardFreeze($elname); // prevent removing of existing outcomes
|
||||
if ($this->_features->gradecat) {
|
||||
$gradecat = false;
|
||||
if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
|
||||
if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
|
||||
$gradecat = true;
|
||||
}
|
||||
}
|
||||
if ($items = grade_item::fetch_all(array('itemtype'=>'mod', 'itemmodule'=>$modulename,
|
||||
'iteminstance'=>$instance, 'courseid'=>$COURSE->id))) {
|
||||
foreach ($items as $item) {
|
||||
if (!empty($item->outcomeid)) {
|
||||
$elname = 'outcome_'.$item->outcomeid;
|
||||
if ($mform->elementExists($elname)) {
|
||||
$mform->hardFreeze($elname); // prevent removing of existing outcomes
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($items as $item) {
|
||||
if (is_bool($gradecat)) {
|
||||
$gradecat = $item->categoryid;
|
||||
continue;
|
||||
}
|
||||
if ($gradecat != $item->categoryid) {
|
||||
//mixed categories
|
||||
$gradecat = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($gradecat === false) {
|
||||
// items and outcomes in different categories - remove the option
|
||||
// TODO: it might be better to add a "Mixed categories" text instead
|
||||
if ($mform->elementExists('gradecat')) {
|
||||
$mform->removeElement('gradecat');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -76,7 +110,7 @@ class moodleform_mod extends moodleform {
|
||||
|
||||
} else if (!$mform->elementExists('groupmode') and $mform->elementExists('groupmembersonly')) {
|
||||
$mform->disabledIf('groupingid', 'groupmembersonly', 'notchecked');
|
||||
|
||||
|
||||
} else if (!$mform->elementExists('groupmode') and !$mform->elementExists('groupmembersonly')) {
|
||||
// groupings have no use without groupmode or groupmembersonly
|
||||
if ($mform->elementExists('groupingid')) {
|
||||
@@ -146,29 +180,40 @@ class moodleform_mod extends moodleform {
|
||||
// deal with legacy $supportgroups param
|
||||
if ($features === true or $features === false) {
|
||||
$groupmode = $features;
|
||||
$features = new object();
|
||||
$features->groups = $groupmode;
|
||||
$this->_features = new object();
|
||||
$this->_features->groups = $groupmode;
|
||||
|
||||
} else if (is_array($features)) {
|
||||
$features = (object)$features;
|
||||
$this->_features = (object)$features;
|
||||
|
||||
} else if (empty($features)) {
|
||||
$features = new object();
|
||||
$this->_features = new object();
|
||||
|
||||
} else {
|
||||
$this->_features = $features;
|
||||
}
|
||||
|
||||
if (!isset($features->groups)) {
|
||||
$features->groups = true;
|
||||
if (!isset($this->_features->groups)) {
|
||||
$this->_features->groups = true;
|
||||
}
|
||||
|
||||
if (!isset($features->groupings)) {
|
||||
$features->groupings = false;
|
||||
if (!isset($this->_features->groupings)) {
|
||||
$this->_features->groupings = false;
|
||||
}
|
||||
|
||||
if (!isset($features->groupmembersonly)) {
|
||||
$features->groupmembersonly = false;
|
||||
if (!isset($this->_features->groupmembersonly)) {
|
||||
$this->_features->groupmembersonly = false;
|
||||
}
|
||||
|
||||
if (!empty($CFG->enableoutcomes)) {
|
||||
if (!isset($this->_features->outcomes)) {
|
||||
$this->_features->outcomes = true;
|
||||
}
|
||||
|
||||
if (!isset($this->_features->gradecat)) {
|
||||
$this->_features->gradecat = true;
|
||||
}
|
||||
|
||||
if (!empty($CFG->enableoutcomes) and $this->_features->outcomes) {
|
||||
if ($outcomes = grade_outcome::fetch_all_available($COURSE->id)) {
|
||||
$mform->addElement('header', 'modoutcomes', get_string('outcomes', 'grades'));
|
||||
foreach($outcomes as $outcome) {
|
||||
@@ -178,7 +223,7 @@ class moodleform_mod extends moodleform {
|
||||
}
|
||||
|
||||
$mform->addElement('header', 'modstandardelshdr', get_string('modstandardels', 'form'));
|
||||
if ($features->groups) {
|
||||
if ($this->_features->groups) {
|
||||
$options = array(NOGROUPS => get_string('groupsnone'),
|
||||
SEPARATEGROUPS => get_string('groupsseparate'),
|
||||
VISIBLEGROUPS => get_string('groupsvisible'));
|
||||
@@ -187,7 +232,7 @@ class moodleform_mod extends moodleform {
|
||||
}
|
||||
|
||||
if (!empty($CFG->enablegroupings)) {
|
||||
if ($features->groupings or $features->groupmembersonly) {
|
||||
if ($this->_features->groupings or $this->_features->groupmembersonly) {
|
||||
//groupings selector - used for normal grouping mode or also when restricting access with groupmembersonly
|
||||
$options = array();
|
||||
$options[0] = get_string('none');
|
||||
@@ -200,7 +245,7 @@ class moodleform_mod extends moodleform {
|
||||
$mform->setAdvanced('groupingid');
|
||||
}
|
||||
|
||||
if ($features->groupmembersonly) {
|
||||
if ($this->_features->groupmembersonly) {
|
||||
$mform->addElement('checkbox', 'groupmembersonly', get_string('groupmembersonly', 'group'));
|
||||
$mform->setAdvanced('groupmembersonly');
|
||||
}
|
||||
@@ -209,6 +254,11 @@ class moodleform_mod extends moodleform {
|
||||
$mform->addElement('modvisible', 'visible', get_string('visible'));
|
||||
$mform->addElement('text', 'cmidnumber', get_string('idnumber'));
|
||||
|
||||
if ($this->_features->gradecat) {
|
||||
$categories = grade_get_categories_menu($COURSE->id, true);
|
||||
$mform->addElement('select', 'gradecat', get_string('gradecategory', 'grades'), $categories);
|
||||
}
|
||||
|
||||
$this->standard_hidden_coursemodule_elements();
|
||||
}
|
||||
|
||||
|
||||
@@ -619,6 +619,40 @@ function grade_format_gradevalue($value, &$grade_item, $localized=true, $display
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns grade options for gradebook category menu
|
||||
* @param int $courseid
|
||||
* @param bool $includenew include option for new category (-1)
|
||||
* @return array of grade categories in course
|
||||
*/
|
||||
function grade_get_categories_menu($courseid, $includenew=false) {
|
||||
$result = array();
|
||||
if (!$categories = grade_category::fetch_all(array('courseid'=>$courseid))) {
|
||||
//make sure course category exists
|
||||
if (!grade_category::fetch_course_category()) {
|
||||
debugging('Can not create course grade category!');
|
||||
return $result;
|
||||
}
|
||||
$categories = grade_category::fetch_all(array('courseid'=>$courseid));
|
||||
}
|
||||
foreach ($categories as $key=>$category) {
|
||||
if ($category->is_course_category()) {
|
||||
$result[$category->id] = get_string('uncategorised', 'grades');
|
||||
unset($categories[$key]);
|
||||
}
|
||||
}
|
||||
if ($includenew) {
|
||||
$result[-1] = get_string('newcategory', 'grades');
|
||||
}
|
||||
$cats = array();
|
||||
foreach ($categories as $category) {
|
||||
$cats[$category->id] = $category->get_name();
|
||||
}
|
||||
asort($cats, SORT_LOCALE_STRING);
|
||||
|
||||
return ($result+$cats);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns grade letters array used in context
|
||||
* @param object $context object or null for defaults
|
||||
|
||||
@@ -43,7 +43,7 @@ class mod_resource_mod_form extends moodleform_mod {
|
||||
$mform->addElement('header', 'typedesc', get_string('resourcetype'.$type,'resource'));
|
||||
$this->_resinstance->setup_elements($mform);
|
||||
|
||||
$this->standard_coursemodule_elements(array('groups'=>false, 'groupmembersonly'=>true));
|
||||
$this->standard_coursemodule_elements(array('groups'=>false, 'groupmembersonly'=>true, 'gradecat'=>false));
|
||||
|
||||
$this->add_action_buttons();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user