예제 #1
0
	public function updateRating($data)
	{
		$user          = JFactory::getUser();
		$created       = JFactory::getDate()->toSql();
		$criteriaArray = isset($data['criteria_array']) ? $data['criteria_array'] : array();
		$docId         = $data['doc_id'];
		$commentId     = $data['comment_id'];

		$ratingObj      = $this->getRating($docId, $commentId);
		$ratingId       = $ratingObj->id;
		$ratingScoreOld = $ratingObj->score;
		$ratingScoreNew = JUDownloadFrontHelperRating::calculateRatingScore($data, $docId, $criteriaArray);

		
		$dataRating = array(
			'id'    => $ratingId, 'user_id' => $user->id, 'comment_id' => $commentId,
			'score' => $ratingScoreNew, 'created' => $created, 'doc_id' => $docId
		);

		JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_judownload/tables');
		$ratingTable = JTable::getInstance('Rating', 'JUDownloadTable');
		$ratingTable->bind($dataRating);

		if (!$ratingTable->check())
		{
			return false;
		}

		$ratingStore = $ratingTable->store();
		if (!$ratingStore)
		{
			return false;
		}

		if ($ratingStore && count($criteriaArray) > 0)
		{
			$oldCriteriaIdArr = $this->getCriteriasId($ratingId);
			$criteriaIdArr    = array();
			foreach ($criteriaArray AS $criteria)
			{
				$criteriaIdArr[] = $criteria->id;
			}
			
			$flagUpdate = false;
			if (count(array_diff($criteriaIdArr, $oldCriteriaIdArr)) == 0)
			{
				$flagUpdate = true;
			}
			if ($flagUpdate == false)
			{
				$this->deleteCriteriasByRatingId($ratingId);
			}
			foreach ($criteriaArray AS $criteria)
			{
				if ($flagUpdate == true)
				{
					$this->updateCriteriaValue($ratingId, $criteria->id, $criteria->value);
				}
				else
				{
					JUDownloadMultiRating::insertCriteriaValue($ratingId, $criteria->id, $criteria->value);
				}
			}
		}
		$documentItem = JUDownloadHelper::getDocumentById($docId);
		$rating       = (($documentItem->rating * $documentItem->total_votes) + $ratingScoreNew - $ratingScoreOld) / $documentItem->total_votes;

		
		$db    = JFactory::getDbo();
		$query = $db->getQuery(true);
		$query->update('#__judownload_documents');
		$query->set('rating = ' . $rating);
		$query->where('id = ' . $docId);
		$db->setQuery($query);
		$db->execute();

		$session      = JFactory::getSession();
		$timeNow      = JFactory::getDate()->toSql();
		$timeNowStamp = strtotime($timeNow);
		
		$inputCookie   = JFactory::getApplication()->input->cookie;
		$config        = JFactory::getConfig();
		$cookie_domain = $config->get('cookie_domain', '');
		$cookie_path   = $config->get('cookie_path', '/');
		
		$inputCookie->set('judl-document-rated-' . $docId, $timeNowStamp, time() + 864000, $cookie_path, $cookie_domain);
		
		$session->set('judl-document-rated-' . $docId, $timeNowStamp);

		
		$logData = array(
			'user_id'   => $user->id,
			'event'     => 'document.rate',
			'item_id'   => $docId,
			'doc_id'    => $docId,
			'value'     => $ratingScoreNew,
			'reference' => $ratingTable->id
		);

		JUDownloadFrontHelperLog::addLog($logData);

		return true;

	}
예제 #2
0
 public function copyDocuments($document_id_arr, $tocat_id_arr, $copy_option_arr, $tmp_doc = false, &$filesStoreMap = array(), &$versionsStoreMap = array(), &$fieldsData = array())
 {
     $dispatcher = JDispatcher::getInstance();
     JTable::addIncludePath(JPATH_ADMINISTRATOR . "/components/com_judownload/tables");
     $db = JFactory::getDbo();
     $user = JFactory::getUser();
     $catTable = JTable::getInstance("Category", "JUDownloadTable");
     $table = $this->getTable();
     if (empty($document_id_arr)) {
         return false;
     }
     if (empty($tocat_id_arr)) {
         return false;
     }
     set_time_limit(0);
     $assetTable = JTable::getInstance('Asset', 'JTable');
     $fileTable = JTable::getInstance("File", "JUDownloadTable");
     $commentTable = JTable::getInstance("Comment", "JUDownloadTable");
     $reportTable = JTable::getInstance("Report", "JUDownloadTable");
     $subscriptionTable = JTable::getInstance("Subscription", "JUDownloadTable");
     $changelogTable = JTable::getInstance("Changelog", "JUDownloadTable");
     $logTable = JTable::getInstance("Log", "JUDownloadTable");
     $versionTable = JTable::getInstance("Version", "JUDownloadTable");
     $total_copied_documents = 0;
     foreach ($tocat_id_arr as $tocat_id) {
         $catTable->reset();
         if (!$catTable->load($tocat_id)) {
             continue;
         }
         $assetName = 'com_judownload.category.' . (int) $tocat_id;
         $canDoCreate = $user->authorise('judl.document.create', $assetName);
         if (!$canDoCreate) {
             JError::raiseWarning(401, JText::sprintf('COM_JUDOWNLOAD_CAN_NOT_CREATE_DOCUMENT_IN_THIS_CATEGORY', $catTable->title));
             continue;
         }
         foreach ($document_id_arr as $doc_id) {
             $table->reset();
             if (!$table->load($doc_id)) {
                 continue;
             }
             $oldTable = $table;
             $table->id = 0;
             $table->cat_id = $tocat_id;
             do {
                 $query = $db->getQuery(true);
                 $query->SELECT('COUNT(*)');
                 $query->FROM('#__judownload_documents AS d');
                 $query->JOIN('', '#__judownload_documents_xref AS dxref ON dxref.doc_id = d.id');
                 $query->JOIN('', '#__judownload_categories AS c ON dxref.cat_id = c.id');
                 $query->WHERE('c.id = ' . $tocat_id);
                 $query->WHERE('d.alias = "' . $table->alias . '"');
                 $db->setQuery($query);
                 $sameAliasDocument = $db->loadResult();
                 if ($sameAliasDocument) {
                     $table->title = JString::increment($table->title);
                     $table->alias = JApplication::stringURLSafe(JString::increment($table->alias, 'dash'));
                 }
             } while ($sameAliasDocument);
             if ($table->style_id == -1) {
                 $old_cat_id = JUDownloadFrontHelperCategory::getMainCategoryId($doc_id);
                 if ($old_cat_id != $tocat_id) {
                     $oldTemplateStyleObject = JUDownloadFrontHelperTemplate::getTemplateStyleOfCategory($old_cat_id);
                     $newTemplateStyleObject = JUDownloadFrontHelperTemplate::getTemplateStyleOfCategory($tocat_id);
                     if ($oldTemplateStyleObject->template_id != $newTemplateStyleObject->template_id) {
                         if (in_array('keep_template_params', $copy_option_arr) && $tmp_doc == false) {
                             $table->style_id = $oldTemplateStyleObject->style_id;
                         } else {
                             if ($tmp_doc == false) {
                                 $table->template_params = '';
                             }
                         }
                     }
                 }
             }
             if (!in_array('copy_downloads', $copy_option_arr) && $tmp_doc == false) {
                 $table->downloads = 0;
             }
             if (!in_array('copy_rates', $copy_option_arr) && $tmp_doc == false) {
                 $table->rating = 0;
                 $table->total_votes = 0;
             }
             if (!in_array('copy_hits', $copy_option_arr) && $tmp_doc == false) {
                 $table->hits = 0;
             }
             if (in_array('copy_permission', $copy_option_arr)) {
                 $assetTable->reset();
                 if ($assetTable->loadByName('com_judownload.document.' . $doc_id)) {
                     $table->setRules($assetTable->rules);
                 } else {
                     $table->setRules('{}');
                 }
             } else {
                 $table->setRules('{}');
             }
             if (!$table->check()) {
                 continue;
             }
             $result = $dispatcher->trigger('onContentBeforeCopy', array($this->option . '.' . $this->name, $table, $oldTable, $copy_option_arr));
             if (in_array(false, $result, true)) {
                 $this->setError($table->getError());
                 return false;
             }
             if ($table->store()) {
                 $table->checkIn();
                 $total_copied_documents++;
             } else {
                 continue;
             }
             $newDocId = $table->id;
             if ($table->icon) {
                 $ori_icon_name = $table->icon;
                 $new_icon_name = $newDocId . substr($ori_icon_name, strpos($ori_icon_name, '_'));
                 $query = "UPDATE #__judownload_documents SET icon = '" . $new_icon_name . "' WHERE id=" . $newDocId;
                 $db->setQuery($query);
                 $db->execute();
                 $icon_directory = JPATH_ROOT . "/" . JUDownloadFrontHelper::getDirectory("document_icon_directory", "media/com_judownload/images/document/");
                 if (JFile::exists($icon_directory . $ori_icon_name)) {
                     JFile::copy($icon_directory . $ori_icon_name, $icon_directory . $new_icon_name);
                 }
                 if (JFile::exists($icon_directory . "original/" . $ori_icon_name)) {
                     JFile::copy($icon_directory . "original/" . $ori_icon_name, $icon_directory . "original/" . $new_icon_name);
                 }
             }
             $query = "INSERT INTO #__judownload_documents_xref (doc_id, cat_id, main) VALUES({$newDocId}, {$tocat_id}, 1)";
             $db->setQuery($query);
             $db->execute();
             $query = "SELECT * FROM #__judownload_tags_xref WHERE doc_id=" . $doc_id . " ORDER BY ordering ASC";
             $db->setQuery($query);
             $tags = $db->loadObjectList();
             if (!empty($tags)) {
                 foreach ($tags as $tag) {
                     $query = "INSERT INTO #__judownload_tags_xref (tag_id, doc_id, ordering) VALUES (" . $tag->tag_id . ", " . $newDocId . ", " . $tag->ordering . ")";
                     $db->setQuery($query);
                     $db->execute();
                 }
             }
             $ori_fieldgroup_id = JUDownloadHelper::getFieldGroupIdByDocId($doc_id);
             $copy_extra_fields = in_array("copy_extra_fields", $copy_option_arr);
             if ($copy_extra_fields) {
                 $copy_extra_fields = $ori_fieldgroup_id == $catTable->fieldgroup_id ? true : false;
             }
             $query = $db->getQuery(true);
             $query->select("field.*");
             $query->from("#__judownload_fields AS field");
             $query->select("plg.folder");
             $query->join("", "#__judownload_plugins AS plg ON field.plugin_id = plg.id");
             if ($copy_extra_fields && $ori_fieldgroup_id) {
                 $query->where("field.group_id IN (1, {$ori_fieldgroup_id})");
             } else {
                 $query->where("field.group_id = 1");
             }
             $db->setQuery($query);
             $fields = $db->loadObjectList();
             foreach ($fields as $field) {
                 $fieldObj = JUDownloadFrontHelperField::getField($field, $doc_id);
                 $fieldObj->onCopy($newDocId, $fieldsData);
             }
             if (in_array('copy_files', $copy_option_arr)) {
                 $query = "SELECT * FROM #__judownload_files WHERE doc_id = " . $doc_id;
                 $db->setQuery($query);
                 $files = $db->loadObjectList();
                 if ($files) {
                     foreach ($files as $file) {
                         $fileTable->reset();
                         if ($fileTable->bind($file) && $fileTable->check()) {
                             $fileTable->id = 0;
                             $fileTable->doc_id = $newDocId;
                             if ($fileTable->store()) {
                                 $filesStoreMap[$file->id] = $fileTable->id;
                                 $query = "SELECT id FROM #__judownload_versions WHERE file_id = " . $file->id;
                                 $db->setQuery($query);
                                 $oldFileVersionIds = $db->loadColumn();
                                 foreach ($oldFileVersionIds as $oldFileVersionId) {
                                     if ($versionTable->load($oldFileVersionId)) {
                                         $versionTable->id = 0;
                                         $versionTable->doc_id = $newDocId;
                                         $versionTable->file_id = $fileTable->id;
                                         $versionTable->store();
                                         $versionsStoreMap[$oldFileVersionId] = $versionTable->id;
                                     }
                                 }
                             }
                         } else {
                             continue;
                         }
                     }
                 }
                 $file_directory = JPATH_ROOT . "/" . JUDownloadFrontHelper::getDirectory("file_directory", "media/com_judownload/files/");
                 $ori_directory = JPath::clean($file_directory . $doc_id);
                 $new_directory = JPath::clean($file_directory . $newDocId);
                 if (JFolder::exists($ori_directory)) {
                     JFolder::copy($ori_directory, $new_directory);
                 }
             }
             $query = "SELECT id FROM #__judownload_versions WHERE doc_id = " . $doc_id . " AND file_id = 0";
             $db->setQuery($query);
             $oldDocVersionIds = $db->loadColumn();
             foreach ($oldDocVersionIds as $oldDocVersionId) {
                 if ($versionTable->load($oldDocVersionId)) {
                     $versionTable->id = 0;
                     $versionTable->doc_id = $newDocId;
                     $versionTable->store();
                     $versionsStoreMap[$oldDocVersionId] = $versionTable->id;
                 }
             }
             if (in_array('copy_changelogs', $copy_option_arr)) {
                 $query = "SELECT * FROM #__judownload_changelogs WHERE `doc_id` = {$doc_id}";
                 $db->setQuery($query);
                 $changelogs = $db->loadObjectList();
                 if ($changelogs) {
                     foreach ($changelogs as $changelog) {
                         $changelogTable->reset();
                         if ($changelogTable->bind($changelog) && $changelogTable->check()) {
                             $changelogTable->id = 0;
                             $changelogTable->doc_id = $newDocId;
                             $changelogTable->store();
                         } else {
                             continue;
                         }
                     }
                 }
             }
             if (in_array('copy_related_documents', $copy_option_arr)) {
                 $query = "INSERT INTO `#__judownload_documents_relations` (doc_id, doc_id_related, ordering) SELECT {$newDocId}, doc_id_related, ordering FROM `#__judownload_documents_relations` WHERE doc_id = {$doc_id}";
                 $db->setQuery($query);
                 $db->execute();
             }
             if (in_array('copy_rates', $copy_option_arr)) {
                 $ratingMapping = array();
                 $query = "SELECT * FROM #__judownload_rating WHERE doc_id = {$doc_id}";
                 $db->setQuery($query);
                 $ratings = $db->loadObjectList();
                 if (count($ratings)) {
                     $criteriagroup_id = JUDownloadHelper::getCriteriaGroupIdByDocId($doc_id);
                     foreach ($ratings as $rating) {
                         $oldRatingId = $rating->id;
                         $rating->id = 0;
                         $rating->doc_id = $newDocId;
                         if ($db->insertObject('#__judownload_rating', $rating, 'id')) {
                             if (JUDownloadHelper::hasMultiRating() && $criteriagroup_id && $criteriagroup_id == $catTable->criteriagroup_id) {
                                 JUDownloadMultiRating::copyCriteriaValue($rating->id, $oldRatingId);
                             }
                             $ratingMapping[$oldRatingId] = $rating->id;
                         }
                     }
                 }
             }
             if (in_array('copy_comments', $copy_option_arr)) {
                 $query = "SELECT id FROM #__judownload_comments WHERE doc_id=" . $doc_id . " AND parent_id = 1";
                 $db->setQuery($query);
                 $commentIds = $db->loadColumn();
                 $commentMapping = array();
                 while (!empty($commentIds)) {
                     $commentId = array_shift($commentIds);
                     $query = "SELECT id FROM #__judownload_comments WHERE doc_id=" . $doc_id . " AND parent_id = {$commentId}";
                     $db->setQuery($query);
                     $_commentIds = $db->loadColumn();
                     foreach ($_commentIds as $_commentId) {
                         if (!in_array($_commentId, $commentIds)) {
                             array_push($commentIds, $_commentId);
                         }
                     }
                     $commentTable->load($commentId, true);
                     $commentTable->id = 0;
                     $commentTable->doc_id = $newDocId;
                     $commentTable->parent_id = isset($commentMapping[$commentTable->parent_id]) ? $commentMapping[$commentTable->parent_id] : 0;
                     if (in_array('copy_rates', $copy_option_arr)) {
                         $commentTable->rating_id = isset($ratingMapping[$commentTable->rating_id]) ? $ratingMapping[$commentTable->rating_id] : 0;
                     }
                     $commentTable->store();
                     $new_comment_id = $commentTable->id;
                     $commentMapping[$commentId] = $new_comment_id;
                     $query = "SELECT * FROM #__judownload_reports WHERE `item_id` = {$commentId} AND `type` = 'comment'";
                     $db->setQuery($query);
                     $reports = $db->loadObjectList();
                     if ($reports) {
                         foreach ($reports as $report) {
                             $reportTable->reset();
                             if ($reportTable->bind($report) && $reportTable->check()) {
                                 $reportTable->id = 0;
                                 $reportTable->item_id = $new_comment_id;
                                 $reportTable->store();
                             } else {
                                 continue;
                             }
                         }
                     }
                     $query = "SELECT * FROM #__judownload_subscriptions WHERE `item_id` = {$commentId} AND `type` = 'comment'";
                     $db->setQuery($query);
                     $subscriptions = $db->loadObjectList();
                     if ($subscriptions) {
                         foreach ($subscriptions as $subscription) {
                             $subscriptionTable->reset();
                             if ($subscriptionTable->bind($subscription) && $subscriptionTable->check()) {
                                 $subscriptionTable->id = 0;
                                 $subscriptionTable->item_id = $new_comment_id;
                                 $subscriptionTable->store();
                             } else {
                                 continue;
                             }
                         }
                     }
                 }
             }
             if (in_array('copy_reports', $copy_option_arr)) {
                 $query = "SELECT * FROM #__judownload_reports WHERE `item_id` = {$doc_id} AND `type` = 'document'";
                 $db->setQuery($query);
                 $reports = $db->loadObjectList();
                 if ($reports) {
                     foreach ($reports as $report) {
                         $reportTable->reset();
                         if ($reportTable->bind($report) && $reportTable->check()) {
                             $reportTable->id = 0;
                             $reportTable->item_id = $newDocId;
                             $reportTable->store();
                         } else {
                             continue;
                         }
                     }
                 }
             }
             if (in_array('copy_subscriptions', $copy_option_arr)) {
                 $query = "SELECT * FROM #__judownload_subscriptions WHERE `item_id` = {$doc_id} AND `type` = 'document'";
                 $db->setQuery($query);
                 $subscriptions = $db->loadObjectList();
                 if ($subscriptions) {
                     foreach ($subscriptions as $subscription) {
                         $subscriptionTable->reset();
                         if ($subscriptionTable->bind($subscription) && $subscriptionTable->check()) {
                             $subscriptionTable->id = 0;
                             $subscriptionTable->item_id = $newDocId;
                             $subscriptionTable->store();
                         } else {
                             continue;
                         }
                     }
                 }
             }
             if (in_array('copy_logs', $copy_option_arr)) {
                 $query = "SELECT * FROM #__judownload_logs WHERE (`doc_id` = {$doc_id})";
                 $db->setQuery($query);
                 $logs = $db->loadObjectList();
                 if ($logs) {
                     foreach ($logs as $log) {
                         $logTable->reset();
                         if ($logTable->bind($log) && $logTable->check()) {
                             $logTable->id = 0;
                             $logTable->item_id = $newDocId;
                             $logTable->doc_id = $newDocId;
                             $logTable->store();
                         } else {
                             continue;
                         }
                     }
                 }
             }
             if ($tmp_doc) {
                 return $newDocId;
             }
             $this->cleanCache();
             $dispatcher->trigger('onContentAfterCopy', array($this->option . '.' . $this->name, $table, $oldTable, $copy_option_arr));
         }
     }
     return $total_copied_documents;
 }
예제 #3
0
 public static function rebuildRating($doc_id)
 {
     $db = JFactory::getDbo();
     $params = self::getParams(null, $doc_id);
     $onlyCalculateLastRating = $params->get('only_calculate_last_rating', 0);
     if ($onlyCalculateLastRating == 1) {
         $query = $db->getQuery(true);
         $query->select('r.id, r.doc_id, r.score');
         $query->select('cm.approved AS comment_approved');
         $query->from('#__judownload_rating AS r');
         $query->join('LEFT', '#__judownload_comments AS cm ON cm.rating_id = r.id');
         $query->where('r.`created` = (SELECT  MAX(created) FROM `#__judownload_rating` AS r2 WHERE r2.doc_id = r.doc_id AND r2.user_id = r.user_id AND r.user_id > 0)');
         $query->where('r.doc_id = ' . $doc_id);
         $query->group('r.created, r.doc_id, r.user_id');
     } else {
         $query = $db->getQuery(true);
         $query->select('r.id, r.doc_id, r.score');
         $query->select('cm.approved AS comment_approved');
         $query->from('#__judownload_rating AS r');
         $query->join('LEFT', '#__judownload_comments AS cm ON cm.rating_id = r.id');
         $query->where('r.doc_id = ' . $doc_id);
     }
     $db->setQuery($query);
     $ratings = $db->loadObjectList();
     if ($ratings) {
         $totalScore = 0;
         $totalVotes = 0;
         if (JFile::exists(JPATH_SITE . '/components/com_judownload/fields/multirating/multirating.class.php')) {
             require_once JPATH_SITE . '/components/com_judownload/fields/multirating/multirating.class.php';
         }
         foreach ($ratings as $rating) {
             if (self::hasMultiRating()) {
                 $ratingScore = JUDownloadMultiRating::rebuildRating($rating);
             } else {
                 $ratingScore = $rating->score;
             }
             if ($rating->comment_approved !== 0) {
                 $totalScore += $ratingScore;
                 $totalVotes++;
             }
         }
         if ($onlyCalculateLastRating == 1) {
             $avgScore = $totalScore / $totalVotes;
             $query = $db->getQuery(true);
             $query->update('#__judownload_documents');
             $query->set('rating = ' . $avgScore);
             $query->set('total_votes = ' . $totalVotes);
             $query->where('id = ' . $doc_id);
         } else {
             $query = $db->getQuery(true);
             $query->update('#__judownload_documents');
             $query->set('rating = (SELECT AVG(score) FROM #__judownload_rating WHERE doc_id=' . $doc_id . ')');
             $query->set('total_votes = ' . $totalVotes);
             $query->where('id = ' . $doc_id);
         }
         $db->setQuery($query);
         $db->execute();
         return false;
     }
 }