PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/local/mahara/taoviewlib.php

https://github.com/nadavkav/MoodleTAO
PHP | 300 lines | 242 code | 19 blank | 39 comment | 45 complexity | 0e0b8e8533e4dd88e61162191c4d30ba MD5 | raw file
  1. <?php
  2. /**
  3. * Moodle - Modular Object-Oriented Dynamic Learning Environment
  4. * http://moodle.org
  5. * Copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. * @package moodle
  21. * @subpackage local
  22. * @author Dan Marsden <dan@danmarsden.com>
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL
  24. * @copyright (C) 1999 onwards Martin Dougiamas http://dougiamas.com
  25. *
  26. *
  27. */
  28. //function to filter artefacts based on the filters set.
  29. //this is a bit messy and could be tidied a bit
  30. function taoview_filter_artefacts($artefacts, $viewtype, $tagfilter, $userfilter) {
  31. global $CFG;
  32. if (!empty($tagfilter) || !empty($userfilter)) {
  33. $printuser = '';
  34. $printtags = '';
  35. if (!empty($userfilter)) {
  36. $user = get_record('user', 'username', $userfilter);
  37. $printuser = get_string('user').': '.fullname($user);
  38. }
  39. if (!empty($tagfilter)) {
  40. $printtags = get_string('tags', 'tag').': '.$tagfilter;
  41. }
  42. if (!empty($printtags) && !(empty($printuser))) {
  43. $printtags .= ', ';
  44. }
  45. $printtags .= $printuser;
  46. $printtags .= ' <a href="'.$CFG->wwwroot.'/local/mahara/taoview'.$viewtype.'.php">'.get_string('removefilters', 'local').'</a>';
  47. echo ' <div class="filteredby">'.get_string('filteredby', 'local').': '.$printtags.'</div>';
  48. foreach ($artefacts as $aid => $artefact) {
  49. if (!empty($tagfilter)) {
  50. //check to see if this artefact uses the tag mentioned in the tagfilter
  51. if (empty($artefact['tags']) || !is_array($artefact['tags'])) {
  52. unset($artefacts[$aid]);
  53. continue;
  54. }
  55. $showartefact = false; //assume this doesn't contain the right tags
  56. foreach($artefact['tags'] as $tag) {
  57. if($tag==$tagfilter) {
  58. $showartefact = true;
  59. }
  60. }
  61. if (!$showartefact) { //if this artefact doesn't contain the tagfilter, hide it!
  62. unset($artefacts[$aid]);
  63. continue;
  64. }
  65. }
  66. //now check if should be filtered by user
  67. if ((!empty($userfilter) && $userfilter <> $artefact['uploader'])) {
  68. unset($artefacts[$aid]);
  69. }
  70. }
  71. }
  72. return $artefacts;
  73. }
  74. //function to print artefacts to screen.
  75. function taoview_print_artefacts($artefacts, $viewtype, $tagfilter, $userfilter, $sort, $page, $perpage) {
  76. global $CFG,$USER;
  77. //get scale
  78. $scale = get_record("scale", "name", 'TAO: Stars');
  79. $artefacts = taoview_get_paginated_results($artefacts, $page, $perpage);
  80. foreach ($artefacts as $artefact) {
  81. if ($perpage <= 0) { //not a great way to paginate.
  82. break;
  83. }
  84. echo '<div class="taoview">';
  85. if (!empty($artefact['thumbnail'])) {
  86. echo '<div class="taoview-thumb"><img src="'.$artefact['thumbnail'].'"></div>';
  87. }
  88. echo '<div class="taoview-download"><a href="'.$artefact['download'].'" target="_blank">'.$artefact['name'].'</a></div>';
  89. if (!empty($artefact['uploader'])) {
  90. $user = get_record('user', 'username', $artefact['uploader']);
  91. if (!empty($user)) {
  92. echo '<div class="taoview-user">'.get_string('submittedby','local').': <a href="'.$CFG->wwwroot.'/local/mahara/taoview'.$viewtype.'.php?tag='.$tagfilter.'&filteruser='.$artefact['uploader'].'&sort='.$sort.'">'.fullname($user).'</a></div>';
  93. }
  94. }
  95. if (!empty($artefact['ctime'])) {
  96. echo '<div class="taoview-date">'.$artefact['ctime'].'</div>';
  97. }
  98. if (!empty($artefact['description'])) {
  99. echo '<div class="taoview-desc">'.$artefact['description'].'</div>';
  100. }
  101. if (!empty($artefact['tags']) && is_array($artefact['tags'])) {
  102. echo '<div class="taoview-tags">'.get_string('tags').': ';
  103. foreach($artefact['tags'] as $tag) {
  104. echo '<a href="'.$CFG->wwwroot.'/local/mahara/taoview'.$viewtype.'.php?tag='.$tag.'&sort='.$sort.'">'.$tag.'</a>, ';
  105. }
  106. echo '</div>';
  107. }
  108. //now do ratings stuff
  109. echo '<div class="ratings">';
  110. $possiblevalues = make_grades_menu(-$scale->id);
  111. echo '<span class="taoviewratingtext">';
  112. tao_print_ratings($artefact['id'], $possiblevalues);
  113. echo '</span>';
  114. if (!empty($user) && $user->id <> $USER->id && !isguest()) {
  115. tao_print_rating_menu($artefact['id'],$USER->id,$possiblevalues);
  116. }
  117. echo '</div>';
  118. //end of ratings stuff
  119. if (!empty($artefact['page'])) {
  120. echo '<div class="taoview-page"><a href="'.$artefact['page'].'">'.get_string('moreinfo', 'local').'</a></div>';
  121. }
  122. echo '</div>';
  123. $perpage--;
  124. }
  125. if (!empty($artefacts) && !isguest()) {
  126. echo "<div class=\"boxaligncenter\"><input id=\"taoviewratingsubmit\" type=\"submit\" value=\"".get_string("sendinratings", "local")."\" />";
  127. if (ajaxenabled()) { /// AJAX enabled, standard submission form
  128. $rate_ajax_config_settings = array("pixpath"=>$CFG->pixpath, "wwwroot"=>$CFG->wwwroot, "sesskey"=>sesskey());
  129. echo "<script type=\"text/javascript\">//<![CDATA[\n".
  130. "var rate_ajax_config = " . json_encode($rate_ajax_config_settings) . ";\n".
  131. "init_rate_ajax();\n".
  132. "//]]></script>\n";
  133. }
  134. //print_scale_menu_helpbutton(SITEID, $scale); //no help file written yet.
  135. echo "</div>";
  136. }
  137. }
  138. function taoview_print_tag_cloud($artefacts, $viewtype) {
  139. global $CFG;
  140. //first generate a list of tags and the frequency of use
  141. $tags = array();
  142. $maxcount = 0;
  143. foreach ($artefacts as $artefact) {
  144. if (!empty($artefact['tags']) && is_array($artefact['tags'])) {
  145. foreach($artefact['tags'] as $tag) {
  146. if(isset($tags[$tag])) {
  147. $tags[$tag]++;
  148. } else {
  149. $tags[$tag] = 1;
  150. }
  151. if ($tags[$tag] > $maxcount) {
  152. $maxcount = $tags[$tag];
  153. }
  154. }
  155. }
  156. }
  157. if (!empty($tags)) {
  158. ksort($tags); //sort by highest number of tags.
  159. $etags = array();
  160. foreach ($tags as $tag => $tagcount) {
  161. $tagob = new stdclass();
  162. $tagob->count = $tagcount;
  163. $tagob->name = $tag;
  164. $size = (int) (( $tagcount / $maxcount) * 20);
  165. $tagob->class = "default s$size";
  166. $etags[] = $tagob;
  167. }
  168. $output = '';
  169. $output .= '<div class="taoviewtagcloud">';
  170. $output .= '<div class="taoviewtagcloud-name">'.get_string('tags','tag').'</div>';
  171. $output .= "\n<ul class='tag_cloud inline-list'>\n";
  172. foreach ($etags as $tag) {
  173. $link = $CFG->wwwroot .'/local/mahara/taoview'.$viewtype.'.php?tag='. rawurlencode($tag->name);
  174. $output .= '<li><a href="'. $link .'" class="'. $tag->class .'" '.
  175. 'title="'. get_string('numberofentries', 'blog', $tag->count) .'">'.
  176. $tag->name .'</a></li> ';
  177. }
  178. $output .= "\n</ul>\n</div>\n";
  179. echo $output;
  180. }
  181. }
  182. function taoview_sort_artefacts($artefacts, $viewtype, $tagfilter='', $userfilter='', $sort='') {
  183. global $CFG;
  184. $returnartefacts = array();
  185. $ratings = array();
  186. echo ' <div class="sortby">'.get_string('sortby', 'local').': ';
  187. if (empty($sort) || $sort=='date') {
  188. echo get_string('date'). ' | <a href="'.$CFG->wwwroot.'/local/mahara/taoview'.$viewtype.'.php?tag='.$tagfilter.'&filteruser='.$userfilter.'&sort=rating">'.get_string('rating', 'local').'</a>';
  189. return $artefacts;
  190. } elseif ($sort=='rating') {
  191. echo '<a href="'.$CFG->wwwroot.'/local/mahara/taoview'.$viewtype.'.php?tag='.$tagfilter.'&filteruser='.$userfilter.'">'.get_string('date'). '</a> | '.get_string('rating', 'local');
  192. $scale = get_record("scale", "name", 'TAO: Stars');
  193. $scalevars = explode(',', $scale->scale);
  194. $possiblevalues = array(); //use values instead of the scale so we can order them!
  195. $i = 1;
  196. foreach($scalevars as $sc) {
  197. $possiblevalues[$i] = $i;
  198. $i++;
  199. }
  200. echo '<div class="ratings">';
  201. foreach($artefacts as $aid => $artefact) {
  202. $ratings[$aid] = tao_get_ratings_mean($artefact['id'], $possiblevalues);
  203. }
  204. arsort($ratings);
  205. foreach($ratings as $id => $rat) {
  206. $returnartefacts[] = $artefacts[$id];
  207. }
  208. }
  209. echo '</div>';
  210. return $returnartefacts;
  211. }
  212. function taoview_call_mnet($viewtype) {
  213. /// Setup MNET environment
  214. global $MNET,$CFG;
  215. if (empty($MNET)) {
  216. $MNET = new mnet_environment();
  217. $MNET->init();
  218. }
  219. /// Setup the server
  220. $host = get_record('mnet_host','name', 'localmahara'); //we retrieve the server(host) from the 'mnet_host' table
  221. if (empty($host)) {
  222. error('Mahara not configured');
  223. }
  224. $a = new stdclass();
  225. $a->link = $CFG->wwwroot.'/auth/mnet/jump.php?hostid='.$host->id.'&wantsurl=local/taoview.php?view='.$viewtype;
  226. echo '<div class="taoviwdesc">';
  227. print_string('toaddartefacts','local', $a);
  228. echo '</div>';
  229. $mnet_peer = new mnet_peer(); //we create a new mnet_peer (server/host)
  230. $mnet_peer->set_wwwroot($host->wwwroot); //we set this mnet_peer with the host http address
  231. $client = new mnet_xmlrpc_client(); //create a new client
  232. $client->set_method('local/mahara/rpclib.php/get_artefacts_by_viewtype'); //tell it which method we're going to call
  233. $client->add_param($viewtype);
  234. $client->send($mnet_peer); //Call the server
  235. if (!empty($client->response['faultString'])) {
  236. error("Mahara error:".$artefacts['faultString']);
  237. }
  238. return $client->response;
  239. }
  240. function taoview_print_artefacts_form($artefacts, $viewtype, $tagfilter, $userfilter, $sort, $page, $perpage) {
  241. if (!empty($artefacts) && is_array($artefacts)) {
  242. print_paging_bar(count($artefacts), $page, $perpage,
  243. "taoview$viewtype.php?tag=$tagfilter&amp;filteruser=$userfilter&amp;sort=$sort&amp;");
  244. echo "<form method=\"post\" action=\"rate.php\">";
  245. echo "<input type='hidden' name='viewtype' value='$viewtype'/>";
  246. taoview_print_artefacts($artefacts, $viewtype, $tagfilter, $userfilter, $sort, $page, $perpage);
  247. echo "</form>";
  248. print_paging_bar(count($artefacts), $page, $perpage,
  249. "taoview$viewtype.php?tag=$tagfilter&amp;filteruser=$userfilter&amp;sort=$sort&amp;");
  250. //now print tags cloud:
  251. taoview_print_tag_cloud($artefacts, $viewtype);
  252. } else {
  253. notify(get_string('noartefactsfound', 'local'));
  254. }
  255. }
  256. function taoview_print_view($viewtype, $tagfilter, $userfilter, $sort, $page, $perpage) {
  257. $artefacts = taoview_call_mnet($viewtype); //get Artefacts.
  258. $artefacts = taoview_filter_artefacts($artefacts, $viewtype, $tagfilter, $userfilter); //filter the artefacts
  259. $artefacts = taoview_sort_artefacts($artefacts, $viewtype, $tagfilter, $userfilter, $sort); //sort the artefacts
  260. taoview_print_artefacts_form($artefacts, $viewtype, $tagfilter, $userfilter, $sort, $page, $perpage);
  261. }
  262. function taoview_get_paginated_results($artefacts, $page, $perpage) {
  263. $artefactnum = 0;
  264. $retart = array();
  265. $skipartefacts = $page * $perpage;
  266. if (empty($artefacts)) {
  267. return array();
  268. }
  269. foreach($artefacts as $artefact) {
  270. if ($skipartefacts > 0) { //check if need to skip some artefacts.
  271. $skipartefacts--;
  272. } elseif ($artefactnum >= $perpage) { //check if have already obtained enough artefacts.
  273. break;
  274. } else {
  275. $retart[] = $artefact;
  276. $artefactnum++;
  277. }
  278. }
  279. return $retart;
  280. }
  281. ?>