mirror of
https://github.com/moodle/moodle.git
synced 2025-01-31 12:45:04 +01:00
MDL-10251 finished hiding implementation for grades, items and categories + unit tests
This commit is contained in:
parent
7470d6de7a
commit
22e23c78a0
@ -913,15 +913,11 @@ class grade_category extends grade_object {
|
||||
/**
|
||||
* Returns the locked state/date of the associated grade_item. This method is also available in
|
||||
* grade_item, for cases where the object type is not known.
|
||||
* @return int 0, 1 or timestamp int(10)
|
||||
* @return boolean
|
||||
*/
|
||||
function is_locked() {
|
||||
$this->load_grade_item();
|
||||
if (!empty($this->grade_item)) {
|
||||
return $this->grade_item->is_locked();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return $this->grade_item->is_locked();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -932,27 +928,17 @@ class grade_category extends grade_object {
|
||||
*/
|
||||
function set_locked($lockedstate) {
|
||||
$this->load_grade_item();
|
||||
|
||||
if (!empty($this->grade_item)) {
|
||||
return $this->grade_item->set_locked($lockedstate);
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return $this->grade_item->set_locked($lockedstate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hidden state/date of the associated grade_item. This method is also available in
|
||||
* grade_item, for cases where the object type is not known.
|
||||
* @return int 0, 1 or timestamp int(10)
|
||||
* grade_item.
|
||||
* @return boolean
|
||||
*/
|
||||
function get_hidden() {
|
||||
function is_hidden() {
|
||||
$this->load_grade_item();
|
||||
if (!empty($this->grade_item)) {
|
||||
return $this->grade_item->hidden;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return $this->grade_item->is_hidden();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -963,12 +949,7 @@ class grade_category extends grade_object {
|
||||
*/
|
||||
function set_hidden($hidden) {
|
||||
$this->load_grade_item();
|
||||
if (!empty($this->grade_item)) {
|
||||
$this->grade_item->hidden = $hidden;
|
||||
return $this->grade_item->update();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
$this->grade_item->set_hidden($hidden);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -159,7 +159,7 @@ class grade_grades extends grade_object {
|
||||
function is_locked() {
|
||||
$this->load_grade_item();
|
||||
|
||||
return $this->grade_item->is_locked() or !empty($this->locked);
|
||||
return !empty($this->locked) or $this->grade_item->is_locked();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -204,6 +204,28 @@ class grade_grades extends grade_object {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check grade lock status. Uses both grade item lock and grade lock.
|
||||
* Internally any date in hidden field (including future ones) means hidden,
|
||||
* the date is stored for logging purposes only.
|
||||
*
|
||||
* @return boolean true if hidden, false if not
|
||||
*/
|
||||
function is_hidden() {
|
||||
$this->load_grade_item();
|
||||
|
||||
return $this->hidden == 1 or $this->hidden > time() or $this->grade_item->is_hidden();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the hidden status of grade, 0 mean visible, 1 always hidden, number means date to hide until.
|
||||
* @param int $hidden new hidden status
|
||||
*/
|
||||
function set_hidden($hidden) {
|
||||
$this->hidden = $hidden;
|
||||
$this->update();
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds and returns a grade_grades instance based on params.
|
||||
* @static
|
||||
|
@ -192,7 +192,7 @@ class grade_item extends grade_object {
|
||||
var $sortorder = 0;
|
||||
|
||||
/**
|
||||
* Date until which to hide this grade_item. If null, 0 or false, grade_item is not hidden. Hiding prevents viewing.
|
||||
* 0 if visible, 1 always hidden or date not visible until
|
||||
* @var int $hidden
|
||||
*/
|
||||
var $hidden = 0;
|
||||
@ -418,12 +418,9 @@ class grade_item extends grade_object {
|
||||
}
|
||||
|
||||
if (!empty($userid)) {
|
||||
|
||||
$grade = new grade_grades(array('itemid'=>$this->id, 'userid'=>$userid));
|
||||
$grade->grade_item =& $this; // prevent db fetching of cached grade_item
|
||||
|
||||
if (!empty($grade->id) and $grade->is_locked()) {
|
||||
return true;
|
||||
if ($grade = grade_grades::fetch(array('itemid'=>$this->id, 'userid'=>$userid))) {
|
||||
$grade->grade_item =& $this; // prevent db fetching of cached grade_item
|
||||
return $grade->is_locked();
|
||||
}
|
||||
}
|
||||
|
||||
@ -478,17 +475,17 @@ class grade_item extends grade_object {
|
||||
|
||||
// this could be improved with direct SQL update
|
||||
$result = true;
|
||||
$grades = $this->get_final();
|
||||
foreach($grades as $g) {
|
||||
$grade = new grade_grades($g, false);
|
||||
$grade->grade_item =& $this;
|
||||
if ($grades = grade_grades::fetch_all(array('itemid'=>$this->id))) {
|
||||
foreach($grades as $grade) {
|
||||
$grade->grade_item =& $this;
|
||||
|
||||
if (!empty($grade->locktime) and $grade->locktime < time()) {
|
||||
$result = false; // can not unlock grade that should be already locked
|
||||
}
|
||||
if (!empty($grade->locktime) and $grade->locktime < time()) {
|
||||
$result = false; // can not unlock grade that should be already locked
|
||||
}
|
||||
|
||||
if (!$grade->set_locked(false)) {
|
||||
$result = false;
|
||||
if (!$grade->set_locked(false)) {
|
||||
$result = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -498,17 +495,43 @@ class grade_item extends grade_object {
|
||||
}
|
||||
|
||||
/**
|
||||
* Locks or unlocks this grade_item and (optionally) all its associated final grades.
|
||||
* @param boolean $update_final Whether to update final grades too
|
||||
* @param boolean $new_state Optional new state. Will use inverse of current state otherwise.
|
||||
* @return int Number of final grades changed, or false if error occurred during update.
|
||||
* Returns the hidden state of this grade_item (if the grade_item is hidden OR no specific
|
||||
* $userid is given) or the hidden state of a specific grade within this item if a specific
|
||||
* $userid is given and the grade_item is unhidden.
|
||||
*
|
||||
* @param int $userid
|
||||
* @return boolean hidden state
|
||||
*/
|
||||
function toggle_hiding($update_final=false, $new_state=NULL) {
|
||||
//TODO: implement new hiding
|
||||
function is_hidden($userid=NULL) {
|
||||
if ($this->hidden == 1 or $this->hidden > time()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return 0;
|
||||
if (!empty($userid)) {
|
||||
if ($grade = grade_grades::fetch(array('itemid'=>$this->id, 'userid'=>$userid))) {
|
||||
$grade->grade_item =& $this; // prevent db fetching of cached grade_item
|
||||
return $grade->is_hidden();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the hidden status of grade_item and all grades, 0 mean visible, 1 always hidden, number means date to hide until.
|
||||
* @param int $hidden new hidden status
|
||||
*/
|
||||
function set_hidden($hidden) {
|
||||
$this->hidden = $hidden;
|
||||
$this->update();
|
||||
|
||||
if ($grades = grade_grades::fetch_all(array('itemid'=>$this->id))) {
|
||||
foreach($grades as $grade) {
|
||||
$grade->grade_item =& $this;
|
||||
$grade->set_hidden($hidden);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs the necessary calculations on the grades_final referenced by this grade_item.
|
||||
@ -944,26 +967,6 @@ class grade_item extends grade_object {
|
||||
$this->categoryid = $parentid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the hidden state/date of this grade_item. This method is also available in
|
||||
* grade_category, for cases where the object type is not known.
|
||||
* @return int 0, 1 or timestamp int(10)
|
||||
*/
|
||||
function is_hidden() {
|
||||
// to do
|
||||
return $this->hidden;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the grade_item's hidden variable and updates the grade_item.
|
||||
* @param int $hidden 0, 1 or a timestamp int(10) after which date the item will be hidden.
|
||||
* @return void
|
||||
*/
|
||||
function set_hidden($hidden) {
|
||||
$this->hidden = $hidden;
|
||||
return $this->update();
|
||||
}
|
||||
|
||||
/**
|
||||
* If the old parent is set (after an update), this checks and returns whether it has any children. Important for
|
||||
* deleting childless categories.
|
||||
@ -1109,7 +1112,7 @@ class grade_item extends grade_object {
|
||||
|
||||
// trigger grade_updated event notification
|
||||
$eventdata = new object();
|
||||
|
||||
|
||||
$eventdata->source = $source;
|
||||
$eventdata->itemid = $this->id;
|
||||
$eventdata->courseid = $this->courseid;
|
||||
@ -1120,7 +1123,7 @@ class grade_item extends grade_object {
|
||||
$eventdata->idnumber = $this->idnumber;
|
||||
$eventdata->userid = $grade->userid;
|
||||
$eventdata->rawgrade = $grade->rawgrade;
|
||||
|
||||
|
||||
// load existing text annotation
|
||||
if ($grade_text = $grade->load_text()) {
|
||||
$eventdata->feedback = $grade_text->feedback;
|
||||
@ -1128,7 +1131,7 @@ class grade_item extends grade_object {
|
||||
$eventdata->information = $grade_text->information;
|
||||
$eventdata->informationformat = $grade_text->informationformat;
|
||||
}
|
||||
|
||||
|
||||
events_trigger('grade_updated', $eventdata);
|
||||
|
||||
return $grade;
|
||||
|
@ -1091,7 +1091,7 @@ class grade_tree {
|
||||
|
||||
// Add dimmed_text span around object name if set to hidden
|
||||
$hide_show = 'hide';
|
||||
if ($object->get_hidden()) {
|
||||
if ($object->is_hidden()) {
|
||||
$object_name = '<span class="dimmed_text">' . $object_name . '</span>';
|
||||
$hide_show = 'show';
|
||||
}
|
||||
|
@ -166,6 +166,36 @@ class grade_grades_test extends grade_test {
|
||||
$this->assertTrue($grade->is_locked());
|
||||
}
|
||||
|
||||
function test_grade_grades_set_hidden() {
|
||||
$grade_item = new grade_item($this->grade_items[0]);
|
||||
$grade = new grade_grades($grade_item->get_final(1));
|
||||
$this->assertTrue(method_exists($grade, 'set_hidden'));
|
||||
|
||||
$this->assertEqual(0, $grade_item->hidden);
|
||||
$this->assertEqual(0, $grade->hidden);
|
||||
|
||||
$grade->set_hidden(0);
|
||||
$this->assertEqual(0, $grade->hidden);
|
||||
|
||||
$grade->set_hidden(1);
|
||||
$this->assertEqual(1, $grade->hidden);
|
||||
}
|
||||
|
||||
function test_grade_grades_is_hidden() {
|
||||
$grade = new grade_grades($this->grade_grades[0]);
|
||||
$this->assertTrue(method_exists($grade, 'is_hidden'));
|
||||
|
||||
$this->assertFalse($grade->is_hidden());
|
||||
$grade->hidden = 1;
|
||||
$this->assertTrue($grade->is_hidden());
|
||||
|
||||
$grade->hidden = time()-666;
|
||||
$this->assertFalse($grade->is_hidden());
|
||||
|
||||
$grade->hidden = time()+666;
|
||||
$this->assertTrue($grade->is_hidden());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
?>
|
||||
|
@ -343,6 +343,44 @@ class grade_item_test extends grade_test {
|
||||
$this->assertTrue($grade_item->is_locked(1));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test hiding of grade items
|
||||
*/
|
||||
function test_grade_item_set_hidden() {
|
||||
$grade_item = new grade_item($this->grade_items[0]);
|
||||
$this->assertTrue(method_exists($grade_item, 'set_hidden'));
|
||||
|
||||
$grade = new grade_grades($grade_item->get_final(1));
|
||||
$this->assertEqual(0, $grade_item->hidden);
|
||||
$this->assertEqual(0, $grade->hidden);
|
||||
|
||||
$grade_item->set_hidden(666);
|
||||
$grade = new grade_grades($grade_item->get_final(1));
|
||||
|
||||
$this->assertEqual(666, $grade_item->hidden);
|
||||
$this->assertEqual(666, $grade->hidden);
|
||||
}
|
||||
|
||||
function test_grade_item_is_hidden() {
|
||||
$grade_item = new grade_item($this->grade_items[0]);
|
||||
$this->assertTrue(method_exists($grade_item, 'is_hidden'));
|
||||
|
||||
$this->assertFalse($grade_item->is_hidden());
|
||||
$this->assertFalse($grade_item->is_hidden(1));
|
||||
|
||||
$grade_item->set_hidden(1);
|
||||
$this->assertTrue($grade_item->is_hidden());
|
||||
$this->assertTrue($grade_item->is_hidden(1));
|
||||
|
||||
$grade_item->set_hidden(666);
|
||||
$this->assertFalse($grade_item->is_hidden());
|
||||
$this->assertFalse($grade_item->is_hidden(1));
|
||||
|
||||
$grade_item->set_hidden(time()+666);
|
||||
$this->assertTrue($grade_item->is_hidden());
|
||||
$this->assertTrue($grade_item->is_hidden(1));
|
||||
}
|
||||
|
||||
function test_grade_item_depends_on() {
|
||||
$grade_item = new grade_item($this->grade_items[1]);
|
||||
|
||||
@ -366,27 +404,6 @@ class grade_item_test extends grade_test {
|
||||
$this->assertEqual($res, $deps);
|
||||
}
|
||||
|
||||
/*
|
||||
function test_grade_item_toggle_hiding() {
|
||||
$grade_item = new grade_item($this->grade_items[0]);
|
||||
$this->assertTrue(method_exists($grade_item, 'toggle_hiding'));
|
||||
|
||||
$this->assertFalse($grade_item->hidden);
|
||||
$this->assertEqual(0, $grade_item->toggle_hiding());
|
||||
$this->assertTrue($grade_item->hidden);
|
||||
$grade_item->load_final();
|
||||
$this->assertFalse($grade_item->grade_grades[1]->hidden);
|
||||
|
||||
$grade_item->hidden = false;
|
||||
$this->assertEqual(3, $grade_item->toggle_hiding(true));
|
||||
$this->assertTrue($grade_item->hidden);
|
||||
$this->assertTrue($grade_item->grade_grades[1]->hidden);
|
||||
$this->assertTrue($grade_item->grade_gra
|
||||
des[2]->hidden);
|
||||
$this->assertTrue($grade_item->grade_grades[3]->hidden);
|
||||
}
|
||||
*/
|
||||
|
||||
function test_grade_item_is_calculated() {
|
||||
$grade_item = new grade_item($this->grade_items[1]);
|
||||
$this->assertTrue(method_exists($grade_item, 'is_calculated'));
|
||||
|
Loading…
x
Reference in New Issue
Block a user