Exemplo n.º 1
0
     $scale_id = key($scale);
 } else {
     if (!has_capability('moodle/course:managescales', $context)) {
         echo $OUTPUT->box(get_string('importskippednomanagescale', 'grades', $csv_data[$imported_headers['outcome_shortname']]));
         continue;
     } else {
         // scale doesn't exists : create it.
         $scale_data = array('name' => $csv_data[$imported_headers['scale_name']], 'scale' => $csv_data[$imported_headers['scale_items']], 'description' => $csv_data[$imported_headers['scale_description']], 'userid' => $USER->id);
         if ($local_scope) {
             $scale_data['courseid'] = $courseid;
         } else {
             $scale_data['courseid'] = 0;
             // 'global' : scale use '0', outcomes use null
         }
         $scale = new grade_scale($scale_data);
         $scale_id = $scale->insert();
     }
 }
 // add outcome
 $outcome_data = array('shortname' => $csv_data[$imported_headers['outcome_shortname']], 'fullname' => $csv_data[$imported_headers['outcome_name']], 'scaleid' => $scale_id, 'description' => $csv_data[$imported_headers['outcome_description']], 'usermodified' => $USER->id);
 if ($local_scope) {
     $outcome_data['courseid'] = $courseid;
 } else {
     $outcome_data['courseid'] = null;
     // 'global' : scale use '0', outcomes use null
 }
 $outcome = new grade_outcome($outcome_data);
 $outcome_id = $outcome->insert();
 $outcome_success_strings = new StdClass();
 $outcome_success_strings->name = $outcome_data['fullname'];
 $outcome_success_strings->id = $outcome_id;
 /**
  * Load initial test information
  *
  * @param  string $assignmentname   Assignment name
  * @param  int $student1rawgrade    Student 1 grade
  * @param  int $student2rawgrade    Student 2 grade
  * @return array                    Array of vars with test information
  */
 protected function load_test_data($assignmentname, $student1rawgrade, $student2rawgrade)
 {
     global $DB;
     // Adds a course, a teacher, 2 students, an assignment and grades for the students.
     $course = $this->getDataGenerator()->create_course();
     $coursecontext = context_course::instance($course->id);
     $studentrole = $DB->get_record('role', array('shortname' => 'student'));
     $student1 = $this->getDataGenerator()->create_user();
     $this->getDataGenerator()->enrol_user($student1->id, $course->id, $studentrole->id);
     $student2 = $this->getDataGenerator()->create_user();
     $this->getDataGenerator()->enrol_user($student2->id, $course->id, $studentrole->id);
     $teacherrole = $DB->get_record('role', array('shortname' => 'editingteacher'));
     $teacher = $this->getDataGenerator()->create_user();
     $this->getDataGenerator()->enrol_user($teacher->id, $course->id, $teacherrole->id);
     $parent = $this->getDataGenerator()->create_user();
     $this->setUser($parent);
     $student1context = context_user::instance($student1->id);
     // Creates a new role, gives it the capability and gives $USER that role.
     $parentroleid = $this->assignUserCapability('moodle/grade:viewall', $student1context->id);
     // Enrol the user in the course using the new role.
     $this->getDataGenerator()->enrol_user($parent->id, $course->id, $parentroleid);
     $assignment = $this->getDataGenerator()->create_module('assign', array('name' => $assignmentname, 'course' => $course->id));
     $modcontext = get_coursemodule_from_instance('assign', $assignment->id, $course->id);
     $assignment->cmidnumber = $modcontext->id;
     $student1grade = array('userid' => $student1->id, 'rawgrade' => $student1rawgrade);
     $student2grade = array('userid' => $student2->id, 'rawgrade' => $student2rawgrade);
     $studentgrades = array($student1->id => $student1grade, $student2->id => $student2grade);
     assign_grade_item_update($assignment, $studentgrades);
     // Insert a custom grade scale to be used by an outcome.
     $gradescale = new grade_scale();
     $gradescale->name = 'unittestscale3';
     $gradescale->courseid = $course->id;
     $gradescale->userid = 0;
     $gradescale->scale = 'Distinction, Very Good, Good, Pass, Fail';
     $gradescale->description = 'This scale is used to mark standard assignments.';
     $gradescale->insert();
     // Insert an outcome.
     $data = new stdClass();
     $data->courseid = $course->id;
     $data->fullname = 'Team work';
     $data->shortname = 'Team work';
     $data->scaleid = $gradescale->id;
     $outcome = new grade_outcome($data, false);
     $outcome->insert();
     $outcomegradeitem = new grade_item();
     $outcomegradeitem->itemname = $outcome->shortname;
     $outcomegradeitem->itemtype = 'mod';
     $outcomegradeitem->itemmodule = 'assign';
     $outcomegradeitem->iteminstance = $assignment->id;
     $outcomegradeitem->outcomeid = $outcome->id;
     $outcomegradeitem->cmid = 0;
     $outcomegradeitem->courseid = $course->id;
     $outcomegradeitem->aggregationcoef = 0;
     $outcomegradeitem->itemnumber = 1;
     // The activity's original grade item will be 0.
     $outcomegradeitem->gradetype = GRADE_TYPE_SCALE;
     $outcomegradeitem->scaleid = $outcome->scaleid;
     // This next two values for testing that returns parameters are correcly formatted.
     $outcomegradeitem->set_locked(true);
     $outcomegradeitem->hidden = '';
     $outcomegradeitem->insert();
     $assignmentgradeitem = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'assign', 'iteminstance' => $assignment->id, 'itemnumber' => 0, 'courseid' => $course->id));
     $outcomegradeitem->set_parent($assignmentgradeitem->categoryid);
     $outcomegradeitem->move_after_sortorder($assignmentgradeitem->sortorder);
     return array($course, $assignment, $student1, $student2, $teacher, $parent);
 }
Exemplo n.º 3
0
 protected function sub_test_scale_one_item()
 {
     $params = new stdClass();
     $params->name = 'unittestscale1i';
     $params->courseid = $this->course->id;
     $params->userid = $this->userid;
     $params->scale = 'Like';
     $params->description = 'This scale is used to like something.';
     $params->timemodified = time();
     $scale = new grade_scale($params, false);
     $scale->load_items();
     $this->assertCount(1, $scale->scale_items);
     $this->assertSame(array('Like'), $scale->scale_items);
     $this->assertSame('Like', $scale->compact_items());
     $scale->insert();
     // Manual grade item with 1 item scale.
     $grade_item = new stdClass();
     $grade_item->courseid = $this->course->id;
     $grade_item->categoryid = $this->grade_categories[0]->id;
     $grade_item->itemname = 'manual grade_item scale_1';
     $grade_item->itemtype = 'manual';
     $grade_item->itemnumber = 0;
     $grade_item->needsupdate = false;
     $grade_item->gradetype = GRADE_TYPE_SCALE;
     $grade_item->scaleid = $scale->id;
     $grade_item->iteminfo = 'Manual grade item used for unit testing';
     $grade_item->timecreated = time();
     $grade_item->timemodified = time();
     $grade_item = new grade_item($grade_item);
     $grade_item->insert();
     $this->assertNotEmpty($grade_item->id);
     $this->assertEquals(1, $grade_item->grademin);
     $this->assertEquals(1, $grade_item->grademax);
 }
Exemplo n.º 4
0
 /**
  * Test get_course_module
  */
 public function test_get_course_module()
 {
     global $DB;
     $this->resetAfterTest(true);
     $this->setAdminUser();
     $course = self::getDataGenerator()->create_course();
     $record = array('course' => $course->id, 'name' => 'First Assignment');
     $options = array('idnumber' => 'ABC', 'visible' => 0);
     // Hidden activity.
     $assign = self::getDataGenerator()->create_module('assign', $record, $options);
     $outcomescale = 'Distinction, Very Good, Good, Pass, Fail';
     // Insert a custom grade scale to be used by an outcome.
     $gradescale = new grade_scale();
     $gradescale->name = 'gettcoursemodulescale';
     $gradescale->courseid = $course->id;
     $gradescale->userid = 0;
     $gradescale->scale = $outcomescale;
     $gradescale->description = 'This scale is used to mark standard assignments.';
     $gradescale->insert();
     // Insert an outcome.
     $data = new stdClass();
     $data->courseid = $course->id;
     $data->fullname = 'Team work';
     $data->shortname = 'Team work';
     $data->scaleid = $gradescale->id;
     $outcome = new grade_outcome($data, false);
     $outcome->insert();
     $outcomegradeitem = new grade_item();
     $outcomegradeitem->itemname = $outcome->shortname;
     $outcomegradeitem->itemtype = 'mod';
     $outcomegradeitem->itemmodule = 'assign';
     $outcomegradeitem->iteminstance = $assign->id;
     $outcomegradeitem->outcomeid = $outcome->id;
     $outcomegradeitem->cmid = 0;
     $outcomegradeitem->courseid = $course->id;
     $outcomegradeitem->aggregationcoef = 0;
     $outcomegradeitem->itemnumber = 1;
     // The activity's original grade item will be 0.
     $outcomegradeitem->gradetype = GRADE_TYPE_SCALE;
     $outcomegradeitem->scaleid = $outcome->scaleid;
     $outcomegradeitem->insert();
     $assignmentgradeitem = grade_item::fetch(array('itemtype' => 'mod', 'itemmodule' => 'assign', 'iteminstance' => $assign->id, 'itemnumber' => 0, 'courseid' => $course->id));
     $outcomegradeitem->set_parent($assignmentgradeitem->categoryid);
     $outcomegradeitem->move_after_sortorder($assignmentgradeitem->sortorder);
     // Test admin user can see the complete hidden activity.
     $result = core_course_external::get_course_module($assign->cmid);
     $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
     $this->assertCount(0, $result['warnings']);
     // Test we retrieve all the fields.
     $this->assertCount(27, $result['cm']);
     $this->assertEquals($record['name'], $result['cm']['name']);
     $this->assertEquals($options['idnumber'], $result['cm']['idnumber']);
     $this->assertEquals(100, $result['cm']['grade']);
     $this->assertEquals(0.0, $result['cm']['gradepass']);
     $this->assertEquals('submissions', $result['cm']['advancedgrading'][0]['area']);
     $this->assertEmpty($result['cm']['advancedgrading'][0]['method']);
     $this->assertEquals($outcomescale, $result['cm']['outcomes'][0]['scale']);
     $student = $this->getDataGenerator()->create_user();
     $studentrole = $DB->get_record('role', array('shortname' => 'student'));
     self::getDataGenerator()->enrol_user($student->id, $course->id, $studentrole->id);
     $this->setUser($student);
     // The user shouldn't be able to see the activity.
     try {
         core_course_external::get_course_module($assign->cmid);
         $this->fail('Exception expected due to invalid permissions.');
     } catch (moodle_exception $e) {
         $this->assertEquals('requireloginerror', $e->errorcode);
     }
     // Make module visible.
     set_coursemodule_visible($assign->cmid, 1);
     // Test student user.
     $result = core_course_external::get_course_module($assign->cmid);
     $result = external_api::clean_returnvalue(core_course_external::get_course_module_returns(), $result);
     $this->assertCount(0, $result['warnings']);
     // Test we retrieve only the few files we can see.
     $this->assertCount(11, $result['cm']);
     $this->assertEquals($assign->cmid, $result['cm']['id']);
     $this->assertEquals($course->id, $result['cm']['course']);
     $this->assertEquals('assign', $result['cm']['modname']);
     $this->assertEquals($assign->id, $result['cm']['instance']);
 }
Exemplo n.º 5
0
 /**
  * Validate that the version 1 plugin deletes appropriate associations when
  * deleting a course
  */
 public function test_version1importdeletecoursedeletesassociations()
 {
     global $DB, $CFG, $USER;
     require_once $CFG->dirroot . '/user/lib.php';
     require_once $CFG->dirroot . '/lib/gradelib.php';
     require_once $CFG->dirroot . '/group/lib.php';
     require_once $CFG->dirroot . '/lib/conditionlib.php';
     require_once $CFG->dirroot . '/lib/enrollib.php';
     require_once $CFG->dirroot . '/tag/lib.php';
     require_once $CFG->dirroot . '/lib/questionlib.php';
     // Setup.
     $initialnumcontexts = $DB->count_records('context', array('contextlevel' => CONTEXT_COURSE));
     $DB->delete_records('block_instances');
     // Set up the course with one section, including default blocks.
     set_config('defaultblocks_topics', 'search_forums');
     set_config('maxsections', 10, 'moodlecourse');
     $this->run_core_course_import(array('shortname' => 'deleteassociationsshortname', 'numsections' => 1));
     // Create a user record.
     $record = new stdClass();
     $record->username = '******';
     $record->password = '******';
     $userid = user_create_user($record);
     // Create a course-level role.
     $courseid = $DB->get_field('course', 'id', array('shortname' => 'deleteassociationsshortname'));
     $coursecontext = context_course::instance($courseid);
     $roleid = create_role('deleterole', 'deleterole', 'deleterole');
     set_role_contextlevels($roleid, array(CONTEXT_COURSE));
     $enrol = new stdClass();
     $enrol->enrol = 'manual';
     $enrol->courseid = $courseid;
     $enrol->status = ENROL_INSTANCE_ENABLED;
     if (!$DB->record_exists('enrol', (array) $enrol)) {
         $DB->insert_record('enrol', $enrol);
     }
     // Assign the user to the course-level role.
     enrol_try_internal_enrol($courseid, $userid, $roleid);
     // Create a grade item.
     $gradeitem = new grade_item(array('courseid' => $courseid, 'itemtype' => 'manual', 'itemname' => 'testitem'), false);
     $gradeitem->insert();
     $gradegrade = new grade_grade(array('itemid' => $gradeitem->id, 'userid' => $userid), false);
     // Assign the user a grade.
     $gradegrade->insert();
     // Create a grade outcome.
     $gradeoutcome = new grade_outcome(array('courseid' => $courseid, 'shortname' => 'bogusshortname', 'fullname' => 'bogusfullname'));
     $gradeoutcome->insert();
     // Create a grade scale.
     $gradescale = new grade_scale(array('courseid' => $courseid, 'name' => 'bogusname', 'userid' => $userid, 'scale' => 'bogusscale', 'description' => 'bogusdescription'));
     $gradescale->insert();
     // Set a grade setting value.
     grade_set_setting($courseid, 'bogus', 'bogus');
     // Set up a grade letter.
     $gradeletter = new stdClass();
     $gradeletter->contextid = $coursecontext->id;
     $gradeletter->lowerboundary = 80;
     $gradeletter->letter = 'A';
     $DB->insert_record('grade_letters', $gradeletter);
     // Set up a forum instance.
     $forum = new stdClass();
     $forum->course = $courseid;
     $forum->intro = 'intro';
     $forum->id = $DB->insert_record('forum', $forum);
     // Add it as a course module.
     $forum->module = $DB->get_field('modules', 'id', array('name' => 'forum'));
     $forum->instance = $forum->id;
     $cmid = add_course_module($forum);
     // Set up a completion record.
     $completion = new stdClass();
     $completion->coursemoduleid = $cmid;
     $completion->completionstate = 0;
     $completion->userid = 9999;
     $completion->timemodified = time();
     $DB->insert_record('course_modules_completion', $completion);
     // Set up a completion condition.
     $forum->id = $cmid;
     $ci = new condition_info($forum, CONDITION_MISSING_EVERYTHING, false);
     $ci->add_completion_condition($cmid, COMPLETION_ENABLED);
     // Set the blocks position.
     $instances = $DB->get_records('block_instances', array('parentcontextid' => $coursecontext->id));
     $page = new stdClass();
     $page->context = $coursecontext;
     $page->pagetype = 'course-view-*';
     $page->subpage = false;
     foreach ($instances as $instance) {
         blocks_set_visibility($instance, $page, 1);
     }
     // Create a group.
     $group = new stdClass();
     $group->name = 'testgroup';
     $group->courseid = $courseid;
     $groupid = groups_create_group($group);
     // Add the user to the group.
     groups_add_member($groupid, $userid);
     // Create a grouping containing our group.
     $grouping = new stdClass();
     $grouping->name = 'testgrouping';
     $grouping->courseid = $courseid;
     $groupingid = groups_create_grouping($grouping);
     groups_assign_grouping($groupingid, $groupid);
     // Set up a user tag.
     tag_set('course', $courseid, array('testtag'));
     // Add a course-level log.
     add_to_log($courseid, 'bogus', 'bogus');
     // Set up the default course question category.
     $newcategory = question_make_default_categories(array($coursecontext));
     // Create a test question.
     $question = new stdClass();
     $question->qtype = 'truefalse';
     $form = new stdClass();
     $form->category = $newcategory->id;
     $form->name = 'testquestion';
     $form->correctanswer = 1;
     $form->feedbacktrue = array('text' => 'bogustext', 'format' => FORMAT_HTML);
     $form->feedbackfalse = array('text' => 'bogustext', 'format' => FORMAT_HTML);
     $question = question_bank::get_qtype('truefalse')->save_question($question, $form);
     if (function_exists('course_set_display')) {
         // Set a "course display" setting.
         course_set_display($courseid, 1);
     }
     // Make a bogus backup record.
     $backupcourse = new stdClass();
     $backupcourse->courseid = $courseid;
     $DB->insert_record('backup_courses', $backupcourse);
     // Add a user lastaccess record.
     $lastaccess = new stdClass();
     $lastaccess->userid = $userid;
     $lastaccess->courseid = $courseid;
     $DB->insert_record('user_lastaccess', $lastaccess);
     // Make a bogus backup log record.
     $log = new stdClass();
     $log->backupid = $courseid;
     $log->timecreated = time();
     $log->loglevel = 1;
     $log->message = 'bogus';
     $DB->insert_record('backup_logs', $log);
     // Get initial counts.
     $initialnumcourse = $DB->count_records('course');
     $initialnumroleassignments = $DB->count_records('role_assignments');
     $initialnumuserenrolments = $DB->count_records('user_enrolments');
     $initialnumgradeitems = $DB->count_records('grade_items');
     $initialnumgradegrades = $DB->count_records('grade_grades');
     $initialnumgradeoutcomes = $DB->count_records('grade_outcomes');
     $initialnumgradeoutcomescourses = $DB->count_records('grade_outcomes_courses');
     $initialnumscale = $DB->count_records('scale');
     $initialnumgradesettings = $DB->count_records('grade_settings');
     $initialnumgradeletters = $DB->count_records('grade_letters');
     $initialnumforum = $DB->count_records('forum');
     $initialnumcoursemodules = $DB->count_records('course_modules');
     $initialnumcoursemodulescompletion = $DB->count_records('course_modules_completion');
     $initialnumcoursemodulesavailability = $DB->count_records('course_modules_availability');
     $initialnumblockinstances = $DB->count_records('block_instances');
     $initialnumblockpositions = $DB->count_records('block_positions');
     $initialnumgroups = $DB->count_records('groups');
     $initialnumgroupsmembers = $DB->count_records('groups_members');
     $initialnumgroupings = $DB->count_records('groupings');
     $initialnumgroupingsgroups = $DB->count_records('groupings_groups');
     $initialnumtaginstance = $DB->count_records('tag_instance');
     $initialnumcoursesections = $DB->count_records('course_sections');
     $initialnumquestioncategories = $DB->count_records('question_categories');
     $initialnumquestion = $DB->count_records('question');
     if (self::$coursedisplay) {
         $initialnumcoursedisplay = $DB->count_records('course_display');
     }
     $initialnumbackupcourses = $DB->count_records('backup_courses');
     $initialnumuserlastaccess = $DB->count_records('user_lastaccess');
     $initialnumbackuplogs = $DB->count_records('backup_logs');
     // Delete the course.
     $data = array('action' => 'delete', 'shortname' => 'deleteassociationsshortname');
     $this->run_core_course_import($data, false);
     // Validate the result.
     $this->assertEquals($DB->count_records('course'), $initialnumcourse - 1);
     $this->assertEquals($DB->count_records('role_assignments'), $initialnumroleassignments - 1);
     $this->assertEquals($DB->count_records('user_enrolments'), $initialnumuserenrolments - 1);
     $this->assertEquals($DB->count_records('grade_items'), $initialnumgradeitems - 2);
     $this->assertEquals($DB->count_records('grade_grades'), $initialnumgradegrades - 1);
     $this->assertEquals($DB->count_records('grade_outcomes'), $initialnumgradeoutcomes - 1);
     $this->assertEquals($DB->count_records('grade_outcomes_courses'), $initialnumgradeoutcomescourses - 1);
     $this->assertEquals($DB->count_records('scale'), $initialnumscale - 1);
     $this->assertEquals($DB->count_records('grade_settings'), $initialnumgradesettings - 1);
     $this->assertEquals($DB->count_records('grade_letters'), $initialnumgradeletters - 1);
     $this->assertEquals($DB->count_records('forum'), $initialnumforum - 1);
     $this->assertEquals($DB->count_records('course_modules'), $initialnumcoursemodules - 1);
     /*
      Uncomment the two lines below when this fix is available: http://tracker.moodle.org/browse/MDL-32988
      $this->assertEquals($DB->count_records('course_modules_completion'), $initialnumcourse_modules_completion - 1);
      $this->assertEquals($DB->count_records('course_modules_availability'), $initialnumcourse_modules_availability - 1);
     */
     $this->assertEquals($initialnumblockinstances - 4, $DB->count_records('block_instances'));
     $this->assertEquals($DB->count_records('block_positions'), 0);
     $this->assertEquals($DB->count_records('groups'), $initialnumgroups - 1);
     $this->assertEquals($DB->count_records('groups_members'), $initialnumgroupsmembers - 1);
     $this->assertEquals($DB->count_records('groupings'), $initialnumgroupings - 1);
     $this->assertEquals($DB->count_records('groupings_groups'), $initialnumgroupingsgroups - 1);
     $this->assertEquals($DB->count_records('log', array('course' => $courseid)), 0);
     $this->assertEquals($DB->count_records('tag_instance'), $initialnumtaginstance - 1);
     $this->assertEquals($DB->count_records('course_sections'), $initialnumcoursesections - 1);
     $this->assertEquals($DB->count_records('question_categories'), $initialnumquestioncategories - 1);
     $this->assertEquals($DB->count_records('question'), $initialnumquestion - 1);
     if (self::$coursedisplay) {
         $this->assertEquals($DB->count_records('course_display'), $initialnumcoursedisplay - 1);
     }
     $this->assertEquals($DB->count_records('backup_courses'), $initialnumbackupcourses - 1);
     $this->assertEquals($DB->count_records('user_lastaccess'), $initialnumuserlastaccess - 1);
 }