[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/blocks/community/ -> locallib.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Community library
  19   *
  20   * @package    block_community
  21   * @author     Jerome Mouneyrac <[email protected]>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
  23   * @copyright  (C) 1999 onwards Martin Dougiamas  http://dougiamas.com
  24   *
  25   *
  26   */
  27  
  28  class block_community_manager {
  29  
  30      /**
  31       * Add a community course
  32       * @param object $course
  33       * @param integer $userid
  34       * @return id of course or false if already added
  35       */
  36      public function block_community_add_course($course, $userid) {
  37          global $DB;
  38  
  39          $community = $this->block_community_get_course($course->url, $userid);
  40  
  41          if (empty($community)) {
  42              $community = new stdClass();
  43              $community->userid = $userid;
  44              $community->coursename = $course->name;
  45              $community->coursedescription = $course->description;
  46              $community->courseurl = $course->url;
  47              $community->imageurl = $course->imageurl;
  48              return $DB->insert_record('block_community', $community);
  49          } else {
  50              return false;
  51          }
  52      }
  53  
  54      /**
  55       * Return all community courses of a user
  56       * @param integer $userid
  57       * @return array of course
  58       */
  59      public function block_community_get_courses($userid) {
  60          global $DB;
  61          return $DB->get_records('block_community', array('userid' => $userid), 'coursename');
  62      }
  63  
  64      /**
  65       * Return a community courses of a user
  66       * @param integer $userid
  67       * @param integer $userid
  68       * @return array of course
  69       */
  70      public function block_community_get_course($courseurl, $userid) {
  71          global $DB;
  72          return $DB->get_record('block_community',
  73                  array('courseurl' => $courseurl, 'userid' => $userid));
  74      }
  75  
  76      /**
  77       * Download the community course backup and save it in file API
  78       * @param integer $courseid
  79       * @param string $huburl
  80       * @return array 'privatefile' the file name saved in private area
  81       *               'tmpfile' the file name saved in the moodledata temp dir (for restore)
  82       */
  83      public function block_community_download_course_backup($course) {
  84          global $CFG, $USER;
  85          require_once($CFG->libdir . "/filelib.php");
  86          require_once($CFG->dirroot. "/course/publish/lib.php");
  87  
  88          $params['courseid'] = $course->id;
  89          $params['filetype'] = HUB_BACKUP_FILE_TYPE;
  90  
  91          make_temp_directory('backup');
  92  
  93          $filename = md5(time() . '-' . $course->id . '-'. $USER->id . '-'. random_string(20));
  94  
  95          $url  = new moodle_url($course->huburl.'/local/hub/webservice/download.php', $params);
  96          $path = $CFG->tempdir.'/backup/'.$filename.".mbz";
  97          $fp = fopen($path, 'w');
  98          $curlurl = $course->huburl.'/local/hub/webservice/download.php?filetype='
  99                  .HUB_BACKUP_FILE_TYPE.'&courseid='.$course->id;
 100  
 101          //send an identification token if the site is registered on the hub
 102          require_once($CFG->dirroot . '/' . $CFG->admin . '/registration/lib.php');
 103          $registrationmanager = new registration_manager();
 104          $registeredhub = $registrationmanager->get_registeredhub($course->huburl);
 105          if (!empty($registeredhub)) {
 106              $token = $registeredhub->token;
 107              $curlurl .= '&token='.$token;
 108          }
 109  
 110          $ch = curl_init($curlurl);
 111          curl_setopt($ch, CURLOPT_FILE, $fp);
 112          $data = curl_exec($ch);
 113          curl_close($ch);
 114          fclose($fp);
 115  
 116          $fs = get_file_storage();
 117          $record = new stdClass();
 118          $record->contextid = context_user::instance($USER->id)->id;
 119          $record->component = 'user';
 120          $record->filearea = 'private';
 121          $record->itemid = 0;
 122          $record->filename = urlencode($course->fullname)."_".time().".mbz";
 123          $record->filepath = '/downloaded_backup/';
 124          if (!$fs->file_exists($record->contextid, $record->component,
 125                  $record->filearea, 0, $record->filepath, $record->filename)) {
 126              $fs->create_file_from_pathname($record,
 127                      $CFG->tempdir.'/backup/'.$filename.".mbz");
 128          }
 129  
 130          $filenames = array();
 131          $filenames['privatefile'] = $record->filename;
 132          $filenames['tmpfile'] = $filename;
 133          return $filenames;
 134      }
 135  
 136      /**
 137       * Delete a community course
 138       * @param integer $communityid
 139       * @param integer $userid
 140       * @return bool true
 141       */
 142      public function block_community_remove_course($communityid, $userid) {
 143          global $DB, $USER;
 144          return $DB->delete_records('block_community',
 145                  array('userid' => $userid, 'id' => $communityid));
 146      }
 147  
 148  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1