// //
///////////////////////////////////////////////////////////////////////////
+require_once($CFG->libdir.'/gradelib.php');
+
/// Some constants
define ('DATA_MAX_ENTRIES', 50);
define ('DATA_PERPAGE_SINGLE', 1);
return false;
}
+ $data = stripslashes_recursive($data);
+ data_grade_item_create($data);
+
return $data->id;
}
function data_update_instance($data) {
global $CFG;
- $data->id = $data->instance;
+ $data->timemodified = time();
+ $data->id = $data->instance;
if (empty($data->assessed)) {
$data->assessed = 0;
}
- $data->timemodified = time();
-
- if (! $data->instance = update_record('data', $data)) {
+ if (! update_record('data', $data)) {
return false;
}
- return $data->instance;
+
+ $data = stripslashes_recursive($data);
+ data_grade_item_update($data);
+
+ return true;
}
// Delete the instance itself
- if (! delete_records('data', 'id', $id)) {
- return false;
- }
- return true;
+ $result = delete_records('data', 'id', $id);
+
+ glossary_grade_item_delete($data);
+
+ return $result;
}
/************************************************************************
}
}
+
+/**
+ * Return grade for given user or all users.
+ *
+ * @param int $dataid id of data
+ * @param int $userid optional user id, 0 means all users
+ * @return array array of grades, false if none
+ */
+function data_get_user_grades($dataid, $userid=0) {
+ global $CFG;
+
+ $user = $userid ? "AND u.id = $userid" : "";
+
+ $sql = "SELECT u.id, avg(drt.rating) AS gradevalue
+ FROM {$CFG->prefix}user u, {$CFG->prefix}data_records dr,
+ {$CFG->prefix}data_ratings drt
+ WHERE u.id = dr.userid AND dr.id = drt.recordid
+ AND drt.userid != u.id AND dr.dataid = $dataid
+ $user
+ GROUP BY u.id";
+
+ return get_records_sql($sql);
+}
+
+/**
+ * Update grades by firing grade_updated event
+ *
+ * @param object $grade_item null means all databases
+ * @param int $userid specific user only, 0 mean all
+ */
+function data_update_grades($grade_item=null, $userid=0, $nullifnone=true) {
+ global $CFG;
+
+ if ($grade_item != null) {
+ if ($grades = data_get_user_grades($grade_item->iteminstance, $userid)) {
+ foreach ($grades as $grade) {
+ $eventdata = new object();
+ $eventdata->itemid = $grade_item->id;
+ $eventdata->userid = $grade->id;
+ $eventdata->gradevalue = $grade->gradevalue;
+ events_trigger('grade_updated', $eventdata);
+ }
+
+ } else if ($userid and $nullifnone) {
+ $eventdata = new object();
+ $eventdata->itemid = $grade_item->id;
+ $eventdata->userid = $userid;
+ $eventdata->gradevalue = NULL;
+ events_trigger('grade_updated', $eventdata);
+ }
+
+ } else {
+ $sql = "SELECT d.*, cm.idnumber as cmidnumber
+ FROM {$CFG->prefix}data d, {$CFG->prefix}course_modules cm, {$CFG->prefix}modules m
+ WHERE m.name='data' AND m.id=cm.module AND cm.instance=d.id";
+ if ($rs = get_recordset_sql($sql)) {
+ if ($rs->RecordCount() > 0) {
+ while ($data = rs_fetch_next_record($rs)) {
+ if (!$data->assessed) {
+ continue; // no grading
+ }
+ $grade_item = data_grade_item_get($data);
+ data_update_grades($grade_item, 0, false);
+ }
+ }
+ rs_close($rs);
+ }
+ }
+}
+
+/**
+ * Return (create if needed) grade item for given data
+ *
+ * @param object $data object with optional cmidnumber
+ * @return object grade_item
+ */
+function data_grade_item_get($data) {
+ if ($items = grade_get_items($data->course, 'mod', 'data', $data->id)) {
+ if (count($items) > 1) {
+ debugging('Multiple grade items present!');
+ }
+ $grade_item = reset($items);
+
+ } else {
+ if (!isset($data->cmidnumber)) {
+ if (!$cm = get_coursemodule_from_instance('data', $data->id)) {
+ error("Course Module ID was incorrect");
+ }
+ $data->cmidnumber = $cm->idnumber;
+ }
+ if (!$itemid = data_grade_item_create($data)) {
+ error('Can not create grade item!');
+ }
+ $grade_item = grade_item::fetch('id', $itemid);
+ }
+
+ return $grade_item;
+}
+
+/**
+ * Update grade item for given data
+ *
+ * @param object $data object with extra cmidnumber
+ * @return object grade_item
+ */
+function data_grade_item_update($data) {
+ $grade_item = data_grade_item_get($data);
+
+ $grade_item->name = $data->name;
+ $grade_item->idnumber = $data->cmidnumber;
+
+ if (!$data->assessed or $data->scale == 0) {
+ //how to indicate no grading?
+ $grade_item->gradetype = GRADE_TYPE_TEXT;
+
+ } else if ($data->scale > 0) {
+ $grade_item->gradetype = GRADE_TYPE_VALUE;
+ $grade_item->grademax = $data->scale;
+ $grade_item->grademin = 0;
+
+ } else if ($data->scale < 0) {
+ $grade_item->gradetype = GRADE_TYPE_SCALE;
+ $grade_item->scaleid = -$data->scale;
+ }
+
+ $grade_item->update();
+}
+
+/**
+ * Create grade item for given data
+ *
+ * @param object $data object with extra cmidnumber
+ * @return object grade_item
+ */
+function data_grade_item_create($data) {
+ $params = array('courseid' =>$data->course,
+ 'itemtype' =>'mod',
+ 'itemmodule' =>'data',
+ 'iteminstance'=>$data->id,
+ 'itemname' =>$data->name,
+ 'idnumber' =>$data->cmidnumber);
+
+ if (!$data->assessed or $data->scale == 0) {
+ //how to indicate no grading?
+ $params['gradetype'] = GRADE_TYPE_TEXT;
+
+ } else if ($data->scale > 0) {
+ $params['gradetype'] = GRADE_TYPE_VALUE;
+ $params['grademax'] = $data->scale;
+ $params['grademin'] = 0;
+
+ } else if ($data->scale < 0) {
+ $params['gradetype'] = GRADE_TYPE_SCALE;
+ $params['scaleid'] = -$data->scale;
+ }
+
+ $itemid = grade_create_item($params);
+ return $itemid;
+}
+
+/**
+ * Delete grade item for given data
+ *
+ * @param object $data object
+ * @return object grade_item
+ */
+function data_grade_item_delete($data) {
+ if ($grade_items = grade_get_items($data->course, 'mod', 'data', $data->id)) {
+ foreach($grade_items as $grade_item) {
+ $grade_item->delete();
+ }
+ }
+}
+
/************************************************************************
* returns a list of participants of this database *
************************************************************************/
echo '<div class="ratings" style="text-align:center">';
echo '<form id="form" method="post" action="rate.php">';
+ echo '<input type="hidden" name="dataid" value="'.$data->id.'" />';
if (has_capability('mod/data:rate', $context) and !data_isowner($record->id)) {
data_print_ratings_mean($record->id, $ratingsscale, has_capability('mod/data:viewrating', $context));
static $strrate;
if (!$rating = get_record("data_ratings", "userid", $userid, "recordid", $recordid)) {
- $rating->rating = 0;
+ $rating->rating = -999;
}
if (empty($strrate)) {
$strrate = get_string("rate", "data");
}
- choose_from_menu($scale, $recordid, $rating->rating, "$strrate...");
+ choose_from_menu($scale, $recordid, $rating->rating, "$strrate...", '', -999);
}
print_heading(format_string($data->name));
/// Groups needed for Add entry tab
- if ($groupmode = groupmode($course, $cm)) { // Groups are being used
- $currentgroup = get_and_set_current_group($course, $groupmode);
- } else {
- $currentgroup = 0;
- }
+ $groupmode = groupmode($course, $cm);
+ $currentgroup = get_and_set_current_group($course, $groupmode);
/// Print the tabs
}
}
-function data_user_can_add_entry($data, $currentgroup=false, $groupmode='') {
+function data_user_can_add_entry($data, $currentgroup, $groupmode) {
global $USER;
if (!$cm = get_coursemodule_from_instance('data', $data->id)) {
return false;
}
+ if (!$groupmode or has_capability('moodle/site:accessallgroups', $context)) {
+ return true;
+ }
+
if ($currentgroup) {
- return (has_capability('moodle/site:accessallgroups', $context) or ismember($currentgroup));
+ return ismember($currentgroup);
} else {
//else it might be group 0 in visible mode
if ($groupmode == VISIBLEGROUPS){
-
- $result = groups_is_member($currentgroup);
- return $result;
- } else {
return true;
+ } else {
+ return false;
}
}
}
require_once('../../config.php');
require_once('lib.php');
- if (!($data = data_submitted($CFG->wwwroot.'/mod/data/view.php')) or !confirm_sesskey()) {
+ $dataid = required_param('dataid', PARAM_INT); // The forum the rated posts are from
+
+ if (!$data = get_record('data', 'id', $dataid)) {
+ error("Incorrect data id");
+ }
+
+ if (!$course = get_record('course', 'id', $data->course)) {
+ error("Course ID was incorrect");
+ }
+
+ if (!$cm = get_coursemodule_from_instance('data', $data->id)) {
+ error("Course Module ID was incorrect");
+ }
+
+ require_login($course, false, $cm);
+
+ if (isguestuser()) {
+ error("Guests are not allowed to rate entries.");
+ }
+
+ $context = get_context_instance(CONTEXT_MODULE, $cm->id);
+ require_capability('mod/data:rate', $context);
+
+ if (!$data->assessed) {
+ error("Rating of items not allowed!");
+ }
+
+ $grade_item = data_grade_item_get($data);
+
+ if (!$frmdata = data_submitted() or !confirm_sesskey()) {
error("This page was not accessed correctly");
}
$count = 0;
- foreach ((array)$data as $recordid => $rating) {
+ foreach ((array)$frmdata as $recordid => $rating) {
if (!is_numeric($recordid)) {
continue;
}
if (!$record = get_record('data_records', 'id', $recordid)) {
error("Record ID is incorrect");
}
- if (!$data = get_record('data', 'id', $record->dataid)) {
- error("Data ID is incorrect");
- }
- if (!$course = get_record('course', 'id', $data->course)) {
- error("Course is misconfigured");
- }
- if (!$cm = get_coursemodule_from_instance('data', $data->id, $course->id)) {
- error("Course Module ID was incorrect");
- }
-
- require_login($course->id, false, $cm);
-
- $context = get_context_instance(CONTEXT_MODULE, $cm->id);
-
- if (isguest()) {
- error("Guests are not allowed to rate posts");
- }
- if (!$data->assessed or !has_capability('mod/data:rate', $context)) {
- error("Not allowed to rate.");
+ if ($data->id != $record->dataid) {
+ error("Incorrect record.");
}
if ($record->userid == $USER->id) {
- error("You can not rate your own data");
- }
-
- if (!$scale = make_grades_menu($data->scale)) {
- error("Icorrect scale");
- }
-
- if (!array_key_exists($rating, $scale)) {
- error("Icorrect rating value");
+ continue;
}
// input validation ok
$count++;
if ($oldrating = get_record('data_ratings', 'userid', $USER->id, 'recordid', $record->id)) {
- if ($rating != $oldrating->rating) {
+ if ($rating == -999) {
+ delete_records('data_ratings', 'userid', $oldrating->userid, 'recordid', $oldrating->recordid);
+ data_update_grades($grade_item, $record->userid);
+
+ } else if ($rating != $oldrating->rating) {
$oldrating->rating = $rating;
if (! update_record('data_ratings', $oldrating)) {
error("Could not update an old rating ($record->id = $rating)");
}
+ data_update_grades($grade_item, $record->userid);
+
}
} else if ($rating) {
$newrating = new object();
if (! insert_record('data_ratings', $newrating)) {
error("Could not insert a new rating ($record->id = $rating)");
}
+ data_update_grades($grade_item, $record->userid);
}
}