Skip to content

Commit

Permalink
OUBlog: Add support for post ratings #11509
Browse files Browse the repository at this point in the history
  • Loading branch information
athompson697 authored and sammarshallou committed Oct 28, 2014
1 parent d0d7df5 commit c4e7c21
Show file tree
Hide file tree
Showing 18 changed files with 858 additions and 49 deletions.
19 changes: 18 additions & 1 deletion backup/moodle2/backup_oublog_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ protected function define_structure() {
'accesstoken', 'intro', 'introformat', 'allowcomments', 'individual',
'maxbytes', 'maxattachments', 'maxvisibility', 'global', 'views',
'completionposts', 'completioncomments', 'reportingemail', 'displayname',
'statblockon', 'allowimport', 'introonpost', 'tags'));
'statblockon', 'allowimport', 'introonpost', 'tags', 'assessed',
'assesstimestart', 'assesstimefinish', 'scale', 'grading'));
$instances = new backup_nested_element('instances');

$instance = new backup_nested_element('instance', array('id'), array(
Expand All @@ -55,6 +56,11 @@ protected function define_structure() {
'timeupdated', 'lasteditedby', 'deletedby',
'timedeleted', 'visibility'));

$ratings = new backup_nested_element('ratings');

$rating = new backup_nested_element('rating', array('id'), array(
'component', 'ratingarea', 'scaleid', 'value', 'userid', 'timecreated', 'timemodified'));

$comments = new backup_nested_element('comments');
$comment = new backup_nested_element('comment', array('id'), array('userid', 'title', 'message',
'timeposted', 'deletedby', 'timedeleted',
Expand All @@ -77,6 +83,9 @@ protected function define_structure() {
$instance->add_child($posts);
$posts->add_child($post);

$post->add_child($ratings);
$ratings->add_child($rating);

$post->add_child($comments);
$comments->add_child($comment);

Expand All @@ -101,6 +110,11 @@ protected function define_structure() {
JOIN {oublog_taginstances} ti
ON t.id=ti.tagid
WHERE ti.postid=?", array(backup::VAR_PARENTID));

$rating->set_source_table('rating', array('contextid' => backup::VAR_CONTEXTID,
'component' => backup_helper::is_sqlparam('mod_oublog'),
'ratingarea' => backup_helper::is_sqlparam('post')));
$rating->set_source_alias('rating', 'value');
}

// Define id annotations
Expand All @@ -111,6 +125,9 @@ protected function define_structure() {
$comment->annotate_ids('user', 'userid');
$edit->annotate_ids('user', 'userid');
$link->annotate_ids('oublog_instances', 'id');
$oublog->annotate_ids('scale', 'scale');
$rating->annotate_ids('scale', 'scaleid');
$rating->annotate_ids('user', 'userid');

// Define file annotations
$oublog->annotate_files('mod_oublog', 'intro', null); // This file area hasn't itemid
Expand Down
28 changes: 28 additions & 0 deletions backup/moodle2/restore_oublog_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected function define_structure() {
$paths[] = new restore_path_element('oublog_instance', '/activity/oublog/instances/instance');
$paths[] = new restore_path_element('oublog_link', '/activity/oublog/links/link');
$paths[] = new restore_path_element('oublog_post', '/activity/oublog/instances/instance/posts/post');
$paths[] = new restore_path_element('oublog_rating', '/activity/oublog/instances/instance/posts/post/ratings/rating');
$paths[] = new restore_path_element('oublog_comment', '/activity/oublog/instances/instance/posts/post/comments/comment');
$paths[] = new restore_path_element('oublog_edit', '/activity/oublog/instances/instance/posts/post/edits/edit');
$paths[] = new restore_path_element('oublog_tag', '/activity/oublog/instances/instance/posts/post/tags/tag');
Expand Down Expand Up @@ -169,6 +170,33 @@ protected function process_oublog_tag($data) {
$this->set_mapping('oublog_tag', $oldid, $newitemid);
}

protected function process_oublog_rating($data) {
global $DB;

$data = (object)$data;

// Cannot use ratings API, cause, it's missing the ability to specify times (modified/created).
$data->contextid = $this->task->get_contextid();
$data->itemid = $this->get_new_parentid('oublog_post');
if ($data->scaleid < 0) { // Scale found, get mapping.
$data->scaleid = -($this->get_mappingid('scale', abs($data->scaleid)));
}
$data->rating = $data->value;
$data->userid = $this->get_mappingid('user', $data->userid);
$data->timecreated = $this->apply_date_offset($data->timecreated);
$data->timemodified = $this->apply_date_offset($data->timemodified);

// Make sure that we have both component and ratingarea set.
if (empty($data->component)) {
$data->component = 'mod_oublog';
}
if (empty($data->ratingarea)) {
$data->ratingarea = 'post';
}

$newitemid = $DB->insert_record('rating', $data);
}

protected function after_execute() {

// Add oublog related files, no need to match by itemname (just internally handled context)
Expand Down
47 changes: 47 additions & 0 deletions db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,51 @@
'manager' => CAP_ALLOW,
)
),
'mod/oublog:viewrating' => array(

'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'student' => CAP_ALLOW,
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),

'mod/oublog:viewanyrating' => array(

'riskbitmask' => RISK_PERSONAL,
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),

'mod/oublog:viewallratings' => array(

'riskbitmask' => RISK_PERSONAL,
'captype' => 'read',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
),
'clonepermissionsfrom' => 'mod/oublog:viewanyrating'
),

'mod/oublog:rate' => array(

'captype' => 'write',
'contextlevel' => CONTEXT_MODULE,
'archetypes' => array(
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW
)
),
);
5 changes: 5 additions & 0 deletions db/install.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<FIELD NAME="allowimport" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="0 or 1 to enable importing of posts"/>
<FIELD NAME="introonpost" TYPE="int" LENGTH="1" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Show blog intro on post form page"/>
<FIELD NAME="tags" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="Predefine tags to choose from when entering a tag on a post"/>
<FIELD NAME="assessed" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="assesstimestart" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="assesstimefinish" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="scale" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
<FIELD NAME="grading" TYPE="int" LENGTH="10" NOTNULL="true" DEFAULT="0" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
Expand Down
57 changes: 57 additions & 0 deletions db/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,5 +268,62 @@ function xmldb_oublog_upgrade($oldversion=0) {
upgrade_mod_savepoint(true, 2014072501, 'oublog');
}

if ($oldversion < 2014102300) {
// Define field assessed to be added to oublog.
$table = new xmldb_table('oublog');
$field = new xmldb_field('assessed', XMLDB_TYPE_INTEGER, '10',
XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'tags');

// Conditionally launch add field assessed.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Define field assesstimestart to be added to oublog.
$table = new xmldb_table('oublog');
$field = new xmldb_field('assesstimestart', XMLDB_TYPE_INTEGER, '10',
XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'assessed');

// Conditionally launch add field assesstimestart.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Define field assesstimefinish to be added to oublog.
$table = new xmldb_table('oublog');
$field = new xmldb_field('assesstimefinish', XMLDB_TYPE_INTEGER, '10',
XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'assesstimestart');

// Conditionally launch add field assesstimefinish.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Define field scale to be added to oublog.
$table = new xmldb_table('oublog');
$field = new xmldb_field('scale', XMLDB_TYPE_INTEGER, '10',
XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'assesstimefinish');

// Conditionally launch add field scale.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}

// Define field grading to be added to oublog.
$table = new xmldb_table('oublog');
$field = new xmldb_field('grading', XMLDB_TYPE_INTEGER, '10',
XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'scale');

// Conditionally launch add field grading.
if (!$dbman->field_exists($table, $field)) {
$dbman->add_field($table, $field);
}
// Set the new grading field to 1 for everything that isn't a default grade.
$DB->set_field_select('oublog', 'grading', 1, 'grade != 0');

// OUblog savepoint reached.
upgrade_mod_savepoint(true, 2014102300, 'oublog');
}

return true;
}
Loading

0 comments on commit c4e7c21

Please sign in to comment.