set_parameters($params); } /** * @return bool */ public function create_user_folder() { //COURSE123/exercises/session_id/exercise_id/question_id/user_id if (empty($this->store_path)) { return false; } //@todo use an array to create folders $folders_to_create = array(); // Trying to create the courses/COURSE123/exercises/ dir just in case. $directoryPermissions = api_get_permissions_for_new_directories(); if (!is_dir($this->store_path)) { mkdir($this->store_path, $directoryPermissions); } if (!is_dir($this->store_path.$this->session_id)) { mkdir($this->store_path.$this->session_id, $directoryPermissions); } if (!is_dir($this->store_path.$this->session_id.'/'.$this->exercise_id)) { mkdir($this->store_path.$this->session_id.'/'.$this->exercise_id, $directoryPermissions); } if (!is_dir($this->store_path.$this->session_id.'/'.$this->exercise_id.'/'.$this->question_id) ) { mkdir($this->store_path.$this->session_id.'/'.$this->exercise_id.'/'.$this->question_id, $directoryPermissions); } if (!empty($this->user_id) && !is_dir($this->store_path.$this->session_id.'/'.$this->exercise_id.'/'.$this->question_id.'/'.$this->user_id) ) { mkdir($this->store_path.$this->session_id.'/'.$this->exercise_id.'/'.$this->question_id.'/'.$this->user_id, $directoryPermissions); } } /** * Setting parameters: course id, session id, etc * @param array */ public function set_parameters($params = array()) { //Setting course id if (isset($params['course_id'])) { $this->course_id = intval($params['course_id']); } else { $this->course_id = $params['course_id'] = api_get_course_int_id(); } //Setting course info if (isset($this->course_id)) { $this->course_info = api_get_course_info_by_id($this->course_id); } //Setting session id if (isset($params['session_id'])) { $this->session_id = intval($params['session_id']); } else { $this->session_id = $params['session_id'] = api_get_session_id(); } //Setting user ids if (isset($params['user_id'])) { $this->user_id = intval($params['user_id']); } else { $this->user_id = $params['user_id'] = api_get_user_id(); } //Setting user ids if (isset($params['exercise_id'])) { $this->exercise_id = intval($params['exercise_id']); } else { $this->exercise_id = 0; } //Setting user ids if (isset($params['question_id'])) { $this->question_id = intval($params['question_id']); } else { $this->question_id = 0; } $this->can_edit = false; if (api_is_allowed_to_edit()) { $this->can_edit = true; } else { if ($this->user_id == api_get_user_id()) { $this->can_edit = true; } } //Settings the params array $this->params = $params; $this->store_path = api_get_path(SYS_COURSE_PATH).$this->course_info['path'].'/exercises/'; $this->create_user_folder(); $this->store_path = $this->store_path.implode('/', array($this->session_id, $this->exercise_id, $this->question_id, $this->user_id)).'/'; $this->filename = $this->generate_filename(); $this->store_filename = $this->store_path.$this->filename; } /** * Generates the filename with the next format: * (course_id)/(session_id)/(user_id)/(exercise_id)/(question_id)/(exe_id) * * @return string */ public function generate_filename() { if (!empty($this->params)) { //filename //course_id/session_id/user_id/exercise_id/question_id/exe_id $filename_array = array( $this->params['course_id'], $this->params['session_id'], $this->params['user_id'], $this->params['exercise_id'], $this->params['question_id'], $this->params['exe_id'] ); return implode('-', $filename_array); } else { return api_get_unique_id(); } } /** * Delete audio file * @return int */ public function delete_files() { $delete_found = 0; if ($this->can_edit) { $file = $this->load_filename_if_exists(); $path_info = pathinfo($file); foreach ($this->available_extensions as $extension) { $file_to_delete = $path_info['dirname'].'/'.$path_info['filename'].'.'.$extension; if (is_file($file_to_delete)) { unlink($file_to_delete); $delete_found = 1; } } } return $delete_found; } /** * * Tricky stuff to deal with the feedback = 0 in exercises (all question per page) * @param int $exe_id */ public function replace_with_real_exe($exe_id) { $filename = null; //@ugly fix foreach($this->available_extensions as $extension) { $items = explode('-', $this->filename); $items[5] = 'temp_exe'; $filename = implode('-', $items); if (is_file($this->store_path.$filename.'.'.$extension)) { $old_name = $this->store_path.$filename.'.'.$extension; $items = explode('-', $this->filename); $items[5] = $exe_id; $filename = $filename = implode('-', $items); $new_name = $this->store_path.$filename.'.'.$extension; rename($old_name, $new_name); break; } } } /** * @param bool $load_from_database * @return null|string */ public function load_filename_if_exists($load_from_database = false) { $filename = null; //@ugly fix foreach($this->available_extensions as $extension) { if (is_file($this->store_path.$this->filename.'.'.$extension)) { $filename = $this->filename.'.'.$extension; break; } } // temp_exe if ($load_from_database) { //Load the real filename just if exists if (isset($this->params['exe_id']) && isset($this->params['user_id']) && isset($this->params['question_id']) && isset($this->params['session_id']) && isset($this->params['course_id']) ) { $attempt_table = Database::get_statistic_table(TABLE_STATISTIC_TRACK_E_ATTEMPT); $sql = "SELECT filename FROM $attempt_table WHERE exe_id = ".$this->params['exe_id']." AND user_id = ".$this->params['user_id']." AND question_id = ".$this->params['question_id']." AND session_id = ".$this->params['session_id']." AND course_code = '".$this->course_info['code']."' LIMIT 1"; $result = Database::query($sql); $result = Database::fetch_row($result,'ASSOC'); if (isset($result) && isset($result[0]) && !empty($result[0])) { $filename = $result[0]; } } } if (is_file($this->store_path.$filename)) { return $this->store_path.$filename; } return null; } /** * * Get the URL of the file * path courses/XXX/exercises/(session_id)/(exercise_id)/(question_id)/(user_id)/ * @param int $force_download * * @return string */ public function get_public_url($force_download = 0) { //$params = $this->get_params(true); //$url = api_get_path(WEB_AJAX_PATH).'nanogong.ajax.php?a=get_file&download='.$force_download.'&'.$params; $params = $this->get_params(); $filename = basename($this->load_filename_if_exists()); $url = api_get_path(WEB_COURSE_PATH).$this->course_info['path'].'/exercises/'.$params['session_id'].'/'.$params['exercise_id'].'/'.$params['question_id'].'/'.$params['user_id'].'/'.$filename; return $url; } /** * Uploads the nanogong wav file * @param bool */ public function upload_file($is_nano = false) { require_once api_get_path(LIBRARY_PATH).'fileDisplay.lib.php'; require_once api_get_path(LIBRARY_PATH).'document.lib.php'; require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php'; if (!empty($_FILES)) { $upload_ok = process_uploaded_file($_FILES['file'], false); if (!is_uploaded_file($_FILES['file']['tmp_name'])) { return 0; } if ($upload_ok) { // Check if there is enough space to save the file if (!DocumentManager::enough_space($_FILES['file']['size'], DocumentManager::get_course_quota())) { return 0; } //first we delete everything before uploading the file $this->delete_files(); //Reload the filename variable $file_name = add_ext_on_mime($_FILES['file']['name'], $_FILES['file']['type']); $file_name = strtolower($file_name); $file_info = pathinfo($file_name); if ($is_nano == true) { $file_info['extension'] = 'wav'; } $file_name = $this->filename.'.'.$file_info['extension']; if (in_array($file_info['extension'], $this->available_extensions)) { if (move_uploaded_file($_FILES['file']['tmp_name'], $this->store_path.$file_name)) { $this->store_filename = $this->store_path.$file_name; return 1; } } } } return 0; } /** * Show the audio file + a button to download * @param bool */ public function show_audio_file($show_delete_button = false) { $html = ''; $file_path = $this->load_filename_if_exists(); if (!empty($file_path)) { $url = $this->get_public_url(true); $actions = Display::url(Display::return_icon('save.png', get_lang('Download'), array(), ICON_SIZE_SMALL), $url, array('target'=>'_blank')); $download_button = Display::url(get_lang('Download'), $url, array('class' =>'btn')); if ($show_delete_button) { $actions .= ' '.Display::url(Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL), "#", array('onclick'=>'delete_file();')); } $basename = basename($file_path); $path_info = pathinfo($basename); if ($path_info['extension'] == 'wav') { $html .= ''; $html .= '