MDL-59495 block_community: search courses on moodle.net only
[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   block_community
26  * @copyright 2010 Moodle Pty Ltd (http://moodle.com)
27  * @author    Jerome Mouneyrac
28  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29  */
30 class block_community_renderer extends plugin_renderer_base {
32     public function restore_confirmation_box($filename, $context) {
33         $restoreurl = new moodle_url('/backup/restore.php',
34                         array('filename' => $filename . ".mbz", 'contextid' => $context->id));
35         $searchurl = new moodle_url('/blocks/community/communitycourse.php',
36                         array('add' => 1, 'courseid' => $context->instanceid,
37                             'cancelrestore' => 1, 'sesskey' => sesskey(),
38                             'filename' => $filename));
39         $formrestore = new single_button($restoreurl,
40                         get_string('dorestore', 'block_community'));
41         $formsearch = new single_button($searchurl,
42                         get_string('donotrestore', 'block_community'));
43         return $this->output->confirm(get_string('restorecourseinfo', 'block_community'),
44                 $formrestore, $formsearch);
45     }
47     /**
48      * Display remove community success message and a button to be redirected to te referer page
49      * @param moodle_url $url the page to be redirected to
50      * @return string html
51      */
52     public function remove_success(moodle_url $url) {
53         $html = $this->output->notification(get_string('communityremoved', 'hub'),
54                     'notifysuccess');
55         $continuebutton = new single_button($url,
56                         get_string('continue', 'block_community'));
57         $html .= html_writer::tag('div', $this->output->render($continuebutton),
58                 array('class' => 'continuebutton'));
59         return $html;
60     }
62     /**
63      * Display add community course success message and a button to be redirected to te referer page
64      * @param moodle_url $url the page to be redirected to
65      * @return string html
66      */
67     public function save_link_success(moodle_url $url) {
68         $html = $this->output->notification(get_string('addedtoblock', 'block_community'),
69                     'notifysuccess');
70         $continuebutton = new single_button($url,
71                         get_string('continue', 'block_community'));
72         $html .= html_writer::tag('div', $this->output->render($continuebutton),
73                 array('class' => 'continuebutton'));
74         return $html;
75     }
77     /**
78      * The 'Next'/'more course result' link for a courses search
79      * @param array $data - the form parameter to execute the search on more result
80      * @return string html code
81      */
82     public function next_button($data) {
83         $nextlink = html_writer::tag('a', get_string('next', 'block_community'),
84                 array('href' => new moodle_url('', $data)));
85         return html_writer::tag('div', $nextlink, array( 'class' => 'nextlink'));
86     }
88     /**
89      * Displays information about moodle.net above course search form
90      *
91      * @return string
92      */
93     public function moodlenet_info() {
94         if (!$info = \core\hub\registration::get_moodlenet_info()) {
95             return '';
96         }
98         $image = html_writer::div(html_writer::img($info['imgurl'], $info['name']), 'hubimage');
100         $namelink = html_writer::link($info['url'], html_writer::tag('h2', $info['name']), array('class' => 'hubtitlelink'));
101         $description = clean_param($info['description'], PARAM_TEXT);
102         $descriptiontext = html_writer::div(format_text($description, FORMAT_PLAIN), 'hubdescription');
104         $additionaldesc = get_string('enrollablecourses', 'block_community') . ': ' . $info['enrollablecourses'] . ' - ' .
105             get_string('downloadablecourses', 'block_community') . ': ' . $info['downloadablecourses'];
106         $stats = html_writer::div(html_writer::tag('div', $additionaldesc), 'hubstats');
108         $text = html_writer::div($descriptiontext . $stats, 'hubtext');
110         $imgandtext = html_writer::div($image . $text, 'hubimgandtext');
112         $fulldesc = html_writer::div($namelink . $imgandtext, 'hubmainhmtl clearfix');
114         return html_writer::div($fulldesc, 'formlisting');
115     }
117     /**
118      * Display a list of courses
119      * @param array $courses
120      * @param mixed $unused parameter is not used
121      * @param int $contextcourseid context course id
122      * @return string
123      */
124     public function course_list($courses, $unused, $contextcourseid) {
125         global $CFG;
127         $renderedhtml = '';
129         if (empty($courses)) {
130             if (isset($courses)) {
131                 $renderedhtml .= get_string('nocourse', 'block_community');
132             }
133         } else {
134             $courseiteration = 0;
135             foreach ($courses as $course) {
136                 $course = (object) $course;
137                 $courseiteration = $courseiteration + 1;
139                 //create visit link html
140                 if (!empty($course->courseurl)) {
141                     $courseurl = new moodle_url($course->courseurl);
142                     $linktext = get_string('visitsite', 'block_community');
143                 } else {
144                     $courseurl = new moodle_url($course->demourl);
145                     $linktext = get_string('visitdemo', 'block_community');
146                 }
148                 $visitlinkhtml = html_writer::tag('a', $linktext,
149                                 array('href' => $courseurl, 'class' => 'hubcoursedownload',
150                                     'onclick' => 'this.target="_blank"'));
152                 //create title html
153                 $coursename = html_writer::tag('h3', $course->fullname,
154                                 array('class' => 'hubcoursetitle'));
155                 $coursenamehtml = html_writer::tag('div', $coursename,
156                         array('class' => 'hubcoursetitlepanel'));
158                 // create screenshots html
159                 $screenshothtml = '';
160                 if (!empty($course->screenshotbaseurl)) {
161                     $screenshothtml = html_writer::empty_tag('img',
162                         array('src' => $course->screenshotbaseurl, 'alt' => $course->fullname));
163                 }
164                 $coursescreenshot = html_writer::tag('div', $screenshothtml,
165                                 array('class' => 'coursescreenshot',
166                                     'id' => 'image-' . $course->id));
168                 //create description html
169                 $deschtml = html_writer::tag('div', $course->description,
170                                 array('class' => 'hubcoursedescription'));
172                 //create users related information html
173                 $courseuserinfo = get_string('userinfo', 'block_community', $course);
174                 if ($course->contributornames) {
175                     $courseuserinfo .= ' - ' . get_string('contributors', 'block_community',
176                                     $course->contributornames);
177                 }
178                 $courseuserinfohtml = html_writer::tag('div', $courseuserinfo,
179                                 array('class' => 'hubcourseuserinfo'));
181                 //create course content related information html
182                 $course->subject = (get_string_manager()->string_exists($course->subject, 'edufields')) ?
183                         get_string($course->subject, 'edufields') : get_string('none');
184                 $course->audience = get_string('audience' . $course->audience, 'hub');
185                 $course->educationallevel = get_string('edulevel' . $course->educationallevel, 'hub');
186                 $coursecontentinfo = '';
187                 if (empty($course->coverage)) {
188                     $course->coverage = '';
189                 } else {
190                     $coursecontentinfo .= get_string('coverage', 'block_community', $course->coverage);
191                     $coursecontentinfo .= ' - ';
192                 }
193                 $coursecontentinfo .= get_string('contentinfo', 'block_community', $course);
194                 $coursecontentinfohtml = html_writer::tag('div', $coursecontentinfo,
195                                 array('class' => 'hubcoursecontentinfo'));
197                 ///create course file related information html
198                 //language
199                 if (!empty($course->language)) {
200                     $languages = get_string_manager()->get_list_of_languages();
201                     $course->lang = $languages[$course->language];
202                 } else {
203                     $course->lang = '';
204                 }
205                 //licence
206                 require_once($CFG->libdir . "/licenselib.php");
207                 $licensemanager = new license_manager();
208                 $licenses = $licensemanager->get_licenses();
209                 foreach ($licenses as $license) {
210                     if ($license->shortname == $course->licenceshortname) {
211                         $course->license = $license->fullname;
212                     }
213                 }
214                 $course->timeupdated = userdate($course->timemodified);
215                 $coursefileinfo = get_string('fileinfo', 'block_community', $course);
216                 $coursefileinfohtml = html_writer::tag('div', $coursefileinfo,
217                                 array('class' => 'hubcoursefileinfo'));
221                 //Create course content html
222                 $blocks = core_component::get_plugin_list('block');
223                 $activities = core_component::get_plugin_list('mod');
224                 if (!empty($course->contents)) {
225                     $activitieshtml = '';
226                     $blockhtml = '';
227                     foreach ($course->contents as $content) {
228                         $content = (object) $content;
229                         if ($content->moduletype == 'block') {
230                             if (!empty($blockhtml)) {
231                                 $blockhtml .= ' - ';
232                             }
233                             if (array_key_exists($content->modulename, $blocks)) {
234                                 $blockname = get_string('pluginname', 'block_' . $content->modulename);
235                             } else {
236                                 $blockname = $content->modulename;
237                             }
238                             $blockhtml .= $blockname . " (" . $content->contentcount . ")";
239                         } else {
240                             if (!empty($activitieshtml)) {
241                                 $activitieshtml .= ' - ';
242                             }
243                             if (array_key_exists($content->modulename, $activities)) {
244                                 $activityname = get_string('modulename', $content->modulename);
245                             } else {
246                                 $activityname = $content->modulename;
247                             }
248                             $activitieshtml .= $activityname . " (" . $content->contentcount . ")";
249                         }
250                     }
252                     $blocksandactivities = html_writer::tag('div',
253                                     get_string('activities', 'block_community') . " : " . $activitieshtml);
255                     //Uncomment following lines to display blocks information
256 //                    $blocksandactivities .= html_writer::tag('span',
257 //                                    get_string('blocks', 'block_community') . " : " . $blockhtml);
258                 }
260                 //Create outcomes html
261                 $outcomes= '';
262                 if (!empty($course->outcomes)) {
263                     foreach ($course->outcomes as $outcome) {
264                         if (!empty($outcomes)) {
265                             $outcomes .= ', ';
266                         }
267                         $outcomes .= $outcome['fullname'];
268                     }
269                     $outcomes = get_string('outcomes', 'block_community',
270                             $outcomes);
271                 }
272                 $outcomeshtml = html_writer::tag('div', $outcomes, array('class' => 'hubcourseoutcomes'));
274                 //create additional information html
275                 $additionaldesc = $courseuserinfohtml . $coursecontentinfohtml
276                         . $coursefileinfohtml . $blocksandactivities . $outcomeshtml;
277                 $additionaldeschtml = html_writer::tag('div', $additionaldesc,
278                                 array('class' => 'additionaldesc'));
280                 //Create add button html
281                 $addbuttonhtml = "";
282                 if ($course->enrollable) {
283                     $params = array('sesskey' => sesskey(), 'add' => 1, 'confirmed' => 1,
284                         'coursefullname' => $course->fullname, 'courseurl' => $courseurl,
285                         'coursedescription' => $course->description,
286                         'courseid' => $contextcourseid);
287                     $addurl = new moodle_url("/blocks/community/communitycourse.php", $params);
288                     $addbuttonhtml = html_writer::tag('a',
289                                     get_string('addtocommunityblock', 'block_community'),
290                                     array('href' => $addurl, 'class' => 'centeredbutton, hubcoursedownload'));
291                 }
293                 //create download button html
294                 $downloadbuttonhtml = "";
295                 if (!$course->enrollable) {
296                     $params = array('sesskey' => sesskey(), 'download' => 1, 'confirmed' => 1,
297                         'remotemoodleurl' => $CFG->wwwroot, 'courseid' => $contextcourseid,
298                         'downloadcourseid' => $course->id,
299                         'coursefullname' => $course->fullname, 'backupsize' => $course->backupsize);
300                     $downloadurl = new moodle_url("/blocks/community/communitycourse.php", $params);
301                     $downloadbuttonhtml = html_writer::tag('a', get_string('install', 'block_community'),
302                                     array('href' => $downloadurl, 'class' => 'centeredbutton, hubcoursedownload'));
303                 }
305                 //Create rating html
306                 $rating = html_writer::tag('div', get_string('noratings', 'block_community'),
307                                 array('class' => 'norating'));
308                 if (!empty($course->rating)) {
309                     $course->rating = (object) $course->rating;
310                     if ($course->rating->count > 0) {
312                         //calculate size of the rating star
313                         $starimagesize = 20; //in px
314                         $numberofstars = 5;
315                         $size = ($course->rating->aggregate / $course->rating->scaleid)
316                                 * $numberofstars * $starimagesize;
317                         $rating = html_writer::tag('li', '',
318                                         array('class' => 'current-rating',
319                                             'style' => 'width:' . $size . 'px;'));
321                         $rating = html_writer::tag('ul', $rating,
322                                         array('class' => 'star-rating clearfix'));
323                         $rating .= html_writer::tag('div', ' (' . $course->rating->count . ')',
324                                         array('class' => 'ratingcount clearfix'));
325                     }
326                 }
329                 //Create comments html
330                 $coursecomments = html_writer::tag('div', get_string('nocomments', 'block_community'),
331                                 array('class' => 'nocomments'));
332                 $commentcount = 0;
333                 if (!empty($course->comments)) {
334                     //display only if there is some comment if there is some comment
335                     $commentcount = count($course->comments);
336                     $coursecomments = html_writer::tag('div',
337                                     get_string('comments', 'block_community', $commentcount),
338                                     array('class' => 'commenttitle'));
340                     foreach ($course->comments as $comment) {
341                         $commentator = html_writer::tag('div',
342                                         $comment['commentator'],
343                                         array('class' => 'hubcommentator'));
344                         $commentdate = html_writer::tag('div',
345                                         ' - ' . userdate($comment['date'], '%e/%m/%y'),
346                                         array('class' => 'hubcommentdate clearfix'));
348                         $commenttext = html_writer::tag('div',
349                                         $comment['comment'],
350                                         array('class' => 'hubcommenttext'));
352                         $coursecomments .= html_writer::tag('div',
353                                         $commentator . $commentdate . $commenttext,
354                                         array('class' => 'hubcomment'));
355                     }
356                     $coursecommenticon = html_writer::tag('div',
357                                     get_string('comments', 'block_community', $commentcount),
358                                     array('class' => 'hubcoursecomments',
359                                         'id' => 'comments-' . $course->id));
360                     $coursecomments = $coursecommenticon . html_writer::tag('div',
361                                     $coursecomments,
362                                     array('class' => 'yui3-overlay-loading',
363                                         'id' => 'commentoverlay-' . $course->id));
364                 }
366                 //link rate and comment
367                 $rateandcomment = html_writer::tag('div',
368                                 html_writer::link($course->commenturl, get_string('rateandcomment', 'block_community'),
369                                             ['onclick' => 'this.target="_blank"']),
370                                 array('class' => 'hubrateandcomment'));
372                 //the main DIV tags
373                 $buttonsdiv = html_writer::tag('div',
374                                 $addbuttonhtml . $downloadbuttonhtml . $visitlinkhtml,
375                                 array('class' => 'courseoperations'));
376                 $screenshotbuttonsdiv = html_writer::tag('div',
377                                 $coursescreenshot . $buttonsdiv,
378                                 array('class' => 'courselinks'));
380                 $coursedescdiv = html_writer::tag('div',
381                                 $deschtml . $additionaldeschtml
382                                 . $rating . $coursecomments . $rateandcomment,
383                                 array('class' => 'coursedescription'));
384                 $coursehtml =
385                         $coursenamehtml . html_writer::tag('div',
386                                 $coursedescdiv . $screenshotbuttonsdiv,
387                                 array('class' => 'hubcourseinfo clearfix'));
389                 $renderedhtml .=html_writer::tag('div', $coursehtml,
390                                 array('class' => 'fullhubcourse clearfix'));
391             }
393             $renderedhtml = html_writer::tag('div', $renderedhtml,
394                             array('class' => 'hubcourseresult'));
395         }
397         return $renderedhtml;
398     }