From eb32c6ca84926b7d76f8336151d9e6052877879e Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Thu, 4 Sep 2008 13:25:16 +0000 Subject: [PATCH] MDL-15666 minor fixes to assignment portfolio callers, additional param for generator (assignment type), and unit tests for sha1 --- admin/generator.php | 55 +++++++++++-------- mod/assignment/lib.php | 7 ++- .../test_assignment_portfolio_callers.php | 51 +++++++++++++++++ .../type/online/assignment.class.php | 8 +-- 4 files changed, 92 insertions(+), 29 deletions(-) create mode 100644 mod/assignment/simpletest/test_assignment_portfolio_callers.php diff --git a/admin/generator.php b/admin/generator.php index 1b513bb326..105c3da7cb 100755 --- a/admin/generator.php +++ b/admin/generator.php @@ -81,6 +81,8 @@ class generator { array('short'=>'mods', 'long' => 'modules_list', 'help' => 'The list of modules you want to generate', 'default' => $this->modules_list, 'type' => 'mod1,mod2...'), + array('short'=>'at', 'long' => 'assignment_type', + 'help' => 'The specific type of assignment you want to generate. Defaults to random', 'default' => 'random'), array('short'=>'ag', 'long' => 'assignment_grades', 'help' => 'Generate random grades for each student/assignment tuple', 'default' => true), array('short'=>'qg', 'long' => 'quiz_grades', @@ -269,7 +271,7 @@ class generator { public function generate_courses() { global $DB; - $this->verbose("Generating {$this->get('number_of_courses')} courses..."); + $this->verbose("Generating " . $this->get('number_of_courses')." courses..."); $base_course = new stdClass(); $next_course_id = $DB->get_field_sql("SELECT MAX(id) FROM {course}") + 1; @@ -288,7 +290,6 @@ class generator { $newcourse->fullname = "Test course $next_course_id"; $newcourse->shortname = "Test $next_course_id"; $newcourse->idnumber = $this->get('data_prefix') . $next_course_id; - if (!$course = create_course($newcourse)) { $this->verbose("Error inserting a new course in the database!"); if (!$this->get('ignore_errors')) { @@ -394,7 +395,12 @@ class generator { switch ($moduledata->name) { case 'assignment': $module->description = $description; - $module->assignmenttype = $assignment_types[rand(0, count($assignment_types) - 1)]; + if ($this->get('assignment_type') == 'random') { + $module->assignmenttype = $assignment_types[rand(0, count($assignment_types) - 1)]; + } else { + $module->assignmenttype = $this->get('assignment_type'); + } + $module->timedue = mktime() + 89487321; $module->grade = rand(50,100); break; @@ -617,7 +623,9 @@ class generator { if (!quiz_add_quiz_question($questions[$quiz->course][$random]->id, $quiz)) { // Could not add question to quiz!! report error - echo "WARNING: Could not add question id $random to quiz id $quiz->id{$this->eolchar}"; + if (!$this->get('quiet')) { + echo "WARNING: Could not add question id $random to quiz id $quiz->id{$this->eolchar}"; + } } else { $this->verbose("Adding question id $random to quiz id $quiz->id."); $questions_added[] = $random; @@ -762,33 +770,36 @@ class generator { } public function generate_grades($course_users, $courses, $modules) { - global $CFG, $DB; + global $CFG, $DB, $USER; /** * ASSIGNMENT GRADES GENERATION */ if ($this->get('assignment_grades') && isset($modules['assignment'])) { $grades_count = 0; - foreach ($course_users as $userid => $courses) { - foreach ($modules['assignment'] as $assignment) { - if (in_array($assignment->course, $courses)) { - $maxgrade = $assignment->grade; - $random_grade = rand(0, $maxgrade); - $grade = new stdClass(); - $grade->assignment = $assignment->id; - $grade->userid = $userid; - $grade->grade = $random_grade; - $grade->rawgrade = $random_grade; - $grade->teacher = $USER->id; - $DB->insert_record('assignment_submissions', $grade); - grade_update('mod/assignment', $courseid, 'mod', 'assignment', $assignment->id, 0, $grade); - $this->verbose("A grade ($random_grade) has been given to user $userid for assignment $assignment->id"); - $grades_count++; + foreach ($course_users as $courseid => $userid_array) { + foreach ($userid_array as $userid) { + foreach ($modules['assignment'] as $assignment) { + if (in_array($assignment->course, $courses)) { + $maxgrade = $assignment->grade; + $random_grade = rand(0, $maxgrade); + $grade = new stdClass(); + $grade->assignment = $assignment->id; + $grade->userid = $userid; + $grade->grade = $random_grade; + $grade->rawgrade = $random_grade; + $grade->teacher = $USER->id; + $DB->insert_record('assignment_submissions', $grade); + grade_update('mod/assignment', $assignment->course, 'mod', 'assignment', $assignment->id, 0, $grade); + $this->verbose("A grade ($random_grade) has been given to user $userid " + . "for assignment $assignment->id"); + $grades_count++; + } } } } if ($grades_count > 0) { - echo "$grades_count assignment grades have been generated.{$this->eolchar}"; + $this->verbose("$grades_count assignment grades have been generated.{$this->eolchar}"); } } @@ -814,7 +825,7 @@ class generator { } } } - if ($grades_count > 0) { + if ($grades_count > 0 && !$this->get('quiet')) { echo "$grades_count quiz grades have been generated.{$this->eolchar}"; } } diff --git a/mod/assignment/lib.php b/mod/assignment/lib.php index 787dcf005b..566ee4c92f 100644 --- a/mod/assignment/lib.php +++ b/mod/assignment/lib.php @@ -3174,7 +3174,7 @@ class assignment_portfolio_caller extends portfolio_module_caller_base { public function prepare_package() { global $CFG; if (is_callable(array($this->assignment, 'portfolio_prepare_package'))) { - return $this->assignment->portfolio_prepare_package($this->exporter); + return $this->assignment->portfolio_prepare_package($this->exporter, $this->user->id); } $fs = get_file_storage(); $status = true; @@ -3189,7 +3189,7 @@ class assignment_portfolio_caller extends portfolio_module_caller_base { public function get_sha1() { global $CFG; if (is_callable(array($this->assignment, 'portfolio_get_sha1'))) { - return $this->assignment->portfolio_get_sha1(); + return $this->assignment->portfolio_get_sha1($this->user->id); } // default ... @@ -3198,7 +3198,8 @@ class assignment_portfolio_caller extends portfolio_module_caller_base { if ($this->file) { return $fs->get_file_by_id($this->file)->get_contenthash(); } - if ($files = $fs->get_area_files($this->assignment->context->id, 'assignment_submission', $this->user->id, '', false)) { + if ($files = $fs->get_area_files($this->assignment->context->id, + 'assignment_submission', $this->user->id, '', false)) { $sha1s = array(); foreach ($files as $file) { $sha1s[] = $file->get_contenthash(); diff --git a/mod/assignment/simpletest/test_assignment_portfolio_callers.php b/mod/assignment/simpletest/test_assignment_portfolio_callers.php new file mode 100644 index 0000000000..f02e414346 --- /dev/null +++ b/mod/assignment/simpletest/test_assignment_portfolio_callers.php @@ -0,0 +1,51 @@ +libdir.'/simpletest/testportfoliolib.php'); +require_once($CFG->dirroot.'/mod/assignment/lib.php'); +require_once($CFG->dirroot.'/admin/generator.php'); + +Mock::generate('assignment_portfolio_caller', 'mock_caller'); +Mock::generate('portfolio_exporter', 'mock_exporter'); + +class testAssignmentPortfolioCallers extends portfoliolib_test { + public $module_type = 'assignment'; + public $modules = array(); + public $entries = array(); + public $caller; + + public function setUp() { + global $DB, $USER; + + parent::setUp(); + + $settings = array('quiet' => 1, 'database_prefix' => 'tst_', 'pre_cleanup' => 1, + 'modules_list' => array($this->module_type), 'assignment_grades' => true, + 'assignment_type' => 'online', + 'number_of_students' => 5, 'students_per_course' => 5, 'number_of_sections' => 1, + 'number_of_modules' => 1, 'questions_per_course' => 0); + generator_generate_data($settings); + + $this->modules = $DB->get_records($this->module_type); + $first_module = reset($this->modules); + $cm = get_coursemodule_from_instance($this->module_type, $first_module->id); + $submissions = $DB->get_records('assignment_submissions', array('assignment' => $first_module->id)); + $first_submission = reset($submissions); + + $callbackargs = array('assignmentid' => $cm->id, 'userid' => $USER->id); + $this->caller = new assignment_portfolio_caller($callbackargs); + $this->caller->set('exporter', new mock_exporter()); + $user = $DB->get_record('user', array('id' => $first_submission->userid)); + $this->caller->set('user', $user); + } + + public function tearDown() { + parent::tearDown(); + } + + public function test_caller_sha1() { + $sha1 = $this->caller->get_sha1(); + $this->caller->prepare_package(); + $this->assertEqual($sha1, $this->caller->get_sha1()); + } + +} +?> diff --git a/mod/assignment/type/online/assignment.class.php b/mod/assignment/type/online/assignment.class.php index 6d726ac147..be8b87f2b6 100644 --- a/mod/assignment/type/online/assignment.class.php +++ b/mod/assignment/type/online/assignment.class.php @@ -270,13 +270,13 @@ class assignment_online extends assignment_base { return true; } - function portfolio_get_sha1() { - $submission = $this->get_submission(); + function portfolio_get_sha1($userid=0) { + $submission = $this->get_submission($userid); return sha1(format_text($submission->data1, $submission->data2)); } - function portfolio_prepare_package($exporter) { - $submission = $this->get_submission(); + function portfolio_prepare_package($exporter, $userid=0) { + $submission = $this->get_submission($userid); return $exporter->write_new_file(format_text($submission->data1, $submission->data2), 'assignment.html'); } } -- 2.39.5