6d3019b754e6debafa5d0f53eca8ee19f4943e57
[atutor.git] / mods / merlot / merlot_rest.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2006 by Greg Gay & Joel Kronenberg        */
6 /* Adaptive Technology Resource Centre / University of Toronto  */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id: merlot_rest.php 6614 2006-09-27 19:32:29Z greg $
14
15 require("classes/MerlotResultParser.class.php");
16
17 $keywords = trim($_REQUEST['keywords']);
18 $title = trim($_REQUEST['title']);
19 $description = trim($_REQUEST['description']);
20 $author = trim($_REQUEST['author']);
21 $results_per_page = intval(trim($_REQUEST['results_per_page']));
22
23 if($keywords <> "" || $title <> "" || $description <> "" || $author <> "" || $_REQUEST["creativeCommons"] == "true")
24 {
25         $page = intval($_REQUEST['p']);
26         if (!$page) {
27                 $page = 1;
28         }       
29
30         if ($results_per_page > $default_results_per_page || $results_per_page == 0)
31                 $results_per_page = $default_results_per_page;
32         
33         $page_str = "results_per_page=".$results_per_page;
34         $url_search = "&size=".$results_per_page."&firstRecNumber=".$results_per_page*($page - 1);
35
36         if ($keywords <> "")
37         {
38                 $page_str .= SEP."keywords=".urlencode($keywords);
39                 $url_search .= "&keywords=".urlencode($keywords);
40         }
41         if ($title <> "") 
42         {
43                 $page_str .= SEP."title=".urlencode($title);
44                 $url_search .= "&title=".urlencode($title);
45         }
46         if ($description <> "") 
47         {
48                 $page_str .= SEP. "description=".urlencode($description);
49                 $url_search .= "&description=".urlencode($description);
50         }
51         if ($author <> "") 
52         {
53                 $page_str .= SEP. "author=".urlencode($author);
54                 $url_search .= "&author=".urlencode($author);
55         }
56         
57         if (isset($_REQUEST["search_type"])) 
58                 $page_str .= SEP."search_type=".$_REQUEST["search_type"];
59         
60         if ($_REQUEST["search_type"] == 0) $url_search .= "&allKeyWords=true";
61         if ($_REQUEST["search_type"] == 1) $url_search .= "&anyKeyWords=true";
62         if ($_REQUEST["search_type"] == 2) $url_search .= "&exactPhraseKeyWords=true";
63         if ($_REQUEST["creativeCommons"] == "true") 
64         {
65                 $page_str .= SEP. "creativeCommons=true";
66                 $url_search .= "&creativeCommons=true";
67         }
68         
69         $url = $_config['merlot_location']."?licenseKey=".$_config['merlot_key'].$url_search;
70
71         $xml_results = file_get_contents($url);
72         
73         if (!$xml_results)
74         {
75                 $infos = array('CANNOT_CONNECT_SERVER', $_config['merlot_location']);
76                 $msg->addInfo($infos);
77         }
78         else
79         {
80                 $MerlotResultParser =& new MerlotResultParser();
81                 $MerlotResultParser->parse($xml_results);
82                 $result_list = $MerlotResultParser->getParsedArray();
83
84                 if ($result_list['status'] == 'failed')  // failed, display error
85                         echo "<span style='color:red'>"._AT('error').": ".$result_list['error']."</span>";
86                 else  // success, display results
87                 {
88                         if (is_array($result_list))
89                         {
90                                 $num_results = $result_list["summary"]["totalCount"];
91                                 $num_pages = max(ceil($num_results / $results_per_page), 1);
92                                 
93                                 echo '  <div id="search_results">';
94                                 echo "          <h2>". _AT('results')." <small>(".$result_list["summary"]["resultCount"]." out of ".$num_results.")</small></h2>";
95
96                                 print_paginator($page, $num_results, htmlspecialchars($page_str), $results_per_page);
97
98                                 foreach ($result_list as $key=>$result)
99                                 {
100                                         if (is_int($key))
101                                         {
102 ?>
103
104                 <dl class="browse-result">
105
106                         <dt></dt>
107                         <dd><h3><a href="<?php echo $result['detailURL']; ?>"><?php echo htmlspecialchars($result['title']); ?></a></h3></dd>
108                                                 
109                         <dt><?php echo _AT("author"); ?></dt>
110                         <dd><?php if ($result['authorName']=='') echo _AT('unknown'); else echo htmlspecialchars($result['authorName']); ?></dd>
111
112                         <dt><?php echo _AT("merlot_creation_date"); ?></dt>
113                         <dd><?php echo date('M j, Y', round($result['creationDate']/1000)); ?></dd>
114
115                         <dt><?php echo _AT("description"); ?></dt>
116                         <dd><?php if ($result['description']=='') echo _AT('na'); else if (strlen($result['description']) > 120) echo substr(htmlspecialchars($result['description']), 0, 120). "..."; else echo htmlspecialchars($result['description']); ?></dd>
117
118                         <dt><?php echo _AT("merlot_creative_commons"); ?></dt>
119                         <dd><?php if ($result['creativeCommons'] == "true") echo _AT('yes'); else echo _AT('no'); ?></dd>
120                 </dl>
121                 <br />
122 <?php
123                                         }
124                                 }
125                                 echo '</div>';
126                         }
127                 }
128         }
129 }
130 ?>