Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activity completion #33

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion backup/moodle2/backup_mediagallery_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected function define_structure() {

// To know if we are including userinfo.
$userinfo = $this->get_setting_value('userinfo');
$includecomments = $this->get_setting_value('comments');

// Define each element separated.
$mediagallery = new backup_nested_element('mediagallery', array('id'), array(
Expand All @@ -40,7 +41,9 @@ protected function define_structure() {
'captionposition', 'galleryfocus', 'carousel', 'grid', 'gridrows',
'gridcolumns', 'enforcedefaults', 'readonlyfrom', 'readonlyto',
'maxbytes', 'maxitems', 'maxgalleries', 'allowcomments', 'allowlikes',
'colltype', 'objectid', 'source', 'mode', 'creator', 'userid',
'colltype',
'completiongalleries', 'completionitems', 'completioncomments',
'objectid', 'source', 'mode', 'creator', 'userid',
));

$userfeedbacks = new backup_nested_element('userfeedback');
Expand All @@ -63,6 +66,16 @@ protected function define_structure() {
'timecreated', 'broadcaster', 'objectid', 'source', 'processing_status', 'creator',
));

$gcomments = new backup_nested_element('gallerycomments');
$gcomment = new backup_nested_element('gallerycomment', ['id'], [
'contextid', 'component', 'commentarea', 'itemid', 'content', 'format', 'userid', 'timecreated',
]);

$icomments = new backup_nested_element('itemcomments');
$icomment = new backup_nested_element('itemcomment', ['id'], [
'contextid', 'component', 'commentarea', 'itemid', 'content', 'format', 'userid', 'timecreated',
]);

$ctags = new backup_nested_element('collectiontags');
$ctag = new backup_nested_element('collectiontag', array('id'), array('itemid', 'rawname'));

Expand All @@ -80,12 +93,16 @@ protected function define_structure() {

$gallerys->add_child($gallery);
$gallery->add_child($items);
$gallery->add_child($gcomments);
$gcomments->add_child($gcomment);
$gallery->add_child($gtags);
$gtags->add_child($gtag);

$items->add_child($item);
$userfeedbacks->add_child($userfeedback);
$item->add_child($userfeedbacks);
$item->add_child($icomments);
$icomments->add_child($icomment);
$item->add_child($itags);
$itags->add_child($itag);

Expand All @@ -98,6 +115,17 @@ protected function define_structure() {
$item->set_source_table('mediagallery_item', array('galleryid' => backup::VAR_PARENTID));
$userfeedback->set_source_table('mediagallery_userfeedback', array('itemid' => backup::VAR_PARENTID));

if ($includecomments) {
$gcomment->set_source_table('comments', [
'contextid' => backup::VAR_CONTEXTID,
'commentarea' => backup_helper::is_sqlparam('gallery'),
'itemid' => backup::VAR_PARENTID]);
$icomment->set_source_table('comments', [
'contextid' => backup::VAR_CONTEXTID,
'commentarea' => backup_helper::is_sqlparam('item'),
'itemid' => backup::VAR_PARENTID]);
}

if (core_tag_tag::is_enabled('mod_mediagallery', 'mediagallery')) {
$ctag->set_source_sql('SELECT t.id, ti.itemid, t.rawname
FROM {tag} t
Expand Down Expand Up @@ -144,6 +172,9 @@ protected function define_structure() {
$gallery->annotate_ids('user', 'userid');
$item->annotate_ids('user', 'userid');

$gcomment->annotate_ids('user', 'userid');
$icomment->annotate_ids('user', 'userid');

// Return the root element (mediagallery), wrapped into standard activity structure.
return $this->prepare_activity_structure($mediagallery);
}
Expand Down
48 changes: 48 additions & 0 deletions backup/moodle2/restore_mediagallery_stepslib.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected function define_structure() {

$paths = array();
$userinfo = $this->get_setting_value('userinfo');
$includecomments = $this->get_setting_value('comments');

$mediagallery = new restore_path_element('mediagallery', '/activity/mediagallery');
$paths[] = $mediagallery;
Expand All @@ -51,6 +52,13 @@ protected function define_structure() {
'/activity/mediagallery/gallerys/gallery/items/item/userfeedback/feedback');
$paths[] = $userfeedback;

if ($includecomments) {
$paths[] = new restore_path_element('mediagallery_gcomment',
'/activity/mediagallery/gallerys/gallery/gallerycomments/gallerycomment');
$paths[] = new restore_path_element('mediagallery_icomment',
'/activity/mediagallery/gallerys/gallery/items/item/itemcomments/itemcomment');
}

$paths[] = new restore_path_element('mediagallery_ctag', '/activity/mediagallery/collectiontags/collectiontag');
$paths[] = new restore_path_element('mediagallery_gtag',
'/activity/mediagallery/gallerys/gallery/gallerytags/gallerytag');
Expand Down Expand Up @@ -127,6 +135,46 @@ protected function process_mediagallery_item($data) {
$this->set_mapping('mediagallery_item', $oldid, $newitemid, true);
}

/**
* Process a gallery comment.
*
* @param object|array $data
* @return void
*/
protected function process_mediagallery_gcomment($data): void {
global $DB;

$data = (object)$data;
$oldid = $data->id;

$data->itemid = $this->get_new_parentid('mediagallery_gallery');
$data->timecreated = $this->apply_date_offset($data->timecreated);
$data->userid = $this->get_mappingid('user', $data->userid);
$data->contextid = $this->get_mappingid('context', $data->contextid);
$newcommentid = $DB->insert_record('comments', $data);
$this->set_mapping('comments', $oldid, $newcommentid, true);
}

/**
* Process an item comment.
*
* @param object|array $data
* @return void
*/
protected function process_mediagallery_icomment($data): void {
global $DB;

$data = (object)$data;
$oldid = $data->id;

$data->itemid = $this->get_new_parentid('mediagallery_item');
$data->timecreated = $this->apply_date_offset($data->timecreated);
$data->userid = $this->get_mappingid('user', $data->userid);
$data->contextid = $this->get_mappingid('context', $data->contextid);
$newcommentid = $DB->insert_record('comments', $data);
$this->set_mapping('comments', $oldid, $newcommentid, true);
}

protected function process_mediagallery_ctag($data) {
$data = (object)$data;
if (!core_tag_tag::is_enabled('mod_mediagallery', 'mediagallery')) {
Expand Down
128 changes: 128 additions & 0 deletions classes/completion/custom_completion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

declare(strict_types=1);

namespace mod_mediagallery\completion;

use core_completion\activity_custom_completion;

/**
* Activity custom completion subclass for the Media collection activity.
*
* Class for defining mod_mediagallery's custom completion rules and fetching the completion statuses
* of the custom completion rules for a given Media collection instance and a user.
*
* @package mod_mediagallery
* @copyright 2023 Otago Polytechnic
* @author James Calder
* @copyright based on work by Simey Lameze <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class custom_completion extends activity_custom_completion {

/**
* Fetches the completion state for a given completion rule.
*
* @param string $rule The completion rule.
* @return int The completion state.
*/
public function get_state(string $rule): int {
global $DB;

$this->validate_rule($rule);

$userid = $this->userid;
$collectionid = $this->cm->instance;

if (!$collection = $DB->get_record('mediagallery', ['id' => $collectionid])) {
throw new \moodle_exception('Unable to find Media collection with id ' . $collectionid);
}

$itemcountparams = ['collectionid' => $collectionid, 'userid' => $userid];
$itemcountsql =
"SELECT COUNT(*)
FROM {mediagallery_item} mgi
JOIN {mediagallery_gallery} mgg ON mgg.id = mgi.galleryid
WHERE mgg.instanceid = :collectionid AND mgi.userid = :userid";

$commentcountparams = ['collectionid' => $collectionid, 'userid' => $userid];
$commentcountsql =
"SELECT COUNT(*)
FROM {comments} c
LEFT JOIN {mediagallery_item} mgi
ON c.component = 'mod_mediagallery' AND c.commentarea = 'item' AND mgi.id = c.itemid
JOIN {mediagallery_gallery} mgg ON c.component = 'mod_mediagallery'
AND (c.commentarea = 'gallery' AND mgg.id = c.itemid
OR c.commentarea = 'item' AND mgg.id = mgi.galleryid)
WHERE c.component = 'mod_mediagallery' AND mgg.instanceid = :collectionid AND c.userid = :userid";

if ($rule == 'completiongalleries') {
$status = $collection->completiongalleries <=
$DB->count_records('mediagallery_gallery', ['instanceid' => $collectionid, 'userid' => $userid]);
} else if ($rule == 'completionitems') {
$status = $collection->completionitems <= $DB->get_field_sql($itemcountsql, $itemcountparams);
} else if ($rule == 'completioncomments') {
$status = $collection->completioncomments <= $DB->get_field_sql($commentcountsql, $commentcountparams);
}

return $status ? COMPLETION_COMPLETE : COMPLETION_INCOMPLETE;
}

/**
* Fetch the list of custom completion rules that this module defines.
*
* @return string[]
*/
public static function get_defined_custom_rules(): array {
return [
'completiongalleries',
'completionitems',
'completioncomments',
];
}

/**
* Returns an associative array of the descriptions of custom completion rules.
*
* @return string[]
*/
public function get_custom_rule_descriptions(): array {
$completiongalleries = $this->cm->customdata['customcompletionrules']['completiongalleries'] ?? 0;
$completionitems = $this->cm->customdata['customcompletionrules']['completionitems'] ?? 0;
$completioncomments = $this->cm->customdata['customcompletionrules']['completioncomments'] ?? 0;

return [
'completiongalleries' => get_string('completiondetail:galleries', 'mediagallery', $completiongalleries),
'completionitems' => get_string('completiondetail:items', 'mediagallery', $completionitems),
'completioncomments' => get_string('completiondetail:comments', 'mediagallery', $completioncomments),
];
}

/**
* Returns an array of all completion rules, in the order they should be displayed to users.
*
* @return string[]
*/
public function get_sort_order(): array {
return [
'completionview',
'completiongalleries',
'completionitems',
'completioncomments',
];
}
}
50 changes: 50 additions & 0 deletions classes/event/comment_created.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* The mod_mediagallery comment created event.
*
* @package mod_mediagallery
* @copyright 2023 Otago Polytechnic
* @author James Calder
* @copyright based on work by 2013 Rajesh Taneja <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_mediagallery\event;

/**
* The mod_mediagallery comment created event class.
*
* @package mod_mediagallery
* @copyright 2023 Otago Polytechnic
* @author James Calder
* @copyright based on work by 2013 Rajesh Taneja <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class comment_created extends \core\event\comment_created {

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description(): string {
return "The user with id '$this->userid' added the comment with id '$this->objectid' " .
"to the Media collection activity with course module id '$this->contextinstanceid'.";
}

}
50 changes: 50 additions & 0 deletions classes/event/comment_deleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* The mod_mediagallery comment deleted event.
*
* @package mod_mediagallery
* @copyright 2023 Otago Polytechnic
* @author James Calder
* @copyright based on work by 2013 Rajesh Taneja <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

namespace mod_mediagallery\event;

/**
* The mod_mediagallery comment deleted event class.
*
* @package mod_mediagallery
* @copyright 2023 Otago Polytechnic
* @author James Calder
* @copyright based on work by 2013 Rajesh Taneja <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class comment_deleted extends \core\event\comment_deleted {

/**
* Returns description of what happened.
*
* @return string
*/
public function get_description(): string {
return "The user with id '$this->userid' deleted the comment with id '$this->objectid' " .
"from the Media collection activity with course module id '$this->contextinstanceid'.";
}

}
Loading
Loading