/** * Action範例 * * @param Array $data 由KALS_controller傳入的資料,組成是關連式的陣列 * * [Controller的JSON格式] * _data = { * "field": "value" * }; * * //取用範例 * _data["field]; //回傳value * * [Model的Array格式] * $data = array( * "field" => "value" * ); * * $data["field"]; //回傳value * * @return Array 要回傳給KALS_controller的資料 * 一樣是以關聯式陣列組成 * */ public function open($data) { // ----------------------- // 從現在的網頁來計算標註跟使用者 $webpage = $this->get_current_webpage(); $data["annotation_count"] = $webpage->get_written_annotations_count(); $data["user_count"] = $webpage->get_written_users_count(); // ------------------ // 取出最近的標註 $search = new Search_annotation_collection(); $search->set_target_webpage($webpage->get_id()); $search->set_check_authorize(FALSE); $limit = 5; $search->set_limit($limit); $order_type_id = 6; $desc = TRUE; $search->add_order($order_type_id, $desc); $data["last_annotation"] = array(); foreach ($search as $index => $annotation) { //$json = $annotation->export_data(); $json = $annotation; if ($index == 0) { //$last_annotation = $json; $data["last_annotation_id"] = $annotation->get_id(); $data["last_annotation_timestamp"] = $annotation->get_update_timestamp(); } // 一一加入標註 $data["last_annotation"][] = $json; } // 也可以最後一口氣取出所有標註 $data["last_annotation"] = $search; // ------------------- // 依照標註數量來判斷熱門程度 $activity = "bad"; if ($data["annotation_count"] > 20) { $activity = "good"; } else { if ($data["annotation_count"] > 5) { $activity = "normal"; } } $data["activity"] = "Good"; return $data; }
/** * 讀取RSS * * 範例:http://localhost/kals/web_apps/rss/webpage/1573 */ public function webpage($webpage_id = NULL) { if (is_null($webpage_id)) { $webpage = get_context_webpage(); $webpage_id = $webpage->get_id(); redirect("/web_apps/rss/webpage/" . $webpage_id); return; } $this->load->library('kals_resource/Webpage'); $this->load->library('kals_resource/Annotation'); $this->load->library('search/Search_annotation_collection'); $this->lang->load('kals_web_apps'); //語系 //echo $webpage_id; $webpage = new Webpage($webpage_id); //$search = $webpage->get_search_annotation(); $search = new Search_annotation_collection(); $search->set_target_webpage($webpage_id); $search->set_limit(10); $order_type_id = 6; $desc = TRUE; $search->add_order($order_type_id, $desc); // ------------------------------ // 以下開始建立RSS清單 // // 請使用php-rss-writer來建立 // // 專案首頁:https://github.com/suin/php-rss-writer // 程式碼位置:https://github.com/suin/php-rss-writer/tree/master/Source/Suin/RSSWriter // ------------------------------ $this->load->library("web_apps/Suin/RSSWriter/Feed"); $this->load->library("web_apps/Suin/RSSWriter/Channel"); $this->load->library("web_apps/Suin/RSSWriter/Item"); $feed = new Feed(); $webpage_title = $webpage->get_title(); //$webpage_topics_path = '/mobile_apps/annotation_topics/webpade_id/'.$webpage_id; //$webpage_topics_url = get_kals_base_url($webpage_topics_path); //$webpage_topics_url = site_url('/mobile_apps/annotation_topics/webpade_id/'.$webpage_id); //$webpage_topics_url = $webpage->get_url() . "#mobile=true"; $webpage_topics_url = get_kals_base_url("mobile_apps/redirect/load/" . $webpage_id); $channel = new Channel(); $channel->title($webpage_title)->description("Channel Description")->url($webpage_topics_url)->appendTo($feed); foreach ($search as $annotation) { $item = new Item(); $type = $annotation->get_type(); $type_name = $type->get_name(); $type_show; if ($type_name != 'annotation.type.custom') { //自定標註顯示 $type_show = $this->lang->line("web_apps." . $type_name); } else { $type_show = $type->get_custom_name(); } // anchor text // user name // date // annotation type // note $annotation_id = $annotation->get_id(); if (isset($annotation_id)) { //$topic_array = $this->db->query("SELECT topic_id // FROM annotation // WHERE annotation_id ='".$annotation_id."'"); $topic_array = $this->db->select("topic_id")->from("annotation")->where("annotation_id", $annotation_id)->where("deleted", "false")->get(); } foreach ($topic_array->result_array() as $row) { $topic_id = $row['topic_id']; } /** * @author Pulipuli Chen 20140429 * 要記得沒有topic的標註啊…… */ $topic_id = trim($topic_id); if ($topic_id == "") { $topic_id = $annotation_id; } //$item_url = 'http://140.119.61.137/kals/mobile/annotation_thread/'.$topic_id.'#annotation_'.$annotation_id; //$annotation_thread_path = "mobile_apps/annotation_thread/topic_id/".$topic_id."#annotation_".$annotation_id; //$item_url = site_url($annotation_thread_path); //$item_url = get_kals_base_url($annotation_thread_path); //$item_url = $webpage->get_url() . "#mobile=true&topic_id=" . $topic_id . "&annotation_id=" . $annotation_id; $item_url = get_kals_base_url("mobile_apps/redirect/load/" . $webpage_id . "/" . $topic_id . "/" . $annotation_id); //$item_url = $_SERVER["HTTP_HOST"] //test_msg($_SERVER["HTTP_HOST"]); $item->title("[" . $type_show . "] " . '"' . $annotation->get_anchor_text() . '"')->description("<div> " . $this->lang->line("web_apps.window.content.search.field.author") . ": " . $annotation->get_user()->get_name() . " </div>" . "<div> " . $this->lang->line("web_apps.window.content.search.field.author") . ": " . $annotation->get_user()->get_name() . " </div>" . "<div>" . $annotation->get_note() . " </div> \n ")->url($item_url)->appendTo($channel); } echo $feed; }
private function _find_recommend_annotation(Annotation $annotation) { $scope_coll = $annotation->get_scopes(); $user = $annotation->get_user(); $recommend_scope = $annotation->get_recommend_by_scopes(); if (is_null($recommend_scope)) { $recommend_scope = $scope_coll; } $target_over_score = $this->_get_recommend_by_threshold(); $this->_CI_load('library', 'search/Search_order_score', 'search_order_score'); $order_type_id = $this->CI->search_order_score->get_type_id(); $this->_CI_load('library', 'search/Search_annotation_collection', 'search_annotation_collection'); $search = new Search_annotation_collection(); $search->set_limit(1); $search->add_order($order_type_id, TRUE); $search->set_overlap_scope($recommend_scope); $search->set_exclude_scope($scope_coll); $search->set_exclude_user($user); $search->set_target_over_score($target_over_score); if ($search->length() == 1) { return $search->get_item(0); } else { return NULL; } }
public function get_consensus_coll() { $this->_CI_load('library', 'search/Search_annotation_collection', 'search_annotation_collection'); $type = 'Search_annotation_collection'; $key = 'annotation_get_consensus_coll'; $value = $this->get_id(); $search = get_cache($type, $key, $value); if (is_null($search)) { $search = new Search_annotation_collection(); $search->set_check_authorize(false); $search->set_limit(NULL); $search->set_target_scope($this->scope_coll); $search->set_exclude_annotation($this->get_id()); $search->set_exclude_user($this->get_user()); $search->set_target_topic(TRUE); //只有topic才有分數 $search->add_order(1); set_cache($search, $key, $value); } return $search; }
/** * 搜尋標註 * @param string $json * @param type $callback * @return type */ function search_annotation($json, $callback = NULL) { /** * @author Pulipuli Chen 20141111 * 測試很長時間才回應搜尋結果用的 */ //sleep(3); /** * 檢查SQL用 * @author Pulipuli Chen <*****@*****.**> 20141114 */ //$enable_profiler = true; $enable_profiler = false; if ($enable_profiler == TRUE) { $this->output->enable_profiler(TRUE); } if (is_string($json)) { $data = json_to_object($json); //把js丟過來的資料(search)包成物件data,內含 // search_range,keyword,新增order_by //test_msg($json); //test_msg($data); } else { $data = $json; } $reuqest_scope_collection = isset($data->limit) && isset($data->offset) === false; //$reuqest_scope_collection = true; //test_msg("reuqest_scope_collection", array($reuqest_scope_collection) ); // $user = get_context_user(); $url = $this->url; //存放目前data來源的頁面連結 // $search = new Search_annotation_collection($url); //Search_annotation_collection→Search_engine $search = new Search_annotation_collection(); //Search_annotation_collection→Search_engine if ($reuqest_scope_collection) { $search_all = new Search_annotation_collection(); } /** * 限定搜尋範圍只在來源的網頁 */ $search->set_target_referer_webpage(); if ($reuqest_scope_collection) { $search_all->set_target_referer_webpage(); } //test_msg("輸入資料", $json); //$data->search_range = "annotation_anchor"; //測試用 if (isset($data->query_field) === FALSE) { $data->query_field = "note"; } switch ($data->query_field) { case "annotation_user_name": //示範用 //$search->set_target_user(new User(1701)); $search->set_search_user_name($data->query_value); if ($reuqest_scope_collection) { $search_all->set_search_user_name($data->query_value); } break; case "note": //標註內容 if ($data->query_value != "" && $data->query_value != "*") { $search->set_search_note($data->query_value); if ($reuqest_scope_collection) { $search_all->set_search_note($data->query_value); } } break; case "annotation_type": //標註類型 if ($data->query_value !== "custom") { $search->set_target_type($data->query_value); if ($reuqest_scope_collection) { $search_all->set_target_type($data->query_value); } } else { $exclude_type_list = $data->exclude_type_list; $search->set_exclude_type_list($exclude_type_list); if ($reuqest_scope_collection) { $search_all->set_exclude_type_list($exclude_type_list); } } break; case "annotation_anchor_text": $search->set_search_anchor_text($data->query_value); if ($reuqest_scope_collection) { $search_all->set_search_anchor_text($data->query_value); } break; } // 6 [ order by ] 設定排序方式(order_by_id,大小|小大 )-data內要有order_by的選項 //test_msg('6 [ order by ]', isset($data->order_by)); //order_by的typ類型e在Search_order_collection if (isset($data->order_by) === FALSE) { $data->order_by = "update"; } //test_msg($data->order_by); if ($data->order_by == 'update') { $search->add_order(6, TRUE); } else { if ($data->order_by == 'create') { $search->add_order(7); } else { if ($data->order_by == 'responded') { $search->add_order(4); $search->set_target_topic(true); } else { if ($data->order_by == 'liked') { $search->add_order(3); } else { if ($data->order_by == 'scope') { $search->add_order(2); } else { $search->add_order(2, TRUE); } } } } } // // if (isset($data->order_by) === FALSE OR $data->order_by != 'update') // { // $search->add_order (6, TRUE); // if (isset($search_id)) { // $search_id->add_order (6, TRUE); // } // } //輸出 $totally_loaded = TRUE; if (isset($data->limit)) { $totally_loaded = FALSE; } // ------------------------------------------------------- // 7 [ offset ] //test_msg('7 [ offset ]', isset($data->offset)); if (isset($data->offset)) { $search->set_offset($data->offset); } // 8 [ limit ] //test_msg('8 [ limit ]', array(isset($data->limit),$data->limit)); if (isset($data->limit)) { //test_msg("設定限制 limit", $data->limit); $search->set_limit($data->limit); //$search_id在此不作設限 } // ------------------------------------------ //不作limit的情況下讀完,表示完全讀取 if (isset($data->limit) && ($reuqest_scope_collection && $search->length() == $search_all->length() || $search->length() === 0)) { $totally_loaded = TRUE; } //test_msg('Search Length', $search->length()); //test_msg($url); //test_msg($this->webpage->filter_webpage_id($url)); $annotation_collection = array(); foreach ($search as $search_annotation) { $annotation_data = $search_annotation->export_webpage_data($url); //把Data export到目前的頁面上 array_push($annotation_collection, $annotation_data); } // $search_annotation = new Annotation(783); // $annotation_data = $search_annotation->export_webpage_data($url); //把Data export到目前的頁面上 // array_push($annotation_collection, $annotation_data); // -------------- // -------------- $output_data = array('annotation_collection' => $annotation_collection, 'totally_loaded' => $totally_loaded); /** * 取得搜尋結果的位置 */ if ($reuqest_scope_collection) { $scope_collection = $search_all->get_result_scope_coll(); $scope_collection_json = $scope_collection->export_webpage_data(get_context_webpage()); $output_data['scope_collection'] = $scope_collection_json; } if ($reuqest_scope_collection && isset($data->show_total_count) && $data->show_total_count === TRUE) { if ($reuqest_scope_collection) { $output_data['total_count'] = $search_all->length(); } else { if (count($annotation_collection) === 0) { $output_data['total_count'] = 0; } else { $output_data['total_count'] = count($annotation_collection); } } } //log區 $action = 30; switch ($data->query_field) { case "annotation_user_name": $action = 31; break; case "annotation_type": //標註類型 $action = 32; break; case "annotation_anchor_text": $action = 33; break; } if ($data->query_field == "note" && $data->query_value == "*" && $data->order_by == "update") { // 搜尋最新標註 $action = 34; } $log_message = array("query_value" => $data->query_value, "order_by" => $data->order_by); if (isset($output_data['total_count'])) { $log_message['total_count'] = $output_data['total_count']; } kals_log($this->db, $action, $log_message); context_complete(); if ($enable_profiler != TRUE) { return $this->_display_jsonp($output_data, $callback); } }