本文整理汇总了PHP中grade_item::insert方法的典型用法代码示例。如果您正苦于以下问题:PHP grade_item::insert方法的具体用法?PHP grade_item::insert怎么用?PHP grade_item::insert使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类grade_item
的用法示例。
在下文中一共展示了grade_item::insert方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_update_score
/**
* Tests update score without sending item details.
* Grade item should not be created without item name.
*
* @return void
*/
public function test_update_score()
{
global $DB;
$this->set_user('admin');
$this->assertEquals(0, $DB->count_records('grade_items'));
// Add an mhaairs item directly.
$iteminstance = 24993;
$itemparams = array('courseid' => $this->course->id, 'itemtype' => 'manual', 'itemmodule' => 'mhaairs', 'iteminstance' => $iteminstance, 'itemname' => 'MH Assignment');
$gitem = new \grade_item($itemparams, false);
$gitem->insert('mhaairs');
$this->assertEquals(2, $DB->count_records('grade_items'));
$service = 'block_mhaairs_gradebookservice_external::update_grade';
$grades = array('userid' => 'student1', 'rawgrade' => 93);
$gradesjson = urlencode(json_encode($grades));
$servicedata = array();
$servicedata['source'] = 'mhaairs';
$servicedata['courseid'] = 'tc1';
$servicedata['itemtype'] = 'manual';
$servicedata['itemmodule'] = 'mhaairs';
$servicedata['iteminstance'] = 111;
$servicedata['itemnumber'] = 0;
$servicedata['grades'] = $gradesjson;
$servicedata['itemdetails'] = null;
// Send score via service without details, item by instance doesn't exist.
$result = call_user_func_array($service, $servicedata);
$this->assertEquals(GRADE_UPDATE_FAILED, $result);
$this->assertEquals(2, $DB->count_records('grade_items'));
// Set the existing item instance.
$servicedata['iteminstance'] = $iteminstance;
// Send score via service without details, item by instance exists.
$result = call_user_func_array($service, $servicedata);
$this->assertEquals(GRADE_UPDATE_OK, $result);
$this->assertEquals(2, $DB->count_records('grade_items'));
$this->assertEquals(1, $DB->count_records('grade_grades'));
$usergrade = $DB->get_field('grade_grades', 'finalgrade', array('userid' => $this->student1->id));
$this->assertEquals(93, $usergrade);
// Set typical item details for score update.
$itemdetails = array('courseid' => '3', 'idnumber' => '111', 'identity_type' => null, 'needsupdate' => 1, 'useexisting' => 0);
$itemdetailsjson = urlencode(json_encode($grades));
$servicedata['itemdetails'] = $itemdetailsjson;
$grades = array('userid' => 'student1', 'rawgrade' => 94);
$gradesjson = urlencode(json_encode($grades));
$servicedata['grades'] = $gradesjson;
// Send score via service to item with details.
$result = call_user_func_array($service, $servicedata);
$this->assertEquals(GRADE_UPDATE_OK, $result);
$this->assertEquals(2, $DB->count_records('grade_items'));
$this->assertEquals(1, $DB->count_records('grade_grades'));
$usergrade = $DB->get_field('grade_grades', 'finalgrade', array('userid' => $this->student1->id));
$this->assertEquals(94, $usergrade);
}
示例2: test_get_grade
/**
* Tests the gradebookservice get grade service.
*
* @return void
*/
public function test_get_grade()
{
global $DB;
$callback = 'block_mhaairs_gradebookservice_external::get_grade';
$this->set_user('admin');
// Add mhaairs grade item directly.
$params = array('courseid' => $this->course->id, 'itemtype' => 'manual', 'itemmodule' => 'mhaairs', 'iteminstance' => 101, 'itemname' => 'MH Assignment');
$gitem = new \grade_item($params, false);
$gitem->insert('mhaairs');
// Add user grade directly.
$params = array('itemid' => $gitem->id, 'userid' => $this->student1->id, 'finalgrade' => '95');
$ggrade = new \grade_grade($params, false);
$ggrade->insert('mhaairs');
// Service params.
$serviceparams = array('source' => 'mhaairs', 'courseid' => $this->course->id, 'itemtype' => 'manual', 'itemmodule' => 'mhaairs', 'iteminstance' => 101, 'itemnumber' => 0, 'grades' => null, 'itemdetails' => null);
// Grade details.
$grades = array('userid' => 'student1', 'identity_type' => '');
$gradesjson = urlencode(json_encode($grades));
$serviceparams['grades'] = $gradesjson;
$result = call_user_func_array($callback, $serviceparams);
$this->assertEquals('MH Assignment', $result['item']['itemname']);
$this->assertEquals($this->student1->id, $result['grades'][0]['userid']);
$this->assertEquals(95, $result['grades'][0]['grade']);
}
示例3: create_grade_item
/**
* Create a grade_item.
*
* @param array|stdClass $record
* @return stdClass the grade item record
*/
public function create_grade_item($record = null)
{
global $CFG;
require_once "{$CFG->libdir}/gradelib.php";
$this->gradeitemcounter++;
if (!isset($record['itemtype'])) {
$record['itemtype'] = 'manual';
}
if (!isset($record['itemname'])) {
$record['itemname'] = 'Grade item ' . $this->gradeitemcounter;
}
if (isset($record['outcomeid'])) {
$outcome = new grade_outcome(array('id' => $record['outcomeid']));
$record['scaleid'] = $outcome->scaleid;
}
if (isset($record['scaleid'])) {
$record['gradetype'] = GRADE_TYPE_SCALE;
} else {
if (!isset($record['gradetype'])) {
$record['gradetype'] = GRADE_TYPE_VALUE;
}
}
// Create new grade item in this course.
$gradeitem = new grade_item($record, false);
$gradeitem->insert();
$gradeitem->update_from_db();
return $gradeitem->get_record_data();
}
示例4: test_sync_completionelements
/**
* Test sync_completionelements method.
*
* @dataProvider dataprovider_sync_completionelements
* @param array $gradeitem Array of parameters to create grade_item.
* @param array $gradegrade Array of parameters to create grade_grade.
* @param array $coursecompletion Array of parameters to create coursecompletion.
* @param array $studentgrade Array of parameters to create student_grade.
* @param int $timenow Current timestamp to enable testing with time.
* @param array $expectedstudentgrade Array of parameters we expect to be set in the student_grade.
*/
public function test_sync_completionelements($gradeitem, $gradegrade, $coursecompletion, $studentgrade, $timenow, $expectedstudentgrade)
{
global $DB;
$sync = new \local_elisprogram\moodle\synchronize();
// Test data setup.
$crs = new \course(array('idnumber' => 'CRS1', 'name' => 'Course 1', 'syllabus' => ''));
$crs->save();
$cls = new \pmclass(array('courseid' => $crs->id, 'idnumber' => 'CLS1'));
$cls->save();
$usr = new \user(array('username' => 'test1', 'idnumber' => 'test2', 'firstname' => 'test', 'lastname' => 'user', 'email' => 'testuser@example.com', 'country' => 'CA'));
$usr->save();
$musr = $this->getDataGenerator()->create_user();
$gradeitem = new \grade_item($gradeitem, false);
$gradeitem->insert();
$gradegrade['itemid'] = $gradeitem->id;
$gradegrade['userid'] = $musr->id;
$gradegrade = new \grade_grade($gradegrade, false);
$gradegrade->insert();
$coursecompletion['courseid'] = $crs->id;
$coursecompletion = new \coursecompletion($coursecompletion);
$coursecompletion->save();
if ($studentgrade !== false) {
$studentgrade['classid'] = $cls->id;
$studentgrade['userid'] = $usr->id;
$studentgrade['completionid'] = $coursecompletion->id;
$studentgrade = new \student_grade($studentgrade);
$studentgrade->save();
$studentgrade = new \student_grade($studentgrade->id);
$studentgrade->load();
}
// Method parameter setup.
$causer = (object) array('cmid' => $usr->id, 'pmclassid' => $cls->id);
$gis = array($gradeitem->id => (object) array('id' => $gradeitem->id, 'grademax' => $gradeitem->grademax));
$compelements = array($gradeitem->id => (object) array('id' => $coursecompletion->id, 'completion_grade' => $coursecompletion->completion_grade));
$moodlegrades = array($gradeitem->id => $gradegrade);
if ($studentgrade !== false) {
$cmgrades = array($coursecompletion->id => $studentgrade->to_object());
} else {
$cmgrades = array();
}
$sync->sync_completionelements($causer, $gis, $compelements, $moodlegrades, $cmgrades, $timenow);
$actualstudentgrade = false;
if ($studentgrade !== false) {
$actualstudentgrade = $DB->get_record(\student_grade::TABLE, array('id' => $studentgrade->id));
} else {
$actualstudentgrades = $DB->get_records(\student_grade::TABLE, array(), 'id DESC');
if (!empty($actualstudentgrades)) {
$actualstudentgrade = array_shift($actualstudentgrades);
}
}
if ($actualstudentgrade !== false) {
if ($expectedstudentgrade !== false) {
$expectedstudentgrade['id'] = $actualstudentgrade->id;
$expectedstudentgrade['classid'] = $cls->id;
$expectedstudentgrade['userid'] = $usr->id;
$expectedstudentgrade['completionid'] = $coursecompletion->id;
// This is here for tests where we can't reliably predetermine timemodified (i.e. no-sync cases).
if (!isset($expectedstudentgrade['timemodified'])) {
$expectedstudentgrade['timemodified'] = $actualstudentgrade->timemodified;
}
$expectedstudentgrade = (object) $expectedstudentgrade;
$this->assertEquals($expectedstudentgrade, $actualstudentgrade);
} else {
$this->assertTrue(false, 'A student_grade was created when one was not expected.');
}
} else {
// If $expectedstudentgrade is false we were expected no grade to be created. If not, we have a problem.
if ($expectedstudentgrade !== false) {
$this->assertTrue(false, 'No student_grade created when one was expected');
} else {
$this->assertTrue(true);
}
}
}
示例5: get_grade_item
/**
* Retrieves this grade categories' associated grade_item from the database
*
* If no grade_item exists yet, creates one.
*
* @return grade_item
*/
public function get_grade_item()
{
if (empty($this->id)) {
debugging("Attempt to obtain a grade_category's associated grade_item without the category's ID being set.");
return false;
}
if (empty($this->parent)) {
$params = array('courseid' => $this->courseid, 'itemtype' => 'course', 'iteminstance' => $this->id);
} else {
$params = array('courseid' => $this->courseid, 'itemtype' => 'category', 'iteminstance' => $this->id);
}
if (!($grade_items = grade_item::fetch_all($params))) {
// create a new one
$grade_item = new grade_item($params, false);
$grade_item->gradetype = GRADE_TYPE_VALUE;
$grade_item->insert('system');
} else {
if (count($grade_items) == 1) {
// found existing one
$grade_item = reset($grade_items);
} else {
debugging("Found more than one grade_item attached to category id:" . $this->id);
// return first one
$grade_item = reset($grade_items);
}
}
return $grade_item;
}
示例6: test_version1importdeleteuserdeletesassociations
/**
* Validate that the version 1 plugin deletes appropriate associations when
* deleting a user
*/
public function test_version1importdeleteuserdeletesassociations()
{
global $CFG, $DB;
set_config('siteadmins', 0);
// New config settings needed for course format refactoring in 2.4.
set_config('numsections', 15, 'moodlecourse');
set_config('hiddensections', 0, 'moodlecourse');
set_config('coursedisplay', 1, 'moodlecourse');
require_once $CFG->dirroot . '/cohort/lib.php';
require_once $CFG->dirroot . '/course/lib.php';
require_once $CFG->dirroot . '/group/lib.php';
require_once $CFG->dirroot . '/lib/enrollib.php';
require_once $CFG->dirroot . '/lib/gradelib.php';
// Create our test user, and determine their userid.
$this->run_core_user_import(array());
$userid = (int) $DB->get_field('user', 'id', array('username' => 'rlipusername', 'mnethostid' => $CFG->mnet_localhost_id));
// Create cohort.
$cohort = new stdClass();
$cohort->name = 'testcohort';
$cohort->contextid = context_system::instance()->id;
$cohortid = cohort_add_cohort($cohort);
// Add the user to the cohort.
cohort_add_member($cohortid, $userid);
// Create a course category - there is no API for doing this.
$category = new stdClass();
$category->name = 'testcategory';
$category->id = $DB->insert_record('course_categories', $category);
// Create a course.
set_config('defaultenrol', 1, 'enrol_manual');
set_config('status', ENROL_INSTANCE_ENABLED, 'enrol_manual');
$course = new stdClass();
$course->category = $category->id;
$course->fullname = 'testfullname';
$course = create_course($course);
// Create a grade.
$gradeitem = new grade_item(array('courseid' => $course->id, 'itemtype' => 'manual', 'itemname' => 'testitem'), false);
$gradeitem->insert();
$gradegrade = new grade_grade(array('itemid' => $gradeitem->id, 'userid' => $userid), false);
$gradegrade->insert();
// Send the user an unprocessed message.
set_config('noemailever', true);
// Set up a user tag.
tag_set('user', $userid, array('testtag'));
// Create a new course-level role.
$roleid = create_role('testrole', 'testrole', 'testrole');
set_role_contextlevels($roleid, array(CONTEXT_COURSE));
// Enrol the user in the course with the new role.
enrol_try_internal_enrol($course->id, $userid, $roleid);
// Create a group.
$group = new stdClass();
$group->name = 'testgroup';
$group->courseid = $course->id;
$groupid = groups_create_group($group);
// Add the user to the group.
groups_add_member($groupid, $userid);
set_user_preference('testname', 'testvalue', $userid);
// Create profile field data - don't both with the API here because it's a bit unwieldy.
$userinfodata = new stdClass();
$userinfodata->fieldid = 1;
$userinfodata->data = 'bogus';
$userinfodata->userid = $userid;
$DB->insert_record('user_info_data', $userinfodata);
// There is no easily accessible API for doing this.
$lastaccess = new stdClass();
$lastaccess->userid = $userid;
$lastaccess->courseid = $course->id;
$DB->insert_record('user_lastaccess', $lastaccess);
$data = array('action' => 'delete', 'username' => 'rlipusername');
$this->run_core_user_import($data, false);
// Assert data condition after delete.
$this->assertEquals($DB->count_records('message_read', array('useridto' => $userid)), 0);
$this->assertEquals($DB->count_records('grade_grades'), 0);
$this->assertEquals($DB->count_records('tag_instance'), 0);
$this->assertEquals($DB->count_records('cohort_members'), 0);
$this->assertEquals($DB->count_records('user_enrolments'), 0);
$this->assertEquals($DB->count_records('role_assignments'), 0);
$this->assertEquals($DB->count_records('groups_members'), 0);
$this->assertEquals($DB->count_records('user_preferences'), 0);
$this->assertEquals($DB->count_records('user_info_data'), 0);
$this->assertEquals($DB->count_records('user_lastaccess'), 0);
}
示例7: restore_create_gradebook
/**
* This function creates all the gradebook data from xml
*/
function restore_create_gradebook($restore, $xml_file)
{
global $CFG;
$status = true;
//Check it exists
if (!file_exists($xml_file)) {
return false;
}
// Get info from xml
// info will contain the number of record to process
$info = restore_read_xml_gradebook($restore, $xml_file);
// If we have info, then process
if (empty($info)) {
return $status;
}
if (empty($CFG->disablegradehistory) and isset($info->gradebook_histories) and $info->gradebook_histories == "true") {
$restore_histories = true;
} else {
$restore_histories = false;
}
// make sure top course category exists
$course_category = grade_category::fetch_course_category($restore->course_id);
$course_category->load_grade_item();
// we need to know if all grade items that were backed up are being restored
// if that is not the case, we do not restore grade categories nor gradeitems of category type or course type
// i.e. the aggregated grades of that category
$restoreall = true;
// set to false if any grade_item is not selected/restored or already exist
$importing = !empty($SESSION->restore->importing);
if ($importing) {
$restoreall = false;
} else {
$prev_grade_items = grade_item::fetch_all(array('courseid' => $restore->course_id));
$prev_grade_cats = grade_category::fetch_all(array('courseid' => $restore->course_id));
// if any categories already present, skip restore of categories from backup - course item or category already exist
if (count($prev_grade_items) > 1 or count($prev_grade_cats) > 1) {
$restoreall = false;
}
unset($prev_grade_items);
unset($prev_grade_cats);
if ($restoreall) {
if ($recs = get_records_select("backup_ids", "table_name = 'grade_items' AND backup_code = {$restore->backup_unique_code}", "", "old_id")) {
foreach ($recs as $rec) {
if ($data = backup_getid($restore->backup_unique_code, 'grade_items', $rec->old_id)) {
$info = $data->info;
// do not restore if this grade_item is a mod, and
$itemtype = backup_todb($info['GRADE_ITEM']['#']['ITEMTYPE']['0']['#']);
if ($itemtype == 'mod') {
$olditeminstance = backup_todb($info['GRADE_ITEM']['#']['ITEMINSTANCE']['0']['#']);
$itemmodule = backup_todb($info['GRADE_ITEM']['#']['ITEMMODULE']['0']['#']);
if (empty($restore->mods[$itemmodule]->granular)) {
continue;
} else {
if (!empty($restore->mods[$itemmodule]->instances[$olditeminstance]->restore)) {
continue;
}
}
// at least one activity should not be restored - do not restore categories and manual items at all
$restoreall = false;
break;
}
}
}
}
}
}
// Start ul
if (!defined('RESTORE_SILENTLY')) {
echo '<ul>';
}
// array of restored categories - speedup ;-)
$cached_categories = array();
$outcomes = array();
/// Process letters
$context = get_context_instance(CONTEXT_COURSE, $restore->course_id);
// respect current grade letters if defined
if ($status and $restoreall and !record_exists('grade_letters', 'contextid', $context->id)) {
if (!defined('RESTORE_SILENTLY')) {
echo '<li>' . get_string('gradeletters', 'grades') . '</li>';
}
// Fetch recordset_size records in each iteration
$recs = get_records_select("backup_ids", "table_name = 'grade_letters' AND backup_code = {$restore->backup_unique_code}", "", "old_id");
if ($recs) {
foreach ($recs as $rec) {
// Get the full record from backup_ids
$data = backup_getid($restore->backup_unique_code, 'grade_letters', $rec->old_id);
if ($data) {
$info = $data->info;
$dbrec = new object();
$dbrec->contextid = $context->id;
$dbrec->lowerboundary = backup_todb($info['GRADE_LETTER']['#']['LOWERBOUNDARY']['0']['#']);
$dbrec->letter = backup_todb($info['GRADE_LETTER']['#']['LETTER']['0']['#']);
insert_record('grade_letters', $dbrec);
}
}
}
}
//.........这里部分代码省略.........
示例8: grade_get_legacy_grade_item
/**
* Get and update/create grade item for legacy modules.
*/
function grade_get_legacy_grade_item($modinstance, $grademax, $scaleid)
{
// does it already exist?
if ($grade_items = grade_item::fetch_all(array('courseid' => $modinstance->course, 'itemtype' => 'mod', 'itemmodule' => $modinstance->modname, 'iteminstance' => $modinstance->id, 'itemnumber' => 0))) {
if (count($grade_items) > 1) {
debugging('Multiple legacy grade_items found.');
return false;
}
$grade_item = reset($grade_items);
if (is_null($grademax) and is_null($scaleid)) {
$grade_item->gradetype = GRADE_TYPE_NONE;
} else {
if ($scaleid) {
$grade_item->gradetype = GRADE_TYPE_SCALE;
$grade_item->scaleid = $scaleid;
$grade_item->grademin = 1;
} else {
$grade_item->gradetype = GRADE_TYPE_VALUE;
$grade_item->grademax = $grademax;
$grade_item->grademin = 0;
}
}
$grade_item->itemname = $modinstance->name;
$grade_item->idnumber = $modinstance->cmidnumber;
$grade_item->update();
return $grade_item;
}
// create new one
$params = array('courseid' => $modinstance->course, 'itemtype' => 'mod', 'itemmodule' => $modinstance->modname, 'iteminstance' => $modinstance->id, 'itemnumber' => 0, 'itemname' => $modinstance->name, 'idnumber' => $modinstance->cmidnumber);
if (is_null($grademax) and is_null($scaleid)) {
$params['gradetype'] = GRADE_TYPE_NONE;
} else {
if ($scaleid) {
$params['gradetype'] = GRADE_TYPE_SCALE;
$params['scaleid'] = $scaleid;
$grade_item->grademin = 1;
} else {
$params['gradetype'] = GRADE_TYPE_VALUE;
$params['grademax'] = $grademax;
$params['grademin'] = 0;
}
}
$grade_item = new grade_item($params);
$grade_item->insert();
return $grade_item;
}
示例9: process_grade_item
protected function process_grade_item($data)
{
global $DB;
$data = (object) $data;
$oldid = $data->id;
// We'll need these later
$oldparentid = $data->categoryid;
$courseid = $this->get_courseid();
$idnumber = null;
if (!empty($data->idnumber)) {
// Don't get any idnumber from course module. Keep them as they are in grade_item->idnumber
// Reason: it's not clear what happens with outcomes->idnumber or activities with multiple items (workshop)
// so the best is to keep the ones already in the gradebook
// Potential problem: duplicates if same items are restored more than once. :-(
// This needs to be fixed in some way (outcomes & activities with multiple items)
// $data->idnumber = get_coursemodule_from_instance($data->itemmodule, $data->iteminstance)->idnumber;
// In any case, verify always for uniqueness
$sql = "SELECT cm.id\n FROM {course_modules} cm\n WHERE cm.course = :courseid AND\n cm.idnumber = :idnumber AND\n cm.id <> :cmid";
$params = array('courseid' => $courseid, 'idnumber' => $data->idnumber, 'cmid' => $this->task->get_moduleid());
if (!$DB->record_exists_sql($sql, $params) && !$DB->record_exists('grade_items', array('courseid' => $courseid, 'idnumber' => $data->idnumber))) {
$idnumber = $data->idnumber;
}
}
if (!empty($data->categoryid)) {
// If the grade category id of the grade item being restored belongs to this course
// then it is a fair assumption that this is the correct grade category for the activity
// and we should leave it in place, if not then unset it.
// TODO MDL-34790 Gradebook does not import if target course has gradebook categories.
$conditions = array('id' => $data->categoryid, 'courseid' => $courseid);
if (!$this->task->is_samesite() || !$DB->record_exists('grade_categories', $conditions)) {
unset($data->categoryid);
}
}
unset($data->id);
$data->courseid = $this->get_courseid();
$data->iteminstance = $this->task->get_activityid();
$data->idnumber = $idnumber;
$data->scaleid = $this->get_mappingid('scale', $data->scaleid);
$data->outcomeid = $this->get_mappingid('outcome', $data->outcomeid);
$data->timecreated = $this->apply_date_offset($data->timecreated);
$data->timemodified = $this->apply_date_offset($data->timemodified);
$gradeitem = new grade_item($data, false);
$gradeitem->insert('restore');
//sortorder is automatically assigned when inserting. Re-instate the previous sortorder
$gradeitem->sortorder = $data->sortorder;
$gradeitem->update('restore');
// Set mapping, saving the original category id into parentitemid
// gradebook restore (final task) will need it to reorganise items
$this->set_mapping('grade_item', $oldid, $gradeitem->id, false, null, $oldparentid);
}
示例10: test_update_item_with_manual_item
/**
* Tests update grade service with existing regular manual items with the same name
* as requested. Should update the first item and turn it into mhaairs item. Should not change
* any item property other than the item instance.
*
* @return void
*/
public function test_update_item_with_manual_item()
{
global $DB;
$this->set_user('admin');
$this->assertEquals(0, $DB->count_records('grade_items'));
$catname = 'Existing grade category';
$itemname = 'Existing grade item';
// Create category.
$catparams = array('fullname' => $catname, 'courseid' => $this->course->id, 'hidden' => false);
$category = new \grade_category($catparams, false);
$category->id = $category->insert();
// Add two manual grade item directly.
$itemparams = array('courseid' => $this->course->id, 'itemtype' => 'manual', 'itemname' => $itemname, 'categoryid' => $category->id);
$gitem = new \grade_item($itemparams, false);
$gitem->insert('manual');
$gitem = new \grade_item($itemparams, false);
$gitem->insert('manual');
// There should be 4 items (including course and category items).
$itemcount = $DB->count_records('grade_items');
$this->assertEquals(4, $itemcount);
$service = 'block_mhaairs_gradebookservice_external::update_grade';
$itemdetails = array('itemname' => $itemname, 'grademax' => 90, 'useexisting' => 1, 'categoryid' => 'New grade category');
$itemdetailsjson = urlencode(json_encode($itemdetails));
// Update item via service.
$servicedata = array();
$servicedata['source'] = 'mhaairs';
$servicedata['courseid'] = 'tc1';
$servicedata['itemtype'] = 'manual';
$servicedata['itemmodule'] = 'mhaairs';
$servicedata['iteminstance'] = 345;
$servicedata['itemnumber'] = 0;
$servicedata['grades'] = null;
$servicedata['itemdetails'] = $itemdetailsjson;
$result = call_user_func_array($service, $servicedata);
// 2 grade categories overall.
$itemcount = $DB->count_records('grade_categories');
$this->assertEquals(2, $itemcount);
// 4 grade items overall.
$itemcount = $DB->count_records('grade_items');
$this->assertEquals(4, $itemcount);
// 2 manual items remaining.
$itemcount = $DB->count_records('grade_items', array('itemtype' => 'manual'));
$this->assertEquals(2, $itemcount);
// 1 mhaairs item.
$itemcount = $DB->count_records('grade_items', array('itemtype' => 'manual', 'itemmodule' => 'mhaairs'));
$this->assertEquals(1, $itemcount);
// 1 mhaairs item with the item instance and original grade.
$itemcount = $DB->count_records('grade_items', array('itemtype' => 'manual', 'itemmodule' => 'mhaairs', 'iteminstance' => 345, 'grademax' => 100.0, 'categoryid' => $category->id));
$this->assertEquals(1, $itemcount);
}
示例11: test_upgrade_calculated_grade_items
/**
* Test the upgrade function for flagging courses with calculated grade item problems.
*/
public function test_upgrade_calculated_grade_items() {
global $DB, $CFG;
$this->resetAfterTest();
// Create a user.
$user = $this->getDataGenerator()->create_user();
// Create a couple of courses.
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$course3 = $this->getDataGenerator()->create_course();
// Enrol the user in the courses.
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$maninstance1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
$maninstance2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST);
$maninstance3 = $DB->get_record('enrol', array('courseid' => $course3->id, 'enrol' => 'manual'), '*', MUST_EXIST);
$manual = enrol_get_plugin('manual');
$manual->enrol_user($maninstance1, $user->id, $studentrole->id);
$manual->enrol_user($maninstance2, $user->id, $studentrole->id);
$manual->enrol_user($maninstance3, $user->id, $studentrole->id);
// To create the data we need we freeze the grade book to use the old behaviour.
set_config('gradebook_calculations_freeze_' . $course1->id, 20150627);
set_config('gradebook_calculations_freeze_' . $course2->id, 20150627);
set_config('gradebook_calculations_freeze_' . $course3->id, 20150627);
$CFG->grade_minmaxtouse = 2;
// Creating a category for a grade item.
$gradecategory = new grade_category();
$gradecategory->fullname = 'calculated grade category';
$gradecategory->courseid = $course1->id;
$gradecategory->insert();
$gradecategoryid = $gradecategory->id;
// This is a manual grade item.
$gradeitem = new grade_item();
$gradeitem->itemname = 'grade item one';
$gradeitem->itemtype = 'manual';
$gradeitem->categoryid = $gradecategoryid;
$gradeitem->courseid = $course1->id;
$gradeitem->idnumber = 'gi1';
$gradeitem->insert();
// Changing the category into a calculated grade category.
$gradecategoryitem = grade_item::fetch(array('iteminstance' => $gradecategory->id));
$gradecategoryitem->calculation = '=##gi' . $gradeitem->id . '##/2';
$gradecategoryitem->update();
// Setting a grade for the student.
$grade = $gradeitem->get_grade($user->id, true);
$grade->finalgrade = 50;
$grade->update();
// Creating all the grade_grade items.
grade_regrade_final_grades($course1->id);
// Updating the grade category to a new grade max and min.
$gradecategoryitem->grademax = 50;
$gradecategoryitem->grademin = 5;
$gradecategoryitem->update();
// Different manual grade item for course 2. We are creating a course with a calculated grade item that has a grade max of
// 50. The grade_grade will have a rawgrademax of 100 regardless.
$gradeitem = new grade_item();
$gradeitem->itemname = 'grade item one';
$gradeitem->itemtype = 'manual';
$gradeitem->courseid = $course2->id;
$gradeitem->idnumber = 'gi1';
$gradeitem->grademax = 25;
$gradeitem->insert();
// Calculated grade item for course 2.
$calculatedgradeitem = new grade_item();
$calculatedgradeitem->itemname = 'calculated grade';
$calculatedgradeitem->itemtype = 'manual';
$calculatedgradeitem->courseid = $course2->id;
$calculatedgradeitem->calculation = '=##gi' . $gradeitem->id . '##*2';
$calculatedgradeitem->grademax = 50;
$calculatedgradeitem->insert();
// Assigning a grade for the user.
$grade = $gradeitem->get_grade($user->id, true);
$grade->finalgrade = 10;
$grade->update();
// Setting all of the grade_grade items.
grade_regrade_final_grades($course2->id);
// Different manual grade item for course 3. We are creating a course with a calculated grade item that has a grade max of
// 50. The grade_grade will have a rawgrademax of 100 regardless.
$gradeitem = new grade_item();
$gradeitem->itemname = 'grade item one';
$gradeitem->itemtype = 'manual';
$gradeitem->courseid = $course3->id;
$gradeitem->idnumber = 'gi1';
$gradeitem->grademax = 25;
$gradeitem->insert();
//.........这里部分代码省略.........
示例12: sub_test_scale_one_item
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);
}
示例13: save_grade_item
private static function save_grade_item($grade_item)
{
if (!$grade_item) {
throw new InvalidArgumentException("grade_item must be set");
}
if (!$grade_item->courseid) {
throw new InvalidArgumentException("grade_item->courseid must be set");
}
if (!$grade_item->categoryid) {
throw new InvalidArgumentException("grade_item->categoryid must be set");
}
if (!$grade_item->name) {
throw new InvalidArgumentException("grade_item->name must be set");
}
if (!isset($grade_item->item_number)) {
$grade_item->item_number = 0;
}
// check for an existing item and update or create
$grade_item_tosave = grade_item::fetch(array('courseid' => $grade_item->courseid, 'itemmodule' => self::GRADE_ITEM_MODULE, 'itemname' => $grade_item->name));
if (!$grade_item_tosave) {
// create new one
$grade_item_tosave = new grade_item();
$grade_item_tosave->itemmodule = self::GRADE_ITEM_MODULE;
$grade_item_tosave->courseid = $grade_item->courseid;
$grade_item_tosave->categoryid = $grade_item->categoryid;
$grade_item_tosave->iteminfo = $grade_item->typename;
//$grade_item_tosave->iteminfo = $grade_item->name.' '.$grade_item->type.' '.self::GRADE_CATEGORY_NAME;
$grade_item_tosave->itemnumber = $grade_item->item_number;
//$grade_item_tosave->idnumber = $grade_item->name;
$grade_item_tosave->itemname = $grade_item->name;
$grade_item_tosave->itemtype = self::GRADE_ITEM_TYPE;
//$grade_item_tosave->itemmodule = self::GRADE_ITEM_MODULE;
if (isset($grade_item->points_possible) && $grade_item->points_possible > 0) {
$grade_item_tosave->grademax = $grade_item->points_possible;
}
$grade_item_tosave->insert(self::GRADE_LOCATION_STR);
} else {
// update
if (isset($grade_item->points_possible) && $grade_item->points_possible > 0) {
$grade_item_tosave->grademax = $grade_item->points_possible;
}
$grade_item_tosave->categoryid = $grade_item->categoryid;
$grade_item_tosave->iteminfo = $grade_item->typename;
$grade_item_tosave->update(self::GRADE_LOCATION_STR);
}
$grade_item_id = $grade_item_tosave->id;
$grade_item_pp = $grade_item_tosave->grademax;
// now save the related scores
if (isset($grade_item->scores) && !empty($grade_item->scores)) {
// get the existing scores
$current_scores = array();
$existing_grades = grade_grade::fetch_all(array('itemid' => $grade_item_id));
if ($existing_grades) {
foreach ($existing_grades as $grade) {
$current_scores[$grade->userid] = $grade;
}
}
// run through the scores in the gradeitem and try to save them
$errors_count = 0;
$processed_scores = array();
foreach ($grade_item->scores as $score) {
$user = self::get_users($score->user_id);
if (!$user) {
$score->error = self::USER_DOES_NOT_EXIST_ERROR;
$processed_scores[] = $score;
$errors_count++;
continue;
}
$user_id = $user->id;
// null/blank scores are not allowed
if (!isset($score->score)) {
$score->error = 'NO_SCORE_ERROR';
$processed_scores[] = $score;
$errors_count++;
continue;
}
if (!is_numeric($score->score)) {
$score->error = 'SCORE_INVALID';
$processed_scores[] = $score;
$errors_count++;
continue;
}
$score->score = floatval($score->score);
// Student Score should not be greater than the total points possible
if ($score->score > $grade_item_pp) {
$score->error = self::POINTS_POSSIBLE_UPDATE_ERRORS;
$processed_scores[] = $score;
$errors_count++;
continue;
}
try {
$grade_tosave = null;
if (isset($current_scores[$user_id])) {
// existing score
$grade_tosave = $current_scores[$user_id];
// check against existing score
if ($score->score < $grade_tosave->rawgrade) {
$score->error = self::SCORE_UPDATE_ERRORS;
$processed_scores[] = $score;
$errors_count++;
//.........这里部分代码省略.........
示例14: execute
//.........这里部分代码省略.........
}
} else {
// Set it to default if no good scale could be found/used.
$gradeid = 0;
$scaleid = 0;
overnight::tlog('No \'gradetype\' found, so using defaults instead.', 'info');
}
// You may get errors here (unknown index IIRC) if no scalename is generated because a scale (or anything) hasn't been set
// for that course (e.g. 'cos it's a new course). Catch this earlier!
$logging['grade_types'][strtolower($course->scalename)]++;
/**
* Category checking: create or skip.
*/
if ($DB->get_record('grade_categories', array('courseid' => $course->id, 'fullname' => CATNAME))) {
// Category exists, so skip creation.
overnight::tlog('Category \'' . CATNAME . '\' already exists for course ' . $course->id . '.', 'skip');
} else {
$grade_category = new \grade_category();
// Create a category for this course.
$grade_category->courseid = $course->id;
// Course id.
$grade_category->fullname = CATNAME;
// Set the category name (no description).
$grade_category->sortorder = 1;
// Need a better way of changing column order.
$grade_category->hidden = 1;
// Attempting to hide the totals.
// Save all that...
if (!($gc = $grade_category->insert())) {
overnight::tlog('Category \'' . CATNAME . '\' could not be inserted for course ' . $course->id . '.', 'EROR');
return false;
} else {
overnight::tlog('Category \'' . CATNAME . '\' (' . $gc . ') created for course ' . $course->id . '.');
}
}
// We've either checked a category exists or created one, so this *should* always work.
$cat_id = $DB->get_record('grade_categories', array('courseid' => $course->id, 'fullname' => CATNAME));
$cat_id = $cat_id->id;
// One thing we need to do is set 'gradetype' to 0 on that newly created category, which prevents a category total showing
// and the grades counting towards the total course grade.
$DB->set_field_select('grade_items', 'gradetype', 0, "courseid = " . $course->id . " AND itemtype = 'category' AND iteminstance = " . $cat_id);
/**
* Column checking: create or update.
*/
// Step through each column name.
foreach ($column_names as $col_name => $col_desc) {
// Need to check for previously-created columns and force an update if they already exist.
//if ( $DB->get_record('grade_items', array( 'courseid' => $course->id, 'itemname' => $col_name, 'itemtype' => 'manual' ) ) ) {
// // Column exists, so update instead.
// overnight::tlog('- Column \'' . $col_name . '\' already exists for course ' . $course->id . '.', 'skip');
//} else {
$grade_item = new \grade_item();
// Create a new item object.
$grade_item->courseid = $course->id;
// Course id.
$grade_item->itemtype = 'manual';
// Set the category name (no description).
$grade_item->itemname = $col_name;
// The item's name.
$grade_item->iteminfo = $col_desc;
// Description of the item.
示例15: tlog
$grade_item->display = 1;
// 'Real'.
}
if ($col_name == 'MAG') {
$grade_item->sortorder = 3;
//$grade_item->locked = 1;
$grade_item->gradetype = $gradeid;
$grade_item->scaleid = $scaleid;
$grade_item->display = 1;
// 'Real'.
}
// Scale ID, generated earlier. An int, 0 or greater.
// TODO: Check if we need this any more!!
$grade_item->scale = $scaleid;
// Save it all.
if (!($gi = $grade_item->insert())) {
tlog(' Column \'' . $col_name . '\' could not be inserted for course ' . $course->id . '.', 'EROR');
exit(1);
} else {
tlog(' Column \'' . $col_name . '\' created for course ' . $course->id . '.');
}
}
// END skip processing if manual column(s) already found in course.
}
// END while working through each rquired column.
/**
* Move the category to the first location in the gradebook if it isn't already.
*/
//$gtree = new grade_tree($course->id, false, false);
//$temp = grade_edit_tree::move_elements(1, '')
/**