From 210611f6eed4a956f00143f67d5aaf6d55fd8468 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Tue, 5 Jun 2007 08:50:24 +0000 Subject: [PATCH] MDL-9629 Changed grade_item.gradetype default to 1, added GRADE_TYPE_NONE constant = 0, incrementing existing constants by 1. Set up auto-generation of idnumber for grade_item if itemmodule and iteminstance are set, only a temporary stub if they are not set (must be replaced by correct generation code). New category form now has server-side validation (not using formslib) --- grade/report/grader/category.php | 22 +++++------ lang/en_utf8/grades.php | 7 +++- lib/db/install.xml | 2 +- lib/db/upgrade.php | 12 ++++++ lib/grade/grade_category.php | 20 +++++----- lib/grade/grade_item.php | 9 +++++ lib/grade/grade_tree.php | 38 ++++++++++--------- lib/gradelib.php | 7 ++-- .../grade/simpletest/testgradecategory.php | 10 +++-- version.php | 2 +- 10 files changed, 79 insertions(+), 50 deletions(-) diff --git a/grade/report/grader/category.php b/grade/report/grader/category.php index cb1c6f8cc7..69213a2f53 100644 --- a/grade/report/grader/category.php +++ b/grade/report/grader/category.php @@ -44,6 +44,8 @@ $param->categories = optional_param('categories', 0, PARAM_INT); $param->element_type = optional_param('element_type', 0, PARAM_ALPHA); $param->category_name = optional_param('category_name', 0, PARAM_ALPHA); +print_object($param); + $tree = new grade_tree($param->courseid); $select_source = false; @@ -75,14 +77,13 @@ if (!empty($param->action) && !empty($param->source) && confirm_sesskey()) { case 'edit': break; case 'delete': - if ($param->confirm == 1) { + if ($param->confirm == 1) { // Perform the deletion $tree->remove_element($param->target); $tree->renumber(); $tree->update_db(); // Print result message - } else { - // Print confirmation dialog + } else { // Print confirmation dialog $strdeletecheckfull = get_string('deletecheck', '', $element->element['object']->get_name()); $linkyes = "category.php?target=$param->target&action=delete&confirm=1$tree->commonvars"; $linkno = "category.php?$tree->commonvars"; @@ -124,12 +125,11 @@ if (!empty($param->action) && !empty($param->source) && confirm_sesskey()) { break; } unset($param->target); -} elseif (!empty($param->element_type) && - !empty($param->action) && - $param->action == 'create' && - confirm_sesskey() && - !empty($param->category_name)) { - if ($param->element_type == 'items') { +} elseif (!empty($param->element_type) && !empty($param->action) && $param->action == 'create' && confirm_sesskey()) { + if (empty($param->category_name)) { + notice(get_string('createcategoryerror', 'grades') . ': ' . get_string('nocategoryname', 'grades')); + } elseif ($param->element_type == 'items') { + if (!empty($param->items)) { $category = new grade_category(); $category->fullname = $param->category_name; @@ -150,7 +150,7 @@ if (!empty($param->action) && !empty($param->source) && confirm_sesskey()) { } } else { // No items selected. Can't create a category over them... - notice("Couldn't create a category, you did not select any grade items."); + notice(get_string('createcategoryerror', 'grades') . ': ' . get_string('noselecteditems', 'grades')); } } elseif ($param->element_type == 'categories') { if (!empty($param->categories)) { @@ -172,7 +172,7 @@ if (!empty($param->action) && !empty($param->source) && confirm_sesskey()) { } } else { // No categories selected. Can't create a category over them... - notice("Couldn't create a category, you did not select any sub-categories."); + notice(get_string('createcategoryerror', 'grades') . ': ' . get_string('noselectedcategories', 'grades')); } } else { // The element_type wasn't set properly, throw up an error diff --git a/lang/en_utf8/grades.php b/lang/en_utf8/grades.php index 34ed69ee51..b281c91fe5 100644 --- a/lang/en_utf8/grades.php +++ b/lang/en_utf8/grades.php @@ -13,7 +13,8 @@ $string['categories'] = 'Categories'; $string['category'] = 'Category'; $string['categoryname'] = 'Category name'; $string['choosecategory'] = 'Select Category'; -$string['createcategory'] = 'Create category'; +$string['createcategory'] = 'Create Category'; +$string['createcategoryerror'] = 'Could not create a new category'; $string['creatinggradebooksettings'] = 'Creating Gradebook settings'; $string['curveto'] = 'Curve To'; $string['deletecategory'] = 'Delete Category'; @@ -75,6 +76,7 @@ $string['movingelement'] = 'Moving $a'; $string['newcategory'] = 'New category'; $string['no'] = 'No'; $string['nocategories'] = 'Grade categories could not be added or found for this course'; +$string['nocategoryname'] = 'No category name was given.'; $string['nocategoryview'] = 'No category to view by'; $string['nogradeletters'] = 'No grade letters set'; $string['nogradesreturned'] = 'No grades returned'; @@ -82,6 +84,8 @@ $string['nolettergrade'] = 'No letter grade for'; $string['nomode'] = 'NA'; $string['nonnumericweight'] = 'Received non-numeric value for'; $string['nonweightedpct'] = 'non-weighted %%'; +$string['noselectedcategories'] = 'no categories were selected.'; +$string['noselecteditems'] = 'no items were selected.'; $string['notteachererror'] = 'You must be a teacher to use this feature.'; $string['pctoftotalgrade'] = '%% of total grade'; $string['percent'] = 'Percent'; @@ -134,5 +138,4 @@ $string['weightedpctcontribution'] = 'weighted %% contribution'; $string['writinggradebookinfo'] = 'Writing Gradebook settings'; $string['yes'] = 'Yes'; $string['yourgrade'] = 'Your grade'; - ?> diff --git a/lib/db/install.xml b/lib/db/install.xml index e946e85282..fcfa70ec80 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1248,7 +1248,7 @@ - + diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index 5a97b266ef..2a22813248 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -1324,6 +1324,7 @@ function xmldb_main_upgrade($oldversion=0) { // Launch add field deleted $result = $result && add_field($table, $field); } + if ($result && $oldversion < 2007060500) { @@ -1343,7 +1344,18 @@ function xmldb_main_upgrade($oldversion=0) { /// Launch add key usermodified $result = $result && add_key($table, $key); } + + if ($result && $oldversion < 2007060501) { + /// Changing the default of field gradetype on table grade_items to 1 + $table = new XMLDBTable('grade_items'); + $field = new XMLDBField('gradetype'); + $field->setAttributes(XMLDB_TYPE_INTEGER, '4', null, XMLDB_NOTNULL, null, null, null, '1', 'idnumber'); + + /// Launch change of default for field gradetype + $result = $result && change_field_default($table, $field); + } + return $result; } diff --git a/lib/grade/grade_category.php b/lib/grade/grade_category.php index 2740e77efd..113fb3219f 100644 --- a/lib/grade/grade_category.php +++ b/lib/grade/grade_category.php @@ -534,9 +534,8 @@ class grade_category extends grade_object { } /** - * This method checks whether an existing child exists for this - * category. If the new child is of a different type, the method will return false (not allowed). - * Otherwise it will return true. + * Checks whether an existing child exists for this category. If the new child is of a + * different type, the method will return false (not allowed). Otherwise it will return true. * @param object $child This must be a complete object, not a stdClass * @return boolean Success or failure */ @@ -721,7 +720,6 @@ class grade_category extends grade_object { * A number of constraints are necessary: * - The children must all be of the same type and at the same level * - The children cannot already be top categories - * - The children cannot already have a top categorya * - The children all belong to the same course * @param array $children An array of fully instantiated grade_category OR grade_item objects * @@ -731,6 +729,11 @@ class grade_category extends grade_object { function set_as_parent($children) { global $CFG; + if (empty($children) || !is_array($children)) { + debugging("Passed an empty or non-array variable to grade_category::set_as_parent()"); + return false; + } + // Check type and sortorder of first child $first_child = current($children); $first_child_type = get_class($first_child); @@ -748,13 +751,8 @@ class grade_category extends grade_object { debugging("Violated constraint: Attempted to set a category over children which are already top categories."); return false; } - if ($first_child_type == 'grade_item') { - $child->load_category(); - if (!empty($child->category->parent)) { - debugging("Violated constraint: Attempted to set a category over children that already have a top category."); - return false; - } - } elseif ($first_child_type == 'grade_category') { + + if ($first_child_type == 'grade_category') { if (!empty($child->parent)) { debugging("Violated constraint: Attempted to set a category over children that already have a top category."); return false; diff --git a/lib/grade/grade_item.php b/lib/grade/grade_item.php index 048f191dd9..42a79c4047 100644 --- a/lib/grade/grade_item.php +++ b/lib/grade/grade_item.php @@ -373,6 +373,15 @@ class grade_item extends grade_object { } } + // If not set, generate an idnumber from itemmodule and iteminstance + if (empty($this->idnumber)) { + if (!empty($this->itemmodule) && !empty($this->iteminstance)) { + $this->idnumber = "$this->itemmodule.$this->iteminstance"; + } else { // No itemmodule or iteminstance, generate a random idnumber + $this->idnumber = rand(0,9999999999); // TODO replace rand() with proper random generator + } + } + $result = parent::insert(); // Notify parent category of need to update. Note that a grade_item may not have a categoryid. diff --git a/lib/grade/grade_tree.php b/lib/grade/grade_tree.php index b8d3e10eba..1671020c67 100644 --- a/lib/grade/grade_tree.php +++ b/lib/grade/grade_tree.php @@ -100,21 +100,29 @@ class grade_tree { * @param array $tree */ function grade_tree($courseid=NULL, $include_grades=false, $tree=NULL) { - global $USER; + if (empty($courseid)) { + // empty object, do nothing + } else { + if ($courseid == 0) { + $courseid = null; + } - $this->courseid = $courseid; - $this->include_grades = $include_grades; - $this->commonvars = "&sesskey=$USER->sesskey&courseid=$this->courseid"; + global $USER; - if (!empty($tree)) { - $this->tree_array = $tree; - } else { - $this->tree_array = $this->get_tree(); - } - - if (!empty($this->tree_array)) { - $this->first_sortorder = key($this->tree_array); - $this->renumber(); + $this->courseid = $courseid; + $this->include_grades = $include_grades; + $this->commonvars = "&sesskey=$USER->sesskey&courseid=$this->courseid"; + + if (!empty($tree)) { + $this->tree_array = $tree; + } else { + $this->tree_array = $this->get_tree(); + } + + if (!empty($this->tree_array)) { + $this->first_sortorder = key($this->tree_array); + $this->renumber(); + } } } @@ -1002,10 +1010,6 @@ class grade_tree { foreach ($elements as $sortorder => $element) { $object = $element['object']; - if (empty($element->next_sortorder)) { - $element->next_sortorder = null; - } - $object_name = $object->get_name(); $object_class = get_class($object); $object_parent = $object->get_parent_id(); diff --git a/lib/gradelib.php b/lib/gradelib.php index 13d78158d9..babdf95cd3 100644 --- a/lib/gradelib.php +++ b/lib/gradelib.php @@ -40,9 +40,10 @@ define('GRADE_CHILDTYPE_ITEM', 0); define('GRADE_CHILDTYPE_CAT', 1); define('GRADE_ITEM', 0); // Used to compare class names with CHILDTYPE values define('GRADE_CATEGORY', 1); // Used to compare class names with CHILDTYPE values -define('GRADE_TYPE_VALUE', 0); -define('GRADE_TYPE_SCALE', 1); -define('GRADE_TYPE_TEXT', 2); +define('GRADE_TYPE_NONE', 0); +define('GRADE_TYPE_VALUE', 1); +define('GRADE_TYPE_SCALE', 2); +define('GRADE_TYPE_TEXT', 3); require_once($CFG->libdir . '/grade/grade_category.php'); require_once($CFG->libdir . '/grade/grade_item.php'); diff --git a/lib/simpletest/grade/simpletest/testgradecategory.php b/lib/simpletest/grade/simpletest/testgradecategory.php index 7fb900a87f..bd17bf93ff 100755 --- a/lib/simpletest/grade/simpletest/testgradecategory.php +++ b/lib/simpletest/grade/simpletest/testgradecategory.php @@ -191,12 +191,12 @@ class grade_category_test extends gradelib_test { // Generate 3 random data sets $grade_sets = array(); - + for ($i = 0; $i < 3; $i++) { for ($j = 0; $j < 200; $j++) { $grade_sets[$i][] = $this->generate_random_raw_grade(new grade_item($this->grade_items[$i]), $j); } - } + } $aggregated_grades = $category->aggregate_grades($grade_sets); $this->assertEqual(200, count($aggregated_grades)); @@ -244,10 +244,12 @@ class grade_category_test extends gradelib_test { $this->assertFalse($grade_category->set_as_parent(array($child1))); $CFG->debug = $debuglevel; - // 3. Child already has a top category + // 3. Children belong to different courses $child1 = new grade_item($this->grade_items[0]); + $child2 = new grade_item($this->grade_items[1]); + $child2->courseid = 543; $CFG->debug = 2; - $this->assertFalse($grade_category->set_as_parent(array($child1))); + $this->assertFalse($grade_category->set_as_parent(array($child1, $child2))); $CFG->debug = $debuglevel; // Now test setting parent correctly diff --git a/version.php b/version.php index 0e88b375d4..11f36df16f 100644 --- a/version.php +++ b/version.php @@ -6,7 +6,7 @@ // This is compared against the values stored in the database to determine // whether upgrades should be performed (see lib/db/*.php) - $version = 2007060500; // YYYYMMDD = date + $version = 2007060501; // YYYYMMDD = date // XY = increments within a single day $release = '1.9 dev'; // Human-friendly version name -- 2.39.5