protected function definition() { global $OUTPUT, $PAGE, $COURSE, $CFG, $USER; $mform =& $this->_form; $courseid = $this->_customdata['courseid']; $postid = !empty($this->_customdata['options']->postid) ? $this->_customdata['options']->postid : 0; // Update of save a post. $action = $postid > 0 ? 'updatepost' : 'savepost'; $context = context_course::instance($courseid); // ...get formparameters from cache. $cache = cache::make('format_socialwall', 'postformparams'); $formparams = $cache->get($courseid . '_' . $postid); $loadposteditor = optional_param('loadposteditor', -1, PARAM_INT); if ($loadposteditor != -1) { $formparams['loadposteditor'] = $loadposteditor; // ...remember this setting, if page is reloaded. $cache->set($courseid . '_' . $postid, $formparams); } // ...get errors from cache and set them to elements. $errorcache = cache::make('format_socialwall', 'postformerrors'); if ($errors = $errorcache->get($courseid)) { foreach ($errors as $element => $error) { $mform->setElementError($element, $error['message']); } } $errorcache->delete($courseid); // ... value of this element is set by javascript (postform.js) before submit. $mform->addElement('hidden', 'cmsequence', '', array('id' => 'cmsequence')); $mform->setType('cmsequence', PARAM_TEXT); $mform->setDefault('cmsequence', ''); // ... posttext. $buttongroup = array(); $buttongroup[] = $mform->createElement('submit', 'submitbutton', get_string($action, 'format_socialwall')); if ($action == 'updatepost') { $buttongroup[] = $mform->createElement('cancel'); } $mform->addGroup($buttongroup); // ... htmleditor/texarea to post text. $canposthtml = has_capability('format/socialwall:posthtml', $context); $showeditor = (!empty($formparams['loadposteditor']) and $canposthtml); $params = array('class' => 'sw-texarea', 'id' => 'posttext'); if ($showeditor) { $mform->addElement('editor', 'posttext', get_string('poststatusordnote', 'format_socialwall'), $params); $mform->setType('posttext', PARAM_RAW); if (isset($formparams['posttext'])) { $element = $mform->getElement('posttext'); $element->setValue(array('text' => $formparams['posttext'])); } } else { $mform->addElement('textarea', 'posttext', get_string('poststatusordnote', 'format_socialwall'), $params); $mform->setType('posttext', PARAM_TEXT); if (isset($formparams['posttext'])) { $mform->setDefault('posttext', $formparams['posttext']); } } $postoptions = array(); // ... Select group. $groupmode = groups_get_course_groupmode($COURSE); if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/course:managegroups', $context)) { $allgroups = groups_get_all_groups($courseid, $USER->id); } else { $allgroups = groups_get_all_groups($courseid); } $groupsmenu = array(); $groupsmenu[0] = get_string('allparticipants'); foreach ($allgroups as $gid => $unused) { $groupsmenu[$gid] = format_string($allgroups[$gid]->name); } if (count($groupsmenu) > 0) { $postoptions[] = $mform->createElement('select', 'togroupid', '', $groupsmenu); if (isset($formparams['togroupid'])) { $mform->setDefault('togroupid', $formparams['togroupid']); } } // ... options group. $poststatusmenu = array(0 => get_string('poststatus', 'format_socialwall')); if (has_capability('format/socialwall:makesticky', $context)) { $poststatusmenu[1] = get_string('makesticky', 'format_socialwall'); } if (has_capability('format/socialwall:postprivate', $context)) { $poststatusmenu[2] = get_string('privatepost', 'format_socialwall'); } if ($PAGE->user_allowed_editing()) { $poststatusmenu[4] = get_string('makealert', 'format_socialwall'); } if (count($poststatusmenu) > 1) { $postoptions[] = $mform->createElement('select', 'poststatus', '', $poststatusmenu); if (isset($formparams['poststatus'])) { $mform->setDefault('poststatus', $formparams['poststatus']); } } // ...switch htmleditor on/off. if ($canposthtml) { $key = !empty($formparams['loadposteditor']) ? 'turneditoroff' : 'turneditoron'; $postoptions[] = $mform->createElement('submit', $key, get_string($key, 'format_socialwall')); } if (count($postoptions) > 0) { $mform->addGroup($postoptions); } // ... display the activites prepared for the next post only by a teacher. if ($PAGE->user_allowed_editing()) { if (!isset($USER->editing) or !$USER->editing) { $addstr = get_string('addactivityresource', 'format_socialwall'); $mform->addElement('submit', 'turneditingon', $addstr, array('id' => 'sw-addactivitylink')); } } else { $o = html_writer::tag('div', '', array('class' => 'clearfix')); $mform->addElement('html', $o); // ...upload options for all users, which cannot edit page. $attachgroup = array(); $course = course_get_format($COURSE)->get_course(); $canpostfile = has_capability('format/socialwall:postfile', $context) && !empty($course->enablestudentupload); if ($canpostfile) { $uploadfileicon = html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('icon', 'resource'))); $linktext = $uploadfileicon . get_string('uploadafile', 'format_socialwall'); $url = new moodle_url('/course/view.php', array('id' => $courseid, 'loadfilemanager' => 1)); $link = html_writer::link($url, $linktext, array('id' => 'uploadfile')); $attachgroup[] = $mform->createElement('static', 'uploadfile', '', $link); } $canposturl = has_capability('format/socialwall:posturl', $context) && !empty($course->enablestudentupload); if ($canposturl) { $addlinkicon = html_writer::empty_tag('img', array('src' => $OUTPUT->pix_url('icon', 'url'))); $at = html_writer::link('#', $addlinkicon . get_string('addalink', 'format_socialwall'), array('id' => 'addalink')); $attachgroup[] = $mform->createElement('static', 'addalink', '', $at); } if (!empty($attachgroup)) { $mform->addGroup($attachgroup); } $loadfilemanager = optional_param('loadfilemanager', 0, PARAM_INT); if ($canpostfile and $loadfilemanager == 1) { $mform->addElement('html', html_writer::start_div('', array('id' => 'fileswrapper'))); // ... filemanager. $filemanageroptions = array(); $filemanageroptions['accepted_types'] = '*'; $filemanageroptions['maxbytes'] = 0; $filemanageroptions['maxfiles'] = 1; $filemanageroptions['mainfile'] = true; $mform->addElement('filemanager', 'files', get_string('selectfiles'), array(), $filemanageroptions); $mform->addElement('html', html_writer::end_div()); $mform->addElement('hidden', 'loadfilemanager', '1', array('id' => 'loadfilemanager')); $mform->setType('loadfilemanager', PARAM_INT); } // ...external url. $style = isset($errors['externalurl']) ? 'display:auto' : 'display:none'; $mform->addElement('html', html_writer::start_div('', array('id' => 'externalurlwrapper', 'style' => $style))); $mform->addElement('url', 'externalurl', get_string('externalurl', 'url'), array('size' => '60'), array('usefilepicker' => true)); $mform->setType('externalurl', PARAM_URL); if (isset($errors['externalurl'])) { $mform->setDefault('externalurl', $errors['externalurl']['value']); } // ... get urlresource filter a try. $filters = filter_get_active_in_context($context); if (isset($filters['urlresource'])) { require_once $CFG->dirroot . '/filter/urlresource/lib.php'; filter_url_resource_helper::add_postformfields($mform, $courseid); } $mform->addElement('html', html_writer::end_div()); } // Id of post to remember the update option for further pageloads. $mform->addElement('hidden', 'id', 0, array('id' => 'id')); $mform->setType('id', PARAM_INT); $mform->setDefault('id', $postid); // Id of course we are in. $mform->addElement('hidden', 'courseid'); $mform->setType('courseid', PARAM_INT); $mform->setDefault('courseid', $courseid); $mform->addElement('hidden', 'action', $action); $mform->setType('action', PARAM_TEXT); $mform->disable_form_change_checker(); }
/** * Save a post using the submitted data * * @param type $data * @return type */ public function save_post($data, $course) { global $USER, $DB, $CFG, $PAGE; require_once $CFG->dirroot . '/mod/url/locallib.php'; $context = \context_course::instance($data->courseid); // ... create post. $post = new \stdClass(); $update = false; // If id is not empty, ensure that post is existing and test whether user is updating the post. if (!empty($data->id)) { if ($exists = $DB->get_record('format_socialwall_posts', array('id' => $data->id))) { $post = $exists; // Check, whether user is allowed to update the post. $caneditpost = ($post->fromuserid == $USER->id and has_capability('format/socialwall:updateownpost', $context)); $caneditpost = ($caneditpost or has_capability('format/socialwall:updateanypost', $context)); if (!$caneditpost) { print_error('missingcapupdatepost', 'format_socialwall'); } $update = true; } else { print_error('noposttoupdate', 'format_socialwall'); } } // ...save when even a posttext or a externalurl or a file or a actvitiy is given. $cmsequence = $data->cmsequence; // ... are there added activities? if (!empty($cmsequence)) { $cmsequence = $this->check_and_move_module($data->courseid, $cmsequence); } // If user may not add any activity but may add a file or a link, replace existing files with new file. if (!$PAGE->user_allowed_editing()) { // ... add a resource. if (!empty($data->files)) { $canpostfile = has_capability('format/socialwall:postfile', $context) && !empty($course->enablestudentupload); if (!$canpostfile) { print_error('missingcappostfile', 'format_socialwall'); } $cmsequence = $this->create_mod_files($data); } else { // ... check externalurl and create a activity in section 1, if necessary. if (!empty($data->externalurl)) { $canposturl = has_capability('format/socialwall:posturl', $context) && !empty($course->enablestudentupload); if (!$canposturl) { print_error('missingcapposturl', 'format_socialwall'); } $cmsequence = $this->create_mod_url($data->externalurl); // ... set filter Plugin here. $filters = filter_get_active_in_context($context); if (isset($filters['urlresource'])) { require_once $CFG->dirroot . '/filter/urlresource/lib.php'; \filter_url_resource_helper::save_externalurl($data, $cmsequence); } } } } if (empty($data->posttext) and empty($cmsequence)) { print_error('attachmentorpostrequired', 'format_socialwall'); } $post->courseid = $data->courseid; $post->fromuserid = $USER->id; $post->togroupid = $data->togroupid; if (is_array($data->posttext)) { $posttext = $data->posttext['text']; } else { $posttext = $data->posttext; } if (has_capability('format/socialwall:posthtml', $context)) { $post->posttext = clean_text($posttext); } else { $post->posttext = clean_param($posttext, PARAM_NOTAGS); } if (isset($data->poststatus)) { $post->sticky = $data->poststatus == 1; $post->private = $data->poststatus == 2; $post->alert = $data->poststatus == 4; } else { $post->sticky = 0; $post->private = 0; $post->alert = 0; } if ($update) { $post->timemodified = time(); $DB->update_record('format_socialwall_posts', $post); // ...reset postid if post was updated. $cache = \cache::make('format_socialwall', 'timelinefilter'); $cache->purge_current_user(); } else { $post->timecreated = time(); $post->timemodified = $post->timecreated; $post->id = $DB->insert_record('format_socialwall_posts', $post); } attaches::save_attaches($post->id, $cmsequence); // We use a instant enqueueing, if needed you might use events here. notification::enqueue_post_created($post); // ...clear the inputed values. $cache = \cache::make('format_socialwall', 'postformparams'); $cache->purge(); // ...clear the attached actvities. $cache = \cache::make('format_socialwall', 'attachedrecentactivities'); $cache->purge(); return array('error' => '0', 'message' => 'postsaved'); }
/** save a post using the submitted data * * @global type $USER * @global obejct $DB * @param type $data * @return type */ public function save_post($data, $course) { global $USER, $DB, $CFG; require_once $CFG->dirroot . '/mod/url/locallib.php'; $context = \context_course::instance($data->id); // ...save when even a posttext or a externalurl or a file or a actvitiy is given. $cmsequence = $data->cmsequence; // ... added activity? if (!empty($cmsequence)) { $cmsequence = $this->check_and_move_module($data->id, $cmsequence); } else { // ... add a resource. if (!empty($data->files)) { $canpostfile = has_capability('format/socialwall:postfile', $context) && !empty($course->enablestudentupload); if (!$canpostfile) { print_error('missingcappostfile', 'format_socialwall'); } $cmsequence = $this->create_mod_files($data); } else { // ... check externalurl and create a activity in section 1, if necessary. if (!empty($data->externalurl)) { $canposturl = has_capability('format/socialwall:posturl', $context) && !empty($course->enablestudentupload); if (!$canposturl) { print_error('missingcapposturl', 'format_socialwall'); } $cmsequence = $this->create_mod_url($data->externalurl); // ... set filter Plugin here. $filters = filter_get_active_in_context($context); if (isset($filters['urlresource'])) { require_once $CFG->dirroot . '/filter/urlresource/lib.php'; \filter_url_resource_helper::save_externalurl($data, $cmsequence); } } } } if (empty($data->posttext) and empty($cmsequence)) { print_error('attachmentorpostrequired', 'format_socialwall'); } // ... create post. $post = new \stdClass(); $post->courseid = $data->id; $post->fromuserid = $USER->id; $post->togroupid = $data->togroupid; if (is_array($data->posttext)) { $posttext = $data->posttext['text']; } else { $posttext = $data->posttext; } if (has_capability('format/socialwall:posthtml', $context)) { $post->posttext = clean_text($posttext); } else { $post->posttext = clean_param($posttext, PARAM_NOTAGS); } if (isset($data->poststatus)) { $post->sticky = $data->poststatus == 1; $post->private = $data->poststatus == 2; $post->alert = $data->poststatus == 4; } else { $post->sticky = 0; $post->private = 0; $post->alert = 0; } $post->timecreated = time(); $post->timemodified = $post->timecreated; $post->id = $DB->insert_record('format_socialwall_posts', $post); if (!empty($cmsequence)) { attaches::save_attaches($post->id, $cmsequence); } // We use a instant enqueueing, if needed you might use events here. notification::enqueue_post_created($post); // ...clear the inputed values. $cache = \cache::make('format_socialwall', 'postformparams'); $cache->purge(); return array('error' => '0', 'message' => 'postsaved'); }