e5e054205e736d89e3bed97721ac8181356f1989
[moodle.git] / blocks / community / renderer.php
1 <?php
3 ///////////////////////////////////////////////////////////////////////////
4 //                                                                       //
5 // This file is part of Moodle - http://moodle.org/                      //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
7 //                                                                       //
8 // Moodle is free software: you can redistribute it and/or modify        //
9 // it under the terms of the GNU General Public License as published by  //
10 // the Free Software Foundation, either version 3 of the License, or     //
11 // (at your option) any later version.                                   //
12 //                                                                       //
13 // Moodle is distributed in the hope that it will be useful,             //
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
16 // GNU General Public License for more details.                          //
17 //                                                                       //
18 // You should have received a copy of the GNU General Public License     //
19 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.       //
20 //                                                                       //
21 ///////////////////////////////////////////////////////////////////////////
23 /**
24  * Block community renderer.
25  * @package    blocks
26  * @subpackage community
27  * @copyright 2010 Moodle Pty Ltd (http://moodle.com)
28  * @author    Jerome Mouneyrac
29  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
30  */
31 class block_community_renderer extends plugin_renderer_base {
33     public function restore_confirmation_box($filename, $context) {
34         $restoreurl = new moodle_url('/backup/restore.php',
35                         array('filename' => $filename . ".mbz", 'contextid' => $context->id));
36         $searchurl = new moodle_url('/blocks/community/communitycourse.php',
37                         array('add' => 1, 'courseid' => $context->instanceid,
38                             'cancelrestore' => 1, 'sesskey' => sesskey(),
39                             'filename' => $filename));
40         $formrestore = new single_button($restoreurl,
41                         get_string('dorestore', 'block_community'));
42         $formsearch = new single_button($searchurl,
43                         get_string('donotrestore', 'block_community'));
44         return $this->output->confirm(get_string('restorecourseinfo', 'block_community'),
45                 $formrestore, $formsearch);
46     }
48     /**
49      * The 'Next'/'more course result' link for a courses search
50      * @param array $data - the form parameter to execute the search on more result
51      * @return string html code
52      */
53     public function next_button($data) {
54         $nextlink = html_writer::tag('a', get_string('next', 'block_community'),
55                 array('href' => new moodle_url('', $data)));
56         return html_writer::tag('div', $nextlink, array( 'class' => 'nextlink'));
57     }
59     /**
60      * Display a list of courses
61      * @param array $courses
62      * @param boolean $withwriteaccess
63      * @param int $contextcourseid context course id
64      * @return string
65      */
66     public function course_list($courses, $huburl, $contextcourseid) {
67         global $CFG;
69         $renderedhtml = '';
71         if (empty($courses)) {
72             if (isset($courses)) {
73                 $renderedhtml .= get_string('nocourse', 'block_community');
74             }
75         } else {
76             $courseiteration = 0;
77             foreach ($courses as $course) {
78                 $course = (object) $course;
79                 $courseiteration = $courseiteration + 1;
81                 //create visit link html
82                 if (!empty($course->courseurl)) {
83                     $courseurl = new moodle_url($course->courseurl);
84                     $linktext = get_string('visitsite', 'block_community');
85                 } else {
86                     $courseurl = new moodle_url($course->demourl);
87                     $linktext = get_string('visitdemo', 'block_community');
88                 }
90                 $visitlinkhtml = html_writer::tag('a', $linktext,
91                                 array('href' => $courseurl, 'class' => 'hubcoursedownload',
92                                     'onclick' => 'this.target="_blank"'));
94                 //create title html
95                 $coursename = html_writer::tag('h3', $course->fullname,
96                                 array('class' => 'hubcoursetitle'));
97                 $coursenamehtml = html_writer::tag('div', $coursename, array());
99                 // create screenshots html
100                 $screenshothtml = '';
102                 if (!empty($course->screenshots)) {
103                     $images = array();
104                     $baseurl = new moodle_url($huburl . '/local/hub/webservice/download.php',
105                                     array('courseid' => $course->id,
106                                         'filetype' => HUB_SCREENSHOT_FILE_TYPE));
107                     for ($i = 1; $i <= $course->screenshots; $i = $i + 1) {
108                         $params['screenshotnumber'] = $i;
109                         $images[] = array(
110                             'thumburl' => new moodle_url($baseurl, array('screenshotnumber' => $i)),
111                             'imageurl' => new moodle_url($baseurl,
112                                     array('screenshotnumber' => $i, 'imagewidth' => 'original')),
113                             'title' => $course->fullname,
114                             'alt' => $course->fullname
115                         );
116                     }
117                     $imagegallery = new image_gallery($images, $course->shortname);
118                     $imagegallery->displayfirstimageonly = true;
119                     $screenshothtml = $this->output->render($imagegallery);
120                 }
121                 $coursescreenshot = html_writer::tag('div', $screenshothtml,
122                                 array('class' => 'coursescreenshot'));
125                 //create description html
126                 $deschtml = html_writer::tag('div', $course->description,
127                                 array('class' => 'hubcoursedescription'));
129                 //create users related information html
130                 $courseuserinfo = get_string('userinfo', 'block_community', $course);
131                 if ($course->contributornames) {
132                     $courseuserinfo .= ' - ' . get_string('contributors', 'block_community',
133                                     $course->contributornames);
134                 }
135                 $courseuserinfohtml = html_writer::tag('div', $courseuserinfo,
136                                 array('class' => 'hubcourseuserinfo'));
138                 //create course content related information html
139                 $course->subject = get_string($course->subject, 'edufields');
140                 $course->audience = get_string('audience' . $course->audience, 'hub');
141                 $course->educationallevel = get_string('edulevel' . $course->educationallevel, 'hub');
142                 $coursecontentinfo = '';
143                 if (empty($course->coverage)) {
144                     $course->coverage = '';
145                 } else {
146                     $coursecontentinfo .= get_string('coverage', 'block_community', $course->coverage);
147                     $coursecontentinfo .= ' - ';
148                 }
149                 $coursecontentinfo .= get_string('contentinfo', 'block_community', $course);
150                 $coursecontentinfohtml = html_writer::tag('div', $coursecontentinfo,
151                                 array('class' => 'hubcoursecontentinfo'));
153                 ///create course file related information html
154                 //language
155                 if (!empty($course->language)) {
156                     $languages = get_string_manager()->get_list_of_languages();
157                     $course->lang = $languages[$course->language];
158                 } else {
159                     $course->lang = '';
160                 }
161                 //licence
162                 require_once($CFG->libdir . "/licenselib.php");
163                 $licensemanager = new license_manager();
164                 $licenses = $licensemanager->get_licenses();
165                 foreach ($licenses as $license) {
166                     if ($license->shortname == $course->licenceshortname) {
167                         $course->license = $license->fullname;
168                     }
169                 }
170                 $course->timeupdated = userdate($course->timemodified);
171                 $coursefileinfo = get_string('fileinfo', 'block_community', $course);
172                 $coursefileinfohtml = html_writer::tag('div', $coursefileinfo,
173                                 array('class' => 'hubcoursefileinfo'));
177                 //Create course content html
178                 if (!empty($course->contents)) {
179                     $activitieshtml = '';
180                     $blockhtml = '';
181                     foreach ($course->contents as $content) {
182                         $content = (object) $content;
183                         if ($content->moduletype == 'block') {
184                             if (!empty($blockhtml)) {
185                                 $blockhtml .= ' - ';
186                             }
187                             $blockhtml .= get_string('pluginname', 'block_' . $content->modulename)
188                                     . " (" . $content->contentcount . ")";
189                         } else {
190                             if (!empty($activitieshtml)) {
191                                 $activitieshtml .= ' - ';
192                             }
193                             $activitieshtml .= get_string('modulename', $content->modulename)
194                                     . " (" . $content->contentcount . ")";
195                         }
196                     }
198                     $blocksandactivities = html_writer::tag('div',
199                                     get_string('activities', 'block_community') . " : " . $activitieshtml);
201                     //Uncomment following lines to display blocks information
202 //                    $blocksandactivities .= html_writer::tag('span',
203 //                                    get_string('blocks', 'block_community') . " : " . $blockhtml);
204                 }
206                 //Create outcomes html
207                 $outcomes= '';
208                 if (!empty($course->outcomes)) {
209                     foreach ($course->outcomes as $outcome) {
210                         if (!empty($outcomes)) {
211                             $outcomes .= ', ';
212                         }
213                         $outcomes .= $outcome['fullname'];
214                     }
215                     $outcomes = get_string('outcomes', 'block_community',
216                             $outcomes);
217                 }
218                 $outcomeshtml = html_writer::tag('div', $outcomes, array('class' => 'hubcourseoutcomes'));
220                 //create additional information html
221                 $additionaldesc = $courseuserinfohtml . $coursecontentinfohtml
222                         . $coursefileinfohtml . $blocksandactivities . $outcomeshtml;
223                 $additionaldeschtml = html_writer::tag('div', $additionaldesc,
224                                 array('class' => 'additionaldesc'));
226                 //Create add button html
227                 $addbuttonhtml = "";
228                 if ($course->enrollable) {
229                     $params = array('sesskey' => sesskey(), 'add' => 1, 'confirmed' => 1,
230                         'coursefullname' => $course->fullname, 'courseurl' => $courseurl,
231                         'coursedescription' => $course->description,
232                         'courseid' => $contextcourseid);
233                     $addurl = new moodle_url("/blocks/community/communitycourse.php", $params);
234                     $addbuttonhtml = html_writer::tag('a',
235                                     get_string('addtocommunityblock', 'block_community'),
236                                     array('href' => $addurl, 'class' => 'centeredbutton, hubcoursedownload'));
237                 }
239                 //create download button html
240                 $downloadbuttonhtml = "";
241                 if (!$course->enrollable) {
242                     $params = array('sesskey' => sesskey(), 'download' => 1, 'confirmed' => 1,
243                         'remotemoodleurl' => $CFG->wwwroot, 'courseid' => $contextcourseid,
244                         'downloadcourseid' => $course->id, 'huburl' => $huburl,
245                         'coursefullname' => $course->fullname);
246                     $downloadurl = new moodle_url("/blocks/community/communitycourse.php", $params);
247                     $downloadbuttonhtml = html_writer::tag('a', get_string('download', 'block_community'),
248                                     array('href' => $downloadurl, 'class' => 'centeredbutton, hubcoursedownload'));
249                 }
251                 //Create rating html
252                 $rating = html_writer::tag('div', get_string('noratings', 'block_community'),
253                                 array('class' => 'norating'));
254                 if (!empty($course->rating)) {
255                     $course->rating = (object) $course->rating;
256                     if ($course->rating->count > 0) {
258                         //calculate size of the rating star
259                         $starimagesize = 20; //in px
260                         $numberofstars = 5;
261                         $size = ($course->rating->aggregate / $course->rating->scaleid)
262                                 * $numberofstars * $starimagesize;
263                         $rating = html_writer::tag('li', '',
264                                         array('class' => 'current-rating',
265                                             'style' => 'width:' . $size . 'px;'));
267                         $rating = html_writer::tag('ul', $rating,
268                                         array('class' => 'star-rating clearfix'));
269                         $rating .= html_writer::tag('div', ' (' . $course->rating->count . ')',
270                                         array('class' => 'ratingcount clearfix'));
271                     }
272                 }
275                 //Create comments html
276                 $coursecomments = html_writer::tag('div', get_string('nocomments', 'block_community'),
277                                 array('class' => 'nocomments'));
278                 $commentcount = 0;
279                 if (!empty($course->comments)) {
280                     //display only if there is some comment if there is some comment
281                     $commentcount = count($course->comments);
282                     $coursecomments = html_writer::tag('div',
283                                     get_string('comments', 'block_community', $commentcount),
284                                     array('class' => 'commenttitle'));
286                     foreach ($course->comments as $comment) {
287                         $commentator = html_writer::tag('div',
288                                         $comment['commentator'],
289                                         array('class' => 'hubcommentator'));
290                         $commentdate = html_writer::tag('div',
291                                         ' - ' . userdate($comment['date'], '%e/%m/%y'),
292                                         array('class' => 'hubcommentdate clearfix'));
294                         $commenttext = html_writer::tag('div',
295                                         $comment['comment'],
296                                         array('class' => 'hubcommenttext'));
298                         $coursecomments .= html_writer::tag('div',
299                                         $commentator . $commentdate . $commenttext,
300                                         array('class' => 'hubcomment'));
301                     }
302                     $coursecommenticon = html_writer::tag('div',
303                                     get_string('comments', 'block_community', $commentcount),
304                                     array('class' => 'hubcoursecomments',
305                                         'id' => 'comments-' . $course->id));
306                     $coursecomments = $coursecommenticon . html_writer::tag('div',
307                                     $coursecomments,
308                                     array('class' => 'yui3-overlay-loading',
309                                         'id' => 'commentoverlay-' . $course->id));
310                 }
312                 //link rate and comment
313                 $rateandcomment = html_writer::tag('div',
314                                 html_writer::tag('a', get_string('rateandcomment', 'block_community'),
315                                         array('href' => new moodle_url($huburl,
316                                                     array('courseid' => $course->id, 'mustbelogged' => true)),
317                                             'onclick' => 'this.target="_blank"')),
318                                 array('class' => 'hubrateandcomment'));
320                 //the main DIV tags
321                 $buttonsdiv = html_writer::tag('div',
322                                 $addbuttonhtml . $downloadbuttonhtml . $visitlinkhtml,
323                                 array('class' => 'courseoperations'));
324                 $screenshotbuttonsdiv = html_writer::tag('div',
325                                 $coursescreenshot . $buttonsdiv,
326                                 array('class' => 'courselinks'));
328                 $coursedescdiv = html_writer::tag('div',
329                                 $deschtml . $additionaldeschtml
330                                 . $rating . $coursecomments . $rateandcomment,
331                                 array('class' => 'coursedescription'));
332                 $coursehtml =
333                         $coursenamehtml . html_writer::tag('div',
334                                 $coursedescdiv . $screenshotbuttonsdiv,
335                                 array('class' => 'hubcourseinfo clearfix'));
337                 $renderedhtml .=html_writer::tag('div', $coursehtml,
338                                 array('class' => 'fullhubcourse clearfix'));
339             }
341             $renderedhtml = html_writer::tag('div', $renderedhtml,
342                             array('class' => 'hubcourseresult'));
343         }
345         return $renderedhtml;
346     }