Пример #1
0
 /**
  * Set redirect and message
  *
  * @param   object  $url  URL to redirect to
  * @param   object  $msg  Message to send
  * @return  void
  */
 public function onCourseAfterLecture($course, $unit, $lecture)
 {
     if (!$course->offering()->section()->access('view')) {
         $view = new \Hubzero\Plugin\View(array('folder' => 'courses', 'element' => 'outline', 'name' => 'shared', 'layout' => '_not_enrolled'));
         $view->set('course', $course)->set('option', 'com_courses')->set('message', 'You must be enrolled to utilize the discussion feature.');
         return $view->loadTemplate();
     }
     // Are discussions turned on?
     if (!$lecture->params('discussions_category')) {
         return '';
     }
     $this->params->merge(new \Hubzero\Config\Registry($course->offering()->section()->get('params')));
     $this->_active = 'outline';
     $this->database = App::get('db');
     $this->offering = $course->offering();
     $this->base = $this->offering->link() . '&active=' . $this->_active;
     $this->_authorize('category');
     $this->_authorize('thread');
     $view = $this->view('lecture', 'threads');
     $view->course = $this->course = $course;
     $view->unit = $this->unit = $unit;
     $view->lecture = $this->lecture = $lecture;
     $view->option = $this->option = 'com_courses';
     $view->config = $this->params;
     // Incoming
     $view->filters = array();
     $view->filters['limit'] = Request::getInt('limit', 500);
     $view->filters['start'] = Request::getInt('limitstart', 0);
     $view->filters['section'] = Request::getVar('section', '');
     $view->filters['category'] = Request::getVar('category', '');
     $view->filters['state'] = 1;
     $view->filters['scope'] = 'course';
     $view->filters['scope_id'] = $course->offering()->get('id');
     if ($this->params->get('discussions_threads', 'all') != 'all') {
         $view->filters['scope_sub_id'] = $course->offering()->section()->get('id');
     }
     $view->filters['sticky'] = false;
     //$view->filters['start_id'] = Request::getInt('start_id', 0);
     $view->filters['search'] = Request::getVar('search', '');
     $view->no_html = Request::getInt('no_html', 0);
     $view->filters['sort_Dir'] = 'DESC';
     $view->filters['sort'] = 'c.created';
     $view->filters['object_id'] = $lecture->get('id');
     $view->post = new \Components\Forum\Tables\Post($this->database);
     $view->total = 0;
     $view->rows = null;
     // Load the section
     $section = new \Components\Forum\Tables\Section($this->database);
     if (!$section->loadByAlias($unit->get('alias'), $view->filters['scope_id'], $view->filters['scope'])) {
         // Create a default section
         $section->title = $unit->get('title');
         $section->alias = $unit->get('alias');
         $section->scope = $view->filters['scope'];
         $section->scope_id = $view->filters['scope_id'];
         $section->object_id = $unit->get('id');
         $section->state = 1;
         if ($section->check()) {
             $section->store();
         }
     }
     $category = new \Components\Forum\Tables\Category($this->database);
     $category->loadByObject($lecture->get('id'), $section->get('id'), $view->filters['scope_id'], $view->filters['scope']);
     if (!$category->get('id')) {
         $category->section_id = $section->get('id');
         if ($lecture->get('title') == '--') {
             $category->title = $lecture->assets()->fetch('first')->get('title');
         } else {
             $category->title = $lecture->get('title');
         }
         $category->alias = $lecture->get('alias');
         $category->description = Lang::txt('Discussions for %s', $unit->get('alias'));
         $category->state = 1;
         $category->scope = $view->filters['scope'];
         $category->scope_id = $view->filters['scope_id'];
         $category->object_id = $lecture->get('id');
         $category->ordering = $lecture->get('ordering');
         if ($category->check()) {
             $category->store();
         }
     }
     $view->post->scope = $view->filters['scope'];
     $view->post->scope_id = $view->filters['scope_id'];
     $view->post->scope_sub_id = $course->offering()->section()->get('id');
     $view->post->category_id = $category->get('id');
     $view->post->object_id = $lecture->get('id');
     $view->post->parent = 0;
     // Get attachments
     $view->attach = new \Components\Forum\Tables\Attachment($this->database);
     $view->attachments = $view->attach->getAttachments($view->post->id);
     $view->filters['state'] = array(1, 3);
     $view->thread = Request::getInt('thread', 0);
     // No thread?
     if (!$view->thread) {
         // Try being more specific
         $view->thread = Request::getInt('thread', 0, 'get');
     }
     $action = strtolower(Request::getWord('action', ''));
     if ($view->no_html == 1) {
         $data = new stdClass();
         $data->success = true;
         $data->threads = new stdClass();
         $data->threads->lastchange = '0000-00-00 00:00:00';
         $data->threads->lastid = 0;
         $data->threads->total = 0;
         $data->threads->posts = null;
         $data->threads->html = null;
         $data->thread = new stdClass();
         $data->thread->lastchange = '0000-00-00 00:00:00';
         $data->thread->lastid = 0;
         $data->thread->posts = null;
         $data->thread->total = 0;
         $data->thread->html = null;
         if ($view->thread) {
             $view->post->load($view->thread);
         }
         if (!$action && $view->thread) {
             $action = 'both';
         }
         switch ($action) {
             case 'posts':
                 $view->filters['parent'] = $view->post->id;
                 $view->filters['start_at'] = Request::getVar('start_at', '');
                 $data->thread = $this->_posts($view->post, $view->filters);
                 break;
             case 'delete':
                 if ($pid = Request::getInt('post', 0)) {
                     $this->deletethread($pid, false);
                 }
                 $data->thread = $this->_thread($view->post, $view->filters);
                 break;
             case 'thread':
                 $data->thread = $this->_thread($view->post, $view->filters);
                 break;
             case 'search':
                 $view->filters['search'] = Request::getVar('search', '');
                 $data->threads = $this->_threadsSearch($view->post, $view->filters);
                 break;
             case 'sticky':
                 $view->post->sticky = Request::getInt('sticky', 0);
                 $view->post->store();
                 break;
             case 'both':
             default:
                 $view->filters['start_at'] = Request::getVar('start_at', '');
                 $data->thread = $this->_thread($view->post, $view->filters);
                 $view->filters['start_at'] = Request::getVar('threads_start', '');
                 $data->threads = $this->_threads($view->post, $view->filters);
                 break;
             case 'threads':
             default:
                 $view->filters['parent'] = $view->post->id;
                 $view->filters['start_at'] = Request::getVar('threads_start', '');
                 $data->threads = $this->_threads($view->post, $view->filters);
                 break;
         }
         if ($this->getError()) {
             $data->success = false;
             $data->errors = $this->getErrors();
         }
         ob_clean();
         header('Content-type: text/plain');
         echo json_encode($data);
         exit;
     }
     switch ($action) {
         case 'search':
             $view->filters['search'] = Request::getVar('search', '');
             $data = $this->_threadsSearch($view->post, $view->filters);
             $view->threads = $data->posts;
             break;
         default:
             if ($action == 'delete') {
                 if ($pid = Request::getInt('post', 0)) {
                     $this->deletethread($pid, false);
                 }
             }
             $view->filters['parent'] = 0;
             $view->threads = $view->post->find($view->filters);
             break;
     }
     $view->data = null;
     if ($view->thread) {
         $view->post->load($view->thread);
         $view->data = $this->_thread($view->post, $view->filters);
     }
     $view->notifications = $this->getPluginMessage();
     // Set any errors
     foreach ($this->getErrors() as $error) {
         $this->view->setError($error);
     }
     return $view->loadTemplate();
 }