[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/blog/ -> edit.php (source)

   1  <?php //$Id: edit.php,v 1.49.2.12 2008/07/07 07:40:35 scyrma Exp $
   2  
   3  require_once ('../config.php');
   4  include_once ('lib.php');
   5  include_once($CFG->dirroot.'/tag/lib.php');
   6  
   7  $action   = required_param('action', PARAM_ALPHA);
   8  $id       = optional_param('id', 0, PARAM_INT);
   9  $confirm  = optional_param('confirm', 0, PARAM_BOOL);
  10  $courseid = optional_param('courseid', 0, PARAM_INT); // needed for user tab - does nothing here
  11  
  12  require_login($courseid);
  13  
  14  if (empty($CFG->bloglevel)) {
  15      error('Blogging is disabled!');
  16  }
  17  
  18  if (isguest()) {
  19      print_error('noguestpost', 'blog');
  20  }
  21  
  22  $sitecontext = get_context_instance(CONTEXT_SYSTEM);
  23  if (!has_capability('moodle/blog:create', $sitecontext) and !has_capability('moodle/blog:manageentries', $sitecontext)) {
  24      error('You can not post or edit blogs.');
  25  }
  26  
  27  // Make sure that the person trying to edit have access right
  28  if ($id) {
  29      if (!$existing = get_record('post', 'id', $id)) {
  30          error('Wrong blog post id');
  31      }
  32  
  33      if (!blog_user_can_edit_post($existing)) {
  34          print_error('notallowedtoedit', 'blog');
  35      }
  36      $userid    = $existing->userid;
  37      $returnurl = $CFG->wwwroot.'/blog/index.php?userid='.$existing->userid;
  38  } else {
  39      if (!has_capability('moodle/blog:create', $sitecontext)) {
  40          print_error('nopost', 'blog'); // manageentries is not enough for adding
  41      }
  42      $existing  = false;
  43      $userid    = $USER->id;
  44      $returnurl = 'index.php?userid='.$USER->id;
  45  }
  46  if (!empty($courseid)) {
  47      $returnurl .= '&amp;courseid='.$courseid;
  48  }
  49  
  50  
  51  $strblogs = get_string('blogs','blog');
  52  
  53  if ($action=='delete'){
  54      if (!$existing) {
  55          error('Incorrect blog post id');
  56      }
  57      if (data_submitted() and $confirm and confirm_sesskey()) {
  58          do_delete($existing);
  59          redirect($returnurl);
  60      } else {
  61          $optionsyes = array('id'=>$id, 'action'=>'delete', 'confirm'=>1, 'sesskey'=>sesskey(), 'courseid'=>$courseid);
  62          $optionsno = array('userid'=>$existing->userid, 'courseid'=>$courseid);
  63          print_header("$SITE->shortname: $strblogs", $SITE->fullname);
  64          blog_print_entry($existing);
  65          echo '<br />';
  66          notice_yesno(get_string('blogdeleteconfirm', 'blog'), 'edit.php', 'index.php', $optionsyes, $optionsno, 'post', 'get');
  67          print_footer();
  68          die;
  69      }
  70  }
  71  
  72  require_once ('edit_form.php');
  73  $blogeditform = new blog_edit_form(null, compact('existing', 'sitecontext'));
  74  
  75  if ($blogeditform->is_cancelled()){
  76      redirect($returnurl);
  77  } else if ($fromform = $blogeditform->get_data()){
  78      //save stuff in db
  79      switch ($action) {
  80          case 'add':
  81              do_add($fromform, $blogeditform);
  82          break;
  83  
  84          case 'edit':
  85              if (!$existing) {
  86                  error('Incorrect blog post id');
  87              }
  88              do_edit($fromform, $blogeditform);
  89          break;
  90          default :
  91              error('Unknown action!');
  92      }
  93      redirect($returnurl);
  94  }
  95  
  96  
  97  // gui setup
  98  switch ($action) {
  99      case 'add':
 100          // prepare new empty form
 101          $post->publishstate = 'site';
 102          $strformheading = get_string('addnewentry', 'blog');
 103          $post->action       = $action;
 104      break;
 105  
 106      case 'edit':
 107          if (!$existing) {
 108              error('Incorrect blog post id');
 109          }
 110          $post->id           = $existing->id;
 111          $post->subject      = $existing->subject;
 112          $post->summary      = $existing->summary;
 113          $post->publishstate = $existing->publishstate;
 114          $post->format       = $existing->format;
 115          $post->action       = $action;
 116          $strformheading = get_string('updateentrywithid', 'blog');
 117  
 118          if ($itemptags = tag_get_tags_csv('post', $post->id, TAG_RETURN_TEXT, 'default')) {
 119              $post->ptags = $itemptags;
 120          }
 121          
 122          if ($itemotags = tag_get_tags_array('post', $post->id, 'official')) {
 123              $post->otags = array_keys($itemotags);
 124          }
 125      break;
 126      default :
 127          error('Unknown action!');
 128  }
 129  
 130  // done here in order to allow deleting of posts with wrong user id above
 131  if (!$user = get_record('user', 'id', $userid)) {
 132      error('Incorrect user id');
 133  }
 134  $navlinks = array();
 135  $navlinks[] = array('name' => fullname($user), 'link' => "$CFG->wwwroot/user/view.php?id=$userid", 'type' => 'misc');
 136  $navlinks[] = array('name' => $strblogs, 'link' => "$CFG->wwwroot/blog/index.php?userid=$userid", 'type' => 'misc');
 137  $navlinks[] = array('name' => $strformheading, 'link' => null, 'type' => 'misc');
 138  $navigation = build_navigation($navlinks);
 139  
 140  print_header("$SITE->shortname: $strblogs", $SITE->fullname, $navigation,'','',true);
 141  $blogeditform->set_data($post);
 142  $blogeditform->display();
 143  
 144  
 145  print_footer();
 146  
 147  
 148  die;
 149  
 150  /*****************************   edit.php functions  ***************************/
 151  
 152  /*
 153  * Delete blog post from database
 154  */
 155  function do_delete($post) {
 156      global $returnurl;
 157  
 158      $status = delete_records('post', 'id', $post->id);
 159      //$status = delete_records('blog_tag_instance', 'entryid', $post->id) and $status;
 160      tag_set('post', $post->id, array());
 161      
 162      blog_delete_old_attachments($post);
 163  
 164      add_to_log(SITEID, 'blog', 'delete', 'index.php?userid='. $post->userid, 'deleted blog entry with entry id# '. $post->id);
 165  
 166      if (!$status) {
 167          error('Error occured while deleting post', $returnurl);
 168      }
 169  }
 170  
 171  /**
 172   * Write a new blog entry into database
 173   */
 174  function do_add($post, $blogeditform) {
 175      global $CFG, $USER, $returnurl;
 176  
 177      $post->module       = 'blog';
 178      $post->userid       = $USER->id;
 179      $post->lastmodified = time();
 180      $post->created      = time();
 181  
 182      // Insert the new blog entry.
 183      if ($id = insert_record('post', $post)) {
 184          $post->id = $id;
 185          // add blog attachment
 186          $dir = blog_file_area_name($post);
 187          if ($blogeditform->save_files($dir) and $newfilename = $blogeditform->get_new_filename()) {
 188              set_field("post", "attachment", $newfilename, "id", $post->id);
 189          }
 190          add_tags_info($post->id);
 191          add_to_log(SITEID, 'blog', 'add', 'index.php?userid='.$post->userid.'&postid='.$post->id, $post->subject);
 192  
 193      } else {
 194          error('There was an error adding this post in the database', $returnurl);
 195      }
 196  
 197  }
 198  
 199  /**
 200   * @param . $post argument is a reference to the post object which is used to store information for the form
 201   * @param . $bloginfo_arg argument is reference to a blogInfo object.
 202   * @todo complete documenting this function. enable trackback and pingback between entries on the same server
 203   */
 204  function do_edit($post, $blogeditform) {
 205  
 206      global $CFG, $USER, $returnurl;
 207  
 208  
 209      $post->lastmodified = time();
 210  
 211      $dir = blog_file_area_name($post);
 212      if ($blogeditform->save_files($dir) and $newfilename = $blogeditform->get_new_filename()) {
 213          $post->attachment = $newfilename;
 214      }
 215  
 216      // update record
 217      if (update_record('post', $post)) {
 218          // delete all tags associated with this entry
 219          
 220          //delete_records('blog_tag_instance', 'entryid', $post->id);
 221          //delete_records('tag_instance', 'itemid', $post->id, 'itemtype', 'blog');
 222          //untag_an_item('post', $post->id);
 223          // add them back
 224          add_tags_info($post->id);
 225  
 226          add_to_log(SITEID, 'blog', 'update', 'index.php?userid='.$USER->id.'&postid='.$post->id, $post->subject);
 227  
 228      } else {
 229          error('There was an error updating this post in the database', $returnurl);
 230      }
 231  }
 232  
 233  /**
 234   * function to attach tags into a post
 235   * @param int postid - id of the blog
 236   */
 237  function add_tags_info($postid) {
 238      
 239      $tags = array();
 240      if ($otags = optional_param('otags', '', PARAM_INT)) {
 241          foreach ($otags as $tagid) {
 242              // TODO : make this use the tag name in the form
 243              if ($tag = tag_get('id', $tagid)) {
 244                  $tags[] = $tag->name;
 245              }
 246          }
 247      }
 248  
 249      $manual_tags = optional_param('ptags', '', PARAM_NOTAGS);
 250      $tags = array_merge($tags, explode(',', $manual_tags));
 251      
 252      tag_set('post', $postid, $tags);
 253  }
 254  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7