MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / lib / grade / simpletest / testgradeitem.php
blobbbe81d56f8389f93e4b59b2256232f52137426ee
1 <?php // $Id$
3 ///////////////////////////////////////////////////////////////////////////
4 // //
5 // NOTICE OF COPYRIGHT //
6 // //
7 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
8 // http://moodle.org //
9 // //
10 // Copyright (C) 1999-2004 Martin Dougiamas http://dougiamas.com //
11 // //
12 // This program is free software; you can redistribute it and/or modify //
13 // it under the terms of the GNU General Public License as published by //
14 // the Free Software Foundation; either version 2 of the License, or //
15 // (at your option) any later version. //
16 // //
17 // This program is distributed in the hope that it will be useful, //
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
20 // GNU General Public License for more details: //
21 // //
22 // http://www.gnu.org/copyleft/gpl.html //
23 // //
24 ///////////////////////////////////////////////////////////////////////////
26 /**
27 * Unit tests for grade_item object.
29 * @author nicolas@moodle.com
30 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
31 * @package moodlecore
34 if (!defined('MOODLE_INTERNAL')) {
35 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
38 require_once($CFG->libdir.'/simpletest/fixtures/gradetest.php');
40 @set_time_limit(0);
42 class grade_item_test extends grade_test {
44 function test_grade_item_construct() {
45 $params = new stdClass();
47 $params->courseid = $this->courseid;
48 $params->categoryid = $this->grade_categories[1]->id;
49 $params->itemname = 'unittestgradeitem4';
50 $params->itemtype = 'mod';
51 $params->itemmodule = 'database';
52 $params->iteminfo = 'Grade item used for unit testing';
54 $grade_item = new grade_item($params, false);
56 $this->assertEqual($params->courseid, $grade_item->courseid);
57 $this->assertEqual($params->categoryid, $grade_item->categoryid);
58 $this->assertEqual($params->itemmodule, $grade_item->itemmodule);
61 function test_grade_item_insert() {
62 $grade_item = new grade_item();
63 $this->assertTrue(method_exists($grade_item, 'insert'));
65 $grade_item->courseid = $this->courseid;
66 $grade_item->categoryid = $this->grade_categories[1]->id;
67 $grade_item->itemname = 'unittestgradeitem4';
68 $grade_item->itemtype = 'mod';
69 $grade_item->itemmodule = 'quiz';
70 $grade_item->iteminfo = 'Grade item used for unit testing';
72 $grade_item->insert();
74 $last_grade_item = end($this->grade_items);
76 $this->assertEqual($grade_item->id, $last_grade_item->id + 1);
77 $this->assertEqual(11, $grade_item->sortorder);
80 function test_grade_item_delete() {
81 $grade_item = new grade_item($this->grade_items[0]);
82 $this->assertTrue(method_exists($grade_item, 'delete'));
84 $this->assertTrue($grade_item->delete());
86 $this->assertFalse(get_record('grade_items', 'id', $grade_item->id));
89 function test_grade_item_update() {
90 $grade_item = new grade_item($this->grade_items[0]);
91 $this->assertTrue(method_exists($grade_item, 'update'));
93 $grade_item->iteminfo = 'Updated info for this unittest grade_item';
95 $this->assertTrue($grade_item->update());
97 $grade_item->grademin = 14;
98 $this->assertTrue($grade_item->qualifies_for_regrading());
99 $this->assertTrue($grade_item->update());
101 $iteminfo = get_field('grade_items', 'iteminfo', 'id', $this->grade_items[0]->id);
102 $this->assertEqual($grade_item->iteminfo, $iteminfo);
105 function test_grade_item_load_scale() {
106 $grade_item = new grade_item($this->grade_items[2]);
107 $this->assertTrue(method_exists($grade_item, 'load_scale'));
108 $scale = $grade_item->load_scale();
109 $this->assertFalse(empty($grade_item->scale));
110 $this->assertEqual($scale->id, $this->grade_items[2]->scaleid);
113 function test_grade_item_load_outcome() {
114 $grade_item = new grade_item($this->grade_items[0]);
115 $this->assertTrue(method_exists($grade_item, 'load_outcome'));
116 //TODO: add tests
119 function test_grade_item_qualifies_for_regrading() {
120 $grade_item = new grade_item($this->grade_items[0]);
121 $this->assertTrue(method_exists($grade_item, 'qualifies_for_regrading'));
123 $this->assertFalse($grade_item->qualifies_for_regrading());
125 $grade_item->iteminfo = 'Updated info for this unittest grade_item';
127 $this->assertFalse($grade_item->qualifies_for_regrading());
129 $grade_item->grademin = 14;
131 $this->assertTrue($grade_item->qualifies_for_regrading());
134 function test_grade_item_force_regrading() {
135 $grade_item = new grade_item($this->grade_items[0]);
136 $this->assertTrue(method_exists($grade_item, 'force_regrading'));
138 $this->assertEqual(0, $grade_item->needsupdate);
140 $grade_item->force_regrading();
141 $this->assertEqual(1, $grade_item->needsupdate);
142 $grade_item->update_from_db();
143 $this->assertEqual(1, $grade_item->needsupdate);
146 function test_grade_item_fetch() {
147 $grade_item = new grade_item();
148 $this->assertTrue(method_exists($grade_item, 'fetch'));
150 $grade_item = grade_item::fetch(array('id'=>$this->grade_items[0]->id));
151 $this->assertEqual($this->grade_items[0]->id, $grade_item->id);
152 $this->assertEqual($this->grade_items[0]->iteminfo, $grade_item->iteminfo);
154 $grade_item = grade_item::fetch(array('itemtype'=>$this->grade_items[1]->itemtype, 'itemmodule'=>$this->grade_items[1]->itemmodule));
155 $this->assertEqual($this->grade_items[1]->id, $grade_item->id);
156 $this->assertEqual($this->grade_items[1]->iteminfo, $grade_item->iteminfo);
159 function test_grade_item_fetch_all() {
160 $grade_item = new grade_item();
161 $this->assertTrue(method_exists($grade_item, 'fetch_all'));
163 $grade_items = grade_item::fetch_all(array('courseid'=>$this->courseid));
164 $this->assertEqual(count($this->grade_items), count($grade_items)-1);
168 * Retrieve all final scores for a given grade_item.
170 function test_grade_item_get_all_finals() {
171 $grade_item = new grade_item($this->grade_items[0]);
172 $this->assertTrue(method_exists($grade_item, 'get_final'));
174 $final_grades = $grade_item->get_final();
175 $this->assertEqual(3, count($final_grades));
180 * Retrieve all final scores for a specific userid.
182 function test_grade_item_get_final() {
183 $grade_item = new grade_item($this->grade_items[0]);
184 $this->assertTrue(method_exists($grade_item, 'get_final'));
185 $final_grade = $grade_item->get_final($this->userid);
186 $this->assertEqual($this->grade_grades[0]->finalgrade, $final_grade->finalgrade);
189 function test_grade_item_get_sortorder() {
190 $grade_item = new grade_item($this->grade_items[0]);
191 $this->assertTrue(method_exists($grade_item, 'get_sortorder'));
192 $sortorder = $grade_item->get_sortorder();
193 $this->assertEqual($this->grade_items[0]->sortorder, $sortorder);
196 function test_grade_item_set_sortorder() {
197 $grade_item = new grade_item($this->grade_items[0]);
198 $this->assertTrue(method_exists($grade_item, 'set_sortorder'));
199 $grade_item->set_sortorder(999);
200 $this->assertEqual($grade_item->sortorder, 999);
203 function test_grade_item_move_after_sortorder() {
204 $grade_item = new grade_item($this->grade_items[0]);
205 $this->assertTrue(method_exists($grade_item, 'move_after_sortorder'));
206 $grade_item->move_after_sortorder(5);
207 $this->assertEqual($grade_item->sortorder, 6);
209 $grade_item = grade_item::fetch(array('id'=>$this->grade_items[0]->id));
210 $this->assertEqual($grade_item->sortorder, 6);
212 $after = grade_item::fetch(array('id'=>$this->grade_items[6]->id));
213 $this->assertEqual($after->sortorder, 8);
216 function test_grade_item_get_name() {
217 $grade_item = new grade_item($this->grade_items[0]);
218 $this->assertTrue(method_exists($grade_item, 'get_name'));
220 $name = $grade_item->get_name();
221 $this->assertEqual($this->grade_items[0]->itemname, $name);
224 function test_grade_item_set_parent() {
225 $grade_item = new grade_item($this->grade_items[0]);
226 $this->assertTrue(method_exists($grade_item, 'set_parent'));
228 $old = $grade_item->get_parent_category();
229 $new = new grade_category($this->grade_categories[3]);
230 $new_item = $new->get_grade_item();
232 $this->assertTrue($grade_item->set_parent($new->id));
234 $new_item->update_from_db();
235 $grade_item->update_from_db();
237 $this->assertEqual($grade_item->categoryid, $new->id);
240 function test_grade_item_get_parent_category() {
241 $grade_item = new grade_item($this->grade_items[0]);
242 $this->assertTrue(method_exists($grade_item, 'get_parent_category'));
244 $category = $grade_item->get_parent_category();
245 $this->assertEqual($this->grade_categories[1]->fullname, $category->fullname);
248 function test_grade_item_load_parent_category() {
249 $grade_item = new grade_item($this->grade_items[0]);
250 $this->assertTrue(method_exists($grade_item, 'load_parent_category'));
252 $category = $grade_item->load_parent_category();
253 $this->assertEqual($this->grade_categories[1]->fullname, $category->fullname);
254 $this->assertEqual($this->grade_categories[1]->fullname, $grade_item->parent_category->fullname);
257 function test_grade_item_get_item_category() {
258 $grade_item = new grade_item($this->grade_items[3]);
259 $this->assertTrue(method_exists($grade_item, 'get_item_category'));
261 $category = $grade_item->get_item_category();
262 $this->assertEqual($this->grade_categories[0]->fullname, $category->fullname);
265 function test_grade_item_load_item_category() {
266 $grade_item = new grade_item($this->grade_items[3]);
267 $this->assertTrue(method_exists($grade_item, 'load_item_category'));
269 $category = $grade_item->load_item_category();
270 $this->assertEqual($this->grade_categories[0]->fullname, $category->fullname);
271 $this->assertEqual($this->grade_categories[0]->fullname, $grade_item->item_category->fullname);
275 * Test update of all final grades
277 function test_grade_item_regrade_final_grades() {
278 $grade_item = new grade_item($this->grade_items[0]);
279 $this->assertTrue(method_exists($grade_item, 'regrade_final_grades'));
280 $this->assertEqual(true, $grade_item->regrade_final_grades());
281 //TODO: add more tests
285 * Test the adjust_raw_grade method
287 function test_grade_item_adjust_raw_grade() {
288 $grade_item = new grade_item($this->grade_items[0]);
289 $this->assertTrue(method_exists($grade_item, 'adjust_raw_grade'));
290 $grade_raw = new stdClass();
292 $grade_raw->rawgrade = 40;
293 $grade_raw->grademax = 100;
294 $grade_raw->grademin = 0;
296 $grade_item->multfactor = 1;
297 $grade_item->plusfactor = 0;
298 $grade_item->grademax = 50;
299 $grade_item->grademin = 0;
301 $original_grade_raw = clone($grade_raw);
302 $original_grade_item = clone($grade_item);
304 $this->assertEqual(20, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
306 // Try a larger maximum grade
307 $grade_item->grademax = 150;
308 $grade_item->grademin = 0;
309 $this->assertEqual(60, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
311 // Try larger minimum grade
312 $grade_item->grademin = 50;
314 $this->assertEqual(90, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
316 // Rescaling from a small scale (0-50) to a larger scale (0-100)
317 $grade_raw->grademax = 50;
318 $grade_raw->grademin = 0;
319 $grade_item->grademax = 100;
320 $grade_item->grademin = 0;
322 $this->assertEqual(80, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
324 // Rescaling from a small scale (0-50) to a larger scale with offset (40-100)
325 $grade_item->grademax = 100;
326 $grade_item->grademin = 40;
328 $this->assertEqual(88, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
330 // Try multfactor and plusfactor
331 $grade_raw = clone($original_grade_raw);
332 $grade_item = clone($original_grade_item);
333 $grade_item->multfactor = 1.23;
334 $grade_item->plusfactor = 3;
336 $this->assertEqual(27.6, $grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax));
338 // Try multfactor below 0 and a negative plusfactor
339 $grade_raw = clone($original_grade_raw);
340 $grade_item = clone($original_grade_item);
341 $grade_item->multfactor = 0.23;
342 $grade_item->plusfactor = -3;
344 $this->assertEqual(round(1.6), round($grade_item->adjust_raw_grade($grade_raw->rawgrade, $grade_raw->grademin, $grade_raw->grademax)));
348 * Test locking of grade items
350 function test_grade_item_set_locked() {
351 $grade_item = new grade_item($this->grade_items[0]);
352 $this->assertTrue(method_exists($grade_item, 'set_locked'));
354 $grade = new grade_grade($grade_item->get_final(1));
355 $this->assertTrue(empty($grade_item->locked));
356 $this->assertTrue(empty($grade->locked));
358 $this->assertTrue($grade_item->set_locked(true));
359 $grade = new grade_grade($grade_item->get_final(1));
361 $this->assertFalse(empty($grade_item->locked));
362 $this->assertFalse(empty($grade->locked)); // individual grades should be locked too
364 $this->assertTrue($grade_item->set_locked(false));
365 $grade = new grade_grade($grade_item->get_final(1));
367 $this->assertTrue(empty($grade_item->locked));
368 $this->assertTrue(empty($grade->locked)); // individual grades should be unlocked too
371 function test_grade_item_is_locked() {
372 $grade_item = new grade_item($this->grade_items[0]);
373 $this->assertTrue(method_exists($grade_item, 'is_locked'));
375 $this->assertFalse($grade_item->is_locked());
376 $this->assertFalse($grade_item->is_locked(1));
377 $this->assertTrue($grade_item->set_locked(true));
378 $this->assertTrue($grade_item->is_locked());
379 $this->assertTrue($grade_item->is_locked(1));
383 * Test hiding of grade items
385 function test_grade_item_set_hidden() {
386 $grade_item = new grade_item($this->grade_items[0]);
387 $this->assertTrue(method_exists($grade_item, 'set_hidden'));
389 $grade = new grade_grade($grade_item->get_final(1));
390 $this->assertEqual(0, $grade_item->hidden);
391 $this->assertEqual(0, $grade->hidden);
393 $grade_item->set_hidden(666);
394 $grade = new grade_grade($grade_item->get_final(1));
396 $this->assertEqual(666, $grade_item->hidden);
397 $this->assertEqual(666, $grade->hidden);
400 function test_grade_item_is_hidden() {
401 $grade_item = new grade_item($this->grade_items[0]);
402 $this->assertTrue(method_exists($grade_item, 'is_hidden'));
404 $this->assertFalse($grade_item->is_hidden());
405 $this->assertFalse($grade_item->is_hidden(1));
407 $grade_item->set_hidden(1);
408 $this->assertTrue($grade_item->is_hidden());
409 $this->assertTrue($grade_item->is_hidden(1));
411 $grade_item->set_hidden(666);
412 $this->assertFalse($grade_item->is_hidden());
413 $this->assertFalse($grade_item->is_hidden(1));
415 $grade_item->set_hidden(time()+666);
416 $this->assertTrue($grade_item->is_hidden());
417 $this->assertTrue($grade_item->is_hidden(1));
420 function test_grade_item_is_category_item() {
421 $grade_item = new grade_item($this->grade_items[3]);
422 $this->assertTrue(method_exists($grade_item, 'is_category_item'));
423 $this->assertTrue($grade_item->is_category_item());
426 function test_grade_item_is_course_item() {
427 $grade_item = grade_item::fetch_course_item($this->courseid);
428 $this->assertTrue(method_exists($grade_item, 'is_course_item'));
429 $this->assertTrue($grade_item->is_course_item());
432 function test_grade_item_fetch_course_item() {
433 $grade_item = grade_item::fetch_course_item($this->courseid);
434 $this->assertTrue(method_exists($grade_item, 'fetch_course_item'));
435 $this->assertTrue($grade_item->itemtype, 'course');
438 function test_grade_item_depends_on() {
439 $grade_item = new grade_item($this->grade_items[1]);
441 // calculated grade dependency
442 $deps = $grade_item->depends_on();
443 sort($deps, SORT_NUMERIC); // for comparison
444 $this->assertEqual(array($this->grade_items[0]->id), $deps);
446 // simulate depends on returns none when locked
447 $grade_item->locked = time();
448 $grade_item->update();
449 $deps = $grade_item->depends_on();
450 sort($deps, SORT_NUMERIC); // for comparison
451 $this->assertEqual(array(), $deps);
453 // category dependency
454 $grade_item = new grade_item($this->grade_items[3]);
455 $deps = $grade_item->depends_on();
456 sort($deps, SORT_NUMERIC); // for comparison
457 $res = array($this->grade_items[4]->id, $this->grade_items[5]->id);
458 $this->assertEqual($res, $deps);
461 function test_grade_item_is_calculated() {
462 $grade_item = new grade_item($this->grade_items[1]);
463 $this->assertTrue(method_exists($grade_item, 'is_calculated'));
464 $grade_itemsource = new grade_item($this->grade_items[0]);
465 $normalizedformula = str_replace('[['.$grade_itemsource->idnumber.']]', '##gi'.$grade_itemsource->id.'##', $this->grade_items[1]->calculation);
467 $this->assertTrue($grade_item->is_calculated());
468 $this->assertEqual($normalizedformula, $grade_item->calculation);
471 function test_grade_item_set_calculation() {
472 $grade_item = new grade_item($this->grade_items[1]);
473 $this->assertTrue(method_exists($grade_item, 'set_calculation'));
474 $grade_itemsource = new grade_item($this->grade_items[0]);
476 $grade_item->set_calculation('=[['.$grade_itemsource->idnumber.']]');
478 $this->assertTrue(!empty($grade_item->needsupdate));
479 $this->assertEqual('=##gi'.$grade_itemsource->id.'##', $grade_item->calculation);
482 function test_grade_item_get_calculation() {
483 $grade_item = new grade_item($this->grade_items[1]);
484 $this->assertTrue(method_exists($grade_item, 'get_calculation'));
485 $grade_itemsource = new grade_item($this->grade_items[0]);
487 $denormalizedformula = str_replace('##gi'.$grade_itemsource->id.'##', '[['.$grade_itemsource->idnumber.']]', $this->grade_items[1]->calculation);
489 $formula = $grade_item->get_calculation();
490 $this->assertTrue(!empty($grade_item->needsupdate));
491 $this->assertEqual($denormalizedformula, $formula);
494 function test_grade_item_compute() {
495 $grade_item = new grade_item($this->grade_items[1]);
496 $this->assertTrue(method_exists($grade_item, 'compute'));
498 $grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[3]->id));
499 $grade_grade->delete();
500 $grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[4]->id));
501 $grade_grade->delete();
502 $grade_grade = grade_grade::fetch(array('id'=>$this->grade_grades[5]->id));
503 $grade_grade->delete();
505 $grade_item->compute();
507 $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[3]->userid, 'itemid'=>$this->grade_grades[3]->itemid));
508 $this->assertEqual($this->grade_grades[3]->finalgrade, $grade_grade->finalgrade);
509 $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[4]->userid, 'itemid'=>$this->grade_grades[4]->itemid));
510 $this->assertEqual($this->grade_grades[4]->finalgrade, $grade_grade->finalgrade);
511 $grade_grade = grade_grade::fetch(array('userid'=>$this->grade_grades[5]->userid, 'itemid'=>$this->grade_grades[5]->itemid));
512 $this->assertEqual($this->grade_grades[5]->finalgrade, $grade_grade->finalgrade);