community hub MDL-24097 change backup zip extension to mbz extension
[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      * Display a list of courses
50      * @param array $courses
51      * @param boolean $withwriteaccess
52      * @param int $contextcourseid context course id
53      * @return string
54      */
55     public function course_list($courses, $huburl, $contextcourseid) {
56         global $CFG;
58         $renderedhtml = '';
60         if (empty($courses)) {
61             if (isset($courses)) {
62                 $renderedhtml .= get_string('nocourse', 'block_community');
63             }
64         } else {
65             $courseiteration = 0;
66             foreach ($courses as $course) {
67                 $course = (object) $course;
68                 $courseiteration = $courseiteration + 1;
70                 //create visit link html
71                 if (!empty($course->courseurl)) {
72                     $courseurl = new moodle_url($course->courseurl);
73                     $linktext = get_string('visitsite', 'block_community');
74                 } else {
75                     $courseurl = new moodle_url($course->demourl);
76                     $linktext = get_string('visitdemo', 'block_community');
77                 }
79                 $visitlinkhtml = html_writer::tag('a', $linktext,
80                                 array('href' => $courseurl, 'class' => 'hubcoursedownload'));
82                 //create title html
83                 $coursename = html_writer::tag('h3', $course->fullname,
84                                 array('class' => 'hubcoursetitle'));
85                 $coursenamehtml = html_writer::tag('div', $coursename, array());
87                 // create screenshots html
88                 $screenshothtml = '';
90                 if (!empty($course->screenshots)) {
91                     $images = array();
92                     $baseurl = new moodle_url($huburl . '/local/hub/webservice/download.php',
93                                     array('courseid' => $course->id,
94                                         'filetype' => HUB_SCREENSHOT_FILE_TYPE));
95                     for ($i = 1; $i <= $course->screenshots; $i = $i + 1) {
96                         $params['screenshotnumber'] = $i;
97                         $images[] = array(
98                             'thumburl' => new moodle_url($baseurl, array('screenshotnumber' => $i)),
99                             'imageurl' => new moodle_url($baseurl,
100                                     array('screenshotnumber' => $i, 'imagewidth' => 'original')),
101                             'title' => $course->fullname,
102                             'alt' => $course->fullname
103                         );
104                     }
105                     $imagegallery = new image_gallery($images, $course->shortname);
106                     $imagegallery->displayfirstimageonly = true;
107                     $screenshothtml = $this->output->render($imagegallery);
108                 }
109                 $coursescreenshot = html_writer::tag('div', $screenshothtml,
110                                 array('class' => 'coursescreenshot'));
113                 //create description html
114                 $deschtml = html_writer::tag('div', $course->description,
115                                 array('class' => 'hubcoursedescription'));
117                 //create users related information html
118                 $courseuserinfo = '';
119                 if (!empty($course->contributornames)) {
120                     $course->contributorname = get_string('contributors', 'block_community',
121                                     $course->contributorname);
122                 }
123                 if ($course->contributornames) {
124                     $courseuserinfo .= get_string('contributors', 'block_community',
125                                     $course->contributornames);
126                     $courseuserinfo .= ' - ';
127                 }
128                 $courseuserinfo = get_string('userinfo', 'block_community', $course);
129                 $courseuserinfohtml = html_writer::tag('div', $courseuserinfo,
130                                 array('class' => 'hubcourseuserinfo'));
132                 //create course content related information html
133                 $course->subject = get_string($course->subject, 'edufields');
134                 $course->audience = get_string('audience' . $course->audience, 'hub');
135                 $course->educationallevel = get_string('edulevel' . $course->educationallevel, 'hub');
136                 $coursecontentinfo = '';
137                 if (empty($course->coverage)) {
138                     $course->coverage = '';
139                 } else {
140                     $coursecontentinfo .= get_string('coverage', 'block_community', $course->coverage);
141                     $coursecontentinfo .= ' - ';
142                 }
143                 $coursecontentinfo .= get_string('contentinfo', 'block_community', $course);
144                 $coursecontentinfohtml = html_writer::tag('div', $coursecontentinfo,
145                                 array('class' => 'hubcoursecontentinfo'));
147                 ///create course file related information html
148                 //language
149                 if (!empty($course->language)) {
150                     $languages = get_string_manager()->get_list_of_languages();
151                     $course->lang = $languages[$course->language];
152                 } else {
153                     $course->lang = '';
154                 }
155                 //licence
156                 require_once($CFG->dirroot . "/lib/licenselib.php");
157                 $licensemanager = new license_manager();
158                 $licenses = $licensemanager->get_licenses();
159                 foreach ($licenses as $license) {
160                     if ($license->shortname == $course->licenceshortname) {
161                         $course->license = $license->fullname;
162                     }
163                 }
164                 $course->timeupdated = userdate($course->timemodified);
165                 $coursefileinfo = get_string('fileinfo', 'block_community', $course);
166                 $coursefileinfohtml = html_writer::tag('div', $coursefileinfo,
167                                 array('class' => 'hubcoursefileinfo'));
171                 //Create course content html
172                 if (!empty($course->contents)) {
173                     $activitieshtml = '';
174                     $blockhtml = '';
175                     foreach ($course->contents as $content) {
176                         $content = (object) $content;
177                         if ($content->moduletype == 'block') {
178                             if (!empty($blockhtml)) {
179                                 $blockhtml .= ' - ';
180                             }
181                             $blockhtml .= get_string('pluginname', 'block_' . $content->modulename)
182                                     . " (" . $content->contentcount . ")";
183                         } else {
184                             if (!empty($activitieshtml)) {
185                                 $activitieshtml .= ' - ';
186                             }
187                             $activitieshtml .= get_string('modulename', $content->modulename)
188                                     . " (" . $content->contentcount . ")";
189                         }
190                     }
192                     $blocksandactivities = html_writer::tag('div',
193                                     get_string('activities', 'block_community') . " : " . $activitieshtml);
195                     //Uncomment following lines to display blocks information
196 //                    $blocksandactivities .= html_writer::tag('span',
197 //                                    get_string('blocks', 'block_community') . " : " . $blockhtml);
198                 }
200                 //create additional information html
201                 $additionaldesc = $courseuserinfohtml . $coursecontentinfohtml
202                         . $coursefileinfohtml . $blocksandactivities;
203                 $additionaldeschtml = html_writer::tag('div', $additionaldesc,
204                                 array('class' => 'additionaldesc'));
206                 //Create add button html
207                 $addbuttonhtml = "";
208                 if ($course->enrollable) {
209                     $params = array('sesskey' => sesskey(), 'add' => 1, 'confirmed' => 1,
210                         'coursefullname' => $course->fullname, 'courseurl' => $courseurl,
211                         'coursedescription' => $course->description,
212                         'courseid' => $contextcourseid);
213                     $addurl = new moodle_url("/blocks/community/communitycourse.php", $params);
214                     $addbuttonhtml = html_writer::tag('a',
215                                     get_string('addtocommunityblock', 'block_community'),
216                                     array('href' => $addurl, 'class' => 'centeredbutton, hubcoursedownload'));
217                 }
219                 //create download button html
220                 $downloadbuttonhtml = "";
221                 if (!$course->enrollable) {
222                     $params = array('sesskey' => sesskey(), 'download' => 1, 'confirmed' => 1,
223                         'remotemoodleurl' => $CFG->wwwroot, 'courseid' => $contextcourseid,
224                         'downloadcourseid' => $course->id, 'huburl' => $huburl,
225                         'coursefullname' => $course->fullname);
226                     $downloadurl = new moodle_url("/blocks/community/communitycourse.php", $params);
227                     $downloadbuttonhtml = html_writer::tag('a', get_string('download', 'block_community'),
228                                     array('href' => $downloadurl, 'class' => 'centeredbutton, hubcoursedownload'));
229                 }
231                 //Create rating html
232                 $rating = html_writer::tag('div', get_string('noratings', 'block_community'),
233                                 array('class' => 'norating'));
234                 if (!empty($course->rating)) {
235                     $course->rating = (object) $course->rating;
236                     if ($course->rating->count > 0) {
238                         //calculate size of the rating star
239                         $starimagesize = 20; //in px
240                         $numberofstars = 5;
241                         $size = ($course->rating->aggregate / $course->rating->scaleid)
242                                 * $numberofstars * $starimagesize;
243                         $rating = html_writer::tag('li', '',
244                                         array('class' => 'current-rating',
245                                             'style' => 'width:' . $size . 'px;'));
247                         $rating = html_writer::tag('ul', $rating ,
248                                         array('class' => 'star-rating clearfix'));
249                         $rating .= html_writer::tag('div', ' (' . $course->rating->count . ')' ,
250                                         array('class' => 'ratingcount clearfix'));;
251                     }
252                 }
255                 //Create comments html
256                 $coursecomments = html_writer::tag('div', get_string('nocomments', 'block_community'),
257                                 array('class' => 'nocomments'));
258                 $commentcount = 0;
259                 if (!empty($course->comments)) {
260                     //display only if there is some comment if there is some comment
261                     $commentcount = count($course->comments);
262                     $coursecomments = html_writer::tag('div',
263                             get_string('comments', 'block_community', $commentcount),
264                             array('class'=>'commenttitle'));;
266                     foreach ($course->comments as $comment) {
267                         $commentator = html_writer::tag('div',
268                                         $comment['commentator'],
269                                         array('class' => 'hubcommentator'));
270                         $commentdate = html_writer::tag('div',
271                                         ' - ' . userdate($comment['date'], '%e/%m/%y'),
272                                         array('class' => 'hubcommentdate clearfix'));
273                      
274                         $commenttext = html_writer::tag('div',
275                                         $comment['comment'],
276                                         array('class' => 'hubcommenttext'));
277                     
278                         $coursecomments .= html_writer::tag('div',
279                                         $commentator . $commentdate . $commenttext,
280                                         array('class' => 'hubcomment'));
281                     }
282                     $coursecommenticon = html_writer::tag('div',
283                                     get_string('comments', 'block_community', $commentcount),
284                                     array('class' => 'hubcoursecomments',
285                                         'id' => 'comments-' . $course->id));
286                     $coursecomments = $coursecommenticon . html_writer::tag('div',
287                                     $coursecomments,
288                                     array('class' => 'yui3-overlay-loading',
289                                         'id' => 'commentoverlay-' . $course->id)); 
290                 }
292                 //the main DIV tags
293                 $buttonsdiv = html_writer::tag('div',
294                                 $addbuttonhtml . $downloadbuttonhtml . $visitlinkhtml,
295                                 array('class' => 'courseoperations'));
296                 $screenshotbuttonsdiv = html_writer::tag('div',
297                                 $coursescreenshot . $buttonsdiv,
298                                 array('class' => 'courselinks'));
300                 $coursedescdiv = html_writer::tag('div',
301                                 $deschtml . $additionaldeschtml
302                                 . $rating . $coursecomments,
303                                 array('class' => 'coursedescription'));
304                 $coursehtml =
305                         $coursenamehtml . html_writer::tag('div',
306                                 $coursedescdiv . $screenshotbuttonsdiv,
307                                 array('class' => 'hubcourseinfo clearfix'));
309                 $renderedhtml .=html_writer::tag('div', $coursehtml,
310                                 array('class' => 'fullhubcourse clearfix'));
311             }
313             $renderedhtml = html_writer::tag('div', $renderedhtml,
314                             array('class' => 'hubcourseresult'));
315         }
317         return $renderedhtml;
318     }