Skip to content

Commit

Permalink
MDL-47542 core_grades: action menu for grade setup
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy authored and stronk7 committed Oct 24, 2014
1 parent c785cc9 commit 079b2e5
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 78 deletions.
31 changes: 22 additions & 9 deletions grade/edit/tree/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function __construct($gtree, $moving=false, $gpr) {
* @return string HTML
*/
public function build_html_tree($element, $totals, $parents, $level, &$row_count) {
global $CFG, $COURSE, $USER, $OUTPUT;
global $CFG, $COURSE, $PAGE, $OUTPUT;

$object = $element['object'];
$eid = $element['eid'];
Expand All @@ -126,30 +126,41 @@ public function build_html_tree($element, $totals, $parents, $level, &$row_count
$rowclasses[] = $parent_eid;
}

$actions = '';
$moveaction = '';
$actionsmenu = new action_menu();
$actionsmenu->initialise_js($PAGE);
$actionsmenu->set_menu_trigger(get_string('edit'));
$actionsmenu->set_owner_selector('grade-item-' . $eid);
$actionsmenu->set_alignment(action_menu::TL, action_menu::BL);

if (!$is_category_item) {
$actions .= $this->gtree->get_edit_icon($element, $this->gpr);
if (!$is_category_item && ($icon = $this->gtree->get_edit_icon($element, $this->gpr, true))) {
$actionsmenu->add($icon);
}

if ($this->show_calculations) {
$actions .= $this->gtree->get_calculation_icon($element, $this->gpr);
if ($this->show_calculations && ($icon = $this->gtree->get_calculation_icon($element, $this->gpr, true))) {
$actionsmenu->add($icon);
}

if ($element['type'] == 'item' or ($element['type'] == 'category' and $element['depth'] > 1)) {
if ($this->element_deletable($element)) {
$aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'delete', 'eid' => $eid, 'sesskey' => sesskey()));
$actions .= $OUTPUT->action_icon($aurl, new pix_icon('t/delete', get_string('delete')));
$icon = new action_menu_link_secondary($aurl, new pix_icon('t/delete', get_string('delete')), get_string('delete'));
$actionsmenu->add($icon);
}

$aurl = new moodle_url('index.php', array('id' => $COURSE->id, 'action' => 'moveselect', 'eid' => $eid, 'sesskey' => sesskey()));
$moveaction .= $OUTPUT->action_icon($aurl, new pix_icon('t/move', get_string('move')));
}

$actions .= $this->gtree->get_hiding_icon($element, $this->gpr);
if ($icon = $this->gtree->get_hiding_icon($element, $this->gpr, true)) {
$actionsmenu->add($icon);
}

if ($icon = $this->gtree->get_reset_icon($element, $this->gpr, true)) {
$actionsmenu->add($icon);
}

$actions .= $this->gtree->get_reset_icon($element, $this->gpr);
$actions = $OUTPUT->render($actionsmenu);

$returnrows = array();
$root = false;
Expand Down Expand Up @@ -279,6 +290,7 @@ public function build_html_tree($element, $totals, $parents, $level, &$row_count
}

$row = new html_table_row();
$row->id = 'grade-item-' . $eid;
$row->attributes['class'] = $courseclass . ' category ' . $dimmed;
foreach ($rowclasses as $class) {
$row->attributes['class'] .= ' ' . $class;
Expand Down Expand Up @@ -327,6 +339,7 @@ public function build_html_tree($element, $totals, $parents, $level, &$row_count

$dimmed = ($item->is_hidden()) ? "dimmed_text" : "";
$gradeitemrow = new html_table_row();
$gradeitemrow->id = 'grade-item-' . $eid;
$gradeitemrow->attributes['class'] = $categoryitemclass . ' item ' . $dimmed;
foreach ($rowclasses as $class) {
$gradeitemrow->attributes['class'] .= ' ' . $class;
Expand Down
72 changes: 49 additions & 23 deletions grade/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -1564,16 +1564,17 @@ public function get_params_for_iconstr($element) {
*
* @param array $element An array representing an element in the grade_tree
* @param object $gpr A grade_plugin_return object
* @return string
* @param bool $returnactionmenulink return the instance of action_menu_link instead of string
* @return string|action_menu_link
*/
public function get_reset_icon($element, $gpr) {
public function get_reset_icon($element, $gpr, $returnactionmenulink = false) {
global $CFG, $OUTPUT;

// Limit to category items set to use the natural weights aggregation method, and users
// with the capability to manage grades.
if ($element['type'] != 'category' || $element['object']->aggregation != GRADE_AGGREGATE_SUM ||
!has_capability('moodle/grade:manage', $this->context)) {
return '';
return $returnactionmenulink ? null : '';
}

$str = get_string('resetweights', 'grades', $this->get_params_for_iconstr($element));
Expand All @@ -1584,25 +1585,30 @@ public function get_reset_icon($element, $gpr) {
'sesskey' => sesskey(),
));

return $OUTPUT->action_icon($gpr->add_url_params($url), new pix_icon('t/reset', $str));
if ($returnactionmenulink) {
return new action_menu_link_secondary($gpr->add_url_params($url), new pix_icon('t/reset', $str),
get_string('resetweightsshort', 'grades'));
} else {
return $OUTPUT->action_icon($gpr->add_url_params($url), new pix_icon('t/reset', $str));
}
}

/**
* Return edit icon for give element
*
* @param array $element An array representing an element in the grade_tree
* @param object $gpr A grade_plugin_return object
*
* @return string
* @param bool $returnactionmenulink return the instance of action_menu_link instead of string
* @return string|action_menu_link
*/
public function get_edit_icon($element, $gpr) {
public function get_edit_icon($element, $gpr, $returnactionmenulink = false) {
global $CFG, $OUTPUT;

if (!has_capability('moodle/grade:manage', $this->context)) {
if ($element['type'] == 'grade' and has_capability('moodle/grade:edit', $this->context)) {
// oki - let them override grade
} else {
return '';
return $returnactionmenulink ? null : '';
}
}

Expand Down Expand Up @@ -1656,10 +1662,16 @@ public function get_edit_icon($element, $gpr) {
}

if ($url) {
return $OUTPUT->action_icon($gpr->add_url_params($url), new pix_icon('t/edit', $stredit));
if ($returnactionmenulink) {
return new action_menu_link_secondary($gpr->add_url_params($url),
new pix_icon('t/edit', $stredit),
get_string('editsettings'));
} else {
return $OUTPUT->action_icon($gpr->add_url_params($url), new pix_icon('t/edit', $stredit));
}

} else {
return '';
return $returnactionmenulink ? null : '';
}
}

Expand All @@ -1668,19 +1680,19 @@ public function get_edit_icon($element, $gpr) {
*
* @param array $element An array representing an element in the grade_tree
* @param object $gpr A grade_plugin_return object
*
* @return string
* @param bool $returnactionmenulink return the instance of action_menu_link instead of string
* @return string|action_menu_link
*/
public function get_hiding_icon($element, $gpr) {
public function get_hiding_icon($element, $gpr, $returnactionmenulink = false) {
global $CFG, $OUTPUT;

if (!$element['object']->can_control_visibility()) {
return '';
return $returnactionmenulink ? null : '';
}

if (!has_capability('moodle/grade:manage', $this->context) and
!has_capability('moodle/grade:hide', $this->context)) {
return '';
return $returnactionmenulink ? null : '';
}

$strparams = $this->get_params_for_iconstr($element);
Expand All @@ -1703,11 +1715,19 @@ public function get_hiding_icon($element, $gpr) {

$url->param('action', 'show');

$hideicon = $OUTPUT->action_icon($url, new pix_icon('t/'.$type, $tooltip, 'moodle', array('alt'=>$strshow, 'class'=>'smallicon')));
if ($returnactionmenulink) {
$hideicon = new action_menu_link_secondary($url, new pix_icon('t/'.$type, $tooltip), get_string('show'));
} else {
$hideicon = $OUTPUT->action_icon($url, new pix_icon('t/'.$type, $tooltip, 'moodle', array('alt'=>$strshow, 'class'=>'smallicon')));
}

} else {
$url->param('action', 'hide');
$hideicon = $OUTPUT->action_icon($url, new pix_icon('t/hide', $strhide));
if ($returnactionmenulink) {
$hideicon = new action_menu_link_secondary($url, new pix_icon('t/hide', $strhide), get_string('hide'));
} else {
$hideicon = $OUTPUT->action_icon($url, new pix_icon('t/hide', $strhide));
}
}

return $hideicon;
Expand Down Expand Up @@ -1775,13 +1795,13 @@ public function get_locking_icon($element, $gpr) {
*
* @param array $element An array representing an element in the grade_tree
* @param object $gpr A grade_plugin_return object
*
* @return string
* @param bool $returnactionmenulink return the instance of action_menu_link instead of string
* @return string|action_menu_link
*/
public function get_calculation_icon($element, $gpr) {
public function get_calculation_icon($element, $gpr, $returnactionmenulink = false) {
global $CFG, $OUTPUT;
if (!has_capability('moodle/grade:manage', $this->context)) {
return '';
return $returnactionmenulink ? null : '';
}

$type = $element['type'];
Expand All @@ -1804,11 +1824,17 @@ public function get_calculation_icon($element, $gpr) {

$url = new moodle_url('/grade/edit/tree/calculation.php', array('courseid' => $this->courseid, 'id' => $object->id));
$url = $gpr->add_url_params($url);
return $OUTPUT->action_icon($url, new pix_icon($icon, $streditcalculation));
if ($returnactionmenulink) {
return new action_menu_link_secondary($url,
new pix_icon($icon, $streditcalculation),
get_string('editcalculation', 'grades'));
} else {
return $OUTPUT->action_icon($url, new pix_icon($icon, $streditcalculation));
}
}
}

return '';
return $returnactionmenulink ? null : '';
}
}

Expand Down
47 changes: 41 additions & 6 deletions grade/tests/behat/behat_grade.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,49 @@ public function i_give_the_grade($grade, $userfullname, $itemname) {
* @return Given[]
*/
public function i_set_the_following_settings_for_grade_item($gradeitem, TableNode $data) {

$steps = array();
$gradeitem = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem);

if ($this->running_javascript()) {
$xpath = "//tr[contains(.,$gradeitem)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
$steps[] = new Given('I click on "' . $this->escape($xpath) . '" "xpath_element"');
}
}

$savechanges = get_string('savechanges', 'grades');
$edit = $this->getSession()->getSelectorsHandler()->xpathLiteral(get_string('edit') . ' ');
$gradeitem = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem);
$linkxpath = "//a[./img[starts-with(@title,$edit) and contains(@title,$gradeitem)]]";
return array(
new Given('I click on "' . $this->escape($linkxpath) . '" "xpath_element"'),
new Given('I set the following fields to these values:', $data),
new Given('I press "' . $this->escape($savechanges) . '"'),
);
$steps[] = new Given('I click on "' . $this->escape($linkxpath) . '" "xpath_element"');
$steps[] = new Given('I set the following fields to these values:', $data);
$steps[] = new Given('I press "' . $this->escape($savechanges) . '"');
return $steps;
}

/**
* Resets the weights for the grade category
*
* Teacher must be on the grade setup page.
*
* @Given /^I reset weights for grade category "(?P<grade_item_string>(?:[^"]|\\")*)"$/
* @param $gradeitem
* @return array
*/
public function i_reset_weights_for_grade_category($gradeitem) {

$steps = array();

if ($this->running_javascript()) {
$gradeitemliteral = $this->getSession()->getSelectorsHandler()->xpathLiteral($gradeitem);
$xpath = "//tr[contains(.,$gradeitemliteral)]//*[contains(@class,'moodle-actionmenu')]//a[contains(@class,'toggle-display')]";
if ($this->getSession()->getPage()->findAll('xpath', $xpath)) {
$steps[] = new Given('I click on "' . $this->escape($xpath) . '" "xpath_element"');
}
}

$linktext = get_string('resetweights', 'grades', (object)array('itemname' => $gradeitem));
$steps[] = new Given('I click on "' . $this->escape($linktext) . '" "link"');
return $steps;
}
}
17 changes: 7 additions & 10 deletions grade/tests/behat/grade_aggregation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -504,15 +504,12 @@ Feature: We can use calculated grade totals
@javascript
Scenario: Natural aggregation from the setup screen
And I set the field "Grade report" to "Categories and items"
And I follow "Edit Course 1"
And I set the field "Aggregation" to "Natural"
And I press "Save changes"
And I follow "Edit Sub category 1"
And I set the field "Aggregation" to "Natural"
And I press "Save changes"
And I follow "Edit Sub category 2"
And I set the field "Aggregation" to "Natural"
And I press "Save changes"
And I set the following settings for grade item "Course 1":
| Aggregation | Natural |
And I set the following settings for grade item "Sub category 1":
| Aggregation | Natural |
And I set the following settings for grade item "Sub category 2":
| Aggregation | Natural |

And I set the field "Override weight of Test assignment one" to "1"
And the field "Weight of Test assignment one" matches value "37.975"
Expand Down Expand Up @@ -553,7 +550,7 @@ Feature: We can use calculated grade totals
And I set the field "Override weight of Sub category 1" to "1"
And the field "Weight of Test assignment one" matches value "37.975"
And the field "Weight of Sub category 1" matches value "5.696"
And I click on "Reset weights of Sub category 2" "link"
And I reset weights for grade category "Sub category 2"
And the field "Weight of Test assignment ten" matches value "33.333"

@javascript
Expand Down
Loading

0 comments on commit 079b2e5

Please sign in to comment.