/**
  * 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');
 }
Пример #2
0
 /** 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');
 }