/** * 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 $anonforum anonforum instance (with magic quotes) * @return bool success */ function anonforum_update_instance($anonforum, $mform) { global $DB, $OUTPUT, $USER; $anonforum->timemodified = time(); $anonforum->id = $anonforum->instance; if (empty($anonforum->assessed)) { $anonforum->assessed = 0; } if (empty($anonforum->ratingtime) or empty($anonforum->assessed)) { $anonforum->assesstimestart = 0; $anonforum->assesstimefinish = 0; } $oldanonforum = $DB->get_record('anonforum', array('id' => $anonforum->id)); if ($anonforum->type == 'single') { // Update related discussion and post. $discussions = $DB->get_records('anonforum_discussions', array('anonforum' => $anonforum->id), 'timemodified ASC'); if (!empty($discussions)) { if (count($discussions) > 1) { echo $OUTPUT->notification(get_string('warnformorepost', 'anonforum')); } $discussion = array_pop($discussions); } else { // try to recover by creating initial discussion - MDL-16262 $discussion = new stdClass(); $discussion->course = $anonforum->course; $discussion->anonforum = $anonforum->id; $discussion->name = $anonforum->name; $discussion->assessed = $anonforum->assessed; $discussion->message = $anonforum->intro; $discussion->messageformat = $anonforum->introformat; $discussion->messagetrust = true; $discussion->mailnow = false; $discussion->groupid = -1; $discussion->anonymouspost = $anonforum->anonymous; $message = ''; anonforum_add_discussion($discussion, null, $message); if (!($discussion = $DB->get_record('anonforum_discussions', array('anonforum' => $anonforum->id)))) { print_error('cannotadd', 'anonforum'); } } if (!($post = $DB->get_record('anonforum_posts', array('id' => $discussion->firstpost)))) { print_error('cannotfindfirstpost', 'anonforum'); } $cm = get_coursemodule_from_instance('anonforum', $anonforum->id); $modcontext = context_module::instance($cm->id, MUST_EXIST); $post = $DB->get_record('anonforum_posts', array('id' => $discussion->firstpost), '*', MUST_EXIST); $post->subject = $anonforum->name; $post->message = $anonforum->intro; $post->messageformat = $anonforum->introformat; $post->messagetrust = trusttext_trusted($modcontext); $post->modified = $anonforum->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_anonforum', 'post', $post->id, $options, $post->message); } $DB->update_record('anonforum_posts', $post); $discussion->name = $anonforum->name; $DB->update_record('anonforum_discussions', $discussion); } $DB->update_record('anonforum', $anonforum); $modcontext = context_module::instance($anonforum->coursemodule); if ($anonforum->forcesubscribe == ANONFORUM_INITIALSUBSCRIBE && $oldanonforum->forcesubscribe != $anonforum->forcesubscribe) { $users = anonforum_get_potential_subscribers($modcontext, 0, 'u.id, u.email', ''); foreach ($users as $user) { anonforum_subscribe($user->id, $anonforum->id); } } anonforum_grade_item_update($anonforum); return true; }
/** * 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 anonymous forum discussion count. $this->anonforumdiscussioncount++; $record = (array) $record; if (!isset($record['course'])) { throw new coding_exception('course must be present in phpunit_util::create_discussion() $record'); } if (!isset($record['anonforum'])) { throw new coding_exception('anonymous forum 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->anonforumdiscussioncount; } if (!isset($record['subject'])) { $record['subject'] = "Subject for discussion " . $this->anonforumdiscussioncount; } if (!isset($record['message'])) { $record['message'] = html_writer::tag('p', 'Message for discussion ' . $this->anonforumdiscussioncount); } 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"; } if (!isset($record['anonymouspost'])) { $record['anonymouspost'] = "0"; } $record = (object) $record; // Add the discussion. $record->id = anonforum_add_discussion($record, null, null, $record->userid); return $record; }
protected function after_execute() { global $DB; // Add anonforum related files, no need to match by itemname (just internally handled context) $this->add_related_files('mod_anonforum', 'intro', null); // If the anonforum is of type 'single' and no discussion has been ignited // (non-userinfo backup/restore) create the discussion here, using anonforum // information as base for the initial post. $anonforumid = $this->task->get_activityid(); $anonforumrec = $DB->get_record('anonforum', array('id' => $anonforumid)); if ($anonforumrec->type == 'single' && !$DB->record_exists('anonforum_discussions', array('anonforum' => $anonforumid))) { // Create single discussion/lead post from anonymous forum data $sd = new stdclass(); $sd->course = $anonforumrec->course; $sd->anonforum = $anonforumrec->id; $sd->name = $anonforumrec->name; $sd->assessed = $anonforumrec->assessed; $sd->message = $anonforumrec->intro; $sd->messageformat = $anonforumrec->introformat; $sd->messagetrust = true; $sd->mailnow = false; $sdid = anonforum_add_discussion($sd, null, null, $this->task->get_userid()); // Mark the post as mailed $DB->set_field('anonforum_posts', 'mailed', '1', array('discussion' => $sdid)); // Copy all the files from mod_foum/intro to mod_anonforum/post $fs = get_file_storage(); $files = $fs->get_area_files($this->task->get_contextid(), 'mod_anonforum', 'intro'); foreach ($files as $file) { $newfilerecord = new stdclass(); $newfilerecord->filearea = 'post'; $newfilerecord->itemid = $DB->get_field('anonforum_discussions', 'firstpost', array('id' => $sdid)); $fs->create_file_from_storedfile($newfilerecord, $file); } } // Add post related files, matching by itemname = 'anonforum_post' $this->add_related_files('mod_anonforum', 'post', 'anonforum_post'); $this->add_related_files('mod_anonforum', 'attachment', 'anonforum_post'); }