From 65a53d9ce98dc51e1e35b50ac13947d87261e30d Mon Sep 17 00:00:00 2001 From: dimonwebb Date: Fri, 26 Nov 2021 15:05:20 +0300 Subject: [PATCH] Moodle assing notifications. --- comment/lib.php | 30 +++++++++++++++++++ mod/assign/lang/en/assign.php | 20 +++++++++++++ mod/assign/locallib.php | 56 ++++++++++++++++++++++++++++++++++- 3 files changed, 105 insertions(+), 1 deletion(-) diff --git a/comment/lib.php b/comment/lib.php index 59536cf7775..e855304f018 100644 --- a/comment/lib.php +++ b/comment/lib.php @@ -683,6 +683,34 @@ class comment { return $str; } + /** + * @param string $eventtype + * @param string $content + * @param stdClass $user + */ + public function notify($eventtype, $content = null, $user = null) + { + global $DB, $CFG, $USER; + + if (!$user) { + $user = $USER; + } + + if ($eventtype == 'add' && $this->component == 'assignsubmission_comments') { + require_once($CFG->dirroot . '/mod/assign/locallib.php'); + $course = get_course($this->courseid); + $context = context::instance_by_id($this->contextid, IGNORE_MISSING); + $info = get_fast_modinfo($course); + $cm = $info->get_cm($context->instanceid); + $submission = $DB->get_record('assign_submission', array('id' => $this->itemid)); + $assign = new assign($context, $cm, $course); + return $assign->notify_about_comments($submission, $content, $user); + } + + // TODO: other comment types? + + } + /** * Add a new comment * @@ -707,6 +735,8 @@ class comment { $newcmt->userid = $USER->id; $newcmt->timecreated = $now; + $this->notify('add', $content, $USER); + // This callback allow module to modify the content of comment, such as filter or replacement plugin_callback($this->plugintype, $this->pluginname, 'comment', 'add', array(&$newcmt, $this->comment_param)); diff --git a/mod/assign/lang/en/assign.php b/mod/assign/lang/en/assign.php index 857efe5eb07..8960216843e 100755 --- a/mod/assign/lang/en/assign.php +++ b/mod/assign/lang/en/assign.php @@ -204,6 +204,26 @@ $string['fixrescalednullgrades'] = 'This assignment contains some erroneous grad $string['fixrescalednullgradesconfirm'] = 'Are you sure you want to fix erroneous grades? All affected grades will be removed. This may affect course totals.'; $string['fixrescalednullgradesdone'] = 'Grades fixed.'; $string['gradecanbechanged'] = 'Grade can be changed'; +$string['studentcommentupdatedtext'] = '{$a->username} has created a comment +for assignment \'{$a->assignment}\' at {$a->timeupdated} + +It is available here: + + {$a->url}'; +$string['studentcommentupdatedhtml'] = '{$a->username} has created a comment +for assignment \'{$a->assignment}\' at {$a->timeupdated}

+It is available on the web site.'; +$string['studentcommentupdatedsmall'] = '{$a->username} has created a comment for assignment {$a->assignment}.'; +$string['gradercommentupdatedtext'] = '{$a->username} has created a comment +for assignment \'{$a->assignment}\' at {$a->timeupdated} + +It is available here: + + {$a->url}'; +$string['gradercommentupdatedhtml'] = '{$a->username} has created a comment +for assignment \'{$a->assignment}\' at {$a->timeupdated}

+It is available on the web site.'; +$string['gradercommentupdatedsmall'] = '{$a->username} has created a comment for assignment {$a->assignment}.'; $string['gradersubmissionupdatedtext'] = '{$a->username} has updated their assignment submission for \'{$a->assignment}\' at {$a->timeupdated} diff --git a/mod/assign/locallib.php b/mod/assign/locallib.php index 6b317f851b2..c31124d3b7c 100755 --- a/mod/assign/locallib.php +++ b/mod/assign/locallib.php @@ -6054,7 +6054,7 @@ class assign { $userfrom->lastname = $uniqueidforuser; $userfrom->email = $CFG->noreplyaddress; } else { - $info->username = fullname($userfrom, true); + $info->username = fullname($userfrom); } $info->assignment = format_string($assignmentname, true, array('context'=>$context)); $info->url = $CFG->wwwroot.'/mod/assign/view.php?id='.$coursemodule->id; @@ -6221,6 +6221,60 @@ class assign { } } + /** + * Notify teacher and students about comments + * + * @param stdClass $submission + * @param string $content + * @param stdClass $user + */ + public function notify_about_comments($submission, $content = null, $user = null) + { + global $DB, $USER; + + if (!$user) { + $user = $USER; + } + + $instance = $this->get_instance(); + + if ($submission->userid == $user->id) { + + // Notify teachers + + $late = $instance->duedate && ($instance->duedate < time()); + + if (!$instance->sendnotifications && !($late && $instance->sendlatenotifications)) { + // No need to do anything. + return; + } + + if ($notifyusers = $this->get_notifiable_users($user->id)) { + foreach ($notifyusers as $notifyuser) { + $this->send_notification($user, + $notifyuser, + 'gradercommentupdated', + 'assign_notification', + time()); + } + } + + } else { + + // Notify student + + $notifyuser = $DB->get_record('user', array('id'=>$submission->userid), '*', MUST_EXIST); + + $this->send_notification($user, + $notifyuser, + 'studentcommentupdated', + 'assign_notification', + time()); + + } + + } + /** * Submit a submission for grading. * -- 2.30.1 (Apple Git-130)