/** * Function to create a dummy discussion. * * @param array|stdClass $record * @return stdClass the discussion object */ public function create_discussion($record = null) { global $DB; // Increment the twf discussion count. $this->twfdiscussioncount++; $record = (array) $record; if (!isset($record['course'])) { throw new coding_exception('course must be present in phpunit_util::create_discussion() $record'); } if (!isset($record['twf'])) { throw new coding_exception('twf must be present in phpunit_util::create_discussion() $record'); } if (!isset($record['userid'])) { throw new coding_exception('userid must be present in phpunit_util::create_discussion() $record'); } if (!isset($record['name'])) { $record['name'] = "Discussion " . $this->twfdiscussioncount; } if (!isset($record['subject'])) { $record['subject'] = "Subject for discussion " . $this->twfdiscussioncount; } if (!isset($record['message'])) { $record['message'] = html_writer::tag('p', 'Message for discussion ' . $this->twfdiscussioncount); } if (!isset($record['messageformat'])) { $record['messageformat'] = editors_get_preferred_format(); } if (!isset($record['messagetrust'])) { $record['messagetrust'] = ""; } if (!isset($record['assessed'])) { $record['assessed'] = '1'; } if (!isset($record['groupid'])) { $record['groupid'] = "-1"; } if (!isset($record['timestart'])) { $record['timestart'] = "0"; } if (!isset($record['timeend'])) { $record['timeend'] = "0"; } if (!isset($record['mailnow'])) { $record['mailnow'] = "0"; } $record = (object) $record; // Add the discussion. $record->id = twf_add_discussion($record, null, null, $record->userid); return $record; }
} else { // Use the value for all participants instead. $groupstopostto[] = -1; } } } // Before we post this we must check that the user will not exceed the blocking threshold. twf_check_blocking_threshold($thresholdwarning); foreach ($groupstopostto as $group) { if (!twf_user_can_post_discussion($twf, $group, -1, $cm, $modcontext)) { print_error('cannotcreatediscussion', 'twf'); } //var_dump($discussion);die; $discussion->groupid = $group; $message = ''; if ($discussion->id = twf_add_discussion($discussion, $mform_post, $message)) { $params = array('context' => $modcontext, 'objectid' => $discussion->id, 'other' => array('twfid' => $twf->id)); $event = \mod_twf\event\discussion_created::create($params); $event->add_record_snapshot('twf_discussions', $discussion); $event->trigger(); $timemessage = 2; if (!empty($message)) { // If we're printing stuff about the file upload. $timemessage = 4; } if ($fromform->mailnow) { $message .= get_string("postmailnow", "twf"); $timemessage = 4; } else { $message .= '<p>' . get_string("postaddedsuccess", "twf") . '</p>'; $message .= '<p>' . get_string("postaddedtimeleft", "twf", format_time($CFG->maxeditingtime)) . '</p>';
protected function after_execute() { global $DB; // Add twf related files, no need to match by itemname (just internally handled context) $this->add_related_files('mod_twf', 'intro', null); // If the twf is of type 'single' and no discussion has been ignited // (non-userinfo backup/restore) create the discussion here, using twf // information as base for the initial post. $twfid = $this->task->get_activityid(); $twfrec = $DB->get_record('twf', array('id' => $twfid)); if ($twfrec->type == 'single' && !$DB->record_exists('twf_discussions', array('twf' => $twfid))) { // Create single discussion/lead post from twf data $sd = new stdclass(); $sd->course = $twfrec->course; $sd->twf = $twfrec->id; $sd->name = $twfrec->name; $sd->assessed = $twfrec->assessed; $sd->message = $twfrec->intro; $sd->messageformat = $twfrec->introformat; $sd->messagetrust = true; $sd->mailnow = false; $sdid = twf_add_discussion($sd, null, null, $this->task->get_userid()); // Mark the post as mailed $DB->set_field('twf_posts', 'mailed', '1', array('discussion' => $sdid)); // Copy all the files from mod_foum/intro to mod_twf/post $fs = get_file_storage(); $files = $fs->get_area_files($this->task->get_contextid(), 'mod_twf', 'intro'); foreach ($files as $file) { $newfilerecord = new stdclass(); $newfilerecord->filearea = 'post'; $newfilerecord->itemid = $DB->get_field('twf_discussions', 'firstpost', array('id' => $sdid)); $fs->create_file_from_storedfile($newfilerecord, $file); } } // Add post related files, matching by itemname = 'twf_post' $this->add_related_files('mod_twf', 'post', 'twf_post'); $this->add_related_files('mod_twf', 'attachment', 'twf_post'); }
/** * Given an object containing all the necessary data, * (defined by the form in mod_form.php) this function * will update an existing instance with new data. * * @global object * @param object $twf twf instance (with magic quotes) * @return bool success */ function twf_update_instance($twf, $mform) { global $DB, $OUTPUT, $USER; $twf->timemodified = time(); $twf->id = $twf->instance; if (empty($twf->assessed)) { $twf->assessed = 0; } if (empty($twf->ratingtime) or empty($twf->assessed)) { $twf->assesstimestart = 0; $twf->assesstimefinish = 0; } $oldtwf = $DB->get_record('twf', array('id' => $twf->id)); // MDL-3942 - if the aggregation type or scale (i.e. max grade) changes then recalculate the grades for the entire twf // if scale changes - do we need to recheck the ratings, if ratings higher than scale how do we want to respond? // for count and sum aggregation types the grade we check to make sure they do not exceed the scale (i.e. max score) when calculating the grade if ($oldtwf->assessed != $twf->assessed or $oldtwf->scale != $twf->scale) { twf_update_grades($twf); // recalculate grades for the twf } if ($twf->type == 'single') { // Update related discussion and post. $discussions = $DB->get_records('twf_discussions', array('twf' => $twf->id), 'timemodified ASC'); if (!empty($discussions)) { if (count($discussions) > 1) { echo $OUTPUT->notification(get_string('warnformorepost', 'twf')); } $discussion = array_pop($discussions); } else { // try to recover by creating initial discussion - MDL-16262 $discussion = new stdClass(); $discussion->course = $twf->course; $discussion->twf = $twf->id; $discussion->name = $twf->name; $discussion->assessed = $twf->assessed; $discussion->message = $twf->intro; $discussion->messageformat = $twf->introformat; $discussion->messagetrust = true; $discussion->mailnow = false; $discussion->groupid = -1; $message = ''; twf_add_discussion($discussion, null, $message); if (!($discussion = $DB->get_record('twf_discussions', array('twf' => $twf->id)))) { print_error('cannotadd', 'twf'); } } if (!($post = $DB->get_record('twf_posts', array('id' => $discussion->firstpost)))) { print_error('cannotfindfirstpost', 'twf'); } $cm = get_coursemodule_from_instance('twf', $twf->id); $modcontext = context_module::instance($cm->id, MUST_EXIST); $post = $DB->get_record('twf_posts', array('id' => $discussion->firstpost), '*', MUST_EXIST); $post->subject = $twf->name; $post->message = $twf->intro; $post->messageformat = $twf->introformat; $post->messagetrust = trusttext_trusted($modcontext); $post->modified = $twf->timemodified; $post->userid = $USER->id; // MDL-18599, so that current teacher can take ownership of activities. if ($mform and $draftid = file_get_submitted_draft_itemid('introeditor')) { // Ugly hack - we need to copy the files somehow. $options = array('subdirs' => true); // Use the same options as intro field! $post->message = file_save_draft_area_files($draftid, $modcontext->id, 'mod_twf', 'post', $post->id, $options, $post->message); } $DB->update_record('twf_posts', $post); $discussion->name = $twf->name; $DB->update_record('twf_discussions', $discussion); } $DB->update_record('twf', $twf); $modcontext = context_module::instance($twf->coursemodule); if ($twf->forcesubscribe == FORUM_INITIALSUBSCRIBE && $oldtwf->forcesubscribe != $twf->forcesubscribe) { $users = \mod_twf\subscriptions::get_potential_subscribers($modcontext, 0, 'u.id, u.email', ''); foreach ($users as $user) { \mod_twf\subscriptions::subscribe_user($user->id, $twf, $modcontext); } } twf_grade_item_update($twf); return true; }